other distros move to nodemanager that has a single specfile for all packages
[build.git] / lbuild-nightly.sh
index 1228e6a..d605573 100755 (executable)
@@ -1,25 +1,22 @@
 #!/bin/bash
 
-# NOTE on lxcsu
-# this tool derives from the PL code, from the lxc-userspace module
-# can be obtained through rpm/yum
-# it is used here as a replacement for 'vserver <> exec'
-# that is used throughout this code
-RUN_IN_DOMAIN="lxcsu -ro"
-# PS.
-# virsh lxc-enter-namespace $dom command to run
-# could maybe be used instead but it seems to require command's full path..
-#RUN_IN_DOMAIN="virsh lxc-enter-namespace"
-
 COMMANDPATH=$0
 COMMAND=$(basename $0)
 
+# close stdin, as with ubuntu and debian VMs this script tends to hang and wait for input ..
+0<&-
+
+# old guests have e.g. mount in /bin but this is no longer part of 
+# the standard PATH in recent hosts after usrmove, so let's keep it simple
+export PATH=$PATH:/bin:/sbin
+
 # default values, tunable with command-line options
 DEFAULT_FCDISTRO=f20
-DEFAULT_PLDISTRO=planetlab
+DEFAULT_PLDISTRO=lxc
 DEFAULT_PERSONALITY=linux64
-DEFAULT_BASE="@DATE@--@PLDISTRO@-@FCDISTRO@-@PERSONALITY@"
+DEFAULT_MAILDEST="build at onelab.eu"
 DEFAULT_BUILD_SCM_URL="git://git.onelab.eu/build"
+DEFAULT_BASE="@DATE@--@PLDISTRO@-@FCDISTRO@-@PERSONALITY@"
 
 # default gpg path used in signing yum repo
 DEFAULT_GPGPATH="/etc/planetlab"
@@ -31,33 +28,24 @@ DEFAULT_TESTCONFIG="default"
 RUN_LOG_EXTRAS=""
 
 # for publishing results, and the tests settings
-x=$(hostname)
-y=$(hostname|sed -e s,inria,,)
-# INRIA defaults
-if [ "$x" != "$y" ] ; then
-    DEFAULT_WEBPATH="/build/@PLDISTRO@/"
-    DEFAULT_TESTBUILDURL="http://build.onelab.eu/"
-    # this is where the buildurl is pointing towards
-    DEFAULT_WEBROOT="/build/"
-    DEFAULT_TESTMASTER="testmaster.onelab.eu"
-else
-    DEFAULT_WEBPATH="/build/@FCDISTRO@/@PLDISTRO@/"
-    DEFAULT_TESTBUILDURL="http://build.planet-lab.org/"
-    # this is where the buildurl is pointing towards
-    DEFAULT_WEBROOT="/build/"
-    DEFAULT_TESTMASTER="manager.test.planet-lab.org"
-fi    
+DEFAULT_WEBPATH="/build/@PLDISTRO@/"
+DEFAULT_TESTBUILDURL="http://build.onelab.eu/"
+# this is where the buildurl is pointing towards
+DEFAULT_WEBROOT="/build/"
+DEFAULT_TESTMASTER="testmaster.onelab.eu"
 
 ####################
 # assuming vm runs in UTC
 DATE=$(date +'%Y.%m.%d')
+BUILD_BEG=$(date +'%H:%M')
+BUILD_BEG_S=$(date +'%s')
 
 # still using /vservers for legacy reasons
 # as far as the build & test infra, we could adopt a new name
 # but the PL code still uses this name for now, so let's keep it simple
 function rootdir () {
     slice=$1; shift
-    echo /vservers/$slice/rootfs
+    echo /vservers/$slice
 }
 function logfile () {
     slice=$1; shift
@@ -144,12 +132,23 @@ function webpublish_rsync_files () {
     rsync --archive $VERBOSE "$@" root@${WEBHOST}:"$remote" ;
 }
 
+function pretty_duration () {
+    total_seconds=$1; shift
+
+    seconds=$(($total_seconds%60))
+    total_minutes=$(($total_seconds/60))
+    minutes=$(($total_minutes%60))
+    hours=$(($total_minutes/60))
+    
+    printf "%02d:%02d:%02d" $hours $minutes $seconds
+}
+
 # Notify recipient of failure or success, manage various stamps 
 function failure() {
     set -x
     # early stage ? - let's not create /build/@PLDISTRO@
-    if test -z "$WEBLOG" ; then
-       WEBHOST=$(hostname)
+    if  [ -z "$WEBLOG" ] ; then
+       WEBHOST=localhost
        WEBPATH=/tmp
        WEBBASE=/tmp/lbuild-early-$(date +%Y-%m-%d)
        WEBLOG=/tmp/lbuild-early-$(date +%Y-%m-%d).log.txt
@@ -160,16 +159,16 @@ function failure() {
     (echo -n "============================== $COMMAND: failure at " ; date ; \
        webpublish tail --lines=1000 $WEBLOG) | \
        webpublish_cp_stdin_to_file $WEBBASE.ko ||:
-    if [ -n "$MAILTO" ] ; then
+    if [ -n "$MAILDEST" ] ; then
        ( \
            echo "Subject: KO ${BASE} ${MAIL_SUBJECT}" ; \
-           echo "To: $MAILTO" ; \
+           echo "To: $MAILDEST" ; \
            echo "see build results at        $WEBBASE_URL" ; \
            echo "including full build log at $WEBBASE_URL/log.txt" ; \
            echo "and complete test logs at   $WEBBASE_URL/testlogs" ; \
            echo "........................................" ; \
            webpublish tail --lines=1000 $WEBLOG ) | \
-           sendmail $MAILTO
+           sendmail $MAILDEST
     fi
     exit 1
 }
@@ -177,8 +176,8 @@ function failure() {
 function success () {
     set -x
     # early stage ? - let's not create /build/@PLDISTRO@
-    if test -z "$WEBLOG" ; then
-       WEBHOST=$(hostname)
+    if [ -z "$WEBLOG" ] ; then
+       WEBHOST=localhost
        WEBPATH=/tmp
        WEBLOG=/tmp/lbuild-early-$(date +%Y-%m-%d).log.txt
     fi
@@ -187,12 +186,15 @@ function success () {
     summary $LOG | webpublish_append_stdin_to_file $WEBLOG
     if [ -n "$DO_TEST" ] ; then
        short_message="PASS"
+       ext="pass"
+       if [ -n "$IGNORED" ] ; then short_message="PASS/WARN"; ext="warn"; fi
        ( \
            echo "Successfully built and tested" ; \
            echo "see build results at        $WEBBASE_URL" ; \
            echo "including full build log at $WEBBASE_URL/log.txt" ; \
            echo "and complete test logs at   $WEBBASE_URL/testlogs" ; \
-           ) | webpublish_cp_stdin_to_file $WEBBASE.pass
+           [ -n "$IGNORED" ] && echo "WARNING: some tests steps failed but were ignored - see trace file" ; \
+           ) | webpublish_cp_stdin_to_file $WEBBASE.$ext
        webpublish rm -f $WEBBASE.pkg-ok $WEBBASE.ko
     else
        short_message="PKGOK"
@@ -203,15 +205,19 @@ function success () {
            ) | webpublish_cp_stdin_to_file $WEBBASE.pkg-ok
        webpublish rm -f $WEBBASE.ko
     fi
-    if [ -n "$MAILTO" ] ; then
+    BUILD_END=$(date +'%H:%M')
+    BUILD_END_S=$(date +'%s')
+    if [ -n "$MAILDEST" ] ; then
        ( \
            echo "Subject: $short_message ${BASE} ${MAIL_SUBJECT}" ; \
-           echo "To: $MAILTO" ; \
+           echo "To: $MAILDEST" ; \
            echo "$PLDISTRO ($BASE) build for $FCDISTRO completed on $(date)" ; \
            echo "see build results at        $WEBBASE_URL" ; \
            echo "including full build log at $WEBBASE_URL/log.txt" ; \
-            [ -n "$DO_TEST" ] && echo "and complete test logs at   $WEBBASE_URL/testlogs" ) \
-           | sendmail $MAILTO
+            [ -n "$DO_TEST" ] && echo "and complete test logs at   $WEBBASE_URL/testlogs" ; \
+           [ -n "$IGNORED" ] && echo "WARNING: some tests steps failed but were ignored - see trace file" ; \
+           echo "BUILD TIME: begin $BUILD_BEG -- end $BUILD_END -- duration $(pretty_duration $(($BUILD_END_S-$BUILD_BEG_S)))" ; \
+           ) | sendmail $MAILDEST
     fi
     # XXX For some reason, we haven't been getting this email for successful builds. If this sleep
     # doesn't fix the problem, I'll remove it -- Sapan.
@@ -219,6 +225,12 @@ function success () {
     exit 0
 }
 
+##############################
+# manage root / container contexts
+function in_root_context () {
+    rpm -q libvirt > /dev/null 
+}
+
 # run in the vm - do not manage success/failure, will be done from the root ctx
 function build () {
     set -x
@@ -237,7 +249,7 @@ function build () {
     MAKEVARS=("PLDISTRO=${PLDISTRO}" "${MAKEVARS[@]}")
     MAKEVARS=("PLDISTROTAGS=${PLDISTROTAGS}" "${MAKEVARS[@]}")
     MAKEVARS=("PERSONALITY=${PERSONALITY}" "${MAKEVARS[@]}")
-    MAKEVARS=("MAILTO=${MAILTO}" "${MAKEVARS[@]}")
+    MAKEVARS=("MAILDEST=${MAILDEST}" "${MAKEVARS[@]}")
     MAKEVARS=("WEBPATH=${WEBPATH}" "${MAKEVARS[@]}")
     MAKEVARS=("TESTBUILDURL=${TESTBUILDURL}" "${MAKEVARS[@]}")
     MAKEVARS=("WEBROOT=${WEBROOT}" "${MAKEVARS[@]}")
@@ -293,20 +305,30 @@ function run_log () {
     ssh -n ${testmaster_ssh} rm -rf ${testdir} ${testdir}.git
 
     # check it out in the build
-    $RUN_IN_DOMAIN $BASE make -C /build tests-module
+    virsh -c lxc:/// lxc-enter-namespace $BASE /bin/bash -c "make -C /build tests-module"
     
     # push it onto the testmaster - just the 'system' subdir is enough
     rsync --verbose --archive $(rootdir $BASE)/build/MODULES/tests/system/ ${testmaster_ssh}:${BASE}
     # toss the build in the bargain, so the tests don't need to mess with extracting it
     rsync --verbose --archive $(rootdir $BASE)/build/MODULES/build ${testmaster_ssh}:${BASE}/
 
-    # invoke test on testbox - pass url and build url - so the tests can use vtest-init-lxc.sh
+    # invoke test on testbox - pass url and build url - so the tests can use lbuild-initvm.sh
     run_log_env="-p $PERSONALITY -d $PLDISTRO -f $FCDISTRO"
 
-    # need to proceed despite of set -e
-    success=true
-    # passing the build_scm_url should not be needed anymore
-    ssh 2>&1 -n ${testmaster_ssh} ${testdir}/run_log --build ${BUILD_SCM_URL} --url ${url} $run_log_env $RUN_LOG_EXTRAS $VERBOSE --all || success=
+    # temporarily turn off set -e
+    set +e
+    trap - ERR INT
+    ssh 2>&1 ${testmaster_ssh} ${testdir}/run_log --build ${BUILD_SCM_URL} --url ${url} $run_log_env $RUN_LOG_EXTRAS $VERBOSE --all; retcod=$?
+
+    set -e
+    trap failure ERR INT
+    # interpret retcod of TestMain.py; 2 means there were ignored steps that failed
+    echo "retcod from run_log" $retcod
+    case $retcod in
+       0) success=true; IGNORED="" ;;
+       2) success=true; IGNORED=true ;;
+       *) success="";   IGNORED="" ;; 
+    esac
 
     # gather logs in the build vm
     mkdir -p $(rootdir $BASE)/build/testlogs
@@ -324,10 +346,6 @@ function run_log () {
     
 }
 
-function in_root_context () {
-    rpm -q libvirt > /dev/null 
-}
-
 # this part won't work if WEBHOST does not match the local host
 # would need to be made webpublish_* compliant
 # but do we really need this feature anyway ?
@@ -431,7 +449,7 @@ function usage () {
     echo " -f fcdistro - defaults to $DEFAULT_FCDISTRO"
     echo " -d pldistro - defaults to $DEFAULT_PLDISTRO"
     echo " -p personality - defaults to $DEFAULT_PERSONALITY"
-    echo " -m mailto - no default"
+    echo " -m mailto - defaults to $DEFAULT_MAILDEST"
     echo " -s build_scm_url - git URL where to fetch the build module - defaults to $DEFAULT_BUILD_SCM_URL"
     echo "    define GIT tag or branch name appending @tagname to url"
     echo " -t pldistrotags - defaults to \${PLDISTRO}-tags.mk"
@@ -442,6 +460,7 @@ function usage () {
     echo " -c testconfig - defaults to $DEFAULT_TESTCONFIG"
     echo " -y {pl,pg} - passed to run_log"
     echo " -e step - passed to run_log"
+    echo " -i step - passed to run_log"
     echo " -X : passes --lxc to run_log"
     echo " -S : passes --vs to run_log"
     echo " -x <run_log_args> - a hook to pass other arguments to run_log"
@@ -466,6 +485,7 @@ function usage () {
 function main () {
 
     set -e
+    trap failure ERR INT
 
     # parse arguments
     MAKEVARS=()
@@ -478,7 +498,7 @@ function main () {
     SIGNYUMREPO=""
 
     OPTS_ORIG=$@
-    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:" -- $@)
+    OPTS=$(getopt -o "f:d:p:m:s:t:b:o:c:y:e:i:XSx:w:W:r:M:Yg:u:K:SBTnv7i:P:h" -l "build-branch:" -- $@)
     if [ $? != 0 ]
     then
         usage
@@ -489,7 +509,7 @@ function main () {
            -f) FCDISTRO=$2; shift 2 ;;
            -d) PLDISTRO=$2; shift 2 ;;
            -p) PERSONALITY=$2; shift 2 ;;
-           -m) MAILTO=$2; shift 2 ;;
+           -m) MAILDEST=$2; shift 2 ;;
            -s) BUILD_SCM_URL=$2; shift 2 ;;
            -t) PLDISTROTAGS=$2; shift 2 ;;
            -b) BASE=$2; shift 2 ;;
@@ -500,6 +520,7 @@ function main () {
            -y) RUN_LOG_EXTRAS="$RUN_LOG_EXTRAS --rspec-style $2"; shift 2 ;;
            # -e foo -> run_log -e foo
            -e) RUN_LOG_EXTRAS="$RUN_LOG_EXTRAS --exclude $2"; shift 2 ;;
+           -i) RUN_LOG_EXTRAS="$RUN_LOG_EXTRAS --ignore $2"; shift 2 ;;
            # -X -> run_log --lxc
            -X) RUN_LOG_EXTRAS="$RUN_LOG_EXTRAS --lxc"; shift;;
            # -S -> run_log --vs
@@ -546,6 +567,7 @@ function main () {
     [ -z "$FCDISTRO" ] && FCDISTRO=$DEFAULT_FCDISTRO
     [ -z "$PLDISTRO" ] && PLDISTRO=$DEFAULT_PLDISTRO
     [ -z "$PERSONALITY" ] && PERSONALITY=$DEFAULT_PERSONALITY
+    [ -z "$MAILDEST" ] && MAILDEST=$(echo $DEFAULT_MAILDEST | sed -e 's, at ,@,')
     [ -z "$PLDISTROTAGS" ] && PLDISTROTAGS="${PLDISTRO}-tags.mk"
     [ -z "$BASE" ] && BASE="$DEFAULT_BASE"
     [ -z "$WEBPATH" ] && WEBPATH="$DEFAULT_WEBPATH"
@@ -557,7 +579,7 @@ function main () {
     [ -z "$TESTCONFIG" ] && TESTCONFIG="$DEFAULT_TESTCONFIG"
     [ -z "$TESTMASTER" ] && TESTMASTER="$DEFAULT_TESTMASTER"
 
-    [ -n "$DRY_RUN" ] && MAILTO=""
+    [ -n "$DRY_RUN" ] && MAILDEST=""
 
     # elaborate the extra args to be passed to run_log
     for config in ${TESTCONFIG} ; do
@@ -620,24 +642,24 @@ function main () {
            set -x
            echo "XXXXXXXXXX $COMMAND: using existing vm $BASE" $(date)
            # start in case e.g. we just rebooted
-           virsh --connect lxc:/// start ${BASE} || :
+           virsh -c lxc:/// start ${BASE} || :
            # retrieve environment from the previous run
-           FCDISTRO=$($RUN_IN_DOMAIN ${BASE} /build/getdistroname.sh)
-           BUILD_SCM_URL=$($RUN_IN_DOMAIN ${BASE} -- make --no-print-directory -C /build stage1=skip +build-GITPATH)
+           FCDISTRO=$(virsh -c lxc:/// lxc-enter-namespace ${BASE} /build/getdistroname.sh)
+           BUILD_SCM_URL=$(virsh -c lxc:/// lxc-enter-namespace ${BASE} /bin/bash -c "make --no-print-directory -C /build stage1=skip +build-GITPATH")
            # for efficiency, crop everything in one make run
            tmp=/tmp/${BASE}-env.sh
-           $RUN_IN_DOMAIN ${BASE} -- make --no-print-directory -C /build stage1=skip \
-               ++PLDISTRO ++PLDISTROTAGS ++PERSONALITY ++MAILTO ++WEBPATH ++TESTBUILDURL ++WEBROOT > $tmp
+           virsh -c lxc:/// lxc-enter-namespace ${BASE} /bin/bash -c "make --no-print-directory -C /build stage1=skip \
+               ++PLDISTRO ++PLDISTROTAGS ++PERSONALITY ++MAILDEST ++WEBPATH ++TESTBUILDURL ++WEBROOT" > $tmp
            . $tmp
            rm -f $tmp
            # update build
            [ -n "$SSH_KEY" ] && setupssh ${BASE} ${SSH_KEY}
-           $RUN_IN_DOMAIN $BASE -- -c "cd /build; git pull; make tests-clean"
+           virsh -c lxc:/// lxc-enter-namespace $BASE /bin/bash -c "cd /build; git pull; make tests-clean"
            # make sure we refresh the tests place in case it has changed
            rm -f /build/MODULES/tests
            options=(${options[@]} -d $PLDISTRO -t $PLDISTROTAGS -s $BUILD_SCM_URL)
            [ -n "$PERSONALITY" ] && options=(${options[@]} -p $PERSONALITY)
-           [ -n "$MAILTO" ] && options=(${options[@]} -m $MAILTO)
+           [ -n "$MAILDEST" ] && options=(${options[@]} -m $MAILDEST)
            [ -n "$WEBPATH" ] && options=(${options[@]} -w $WEBPATH)
            [ -n "$TESTBUILDURL" ] && options=(${options[@]} -W $TESTBUILDURL)
            [ -n "$WEBROOT" ] && options=(${options[@]} -r $WEBROOT)
@@ -669,7 +691,10 @@ function main () {
            GIT_REPO=$(echo $BUILD_SCM_URL | cut -d@ -f1)
            GIT_TAG=$(echo $BUILD_SCM_URL | cut -s -d@ -f2)
            GIT_TAG=${GIT_TAG:-master}
-           mkdir -p $tmpdir; git archive --remote=$GIT_REPO $GIT_TAG | tar -C $tmpdir -xf -
+           mkdir -p $tmpdir 
+            ( git archive --remote=$GIT_REPO $GIT_TAG | tar -C $tmpdir -xf -) || \
+               ( echo "==================== git archive FAILED, trying git clone instead" ; \
+                 git clone $GIT_REPO $tmpdir && cd $tmpdir && git checkout $GIT_TAG && rm -rf .git)
 
             # Create lxc vm
            cd $tmpdir
@@ -679,8 +704,7 @@ function main () {
            rm -rf $tmpdir
            # Extract build again - in the vm
            [ -n "$SSH_KEY" ] && setupssh ${BASE} ${SSH_KEY}
-           # xxx not working as of now - waiting for Sapan to look into this
-           $RUN_IN_DOMAIN $BASE -- "git clone $GIT_REPO /build; cd /build; git checkout $GIT_TAG"
+           virsh -c lxc:/// lxc-enter-namespace $BASE /bin/bash -c "git clone $GIT_REPO /build; cd /build; git checkout $GIT_TAG"
        fi
        echo "XXXXXXXXXX $COMMAND: preparation of vm $BASE done" $(date)
 
@@ -712,12 +736,13 @@ function main () {
            cp $COMMANDPATH $(rootdir ${BASE})/build/
 
            # invoke this command in the vm for building (-T)
-           $RUN_IN_DOMAIN ${BASE} chmod +x /build/$COMMAND
-           $RUN_IN_DOMAIN ${BASE} -- /build/$COMMAND "${options[@]}" -b "${BASE}" "${MAKEVARS[@]}" "${MAKETARGETS[@]}"
+           virsh -c lxc:/// lxc-enter-namespace ${BASE} /bin/bash -c "chmod +x /build/$COMMAND"
+           virsh -c lxc:/// lxc-enter-namespace ${BASE} /build/$COMMAND "${options[@]}" -b "${BASE}" "${MAKEVARS[@]}" "${MAKETARGETS[@]}"
        fi
 
        # publish to the web so run_log can find them
        set +e
+       trap - ERR INT
        webpublish rm -rf $WEBPATH/$BASE 
        # guess if we've been doing any debian-related build
        if [ ! -f $(rootdir $BASE)/etc/debian_version  ] ; then
@@ -727,7 +752,7 @@ function main () {
        else
            # run scanpackages so we can use apt-get on this
            # (not needed on fedora b/c this is done by the regular build already)
-           $RUN_IN_DOMAIN $BASE -- -c "(cd /build ; dpkg-scanpackages DEBIAN/ | gzip -9c > Packages.gz)"
+           virsh -c lxc:/// lxc-enter-namespace $BASE /bin/bash -c "(cd /build ; dpkg-scanpackages DEBIAN/ | gzip -9c > Packages.gz)"
            webpublish mkdir -p $WEBPATH/$BASE/DEBIAN
            webpublish_rsync_files $WEBPATH/$BASE/DEBIAN/ $(rootdir $BASE)/build/DEBIAN/*.deb 
            webpublish_rsync_files $WEBPATH/$BASE/ $(rootdir $BASE)/build/Packages.gz
@@ -736,6 +761,7 @@ function main () {
        release=$(rootdir $BASE)/build/myplc-release
        [ -f $release ] && webpublish_rsync_files $WEBPATH/$BASE $release
        set -e
+       trap failure ERR INT
 
         # create yum repo and sign packages.
        if [ -n "$SIGNYUMREPO" ] ; then