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

Joel Grunbaum
2020-10-15 a2d95e38d1bbaf3b84f4a725412e4f6bd7de206d
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
9837c8 51     rm *$1*.pkg.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")
d4b7a7 74         pkgs+=("$(find . -mindepth 1 -maxdepth 1 -type f -name "$1*.pkg.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
83             rm $REPODIR/*$1*.pkg.tar.*
84             cp $i $REPODIR/
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
d4b7a7 109                 repo-add $([[ "$SIGN" == "Y" ]] && echo "--sign --key $KEY") $REPODIR/$REPONAME.db.tar.xz $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
f6854c 176 #There is no name checking so be sure to put in the name correctly
JG 177 # Usage: add [package name]
8ea4ef 178 function add {
67ef73 179     for i in $@; do
JG 180         cd $BUILDDIR
181         git clone https://aur.archlinux.org/$i.git
182         cd $i
9837c8 183         build_pkg $i -f
67ef73 184     done
8ea4ef 185     return 0
c3d80e 186 }
C 187
188 #Remove a package from the build list and repository
189 # Usage remove [package name]
190 function remove {
67ef73 191     for i in $@; do
JG 192         rm -rf $BUILDDIR/$i*
193         repo-remove $REPODIR/$REPONAME.db.tar.xz $i
194         rm $REPODIR/$i*
195     done
8ea4ef 196 }
JG 197
198 #Check config and create build folders
f6854c 199 #Set variables before usage
JG 200 # Usage: init
8ea4ef 201 function init {
9837c8 202     if [[ $uid != 1 ]]; then
4ebf3d 203         echo "This must be run as root"
JG 204     fi
205
8ea4ef 206     #check for configuration here
9837c8 207     [[ -z $REPODIR ]] && echo "Enter REPODIR" && return 1
C 208     [[ -z $BUILDDIR ]] && echo "Enter BUILDDIR" && return 2
209     [[ -z $REPONAME ]] && echo "Enter REPONAME" && return 3
8ea4ef 210
JG 211     #make build directories
9837c8 212     [[ ! -d $REPODIR ]] && mkdir -p $REPODIR
C 213     [[ ! -d $BUILDDIR ]] && mkdir -p $BUILDDIR
8ea4ef 214
JG 215     #packages required to build others
4ebf3d 216     pacman -S --noconfirm base-devel git
8ea4ef 217
JG 218     #add repo to pacman.conf so can install own packages
9837c8 219     if [[ -z $(grep "$REPONAME" /etc/pacman.conf) ]]; then
8ea4ef 220         printf "[$REPONAME]\nSigLevel = Optional TrustAll\nServer = file://$REPODIR\n" >> /etc/pacman.conf
JG 221     fi
222
223     #create GPG key for package signing
4ebf3d 224     if [[ "$SIGN" == "Y" && "$KEY" == "" ]]; then
8ea4ef 225         (
JG 226             echo "Key-Type: RSA"
227             echo "Key-Length: 2048"
228             echo "Subkey-Type: RSA"
229             echo "Subkey-Length: 2048"
230             echo "Passphrase: \"\""
231             echo "Expire-Date: 0"
232             echo "Name-Real: John Doe"
233             echo "Name-Comment: Arch buildbot"
234             echo "Name-Email: $(whoami)@localhost"
235             echo "%commit"
236         ) | gpg --batch --generate-key
237         gpg --export --output $REPONAME.key --armor "John Doe"
238         gpg --export-secret-keys --output $REPONAME.secret.key --armor "John Doe"
239         echo "Please change the key information in this file"
240     fi
241
242     return 0
243 }
244
86d762 245 function send_email {
C 246     (
247     echo "From: build@localhost"
248     echo "To: $EMAIL"
249     echo "Subject: Build errors"
e6f0af 250     echo "There were build errors for the build of $REPONAME at $(date), please address them soon."
be70fd 251     echo "The errors were: $@"
86d762 252     ) | sendmail -t
C 253 }
254
8ea4ef 255 case $1 in
JG 256     "init")
257         init;;
258     "add")
9837c8 259         add ${@:2};;
8ea4ef 260     "build-all")
9837c8 261         build_all $([[ "$2" == "-f" ]] && echo "-f");;
c3d80e 262     "remove")
9837c8 263         remove ${@:2};;
8ea4ef 264     *)
f11bfd 265         printf "Invalid usage\nUsage: $0 init|add|build-all\n";;
8ea4ef 266 esac
9837c8 267
C 268 # Error reporting, send email only for build-all as assuming an batch job for that
269 if [[ -f $REPODIR/.errors ]]; then
270     ERRORS=$(cat $REPODIR/.errors | tr '\n' ' ')
271     rm $REPODIR/.errors
272     echo "Errors in packages: $ERRORS"
273     if [[ "$EMAIL" != "" && "$1" == "build-all" ]]; then
274         send_email $ERRORS
275     fi
276 else
277     echo "All packages built successfully"
278 fi
279