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

Chizi123
2020-10-15 7ac53faa1544a9c8ace058a2721647f368b059a6
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")
7ac53f 74         pkgs+=("$i")
88a91e 75     done
JG 76
e72b38 77     #Move package to repodir and add to repo db
88a91e 78     for i in ${pkgs[@]}; do
JG 79              rm $REPODIR/$i*.pkg.tar.??*
80              cp $i*.pkg.tar.?? $REPODIR/
81              [[ "$SIGN" == "Y" ]] && cp $i*.pkg.tar.??.sig $REPODIR/
82     done
9837c8 83
C 84     # Weird exceptions
85     if [[ "$1" == "zoom" ]]; then
86         rm zoom*_orig*.pkg.tar.xz
87     fi
88
e72b38 89     # Add package to waiting list to be added to repo db
9837c8 90     while true; do
C 91         if [[ $(cat $REPODIR/.waitlist.lck) == 1 ]]; then
e6f0af 92             sleep 1
4ebf3d 93         else
e6f0af 94             echo 1 > $REPODIR/.waitlist.lck
C 95             echo $1 >> $REPODIR/.waitlist
96             echo 0 > $REPODIR/.waitlist.lck
97             break
98             fi
99     done
9837c8 100     while true; do
e72b38 101         # Wait until package is at the top of the queue and add to db
9837c8 102         if [[ "$(head -n1 $REPODIR/.waitlist)" == "$1" ]]; then
88a91e 103             for i in ${pkgs[@]}; do
JG 104                 repo-add $([[ "$SIGN" == "Y" ]] && echo "--sign --key $KEY") $REPODIR/$REPONAME.db.tar.xz $REPODIR/$i*.pkg.tar.??
105             done
9837c8 106             while true; do
C 107                 if [[ $(cat $REPODIR/.waitlist.lck) == 1 ]]; then
e6f0af 108                     sleep 1
4ebf3d 109                 else
e72b38 110                     # Remove self from top of queue
e6f0af 111                     echo 1 > $REPODIR/.waitlist.lck
C 112                     tail -n +2 $REPODIR/.waitlist > $REPODIR/.waitlist.tmp
e72b38 113                     mv $REPODIR/.waitlist.tmp $REPODIR/.waitlist
e6f0af 114                     echo 0 > $REPODIR/.waitlist.lck
C 115                     break
116                 fi
117             done
118             break
119         else
e74aea 120             if [[ -z "$(grep $1 $REPODIR/.waitlist)" ]]; then
b2c2b5 121                 # Not on waitlist for some reason, need to readd
e74aea 122                 if [[ $(cat $REPODIR/.waitlist.lck) == 1 ]]; then
C 123                     sleep 1
124                 else
125                     echo 1 > $REPODIR/.waitlist.lck
126                     echo $1 >> $REPODIR/.waitlist
127                     echo 0 > $REPODIR/.waitlist.lck
128                 fi
129             fi
e6f0af 130             sleep 10
4ebf3d 131         fi
e6f0af 132     done
8ea4ef 133
JG 134     #Remove old versions of packages
f6854c 135     #TODO: Want to be able to keep multiple versions of old packages, future work
JG 136     #Currently old package versions stay in the repodir indefinately
137     # while [ $NUM_OLD \< $(find . -name '*.pkg.tar.xz' | wc -l) ]
138     # do
139     #     old=$(newold_matching_file o '*.pkg.tar.xz')
140     #     rm $REPODIR/$old $old
141     # done
8ea4ef 142     return 0
JG 143 }
144
145 #Update packages in BUILDDIR
f6854c 146 # Usage: build_all [-f force]
8ea4ef 147 function build_all {
JG 148     #system update
9837c8 149     if [[ $UPDATE == "Y" ]]; then
8ea4ef 150         sudo pacman -Syu --noconfirm
JG 151     fi
ac333f 152
e74aea 153     #Remove waitlist and errors from old builds
C 154     rm -f $REPODIR/{.waitlist,.errors}
8ea4ef 155     #update every package currently stored
c3d80e 156     for d in $(find $BUILDDIR -maxdepth 1 -mindepth 1 -type d)
8ea4ef 157     do
JG 158         cd $d
9837c8 159         if [[ "$PARALLEL" == "Y" ]]; then
C 160             build_pkg $(echo $d | rev | cut -d'/' -f1 | rev) $1 &> $([[ "$QUIET" == "Y" ]] && echo "/dev/null" || echo "/dev/tty")  &
161         else
162             build_pkg $(echo $d | rev | cut -d'/' -f1 | rev) $1 &> $([[ "$QUIET" == "Y" ]] && echo "/dev/null" || echo "/dev/tty")
163         fi
8ea4ef 164     done
e6f0af 165     wait
8ea4ef 166
JG 167     return 0
168 }
169
170 #Add a new package to be built
f6854c 171 #There is no name checking so be sure to put in the name correctly
JG 172 # Usage: add [package name]
8ea4ef 173 function add {
67ef73 174     for i in $@; do
JG 175         cd $BUILDDIR
176         git clone https://aur.archlinux.org/$i.git
177         cd $i
9837c8 178         build_pkg $i -f
67ef73 179     done
8ea4ef 180     return 0
c3d80e 181 }
C 182
183 #Remove a package from the build list and repository
184 # Usage remove [package name]
185 function remove {
67ef73 186     for i in $@; do
JG 187         rm -rf $BUILDDIR/$i*
188         repo-remove $REPODIR/$REPONAME.db.tar.xz $i
189         rm $REPODIR/$i*
190     done
8ea4ef 191 }
JG 192
193 #Check config and create build folders
f6854c 194 #Set variables before usage
JG 195 # Usage: init
8ea4ef 196 function init {
9837c8 197     if [[ $uid != 1 ]]; then
4ebf3d 198         echo "This must be run as root"
JG 199     fi
200
8ea4ef 201     #check for configuration here
9837c8 202     [[ -z $REPODIR ]] && echo "Enter REPODIR" && return 1
C 203     [[ -z $BUILDDIR ]] && echo "Enter BUILDDIR" && return 2
204     [[ -z $REPONAME ]] && echo "Enter REPONAME" && return 3
8ea4ef 205
JG 206     #make build directories
9837c8 207     [[ ! -d $REPODIR ]] && mkdir -p $REPODIR
C 208     [[ ! -d $BUILDDIR ]] && mkdir -p $BUILDDIR
8ea4ef 209
JG 210     #packages required to build others
4ebf3d 211     pacman -S --noconfirm base-devel git
8ea4ef 212
JG 213     #add repo to pacman.conf so can install own packages
9837c8 214     if [[ -z $(grep "$REPONAME" /etc/pacman.conf) ]]; then
8ea4ef 215         printf "[$REPONAME]\nSigLevel = Optional TrustAll\nServer = file://$REPODIR\n" >> /etc/pacman.conf
JG 216     fi
217
218     #create GPG key for package signing
4ebf3d 219     if [[ "$SIGN" == "Y" && "$KEY" == "" ]]; then
8ea4ef 220         (
JG 221             echo "Key-Type: RSA"
222             echo "Key-Length: 2048"
223             echo "Subkey-Type: RSA"
224             echo "Subkey-Length: 2048"
225             echo "Passphrase: \"\""
226             echo "Expire-Date: 0"
227             echo "Name-Real: John Doe"
228             echo "Name-Comment: Arch buildbot"
229             echo "Name-Email: $(whoami)@localhost"
230             echo "%commit"
231         ) | gpg --batch --generate-key
232         gpg --export --output $REPONAME.key --armor "John Doe"
233         gpg --export-secret-keys --output $REPONAME.secret.key --armor "John Doe"
234         echo "Please change the key information in this file"
235     fi
236
237     return 0
238 }
239
86d762 240 function send_email {
C 241     (
242     echo "From: build@localhost"
243     echo "To: $EMAIL"
244     echo "Subject: Build errors"
e6f0af 245     echo "There were build errors for the build of $REPONAME at $(date), please address them soon."
be70fd 246     echo "The errors were: $@"
86d762 247     ) | sendmail -t
C 248 }
249
8ea4ef 250 case $1 in
JG 251     "init")
252         init;;
253     "add")
9837c8 254         add ${@:2};;
8ea4ef 255     "build-all")
9837c8 256         build_all $([[ "$2" == "-f" ]] && echo "-f");;
c3d80e 257     "remove")
9837c8 258         remove ${@:2};;
8ea4ef 259     *)
f11bfd 260         printf "Invalid usage\nUsage: $0 init|add|build-all\n";;
8ea4ef 261 esac
9837c8 262
C 263 # Error reporting, send email only for build-all as assuming an batch job for that
264 if [[ -f $REPODIR/.errors ]]; then
265     ERRORS=$(cat $REPODIR/.errors | tr '\n' ' ')
266     rm $REPODIR/.errors
267     echo "Errors in packages: $ERRORS"
268     if [[ "$EMAIL" != "" && "$1" == "build-all" ]]; then
269         send_email $ERRORS
270     fi
271 else
272     echo "All packages built successfully"
273 fi
274