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