#!/bin/bash # # This is designed to be called from cron DEBUG=0 FORCE=0 # You can set DEBUG_RELEASE in the environment for testing DEBUG_RELEASE="" VERBOSE=0 FAKE=0 UPGRADE_TO_TESTING=no UPGRADE_ANYWAY=no # Read defaults here so as not to clobber command-line args later [ -f /etc/default/debathena-auto-update ] && . /etc/default/debathena-auto-update while getopts "vfdt" opt; do case "$opt" in d) DEBUG=1 FAKE=1 UPGRADE_ANYWAY=yes ;; f) FORCE=1 UPGRADE_ANYWAY=yes ;; t) UPGRADE_TO_TESTING=yes ;; v) # Spam level VERBOSE=1 ;; \?) cat</dev/null 2>&1 else echo perl -i.bak -pe 's/^GRUB_DEFAULT=.*$/GRUB_DEFAULT=saved/' /etc/default/grub perl -i.bak -pe 's/^GRUB_DEFAULT=.*$/GRUB_DEFAULT=saved/' /etc/default/grub fi if [[ $? -ne 0 ]]; then complain "Setting DEFAULT in /etc/default/grub returned a non-zero exit status" exit 1 fi } function complain() { echo "ERROR: $*" if [ $DEBUG -ne 1 ]; then logger -t "athena-auto-upgrade" -p user.notice "$*" fi } function runcmd() { CMD=$1 if [[ $FAKE == 1 ]]; then echo $CMD elif [[ $VERBOSE == 0 ]]; then $CMD > /dev/null 2>&1 else echo $CMD $CMD fi if [[ $? -ne 0 ]]; then complain "$CMD returned a non-zero exit status" exit 1 fi } function debug() { [ $DEBUG -eq 1 ] && echo "DEBUG: $*" } function maybe_quit() { if [ $FORCE -eq 1 ]; then echo "Would normally quit here, but you passed -f, so we keep going..." else echo "Ending upgrade at $(date)" exit 0 fi } if [ $DEBUG -ne 1 ]; then # Redirect output to a log file # Unless we're in debug mode exec >>/var/log/athena-upgrade 2>&1 fi echo echo "Starting upgrade at $(date)" # Not supported on Debian if [ "$(lsb_release -si)" != "Ubuntu" ]; then debug "Only supported on Ubuntu.". exit 0 fi # Skip other sanity checks in debug mode if [ $DEBUG -ne 1 ]; then # Only run this on cluster if [ "$(machtype -L)" != "debathena-cluster" ] && \ [ "$UPGRADE_ANYWAY" != "yes" ]; then debug "Not a cluster machine and UPGRADE_ANYWAY != yes.". exit 0 fi # Bail if someone is logged in (stolen from auto-update) ttys=$(w -h -s | awk '{print $2}') for tty in $ttys; do pids=$(ps --no-heading -j -t "$tty" 2>/dev/null \ | awk '($1 == $3) {print $1}') if [ -n "$pids" ]; then debug "Users logged in, won't continue." debug "PIDs: $pids" maybe_quit fi done # screen processes count as logins. if pgrep '^screen' > /dev/null; then debug "Screen processes found, won't continue." maybe_quit fi fi export CLUSTERINFO=$(getcluster -b $(lsb_release -sr)) [ $? != 0 ] && complain "Failed to get clusterinfo" && exit 1 eval $CLUSTERINFO REINSTALL=no UBUNTU_RELEASE= if [ ! -z "$NEW_PRODUCTION_RELEASE" ]; then debug "Taking new production release: $NEW_PRODUCTION_RELEASE" NEWCLUSTER=`getcluster -b $NEW_PRODUCTION_RELEASE` [ $? != 0 ] && complain "Failed to get clusterinfo" && exit 1 # This will set UBUNTU_RELEASE eval $NEWCLUSTER elif [ ! -z "$NEW_TESTING_RELEASE" ] && \ [ "$UPGRADE_TO_TESTING" = "yes" ]; then debug "Taking new testing release: $NEW_TESTING_RELEASE" NEWCLUSTER=`getcluster -b $NEW_TESTING_RELEASE` [ $? != 0 ] && complain "Failed to get clusterinfo" && exit 1 # This will set UBUNTU_RELEASE eval $NEWCLUSTER elif [ ! -z "$REINSTALL_AT" ] && echo $REINSTALL_AT | grep -qx '[0-9]*'; then debug "Found REINSTALL_AT = $REINSTALL_AT ..." installtime=$(stat -c "%Y" /var/log/athena-install.log 2>/dev/null) debug "Machine was last installed at $installtime" if [ -z "$installtime" ]; then echo "W: Could not stat /var/log/athena-install.log, forcing reinstall." installtime=0 fi if [ $installtime -lt $REINSTALL_AT ]; then debug "$installtime less than $REINSTALL_AT, so reinstalling" REINSTALL=yes UBUNTU_RELEASE="$(lsb_release -sc)" fi fi if [ "$(lsb_release -sc)" = "$UBUNTU_RELEASE" ] && \ [ "$REINSTALL" = "no" ]; then complain "Tried to upgrade to already running release; shouldn't happen" exit 1 fi if [ -z "$UBUNTU_RELEASE" ]; then if [ -n "$DEBUG_RELEASE" ]; then UBUNTU_RELEASE=$DEBUG_RELEASE else echo "No new releases found." exit 0 fi fi echo "Found $UBUNTU_RELEASE, starting upgrade" # That's a space and then a tab inside the brackets if egrep -q '^flags[ ].* lm( |$)' /proc/cpuinfo; then arch=amd64 else arch=i386 fi debug "Arch: $arch" IPADDR= NETMASK= eval `ifconfig eth0 |perl -ne'/^\s+inet addr:([\d\.]+).*? Mask:([\d\.]+)/ && print "IPADDR=$1 NETMASK=$2"'` if [ -z "$IPADDR" ]; then complain "Couldn't get IPADDR from ifconfig!" exit 0 fi GATEWAY=`route -n | awk '/^0\.0\.0\.0/ { print $2 }'` debug "Using IPADDR=$IPADDR" debug "Using NETMASK=$NETMASK" debug "Using GATEWAY=$GATEWAY" if [ -n "$NETMASK" ] && [ -n "$GATEWAY" ]; then knetinfo="netcfg/get_hostname=$(hostname) \ netcfg/disable_autoconfig=true \ netcfg/get_domain=mit.edu \ netcfg/get_nameservers=\"18.72.0.3 18.71.0.151 18.70.0.160\"\ netcfg/get_ipaddress=$IPADDR \ netcfg/get_netmask=$NETMASK \ netcfg/get_gateway=$GATEWAY \ netcfg/confirm_static=true" else knetinfo="netcfg/dhcp_timeout=60 netcfg/get_hostname=$(hostname)" fi runcmd "rm -rf /auto-upgrade" runcmd "mkdir -p /auto-upgrade" runcmd "wget http://debathena.mit.edu/net-install/${UBUNTU_RELEASE}/${arch}/initrd.gz -O /auto-upgrade/initrd.gz" runcmd "wget http://debathena.mit.edu/net-install/${UBUNTU_RELEASE}/${arch}/linux -O /auto-upgrade/linux" # This is just the guts of the hackboot script: dkargs="DEBCONF_DEBUG=5" kargs="$knetinfo locale=en_US \ keyboard-configuration/layoutcode=us \ quiet \ panic=5 \ interface=auto \ url=http://18.9.60.73/installer/${UBUNTU_RELEASE}/debathena.preseed \ da/pxe=cluster --" debug "USING kargs=$kargs" if [ $(echo $kargs | wc -c) -ge 512 ]; then complain "kargs exceeds 512 bytes. That's not good." exit 1 fi tmpfile="/tmp/$(basename $0).$RANDOM.tmp" cat >"$tmpfile"<&2 cat<