mirror of https://github.com/Chizi123/Arch-autobuild-repo.git

Chizi123
2020-10-15 ca0d8a2ab391bf8dd1a40635d8755ab428bb1f4c
commit | author | age
8ea4ef 1 #!/bin/bash
f6854c 2 #A basic bash script to automate the building of arch packages
JG 3 # Usage: main.sh init|add|build_all [-f force]
8ea4ef 4
e6f0af 5 source $(dirname "$(realpath $0)")/vars.sh
8ea4ef 6
JG 7 #Helper for finding newest and oldest files
8 #Sourced from stack overflow
f6854c 9 # Usage: newold_matching_file [n/o] [filename]
8ea4ef 10 function newold_matching_file
JG 11 {
12     # Use ${1-} instead of $1 in case 'nounset' is set
13     local -r glob_pattern=${2-}
14
15     # To avoid printing garbage if no files match the pattern, set
16     # 'nullglob' if necessary
17     local -i need_to_unset_nullglob=0
18     if [[ ":$BASHOPTS:" != *:nullglob:* ]] ; then
19         shopt -s nullglob
20         need_to_unset_nullglob=1
21     fi
22
23     file=
24     for f in $glob_pattern ; do
25         if [ $1 == "n" ]; then
26             [[ -z $f || $f -nt $_file ]] && file=$f
27         elif [ $1 == "o" ]; then
28             [[ -z $f || $f -ot $_file ]] && file=$f
29         fi
30     done
31
32     # To avoid unexpected behaviour elsewhere, unset nullglob if it was
33     # set by this function
34     (( need_to_unset_nullglob )) && shopt -u nullglob
35
36     # Use printf instead of echo in case the file name begins with '-'
37     [[ -n $file ]] && printf '%s\n' "$file"
38
39     return 0
40 }
41
42 #Build latest version of a package
9837c8 43 # Usage: build_pkg [package name] [-f force]
8ea4ef 44 function build_pkg {
JG 45     #check if PKGBUILD has updated, don't rebuild if hasn't changed
46     if [[ ! -z $(git pull | grep "Already up to date.") && -z $(echo $1 | grep git) && -z $2 ]]; then
47         return 2
48     fi
f6854c 49
JG 50     #remove old versions before build
735bae 51     rm *$1*
f6854c 52
JG 53     #make and force rebuild if is git package
9837c8 54     # Mictosoft fonts have problems with checksums and need a seperate argument
C 55     if [[ "$1" == "ttf-ms-win10" ||
56         "$1" == "ttf-office-2007-fonts" ||
57         "$1" == "ttf-ms-win8" ||
58         "$1" == "ttf-win7-fonts" ]]; then
59         makepkg -s --noconfirm $([[ $CLEAN == "Y" ]] && echo "-c") $([[ $SIGN == "Y" ]] && echo "--sign --key $KEY") $([[ "$2" == "-f" ]] && echo -f) --skipchecksums
60     else
e74aea 61         makepkg -s --noconfirm $([[ $CLEAN == "Y" ]] && echo "-c") $([[ $SIGN == "Y" ]] && echo "--sign --key $KEY") $([[ "$2" == "-f" ]] && echo -f) 2>&1
9837c8 62     fi
C 63     if [[ $? != 0 ]]; then
8ea4ef 64         #Register error
e6f0af 65         echo $1 >> $REPODIR/.errors
8ea4ef 66         return 1
JG 67     fi
68
88a91e 69     #Get build artifact names
JG 70     source PKGBUILD
71     pkgs=()
72     for i in ${pkgname[@]}; do
b2c2b5 73         #pkgs+=("$i-$pkgver-$pkgrel")
735bae 74         pkgs+=("$(find . -mindepth 1 -maxdepth 1 -type f -name "$1*.tar.*" -not -name "*.sig" | sed 's/^\.\///')")
88a91e 75     done
JG 76
e72b38 77     #Move package to repodir and add to repo db
d4b7a7 78     #Dont change the database if rebuilt the same package at same release and version
88a91e 79     for i in ${pkgs[@]}; do
d4b7a7 80         if [[ -f $REPODIR/$i ]]; then
C 81             pkgs=${pkgs[@]/$i}
82         else
735bae 83             rm $REPODIR/*$1*
d4b7a7 84             cp $i $REPODIR/
C 85             [[ "$SIGN" == "Y" ]] && cp $i.sig $REPODIR/
86         fi
88a91e 87     done
9837c8 88
C 89     # Weird exceptions
90     if [[ "$1" == "zoom" ]]; then
91         rm zoom*_orig*.pkg.tar.xz
92     fi
93
e72b38 94     # Add package to waiting list to be added to repo db
9837c8 95     while true; do
C 96         if [[ $(cat $REPODIR/.waitlist.lck) == 1 ]]; then
e6f0af 97             sleep 1
4ebf3d 98         else
e6f0af 99             echo 1 > $REPODIR/.waitlist.lck
C 100             echo $1 >> $REPODIR/.waitlist
101             echo 0 > $REPODIR/.waitlist.lck
102             break
103             fi
104     done
9837c8 105     while true; do
e72b38 106         # Wait until package is at the top of the queue and add to db
9837c8 107         if [[ "$(head -n1 $REPODIR/.waitlist)" == "$1" ]]; then
88a91e 108             for i in ${pkgs[@]}; do
735bae 109                 repo-add $([[ "$SIGN" == "Y" ]] && echo "--sign --key $KEY") $REPODIR/$REPONAME.db.tar.$([ -n COMPRESSION ] || echo $COMPRESSION && echo zst) $REPODIR/$i
88a91e 110             done
9837c8 111             while true; do
C 112                 if [[ $(cat $REPODIR/.waitlist.lck) == 1 ]]; then
e6f0af 113                     sleep 1
4ebf3d 114                 else
e72b38 115                     # Remove self from top of queue
e6f0af 116                     echo 1 > $REPODIR/.waitlist.lck
C 117                     tail -n +2 $REPODIR/.waitlist > $REPODIR/.waitlist.tmp
e72b38 118                     mv $REPODIR/.waitlist.tmp $REPODIR/.waitlist
e6f0af 119                     echo 0 > $REPODIR/.waitlist.lck
C 120                     break
121                 fi
122             done
123             break
124         else
e74aea 125             if [[ -z "$(grep $1 $REPODIR/.waitlist)" ]]; then
b2c2b5 126                 # Not on waitlist for some reason, need to readd
e74aea 127                 if [[ $(cat $REPODIR/.waitlist.lck) == 1 ]]; then
C 128                     sleep 1
129                 else
130                     echo 1 > $REPODIR/.waitlist.lck
131                     echo $1 >> $REPODIR/.waitlist
132                     echo 0 > $REPODIR/.waitlist.lck
133                 fi
134             fi
e6f0af 135             sleep 10
4ebf3d 136         fi
e6f0af 137     done
8ea4ef 138
JG 139     #Remove old versions of packages
f6854c 140     #TODO: Want to be able to keep multiple versions of old packages, future work
JG 141     #Currently old package versions stay in the repodir indefinately
142     # while [ $NUM_OLD \< $(find . -name '*.pkg.tar.xz' | wc -l) ]
143     # do
144     #     old=$(newold_matching_file o '*.pkg.tar.xz')
145     #     rm $REPODIR/$old $old
146     # done
8ea4ef 147     return 0
JG 148 }
149
150 #Update packages in BUILDDIR
f6854c 151 # Usage: build_all [-f force]
8ea4ef 152 function build_all {
JG 153     #system update
9837c8 154     if [[ $UPDATE == "Y" ]]; then
8ea4ef 155         sudo pacman -Syu --noconfirm
JG 156     fi
ac333f 157
e74aea 158     #Remove waitlist and errors from old builds
C 159     rm -f $REPODIR/{.waitlist,.errors}
8ea4ef 160     #update every package currently stored
c3d80e 161     for d in $(find $BUILDDIR -maxdepth 1 -mindepth 1 -type d)
8ea4ef 162     do
JG 163         cd $d
9837c8 164         if [[ "$PARALLEL" == "Y" ]]; then
C 165             build_pkg $(echo $d | rev | cut -d'/' -f1 | rev) $1 &> $([[ "$QUIET" == "Y" ]] && echo "/dev/null" || echo "/dev/tty")  &
166         else
167             build_pkg $(echo $d | rev | cut -d'/' -f1 | rev) $1 &> $([[ "$QUIET" == "Y" ]] && echo "/dev/null" || echo "/dev/tty")
168         fi
8ea4ef 169     done
e6f0af 170     wait
8ea4ef 171
JG 172     return 0
173 }
174
175 #Add a new package to be built
ca0d8a 176 #Adding build dependencies is 
f6854c 177 # Usage: add [package name]
8ea4ef 178 function add {
67ef73 179     for i in $@; do
JG 180         cd $BUILDDIR
ca0d8a 181         if [[ -z $(git ls-remote https://aur.archlinux.org/$i.git) ]]; then
C 182             echo "Not a package"
183             exit 2
184         fi
67ef73 185         git clone https://aur.archlinux.org/$i.git
JG 186         cd $i
ca0d8a 187
C 188         #check for all build dependencies
189         for i in ${makedepends[@]}; do
190             if pacman -Si $i; then 
191                 makedepends=${makedepends[@]/$delete}
192             fi &>/dev/null
193         done
194         for i in ${makedepends[@]}; do
195             add $i
196         done
197         sudo pacman -Sy
198         
199         #Actually build wanted package
9837c8 200         build_pkg $i -f
67ef73 201     done
8ea4ef 202     return 0
c3d80e 203 }
C 204
205 #Remove a package from the build list and repository
206 # Usage remove [package name]
207 function remove {
67ef73 208     for i in $@; do
JG 209         rm -rf $BUILDDIR/$i*
210         repo-remove $REPODIR/$REPONAME.db.tar.xz $i
211         rm $REPODIR/$i*
212     done
8ea4ef 213 }
JG 214
215 #Check config and create build folders
f6854c 216 #Set variables before usage
JG 217 # Usage: init
8ea4ef 218 function init {
9837c8 219     if [[ $uid != 1 ]]; then
4ebf3d 220         echo "This must be run as root"
JG 221     fi
222
8ea4ef 223     #check for configuration here
9837c8 224     [[ -z $REPODIR ]] && echo "Enter REPODIR" && return 1
C 225     [[ -z $BUILDDIR ]] && echo "Enter BUILDDIR" && return 2
226     [[ -z $REPONAME ]] && echo "Enter REPONAME" && return 3
8ea4ef 227
JG 228     #make build directories
9837c8 229     [[ ! -d $REPODIR ]] && mkdir -p $REPODIR
C 230     [[ ! -d $BUILDDIR ]] && mkdir -p $BUILDDIR
8ea4ef 231
JG 232     #packages required to build others
4ebf3d 233     pacman -S --noconfirm base-devel git
8ea4ef 234
JG 235     #add repo to pacman.conf so can install own packages
9837c8 236     if [[ -z $(grep "$REPONAME" /etc/pacman.conf) ]]; then
8ea4ef 237         printf "[$REPONAME]\nSigLevel = Optional TrustAll\nServer = file://$REPODIR\n" >> /etc/pacman.conf
JG 238     fi
239
240     #create GPG key for package signing
4ebf3d 241     if [[ "$SIGN" == "Y" && "$KEY" == "" ]]; then
8ea4ef 242         (
JG 243             echo "Key-Type: RSA"
244             echo "Key-Length: 2048"
245             echo "Subkey-Type: RSA"
246             echo "Subkey-Length: 2048"
247             echo "Passphrase: \"\""
248             echo "Expire-Date: 0"
249             echo "Name-Real: John Doe"
250             echo "Name-Comment: Arch buildbot"
251             echo "Name-Email: $(whoami)@localhost"
252             echo "%commit"
253         ) | gpg --batch --generate-key
254         gpg --export --output $REPONAME.key --armor "John Doe"
255         gpg --export-secret-keys --output $REPONAME.secret.key --armor "John Doe"
256         echo "Please change the key information in this file"
257     fi
258
259     return 0
260 }
261
86d762 262 function send_email {
C 263     (
264     echo "From: build@localhost"
265     echo "To: $EMAIL"
266     echo "Subject: Build errors"
e6f0af 267     echo "There were build errors for the build of $REPONAME at $(date), please address them soon."
be70fd 268     echo "The errors were: $@"
86d762 269     ) | sendmail -t
C 270 }
271
8ea4ef 272 case $1 in
JG 273     "init")
274         init;;
275     "add")
9837c8 276         add ${@:2};;
8ea4ef 277     "build-all")
9837c8 278         build_all $([[ "$2" == "-f" ]] && echo "-f");;
c3d80e 279     "remove")
9837c8 280         remove ${@:2};;
8ea4ef 281     *)
f11bfd 282         printf "Invalid usage\nUsage: $0 init|add|build-all\n";;
8ea4ef 283 esac
9837c8 284
C 285 # Error reporting, send email only for build-all as assuming an batch job for that
286 if [[ -f $REPODIR/.errors ]]; then
287     ERRORS=$(cat $REPODIR/.errors | tr '\n' ' ')
288     rm $REPODIR/.errors
289     echo "Errors in packages: $ERRORS"
290     if [[ "$EMAIL" != "" && "$1" == "build-all" ]]; then
291         send_email $ERRORS
292     fi
293 else
294     echo "All packages built successfully"
295 fi
296