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