(*) reviewed make variable naming scheme: uses -UPPER-CASE for the ones set in .mk...
[build.git] / vbuild-nightly.sh
1 #!/bin/bash
2 REVISION=$(echo '$Revision$' | sed -e 's,\$,,g' -e 's,^\w*:\s,,' )
3
4 COMMANDPATH=$0
5 COMMAND=$(basename $0)
6
7 # default values, tunable with command-line options
8 DEFAULT_FCDISTRO=fc6
9 DEFAULT_PLDISTRO=planetlab
10 DEFAULT_BASE="@DATE@--@PLDISTRO@-@FCDISTRO@"
11 DEFAULT_SVNPATH="http://svn.planet-lab.org/svn/build/trunk"
12
13 # NOTE: do not think we want to put email addresses into scripts
14 # that can be harvested by spambots. --mef
15 DEFAULT_MAILTO="onelab-build@one-lab.org"
16
17 # web publishing results
18 DEFAULT_WEBPATH="/build/@PLDISTRO@/"
19
20 # for the test part
21 TESTBUILDURL="http://build.one-lab.org/"
22 TESTBOX=onelab-test.inria.fr
23 TESTBOXSSH=root@onelab-test.inria.fr
24 TESTSVNPATH="http://svn.planet-lab.org/svn/tests/trunk/system/"
25 TESTSCRIPT=TestMain.py
26 ####################
27 # assuming vserver runs in UTC
28 DATE=$(date +'%Y.%m.%d')
29
30 # temporary - wrap a quick summary of suspicious stuff
31 # this is to focus on installation that go wrong
32 # use with care, a *lot* of other things can go bad as well
33 function summary () {
34     from=$1; shift
35     echo "******************** BEG SUMMARY" 
36     tr -d '\r' < $from | egrep 'BEG RPM|not installed|Installing:.*([eE]rror|[wW]arning)' 
37     echo "******************** END SUMMARY" 
38 }
39
40
41 # Notify recipient of failure or success, manage various stamps 
42 function failure() {
43     set -x
44     WEBLOG=${WEBPATH}/${BASE}.log.txt
45     cp $LOG ${WEBLOG}
46     summary $LOG >> ${WEBLOG}
47     (echo -n "============================== $COMMAND: failure at " ; date ; tail -c 20k $WEBLOG) > ${WEBLOG}.ko
48     if [ -n "$MAILTO" ] ; then
49         tail -c 20k ${WEBPATH}/${BASE}.log.txt | mail -s "Failures for build ${BASE}" $MAILTO
50     fi
51     exit 1
52 }
53
54 function success () {
55     set -x
56     WEBLOG=${WEBPATH}/${BASE}.log.txt
57     cp $LOG ${WEBLOG}
58     summary $LOG >> ${WEBLOG}
59     touch ${WEBLOG}.ok
60     if [ -n "$MAILTO" ] ; then
61         (echo "$PLDISTRO ($BASE) build for $FCDISTRO completed on $(date)" ) | mail -s "Successfull build for ${BASE}" $MAILTO
62     fi
63     exit 0
64 }
65
66 # run in the vserver - do not manage success/failure, will be done from the root ctx
67 function build () {
68     set -x
69     set -e
70
71     echo -n "============================== Starting $COMMAND:build on "
72     date
73
74     cd /
75
76   # if TAGSRELEASE specified : update PLDISTROTAGS with this tag
77     if [ -n "$TAGSRELEASE" ] ; then
78         cd build
79         svn up -r $TAGSRELEASE $PLDISTROTAGS
80         cd - 
81     fi
82
83     show_env
84     
85     echo "Running make IN $(pwd)"
86     make stage1=true "${MAKEOPTS[@]}" PLDISTROTAGS=${PLDISTROTAGS} PLDISTRO=${PLDISTRO} "${MAKEVARS[@]}" -C /build
87     # in case we use an older build that does not know about versions
88     set +e
89     make "${MAKEOPTS[@]}" PLDISTROTAGS=${PLDISTROTAGS} PLDISTRO=${PLDISTRO} "${MAKEVARS[@]}" -C /build versions
90     set -e
91     make "${MAKEOPTS[@]}" PLDISTROTAGS=${PLDISTROTAGS} PLDISTRO=${PLDISTRO} "${MAKEVARS[@]}" -C /build $MAKETARGETS
92
93 }
94
95 # this was formerly run in the myplc-devel chroot but now is run in the root context,
96 # this is so that the .ssh config gets done manually, and once and for all
97 function runtest () {
98     set -x
99     set -e
100     trap failure ERR INT
101
102     echo -n "============================== Starting $COMMAND:runtest on $(date)"
103     here=$(pwd)
104
105     ### the URL to the myplc package
106     cd /vservers/$BASE/build/RPMS/i386
107     rpm=$(ls myplc-[0-9]*.rpm)
108     if [ ${#rpm[@]} != 1 ] ; then
109         echo "$COMMAND: Cannot locate rpm for testing"
110         failure
111         exit 1
112     fi
113     url=${TESTBUILDURL}${PLDISTRO}/${BASE}/RPMS/i386/${rpm}
114
115     # checkout the system test (formerly known as plctest)
116     cd /vservers/${BASE}/build
117     rm -rf TESTS
118     svn export $TESTSVNPATH TESTS
119     # dont trust retcod
120     if [ ! -d TESTS ] ; then 
121         echo "$COMMAND: could not svn export $SVNPATH - check url"
122         exit 1
123     fi
124
125   # compute test directory name on test box
126     testdir=plctest-${BASE}
127   # rsync/push test material onto the test box - clean first
128     ssh ${TESTBOXSSH} rm -rf ${testdir}
129     ssh ${TESTBOXSSH} mkdir -p ${testdir}
130     rsync -a -v TESTS/ ${TESTBOXSSH}:${testdir}/
131   # invoke test on testbox
132     ssh ${TESTBOXSSH} python -u ${testdir}/${TESTSCRIPT} ${url} 
133   #invoke make install from build to the testbox
134   # looks suspicious : we'd need this *during* myplc run, not at the end
135   # in addition we are not in the vserver here so running make can have weird effects
136   # make install PLCHOST=${TESTBOX}
137         
138     if [ "$?" != 0 ] ; then
139         failure
140     fi
141     
142     cd $here
143     echo -n "============================== End $COMMAND:runtest on $(date)"
144 }
145
146 function show_env () {
147     set +x
148     echo FCDISTRO=$FCDISTRO
149     echo PLDISTRO=$PLDISTRO
150     echo BASE=$BASE
151     echo SVNPATH=$SVNPATH
152     echo MAKEVARS="${MAKEVARS[@]}"
153     echo MAKEOPTS="${MAKEOPTS[@]}"
154     echo PLDISTROTAGS="$PLDISTROTAGS"
155     echo TAGSRELEASE="$TAGSRELEASE"
156     echo -n "(might be unexpanded)"
157     echo WEBPATH="$WEBPATH"
158     if [ -d /vservers ] ; then
159         echo PLDISTROTAGS="$PLDISTROTAGS"
160     else
161         echo "XXXXXXXXXXXXXXXXXXXX Contents of tags definition file /build/$PLDISTROTAGS"
162         cat /build/$PLDISTROTAGS
163         echo "XXXXXXXXXXXXXXXXXXXX end tags definition"
164     fi
165     set -x
166 }
167
168 function usage () {
169     echo "Usage: $COMMAND [option] make-targets"
170     echo "This is $REVISION"
171     echo "Supported options"
172     echo " -n dry-run : -n passed to make - vserver gets created though - no mail sent"
173     echo " -f FCDISTRO - defaults to $DEFAULT_FCDISTRO"
174     echo " -d PLDISTRO - defaults to $DEFAULT_PLDISTRO"
175     echo " -b BASE - defaults to $DEFAULT_BASE"
176     echo "    @NAME@ replaced as appropriate"
177     echo " -t PLDISTROTAGS - defaults to \${PLDISTRO}-tags.mk"
178     echo " -r TAGSRELEASE - a release number that refers to PLDISTROTAGS - defaults to HEAD"
179     echo " -s SVNPATH - where to fetch the build module"
180     echo " -o : overwrite - re-run in base directory, do not create vserver"
181     echo " -m MAILTO"
182     echo " -a MAKEVAR=value - space in values are not supported"
183     echo " -w WEBPATH - defaults to $DEFAULT_WEBPATH"
184     echo " -B : run build only"
185     echo " -T : run test only"
186     echo " -v : be verbose"
187     exit 1
188 }
189
190 function main () {
191
192     set -e
193
194     # preserve arguments for passing them again later
195     declare -a argv
196     for arg in "$@"; do argv=(${argv[@]} "$arg") ; done
197     
198     # parse arguments
199     MAKEVARS=()
200     MAKEOPTS=()
201     DO_BUILD=true
202     DO_TEST=true
203     while getopts "nf:d:b:t:r:s:om:a:w:BTvh" opt ; do
204         case $opt in
205             n) DRY_RUN="true" ; MAKEOPTS=(${MAKEOPTS[@]} -n) ;;
206             f) FCDISTRO=$OPTARG ;;
207             d) PLDISTRO=$OPTARG ;;
208             b) BASE=$OPTARG ;;
209             t) PLDISTROTAGS=$OPTARG ;;
210             r) TAGSRELEASE=$OPTARG ;;
211             s) SVNPATH=$OPTARG ;;
212             o) USEOLD=true ;;
213             m) MAILTO=$OPTARG ;;
214             a) MAKEVARS=(${MAKEVARS[@]} "$OPTARG") ;;
215             w) WEBPATH=$OPTARG ;;
216             B) DO_TEST= ;;
217             T) DO_BUILD= ; USEOLD=true ;;
218             v) set -x ;;
219             h|*) usage ;;
220         esac
221     done
222         
223     shift $(($OPTIND - 1))
224     MAKETARGETS="$@"
225     
226     # set defaults
227     [ -z "$FCDISTRO" ] && FCDISTRO=$DEFAULT_FCDISTRO
228     [ -z "$PLDISTRO" ] && PLDISTRO=$DEFAULT_PLDISTRO
229     [ -z "$PLDISTROTAGS" ] && PLDISTROTAGS="${PLDISTRO}-tags.mk"
230     [ -z "$BASE" ] && BASE="$DEFAULT_BASE"
231     [ -z "$WEBPATH" ] && WEBPATH="$DEFAULT_WEBPATH"
232     [ -z "$SVNPATH" ] && SVNPATH="$DEFAULT_SVNPATH"
233
234     # NOTE: suggest that by default no email is sent and that the user
235     # should explicitly invoke this script with the -m arg to pass in
236     # the appropriate email address. --mef
237     if [ "$PLDISTRO" = "onelab" ] ; then
238         [ -z "$MAILTO" ] && MAILTO="$DEFAULT_MAILTO"
239     fi
240     [ -n "$DRY_RUN" ] && MAILTO=""
241         
242     ### set BASE from DISTRO, if unspecified
243     sedargs="-e s,@DATE@,${DATE},g -e s,@FCDISTRO@,${FCDISTRO},g -e s,@PLDISTRO@,${PLDISTRO},g"
244     BASE=$(echo ${BASE} | sed $sedargs)
245     WEBPATH=$(echo ${WEBPATH} | sed $sedargs)
246
247     if [ ! -d /vservers ] ; then
248         # in the vserver
249         echo "==================== Within vserver BEG $(date)"
250         build
251         echo "==================== Within vserver END $(date)"
252
253     else
254         trap failure ERR INT
255         # we run in the root context : 
256         # (*) create or check for the vserver to use
257         # (*) copy this command in the vserver
258         # (*) invoke it
259         
260         if [ -n "$USEOLD" ] ; then
261             ### Re-use a vserver (finish an unfinished build..)
262             if [ ! -d /vservers/${BASE} ] ; then
263                 echo $COMMAND : cannot find vserver $BASE
264                 exit 1
265             fi
266             # manage LOG - beware it might be a symlink so nuke it first
267             LOG=/vservers/${BASE}.log.txt
268             rm -f $LOG
269             exec > $LOG 2>&1
270             set -x
271             echo "XXXXXXXXXX $COMMAND: using existing vserver $BASE" $(date)
272             show_env
273             # update build
274             vserver ${BASE} exec svn update /build
275         else
276             # create vserver: check it does not exist yet
277             i=
278             while [ -d /vservers/${BASE}${i} ] ; do
279                 # we name subsequent builds <base>-n<i> so the logs and builds get sorted properly
280                 [ -z ${i} ] && BASE=${BASE}-n
281                 i=$((${i}+1))
282                 if [ $i -gt 100 ] ; then
283                     echo "$COMMAND: Failed to create build vserver /vservers/${BASE}${i}"
284                     exit 1
285                 fi
286             done
287             BASE=${BASE}${i}
288             # need update
289             # manage LOG - beware it might be a symlink so nuke it first
290             LOG=/vservers/${BASE}.log.txt
291             rm -f $LOG
292             exec > $LOG 2>&1 
293             set -x
294             echo "XXXXXXXXXX $COMMAND: creating vserver $BASE" $(date)
295             show_env
296
297             ### extract the whole build - much simpler
298             tmpdir=/tmp/$COMMAND-$$
299             svn export $SVNPATH $tmpdir
300             # Create vserver
301             cd $tmpdir
302             ./vbuild-init-vserver.sh ${BASE} ${FCDISTRO} ${PLDISTRO}
303             # cleanup
304             cd -
305             rm -rf $tmpdir
306             # Extract build again - in the vserver
307             vserver ${BASE} exec svn checkout ${SVNPATH} /build
308         fi
309         echo "XXXXXXXXXX $COMMAND: preparation of vserver $BASE done" $(date)
310
311         # The log inside the vserver contains everything
312         LOG2=/vservers/${BASE}/log.txt
313         (echo "==================== BEG VSERVER Transcript of vserver creation" ; \
314          cat $LOG ; \
315          echo "==================== END VSERVER Transcript of vserver creation" ; \
316          echo "xxxxxxxxxx Messing with logs, symlinking $LOG2 to $LOG" ) >> $LOG2
317         ### not too nice : nuke the former log, symlink it to the new one
318         rm $LOG; ln -s $LOG2 $LOG
319         LOG=$LOG2
320         # redirect log again
321         exec >> $LOG 2>&1 
322
323         if [ -n "$DO_BUILD" ] ; then 
324
325             cp $COMMANDPATH /vservers/${BASE}/build/
326
327             # invoke this command in the vserver for building (-T)
328             vserver ${BASE} exec chmod +x /build/$COMMAND
329             vserver ${BASE} exec /build/$COMMAND "${argv[@]}" -b "${BASE}"
330         fi
331
332         # publish to the web so runtest can find them
333         rm -rf $WEBPATH/$BASE ; mkdir -p $WEBPATH/$BASE/{RPMS,SRPMS}
334         rsync --archive --delete --verbose /vservers/$BASE/build/RPMS/ $WEBPATH/$BASE/RPMS/
335         rsync --archive --delete --verbose /vservers/$BASE/build/SRPMS/ $WEBPATH/$BASE/SRPMS/
336         
337         if [ -n "$DO_TEST" ] ; then 
338             runtest
339         fi
340
341         success 
342         
343     fi
344
345 }  
346
347 ##########
348 main "$@"