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

Joel Grunbaum
2020-10-16 e5257b81f5651586a6cc5c3e31995b5ab325f9ae
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 {
7c4087 12     # Use ${1-} instead of $1 in case 'nounset' is set
JG 13     local -r glob_pattern=${2-}
8ea4ef 14
7c4087 15     # To avoid printing garbage if no files match the pattern, set
JG 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
8ea4ef 22
7c4087 23     file=
JG 24     for f in $glob_pattern ; do
8ea4ef 25         if [ $1 == "n" ]; then
JG 26             [[ -z $f || $f -nt $_file ]] && file=$f
27         elif [ $1 == "o" ]; then
28             [[ -z $f || $f -ot $_file ]] && file=$f
29         fi
7c4087 30     done
8ea4ef 31
7c4087 32     # To avoid unexpected behaviour elsewhere, unset nullglob if it was
JG 33     # set by this function
34     (( need_to_unset_nullglob )) && shopt -u nullglob
8ea4ef 35
7c4087 36     # Use printf instead of echo in case the file name begins with '-'
JG 37     [[ -n $file ]] && printf '%s\n' "$file"
8ea4ef 38
7c4087 39     return 0
8ea4ef 40 }
JG 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
3b9f12 51     rm -f *$1*.tar.*
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
3b9f12 83             rm -f $REPODIR/*$1*.tar.*
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
7c4087 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
7c4087 144     #    old=$(newold_matching_file o '*.pkg.tar.xz')
JG 145     #    rm $REPODIR/$old $old
f6854c 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
7c4087 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
7c4087 190             if pacman -Si $i; then
ca0d8a 191                 makedepends=${makedepends[@]/$delete}
C 192             fi &>/dev/null
193         done
194         for i in ${makedepends[@]}; do
195             add $i
196         done
3b9f12 197         if [[ -n "${makedepends[@]}" ]]; then
C 198             sudo pacman -Sy
199         fi
200
ca0d8a 201         #Actually build wanted package
9837c8 202         build_pkg $i -f
67ef73 203     done
8ea4ef 204     return 0
c3d80e 205 }
C 206
207 #Remove a package from the build list and repository
208 # Usage remove [package name]
209 function remove {
67ef73 210     for i in $@; do
3b9f12 211         rm -rf $BUILDDIR/$i
e5257b 212         repo-remove $([[ "$SIGN" == "Y" ]] && echo "--sign --key $KEY") $REPODIR/$REPONAME.db.tar.$([ -n "$COMPRESSION" ] || echo $COMPRESSION && echo zst) $i
3b9f12 213         rm -f $REPODIR/*$i*
67ef73 214     done
8ea4ef 215 }
JG 216
217 #Check config and create build folders
f6854c 218 #Set variables before usage
JG 219 # Usage: init
8ea4ef 220 function init {
9837c8 221     if [[ $uid != 1 ]]; then
4ebf3d 222         echo "This must be run as root"
JG 223     fi
224
8ea4ef 225     #check for configuration here
9837c8 226     [[ -z $REPODIR ]] && echo "Enter REPODIR" && return 1
C 227     [[ -z $BUILDDIR ]] && echo "Enter BUILDDIR" && return 2
228     [[ -z $REPONAME ]] && echo "Enter REPONAME" && return 3
8ea4ef 229
JG 230     #make build directories
9837c8 231     [[ ! -d $REPODIR ]] && mkdir -p $REPODIR
C 232     [[ ! -d $BUILDDIR ]] && mkdir -p $BUILDDIR
8ea4ef 233
JG 234     #packages required to build others
4ebf3d 235     pacman -S --noconfirm base-devel git
8ea4ef 236
JG 237     #add repo to pacman.conf so can install own packages
9837c8 238     if [[ -z $(grep "$REPONAME" /etc/pacman.conf) ]]; then
8ea4ef 239         printf "[$REPONAME]\nSigLevel = Optional TrustAll\nServer = file://$REPODIR\n" >> /etc/pacman.conf
JG 240     fi
241
242     #create GPG key for package signing
4ebf3d 243     if [[ "$SIGN" == "Y" && "$KEY" == "" ]]; then
8ea4ef 244         (
JG 245             echo "Key-Type: RSA"
246             echo "Key-Length: 2048"
247             echo "Subkey-Type: RSA"
248             echo "Subkey-Length: 2048"
249             echo "Passphrase: \"\""
250             echo "Expire-Date: 0"
251             echo "Name-Real: John Doe"
252             echo "Name-Comment: Arch buildbot"
253             echo "Name-Email: $(whoami)@localhost"
254             echo "%commit"
255         ) | gpg --batch --generate-key
256         gpg --export --output $REPONAME.key --armor "John Doe"
257         gpg --export-secret-keys --output $REPONAME.secret.key --armor "John Doe"
258         echo "Please change the key information in this file"
259     fi
260
261     return 0
262 }
263
86d762 264 function send_email {
C 265     (
266     echo "From: build@localhost"
267     echo "To: $EMAIL"
268     echo "Subject: Build errors"
e6f0af 269     echo "There were build errors for the build of $REPONAME at $(date), please address them soon."
be70fd 270     echo "The errors were: $@"
86d762 271     ) | sendmail -t
C 272 }
273
8ea4ef 274 case $1 in
JG 275     "init")
276         init;;
277     "add")
9837c8 278         add ${@:2};;
8ea4ef 279     "build-all")
9837c8 280         build_all $([[ "$2" == "-f" ]] && echo "-f");;
c3d80e 281     "remove")
9837c8 282         remove ${@:2};;
8ea4ef 283     *)
f11bfd 284         printf "Invalid usage\nUsage: $0 init|add|build-all\n";;
8ea4ef 285 esac
9837c8 286
C 287 # Error reporting, send email only for build-all as assuming an batch job for that
288 if [[ -f $REPODIR/.errors ]]; then
289     ERRORS=$(cat $REPODIR/.errors | tr '\n' ' ')
290     rm $REPODIR/.errors
291     echo "Errors in packages: $ERRORS"
292     if [[ "$EMAIL" != "" && "$1" == "build-all" ]]; then
293         send_email $ERRORS
294     fi
295 else
296     echo "All packages built successfully"
297 fi