#!/bin/bash ####################################################### # # v0.1 # originally written by Thimo Neubauer 2003 or 2004 # # v0.2 Thu Aug 18 12:13:21 CEST 2005 # by Ingo Juergensmann # - added config file support for hostname&passwd # - changed BASEURL to use ~buildd instead of # fixed path # # v0.3 Wed Aug 24 12:14:19 CEST 2005 # by Ingo Juergensmann # - modified config file section as suggested # by Thimo Neubauer # - added exit code as suggested by Thimo Neubauer # to give an error code when login fails # - added version check and mail notification # to the buildd admin about new version when # available # # v0.4 Mon Sep 5 10:23:19 CEST 2005 # by Ingo Juergensmann # - modified tmpfile handling as suggested by # Thimo Neubauer # - added flavour support, so that the building # package will only appear on the correct buildd.net # page. # - added repitition support, so that the package name # will only transferred when it changes from the prior # package name. # # v0.5 Fri Sep 9 14:18:21 CEST 2005 # by Ingo Juergensmann # - added support for buildd status LEDs on buildd.net # (-1 not running, 0 idle, 1 building, 2 N-D-P) # # v0.6 Mon Sep 12 23:20:34 CEST 2005 (not offically released) # by Ingo Juergensmann # - added BUILDD_ADMIN_MAIL config option # - changed BASEURL to update.buildd.net # - added config file variable check # - added config file creation when none found # - smaller bug fixes and cleanups # # v0.7 Wed Sep 14 10:32:06 CEST 2005 # by Ingo Juergensmann # - included patches from Thimo Neubauer to implement # CONFDIR and BUILDDHOME # - added AUTOUPGRADE function (requested by Wouter Verhelst) # # v0.8 Fri Sep 16 09:53:12 CEST 2005 # by Ingo Juergensmann # - code cleanup for release version # # v0.81 & v0.82 Fri Sep 16 12:11:12 CEST 2005 # by Ingo Juergensmann # - quick bug fix: kfreebsd seems to need # quotes: echo "$PACKAGE" > $OLDPACKAGE # - VERSIONCHECK=`grep "^vers=" $TMPFILE | cut -f2 -d"=" ` || VERSIONCHECK=$VERSION # # v0.9 Fri Sep 23 16:36:48 CEST 2005 # by Ingo Juergensmann # - added support for hide security builds # - when buildd process is swapped out, it's listed as [buildd]. Possibly fixed. # # v0.91 Mon Dec 19 16:59:13 CET 2005 # by Ingo Juergensmann # - added support for *forced* hidden security builds: # when HIDESECURITY=force the buildd is listed as idle on buildd.net # - fixes for urlencoding in package/version and flavours # - creation of config file missed some $ before CONFFILE - fixed # # v0.92 Sun Jan 8 15:54:58 CET 2006 # by Ingo Juergensmann # - set HIDESECURITY=force as config default # - version bump for release # # v0.93 Thu Mar 30 20:20:20 CEST 2006 # by Ingo Juergensmann # - moved some reoccuring config file options to cfg_* funtions # - added support for HOSTSTATS: # you can now configure if your client should report such stats as # uptime, load average, number of users, mem & swap usage to buildd.net # - added config file support for $BUILDDUSER # - changed detection of which package is being build to ps -au $BUILDDUSER # to avoid mangled dist= lines when there are multiple sbuilds running. # Note: detection is still broken when buildd user runs several instances # of sbuild. # - use dpkg --compare-versions to check whether there's a newer version or # not to prevent downgrading when you've a local testversion installed # - version bump for release # - applied patch for code cleanup & minor bugs by Alexander Wirt # # v0.94 Wed Apr 12 13:37:23 CEST 2006 # by Ingo Juergensmann # - fixed a bug in hoststats() when uptime doesn't contains "day" # # v0.95 Wed Sep 20 16:04:18 CEST 2006 # by Ingo Juergensmann # - fixed a bug in hoststats() when uptime doesn't contains "min" # - added reporting of kernel version to hoststats() # # v0.96 Sat Feb 24 15:35:42 CET 2007 # by Ingo Juergensmann # - fixed a bug in recognition of running buildd processes # - fixed a bug in N-D-P detection # # v0.97 So 16. Dez 12:36:44 CET 2007 # by Ingo Juergensmann # - fixed uptime bug with only X mins uptime # # v0.98 Wed Dec 26 15:34:20 CET 2012 # by Ingo Juergensmann # - adopted to new sbuild # set -e VERSION=0.98 ### settings # your login-data for buildd.net # where is your settings-file? # where is the configuration? # (default: in home of user running the script) # Note: your login-data for buildd.net will need to be set in # $CONFDIR/update-buildd.net.conf CONFDIR=$HOME/.buildd.net ### end of settings PROGDIR="$(dirname `which $0`)" CONFFILE=$CONFDIR/update-buildd.net.conf OLDPACKAGE=$CONFDIR/oldpackage TMPFILE=`mktemp` || exit 1 trap "rm $TMPFILE" EXIT FLAVOUR=unstable # you won't need to configure this :) BASEURL='http://update.buildd.net/status.phtml?' usage () { echo "Usage: update-buildd.net [status|heartbeat]" echo echo "status sends the name of the package currently building." echo " Should be called every 10 minutes" echo echo "heartbeat updates host entry" echo " Should be called hourly" echo echo "stats send some host statistics" echo } upgrade_version () { if [ -n "$AUTOUPGRADE" ] ; then if [ "$AUTOUPGRADE" = "true" ] ; then mv $PROGDIR/update-buildd.net $PROGDIR/update-buildd.net.old wget -q -O $PROGDIR/update-buildd.net http://buildd.net/files/update-buildd.net chmod ug+rx $PROGDIR/update-buildd.net echo "A new version of update-buildd.net was discovered and automatically installed because you have AUTOUPGRADE=true in your config file." \ | mail -s "update-buildd.net: a new version $VERSIONCHECK was available and automatically installed" $BUILDD_ADMIN_MAIL fi fi } cfg_hidesecurity () { echo " # HIDESECURITY : enabling this will cause update-buildd.net to hide # security builds. It greps for 'secur' in \$FLAVOUR and # setting PKG=security. This is for paranoid persons, because # buildd.net hides unknown FLAVOURs itself and security is not # included on buildd.net yet (and won't be publically available) # HIDESECURITY=force will transmit an 'empty' package. # default : HIDESECURITY=force (can be true, force or false) HIDESECURITY=force " } cfg_report_host_stats () { echo " # REPORT_HOST_STATS : enabling this will result in host stats reports such as # load average, user count, memory usuage and disk space. # If set to true, you can transmit the data with commandline # option 'stats' instead of 'heartbeat'. When set to 'update' # the stats will be transferred with every 'heartbeat' whereas # 'status' will transmit the stats with every status update. # default : REPORT_HOST_STATS=false (can be false, true, update or status) REPORT_HOST_STATS=false " } check_config () { if [ -z $BUILDDUSER ] ; then BUILDDUSER=buildd fi # where does the buildd live? # (usually ~buildd) BUILDDHOME=`getent passwd ${BUILDDUSER} | cut -f6 -d":"` # where does your buildd put his progress file? PROGRESSFILE=$BUILDDHOME/build/build-progress if [ -z "$HOST" ] ; then echo "There is no HOST variable configured in $CONFFILE!" exit 1 fi if [ -z $PASSWD -o "$PASSWD" = "password" ] ; then echo "There is no PASSWD variable configured in $CONFFILE!" exit 1 fi if [ -z $HIDESECURITY ] ; then echo "There is no HIDESECURITY variable configured in $CONFFILE! Adding it." cfg_hidesecurity >> $CONFFILE HIDESECURITY=false fi if [ -z $BUILDD_ADMIN_MAIL ] ; then echo "There is no BUILDD_ADMIN_MAIL variable configured in $CONFFILE! Using local user 'buildd' as destination mail address." BUILDD_ADMIN_MAIL=buildd fi if [ -z $REPORT_HOST_STATS ] ; then cfg_report_host_stats >> $CONFFILE REPORT_HOST_STATS=false fi } collect_stats () { string1=`uptime` testdays=`echo ${string1} | grep day > /dev/null ; echo $?` testmins=`echo ${string1} | grep min > /dev/null ; echo $?` if [ "${testdays}" == "1" ] ; then if [ "${testmins}" == "0" ] ; then UPTIME=`echo $string1 | awk '{print $3" "$4}' | sed -e 's/,//g'` USERS=`echo $string1 | awk '{print $5}' | sed -e 's/,//g'` else UPTIME=`echo $string1 | awk '{print $3}' | sed -e 's/,//g'` USERS=`echo $string1 | awk '{print $4}' | sed -e 's/,//g'` fi else if [ "${testmins}" == 1 ] ; then UPTIME=`echo $string1 | awk '{print $3" "$4" "$5}' | sed -e 's/,//g'` USERS=`echo $string1 | awk '{print $6}' | sed -e 's/,//g'` else UPTIME=`echo $string1 | awk '{print $3" "$4" "$5" "$6}' | sed -e 's/,//g'` USERS=`echo $string1 | awk '{print $7}' | sed -e 's/,//g'` fi fi LAVG=`cat /proc/loadavg | awk '{print $1" "$2" "$3}' | sed -e 's/,//g'` MEMTOTAL=`cat /proc/meminfo | grep MemTotal | awk '{print $2}'` MEMFREE=`cat /proc/meminfo | grep MemFree | awk '{print $2}'` SWAPTOTAL=`cat /proc/meminfo | grep SwapTotal | awk '{print $2}'` SWAPFREE=`cat /proc/meminfo | grep SwapFree | awk '{print $2}'` KERNEL=`uname -r` HOSTSTATS="&uptime=${UPTIME}&users=${USERS}&lavg=${LAVG}&memtotal=${MEMTOTAL}&memfree=${MEMFREE}&swaptotal=${SWAPTOTAL}&swapfree=${SWAPFREE}&kernel=${KERNEL}" } # check sys.argv if [ $# != 1 ] ; then usage exit 0 fi #check for config file #if its readable it can be tested for upgrades and sanity if [ -r $CONFFILE ] ; then source $CONFFILE check_config else #the config dir don't exists if [ ! -e $CONFDIR ] ; then echo -n "No $CONFDIR existing... " if mkdir -p $CONFDIR ; then echo "created." else echo "Could not create config dir" exit 0 fi fi echo "There is no $CONFFILE - creating one" ( cat << EOF ########################################################### # # Configuration file for update-buildd.net # # HOST : your buildd name in buildd.net DB # default: HOST=$(hostname) HOST=$(hostname) # PASSWD : your password for buildd.net # default: PASSWD=password PASSWD=password # BUILDD_ADMIN_MAIL : the email address of the buildd admin or host admin # default: BUILDD_ADMIN_MAIL=buildd BUILDD_ADMIN_MAIL=buildd # AUTOUPGRADE : enabling this will cause update-buildd.net to upgrade itself # when a new version is available. USE WITH CARE! # default : AUTOUPGRADE=false (can be true or false) AUTOUPGRADE=false EOF ) > $CONFFILE cfg_hidesecurity >> $CONFFILE echo "" >> $CONFFILE cfg_report_host_stats >> $CONFFILE echo "" >> $CONFFILE exit 1 fi case $1 in status) ACTION="name" ;; heartbeat) ACTION="update" ;; stats) ACTION="stats" ;; *) echo Unknown parameter echo usage exit 1;; esac # trick: the CGI-script doesn't change the package name if an empty # one is supplied. Set to a space so that package vanishes when buildd is idle # In fact, Pythons cgi parser doesn't include the package part in its form, when # the string is empty. So, this is a workaround. PACKAGE=" " check_package () { #check for running buildd" if pgrep -f '(\[buildd$\]|bin\/buildd$)' >& /dev/null ; then # buildd is running" if [ -e $BUILDDHOME/NO-DAEMON-PLEASE -o -e $BUILDDHOME/build/NO-DAEMON-PLEASE ] ; then # N-D-P exists, LED should be yellow BUILDDSTATUS=2 else # check for current package" if [ -f $PROGRESSFILE ] ; then # is the buildd running? If not, the content of the progress file is wrong # buildd runs and should be building BUILDDSTATUS=0 if pgrep -u $BUILDDUSER -f 'sbuild.*--dist=' >& /dev/null ; then # ok, analyze content of file # PACKAGE=`sed -ne 's/\: currently.*//p' < $PROGRESSFILE | sed -e 's/+/%2b/g'` PACKAGE=`sed -ne 's/\: building.*//p' < $PROGRESSFILE | sed -e 's/+/%2b/g'` FLAVOUR=`ps aU $BUILDDUSER | grep sbuild | grep -v grep | sed -e 's/.*--dist=//' -e 's/ .*//' | sed -e 's/sid/unstable/g' | grep '[[:alpha:]]' | head -1` BUILDDSTATUS=1 if [ -n "$HIDESECURITY" -a "$HIDESECURITY" = "true" ] ; then if [ `echo $FLAVOUR | grep secur` ]; then PACKAGE="security" fi elif [ -n "$HIDESECURITY" -a "$HIDESECURITY" = "force" ] ; then if [ `echo $FLAVOUR | grep secur` ]; then # hidesecurity set to force -> mask the buildd as being idle PACKAGE=" " FLAVOUR="unstable" BUILDDSTATUS=0 fi fi fi else BUILDDSTATUS=0 PACKAGE=" " fi fi else BUILDDSTATUS=-1 fi } # ok, we're prepared: send data if [ "$ACTION" == "update" ]; then # update -> just heartbeat - the machine is alive #check_package FULLURL="${BASEURL}${ACTION}=${HOST}&p=${PASSWD}&buildd=${BUILDDSTATUS}" if [ "$REPORT_HOST_STATS" == "update" ] ; then collect_stats FULLURL="${FULLURL}${HOSTSTATS}" fi elif [ "$ACTION" == "stats" ]; then collect_stats FULLURL="${BASEURL}${ACTION}=${HOST}&p=${PASSWD}${HOSTSTATS}" elif [ "$ACTION" == "name" ]; then check_package # name -> transmit all information that need to transmitted" FULLURL="${BASEURL}${ACTION}=${HOST}&p=${PASSWD}&building=${PACKAGE}&flavour=${FLAVOUR}&buildd=${BUILDDSTATUS}" if [ "$REPORT_HOST_STATS" == "status" ] ; then collect_stats FULLURL="${FULLURL}${HOSTSTATS}" fi if [ -r "$OLDPACKAGE" ] ; then if [ "$(cat $OLDPACKAGE)" = "${PACKAGE}" ] ; then #echo "# test showed that building package is equal to the prior reported package -> no update needed, exiting" exit 0 else # packages are different, so output the new package name to control file and proceed" echo "$PACKAGE" > $OLDPACKAGE fi else echo "$PACKAGE" > $OLDPACKAGE fi fi wget --no-proxy -q -U "update-buildd.net/v${VERSION}" -O $TMPFILE "$FULLURL" # for debugging purpose uncomment the following line #cat $TMPFILE VERSIONCHECK=`grep "^vers=" $TMPFILE | cut -f2 -d"=" ` || VERSIONCHECK=$VERSION CHECK_OK=`grep OK $TMPFILE` || CHECK_OK="NACK" # check if the version is uptodate if [ "$CHECK_OK" = "OK" ]; then RC=0 if $(dpkg --compare-versions ${VERSION} ge ${VERSIONCHECK}); then if [ -e $CONFDIR/new_version_check ]; then rm $CONFDIR/new_version_check fi else if [ ! -e $CONFDIR/new_version_check ]; then if [ -n "$AUTOUPGRADE" -a "$AUTOUPGRADE" = "true" ] ; then upgrade_version else echo "Please download new version from http://www.buildd.net/files/update-buildd.net" \ | mail -s "update-buildd.net: new version $VERSIONCHECK available" $BUILDD_ADMIN_MAIL touch $CONFDIR/new_version_check fi fi fi else echo "Something wicked happened. Exiting..." RC=1 fi exit $RC