#!/bin/bash # File: maat-builder # # Description: This file allows a Maat host to build source packages # and optionally upload to the AniNIX::Foundation # # Package: ConfigPackages/Maat # Copyright: WTFPL # # Author: darkfeather@aninix.net # ### Printing defaults passCell="PASS"; failCell="FAIL"; warnCell="N/A"; ### Add helptext. function Usage() { echo "Usage: $0" echo " $0 [ -b basedir ] [ -c AUR.git.list ] [ -u https://base.url/ ]" echo " $0 [ -T ]" echo " $0 -h" echo echo 'Add -v to increase verbosity or -h for help. Add the -l LOGFILE flags to log to a file' echo "By default, $0 uses the depriv user to build packages. Override with -U" } ### Put the initial content in the webfile function SeedWebFile() { printf '\n\nAniNIX::Maat \\\\ Build Results\n\n\n\n\n\n\n\n

AniNIX::Maat \\\\ Build Status

\nWEBSTATSGOHERE\n' > "$webfile" } ### Update the webfile to close up table tags and add stats. function UpdateWebFile() { sed -i "s#WEBSTATSGOHERE#

These are the AniNIX testing results. We found $passcount passing and $failcount failing packages, with $warncount warnings. It took $runtime seconds to finish.

#" "$webfile" echo '
PackageTesting StatusBuild StatusLatest BuildTime of Run
' >> "$webfile" } ### Build the package. Assumes a PKGBUILD is resent in the repo. function BuildPackage() { timeout --preserve-status 30s sudo -u "$user" makepkg -s --noconfirm &>/dev/null if [ $? -ne 0 ]; then # Build failed. printf "$failCell""$warnCell" >> "$webfile" else # Build passed. printf "$passCell""" >> "$webfile" # List passing versions ls -1 *.pkg.tar.xz | tr '\n' '`' | sed 's#`#
#g' >> "$webfile" printf "" >> "$webfile" fi for pkg in `find . -type f | egrep '.tar.xz$'`; do mv "$pkg" "$pkgdir"; done } ### Build the repo passed as argument # param repo: the repo to build. function BuildRepo() { repo="$1" cd "$srcdir" if [ -z "$repo" ]; then continue; fi repodir="$(basename "$repo" | sed 's/\.git$//')" #Set up the checkout if [ ! -d "$repodir" ]; then git clone "$repo" fi chown -R "$user": "$repodir" cd "$repodir" output="$(git pull 2>&1)" if [ $? -eq 0 ] && [ -n "$incremental" ] && [[ "$output" =~ Already\ up\ to\ date. ]] && [ -f *.tar.xz ]; then return; fi # Find the PKGBuilds in the repo for pkgbuild in `find . -type f -name PKGBUILD`; do cd "$(dirname "$pkgbuild")" # Tell the status file about it. printf ''"$repodir -- $pkgbuild"'' >> "$webfile" if [ -f Makefile ] && [ `egrep -c '^test:' Makefile` -ge 1 ]; then # Check test status. timeout --preserve-status 30s /bin/bash -c "sudo -u \'$user\' make test" &>/dev/null if [ $? -ne 0 ]; then # Testing failed. printf "$failCell""$warnCell""$warnCell" >> "$webfile" else # Testing passed. printf "$passCell" >> "$webfile" BuildPackage fi else # Can't test -- usually from non-AniNIX repos. printf "$warnCell" >> "$webfile" BuildPackage fi # Timestamp printf "$(date +%F-%R)\n" >> "$webfile" cd "$cwd" if [ ! -z "$testing" ]; then break; fi done cd "$cwd" } ### Update the local repo function UpdateLocalRepo() { cd "$pkgdir" # TODO Add deduplication of updated files -- keep latest 3 versions. repo-add --new ./AniNIX::Maat.db.tar.xz `ls -1 *.tar.xz` } # Production defaults baseurl='https://aninix.net/foundation/' aurconf='/usr/local/etc/Maat/aur.list' homedir=/srv/maat/ unset upload unset testing unset incremental user="depriv" # Stat tracking starttime=`date +%s` # Parse arguments while getopts 'b:c:hil:Tu:U:v' OPTION; do case "${OPTION}" in b) homedir="${OPTARG}" ;; c) aurconf="${OPTARG}" ;; h) Usage; exit 0 ;; i) incremental=1 ;; l) cmdstring="$0"; for arg in $@; do if [ "$arg" != "-l" ] && [ "$arg" != "${OPTARG}" ]; then cmdstring="$cmdstring \"${arg}\""; fi; done; exec /bin/bash -c "$0 $cmdstring | tee -a \"${OPTARG}\"" ;; T) homedir='.'; testing=1 ;; u) baseurl="${OPTARG}" ;; U) user="${OPTARG}" ;; v) set -x ;; *) usage; exit 1 ;; esac done # Ensure work directories live if [ $( echo "$homedir" | egrep -c '^/') -ne 1 ]; then homedir="${PWD}/${homedir}" fi srcdir="${homedir}/src" && mkdir -p "${srcdir}" pkgdir="${homedir}/pkg" && mkdir -p "${pkgdir}" webdir="$pkgdir" webfile="$webdir"/index.html # For each repo listed at the CGIT URL and conf file cwd="$(pwd)" SeedWebFile for aninixrepo in `wget -q -O - "$baseurl" | grep toplevel-repo | cut -f 4 -d \' | sed "s#^#$baseurl#"`; do BuildRepo "$aninixrepo" if [ ! -z "$testing" ]; then break; fi done for repo in `cat "$aurconf"`; do BuildRepo "$repo"; if [ ! -z "$testing" ]; then break; fi done UpdateLocalRepo runtime=$(( `date +%s` - $starttime )) # Update stats failcount=$(grep -c "$failCell" "$webfile") warncount=$(grep -c "$warnCell" "$webfile") passcount=$( grep -v "$failCell" "$webfile" | grep -c "$passCell" ) UpdateWebFile # Exit