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

Joel Grunbaum
2020-09-21 4ebf3d611acf7eae221c10aee1d01848d8d701cc
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 {
JG 130     cd $BUILDDIR
131     git clone https://aur.archlinux.org/$1.git
132     cd $1
f11bfd 133     build_pkg $1 new -f
8ea4ef 134     return 0
c3d80e 135 }
C 136
137 #Remove a package from the build list and repository
138 # Usage remove [package name]
139 function remove {
140     rm -rf $BUILDDIR/$1*
141     repo-remove $REPODIR/$REPONAME.db.tar.xz $1
142     rm $REPODIR/$1*
8ea4ef 143 }
JG 144
145 #Check config and create build folders
f6854c 146 #Set variables before usage
JG 147 # Usage: init
8ea4ef 148 function init {
4ebf3d 149     if [ $uid != 1 ]; then
JG 150         echo "This must be run as root"
151     fi
152
8ea4ef 153     #check for configuration here
JG 154     [ -z $REPODIR ] && echo "Enter REPODIR" && return 1
155     [ -z $BUILDDIR ] && echo "Enter BUILDDIR" && return 2
156     [ -z $REPONAME ] && echo "Enter REPONAME" && return 3
157
158     #make build directories
159     [ ! -d $REPODIR ] && mkdir -p $REPODIR
160     [ ! -d $BUILDDIR ] && mkdir -p $BUILDDIR
161
162     #packages required to build others
4ebf3d 163     pacman -S --noconfirm base-devel git
8ea4ef 164
JG 165     #add repo to pacman.conf so can install own packages
166     if [ -z $(grep "$REPONAME" /etc/pacman.conf) ]; then
167         printf "[$REPONAME]\nSigLevel = Optional TrustAll\nServer = file://$REPODIR\n" >> /etc/pacman.conf
168     fi
169
170     #create GPG key for package signing
4ebf3d 171     if [[ "$SIGN" == "Y" && "$KEY" == "" ]]; then
8ea4ef 172         (
JG 173             echo "Key-Type: RSA"
174             echo "Key-Length: 2048"
175             echo "Subkey-Type: RSA"
176             echo "Subkey-Length: 2048"
177             echo "Passphrase: \"\""
178             echo "Expire-Date: 0"
179             echo "Name-Real: John Doe"
180             echo "Name-Comment: Arch buildbot"
181             echo "Name-Email: $(whoami)@localhost"
182             echo "%commit"
183         ) | gpg --batch --generate-key
184         gpg --export --output $REPONAME.key --armor "John Doe"
185         gpg --export-secret-keys --output $REPONAME.secret.key --armor "John Doe"
186         echo "Please change the key information in this file"
187     fi
188
189     return 0
190 }
191
86d762 192 function send_email {
C 193     (
194     echo "From: build@localhost"
195     echo "To: $EMAIL"
196     echo "Subject: Build errors"
e6f0af 197     echo "There were build errors for the build of $REPONAME at $(date), please address them soon."
C 198     echo "The errors were: $1"
86d762 199     ) | sendmail -t
C 200 }
201
8ea4ef 202 case $1 in
JG 203     "init")
204         init;;
205     "add")
206         add $2;;
207     "build-all")
208         build_all $([ "$2" == "-f" ] && echo "-f")
e6f0af 209         if [ -f $REPODIR/.errors ]; then
C 210             ERRORS=$(cat $REPODIR/.errors | tr '\n' ' ')
211             rm $REPODIR/.errors
212             echo "Errors in packages: $ERRORS"
8ea4ef 213             if [ "$EMAIL" != "" ]; then
e6f0af 214                 send_email $ERRORS
8ea4ef 215             fi
JG 216         else
217             echo "All packages built successfully"
218         fi
219         ;;
c3d80e 220     "remove")
C 221         remove $2;;
8ea4ef 222     *)
f11bfd 223         printf "Invalid usage\nUsage: $0 init|add|build-all\n";;
8ea4ef 224 esac