- redirect stderr to the log file
[build.git] / build.sh
1 #!/bin/bash
2 #
3 # PlanetLab release build script. Intended to be used by scripts and
4 # crontabs to build nightly releases (default). Can also be invoked
5 # manually to build a tagged release (-r) in the current directory.
6 #
7 # $Id: build.sh,v 1.12 2004/09/22 15:21:18 mlhuang Exp $
8 #
9
10 # Set defaults
11 CVSROOT=:pserver:anon@build.planet-lab.org:/cvs
12 CVS_RSH=ssh
13 MODULE=build
14 TAG=HEAD
15 BASE=$PWD
16
17 # Alpha node repository
18 ALPHA_BOOT=build@boot.planet-lab.org
19 ALPHA_ROOT=/www/planetlab/install-rpms/archive/planetlab-alpha
20 ALPHA_RPMS=/www/planetlab/install-rpms/planetlab-alpha
21
22 # Export certain variables
23 export CVS_RSH
24
25 # Get options
26 while getopts "d:r:m:b:x:h" opt ; do
27     case $opt in
28         d)
29             CVSROOT=$OPTARG
30             ;;
31         r)
32             TAG=$OPTARG
33             ;;
34         m)
35             MAILTO=$OPTARG
36             ;;
37         b)
38             BASE=$OPTARG
39             ;;
40         x)
41             BUILDS=$OPTARG
42             ;;
43         h|*)
44             echo "usage: `basename $0` [OPTION]..."
45             echo "      -d directory    CVS repository root (default $CVSROOT)"
46             echo "      -r revision     CVS revision to checkout (default $TAG)"
47             echo "      -m address      Notify recipient of failures (default: none)"
48             echo "      -b base         Run operations in specified base directory (default $BASE)"
49             echo "      -x N            Remove all but the last N runs from the base directory (default: none)"
50             exit 1
51             ;;
52     esac
53 done
54
55 # Base operations in specified directory
56 mkdir -p $BASE
57 cd $BASE || exit $?
58
59 # Remove old runs
60 if [ -n "$BUILDS" ] ; then
61     ls -t | sed -n ${BUILDS}~1p | xargs rm -rf
62 fi
63
64 # Create a unique build base
65 BASE=${TAG/HEAD/`date +%Y.%m.%d`}
66 i=
67 while ! mkdir ${BASE}${i} 2>/dev/null ; do
68     [ -z ${i} ] && BASE=${BASE}.
69     i=$((${i}+1))
70     if [ $i -gt 100 ] ; then
71         echo "$0: Failed to create release directory `pwd`/${BASE}${i}"
72         exit 1
73     fi
74 done
75 BASE=${BASE}${i}
76
77 # Redirect output from here
78 exec 2>&1
79 exec &>${BASE}/log
80
81 # XXX Hack to store the pup key as well as the bui key
82 eval `ssh-agent`
83 for i in `grep -l "BEGIN.*PRIVATE KEY" $HOME/.ssh/*` ; do
84     SSH_ASKPASS=/bin/false ssh-add $i
85 done
86
87 # Build
88 cvs -d ${CVSROOT} export -r ${TAG} -d ${BASE} ${MODULE}
89 make -C ${BASE}
90
91 if [ $? -ne 0 ] ; then
92     # Notify recipient of failure or just dump to stdout
93     if [ -n "$MAILTO" ] ; then
94         NOTIFY="mail -s 'Failures for ${BASE}' $MAILTO"
95     else
96         NOTIFY=cat
97     fi
98     (
99     # Dump log
100     if [ -f ${BASE}/log ] ; then
101         tail -100 ${BASE}/log
102     else
103         echo "Error $?"
104     fi
105     ) | eval $NOTIFY
106 elif [ -n "$BUILDS" ] ; then
107     # Remove old nightly runs
108     echo "cd ${ALPHA_ROOT} && ls -t | sed -n ${BUILDS}~1p | xargs rm -rf" | ssh ${ALPHA_BOOT} sh -s
109     # Update alpha node repository
110     for i in RPMS SRPMS ; do
111         ssh ${ALPHA_BOOT} mkdir -p ${ALPHA_ROOT}/${BASE}/${i}
112         find ${BASE}/${i} -type f | xargs -i scp {} ${ALPHA_BOOT}:${ALPHA_ROOT}/${BASE}/${i}
113         ssh ${ALPHA_BOOT} yum-arch ${ALPHA_ROOT}/${BASE}/${i} >/dev/null
114     done
115     # Update symlink
116     ssh ${ALPHA_BOOT} ln -nsf ${ALPHA_ROOT}/${BASE}/RPMS/ ${ALPHA_RPMS}
117 fi
118
119 # Kill the current agent
120 ssh-agent -k