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