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

Chizi123
2020-10-19 5f18f4c7e7fafd5bc12acf688a35eaf136bc8762
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 {
7c4087 12     # Use ${1-} instead of $1 in case 'nounset' is set
JG 13     local -r glob_pattern=${2-}
8ea4ef 14
7c4087 15     # To avoid printing garbage if no files match the pattern, set
JG 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
8ea4ef 22
7c4087 23     file=
JG 24     for f in $glob_pattern ; do
8ea4ef 25         if [ $1 == "n" ]; then
JG 26             [[ -z $f || $f -nt $_file ]] && file=$f
27         elif [ $1 == "o" ]; then
28             [[ -z $f || $f -ot $_file ]] && file=$f
29         fi
7c4087 30     done
8ea4ef 31
7c4087 32     # To avoid unexpected behaviour elsewhere, unset nullglob if it was
JG 33     # set by this function
34     (( need_to_unset_nullglob )) && shopt -u nullglob
8ea4ef 35
7c4087 36     # Use printf instead of echo in case the file name begins with '-'
JG 37     [[ -n $file ]] && printf '%s\n' "$file"
8ea4ef 38
7c4087 39     return 0
8ea4ef 40 }
JG 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
3b9f12 51     rm -f *$1*.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
5f18f4 69     #Get build artifact names from PKGBUILD and build artifacts
C 70     #Remove duplicates from the list
88a91e 71     source PKGBUILD
JG 72     pkgs=()
5f18f4 73     ipkgs=()
88a91e 74     for i in ${pkgname[@]}; do
b2c2b5 75         #pkgs+=("$i-$pkgver-$pkgrel")
5f18f4 76         ipkgs+=($(find . -mindepth 1 -maxdepth 1 -type f \( -name "$i*.pkg.tar.*" -o -name "$i*.src.tar.*" \) -not -name "*.sig" | sed 's/^\.\///'))
88a91e 77     done
5f18f4 78     while read -r -d '' x; do pkgs+=("$x"); done < <(printf "%s\0" "${ipkgs[@]}" | sort -uz)
88a91e 79
e72b38 80     #Move package to repodir and add to repo db
d4b7a7 81     #Dont change the database if rebuilt the same package at same release and version
5f18f4 82     flag=0
88a91e 83     for i in ${pkgs[@]}; do
d4b7a7 84         if [[ -f $REPODIR/$i ]]; then
5f18f4 85             flag=1
d4b7a7 86         fi
88a91e 87     done
5f18f4 88     flag=1
C 89     if [[ $flag == 1 ]]; then
90         rm -f $REPODIR/*$1*.tar.*
91         for i in ${pkgs[@]}; do
92             cp $i $REPODIR/
93             [[ "$SIGN" == "Y" ]] && cp $i.sig $REPODIR/
94         done
95     else 
96         return;
97     fi
9837c8 98
C 99     # Weird exceptions
100     if [[ "$1" == "zoom" ]]; then
101         rm zoom*_orig*.pkg.tar.xz
102     fi
103
e72b38 104     # Add package to waiting list to be added to repo db
9837c8 105     while true; do
C 106         if [[ $(cat $REPODIR/.waitlist.lck) == 1 ]]; then
e6f0af 107             sleep 1
4ebf3d 108         else
e6f0af 109             echo 1 > $REPODIR/.waitlist.lck
C 110             echo $1 >> $REPODIR/.waitlist
111             echo 0 > $REPODIR/.waitlist.lck
112             break
113             fi
114     done
9837c8 115     while true; do
e72b38 116         # Wait until package is at the top of the queue and add to db
9837c8 117         if [[ "$(head -n1 $REPODIR/.waitlist)" == "$1" ]]; then
5f18f4 118         #    for i in ${pkgs[@]}; do
C 119                 repo-add $([[ "$SIGN" == "Y" ]] && echo "--sign --key $KEY") $REPODIR/$REPONAME.db.tar.$([ -n "$COMPRESSION" ] || echo $COMPRESSION && echo zst) ${pkgs[@]}
120         #    done
9837c8 121             while true; do
C 122                 if [[ $(cat $REPODIR/.waitlist.lck) == 1 ]]; then
e6f0af 123                     sleep 1
4ebf3d 124                 else
e72b38 125                     # Remove self from top of queue
e6f0af 126                     echo 1 > $REPODIR/.waitlist.lck
C 127                     tail -n +2 $REPODIR/.waitlist > $REPODIR/.waitlist.tmp
e72b38 128                     mv $REPODIR/.waitlist.tmp $REPODIR/.waitlist
e6f0af 129                     echo 0 > $REPODIR/.waitlist.lck
C 130                     break
131                 fi
132             done
133             break
134         else
e74aea 135             if [[ -z "$(grep $1 $REPODIR/.waitlist)" ]]; then
b2c2b5 136                 # Not on waitlist for some reason, need to readd
e74aea 137                 if [[ $(cat $REPODIR/.waitlist.lck) == 1 ]]; then
C 138                     sleep 1
139                 else
140                     echo 1 > $REPODIR/.waitlist.lck
141                     echo $1 >> $REPODIR/.waitlist
142                     echo 0 > $REPODIR/.waitlist.lck
143                 fi
144             fi
e6f0af 145             sleep 10
4ebf3d 146         fi
e6f0af 147     done
8ea4ef 148
JG 149     #Remove old versions of packages
f6854c 150     #TODO: Want to be able to keep multiple versions of old packages, future work
JG 151     #Currently old package versions stay in the repodir indefinately
152     # while [ $NUM_OLD \< $(find . -name '*.pkg.tar.xz' | wc -l) ]
153     # do
7c4087 154     #    old=$(newold_matching_file o '*.pkg.tar.xz')
JG 155     #    rm $REPODIR/$old $old
f6854c 156     # done
8ea4ef 157     return 0
JG 158 }
159
160 #Update packages in BUILDDIR
f6854c 161 # Usage: build_all [-f force]
8ea4ef 162 function build_all {
JG 163     #system update
9837c8 164     if [[ $UPDATE == "Y" ]]; then
8ea4ef 165         sudo pacman -Syu --noconfirm
JG 166     fi
ac333f 167
e74aea 168     #Remove waitlist and errors from old builds
C 169     rm -f $REPODIR/{.waitlist,.errors}
8ea4ef 170     #update every package currently stored
c3d80e 171     for d in $(find $BUILDDIR -maxdepth 1 -mindepth 1 -type d)
8ea4ef 172     do
JG 173         cd $d
9837c8 174         if [[ "$PARALLEL" == "Y" ]]; then
C 175             build_pkg $(echo $d | rev | cut -d'/' -f1 | rev) $1 &> $([[ "$QUIET" == "Y" ]] && echo "/dev/null" || echo "/dev/tty")  &
176         else
177             build_pkg $(echo $d | rev | cut -d'/' -f1 | rev) $1 &> $([[ "$QUIET" == "Y" ]] && echo "/dev/null" || echo "/dev/tty")
178         fi
8ea4ef 179     done
e6f0af 180     wait
8ea4ef 181
JG 182     return 0
183 }
184
185 #Add a new package to be built
7c4087 186 #Adding build dependencies is
f6854c 187 # Usage: add [package name]
8ea4ef 188 function add {
67ef73 189     for i in $@; do
JG 190         cd $BUILDDIR
ca0d8a 191         if [[ -z $(git ls-remote https://aur.archlinux.org/$i.git) ]]; then
C 192             echo "Not a package"
193             exit 2
194         fi
67ef73 195         git clone https://aur.archlinux.org/$i.git
JG 196         cd $i
ca0d8a 197
C 198         #check for all build dependencies
199         for i in ${makedepends[@]}; do
7c4087 200             if pacman -Si $i; then
ca0d8a 201                 makedepends=${makedepends[@]/$delete}
C 202             fi &>/dev/null
203         done
204         for i in ${makedepends[@]}; do
205             add $i
206         done
3b9f12 207         if [[ -n "${makedepends[@]}" ]]; then
C 208             sudo pacman -Sy
209         fi
210
ca0d8a 211         #Actually build wanted package
9837c8 212         build_pkg $i -f
67ef73 213     done
8ea4ef 214     return 0
c3d80e 215 }
C 216
217 #Remove a package from the build list and repository
e6670f 218 #Usage of -a removes all packages moved to official repos
JG 219 # Usage remove [-a|package name]
c3d80e 220 function remove {
e6670f 221     if [[ "$1" == "-a" ]]; then
JG 222         rmlist=""
223         rmlist="$rmlist $(comm -12 <(pacman -Slq $REPONAME | sort) <(pacman -Slq core | sort) | tr '\n' ' ')"
224         rmlist="$rmlist $(comm -12 <(pacman -Slq $REPONAME | sort) <(pacman -Slq extra | sort) | tr '\n' ' ')"
225         rmlist="$rmlist $(comm -12 <(pacman -Slq $REPONAME | sort) <(pacman -Slq community | sort) | tr '\n' ' ')"
226         for i in $rmlist; do
227             rm -rf $BUILDDIR/$i
228             repo-remove $([[ "$SIGN" == "Y" ]] && echo "--sign --key $KEY") $REPODIR/$REPONAME.db.tar.$([ -n "$COMPRESSION" ] || echo $COMPRESSION && echo zst) $i
229             rm -f $REPODIR/*$i*
230         done
231     else
232         for i in $@; do
233             rm -rf $BUILDDIR/$i
234             repo-remove $([[ "$SIGN" == "Y" ]] && echo "--sign --key $KEY") $REPODIR/$REPONAME.db.tar.$([ -n "$COMPRESSION" ] || echo $COMPRESSION && echo zst) $i
235             rm -f $REPODIR/*$i*
236         done
237     fi
8ea4ef 238 }
JG 239
240 #Check config and create build folders
f6854c 241 #Set variables before usage
JG 242 # Usage: init
8ea4ef 243 function init {
9837c8 244     if [[ $uid != 1 ]]; then
4ebf3d 245         echo "This must be run as root"
JG 246     fi
247
8ea4ef 248     #check for configuration here
9837c8 249     [[ -z $REPODIR ]] && echo "Enter REPODIR" && return 1
C 250     [[ -z $BUILDDIR ]] && echo "Enter BUILDDIR" && return 2
251     [[ -z $REPONAME ]] && echo "Enter REPONAME" && return 3
8ea4ef 252
JG 253     #make build directories
9837c8 254     [[ ! -d $REPODIR ]] && mkdir -p $REPODIR
C 255     [[ ! -d $BUILDDIR ]] && mkdir -p $BUILDDIR
8ea4ef 256
JG 257     #packages required to build others
4ebf3d 258     pacman -S --noconfirm base-devel git
8ea4ef 259
JG 260     #add repo to pacman.conf so can install own packages
9837c8 261     if [[ -z $(grep "$REPONAME" /etc/pacman.conf) ]]; then
8ea4ef 262         printf "[$REPONAME]\nSigLevel = Optional TrustAll\nServer = file://$REPODIR\n" >> /etc/pacman.conf
JG 263     fi
264
265     #create GPG key for package signing
4ebf3d 266     if [[ "$SIGN" == "Y" && "$KEY" == "" ]]; then
8ea4ef 267         (
JG 268             echo "Key-Type: RSA"
269             echo "Key-Length: 2048"
270             echo "Subkey-Type: RSA"
271             echo "Subkey-Length: 2048"
272             echo "Passphrase: \"\""
273             echo "Expire-Date: 0"
274             echo "Name-Real: John Doe"
275             echo "Name-Comment: Arch buildbot"
276             echo "Name-Email: $(whoami)@localhost"
277             echo "%commit"
278         ) | gpg --batch --generate-key
279         gpg --export --output $REPONAME.key --armor "John Doe"
280         gpg --export-secret-keys --output $REPONAME.secret.key --armor "John Doe"
281         echo "Please change the key information in this file"
282     fi
283
284     return 0
285 }
286
86d762 287 function send_email {
C 288     (
289     echo "From: build@localhost"
290     echo "To: $EMAIL"
291     echo "Subject: Build errors"
e6f0af 292     echo "There were build errors for the build of $REPONAME at $(date), please address them soon."
be70fd 293     echo "The errors were: $@"
86d762 294     ) | sendmail -t
C 295 }
296
8ea4ef 297 case $1 in
JG 298     "init")
299         init;;
300     "add")
9837c8 301         add ${@:2};;
8ea4ef 302     "build-all")
9837c8 303         build_all $([[ "$2" == "-f" ]] && echo "-f");;
c3d80e 304     "remove")
9837c8 305         remove ${@:2};;
8ea4ef 306     *)
f11bfd 307         printf "Invalid usage\nUsage: $0 init|add|build-all\n";;
8ea4ef 308 esac
9837c8 309
C 310 # Error reporting, send email only for build-all as assuming an batch job for that
311 if [[ -f $REPODIR/.errors ]]; then
312     ERRORS=$(cat $REPODIR/.errors | tr '\n' ' ')
313     rm $REPODIR/.errors
314     echo "Errors in packages: $ERRORS"
315     if [[ "$EMAIL" != "" && "$1" == "build-all" ]]; then
316         send_email $ERRORS
317     fi
318 else
319     echo "All packages built successfully"
320 fi