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