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

Joel Grunbaum
2022-04-10 ae3c9b666a74f98847bb28d5e683861494c4c880
commit | author | age
8ea4ef 1 #!/bin/bash
f6854c 2 #A basic bash script to automate the building of arch packages
dc0641 3 # Usage: main.sh init|check|add|remove|build_all
8ea4ef 4
e6f0af 5 source $(dirname "$(realpath $0)")/vars.sh
8ea4ef 6
54808c 7 ERRORFILE=$(mktemp)
C 8 WAITLIST=$(mktemp)
9 WAITLIST_LCK=$(mktemp)
10
8ea4ef 11 #Helper for finding newest and oldest files
JG 12 #Sourced from stack overflow
f6854c 13 # Usage: newold_matching_file [n/o] [filename]
8ea4ef 14 function newold_matching_file
JG 15 {
7c4087 16     # Use ${1-} instead of $1 in case 'nounset' is set
JG 17     local -r glob_pattern=${2-}
8ea4ef 18
7c4087 19     # To avoid printing garbage if no files match the pattern, set
JG 20     # 'nullglob' if necessary
21     local -i need_to_unset_nullglob=0
22     if [[ ":$BASHOPTS:" != *:nullglob:* ]] ; then
23         shopt -s nullglob
24         need_to_unset_nullglob=1
25     fi
8ea4ef 26
7c4087 27     file=
JG 28     for f in $glob_pattern ; do
8ea4ef 29         if [ $1 == "n" ]; then
JG 30             [[ -z $f || $f -nt $_file ]] && file=$f
31         elif [ $1 == "o" ]; then
32             [[ -z $f || $f -ot $_file ]] && file=$f
33         fi
7c4087 34     done
8ea4ef 35
7c4087 36     # To avoid unexpected behaviour elsewhere, unset nullglob if it was
JG 37     # set by this function
38     (( need_to_unset_nullglob )) && shopt -u nullglob
8ea4ef 39
7c4087 40     # Use printf instead of echo in case the file name begins with '-'
JG 41     [[ -n $file ]] && printf '%s\n' "$file"
8ea4ef 42
7c4087 43     return 0
8ea4ef 44 }
JG 45
46 #Build latest version of a package
9837c8 47 # Usage: build_pkg [package name] [-f force]
8ea4ef 48 function build_pkg {
b2dc9d 49     #first need to reset repo, revert any changes so update can happen
JG 50     git reset --hard
8ea4ef 51     #check if PKGBUILD has updated, don't rebuild if hasn't changed
b59712 52     if [[ -n $(git pull | grep 'Already up to date.') && -z $(grep 'pkgver() {' PKGBUILD) && -z "$2" ]]; then
8ea4ef 53         return 2
JG 54     fi
f6854c 55
5f1f4f 56     #Remove old packages from build directory
JG 57     source PKGBUILD
58     srcdir="$(pwd)/src"
59     if grep -q 'pkgver() {' PKGBUILD; then
60         ver=$(pkgver)
61     else
62         ver=$pkgver
63     fi
64     find . -mindepth 1 -maxdepth 1 -type f \( -name "*.pkg.tar.*" -o -name "*.src.tar.*" \) -not -name "*$ver-$pkgrel*" -delete
65
f6854c 66     #make and force rebuild if is git package
9837c8 67     # Mictosoft fonts have problems with checksums and need a seperate argument
C 68     if [[ "$1" == "ttf-ms-win10" ||
69         "$1" == "ttf-office-2007-fonts" ||
70         "$1" == "ttf-ms-win8" ||
71         "$1" == "ttf-win7-fonts" ]]; then
cb04c8 72         makepkg -s --noconfirm $([[ $CLEAN == "Y" ]] && echo "-c") $([[ $SIGN == "Y" ]] && echo "--sign --key $KEY") $([[ "$2" == "-f" ]] && echo -f) --skipchecksums 2>&1
9837c8 73     else
e74aea 74         makepkg -s --noconfirm $([[ $CLEAN == "Y" ]] && echo "-c") $([[ $SIGN == "Y" ]] && echo "--sign --key $KEY") $([[ "$2" == "-f" ]] && echo -f) 2>&1
9837c8 75     fi
266025 76     if [[ $? != 0  && $? != 13 ]]; then
8ea4ef 77         #Register error
54808c 78         echo $1 >> $ERRORFILE
8ea4ef 79         return 1
JG 80     fi
3d9186 81
2bcbc6 82     #Get build artifact names from PKGBUILD and build artifacts
JG 83     #Remove duplicates from the list
37434c 84     pkgs=()
5f18f4 85     ipkgs=()
88a91e 86     for i in ${pkgname[@]}; do
b2c2b5 87         #pkgs+=("$i-$pkgver-$pkgrel")
5f18f4 88         ipkgs+=($(find . -mindepth 1 -maxdepth 1 -type f \( -name "$i*.pkg.tar.*" -o -name "$i*.src.tar.*" \) -not -name "*.sig" | sed 's/^\.\///'))
88a91e 89     done
5f18f4 90     while read -r -d '' x; do pkgs+=("$x"); done < <(printf "%s\0" "${ipkgs[@]}" | sort -uz)
88a91e 91
94852c 92     # Weird exceptions
C 93     if [[ "$1" == "zoom" ]]; then
94         rm zoom*_orig*
95         for i in ${pkgs[@]}; do
96             if [ -z "${i##*_orig*}" ]; then
97                 pkgs=(${pkgs[@]/$i})
98             fi
99         done
100     fi
101
e72b38 102     #Move package to repodir and add to repo db
d4b7a7 103     #Dont change the database if rebuilt the same package at same release and version
5f18f4 104     flag=0
88a91e 105     for i in ${pkgs[@]}; do
e4347d 106         if [[ ! -f $REPODIR/$i ]]; then
5f18f4 107             flag=1
d4b7a7 108         fi
88a91e 109     done
5f18f4 110     if [[ $flag == 1 ]]; then
C 111         rm -f $REPODIR/*$1*.tar.*
112         for i in ${pkgs[@]}; do
113             cp $i $REPODIR/
114             [[ "$SIGN" == "Y" ]] && cp $i.sig $REPODIR/
115         done
3d9186 116     else
5f18f4 117         return;
9837c8 118     fi
C 119
e72b38 120     # Add package to waiting list to be added to repo db
9837c8 121     while true; do
54808c 122         if [[ $(cat $WAITLIST_LCK) == 1 ]]; then
e6f0af 123             sleep 1
4ebf3d 124         else
54808c 125             echo 1 > $WAITLIST_LCK
C 126             echo $1 >> $WAITLIST
127             echo 0 > $WAITLIST_LCK
e6f0af 128             break
C 129             fi
130     done
9837c8 131     while true; do
e72b38 132         # Wait until package is at the top of the queue and add to db
54808c 133         if [[ "$(head -n1 $WAITLIST)" == "$1" ]]; then
3d9186 134             repo-add $([[ "$SIGN" == "Y" ]] && echo "--sign --key $KEY") $REPODIR/$REPONAME.db.tar.$([ -n "$COMPRESSION" ] || echo $COMPRESSION && echo zst) ${pkgs[@]}
9837c8 135             while true; do
54808c 136                 if [[ $(cat $WAITLIST_LCK) == 1 ]]; then
e6f0af 137                     sleep 1
4ebf3d 138                 else
e72b38 139                     # Remove self from top of queue
5e843f 140                     echo 1 > $WAITLIST_LCK
54808c 141                     TEMP=$(mktemp)
C 142                     tail -n +2 $WAITLIST > $TEMP
143                     cp $TEMP $WAITLIST
144                     rm $TEMP
145                     unset TEMP
146                     echo 0 > $WAITLIST_LCK
e6f0af 147                     break
C 148                 fi
149             done
150             break
151         else
54808c 152             if [[ -z "$(grep $1 $WAITLIST)" ]]; then
b2c2b5 153                 # Not on waitlist for some reason, need to readd
54808c 154                 if [[ $(cat $WAITLIST_LCK) == 1 ]]; then
e74aea 155                     sleep 1
C 156                 else
54808c 157                     echo 1 > $WAITLIST_LCK
C 158                     echo $1 >> $WAITLIST
159                     echo 0 > $WAITLIST_LCK
e74aea 160                 fi
C 161             fi
e6f0af 162             sleep 10
4ebf3d 163         fi
e6f0af 164     done
8ea4ef 165
JG 166     #Remove old versions of packages
f6854c 167     #TODO: Want to be able to keep multiple versions of old packages, future work
JG 168     #Currently old package versions stay in the repodir indefinately
169     # while [ $NUM_OLD \< $(find . -name '*.pkg.tar.xz' | wc -l) ]
170     # do
7c4087 171     #    old=$(newold_matching_file o '*.pkg.tar.xz')
JG 172     #    rm $REPODIR/$old $old
f6854c 173     # done
8ea4ef 174     return 0
JG 175 }
176
177 #Update packages in BUILDDIR
f6854c 178 # Usage: build_all [-f force]
8ea4ef 179 function build_all {
JG 180     #system update
9837c8 181     if [[ $UPDATE == "Y" ]]; then
8ea4ef 182         sudo pacman -Syu --noconfirm
JG 183     fi
ac333f 184
8ea4ef 185     #update every package currently stored
c3d80e 186     for d in $(find $BUILDDIR -maxdepth 1 -mindepth 1 -type d)
8ea4ef 187     do
JG 188         cd $d
9837c8 189         if [[ "$PARALLEL" == "Y" ]]; then
C 190             build_pkg $(echo $d | rev | cut -d'/' -f1 | rev) $1 &> $([[ "$QUIET" == "Y" ]] && echo "/dev/null" || echo "/dev/tty")  &
191         else
192             build_pkg $(echo $d | rev | cut -d'/' -f1 | rev) $1 &> $([[ "$QUIET" == "Y" ]] && echo "/dev/null" || echo "/dev/tty")
193         fi
8ea4ef 194     done
e6f0af 195     wait
8ea4ef 196
JG 197     return 0
198 }
199
200 #Add a new package to be built
7c4087 201 #Adding build dependencies is
f6854c 202 # Usage: add [package name]
8ea4ef 203 function add {
37434c 204     local i j k
67ef73 205     for i in $@; do
JG 206         cd $BUILDDIR
5ac326 207         if [ ! -d $i ] && [[ -z $(git ls-remote https://aur.archlinux.org/$i.git) ]]; then
863ad1 208             echo "Not a package: $i"
ca0d8a 209             exit 2
C 210         fi
67ef73 211         git clone https://aur.archlinux.org/$i.git
JG 212         cd $i
37434c 213         unset depends
JG 214         unset makedepends
215         local makedeps
216         source PKGBUILD
ca0d8a 217
3d9186 218         #Check for all build dependencies
37434c 219         for j in ${makedepends[@]}; do
JG 220             k=$(echo $j | sed 's/[>]=.*//g')
863ad1 221             if ! (pacman -Si $k || pacman -Qi $k); then
37434c 222                 makedeps+=($k)
ca0d8a 223             fi &>/dev/null
C 224         done
37434c 225         for j in ${depends[@]}; do
JG 226             k=$(echo $j | sed 's/[>]=.*//g')
863ad1 227             if ! (pacman -Si $k || pacman -Qi $k); then
37434c 228                 makedeps+=($k)
JG 229             fi &>/dev/null
ca0d8a 230         done
3d9186 231
JG 232         #Add dependencies and update so overall build can work
37434c 233         for j in ${makedeps[@]}; do
JG 234             add $j
235         done
236         if [[ -n "${makedeps[@]}" ]]; then
3b9f12 237             sudo pacman -Sy
C 238         fi
3d9186 239
ca0d8a 240         #Actually build wanted package
37434c 241         cd $BUILDDIR/$i
9837c8 242         build_pkg $i -f
67ef73 243     done
8ea4ef 244     return 0
c3d80e 245 }
C 246
247 #Remove a package from the build list and repository
e6670f 248 #Usage of -a removes all packages moved to official repos
JG 249 # Usage remove [-a|package name]
c3d80e 250 function remove {
e6670f 251     if [[ "$1" == "-a" ]]; then
JG 252         rmlist=""
253         rmlist="$rmlist $(comm -12 <(pacman -Slq $REPONAME | sort) <(pacman -Slq core | sort) | tr '\n' ' ')"
254         rmlist="$rmlist $(comm -12 <(pacman -Slq $REPONAME | sort) <(pacman -Slq extra | sort) | tr '\n' ' ')"
255         rmlist="$rmlist $(comm -12 <(pacman -Slq $REPONAME | sort) <(pacman -Slq community | sort) | tr '\n' ' ')"
256         for i in $rmlist; do
257             rm -rf $BUILDDIR/$i
258             repo-remove $([[ "$SIGN" == "Y" ]] && echo "--sign --key $KEY") $REPODIR/$REPONAME.db.tar.$([ -n "$COMPRESSION" ] || echo $COMPRESSION && echo zst) $i
259             rm -f $REPODIR/*$i*
260         done
261     else
262         for i in $@; do
263             rm -rf $BUILDDIR/$i
264             repo-remove $([[ "$SIGN" == "Y" ]] && echo "--sign --key $KEY") $REPODIR/$REPONAME.db.tar.$([ -n "$COMPRESSION" ] || echo $COMPRESSION && echo zst) $i
265             rm -f $REPODIR/*$i*
266         done
267     fi
8ea4ef 268 }
JG 269
7aa510 270 #Check for packages moved to official repos or removed from the AUR
C 271 function check {
272     rmlist=""
273     rmlist="$rmlist $(comm -12 <(pacman -Slq $REPONAME | sort) <(pacman -Slq core | sort) | tr '\n' ' ')"
274     rmlist="$rmlist $(comm -12 <(pacman -Slq $REPONAME | sort) <(pacman -Slq extra | sort) | tr '\n' ' ')"
275     rmlist="$rmlist $(comm -12 <(pacman -Slq $REPONAME | sort) <(pacman -Slq community | sort) | tr '\n' ' ')"
276     TMPFILE=$(mktemp)
277     for i in $(find $BUILDDIR -mindepth 1 -maxdepth 1 -type d); do
278         check_pkg $TMPFILE "$(echo $i | rev | cut -d'/' -f1 | rev)" &
279     done
280     wait
281     echo "Merged into official repos: $rmlist"
282     echo "Not in AUR: $(cat $TMPFILE | tr '\n' ' ')"
283     rm -f $TMPFILE
284 }
285
3d9186 286 #Check helper function
7aa510 287 function check_pkg {
ae3c9b 288     if [[ -z "$(curl -si "https://aur.archlinux.org/packages/$2" | head -n1 | grep 200)" ]]; then
7aa510 289         echo "$2" >> $1
C 290     fi
291 }
292
293
8ea4ef 294 #Check config and create build folders
f6854c 295 #Set variables before usage
JG 296 # Usage: init
8ea4ef 297 function init {
9837c8 298     if [[ $uid != 1 ]]; then
4ebf3d 299         echo "This must be run as root"
JG 300     fi
301
8ea4ef 302     #check for configuration here
9837c8 303     [[ -z $REPODIR ]] && echo "Enter REPODIR" && return 1
C 304     [[ -z $BUILDDIR ]] && echo "Enter BUILDDIR" && return 2
305     [[ -z $REPONAME ]] && echo "Enter REPONAME" && return 3
8ea4ef 306
JG 307     #make build directories
9837c8 308     [[ ! -d $REPODIR ]] && mkdir -p $REPODIR
C 309     [[ ! -d $BUILDDIR ]] && mkdir -p $BUILDDIR
8ea4ef 310
JG 311     #packages required to build others
4ebf3d 312     pacman -S --noconfirm base-devel git
8ea4ef 313
JG 314     #add repo to pacman.conf so can install own packages
9837c8 315     if [[ -z $(grep "$REPONAME" /etc/pacman.conf) ]]; then
8ea4ef 316         printf "[$REPONAME]\nSigLevel = Optional TrustAll\nServer = file://$REPODIR\n" >> /etc/pacman.conf
JG 317     fi
318
319     #create GPG key for package signing
4ebf3d 320     if [[ "$SIGN" == "Y" && "$KEY" == "" ]]; then
8ea4ef 321         (
JG 322             echo "Key-Type: RSA"
323             echo "Key-Length: 2048"
324             echo "Subkey-Type: RSA"
325             echo "Subkey-Length: 2048"
326             echo "Passphrase: \"\""
327             echo "Expire-Date: 0"
328             echo "Name-Real: John Doe"
329             echo "Name-Comment: Arch buildbot"
330             echo "Name-Email: $(whoami)@localhost"
331             echo "%commit"
332         ) | gpg --batch --generate-key
333         gpg --export --output $REPONAME.key --armor "John Doe"
334         gpg --export-secret-keys --output $REPONAME.secret.key --armor "John Doe"
335         echo "Please change the key information in this file"
336     fi
337
338     return 0
339 }
340
86d762 341 function send_email {
ae3c9b 342 #    message=(echo "From: $FROM_EMAIL"
JG 343 #            echo "To: $TO_EMAIL"
344 #            echo "Subject: Build errors"
345 #            echo ""
346 #            echo "There were build errors for the build of $REPONAME at $(date), please address them soon."
347 #            echo "The errors were: $@"
348 #        )
349     if [ 1 ]; then
350         (
351             echo "From: $FROM_EMAIL"
352             echo "To: $TO_EMAIL"
353             echo "Subject: Build errors"
354             echo ""
355             echo "There were build errors for the build of $REPONAME at $(date), please address them soon."
356             echo "The errors were: $@"
357         ) | sendmail -t
358     else 
6bd4de 359     curl -s --url "smtp://$EMAIL_HOST" --ssl \
d41201 360         --mail-from "$FROM_EMAIL" \
JG 361         --mail-rcpt "$TO_EMAIL" \
362         --user "$EMAIL_USER" \
363         -T <(
364             echo "From: $FROM_EMAIL"
365             echo "To: $TO_EMAIL"
366             echo "Subject: Build errors"
367             echo ""
368             echo "There were build errors for the build of $REPONAME at $(date), please address them soon."
369             echo "The errors were: $@"
370         )
ae3c9b 371     fi
095fd9 372     return $?
86d762 373 }
C 374
8ea4ef 375 case $1 in
JG 376     "init")
377         init;;
378     "add")
9837c8 379         add ${@:2};;
8ea4ef 380     "build-all")
9837c8 381         build_all $([[ "$2" == "-f" ]] && echo "-f");;
c3d80e 382     "remove")
9837c8 383         remove ${@:2};;
7aa510 384     "check")
C 385         check;;
f4a6ac 386     "test-mail")
095fd9 387         send_email
JG 388         exit $?;;
8ea4ef 389     *)
dc0641 390         echo -e "\033[0;31mInvalid usage\033[0m"
C 391         echo -e "Usage: $0 init|check|add|remove|build-all"
392         echo -e "\033[0;32minit\033[0m                        - initialise repository for use"
f4a6ac 393         echo -e "\033[0;32mtest-mail\033[m                    - test email configuration by sending the default email without a message"
dc0641 394         echo -e "\033[0;32mcheck\033[0m                       - check if packages have been moved into the official repositories or removed from the AUR"
C 395         echo -e "\033[0;32madd package ...\033[0m             - add a package to \$BUILDDIR and repository, also used to rebuild failed packages"
396         echo -e "\033[0;32mremove -a | package ...\033[0m     - remove package from \$BUILDDIR and repository, \"-a\" removes packages added to official repos"
397         echo -e "\033[0;32mbuild-all [-f]\033[0m              - build all packages in \$BUILDDIR, \"-f\" force builds whole repository"
8ea4ef 398 esac
9837c8 399
C 400 # Error reporting, send email only for build-all as assuming an batch job for that
7aa510 401 if [[ $1 == "build-all" || $1 == "add" ]]; then
54808c 402     if [[ -n $(cat $ERRORFILE) ]]; then
C 403         ERRORS=$(cat $ERRORFILE | tr '\n' ' ')
7aa510 404         echo "Errors in packages: $ERRORS"
f4a6ac 405         if [[ "$TO_EMAIL" != "" && "$1" == "build-all" ]]; then
7aa510 406             send_email $ERRORS
C 407         fi
408     else
409         echo "All packages built successfully"
9837c8 410     fi
C 411 fi
54808c 412
C 413 rm $ERRORFILE $WAITLIST $WAITLIST_LCK