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

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