set default test config in tests/, not here
[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=f8
9 DEFAULT_PLDISTRO=planetlab
10 DEFAULT_PERSONALITY=linux32
11 DEFAULT_BASE="@DATE@--@PLDISTRO@-@FCDISTRO@-@PERSONALITY@"
12 DEFAULT_SVNPATH="http://svn.planet-lab.org/svn/build/trunk"
13 DEFAULT_TESTSVNPATH="http://svn.planet-lab.org/svn/tests/trunk/system/"
14 DEFAULT_TESTCONFIG="default"
15 DEFAULT_IFNAME=eth0
16
17 # web publishing results
18 DEFAULT_WEBPATH="/build/@PLDISTRO@/"
19
20 # for the test part
21 TESTBUILDURL="http://build.one-lab.org/"
22 TESTBOXSSH=root@testbox.one-lab.org
23 ####################
24 # assuming vserver runs in UTC
25 DATE=$(date +'%Y.%m.%d')
26
27 # temporary - wrap a quick summary of suspicious stuff
28 # this is to focus on installation that go wrong
29 # use with care, a *lot* of other things can go bad as well
30 function summary () {
31     from=$1; shift
32     echo "******************** BEG SUMMARY" 
33     python - $from <<EOF
34 #!/usr/bin/env python
35 # read a full log and tries to extract the interesting stuff
36
37 import sys,re
38 m_show_line=re.compile(".* BEG (RPM|VSERVER).*|.*'boot'.*|\* .*|.*is not installed.*")
39 m_installing_any=re.compile('\r  (Installing:[^\]]*]) ')
40 m_installing_err=re.compile('\r  (Installing:[^\]]*])(..+)')
41 m_installing_end=re.compile('Installed:.*')
42 m_installing_doc1=re.compile("(.*)install-info: No such file or directory for /usr/share/info/\S+(.*)")
43 m_installing_doc2=re.compile("(.*)grep: /usr/share/info/dir: No such file or directory(.*)")
44
45 def summary (filename):
46
47     try:
48         if filename=="-":
49             filename="stdin"
50             f=sys.stdin
51         else:
52             f=open(filename)
53         echo=False
54         for line in f.xreadlines():
55             # first off : discard warnings related to doc
56             if m_installing_doc1.match(line):
57                 (begin,end)=m_installing_doc1.match(line).groups()
58                 line=begin+end
59             if m_installing_doc2.match(line):
60                 (begin,end)=m_installing_doc2.match(line).groups()
61                 line=begin+end
62             # unconditionnally show these lines
63             if m_show_line.match(line):
64                 print '>>>',line,
65             # an 'installing' line with messages afterwards : needs to be echoed
66             elif m_installing_err.match(line):
67                 (installing,error)=m_installing_err.match(line).groups()
68                 print '>>>',installing
69                 print '>>>',error
70                 echo=True
71             # closing an 'installing' section
72             elif m_installing_end.match(line):
73                 echo=False
74             # any 'installing' line
75             elif m_installing_any.match(line):
76                 if echo: 
77                     installing=m_installing_any.match(line).group(1)
78                     print '>>>',installing
79                 echo=False
80             # print lines when echo is true
81             else:
82                 if echo: print '>>>',line,
83         f.close()
84     except:
85         print 'Failed to analyze',filename
86
87 for arg in sys.argv[1:]:
88     summary(arg)
89 EOF
90     echo "******************** END SUMMARY" 
91 }
92
93
94 # Notify recipient of failure or success, manage various stamps 
95 function failure() {
96     set -x
97     WEBLOG=${WEBPATH}/${BASE}.log.txt
98     mkdir -p ${WEBPATH}
99     cp $LOG ${WEBLOG}
100     summary $LOG >> ${WEBLOG}
101     (echo -n "============================== $COMMAND: failure at " ; date ; tail -c 30k $WEBLOG) > ${WEBLOG}.ko
102     if [ -n "$MAILTO" ] ; then
103         tail -c 30k ${WEBLOG} | mail -s "Failures for build ${BASE}" $MAILTO
104     fi
105     exit 1
106 }
107
108 function success () {
109     set -x
110     WEBLOG=${WEBPATH}/${BASE}.log.txt
111     mkdir -p ${WEBPATH}
112     cp $LOG ${WEBLOG}
113     summary $LOG >> ${WEBLOG}
114     if [ -n "$DO_TEST" ] ; then
115         echo "Successfully built and tested - see testlogs for details" > ${WEBLOG}.pass
116         rm -f ${WEBLOG}.ok
117     else
118         echo "Successfully built"> ${WEBLOG}.ok
119     fi
120     if [ -n "$MAILTO" ] ; then
121         (echo "$PLDISTRO ($BASE) build for $FCDISTRO completed on $(date)" ) | mail -s "Successful build for ${BASE}" $MAILTO
122     fi
123     exit 0
124 }
125
126 # run in the vserver - do not manage success/failure, will be done from the root ctx
127 function build () {
128     set -x
129     set -e
130
131     echo -n "============================== Starting $COMMAND:build on "
132     date
133
134     cd /build
135   # if TAGSRELEASE specified : update PLDISTROTAGS with this tag
136     if [ -n "$TAGSRELEASE" ] ; then
137         svn up -r $TAGSRELEASE $PLDISTROTAGS
138     fi
139
140     show_env
141     
142     echo "Running make IN $(pwd)"
143     
144     # stuff our own variable settings
145     MAKEVARS=("PLDISTRO=${PLDISTRO}" "${MAKEVARS[@]}")
146     MAKEVARS=("PLDISTROTAGS=${PLDISTROTAGS}" "${MAKEVARS[@]}")
147     MAKEVARS=("NIGHTLY_BASE=${BASE}" "${MAKEVARS[@]}")
148     MAKEVARS=("NIGHTLY_PERSONALITY=${PERSONALITY}" "${MAKEVARS[@]}")
149
150     # stage1
151     make -C /build $DRY_RUN "${MAKEVARS[@]}" stage1=true 
152     # versions
153     make -C /build $DRY_RUN "${MAKEVARS[@]}" versions
154     # actual stuff
155     make -C /build $DRY_RUN "${MAKEVARS[@]}" $MAKETARGETS
156
157 }
158
159 # this was formerly run in the myplc-devel chroot but now is run in the root context,
160 # this is so that the .ssh config gets done manually, and once and for all
161 function runtest () {
162     set -x
163     set -e
164     trap failure ERR INT
165
166     echo -n "============================== Starting $COMMAND:runtest on $(date)"
167
168     ### the URL to the myplc package
169     rpm=$( find /vservers/$BASE/build/RPMS -name 'myplc-[0-9]*' )
170     if [ ${#rpm[@]} != 1 ] ; then
171         echo "$COMMAND: Cannot locate rpm for testing"
172         failure
173         exit 1
174     fi
175     url=$(echo $rpm | sed -e "s,/vservers/$BASE/build,${TESTBUILDURL}${PLDISTRO}/${BASE},")
176
177     # compute test directory name on test box
178     testdir=chroot-${BASE}
179     # use another name if any config contains vserver
180     echo $TESTCONFIG | grep vserver &> /dev/null && testdir=vserver-${BASE}
181     # clean it
182     ssh ${TESTBOXSSH} rm -rf ${testdir}
183     # check it out
184     ssh ${TESTBOXSSH} svn co ${TESTSVNPATH} ${testdir}
185     # invoke test on testbox - pass url and build url - so the tests can use vtest-init-vserver.sh
186     configs=""
187     for config in ${TESTCONFIG} ; do
188         configs="$configs --config $config"
189     done
190     [ "$PERSONALITY" == linux64 ] && personality="--personality linux64"
191     
192     
193     # need to proceed despite of set -e
194     success=true
195     ssh 2>&1 ${TESTBOXSSH} ${testdir}/runtest --build ${SVNPATH} --url ${url} $configs $personality --all || success=
196
197     # gather logs in the vserver
198     mkdir -p /vservers/$BASE/build/testlogs
199     ssh 2>&1 ${TESTBOXSSH} tar -C ${testdir}/logs -cf - . | tar -C /vservers/$BASE/build/testlogs -xf - || true
200     # push them to the build web
201     rsync --archive --delete /vservers/$BASE/build/testlogs/ $WEBPATH/$BASE/testlogs/
202     chmod -R a+r $WEBPATH/$BASE/testlogs/
203
204     if [ -z "$success" ] ; then
205         failure
206     fi
207     
208     echo -n "============================== End $COMMAND:runtest on $(date)"
209 }
210
211 function show_env () {
212     set +x
213     echo FCDISTRO=$FCDISTRO
214     echo PLDISTRO=$PLDISTRO
215     echo BASE=$BASE
216     echo SVNPATH=$SVNPATH
217     echo MAKEVARS="${MAKEVARS[@]}"
218     echo DRY_RUN="$DRY_RUN"
219     echo PLDISTROTAGS="$PLDISTROTAGS"
220     echo TAGSRELEASE="$TAGSRELEASE"
221     echo -n "(might be unexpanded)"
222     echo WEBPATH="$WEBPATH"
223     if [ -d /vservers ] ; then
224         echo PLDISTROTAGS="$PLDISTROTAGS"
225     else
226         echo "XXXXXXXXXXXXXXXXXXXX Contents of tags definition file /build/$PLDISTROTAGS"
227         cat /build/$PLDISTROTAGS
228         echo "XXXXXXXXXXXXXXXXXXXX end tags definition"
229     fi
230     set -x
231 }
232
233 function usage () {
234     echo "Usage: $COMMAND [option] make-targets"
235     echo "This is $REVISION"
236     echo "Supported options"
237     echo " -f fcdistro - defaults to $DEFAULT_FCDISTRO"
238     echo " -d pldistro - defaults to $DEFAULT_PLDISTRO"
239     echo " -p personality - defaults to $DEFAULT_PERSONALITY"
240     echo " -b base - defaults to $DEFAULT_BASE"
241     echo "    @NAME@ replaced as appropriate"
242     echo " -t pldistrotags - defaults to \${PLDISTRO}-tags.mk"
243     echo " -r tagsrelease - a release number that refers to PLDISTROTAGS - defaults to HEAD"
244     echo " -s svnpath - where to fetch the build module"
245     echo " -x testsvnpath - defaults to $DEFAULT_TESTSVNPATH"
246     echo " -c testconfig - defaults to $DEFAULT_TESTCONFIG"
247     echo " -w webpath - defaults to $DEFAULT_WEBPATH"
248     echo " -m mailto - no default"
249     echo " -O : overwrite - re-run in base directory, do not re-create vserver"
250     echo " -B : run build only"
251     echo " -T : run test only"
252     echo " -n dry-run : -n passed to make - vserver gets created though - no mail sent"
253     echo " -v : be verbose"
254     echo " -7 : uses weekday-@FCDISTRO@ as base"
255     echo " -a makevar=value - space in values are not supported"
256     echo " -i ifname - defaults to $DEFAULT_IFNAME - used to determine local IP"
257     exit 1
258 }
259
260 function main () {
261
262     set -e
263
264     # parse arguments
265     MAKEVARS=()
266     DRY_RUN=
267     DO_BUILD=true
268     DO_TEST=true
269     while getopts "f:d:p:b:t:r:s:x:c:w:m:OBTnv7a:i:" opt ; do
270         case $opt in
271             f) FCDISTRO=$OPTARG ;;
272             d) PLDISTRO=$OPTARG ;;
273             p) PERSONALITY=$OPTARG ;;
274             b) BASE=$OPTARG ;;
275             t) PLDISTROTAGS=$OPTARG ;;
276             r) TAGSRELEASE=$OPTARG ;;
277             s) SVNPATH=$OPTARG ;;
278             x) TESTSVNPATH=$OPTARG ;;
279             c) TESTCONFIG="$TESTCONFIG $OPTARG" ;;
280             w) WEBPATH=$OPTARG ;;
281             m) MAILTO=$OPTARG ;;
282             O) OVERWRITEMODE=true ;;
283             B) DO_TEST= ;;
284             T) DO_BUILD= ; OVERWRITEMODE=true ;;
285             n) DRY_RUN="-n" ;;
286             v) set -x ;;
287             7) BASE="$(date +%a|tr A-Z a-z)-@FCDISTRO@" ;;
288             a) MAKEVARS=(${MAKEVARS[@]} "$OPTARG") ;;
289             i) IFNAME=$OPTARG ;;
290             h|*) usage ;;
291         esac
292     done
293         
294     # preserve options for passing them again later, together with expanded base
295     declare -a options
296     toshift=$(($OPTIND - 1))
297     arg=1; while [ $arg -le $toshift ] ; do options=(${options[@]} "$1") ; shift; arg=$(($arg+1)) ; done
298
299     MAKETARGETS="$@"
300     
301     # set defaults
302     [ -z "$FCDISTRO" ] && FCDISTRO=$DEFAULT_FCDISTRO
303     [ -z "$PLDISTRO" ] && PLDISTRO=$DEFAULT_PLDISTRO
304     [ -z "$PERSONALITY" ] && PERSONALITY=$DEFAULT_PERSONALITY
305     [ -z "$PLDISTROTAGS" ] && PLDISTROTAGS="${PLDISTRO}-tags.mk"
306     [ -z "$BASE" ] && BASE="$DEFAULT_BASE"
307     [ -z "$WEBPATH" ] && WEBPATH="$DEFAULT_WEBPATH"
308     [ -z "$IFNAME" ] && IFNAME="$DEFAULT_IFNAME"
309     [ -z "$SVNPATH" ] && SVNPATH="$DEFAULT_SVNPATH"
310     [ -z "$TESTSVNPATH" ] && TESTSVNPATH="$DEFAULT_TESTSVNPATH"
311     [ -z "$TESTCONFIG" ] && TESTCONFIG="$DEFAULT_TESTCONFIG"
312
313     [ -n "$DRY_RUN" ] && MAILTO=""
314         
315     ### set BASE from DISTRO, if unspecified
316     sedargs="-e s,@DATE@,${DATE},g -e s,@FCDISTRO@,${FCDISTRO},g -e s,@PLDISTRO@,${PLDISTRO},g -e s,@PERSONALITY@,${PERSONALITY},g"
317     BASE=$(echo ${BASE} | sed $sedargs)
318     WEBPATH=$(echo ${WEBPATH} | sed $sedargs)
319
320     if [ ! -d /vservers ] ; then
321         # in the vserver
322         echo "==================== Within vserver BEG $(date)"
323         build
324         echo "==================== Within vserver END $(date)"
325
326     else
327         trap failure ERR INT
328         # we run in the root context : 
329         # (*) create or check for the vserver to use
330         # (*) copy this command in the vserver
331         # (*) invoke it
332         
333         if [ -n "$OVERWRITEMODE" ] ; then
334             ### Re-use a vserver (finish an unfinished build..)
335             if [ ! -d /vservers/${BASE} ] ; then
336                 echo $COMMAND : cannot find vserver $BASE
337                 exit 1
338             fi
339             # manage LOG - beware it might be a symlink so nuke it first
340             LOG=/vservers/${BASE}.log.txt
341             rm -f $LOG
342             exec > $LOG 2>&1
343             set -x
344             echo "XXXXXXXXXX $COMMAND: using existing vserver $BASE" $(date)
345             show_env
346             # update build
347             vserver ${BASE} exec svn update /build
348         else
349             # create vserver: check it does not exist yet
350             i=
351             while [ -d /vservers/${BASE}${i} ] ; do
352                 # we name subsequent builds <base>-n<i> so the logs and builds get sorted properly
353                 [ -z ${i} ] && BASE=${BASE}-n
354                 i=$((${i}+1))
355                 if [ $i -gt 100 ] ; then
356                     echo "$COMMAND: Failed to create build vserver /vservers/${BASE}${i}"
357                     exit 1
358                 fi
359             done
360             BASE=${BASE}${i}
361             # need update
362             # manage LOG - beware it might be a symlink so nuke it first
363             LOG=/vservers/${BASE}.log.txt
364             rm -f $LOG
365             exec > $LOG 2>&1 
366             set -x
367             echo "XXXXXXXXXX $COMMAND: creating vserver $BASE" $(date)
368             show_env
369
370             ### extract the whole build - much simpler
371             tmpdir=/tmp/$COMMAND-$$
372             svn export $SVNPATH $tmpdir
373             # Create vserver
374             cd $tmpdir
375             ./vbuild-init-vserver.sh -f ${FCDISTRO} -d ${PLDISTRO} -p ${PERSONALITY} -i ${IFNAME} ${BASE} 
376             # cleanup
377             cd -
378             rm -rf $tmpdir
379             # Extract build again - in the vserver
380             vserver ${BASE} exec svn checkout ${SVNPATH} /build
381         fi
382         echo "XXXXXXXXXX $COMMAND: preparation of vserver $BASE done" $(date)
383
384         # The log inside the vserver contains everything
385         LOG2=/vservers/${BASE}/log.txt
386         (echo "==================== BEG VSERVER Transcript of vserver creation" ; \
387          cat $LOG ; \
388          echo "==================== END VSERVER Transcript of vserver creation" ; \
389          echo "xxxxxxxxxx Messing with logs, symlinking $LOG2 to $LOG" ) >> $LOG2
390         ### not too nice : nuke the former log, symlink it to the new one
391         rm $LOG; ln -s $LOG2 $LOG
392         LOG=$LOG2
393         # redirect log again
394         exec >> $LOG 2>&1 
395
396         if [ -n "$DO_BUILD" ] ; then 
397
398             cp $COMMANDPATH /vservers/${BASE}/build/
399
400             # invoke this command in the vserver for building (-T)
401             vserver ${BASE} exec chmod +x /build/$COMMAND
402             vserver ${BASE} exec /build/$COMMAND "${options[@]}" -b "${BASE}" $MAKETARGETS
403         fi
404
405         # publish to the web so runtest can find them
406         rm -rf $WEBPATH/$BASE ; mkdir -p $WEBPATH/$BASE/{RPMS,SRPMS}
407         rsync --archive --delete --verbose /vservers/$BASE/build/RPMS/ $WEBPATH/$BASE/RPMS/
408         rsync --archive --delete --verbose /vservers/$BASE/build/SRPMS/ $WEBPATH/$BASE/SRPMS/
409         
410         if [ -n "$DO_TEST" ] ; then 
411             runtest
412         fi
413
414         success 
415         
416     fi
417
418 }  
419
420 ##########
421 main "$@"