#!/bin/sh
# Example script for automatically updating sbuild chroots
#
# Simply create a crontab /etc/cron.d/sbuild-update-all and specify the
# schedule that you want to use. The behaviour of this script can be influenced
# by the following evironment variables:
#
#   PATTERN     glob pattern to match the chroot config name against, in
#               directory /etc/schroot/chroot.d/.
#
#               Default: *-sbuild
#
#   UPDATEARGS  The arguments with which sbuild-update will be invoked.
#
#               Default: --update --dist-upgrade --autoclean --autoremove
#
#   LOGFILE     Log file to write to. These files are not rotated, you must set
#               this up yourself. See logrotate(8)
#
#               Default: /var/log/sbuild-update-all.log
#
# This script will refuse to run if another instance of it is running. In fact,
# it will refuse to run if there is an active chroot session (regardless of
# whether it is related to the matched patterns or not). Care for this must be
# taken when scheduling cron jobs, as crontabs are processed sequentially (with
# regards to variable assignment), but jobs are executed in parallel. 
#
# Examples
# ========
#
# 1. Update all sbuild chroots four times a day (at 00:15/06:15/12:15/18:15):
#
#     15 */6 * * * root /usr/share/doc/sbuild/examples/sbuild-debian-developer-setup-update-all
#
# 2. Update all sid sbuild chroots daily, and all jessie sbuild chroots weekly,
#    and log the latter to a separate file:
#
#     PATTERN = sid-*-sbuild
#     @daily    root /usr/share/doc/sbuild/examples/sbuild-debian-developer-setup-update-all
#
#     PATTERN = jessie-*-sbuild
#     LOGFILE = /var/log/wheezy-chroot-update.log
#     @weekly   root /usr/share/doc/sbuild/examples/sbuild-debian-developer-setup-update-all
#
# The following will NOT work. Both of these jobs are executed at 00:15, so
# the second one will refuse to run:
#
#     PATTERN = foo-*
#     15 0  * * * root /usr/share/doc/sbuild/examples/sbuild-debian-developer-setup-update-all
#
#     PATTERN = bar-*
#     15 0  * * * root /usr/share/doc/sbuild/examples/sbuild-debian-developer-setup-update-all


# Output of sbuild-update invocations will be written to this file
LOGFILE=${LOGFILE:-/var/log/sbuild-update-all.log}

# Arguments passed to sbuild-update
UPDATEARGS=${UPDATEARGS:-"--update --dist-upgrade --autoclean --autoremove"}

# chroot config names are identified by this pattern
PATTERN=${PATTERN:-*-sbuild-*}


# Open logfile for output, make sure we're the only instance active
exec 8>>$LOGFILE
if ! flock -x -n 8
then
	echo "$0: another instance is already running"
	exit 1
fi
# Redirect stdout to logfile
exec 9>&1
exec 1>&8

if ! ls /etc/schroot/chroot.d/$PATTERN >/dev/null 2>&1
then
	echo "No chroots defined"
	exit 0
fi

for fullname in /etc/schroot/chroot.d/$PATTERN
do
	confname=`basename $fullname | sed -e "s/-sbuild-.*//"`
    
	# Check for *any* active session and skip if there is one.
	# chroots can share common factors, for example an LVM volume, so it's
	# better to be safe than sorry.
	if  [ -n "`schroot -l --all-sessions`" ]
	then
		echo "Active schroot session, will not continue"
		break
	fi

	echo "****** `date` ******"
	echo "Action: sbuild-update $UPDATEARGS $confname"

	if ! sbuild-update $UPDATEARGS $confname
	then
		echo "ERROR: failed to update $confname"
		continue
	fi
done

# Release lock and undo redirections
exec 8>&- 1>&9 9>&-
