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