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