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