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