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

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