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