nightly passes build-SVNPATH to make, to avoid having to tune the tags file when...
[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=f8
9 DEFAULT_PLDISTRO=planetlab
10 DEFAULT_PERSONALITY=linux32
11 DEFAULT_BASE="@DATE@--@PLDISTRO@-@FCDISTRO@-@PERSONALITY@"
12 DEFAULT_SVNPATH="http://svn.planet-lab.org/svn/build/trunk"
13 # TESTSVNPATH to be computed from the -tags.mk file - no default anymore
14 DEFAULT_TESTCONFIG="default"
15 DEFAULT_IFNAME=eth0
16
17 # web publishing results
18 DEFAULT_WEBPATH="/build/@PLDISTRO@/"
19
20 # for the test part
21 TESTBUILDURL="http://build.one-lab.org/"
22 TESTBOXSSH=root@testbox.one-lab.org
23 ####################
24 # assuming vserver runs in UTC
25 DATE=$(date +'%Y.%m.%d')
26
27 # temporary - wrap a quick summary of suspicious stuff
28 # this is to focus on installation that go wrong
29 # use with care, a *lot* of other things can go bad as well
30 function summary () {
31     from=$1; shift
32     echo "******************** BEG SUMMARY" 
33     python - $from <<EOF
34 #!/usr/bin/env python
35 # read a full log and tries to extract the interesting stuff
36
37 import sys,re
38 m_show_line=re.compile(".* BEG (RPM|VSERVER).*|.*'boot'.*|\* .*|.*is not installed.*")
39 m_installing_any=re.compile('\r  (Installing:[^\]]*]) ')
40 m_installing_err=re.compile('\r  (Installing:[^\]]*])(..+)')
41 m_installing_end=re.compile('Installed:.*')
42 m_installing_doc1=re.compile("(.*)install-info: No such file or directory for /usr/share/info/\S+(.*)")
43 m_installing_doc2=re.compile("(.*)grep: /usr/share/info/dir: No such file or directory(.*)")
44
45 def summary (filename):
46
47     try:
48         if filename=="-":
49             filename="stdin"
50             f=sys.stdin
51         else:
52             f=open(filename)
53         echo=False
54         for line in f.xreadlines():
55             # first off : discard warnings related to doc
56             if m_installing_doc1.match(line):
57                 (begin,end)=m_installing_doc1.match(line).groups()
58                 line=begin+end
59             if m_installing_doc2.match(line):
60                 (begin,end)=m_installing_doc2.match(line).groups()
61                 line=begin+end
62             # unconditionnally show these lines
63             if m_show_line.match(line):
64                 print '>>>',line,
65             # an 'installing' line with messages afterwards : needs to be echoed
66             elif m_installing_err.match(line):
67                 (installing,error)=m_installing_err.match(line).groups()
68                 print '>>>',installing
69                 print '>>>',error
70                 echo=True
71             # closing an 'installing' section
72             elif m_installing_end.match(line):
73                 echo=False
74             # any 'installing' line
75             elif m_installing_any.match(line):
76                 if echo: 
77                     installing=m_installing_any.match(line).group(1)
78                     print '>>>',installing
79                 echo=False
80             # print lines when echo is true
81             else:
82                 if echo: print '>>>',line,
83         f.close()
84     except:
85         print 'Failed to analyze',filename
86
87 for arg in sys.argv[1:]:
88     summary(arg)
89 EOF
90     echo "******************** END SUMMARY" 
91 }
92
93
94 # Notify recipient of failure or success, manage various stamps 
95 function failure() {
96     set -x
97     WEBLOG=${WEBPATH}/${BASE}.log.txt
98     mkdir -p ${WEBPATH}
99     cp $LOG ${WEBLOG}
100     summary $LOG >> ${WEBLOG}
101     (echo -n "============================== $COMMAND: failure at " ; date ; tail -c 30k $WEBLOG) > ${WEBLOG}.ko
102     if [ -n "$MAILTO" ] ; then
103         tail -c 30k ${WEBLOG} | mail -s "Failures for build ${BASE}" $MAILTO
104     fi
105     exit 1
106 }
107
108 function success () {
109     set -x
110     WEBLOG=${WEBPATH}/${BASE}.log.txt
111     mkdir -p ${WEBPATH}
112     cp $LOG ${WEBLOG}
113     summary $LOG >> ${WEBLOG}
114     if [ -n "$DO_TEST" ] ; then
115         echo "Successfully built and tested - see testlogs for details" > ${WEBLOG}.pass
116         rm -f ${WEBLOG}.ok
117     else
118         echo "Successfully built"> ${WEBLOG}.ok
119     fi
120     if [ -n "$MAILTO" ] ; then
121         (echo "$PLDISTRO ($BASE) build for $FCDISTRO completed on $(date)" ) | mail -s "Successful build for ${BASE}" $MAILTO
122     fi
123     exit 0
124 }
125
126 # run in the vserver - do not manage success/failure, will be done from the root ctx
127 function build () {
128     set -x
129     set -e
130
131     echo -n "============================== Starting $COMMAND:build on "
132     date
133
134     cd /build
135   # if TAGSRELEASE specified : update PLDISTROTAGS with this tag
136     if [ -n "$TAGSRELEASE" ] ; then
137         svn up -r $TAGSRELEASE $PLDISTROTAGS
138     fi
139
140     show_env
141     
142     echo "Running make IN $(pwd)"
143     
144     # stuff our own variable settings
145     MAKEVARS=("PLDISTRO=${PLDISTRO}" "${MAKEVARS[@]}")
146     MAKEVARS=("PLDISTROTAGS=${PLDISTROTAGS}" "${MAKEVARS[@]}")
147     MAKEVARS=("NIGHTLY_BASE=${BASE}" "${MAKEVARS[@]}")
148     MAKEVARS=("NIGHTLY_PERSONALITY=${PERSONALITY}" "${MAKEVARS[@]}")
149     MAKEVARS=("build-SVNPATH=${SVNPATH}" "${MAKEVARS[@]}")
150
151     # stage1
152     make -C /build $DRY_RUN "${MAKEVARS[@]}" stage1=true 
153     # versions
154     make -C /build $DRY_RUN "${MAKEVARS[@]}" versions
155     # store testsvnpath
156     make -C /build $DRY_RUN "${MAKEVARS[@]}" stage1=true testsvnpath
157     # actual stuff
158     make -C /build $DRY_RUN "${MAKEVARS[@]}" $MAKETARGETS
159
160 }
161
162 # this was formerly run in the myplc-devel chroot but now is run in the root context,
163 # this is so that the .ssh config gets done manually, and once and for all
164 function runtest () {
165     set -x
166     set -e
167     trap failure ERR INT
168
169     echo -n "============================== Starting $COMMAND:runtest on $(date)"
170
171     # where to find TESTSVNPATH
172     stamp=/vservers/$BASE/build/testsvnpath
173     if [ ! -f $stamp ] ; then
174         echo "$COMMAND: Cannot figure TESTSVNPATH from missing $stamp"
175         failure
176         exit 1
177     fi
178     TESTSVNPATH=$(cat $stamp)
179     # use only this pat of the tests right now
180     TESTSVNPATH=${TESTSVNPATH}/system
181
182     ### the URL to the RPMS/<arch> location
183     url=""
184     for a in i386 x86_64; do
185         archdir=/vservers/$BASE/build/RPMS/$a
186         if [ -d $archdir ] ; then
187             url=$(echo $archdir | sed -e "s,/vservers/$BASE/build,${TESTBUILDURL}${PLDISTRO}/${BASE},")
188             break
189         fi
190     done
191
192     if [ -z "$url" ] ; then
193         echo "$COMMAND: Cannot locate arch URL for testing"
194         failure
195         exit 1
196     fi
197
198     # test directory name on test box
199     testdir=${BASE}
200     # clean it
201     ssh -n ${TESTBOXSSH} rm -rf ${testdir}
202     # check it out
203     ssh -n ${TESTBOXSSH} svn co ${TESTSVNPATH} ${testdir}
204     # invoke test on testbox - pass url and build url - so the tests can use vtest-init-vserver.sh
205     configs=""
206     for config in ${TESTCONFIG} ; do
207         configs="$configs --config $config"
208     done
209     
210     # need to proceed despite of set -e
211     success=true
212     ssh 2>&1 -n ${TESTBOXSSH} ${testdir}/runtest --build ${SVNPATH} --url ${url} $configs --all || success=
213
214     # gather logs in the vserver
215     mkdir -p /vservers/$BASE/build/testlogs
216     ssh 2>&1 -n ${TESTBOXSSH} tar -C ${testdir}/logs -cf - . | tar -C /vservers/$BASE/build/testlogs -xf - || true
217     # push them to the build web
218     rsync --archive --delete /vservers/$BASE/build/testlogs/ $WEBPATH/$BASE/testlogs/
219     chmod -R a+r $WEBPATH/$BASE/testlogs/
220
221     if [ -z "$success" ] ; then
222         failure
223     fi
224     
225     echo -n "============================== End $COMMAND:runtest on $(date)"
226 }
227
228 function show_env () {
229     set +x
230     echo FCDISTRO=$FCDISTRO
231     echo PLDISTRO=$PLDISTRO
232     echo BASE=$BASE
233     echo SVNPATH=$SVNPATH
234     echo MAKEVARS="${MAKEVARS[@]}"
235     echo DRY_RUN="$DRY_RUN"
236     echo PLDISTROTAGS="$PLDISTROTAGS"
237     echo TAGSRELEASE="$TAGSRELEASE"
238     echo -n "(might be unexpanded)"
239     echo WEBPATH="$WEBPATH"
240     if [ -d /vservers ] ; then
241         echo PLDISTROTAGS="$PLDISTROTAGS"
242     else
243         echo "XXXXXXXXXXXXXXXXXXXX Contents of tags definition file /build/$PLDISTROTAGS"
244         cat /build/$PLDISTROTAGS
245         echo "XXXXXXXXXXXXXXXXXXXX end tags definition"
246     fi
247     set -x
248 }
249
250 function usage () {
251     echo "Usage: $COMMAND [option] make-targets"
252     echo "This is $REVISION"
253     echo "Supported options"
254     echo " -f fcdistro - defaults to $DEFAULT_FCDISTRO"
255     echo " -d pldistro - defaults to $DEFAULT_PLDISTRO"
256     echo " -p personality - defaults to $DEFAULT_PERSONALITY"
257     echo " -b base - defaults to $DEFAULT_BASE"
258     echo "    @NAME@ replaced as appropriate"
259     echo " -t pldistrotags - defaults to \${PLDISTRO}-tags.mk"
260     echo " -r tagsrelease - a release number that refers to PLDISTROTAGS - defaults to HEAD"
261     echo " -s svnpath - where to fetch the build module"
262     echo " -c testconfig - defaults to $DEFAULT_TESTCONFIG"
263     echo " -w webpath - defaults to $DEFAULT_WEBPATH"
264     echo " -m mailto - no default"
265     echo " -O : overwrite - re-run in base directory, do not re-create vserver"
266     echo " -B : run build only"
267     echo " -T : run test only"
268     echo " -n dry-run : -n passed to make - vserver gets created though - no mail sent"
269     echo " -v : be verbose"
270     echo " -7 : uses weekday-@FCDISTRO@ as base"
271     echo " -a makevar=value - space in values are not supported"
272     echo " -i ifname - defaults to $DEFAULT_IFNAME - used to determine local IP"
273     exit 1
274 }
275
276 function main () {
277
278     set -e
279
280     # parse arguments
281     MAKEVARS=()
282     DRY_RUN=
283     DO_BUILD=true
284     DO_TEST=true
285     while getopts "f:d:p:b:t:r:s:x:c:w:m:OBTnv7a:i:" opt ; do
286         case $opt in
287             f) FCDISTRO=$OPTARG ;;
288             d) PLDISTRO=$OPTARG ;;
289             p) PERSONALITY=$OPTARG ;;
290             b) BASE=$OPTARG ;;
291             t) PLDISTROTAGS=$OPTARG ;;
292             r) TAGSRELEASE=$OPTARG ;;
293             s) SVNPATH=$OPTARG ;;
294             c) TESTCONFIG="$TESTCONFIG $OPTARG" ;;
295             w) WEBPATH=$OPTARG ;;
296             m) MAILTO=$OPTARG ;;
297             O) OVERWRITEMODE=true ;;
298             B) DO_TEST= ;;
299             T) DO_BUILD= ; OVERWRITEMODE=true ;;
300             n) DRY_RUN="-n" ;;
301             v) set -x ;;
302             7) BASE="$(date +%a|tr A-Z a-z)-@FCDISTRO@" ;;
303             a) MAKEVARS=(${MAKEVARS[@]} "$OPTARG") ;;
304             i) IFNAME=$OPTARG ;;
305             h|*) usage ;;
306         esac
307     done
308         
309     # preserve options for passing them again later, together with expanded base
310     declare -a options
311     toshift=$(($OPTIND - 1))
312     arg=1; while [ $arg -le $toshift ] ; do options=(${options[@]} "$1") ; shift; arg=$(($arg+1)) ; done
313
314     MAKETARGETS="$@"
315     
316     # set defaults
317     [ -z "$FCDISTRO" ] && FCDISTRO=$DEFAULT_FCDISTRO
318     [ -z "$PLDISTRO" ] && PLDISTRO=$DEFAULT_PLDISTRO
319     [ -z "$PERSONALITY" ] && PERSONALITY=$DEFAULT_PERSONALITY
320     [ -z "$PLDISTROTAGS" ] && PLDISTROTAGS="${PLDISTRO}-tags.mk"
321     [ -z "$BASE" ] && BASE="$DEFAULT_BASE"
322     [ -z "$WEBPATH" ] && WEBPATH="$DEFAULT_WEBPATH"
323     [ -z "$IFNAME" ] && IFNAME="$DEFAULT_IFNAME"
324     [ -z "$SVNPATH" ] && SVNPATH="$DEFAULT_SVNPATH"
325     [ -z "$TESTCONFIG" ] && TESTCONFIG="$DEFAULT_TESTCONFIG"
326
327     [ -n "$DRY_RUN" ] && MAILTO=""
328         
329     ### set BASE from DISTRO, if unspecified
330     sedargs="-e s,@DATE@,${DATE},g -e s,@FCDISTRO@,${FCDISTRO},g -e s,@PLDISTRO@,${PLDISTRO},g -e s,@PERSONALITY@,${PERSONALITY},g"
331     BASE=$(echo ${BASE} | sed $sedargs)
332     WEBPATH=$(echo ${WEBPATH} | sed $sedargs)
333
334     if [ ! -d /vservers ] ; then
335         # in the vserver
336         echo "==================== Within vserver BEG $(date)"
337         build
338         echo "==================== Within vserver END $(date)"
339
340     else
341         trap failure ERR INT
342         # we run in the root context : 
343         # (*) create or check for the vserver to use
344         # (*) copy this command in the vserver
345         # (*) invoke it
346         
347         if [ -n "$OVERWRITEMODE" ] ; then
348             ### Re-use a vserver (finish an unfinished build..)
349             if [ ! -d /vservers/${BASE} ] ; then
350                 echo $COMMAND : cannot find vserver $BASE
351                 exit 1
352             fi
353             # manage LOG - beware it might be a symlink so nuke it first
354             LOG=/vservers/${BASE}.log.txt
355             rm -f $LOG
356             exec > $LOG 2>&1
357             set -x
358             echo "XXXXXXXXXX $COMMAND: using existing vserver $BASE" $(date)
359             show_env
360             # start in case e.g. we just rebooted
361             vserver ${BASE} start || :
362             # update build
363             vserver ${BASE} exec svn update /build
364         else
365             # create vserver: check it does not exist yet
366             i=
367             while [ -d /vservers/${BASE}${i} ] ; do
368                 # we name subsequent builds <base>-n<i> so the logs and builds get sorted properly
369                 [ -z ${i} ] && BASE=${BASE}-n
370                 i=$((${i}+1))
371                 if [ $i -gt 100 ] ; then
372                     echo "$COMMAND: Failed to create build vserver /vservers/${BASE}${i}"
373                     exit 1
374                 fi
375             done
376             BASE=${BASE}${i}
377             # need update
378             # manage LOG - beware it might be a symlink so nuke it first
379             LOG=/vservers/${BASE}.log.txt
380             rm -f $LOG
381             exec > $LOG 2>&1 
382             set -x
383             echo "XXXXXXXXXX $COMMAND: creating vserver $BASE" $(date)
384             show_env
385
386             ### extract the whole build - much simpler
387             tmpdir=/tmp/$COMMAND-$$
388             svn export $SVNPATH $tmpdir
389             # Create vserver
390             cd $tmpdir
391             ./vbuild-init-vserver.sh -f ${FCDISTRO} -d ${PLDISTRO} -p ${PERSONALITY} -i ${IFNAME} ${BASE} 
392             # cleanup
393             cd -
394             rm -rf $tmpdir
395             # Extract build again - in the vserver
396             vserver ${BASE} exec svn checkout ${SVNPATH} /build
397         fi
398         echo "XXXXXXXXXX $COMMAND: preparation of vserver $BASE done" $(date)
399
400         # The log inside the vserver contains everything
401         LOG2=/vservers/${BASE}/log.txt
402         (echo "==================== BEG VSERVER Transcript of vserver creation" ; \
403          cat $LOG ; \
404          echo "==================== END VSERVER Transcript of vserver creation" ; \
405          echo "xxxxxxxxxx Messing with logs, symlinking $LOG2 to $LOG" ) >> $LOG2
406         ### not too nice : nuke the former log, symlink it to the new one
407         rm $LOG; ln -s $LOG2 $LOG
408         LOG=$LOG2
409         # redirect log again
410         exec >> $LOG 2>&1 
411
412         if [ -n "$DO_BUILD" ] ; then 
413
414             cp $COMMANDPATH /vservers/${BASE}/build/
415
416             # invoke this command in the vserver for building (-T)
417             vserver ${BASE} exec chmod +x /build/$COMMAND
418             vserver ${BASE} exec /build/$COMMAND "${options[@]}" -b "${BASE}" $MAKETARGETS
419         fi
420
421         # publish to the web so runtest can find them
422         rm -rf $WEBPATH/$BASE ; mkdir -p $WEBPATH/$BASE/{RPMS,SRPMS}
423         rsync --archive --delete --verbose /vservers/$BASE/build/RPMS/ $WEBPATH/$BASE/RPMS/
424         rsync --archive --delete --verbose /vservers/$BASE/build/SRPMS/ $WEBPATH/$BASE/SRPMS/
425         # publish myplc-release
426         rsync --verbose /vservers/$BASE/build/myplc-release $WEBPATH/$BASE
427         
428         if [ -n "$DO_TEST" ] ; then 
429             runtest
430         fi
431
432         success 
433         
434     fi
435
436 }  
437
438 ##########
439 main "$@"