- infer tag from CVS meta-data
[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 # Mark Huang <mlhuang@cs.princeton.edu>
8 # Copyright (C) 2003-2005 The Trustees of Princeton University
9 #
10 # $Id: build.sh,v 1.35 2006/07/06 14:57:50 mlhuang Exp $
11 #
12
13 # Set defaults
14 if [ -f CVS/Root ] ; then
15     CVSROOT=$(cat CVS/Root)
16     TAG=$(cvs status build.sh | sed -ne 's/[[:space:]]*Sticky Tag:[[:space:]]*\([^[:space:]]*\).*/\1/p')
17     if [ "$TAG" = "(none)" ] ; then
18         TAG=HEAD
19     fi
20 else
21     CVSROOT=:pserver:anon@cvs.planet-lab.org:/cvs
22     TAG=HEAD
23 fi
24 CVS_RSH=ssh
25 MODULE=build
26 BASE=$PWD
27 PLDISTRO=planetlab
28
29 # cron does not set USER?
30 [ -z "$USER" ] && export USER=$LOGNAME
31
32 # Export certain variables
33 export CVS_RSH
34
35 # Get options
36 while getopts "d:r:m:f:b:x:h" opt ; do
37     case $opt in
38         d)
39             CVSROOT=$OPTARG
40             ;;
41         r)
42             TAG=$OPTARG
43             ;;
44         m)
45             MAILTO=$OPTARG
46             ;;
47         f)
48             PLDISTRO="$OPTARG"
49             ;;
50         b)
51             BASE=$OPTARG
52             ;;
53         x)
54             BUILDS=$OPTARG
55             ;;
56         h|*)
57             echo "usage: `basename $0` [OPTION]..."
58             echo "      -d directory    CVS repository root (default $CVSROOT)"
59             echo "      -r revision     CVS revision to checkout (default $TAG)"
60             echo "      -m address      Notify recipient of failures (default: none)"
61             echo "      -f distro       Distribution to build (default: $PLDISTRO)"
62             echo "      -b base         Run operations in specified base directory (default $BASE)"
63             echo "      -x N            Remove all but the last N runs from the base directory (default: none)"
64             exit 1
65             ;;
66     esac
67 done
68 shift $(($OPTIND - 1))
69
70 # Base operations in specified directory
71 mkdir -p $BASE
72 cd $BASE || exit $?
73
74 # Remove old runs
75 if [ -n "$BUILDS" ] ; then
76     ls -t | sed -n ${BUILDS}~1p | xargs rm -rf
77 fi
78
79 # Create a unique build base
80 BASE=${TAG/HEAD/`date +%Y.%m.%d`}
81 i=
82 while ! mkdir ${BASE}${i} 2>/dev/null ; do
83     [ -z ${i} ] && BASE=${BASE}.
84     i=$((${i}+1))
85     if [ $i -gt 100 ] ; then
86         echo "$0: Failed to create release directory `pwd`/${BASE}${i}"
87         exit 1
88     fi
89 done
90 BASE=${BASE}${i}
91
92 # Redirect output from here
93 exec 2>&1
94 exec &>${BASE}/log
95
96 failure() {
97     # Notify recipient of failure
98     if [ -n "$MAILTO" ] ; then
99         tail -c 8k ${BASE}/log | mail -s "Failures for ${BASE}" $MAILTO
100     fi
101     exit 1
102 }
103
104 trap failure ERR INT
105
106 # Checkout build directory
107 cvs -d ${CVSROOT} checkout -r ${TAG} -d ${BASE} ${MODULE}
108
109 # Build development environment first
110 make TAG=${TAG} PLDISTRO=${PLDISTRO} -C ${BASE} myplc-devel
111
112 # Build everything else inside the development environment
113 export PLC_ROOT=$BASE/BUILD/myplc-devel-*/myplc/devel/root
114 export PLC_DATA=$BASE/BUILD/myplc-devel-*/myplc/devel/data
115
116 cleanup() {
117     sudo umount $PLC_ROOT/data/build
118     sudo $BASE/BUILD/myplc-devel-*/myplc/host.init stop
119 }
120
121 trap "cleanup; failure" ERR INT
122
123 # Start development environment
124 sudo $BASE/BUILD/myplc-devel-*/myplc/host.init start
125
126 # Cross mount the current build directory to the build user home directory
127 sudo mount -o bind,rw $BASE $PLC_ROOT/data/build
128
129 # Delete .rpmmacros file so that it gets regenerated with the
130 # appropriate paths relative to the chroot.
131 rm -f .rpmmacros
132
133 # Enable networking
134 sudo cp -f /etc/hosts /etc/resolv.conf $PLC_ROOT/etc/
135
136 # Run the rest of the build
137 sudo chroot $PLC_ROOT su - build -c "make TAG=\"$TAG\" PLDISTRO=\"$PLDISTRO\""
138
139 # Clean up
140 cleanup
141 trap failure ERR INT
142
143 # Install to boot server
144 make TAG=${TAG} PLDISTRO=${PLDISTRO} -C ${BASE} install BASE=$BASE BUILDS=$BUILDS
145
146 exit 0