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

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