0bd867952661c3699deb8735066aede3637fe972
[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=f7
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
14 # web publishing results
15 DEFAULT_WEBPATH="/build/@PLDISTRO@/"
16
17 # for the test part
18 TESTBUILDURL="http://build.one-lab.org/"
19 TESTBOX=onelab-test.inria.fr
20 TESTBOXSSH=root@onelab-test.inria.fr
21 TESTSVNPATH="http://svn.planet-lab.org/svn/tests/trunk/system/"
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     cp $LOG ${WEBLOG}
98     summary $LOG >> ${WEBLOG}
99     (echo -n "============================== $COMMAND: failure at " ; date ; tail -c 20k $WEBLOG) > ${WEBLOG}.ko
100     if [ -n "$MAILTO" ] ; then
101         tail -c 20k ${WEBLOG} | mail -s "Failures for build ${BASE}" $MAILTO
102     fi
103     exit 1
104 }
105
106 function success () {
107     set -x
108     WEBLOG=${WEBPATH}/${BASE}.log.txt
109     cp $LOG ${WEBLOG}
110     summary $LOG >> ${WEBLOG}
111     touch ${WEBLOG}.ok
112     if [ -n "$MAILTO" ] ; then
113         (echo "$PLDISTRO ($BASE) build for $FCDISTRO completed on $(date)" ) | mail -s "Successful build for ${BASE}" $MAILTO
114     fi
115     exit 0
116 }
117
118 # run in the vserver - do not manage success/failure, will be done from the root ctx
119 function build () {
120     set -x
121     set -e
122
123     echo -n "============================== Starting $COMMAND:build on "
124     date
125
126     cd /build
127   # if TAGSRELEASE specified : update PLDISTROTAGS with this tag
128     if [ -n "$TAGSRELEASE" ] ; then
129         svn up -r $TAGSRELEASE $PLDISTROTAGS
130     fi
131
132     show_env
133     
134     echo "Running make IN $(pwd)"
135     
136     # stuff our own variable settings
137     MAKEVARS=("PLDISTRO=${PLDISTRO}" "${MAKEVARS[@]}")
138     MAKEVARS=("PLDISTROTAGS=${PLDISTROTAGS}" "${MAKEVARS[@]}")
139     MAKEVARS=("NIGHTLY_BASE=${BASE}" "${MAKEVARS[@]}")
140     MAKEVARS=("NIGHTLY_PERSONALITY=${PERSONALITY}" "${MAKEVARS[@]}")
141
142     # stage1
143     make -C /build $DRY_RUN "${MAKEVARS[@]}" stage1=true 
144     # versions
145     make -C /build $DRY_RUN "${MAKEVARS[@]}" versions
146     # actual stuff
147     make -C /build $DRY_RUN "${MAKEVARS[@]}" $MAKETARGETS
148
149 }
150
151 # this was formerly run in the myplc-devel chroot but now is run in the root context,
152 # this is so that the .ssh config gets done manually, and once and for all
153 function runtest () {
154     set -x
155     set -e
156     trap failure ERR INT
157
158     echo -n "============================== Starting $COMMAND:runtest on $(date)"
159
160     ### the URL to the myplc package
161     rpm=$( (cd /vservers/$BASE/build/RPMS/i386 ; ls myplc-[0-9]*.rpm) )
162     if [ ${#rpm[@]} != 1 ] ; then
163         echo "$COMMAND: Cannot locate rpm for testing"
164         failure
165         exit 1
166     fi
167     url=${TESTBUILDURL}${PLDISTRO}/${BASE}/RPMS/i386/${rpm}
168
169     # compute test directory name on test box
170     testdir=test-${BASE}
171     # clean it
172     ssh ${TESTBOXSSH} rm -rf ${testdir}
173     # check it out
174     ssh ${TESTBOXSSH} svn co ${TESTSVNPATH} ${testdir}
175     # invoke test on testbox - pass url and build url - so the tests can use vtest-init-vserver.sh
176     ssh 2>&1 ${TESTBOXSSH} python -u ${testdir}/runtest --build ${SVNPATH} --url ${url} --all
177     # still missing - need to populate /var/www/html/install-rpms on the myplc
178         
179     if [ "$?" != 0 ] ; then
180         failure
181     fi
182     
183     echo -n "============================== End $COMMAND:runtest on $(date)"
184 }
185
186 function show_env () {
187     set +x
188     echo FCDISTRO=$FCDISTRO
189     echo PLDISTRO=$PLDISTRO
190     echo BASE=$BASE
191     echo SVNPATH=$SVNPATH
192     echo MAKEVARS="${MAKEVARS[@]}"
193     echo DRY_RUN="$DRY_RUN"
194     echo PLDISTROTAGS="$PLDISTROTAGS"
195     echo TAGSRELEASE="$TAGSRELEASE"
196     echo -n "(might be unexpanded)"
197     echo WEBPATH="$WEBPATH"
198     if [ -d /vservers ] ; then
199         echo PLDISTROTAGS="$PLDISTROTAGS"
200     else
201         echo "XXXXXXXXXXXXXXXXXXXX Contents of tags definition file /build/$PLDISTROTAGS"
202         cat /build/$PLDISTROTAGS
203         echo "XXXXXXXXXXXXXXXXXXXX end tags definition"
204     fi
205     set -x
206 }
207
208 function usage () {
209     echo "Usage: $COMMAND [option] make-targets"
210     echo "This is $REVISION"
211     echo "Supported options"
212     echo " -n dry-run : -n passed to make - vserver gets created though - no mail sent"
213     echo " -f fcdistro - defaults to $DEFAULT_FCDISTRO"
214     echo " -d pldistro - defaults to $DEFAULT_PLDISTRO"
215     echo " -p personality - defaults to $DEFAULT_PERSONALITY"
216     echo " -b base - defaults to $DEFAULT_BASE"
217     echo "    @NAME@ replaced as appropriate"
218     echo " -t pldistrotags - defaults to \${PLDISTRO}-tags.mk"
219     echo " -r tagsrelease - a release number that refers to PLDISTROTAGS - defaults to HEAD"
220     echo " -s svnpath - where to fetch the build module"
221     echo " -o : overwrite - re-run in base directory, do not create vserver"
222     echo " -m mailto"
223     echo " -a makevar=value - space in values are not supported"
224     echo " -w webpath - defaults to $DEFAULT_WEBPATH"
225     echo " -B : run build only"
226     echo " -T : run test only"
227     echo " -v : be verbose"
228     echo " -7 : uses weekday-@FCDISTRO@ as base"
229     exit 1
230 }
231
232 function main () {
233
234     set -e
235
236     # preserve arguments for passing them again later
237     declare -a argv
238     for arg in "$@"; do argv=(${argv[@]} "$arg") ; done
239     
240     # parse arguments
241     MAKEVARS=()
242     DRY_RUN=
243     DO_BUILD=true
244     DO_TEST=true
245     while getopts "nf:d:b:p:t:r:s:om:a:w:BTvh7" opt ; do
246         case $opt in
247             n) DRY_RUN="-n" ;;
248             f) FCDISTRO=$OPTARG ;;
249             d) PLDISTRO=$OPTARG ;;
250             p) PERSONALITY=$OPTARG ;;
251             b) BASE=$OPTARG ;;
252             t) PLDISTROTAGS=$OPTARG ;;
253             r) TAGSRELEASE=$OPTARG ;;
254             s) SVNPATH=$OPTARG ;;
255             o) OVERWRITEMODE=true ;;
256             m) MAILTO=$OPTARG ;;
257             a) MAKEVARS=(${MAKEVARS[@]} "$OPTARG") ;;
258             w) WEBPATH=$OPTARG ;;
259             B) DO_TEST= ;;
260             T) DO_BUILD= ; OVERWRITEMODE=true ;;
261             v) set -x ;;
262             7) BASE="$(date +%a|tr A-Z a-z)-@FCDISTRO@" ;;
263             h|*) usage ;;
264         esac
265     done
266         
267     shift $(($OPTIND - 1))
268     MAKETARGETS="$@"
269     
270     # set defaults
271     [ -z "$FCDISTRO" ] && FCDISTRO=$DEFAULT_FCDISTRO
272     [ -z "$PLDISTRO" ] && PLDISTRO=$DEFAULT_PLDISTRO
273     [ -z "$PERSONALITY" ] && PERSONALITY=$DEFAULT_PERSONALITY
274     [ -z "$PLDISTROTAGS" ] && PLDISTROTAGS="${PLDISTRO}-tags.mk"
275     [ -z "$BASE" ] && BASE="$DEFAULT_BASE"
276     [ -z "$WEBPATH" ] && WEBPATH="$DEFAULT_WEBPATH"
277     [ -z "$SVNPATH" ] && SVNPATH="$DEFAULT_SVNPATH"
278
279     [ -n "$DRY_RUN" ] && MAILTO=""
280         
281     ### set BASE from DISTRO, if unspecified
282     sedargs="-e s,@DATE@,${DATE},g -e s,@FCDISTRO@,${FCDISTRO},g -e s,@PLDISTRO@,${PLDISTRO},g -e s,@PERSONALITY@,${PERSONALITY},g"
283     BASE=$(echo ${BASE} | sed $sedargs)
284     WEBPATH=$(echo ${WEBPATH} | sed $sedargs)
285
286     if [ ! -d /vservers ] ; then
287         # in the vserver
288         echo "==================== Within vserver BEG $(date)"
289         build
290         echo "==================== Within vserver END $(date)"
291
292     else
293         trap failure ERR INT
294         # we run in the root context : 
295         # (*) create or check for the vserver to use
296         # (*) copy this command in the vserver
297         # (*) invoke it
298         
299         if [ -n "$OVERWRITEMODE" ] ; then
300             ### Re-use a vserver (finish an unfinished build..)
301             if [ ! -d /vservers/${BASE} ] ; then
302                 echo $COMMAND : cannot find vserver $BASE
303                 exit 1
304             fi
305             # manage LOG - beware it might be a symlink so nuke it first
306             LOG=/vservers/${BASE}.log.txt
307             rm -f $LOG
308             exec > $LOG 2>&1
309             set -x
310             echo "XXXXXXXXXX $COMMAND: using existing vserver $BASE" $(date)
311             show_env
312             # update build
313             vserver ${BASE} exec svn update /build
314         else
315             # create vserver: check it does not exist yet
316             i=
317             while [ -d /vservers/${BASE}${i} ] ; do
318                 # we name subsequent builds <base>-n<i> so the logs and builds get sorted properly
319                 [ -z ${i} ] && BASE=${BASE}-n
320                 i=$((${i}+1))
321                 if [ $i -gt 100 ] ; then
322                     echo "$COMMAND: Failed to create build vserver /vservers/${BASE}${i}"
323                     exit 1
324                 fi
325             done
326             BASE=${BASE}${i}
327             # need update
328             # manage LOG - beware it might be a symlink so nuke it first
329             LOG=/vservers/${BASE}.log.txt
330             rm -f $LOG
331             exec > $LOG 2>&1 
332             set -x
333             echo "XXXXXXXXXX $COMMAND: creating vserver $BASE" $(date)
334             show_env
335
336             ### extract the whole build - much simpler
337             tmpdir=/tmp/$COMMAND-$$
338             svn export $SVNPATH $tmpdir
339             # Create vserver
340             cd $tmpdir
341             ./vbuild-init-vserver.sh -f ${FCDISTRO} -d ${PLDISTRO} -p ${PERSONALITY} ${BASE}
342             # cleanup
343             cd -
344             rm -rf $tmpdir
345             # Extract build again - in the vserver
346             vserver ${BASE} exec svn checkout ${SVNPATH} /build
347         fi
348         echo "XXXXXXXXXX $COMMAND: preparation of vserver $BASE done" $(date)
349
350         # The log inside the vserver contains everything
351         LOG2=/vservers/${BASE}/log.txt
352         (echo "==================== BEG VSERVER Transcript of vserver creation" ; \
353          cat $LOG ; \
354          echo "==================== END VSERVER Transcript of vserver creation" ; \
355          echo "xxxxxxxxxx Messing with logs, symlinking $LOG2 to $LOG" ) >> $LOG2
356         ### not too nice : nuke the former log, symlink it to the new one
357         rm $LOG; ln -s $LOG2 $LOG
358         LOG=$LOG2
359         # redirect log again
360         exec >> $LOG 2>&1 
361
362         if [ -n "$DO_BUILD" ] ; then 
363
364             cp $COMMANDPATH /vservers/${BASE}/build/
365
366             # invoke this command in the vserver for building (-T)
367             vserver ${BASE} exec chmod +x /build/$COMMAND
368             vserver ${BASE} exec /build/$COMMAND "${argv[@]}" -b "${BASE}"
369         fi
370
371         # publish to the web so runtest can find them
372         rm -rf $WEBPATH/$BASE ; mkdir -p $WEBPATH/$BASE/{RPMS,SRPMS}
373         rsync --archive --delete --verbose /vservers/$BASE/build/RPMS/ $WEBPATH/$BASE/RPMS/
374         rsync --archive --delete --verbose /vservers/$BASE/build/SRPMS/ $WEBPATH/$BASE/SRPMS/
375         
376         if [ -n "$DO_TEST" ] ; then 
377             runtest
378         fi
379
380         success 
381         
382     fi
383
384 }  
385
386 ##########
387 main "$@"