Home | History | Annotate | Download | only in contrib
      1 #!/bin/bash
      2 # Copyright (c) 2015 The Chromium OS Authors. All rights reserved.
      3 # Use of this source code is governed by a BSD-style license that can be
      4 # found in the LICENSE file.
      5 
      6 SCRIPT_DIR=$(dirname $(readlink -f $0))
      7 cd ${SCRIPT_DIR}/..
      8 
      9 LOGDIR=logs/stable-version
     10 OPTIONS=()
     11 
     12 # STATUS is part hack, part safety valve.  The cron job that invokes
     13 # this script may be installed and enabled on both a primary and backup
     14 # server.  We skip running on the backup, but really, the backup
     15 # shouldn't have invoked this script in the first place...
     16 #
     17 # If we're not invoked on an autotest server at all, we assume this is
     18 # for testing, and invoke a dry run instead.
     19 #
     20 SERVER_STATUS=$(cli/atest server list $(hostname) 2>&1 |
     21                   awk '/^Status *:/ { print $NF }')
     22 if [ "${SERVER_STATUS}" = "primary" ]; then
     23   mkdir -p ${LOGDIR}
     24   NOTIFY=(
     25     chromeos-infra-eng@grotations.appspotmail.com
     26   )
     27 else
     28   if [ -n "${SERVER_STATUS}" ]; then
     29     # must be backup
     30     exit 0
     31   fi
     32   OPTIONS=( --dry-run )
     33   NOTIFY=( ${LOGNAME}@google.com )
     34 fi
     35 
     36 # Redirect onto a log file.  For debug purposes, skip redirection if
     37 # there's a command line argument (we ignore what the argument is), or
     38 # if there's no log directory.
     39 #
     40 TAG=$(date '+%Y-%W')
     41 if [ $# -eq 0 -a -d ${LOGDIR} ]; then
     42     LOGFILE="update-${TAG}.log"
     43     exec >>${LOGDIR}/${LOGFILE} 2>&1
     44 fi
     45 
     46 trap 'rm -f ${TMPFILE}' EXIT
     47 TMPFILE=$(mktemp)
     48 
     49 date
     50 site_utils/assign_stable_images.py "${OPTIONS[@]}" | tee ${TMPFILE}
     51 echo
     52 
     53 # If we have a log directory, clean it up, and send e-mail notification.
     54 # The log files change name each week, so by throwing out all but the
     55 # most recent 14 files, we keep about 3 months of history, plus this
     56 # week's log.
     57 #
     58 if [ -d ${LOGDIR} ]; then
     59     SUBJECT="Stable version update summary ${TAG}"
     60     site_utils/gmail_lib.py -s "${SUBJECT}" "${NOTIFY[@]}" <${TMPFILE}
     61     rm -f $(ls -r ${LOGDIR}/update-*.log | sed '1,14 d')
     62 fi
     63