Home | History | Annotate | Download | only in gnome-2-24
      1 #!/bin/bash
      2 
      3 # Sample usage in crontab:
      4 # Run, Mon-Fri at 1:30am. Add to build user's crontab.
      5 #   30 1 * * 1-5 . /jds/cbe/bin/env.sh; I_KNOW_WHAT_IM_DOING=yes myEnv=nightly-beijing.env $0
      6 #
      7 # The same with a jail. Add to root's crontab. Example assumes 'gbuild' is the
      8 # build user
      9 #   30 1 * * 1-5 /usr/sbin/chroot /path/to/jail/root /usr/bin/su - gbuild -c ". /jds/cbe/bin/env.sh; I_KNOW_WHAT_IM_DOING=yes myEnv=nightly-beijing.env $0"
     10 #
     11 # Or - you may choose to only build a particular package
     12 #   30 1 * * 1-5 . /jds/cbe/bin/env.sh; thisSpec=SUNWTiff.spec I_KNOW_WHAT_IM_DOING=yes myEnv=nightly-beijing.env $0
     13 #
     14 # History:
     15 # --------
     16 #
     17 # Damien Carbery:
     18 #       cron-script.sh, original script
     19 #
     20 # Alexandre Berman:
     21 #       based on the original script, created cron-nightly.sh
     22 #       added/changed features:
     23 #         - added sub-routines for easy debugging, clarity
     24 #         - changed to SVN (original script used cvs)
     25 #         - took all site-dependent variable declarations out of the script and adopted it to use env file
     26 #         - added support for building individual components (must specify spec file), useful for debugging and others..
     27 #         - added more verbosity to the script for debugging and clarity
     28 
     29 if [ -r $myEnv ]; then
     30    . $myEnv # setup our env
     31 else
     32    echo "-- ENV is NOT defined ! Exiting..."
     33    exit 1
     34 fi
     35 
     36 # host to rcp the rpms to
     37 RHOST="$RHOST"
     38 # user to rcp as (has to have root@<this host> in it's .rhosts file)
     39 RUSER="$RUSER"
     40 # other vars
     41 TEMP_DIR="$TEMP_DIR"
     42 SPECDIR="$SPECDIR"
     43 BUILD_BASE="$BUILD_BASE"
     44 PRODNAME="$PRODNAME"
     45 # directory to copy rpms/srpms to on the remote host
     46 RPMSDIR="$RPMSDIR"
     47 LOCKFILE="$RPMSDIR/.build.lock"
     48 # reply-to/to address to send the build log as/to
     49 EMAIL_ERRORS_TO="$EMAIL_ERRORS_TO"
     50 EMAIL_NOTIFICATION="$EMAIL_NOTIFICATION"
     51 # document root of the web server
     52 WEBROOT="$WEBROOT"
     53 LOGDIR_BASE_URL="$LOGDIR_BASE_URL"
     54 # tarballsdir
     55 TARBALLSDIR="$TARBALLSDIR"
     56 # subdir to keep logs and reports on the webserver
     57 WEBDIR="$WEBDIR"
     58 
     59 # date format appended to the Release tag in the spec files
     60 # (passed to the date command on the cmd line)
     61 RELEASE_DATE_FMT="%y%m%d"
     62 # date format used for naming the directories
     63 DIR_DATE_FMT="%Y-%m-%d"
     64 RELEASE_DATE=`date +$RELEASE_DATE_FMT`
     65 DIR_DATE=`date +$DIR_DATE_FMT`
     66 LOGDIR="$WEBDIR/$DIR_DATE"
     67 
     68 # ------------ nothing to configure below this line --------------
     69 
     70 if [ "x$I_KNOW_WHAT_IM_DOING" != xyes ]; then
     71     echo " ,---------------------------------------------------------------."
     72     echo "| This script is intended to be run from cron for producing      |"
     73     echo "| official nightly builds (Beijing site).                        |"
     74     echo "| It will mail responsible engineers                             |"
     75     echo "| if any build failure occurs, sends build reports to            |"
     76     echo "| RE and update web pages.                                       |"
     77     echo "|"
     78     echo "| Don't run it unless you know what you are doing. Thanks."
     79     echo "|"
     80     echo "| Mail jdsbj-re (at] sun.com if you need more info."
     81     echo ".................................................................+"
     82     exit 1
     83 fi
     84 
     85 MYNAME="$0"
     86 MYDIR=$(cd `dirname $0`; pwd)
     87 shortName=`echo $MYNAME | sed -e 's|^.*\/||g'`
     88 
     89 # remove temporary files on exit
     90 clean_up () {
     91   case "$MYNAME" in
     92   /tmp/$shortName.copy.* )
     93         rm -f $MYNAME
     94         ;;
     95   esac
     96   exit
     97 }
     98 
     99 trap clean_up HUP INT TERM QUIT EXIT
    100 
    101 # make a copy of the cron script in /tmp and execute that in order to
    102 # avoid disasters caused by cvs update.
    103 case "$MYNAME" in
    104     /tmp/*.copy.* )
    105         ;;
    106     *)
    107         cp $MYNAME /tmp/$shortName.copy.$$
    108         chmod 755 /tmp/$shortName.copy.$$
    109         cd /tmp
    110         exec /tmp/$shortName.copy.$$ "$MYDIR"
    111         ;;
    112 esac
    113 
    114 fatal_error () {
    115   echo "ERROR: $*"
    116   exit 1
    117 }
    118 
    119 # prepare TEMP_DIR and repository - using SVN now
    120 prep_repository() {
    121    if [ -d $SPECDIR ]; then
    122       cd $SPECDIR || fatal_error "$SPECDIR not found"
    123       echo "-- updating SVN rep ..."
    124       #revert any local changes
    125       svn revert -R .
    126 
    127       svn -q up > /dev/null 2>&1 || fatal_error "SVN update failed"
    128    else
    129       echo "-- checking out fresh copy of Spec files from SVN rep ..."
    130       rm -rf $TEMP_DIR
    131       mkdir $TEMP_DIR; cd $TEMP_DIR
    132       svn -q checkout svn://dtsvn.ireland.sun.com/sgnome/svn/repos/jds-spec-files/trunk \
    133                                  > /dev/null 2>&1 || fatal_error "SVN checkout failed"
    134    fi
    135    # if the script changed during repository update, restart with the updated script
    136    if ! /usr/bin/cmp -s $SPECDIR/$shortName $MYNAME; then exec $SPECDIR/$shortName; fi
    137 }
    138 
    139 
    140 # uninstall all pkgs left behind by a previous build
    141 do_uninst() {
    142    echo "-- uninstalling packages..."
    143    pkgtool uninstall-pkgs --with-l10n --with-tjds $thisSpec >/dev/null
    144    # remove-gnome will now remove anything left from uninstall-pkgs in case
    145    # of a packaging change for example
    146    $SPECDIR/scripts/remove-gnome --version jds -q -f --no_extras > /dev/null 2>&1
    147 }
    148 
    149 do_clean_pkgs() {
    150    rm -rf $BUILD_BASE/PKGS/*
    151    rm -rf $BUILD_BASE/SPKGS/*
    152    rm -rf $BUILD_BASE/BUILD/*
    153    rm -rf /var/tmp/*-build
    154 }
    155 
    156 # if the log directory exists, open a new one with numbered suffix
    157 do_log_dir() {
    158    echo "-- setting up logs..."
    159    NEW_LOGDIR=$LOGDIR
    160    N=1
    161    while [ -d $WEBROOT/$NEW_LOGDIR ]; do
    162        NEW_LOGDIR=$LOGDIR.$N
    163        N=`expr $N + 1`
    164        echo "-- LOGDIR exists, changing to: $NEW_LOGDIR"
    165    done
    166    LOGDIR=$NEW_LOGDIR
    167    echo "-- LOGDIR:     $WEBROOT/$LOGDIR"
    168    echo "-- LOGDIR URL: $LOGDIR_BASE_URL/$LOGDIR"
    169    mkdir -p $WEBROOT/$LOGDIR || exit 5
    170 }
    171 
    172 # start the build
    173 do_build() {
    174    echo "-- build started, using log: /tmp/build.log.$$ ..."
    175    cd $SPECDIR || fatal_error "$SPECDIR not found"
    176    echo '' | rsh $RHOST -l $RUSER "mkdir -p $RPMSDIR; touch $LOCKFILE"
    177    pkgtool -v --nightly --date "$RELEASE_DATE" build $thisSpec \
    178         --logdir=$WEBROOT/$LOGDIR \
    179         --summary-log=$WEBROOT/$LOGDIR.html \
    180         --logdir-url=$LOGDIR_BASE_URL/$LOGDIR \
    181 	--mail-errors-to=$EMAIL_ERRORS_TO \
    182         --prodname="${PRODNAME}/s${OSrel}${OSarch}" \
    183         --live --with-l10n --with-tjds \
    184 	--norc \
    185 	--tarballdirs=$TARBALLSDIR \
    186         --define "nightly 1" \
    187         --summary-title="${PRODNAME} S${OSrel}/${OSarch_full} Nightly Build Report `date +'%d %B %Y'`" \
    188          > /tmp/build.log.$$ 2>&1
    189    #     --rpm-url=file:///net/allstar.prc$RPMSDIR/all_pkgs \
    190    # the number of failed pkgs is returned
    191    FAILED=$?; export FAILED
    192 }
    193 
    194 # choose what to build ?
    195 # coices are: everything or specific spec file
    196 choose_build() {
    197    if [ "x$thisSpec" = "x" ]; then
    198       # no spec file was chosen, build everything
    199       thisSpec='*.spec closed/*.spec'; export thisSpec
    200       echo "-- building following components: $thisSpec"
    201    else
    202       # verify chosen spec file
    203       if [ ! -f $SPECDIR/$thisSpec ]; then
    204          fatal_error "chosen spec file ($thisSpec) does not exist in spec dir ($SPECDIR)"
    205       fi
    206       echo "-- building following components: $thisSpec"
    207    fi
    208 }
    209 
    210 # rotate rpms dir
    211 do_rotate_rpms() {
    212    echo '' | rsh $RHOST -l $RUSER "rm -rf $RPMSDIR.prev; mv $RPMSDIR $RPMSDIR.prev; mkdir -p $RPMSDIR"
    213 }
    214 
    215 # make dist
    216 do_make_dist() {
    217    echo "-- making dist ..."
    218    /sgnome/tools/re-scripts/jds-build/make-jds-dist /jds/packages/PKGS /jds/dist nightly- > /dev/null 2>&1
    219    echo '' | rcp -r /jds/dist/nightly-/${OSarch_full}/* /jds/dist/nightly-/${OSarch_full}/.??* ${RUSER}@${RHOST}:$RPMSDIR
    220    echo '' | rsh $RHOST -l $RUSER "chmod a+x $RPMSDIR/install-jds"
    221    echo '' | rsh $RHOST -l $RUSER "mkdir -p $RPMSDIR/all_pkgs && cd $RPMSDIR/all_pkgs && ln -s ../*/*.tar.gz ."
    222    rm -rf /jds/dist/nightly-
    223 }
    224 
    225 # web reports
    226 do_web_reports() {
    227    ALL_REPORTS=$WEBROOT/$WEBDIR/all_reports.html
    228    echo "-- creating main report in: $ALL_REPORTS"
    229    touch $ALL_REPORTS
    230    cp $ALL_REPORTS $ALL_REPORTS.old
    231    export ALL_REPORTS
    232    # update web page
    233    ( echo "<A HREF=$LOGDIR_BASE_URL/$LOGDIR.html>$DIR_DATE</A> $FAILED package(s) failed<BR>"; \
    234                                                            cat $ALL_REPORTS.old ) > $ALL_REPORTS
    235 }
    236 
    237 # send warnings, errors and summary in email
    238 do_email() {
    239    grep -v '^INFO:' /tmp/build.log.$$ | \
    240     mailx -s "${PRODNAME} S${OSrel} ${OSarch_full} nightly build: $FAILED pkgs failed" $EMAIL_NOTIFICATION
    241 }
    242 
    243 # final cleanup
    244 do_finally() {
    245    rm -f /tmp/build.log.$$
    246    echo '' | rsh $RHOST -l $RUSER "rm $LOCKFILE"
    247 }
    248 
    249 # let's do it
    250 choose_build
    251 prep_repository
    252 do_uninst
    253 do_clean_pkgs
    254 do_log_dir
    255 do_build
    256 do_rotate_rpms
    257 do_make_dist
    258 do_web_reports
    259 do_email
    260 do_finally
    261 
    262 exit 0
    263