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

Chizi123
2020-09-21 e6f0af8c329f8f66410b99015445ac58ae669475
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 ERRORS=""
8
9 #Helper for finding newest and oldest files
10 #Sourced from stack overflow
f6854c 11 # Usage: newold_matching_file [n/o] [filename]
8ea4ef 12 function newold_matching_file
JG 13 {
14     # Use ${1-} instead of $1 in case 'nounset' is set
15     local -r glob_pattern=${2-}
16
17     # To avoid printing garbage if no files match the pattern, set
18     # 'nullglob' if necessary
19     local -i need_to_unset_nullglob=0
20     if [[ ":$BASHOPTS:" != *:nullglob:* ]] ; then
21         shopt -s nullglob
22         need_to_unset_nullglob=1
23     fi
24
25     file=
26     for f in $glob_pattern ; do
27         if [ $1 == "n" ]; then
28             [[ -z $f || $f -nt $_file ]] && file=$f
29         elif [ $1 == "o" ]; then
30             [[ -z $f || $f -ot $_file ]] && file=$f
31         fi
32     done
33
34     # To avoid unexpected behaviour elsewhere, unset nullglob if it was
35     # set by this function
36     (( need_to_unset_nullglob )) && shopt -u nullglob
37
38     # Use printf instead of echo in case the file name begins with '-'
39     [[ -n $file ]] && printf '%s\n' "$file"
40
41     return 0
42 }
43
44 #Build latest version of a package
f6854c 45 # Usage: build_pkg [package name] [new?] [-f force]
8ea4ef 46 function build_pkg {
JG 47     #check if PKGBUILD has updated, don't rebuild if hasn't changed
48     if [[ ! -z $(git pull | grep "Already up to date.") && -z $(echo $1 | grep git) && -z $2 ]]; then
49         return 2
50     fi
f6854c 51
JG 52     #remove old versions before build
09a580 53     rm *$1*.pkg.tar.xz*
f6854c 54
JG 55     #make and force rebuild if is git package
8ea4ef 56     makepkg -s --noconfirm $([ $CLEAN == "Y" ] && echo "-c") $([ $SIGN == "Y" ] && echo "--sign --key $KEY") $([ "$2" == "-f" ] && echo -f)
JG 57     if [ $? != 0 ]; then
58         #Register error
e6f0af 59         echo $1 >> $REPODIR/.errors
8ea4ef 60         return 1
JG 61     fi
62
09a580 63     rm $REPODIR/*$1*.pkg.tar.xz*
C 64     cp *$1*.pkg.tar.xz $REPODIR/
65     [ "$SIGN" == "Y" ] && cp *$1*.pkg.tar.xz.sig $REPODIR
e6f0af 66     while [ 1 ]; do
C 67         if [ $(cat $REPODIR/.waitlist.lck) == 1 ]; then
68             sleep 1
69         else 
70             echo 1 > $REPODIR/.waitlist.lck
71             echo $1 >> $REPODIR/.waitlist
72             echo 0 > $REPODIR/.waitlist.lck
73             break
74             fi
75     done
76     while [ 1 ]; do
77         if [ "$(head -n1 $REPODIR/.waitlist)" == "$1" ]; then
78     
09a580 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
83                 else 
84                     echo 1 > $REPODIR/.waitlist.lck
85                     tail -n +2 $REPODIR/.waitlist > $REPODIR/.waitlist.tmp
86                     cat $REPODIR/.waitlist.tmp > $REPODIR/.waitlist
87                     rm $REPODIR/.waitlist.tmp
88                     echo 0 > $REPODIR/.waitlist.lck
89                     break
90                 fi
91             done
92             break
93         else
94             sleep 10
95         fi    
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 {
JG 132     cd $BUILDDIR
133     git clone https://aur.archlinux.org/$1.git
134     cd $1
f11bfd 135     build_pkg $1 new -f
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 {
142     rm -rf $BUILDDIR/$1*
143     repo-remove $REPODIR/$REPONAME.db.tar.xz $1
144     rm $REPODIR/$1*
8ea4ef 145 }
JG 146
147 #Check config and create build folders
f6854c 148 #Set variables before usage
JG 149 # Usage: init
8ea4ef 150 function init {
JG 151     #check for configuration here
152     [ -z $REPODIR ] && echo "Enter REPODIR" && return 1
153     [ -z $BUILDDIR ] && echo "Enter BUILDDIR" && return 2
154     [ -z $REPONAME ] && echo "Enter REPONAME" && return 3
155
156     #make build directories
157     [ ! -d $REPODIR ] && mkdir -p $REPODIR
158     [ ! -d $BUILDDIR ] && mkdir -p $BUILDDIR
159
160     #packages required to build others
161     sudo pacman -S --noconfirm base-devel git
162
163     #add repo to pacman.conf so can install own packages
164     if [ -z $(grep "$REPONAME" /etc/pacman.conf) ]; then
165         printf "[$REPONAME]\nSigLevel = Optional TrustAll\nServer = file://$REPODIR\n" >> /etc/pacman.conf
166     fi
167
168     #create GPG key for package signing
169     if [ "$SIGN" == "Y" && "$KEY" == "" ]; then
170         (
171             echo "Key-Type: RSA"
172             echo "Key-Length: 2048"
173             echo "Subkey-Type: RSA"
174             echo "Subkey-Length: 2048"
175             echo "Passphrase: \"\""
176             echo "Expire-Date: 0"
177             echo "Name-Real: John Doe"
178             echo "Name-Comment: Arch buildbot"
179             echo "Name-Email: $(whoami)@localhost"
180             echo "%commit"
181         ) | gpg --batch --generate-key
182         gpg --export --output $REPONAME.key --armor "John Doe"
183         gpg --export-secret-keys --output $REPONAME.secret.key --armor "John Doe"
184         echo "Please change the key information in this file"
185     fi
186
187     return 0
188 }
189
86d762 190 function send_email {
C 191     (
192     echo "From: build@localhost"
193     echo "To: $EMAIL"
194     echo "Subject: Build errors"
e6f0af 195     echo "There were build errors for the build of $REPONAME at $(date), please address them soon."
C 196     echo "The errors were: $1"
86d762 197     ) | sendmail -t
C 198 }
199
8ea4ef 200 case $1 in
JG 201     "init")
202         init;;
203     "add")
204         add $2;;
205     "build-all")
206         build_all $([ "$2" == "-f" ] && echo "-f")
e6f0af 207         if [ -f $REPODIR/.errors ]; then
C 208             ERRORS=$(cat $REPODIR/.errors | tr '\n' ' ')
209             rm $REPODIR/.errors
210             echo "Errors in packages: $ERRORS"
8ea4ef 211             if [ "$EMAIL" != "" ]; then
e6f0af 212                 send_email $ERRORS
8ea4ef 213             fi
JG 214         else
215             echo "All packages built successfully"
216         fi
217         ;;
c3d80e 218     "remove")
C 219         remove $2;;
8ea4ef 220     *)
f11bfd 221         printf "Invalid usage\nUsage: $0 init|add|build-all\n";;
8ea4ef 222 esac