invoke the test framework through ssh -n
[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
150     # stage1
151     make -C /build $DRY_RUN "${MAKEVARS[@]}" stage1=true 
152     # versions
153     make -C /build $DRY_RUN "${MAKEVARS[@]}" versions
154     # store testsvnpath
155     make -C /build $DRY_RUN "${MAKEVARS[@]}" stage1=true testsvnpath
156     # actual stuff
157     make -C /build $DRY_RUN "${MAKEVARS[@]}" $MAKETARGETS
158
159 }
160
161 # this was formerly run in the myplc-devel chroot but now is run in the root context,
162 # this is so that the .ssh config gets done manually, and once and for all
163 function runtest () {
164     set -x
165     set -e
166     trap failure ERR INT
167
168     echo -n "============================== Starting $COMMAND:runtest on $(date)"
169
170     # where to find TESTSVNPATH
171     stamp=/vservers/$BASE/build/testsvnpath
172     if [ ! -f $stamp ] ; then
173         echo "$COMMAND: Cannot figure TESTSVNPATH from missing $stamp"
174         failure
175         exit 1
176     fi
177     TESTSVNPATH=$(cat $stamp)
178     # use only this pat of the tests right now
179     TESTSVNPATH=${TESTSVNPATH}/system
180
181     ### the URL to the RPMS/<arch> location
182     url=""
183     for a in i386 x86_64; do
184         archdir=/vservers/$BASE/build/RPMS/$a
185         if [ -d $archdir ] ; then
186             url=$(echo $archdir | sed -e "s,/vservers/$BASE/build,${TESTBUILDURL}${PLDISTRO}/${BASE},")
187             break
188         fi
189     done
190
191     if [ -z "$url" ] ; then
192         echo "$COMMAND: Cannot locate arch URL for testing"
193         failure
194         exit 1
195     fi
196
197     # test directory name on test box
198     testdir=${BASE}
199     # clean it
200     ssh -n ${TESTBOXSSH} rm -rf ${testdir}
201     # check it out
202     ssh -n ${TESTBOXSSH} svn co ${TESTSVNPATH} ${testdir}
203     # invoke test on testbox - pass url and build url - so the tests can use vtest-init-vserver.sh
204     configs=""
205     for config in ${TESTCONFIG} ; do
206         configs="$configs --config $config"
207     done
208     
209     # need to proceed despite of set -e
210     success=true
211     ssh 2>&1 -n ${TESTBOXSSH} ${testdir}/runtest --build ${SVNPATH} --url ${url} $configs --all || success=
212
213     # gather logs in the vserver
214     mkdir -p /vservers/$BASE/build/testlogs
215     ssh 2>&1 -n ${TESTBOXSSH} tar -C ${testdir}/logs -cf - . | tar -C /vservers/$BASE/build/testlogs -xf - || true
216     # push them to the build web
217     rsync --archive --delete /vservers/$BASE/build/testlogs/ $WEBPATH/$BASE/testlogs/
218     chmod -R a+r $WEBPATH/$BASE/testlogs/
219
220     if [ -z "$success" ] ; then
221         failure
222     fi
223     
224     echo -n "============================== End $COMMAND:runtest on $(date)"
225 }
226
227 function show_env () {
228     set +x
229     echo FCDISTRO=$FCDISTRO
230     echo PLDISTRO=$PLDISTRO
231     echo BASE=$BASE
232     echo SVNPATH=$SVNPATH
233     echo MAKEVARS="${MAKEVARS[@]}"
234     echo DRY_RUN="$DRY_RUN"
235     echo PLDISTROTAGS="$PLDISTROTAGS"
236     echo TAGSRELEASE="$TAGSRELEASE"
237     echo -n "(might be unexpanded)"
238     echo WEBPATH="$WEBPATH"
239     if [ -d /vservers ] ; then
240         echo PLDISTROTAGS="$PLDISTROTAGS"
241     else
242         echo "XXXXXXXXXXXXXXXXXXXX Contents of tags definition file /build/$PLDISTROTAGS"
243         cat /build/$PLDISTROTAGS
244         echo "XXXXXXXXXXXXXXXXXXXX end tags definition"
245     fi
246     set -x
247 }
248
249 function usage () {
250     echo "Usage: $COMMAND [option] make-targets"
251     echo "This is $REVISION"
252     echo "Supported options"
253     echo " -f fcdistro - defaults to $DEFAULT_FCDISTRO"
254     echo " -d pldistro - defaults to $DEFAULT_PLDISTRO"
255     echo " -p personality - defaults to $DEFAULT_PERSONALITY"
256     echo " -b base - defaults to $DEFAULT_BASE"
257     echo "    @NAME@ replaced as appropriate"
258     echo " -t pldistrotags - defaults to \${PLDISTRO}-tags.mk"
259     echo " -r tagsrelease - a release number that refers to PLDISTROTAGS - defaults to HEAD"
260     echo " -s svnpath - where to fetch the build module"
261     echo " -c testconfig - defaults to $DEFAULT_TESTCONFIG"
262     echo " -w webpath - defaults to $DEFAULT_WEBPATH"
263     echo " -m mailto - no default"
264     echo " -O : overwrite - re-run in base directory, do not re-create vserver"
265     echo " -B : run build only"
266     echo " -T : run test only"
267     echo " -n dry-run : -n passed to make - vserver gets created though - no mail sent"
268     echo " -v : be verbose"
269     echo " -7 : uses weekday-@FCDISTRO@ as base"
270     echo " -a makevar=value - space in values are not supported"
271     echo " -i ifname - defaults to $DEFAULT_IFNAME - used to determine local IP"
272     exit 1
273 }
274
275 function main () {
276
277     set -e
278
279     # parse arguments
280     MAKEVARS=()
281     DRY_RUN=
282     DO_BUILD=true
283     DO_TEST=true
284     while getopts "f:d:p:b:t:r:s:x:c:w:m:OBTnv7a:i:" opt ; do
285         case $opt in
286             f) FCDISTRO=$OPTARG ;;
287             d) PLDISTRO=$OPTARG ;;
288             p) PERSONALITY=$OPTARG ;;
289             b) BASE=$OPTARG ;;
290             t) PLDISTROTAGS=$OPTARG ;;
291             r) TAGSRELEASE=$OPTARG ;;
292             s) SVNPATH=$OPTARG ;;
293             c) TESTCONFIG="$TESTCONFIG $OPTARG" ;;
294             w) WEBPATH=$OPTARG ;;
295             m) MAILTO=$OPTARG ;;
296             O) OVERWRITEMODE=true ;;
297             B) DO_TEST= ;;
298             T) DO_BUILD= ; OVERWRITEMODE=true ;;
299             n) DRY_RUN="-n" ;;
300             v) set -x ;;
301             7) BASE="$(date +%a|tr A-Z a-z)-@FCDISTRO@" ;;
302             a) MAKEVARS=(${MAKEVARS[@]} "$OPTARG") ;;
303             i) IFNAME=$OPTARG ;;
304             h|*) usage ;;
305         esac
306     done
307         
308     # preserve options for passing them again later, together with expanded base
309     declare -a options
310     toshift=$(($OPTIND - 1))
311     arg=1; while [ $arg -le $toshift ] ; do options=(${options[@]} "$1") ; shift; arg=$(($arg+1)) ; done
312
313     MAKETARGETS="$@"
314     
315     # set defaults
316     [ -z "$FCDISTRO" ] && FCDISTRO=$DEFAULT_FCDISTRO
317     [ -z "$PLDISTRO" ] && PLDISTRO=$DEFAULT_PLDISTRO
318     [ -z "$PERSONALITY" ] && PERSONALITY=$DEFAULT_PERSONALITY
319     [ -z "$PLDISTROTAGS" ] && PLDISTROTAGS="${PLDISTRO}-tags.mk"
320     [ -z "$BASE" ] && BASE="$DEFAULT_BASE"
321     [ -z "$WEBPATH" ] && WEBPATH="$DEFAULT_WEBPATH"
322     [ -z "$IFNAME" ] && IFNAME="$DEFAULT_IFNAME"
323     [ -z "$SVNPATH" ] && SVNPATH="$DEFAULT_SVNPATH"
324     [ -z "$TESTCONFIG" ] && TESTCONFIG="$DEFAULT_TESTCONFIG"
325
326     [ -n "$DRY_RUN" ] && MAILTO=""
327         
328     ### set BASE from DISTRO, if unspecified
329     sedargs="-e s,@DATE@,${DATE},g -e s,@FCDISTRO@,${FCDISTRO},g -e s,@PLDISTRO@,${PLDISTRO},g -e s,@PERSONALITY@,${PERSONALITY},g"
330     BASE=$(echo ${BASE} | sed $sedargs)
331     WEBPATH=$(echo ${WEBPATH} | sed $sedargs)
332
333     if [ ! -d /vservers ] ; then
334         # in the vserver
335         echo "==================== Within vserver BEG $(date)"
336         build
337         echo "==================== Within vserver END $(date)"
338
339     else
340         trap failure ERR INT
341         # we run in the root context : 
342         # (*) create or check for the vserver to use
343         # (*) copy this command in the vserver
344         # (*) invoke it
345         
346         if [ -n "$OVERWRITEMODE" ] ; then
347             ### Re-use a vserver (finish an unfinished build..)
348             if [ ! -d /vservers/${BASE} ] ; then
349                 echo $COMMAND : cannot find vserver $BASE
350                 exit 1
351             fi
352             # manage LOG - beware it might be a symlink so nuke it first
353             LOG=/vservers/${BASE}.log.txt
354             rm -f $LOG
355             exec > $LOG 2>&1
356             set -x
357             echo "XXXXXXXXXX $COMMAND: using existing vserver $BASE" $(date)
358             show_env
359             # start in case e.g. we just rebooted
360             vserver ${BASE} start || :
361             # update build
362             vserver ${BASE} exec svn update /build
363         else
364             # create vserver: check it does not exist yet
365             i=
366             while [ -d /vservers/${BASE}${i} ] ; do
367                 # we name subsequent builds <base>-n<i> so the logs and builds get sorted properly
368                 [ -z ${i} ] && BASE=${BASE}-n
369                 i=$((${i}+1))
370                 if [ $i -gt 100 ] ; then
371                     echo "$COMMAND: Failed to create build vserver /vservers/${BASE}${i}"
372                     exit 1
373                 fi
374             done
375             BASE=${BASE}${i}
376             # need update
377             # manage LOG - beware it might be a symlink so nuke it first
378             LOG=/vservers/${BASE}.log.txt
379             rm -f $LOG
380             exec > $LOG 2>&1 
381             set -x
382             echo "XXXXXXXXXX $COMMAND: creating vserver $BASE" $(date)
383             show_env
384
385             ### extract the whole build - much simpler
386             tmpdir=/tmp/$COMMAND-$$
387             svn export $SVNPATH $tmpdir
388             # Create vserver
389             cd $tmpdir
390             ./vbuild-init-vserver.sh -f ${FCDISTRO} -d ${PLDISTRO} -p ${PERSONALITY} -i ${IFNAME} ${BASE} 
391             # cleanup
392             cd -
393             rm -rf $tmpdir
394             # Extract build again - in the vserver
395             vserver ${BASE} exec svn checkout ${SVNPATH} /build
396         fi
397         echo "XXXXXXXXXX $COMMAND: preparation of vserver $BASE done" $(date)
398
399         # The log inside the vserver contains everything
400         LOG2=/vservers/${BASE}/log.txt
401         (echo "==================== BEG VSERVER Transcript of vserver creation" ; \
402          cat $LOG ; \
403          echo "==================== END VSERVER Transcript of vserver creation" ; \
404          echo "xxxxxxxxxx Messing with logs, symlinking $LOG2 to $LOG" ) >> $LOG2
405         ### not too nice : nuke the former log, symlink it to the new one
406         rm $LOG; ln -s $LOG2 $LOG
407         LOG=$LOG2
408         # redirect log again
409         exec >> $LOG 2>&1 
410
411         if [ -n "$DO_BUILD" ] ; then 
412
413             cp $COMMANDPATH /vservers/${BASE}/build/
414
415             # invoke this command in the vserver for building (-T)
416             vserver ${BASE} exec chmod +x /build/$COMMAND
417             vserver ${BASE} exec /build/$COMMAND "${options[@]}" -b "${BASE}" $MAKETARGETS
418         fi
419
420         # publish to the web so runtest can find them
421         rm -rf $WEBPATH/$BASE ; mkdir -p $WEBPATH/$BASE/{RPMS,SRPMS}
422         rsync --archive --delete --verbose /vservers/$BASE/build/RPMS/ $WEBPATH/$BASE/RPMS/
423         rsync --archive --delete --verbose /vservers/$BASE/build/SRPMS/ $WEBPATH/$BASE/SRPMS/
424         # publish myplc-release
425         rsync --verbose /vservers/$BASE/build/myplc-release $WEBPATH/$BASE
426         
427         if [ -n "$DO_TEST" ] ; then 
428             runtest
429         fi
430
431         success 
432         
433     fi
434
435 }  
436
437 ##########
438 main "$@"