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

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