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