- also delete parseSpec binary, so that it gets regenerated
[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.37 2007/01/20 14:08:42 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 # Checkout build directory
109 cvs -d ${CVSROOT} checkout -r ${TAG} -d ${BASE} ${MODULE}
110
111 # Build development environment first
112 make TAG=${TAG} PLDISTRO=${PLDISTRO} -C ${BASE} myplc-devel
113
114 # Build everything else inside the development environment
115 export PLC_ROOT=$BASE/BUILD/myplc-devel-*/myplc/devel/root
116 export PLC_DATA=$BASE/BUILD/myplc-devel-*/myplc/devel/data
117
118 cleanup() {
119     sudo umount $PLC_ROOT/data/build
120     sudo $BASE/BUILD/myplc-devel-*/myplc/host.init stop
121 }
122
123 trap "cleanup; failure" ERR INT
124
125 # Start development environment
126 sudo $BASE/BUILD/myplc-devel-*/myplc/host.init start
127
128 # Cross mount the current build directory to the build user home directory
129 sudo mount -o bind,rw $BASE $PLC_ROOT/data/build
130
131 # Delete .rpmmacros and parseSpec files so that they get regenerated
132 # appropriately in the development environment.
133 rm -f .rpmmacros parseSpec
134
135 # Enable networking
136 sudo cp -f /etc/hosts /etc/resolv.conf $PLC_ROOT/etc/
137
138 # Run the rest of the build
139 sudo chroot $PLC_ROOT su - build -c "make TAG=\"$TAG\" PLDISTRO=\"$PLDISTRO\""
140
141 # Clean up
142 cleanup
143 trap failure ERR INT
144
145 # Install to boot server
146 make TAG=${TAG} PLDISTRO=${PLDISTRO} -C ${BASE} install BASE=$BASE BUILDS=$BUILDS
147
148 exit 0