Classic Shell Scripting - Arnold Robbins [119]
* * *
Example 8-2. The build-all program
#! /bin/sh -
# Build one or more packages in parallel on one or more build hosts.
#
# Usage:
# build-all [ --? ]
# [ --all "..." ]
# [ --check "..." ]
# [ --configure "..." ]
# [ --environment "..." ]
# [ --help ]
# [ --logdirectory dir ]
# [ --on "[user@]host[:dir][,envfile] ..." ]
# [ --source "dir ..." ]
# [ --userhosts "file(s)" ]
# [ --version ]
# package(s)
#
# Optional initialization files:
# $HOME/.build/directories list of source directories
# $HOME/.build/userhosts list of [user@]host[:dir][,envfile]
IFS='
'
PATH=/usr/local/bin:/bin:/usr/bin
export PATH
UMASK=002
umask $UMASK
build_one( )
{
# Usage:
# build_one [user@]host[:build-directory][,envfile]
arg="`eval echo $1`"
userhost="`echo $arg | sed -e 's/:.*$//'`"
user="`echo $userhost | sed -e s'/@.*$//'`"
test "$user" = "$userhost" && user=$USER
host="`echo $userhost | sed -e s'/^[^@]*@//'`"
envfile="`echo $arg | sed -e 's/^[^,]*,//'`"
test "$envfile" = "$arg" && envfile=/dev/null
builddir="`echo $arg | sed -e s'/^.*://' -e 's/,.*//'`"
test "$builddir" = "$arg" && builddir=/tmp
parbase=`basename $PARFILE`
# NB: update find_package( ) if these patterns are changed
package="`echo $parbase | \
sed -e 's/[.]jar$//' \
-e 's/[.]tar[.]bz2$//' \
-e 's/[.]tar[.]gz$//' \
-e 's/[.]tar[.]Z$//' \
-e 's/[.]tar$//' \
-e 's/[.]tgz$//' \
-e 's/[.]zip$//'`"
# Copy the package file if we cannot see it on the remote host
echo $SSH $SSHFLAGS $userhost "test -f $PARFILE"
if $SSH $SSHFLAGS $userhost "test -f $PARFILE"
then
parbaselocal=$PARFILE
else
parbaselocal=$parbase
echo $SCP $PARFILE $userhost:$builddir
$SCP $PARFILE $userhost:$builddir
fi
# Unbundle the archive file on the remote host, build, and
# check it, running in the background
sleep 1 # to guarantee unique log filename
now="`date $DATEFLAGS`"
logfile="$package.$host.$now.log"
nice $SSH $SSHFLAGS $userhost "
echo '= = = = = = = = = = = = = = = = = = = = = = = =
= = = = = = = = = = = = = = = = = = = = = = = = = = =' ;
test -f $BUILDBEGIN && . $BUILDBEGIN || \
test -f $BUILDBEGIN && source $BUILDBEGIN || \
true ;
echo 'Package: $package' ;
echo 'Archive: $PARFILE' ;
echo 'Date: $now' ;
echo 'Local user: $USER' ;
echo 'Local host: `hostname`' ;
echo 'Local log directory: $LOGDIR' ;
echo 'Local log file: $logfile' ;
echo 'Remote user: $user' ;
echo 'Remote host: $host' ;
echo 'Remote directory: $builddir' ;
printf 'Remote date: ' ;
date $DATEFLAGS ;
printf 'Remote uname: ' ;
uname -a || true ;
printf 'Remote gcc version: ' ;
gcc --version | head -n 1 || echo ;
printf 'Remote g++ version: ' ;
g++ --version | head -n 1 || echo ;
echo 'Configure environment: `$STRIPCOMMENTS $envfile | $JOINLINES`' ;
echo 'Extra environment: $EXTRAENVIRONMENT' ;
echo 'Configure directory: $CONFIGUREDIR' ;
echo 'Configure flags: $CONFIGUREFLAGS' ;
echo 'Make all targets: $ALLTARGETS' ;
echo 'Make check targets: $CHECKTARGETS' ;
echo 'Disk free report for $builddir/$package:' ;
df $builddir | $INDENT ;
echo 'Environment:' ;
env | env LC_ALL=C sort | $INDENT ;
echo '= = = = = = = = = = = = = = = = = = = = = = = =
= = = = = = = = = = = = = = = = = = = = = = = = = = =' ;
umask $UMASK ;
cd $builddir || exit 1 ;
/bin/rm -rf $builddir/$package ;
$PAR $parbaselocal ;
test "$parbase" = "$parbaselocal"