comment about status of the ‘build-on-f20’ issue
[build.git] / lbuild-nightly.sh
1 #!/bin/bash
2
3 # NOTE on lxcsu
4 # this tool derives from the PL code, from the lxc-userspace module
5 # can be obtained through rpm/yum
6 # it is used here as a replacement for 'vserver <> exec'
7 # that is used throughout this code
8
9 COMMANDPATH=$0
10 COMMAND=$(basename $0)
11
12 # default values, tunable with command-line options
13 DEFAULT_FCDISTRO=f20
14 DEFAULT_PLDISTRO=planetlab
15 DEFAULT_PERSONALITY=linux64
16 DEFAULT_BASE="@DATE@--@PLDISTRO@-@FCDISTRO@-@PERSONALITY@"
17 DEFAULT_BUILD_SCM_URL="git://git.onelab.eu/build"
18 DEFAULT_IFNAME=eth0
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 DEFAULT_TESTCONFIG="default"
26 # for passing args to run_log
27 RUN_LOG_EXTRAS=""
28
29 # for publishing results, and the tests settings
30 x=$(hostname)
31 y=$(hostname|sed -e s,inria,,)
32 # INRIA defaults
33 if [ "$x" != "$y" ] ; then
34     DEFAULT_WEBPATH="/build/@PLDISTRO@/"
35     DEFAULT_TESTBUILDURL="http://build.onelab.eu/"
36     # this is where the buildurl is pointing towards
37     DEFAULT_WEBROOT="/build/"
38     DEFAULT_TESTMASTER="testmaster.onelab.eu"
39 else
40     DEFAULT_WEBPATH="/build/@FCDISTRO@/@PLDISTRO@/"
41     DEFAULT_TESTBUILDURL="http://build.planet-lab.org/"
42     # this is where the buildurl is pointing towards
43     DEFAULT_WEBROOT="/build/"
44     DEFAULT_TESTMASTER="manager.test.planet-lab.org"
45 fi    
46
47 ####################
48 # assuming vm runs in UTC
49 DATE=$(date +'%Y.%m.%d')
50
51 # still using /vservers for legacy reasons
52 # as far as the build & test infra, we could adopt a new name
53 # but the PL code still uses this name for now, so let's keep it simple
54 function rootdir () {
55     slice=$1; shift
56     echo /vservers/$slice/rootfs
57 }
58 function logfile () {
59     slice=$1; shift
60     echo /vservers/$slice.log.txt
61 }
62
63 # wrap a quick summary of suspicious stuff
64 # this is to focus on installation that go wrong
65 # use with care, a *lot* of other things can go bad as well
66 function summary () {
67     from=$1; shift
68     echo "******************** BEG SUMMARY" 
69     python - $from <<EOF
70 #!/usr/bin/env python
71 # read a full log and tries to extract the interesting stuff
72
73 import sys,re
74 m_show_line=re.compile(".* (BEG|END) (RPM|LXC).*|.*'boot'.*|\* .*| \* .*|.*is not installed.*|.*PROPFIND.*|.* (BEG|END).*:run_log.*|.* Within LXC (BEG|END) .*|.* MAIN (BEG|END).*")
75 m_installing_any=re.compile('\r  (Installing:[^\]]*]) ')
76 m_installing_err=re.compile('\r  (Installing:[^\]]*])(..+)')
77 m_installing_end=re.compile('Installed:.*')
78 m_installing_doc1=re.compile("(.*)install-info: No such file or directory for /usr/share/info/\S+(.*)")
79 m_installing_doc2=re.compile("(.*)grep: /usr/share/info/dir: No such file or directory(.*)")
80
81 def summary (filename):
82
83     try:
84         if filename=="-":
85             filename="stdin"
86             f=sys.stdin
87         else:
88             f=open(filename)
89         echo=False
90         for line in f.xreadlines():
91             # first off : discard warnings related to doc
92             if m_installing_doc1.match(line):
93                 (begin,end)=m_installing_doc1.match(line).groups()
94                 line=begin+end
95             if m_installing_doc2.match(line):
96                 (begin,end)=m_installing_doc2.match(line).groups()
97                 line=begin+end
98             # unconditionnally show these lines
99             if m_show_line.match(line):
100                 print '>>>',line,
101             # an 'installing' line with messages afterwards : needs to be echoed
102             elif m_installing_err.match(line):
103                 (installing,error)=m_installing_err.match(line).groups()
104                 print '>>>',installing
105                 print '>>>',error
106                 echo=True
107             # closing an 'installing' section
108             elif m_installing_end.match(line):
109                 echo=False
110             # any 'installing' line
111             elif m_installing_any.match(line):
112                 if echo: 
113                     installing=m_installing_any.match(line).group(1)
114                     print '>>>',installing
115                 echo=False
116             # print lines when echo is true
117             else:
118                 if echo: print '>>>',line,
119         f.close()
120     except:
121         print 'Failed to analyze',filename
122
123 for arg in sys.argv[1:]:
124     summary(arg)
125 EOF
126     echo "******************** END SUMMARY" 
127 }
128
129 ### we might build on a box other than the actual web server
130 # utilities for handling the pushed material (rpms, logfiles, ...)
131 function webpublish_misses_dir () { ssh root@${WEBHOST}  "bash -c \"test \! -d $1\"" ; }
132 function webpublish () { ssh root@${WEBHOST} "$@" ; }
133 function webpublish_cp_local_to_remote () { scp $1 root@${WEBHOST}:$2 ; }
134 function webpublish_cp_stdin_to_file () { ssh root@${WEBHOST} cat \> $1; }
135 function webpublish_append_stdin_to_file () { ssh root@${WEBHOST} cat \>\> $1; }
136 # provide remote dir as first argument, so any number of local files can be passed next
137 function webpublish_rsync_dir () { rsync --archive --delete $VERBOSE $2 root@${WEBHOST}:$1 ; }
138 function webpublish_rsync_files () {
139     remote="$1"; shift
140     rsync --archive $VERBOSE "$@" root@${WEBHOST}:"$remote" ;
141 }
142
143 # Notify recipient of failure or success, manage various stamps 
144 function failure() {
145     set -x
146     # early stage ? - let's not create /build/@PLDISTRO@
147     if test -z "$WEBLOG" ; then
148         WEBHOST=$(hostname)
149         WEBPATH=/tmp
150         WEBBASE=/tmp/lbuild-early-$(date +%Y-%m-%d)
151         WEBLOG=/tmp/lbuild-early-$(date +%Y-%m-%d).log.txt
152     fi
153     webpublish mkdir -p $WEBBASE ||:
154     webpublish_cp_local_to_remote $LOG $WEBLOG ||:
155     summary $LOG | webpublish_append_stdin_to_file $WEBLOG ||:
156     (echo -n "============================== $COMMAND: failure at " ; date ; \
157         webpublish tail --lines=1000 $WEBLOG) | \
158         webpublish_cp_stdin_to_file $WEBBASE.ko ||:
159     if [ -n "$MAILTO" ] ; then
160         ( \
161             echo "Subject: KO ${BASE} ${MAIL_SUBJECT}" ; \
162             echo "To: $MAILTO" ; \
163             echo "see build results at        $WEBBASE_URL" ; \
164             echo "including full build log at $WEBBASE_URL/log.txt" ; \
165             echo "and complete test logs at   $WEBBASE_URL/testlogs" ; \
166             echo "........................................" ; \
167             webpublish tail --lines=1000 $WEBLOG ) | \
168             sendmail $MAILTO
169     fi
170     exit 1
171 }
172
173 function success () {
174     set -x
175     # early stage ? - let's not create /build/@PLDISTRO@
176     if test -z "$WEBLOG" ; then
177         WEBHOST=$(hostname)
178         WEBPATH=/tmp
179         WEBLOG=/tmp/lbuild-early-$(date +%Y-%m-%d).log.txt
180     fi
181     webpublish mkdir -p $WEBBASE
182     webpublish_cp_local_to_remote $LOG $WEBLOG
183     summary $LOG | webpublish_append_stdin_to_file $WEBLOG
184     if [ -n "$DO_TEST" ] ; then
185         short_message="PASS"
186         ( \
187             echo "Successfully built and tested" ; \
188             echo "see build results at        $WEBBASE_URL" ; \
189             echo "including full build log at $WEBBASE_URL/log.txt" ; \
190             echo "and complete test logs at   $WEBBASE_URL/testlogs" ; \
191             ) | webpublish_cp_stdin_to_file $WEBBASE.pass
192         webpublish rm -f $WEBBASE.pkg-ok $WEBBASE.ko
193     else
194         short_message="PKGOK"
195         ( \
196             echo "Successful package-only build, no test requested" ; \
197             echo "see build results at        $WEBBASE_URL" ; \
198             echo "including full build log at $WEBBASE_URL/log.txt" ; \
199             ) | webpublish_cp_stdin_to_file $WEBBASE.pkg-ok
200         webpublish rm -f $WEBBASE.ko
201     fi
202     if [ -n "$MAILTO" ] ; then
203         ( \
204             echo "Subject: $short_message ${BASE} ${MAIL_SUBJECT}" ; \
205             echo "To: $MAILTO" ; \
206             echo "$PLDISTRO ($BASE) build for $FCDISTRO completed on $(date)" ; \
207             echo "see build results at        $WEBBASE_URL" ; \
208             echo "including full build log at $WEBBASE_URL/log.txt" ; \
209             [ -n "$DO_TEST" ] && echo "and complete test logs at   $WEBBASE_URL/testlogs" ) \
210             | sendmail $MAILTO
211     fi
212     # XXX For some reason, we haven't been getting this email for successful builds. If this sleep
213     # doesn't fix the problem, I'll remove it -- Sapan.
214     sleep 5
215     exit 0
216 }
217
218 # run in the vm - do not manage success/failure, will be done from the root ctx
219 function build () {
220     set -x
221     set -e
222
223     echo -n "============================== Starting $COMMAND:build on "
224     date
225
226     cd /build
227     show_env
228     
229     echo "Running make IN $(pwd)"
230     
231     # stuff our own variable settings
232     MAKEVARS=("build-GITPATH=${BUILD_SCM_URL}" "${MAKEVARS[@]}")
233     MAKEVARS=("PLDISTRO=${PLDISTRO}" "${MAKEVARS[@]}")
234     MAKEVARS=("PLDISTROTAGS=${PLDISTROTAGS}" "${MAKEVARS[@]}")
235     MAKEVARS=("PERSONALITY=${PERSONALITY}" "${MAKEVARS[@]}")
236     MAKEVARS=("MAILTO=${MAILTO}" "${MAKEVARS[@]}")
237     MAKEVARS=("WEBPATH=${WEBPATH}" "${MAKEVARS[@]}")
238     MAKEVARS=("TESTBUILDURL=${TESTBUILDURL}" "${MAKEVARS[@]}")
239     MAKEVARS=("WEBROOT=${WEBROOT}" "${MAKEVARS[@]}")
240
241     MAKEVARS=("BASE=${BASE}" "${MAKEVARS[@]}")
242
243     # initialize latex
244     /build/latex-first-run.sh || :
245
246     # stage1
247     make -C /build $DRY_RUN "${MAKEVARS[@]}" stage1=true 
248     # versions
249     make -C /build $DRY_RUN "${MAKEVARS[@]}" versions
250     # actual stuff
251     make -C /build $DRY_RUN "${MAKEVARS[@]}" "${MAKETARGETS[@]}"
252
253 }
254
255 # this was formerly run in the myplc-devel chroot but now is run in the root context,
256 # this is so that the .ssh config gets done manually, and once and for all
257 function run_log () {
258     set -x
259     set -e
260     trap failure ERR INT
261
262     echo "============================== BEG $COMMAND:run_log on $(date)"
263
264     ### the URL to the RPMS/<arch> location
265     # f12 now has everything in i686; try i386 first as older fedoras have both
266     url=""
267     for a in i386 i686 x86_64; do
268         archdir=$(rootdir $BASE)/build/RPMS/$a
269         if [ -d $archdir ] ; then
270             # where was that installed
271             url=$(echo $archdir | sed -e "s,$(rootdir $BASE)/build,${WEBPATH}/${BASE},")
272             url=$(echo $url | sed -e "s,${WEBROOT},${TESTBUILDURL},")
273             break
274         fi
275     done
276
277     if [ -z "$url" ] ; then
278         echo "$COMMAND: Cannot locate arch URL for testing"
279         failure
280         exit 1
281     fi
282
283     testmaster_ssh="root@${TESTMASTER}"
284
285     # test directory name on test box
286     testdir=${BASE}
287
288     # clean it
289     ssh -n ${testmaster_ssh} rm -rf ${testdir} ${testdir}.git
290
291     # check it out in the build
292     lxcsu -ro $BASE make -C /build tests-module
293     
294     # push it onto the testmaster - just the 'system' subdir is enough
295     rsync --verbose --archive $(rootdir $BASE)/build/MODULES/tests/system/ ${testmaster_ssh}:${BASE}
296     # toss the build in the bargain, so the tests don't need to mess with extracting it
297     rsync --verbose --archive $(rootdir $BASE)/build/MODULES/build ${testmaster_ssh}:${BASE}/
298
299     # invoke test on testbox - pass url and build url - so the tests can use vtest-init-lxc.sh
300     run_log_env="-p $PERSONALITY -d $PLDISTRO -f $FCDISTRO"
301
302     # need to proceed despite of set -e
303     success=true
304     # passing the build_scm_url should not be needed anymore
305     ssh 2>&1 -n ${testmaster_ssh} ${testdir}/run_log --build ${BUILD_SCM_URL} --url ${url} $run_log_env $RUN_LOG_EXTRAS $VERBOSE --all || success=
306
307     # gather logs in the build vm
308     mkdir -p $(rootdir $BASE)/build/testlogs
309     rsync --verbose --archive ${testmaster_ssh}:$BASE/logs/ $(rootdir $BASE)/build/testlogs
310     # push them to the build web
311     chmod -R a+r $(rootdir $BASE)/build/testlogs/
312     webpublish_rsync_dir $WEBPATH/$BASE/testlogs/ $(rootdir $BASE)/build/testlogs/
313
314     echo  "============================== END $COMMAND:run_log on $(date)"
315
316     if [ -z "$success" ] ; then
317         echo "Tests have failed - bailing out"
318         failure
319     fi
320     
321 }
322
323 function in_root_context () {
324     rpm -q libvirt > /dev/null 
325 }
326
327 # this part won't work if WEBHOST does not match the local host
328 # would need to be made webpublish_* compliant
329 # but do we really need this feature anyway ?
330 function sign_node_packages () {
331
332     echo "Signing node packages"
333     
334     need_createrepo=""
335
336     repository=$WEBPATH/$BASE/RPMS/
337     # the rpms that need signing
338     new_rpms=
339     # and the corresponding stamps
340     new_stamps=
341
342     for package in $(find $repository/ -name '*.rpm') ; do
343         stamp=$repository/signed-stamps/$(basename $package).signed
344         # If package is newer than signature stamp
345         if [ $package -nt $stamp ] ; then
346             new_rpms="$new_rpms $package"
347             new_stamps="$new_stamps $stamp"
348         fi
349         # Or than createrepo database
350         [ $package -nt $repository/repodata/repomd.xml ] && need_createrepo=true
351     done
352
353     if [ -n "$new_rpms" ] ; then
354         # Create a stamp once the package gets signed
355         mkdir $repository/signed-stamps 2> /dev/null
356         
357         # Sign RPMS. setsid detaches rpm from the terminal,
358         # allowing the (hopefully blank) GPG password to be
359         # entered from stdin instead of /dev/tty.
360         echo | setsid rpm \
361             --define "_signature gpg" \
362             --define "_gpg_path $GPGPATH" \
363             --define "_gpg_name $GPGUID" \
364             --resign $new_rpms && touch $new_stamps
365     fi
366
367      # Update repository index / yum metadata. 
368     if [ -n "$need_createrepo" ] ; then
369         echo "Indexing node packages after signing"
370         if [ -f $repository/yumgroups.xml ] ; then
371             createrepo --quiet -g yumgroups.xml $repository
372         else
373             createrepo --quiet $repository
374         fi
375     fi
376 }
377
378 function show_env () {
379     set +x
380     echo FCDISTRO=$FCDISTRO
381     echo PLDISTRO=$PLDISTRO
382     echo PERSONALITY=$PERSONALITY
383     echo BASE=$BASE
384     echo BUILD_SCM_URL=$BUILD_SCM_URL
385     echo MAKEVARS="${MAKEVARS[@]}"
386     echo DRY_RUN="$DRY_RUN"
387     echo PLDISTROTAGS="$PLDISTROTAGS"
388     # this does not help, it's not yet set when we run show_env
389     #echo WEBPATH="$WEBPATH"
390     echo TESTBUILDURL="$TESTBUILDURL"
391     echo WEBHOST="$WEBHOST"
392     if in_root_context ; then
393         echo PLDISTROTAGS="$PLDISTROTAGS"
394     else
395         if [ -f /build/$PLDISTROTAGS ] ; then
396             echo "XXXXXXXXXXXXXXXXXXXX Contents of tags definition file /build/$PLDISTROTAGS"
397             cat /build/$PLDISTROTAGS
398             echo "XXXXXXXXXXXXXXXXXXXX end tags definition"
399         else
400             echo "XXXXXXXXXXXXXXXXXXXX Cannot find tags definition file /build/$PLDISTROTAGS, assuming remote pldistro"
401         fi
402     fi
403     set -x
404 }
405
406 function setupssh () {
407     base=$1; shift
408     sshkey=$1; shift
409     
410     if [ -f ${sshkey} ] ; then
411         SSHDIR=$(rootdir ${base})/root/.ssh
412         mkdir -p ${SSHDIR}
413         cp $sshkey ${SSHDIR}/thekey
414         (echo "host *"; \
415             echo "  IdentityFile ~/.ssh/thekey"; \
416             echo "  StrictHostKeyChecking no" ) > ${SSHDIR}/config
417         chmod 700 ${SSHDIR}
418         chmod 400 ${SSHDIR}/*
419     else 
420         echo "WARNING : could not find provided ssh key $sshkey - ignored"
421     fi
422 }
423
424 function usage () {
425     echo "Usage: $COMMAND [option] [var=value...] make-targets"
426     echo "Supported options"
427     echo " -f fcdistro - defaults to $DEFAULT_FCDISTRO"
428     echo " -d pldistro - defaults to $DEFAULT_PLDISTRO"
429     echo " -p personality - defaults to $DEFAULT_PERSONALITY"
430     echo " -m mailto - no default"
431     echo " -s build_scm_url - git URL where to fetch the build module - defaults to $DEFAULT_BUILD_SCM_URL"
432     echo "    define GIT tag or branch name appending @tagname to url"
433     echo " -t pldistrotags - defaults to \${PLDISTRO}-tags.mk"
434     echo " -b base - defaults to $DEFAULT_BASE"
435     echo "    @NAME@ replaced as appropriate"
436     echo " -o base: (overwrite) do not re-create vm, re-use base instead"
437     echo "    the -f/-d/-p/-m/-s/-t options are uneffective in this case"
438     echo " -c testconfig - defaults to $DEFAULT_TESTCONFIG"
439     echo " -y {pl,pg} - passed to run_log"
440     echo " -e step - passed to run_log"
441     echo " -X : passes --lxc to run_log"
442     echo " -S : passes --vs to run_log"
443     echo " -x <run_log_args> - a hook to pass other arguments to run_log"
444     echo " -w webpath - defaults to $DEFAULT_WEBPATH"
445     echo " -W testbuildurl - defaults to $DEFAULT_TESTBUILDURL; this is also used to get the hostname where to publish builds"
446     echo " -r webroot - defaults to $DEFAULT_WEBROOT - the fs point where testbuildurl actually sits"
447     echo " -M testmaster - defaults to $DEFAULT_TESTMASTER"
448     echo " -Y - sign yum repo in webpath"
449     echo " -g gpg_path - to the gpg secring used to sign rpms.  Defaults to $DEFAULT_GPGPATH" 
450     echo " -u gpg_uid - email used in secring. Defaults to $DEFAULT_GPGUID"
451     echo " -K sshkey - specify ssh key to use when reaching git over ssh"
452     echo " -S - do not publish source rpms"
453     echo " -B - run build only"
454     echo " -T - run test only"
455     echo " -n - dry-run: -n passed to make - vm gets created though - no mail sent"
456     echo " -v - be verbose"
457     echo " -7 - uses weekday-@FCDISTRO@ as base"
458     echo " -i ifname - defaults to $DEFAULT_IFNAME - used to determine local IP"
459     echo " --build-branch branch - build using the branch from build module"
460     exit 1
461 }
462
463 function main () {
464
465     set -e
466
467     # parse arguments
468     MAKEVARS=()
469     MAKETARGETS=()
470     DRY_RUN=
471     DO_BUILD=true
472     DO_TEST=true
473     PUBLISH_SRPMS=true
474     SSH_KEY=""
475     SIGNYUMREPO=""
476
477     OPTS_ORIG=$@
478     OPTS=$(getopt -o "f:d:p:m:s:t:b:o:c:y:e:XSx:w:W:r:M:Yg:u:K:SBTnv7i:P:h" -l "build-branch:" -- $@)
479     if [ $? != 0 ]
480     then
481         usage
482     fi
483     eval set -- "$OPTS"
484     while true; do
485         case $1 in
486             -f) FCDISTRO=$2; shift 2 ;;
487             -d) PLDISTRO=$2; shift 2 ;;
488             -p) PERSONALITY=$2; shift 2 ;;
489             -m) MAILTO=$2; shift 2 ;;
490             -s) BUILD_SCM_URL=$2; shift 2 ;;
491             -t) PLDISTROTAGS=$2; shift 2 ;;
492             -b) BASE=$2; shift 2 ;;
493             -o) OVERBASE=$2; shift 2 ;;
494             -c) TESTCONFIG="$TESTCONFIG $2"; shift 2 ;;
495             ########## passing stuff to run_log
496             # -y foo -> run_log -y foo
497             -y) RUN_LOG_EXTRAS="$RUN_LOG_EXTRAS --rspec-style $2"; shift 2 ;;
498             # -e foo -> run_log -e foo
499             -e) RUN_LOG_EXTRAS="$RUN_LOG_EXTRAS --exclude $2"; shift 2 ;;
500             # -X -> run_log --lxc
501             -X) RUN_LOG_EXTRAS="$RUN_LOG_EXTRAS --lxc"; shift;;
502             # -S -> run_log --vs
503             -S) RUN_LOG_EXTRAS="$RUN_LOG_EXTRAS --vs"; shift;;
504             # more general form to pass args to run_log
505             # -x foo -> run_log foo
506             -x) RUN_LOG_EXTRAS="$RUN_LOG_EXTRAS $2"; shift 2;;
507             ##########
508             -w) WEBPATH=$2; shift 2 ;;
509             -W) TESTBUILDURL=$2; shift 2 ;;
510             -r) WEBROOT=$2; shift 2 ;;
511             -M) TESTMASTER=$2; shift 2 ;;
512             -Y) SIGNYUMREPO=true; shift ;;
513             -g) GPGPATH=$2; shift 2 ;;
514             -u) GPGUID=$2; shift 2 ;;
515             -K) SSH_KEY=$2; shift 2 ;;
516             -S) PUBLISH_SRPMS="" ; shift ;;
517             -B) DO_TEST= ; shift ;;
518             -T) DO_BUILD= ; shift;;
519             -n) DRY_RUN="-n" ; shift ;;
520             -v) set -x ; VERBOSE="-v" ; shift ;;
521             -7) BASE="$(date +%a|tr A-Z a-z)-@FCDISTRO@" ; shift ;;
522             -i) IFNAME=$2; shift 2 ;;
523             -P) PREINSTALLED="-P $2"; shift 2;;
524             -h) usage ; shift ;;
525             --) shift; break ;;
526         esac
527     done
528
529     # preserve options for passing them again later, together with expanded base
530     options=$OPTS_ORIG
531
532     # allow var=value stuff; 
533     for target in "$@" ; do
534         # check if contains '='
535         target1=$(echo $target | sed -e s,=,,)
536         if [ "$target" = "$target1" ] ; then
537             MAKETARGETS=(${MAKETARGETS[@]} "$target")
538         else
539             MAKEVARS=(${MAKEVARS[@]} "$target")
540         fi
541     done
542     
543     # set defaults
544     [ -z "$FCDISTRO" ] && FCDISTRO=$DEFAULT_FCDISTRO
545     [ -z "$PLDISTRO" ] && PLDISTRO=$DEFAULT_PLDISTRO
546     [ -z "$PERSONALITY" ] && PERSONALITY=$DEFAULT_PERSONALITY
547     [ -z "$PLDISTROTAGS" ] && PLDISTROTAGS="${PLDISTRO}-tags.mk"
548     [ -z "$BASE" ] && BASE="$DEFAULT_BASE"
549     [ -z "$WEBPATH" ] && WEBPATH="$DEFAULT_WEBPATH"
550     [ -z "$TESTBUILDURL" ] && TESTBUILDURL="$DEFAULT_TESTBUILDURL"
551     [ -z "$WEBROOT" ] && WEBROOT="$DEFAULT_WEBROOT"
552     [ -z "$GPGPATH" ] && GPGPATH="$DEFAULT_GPGPATH"
553     [ -z "$GPGUID" ] && GPGUID="$DEFAULT_GPGUID"
554     [ -z "$IFNAME" ] && IFNAME="$DEFAULT_IFNAME"
555     [ -z "$BUILD_SCM_URL" ] && BUILD_SCM_URL="$DEFAULT_BUILD_SCM_URL"
556     [ -z "$TESTCONFIG" ] && TESTCONFIG="$DEFAULT_TESTCONFIG"
557     [ -z "$TESTMASTER" ] && TESTMASTER="$DEFAULT_TESTMASTER"
558
559     [ -n "$DRY_RUN" ] && MAILTO=""
560
561     # elaborate the extra args to be passed to run_log
562     for config in ${TESTCONFIG} ; do
563         RUN_LOG_EXTRAS="$RUN_LOG_EXTRAS --config $config"
564     done
565     
566         
567     if [ -n "$OVERBASE" ] ; then
568         sedargs="-e s,@DATE@,${DATE},g"
569         BASE=$(echo ${OVERBASE} | sed $sedargs)
570     else
571         sedargs="-e s,@DATE@,${DATE},g -e s,@FCDISTRO@,${FCDISTRO},g -e s,@PLDISTRO@,${PLDISTRO},g -e s,@PERSONALITY@,${PERSONALITY},g"
572         BASE=$(echo ${BASE} | sed $sedargs)
573     fi
574
575     ### elaborate mail subject
576     if [ -n "$DO_BUILD" -a -n "$DO_TEST" ] ; then
577         MAIL_SUBJECT="full"
578     elif [ -n "$DO_BUILD" ] ; then
579         MAIL_SUBJECT="pkg-only"
580     elif [ -n "$DO_TEST" ] ; then
581         MAIL_SUBJECT="test-only"
582     fi
583     if [ -n "$OVERBASE" ] ; then
584         MAIL_SUBJECT="${MAIL_SUBJECT} rerun"
585     else
586         MAIL_SUBJECT="${MAIL_SUBJECT} fresh"
587     fi
588     short_hostname=$(hostname | cut -d. -f1)
589     MAIL_SUBJECT="on ${short_hostname} - ${MAIL_SUBJECT}"
590
591     ### compute WEBHOST from TESTBUILDURL 
592     # this is to avoid having to change the builds configs everywhere
593     # simplistic way to extract hostname from a URL
594     WEBHOST=$(echo "$TESTBUILDURL" | cut -d/ -f 3)
595
596     if ! in_root_context ; then
597         # in the vm
598         echo "==================== Within LXC BEG $(date)"
599         build
600         echo "==================== Within LXC END $(date)"
601
602     else
603         trap failure ERR INT
604         # we run in the root context : 
605         # (*) create or check for the vm to use
606         # (*) copy this command in the vm
607         # (*) invoke it
608         
609         if [ -n "$OVERBASE" ] ; then
610             ### Re-use a vm (finish an unfinished build..)
611             if [ ! -d $(rootdir ${BASE}) ] ; then
612                 echo $COMMAND : cannot find vm $BASE
613                 exit 1
614             fi
615             # manage LOG - beware it might be a symlink so nuke it first
616             LOG=$(logfile ${BASE})
617             rm -f $LOG
618             exec > $LOG 2>&1
619             set -x
620             echo "XXXXXXXXXX $COMMAND: using existing vm $BASE" $(date)
621             # start in case e.g. we just rebooted
622             virsh --connect lxc:/// start ${BASE} || :
623             # retrieve environment from the previous run
624             FCDISTRO=$(lxcsu -ro ${BASE} /build/getdistroname.sh)
625             BUILD_SCM_URL=$(lxcsu -ro ${BASE} make --no-print-directory -C /build stage1=skip +build-GITPATH)
626             # for efficiency, crop everything in one make run
627             tmp=/tmp/${BASE}-env.sh
628             lxcsu -ro ${BASE} make --no-print-directory -C /build stage1=skip \
629                 ++PLDISTRO ++PLDISTROTAGS ++PERSONALITY ++MAILTO ++WEBPATH ++TESTBUILDURL ++WEBROOT > $tmp
630             . $tmp
631             rm -f $tmp
632             # update build
633             [ -n "$SSH_KEY" ] && setupssh ${BASE} ${SSH_KEY}
634             lxcsu -ro $BASE bash -c "cd /build; git pull; make tests-clean"
635             # make sure we refresh the tests place in case it has changed
636             rm -f /build/MODULES/tests
637             options=(${options[@]} -d $PLDISTRO -t $PLDISTROTAGS -s $BUILD_SCM_URL)
638             [ -n "$PERSONALITY" ] && options=(${options[@]} -p $PERSONALITY)
639             [ -n "$MAILTO" ] && options=(${options[@]} -m $MAILTO)
640             [ -n "$WEBPATH" ] && options=(${options[@]} -w $WEBPATH)
641             [ -n "$TESTBUILDURL" ] && options=(${options[@]} -W $TESTBUILDURL)
642             [ -n "$WEBROOT" ] && options=(${options[@]} -r $WEBROOT)
643             show_env
644         else
645             # create vm: check it does not exist yet
646             i=
647             while [ -d $(rootdir ${BASE})${i} ] ; do
648                 # we name subsequent builds <base>-n<i> so the logs and builds get sorted properly
649                 [ -z ${i} ] && BASE=${BASE}-n
650                 i=$((${i}+1))
651                 if [ $i -gt 100 ] ; then
652                     echo "$COMMAND: Failed to create build vm $(rootdir ${BASE})${i}"
653                     exit 1
654                 fi
655             done
656             BASE=${BASE}${i}
657             # need update
658             # manage LOG - beware it might be a symlink so nuke it first
659             LOG=$(logfile ${BASE})
660             rm -f $LOG
661             exec > $LOG 2>&1 
662             set -x
663             echo "XXXXXXXXXX $COMMAND: creating vm $BASE" $(date)
664             show_env
665
666             ### extract the whole build - much simpler
667             tmpdir=/tmp/$COMMAND-$$
668             GIT_REPO=$(echo $BUILD_SCM_URL | cut -d@ -f1)
669             GIT_TAG=$(echo $BUILD_SCM_URL | cut -s -d@ -f2)
670             GIT_TAG=${GIT_TAG:-master}
671             mkdir -p $tmpdir; git archive --remote=$GIT_REPO $GIT_TAG | tar -C $tmpdir -xf -
672
673             # Create lxc vm
674             cd $tmpdir
675             ./lbuild-initvm.sh $VERBOSE -f ${FCDISTRO} -d ${PLDISTRO} -p ${PERSONALITY} -i ${IFNAME} ${PREINSTALLED} ${BASE} 
676             # cleanup
677             cd -
678             rm -rf $tmpdir
679             # Extract build again - in the vm
680             [ -n "$SSH_KEY" ] && setupssh ${BASE} ${SSH_KEY}
681             # xxx not working as of now - waiting for Sapan to look into this
682             lxcsu -ro $BASE -- bash -c "git clone $GIT_REPO /build; cd /build; git checkout $GIT_TAG"
683         fi
684         echo "XXXXXXXXXX $COMMAND: preparation of vm $BASE done" $(date)
685
686         # The log inside the vm contains everything
687         LOG2=$(rootdir ${BASE})/log.txt
688         (echo "==================== BEG LXC Transcript of vm creation" ; \
689          cat $LOG ; \
690          echo "==================== END LXC Transcript of vm creation" ; \
691          echo "xxxxxxxxxx Messing with logs, symlinking $LOG2 to $LOG" ) >> $LOG2
692         ### not too nice : nuke the former log, symlink it to the new one
693         rm $LOG; ln -s $LOG2 $LOG
694         LOG=$LOG2
695         # redirect log again
696         exec >> $LOG 2>&1 
697
698         sedargs="-e s,@DATE@,${DATE},g -e s,@FCDISTRO@,${FCDISTRO},g -e s,@PLDISTRO@,${PLDISTRO},g -e s,@PERSONALITY@,${PERSONALITY},g"
699         WEBPATH=$(echo ${WEBPATH} | sed $sedargs)
700         webpublish mkdir -p ${WEBPATH}
701
702         # where to store the log for web access
703         WEBBASE=${WEBPATH}/${BASE}
704         WEBLOG=${WEBPATH}/${BASE}/log.txt
705         # compute the log URL - inserted in the mail messages for convenience
706         WEBBASE_URL=$(echo $WEBBASE | sed -e "s,//,/,g" -e "s,${WEBROOT},${TESTBUILDURL},")
707     
708         if [ -n "$DO_BUILD" ] ; then 
709
710             # invoke this command into the build directory of the vm
711             cp $COMMANDPATH $(rootdir ${BASE})/build/
712
713             # invoke this command in the vm for building (-T)
714             lxcsu -ro ${BASE} chmod +x /build/$COMMAND
715             lxcsu -ro ${BASE} /build/$COMMAND "${options[@]}" -b "${BASE}" "${MAKEVARS[@]}" "${MAKETARGETS[@]}"
716         fi
717
718         # publish to the web so run_log can find them
719         set +e
720         webpublish rm -rf $WEBPATH/$BASE 
721         # guess if we've been doing any debian-related build
722         if [ ! -f $(rootdir $BASE)/etc/debian_version  ] ; then
723             webpublish mkdir -p $WEBPATH/$BASE/{RPMS,SRPMS}
724             webpublish_rsync_dir $WEBPATH/$BASE/RPMS/ $(rootdir $BASE)/build/RPMS/
725             [[ -n "$PUBLISH_SRPMS" ]] && webpublish_rsync_dir $WEBPATH/$BASE/SRPMS/ $(rootdir $BASE)/build/SRPMS/
726         else
727             # run scanpackages so we can use apt-get on this
728             # (not needed on fedora b/c this is done by the regular build already)
729             lxcsu -ro $BASE bash -c "(cd /build ; dpkg-scanpackages DEBIAN/ | gzip -9c > Packages.gz)"
730             webpublish mkdir -p $WEBPATH/$BASE/DEBIAN
731             webpublish_rsync_files $WEBPATH/$BASE/DEBIAN/ $(rootdir $BASE)/build/DEBIAN/*.deb 
732             webpublish_rsync_files $WEBPATH/$BASE/ $(rootdir $BASE)/build/Packages.gz
733         fi
734         # publish myplc-release if this exists
735         release=$(rootdir $BASE)/build/myplc-release
736         [ -f $release ] && webpublish_rsync_files $WEBPATH/$BASE $release
737         set -e
738
739         # create yum repo and sign packages.
740         if [ -n "$SIGNYUMREPO" ] ; then
741             # this script does not yet support signing on a remote (webhost) repo
742             sign_here=$(hostname) ; sign_web=$(webpublish hostname)
743             if [ "$hostname" = "$sign_here" ] ; then
744                 sign_node_packages
745             else
746                 echo "$COMMAND does not support signing on a remote yum repo"
747                 echo "you might want to turn off the -y option, or run this on the web server box itself"
748                 exit 1
749             fi
750         fi
751
752         if [ -n "$DO_TEST" ] ; then 
753             run_log
754         fi
755
756         success 
757
758         echo "==================== MAIN END $(date)"    
759     fi
760
761 }  
762
763 ##########
764 main "$@"