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