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