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