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