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