source: trunk/debathena/config/auto-update/debian/athena-auto-upgrade @ 25326

Revision 25326, 5.3 KB checked in by jdreed, 13 years ago (diff)
In auto-update: * Add some debugging and options to athena-auto-upgrade
Line 
1#!/bin/bash
2#
3# This is designed to be called from cron
4
5debug() {
6    [ $DEBUG -eq 1 ] && echo "DEBUG: $*"
7}
8
9complain() {
10    if [ $DEBUG -eq 1 ]; then
11        echo "ERROR: $*"
12    else
13        logger -t "athena-auto-upgrade" -p user.notice "$*"
14    fi
15}
16
17maybe_quit() {
18    if [ $FORCE -eq 1 ]; then
19        echo "Would normally quit here, but you passed -f, so we keep going..."
20    else
21        exit 0
22    fi
23}
24
25DEBUG=0
26MAYBE=""
27FORCE=0
28
29UPGRADE_TO_TESTING=no
30UPGRADE_ANYWAY=no
31[ -f /etc/default/debathena-auto-update ] && . /etc/default/debathena-auto-update
32
33while getopts "fdta" opt; do
34    case "$opt" in
35        d)
36            DEBUG=1
37            MAYBE=echo
38            UPGRADE_ANYWAY=yes
39            ;;
40        f)
41            FORCE=1
42            ;;
43        t)
44            UPGRADE_TO_TESTING=yes
45            ;;
46        \?)
47            echo "Usage: $0 [-d] [-t] [-f]"
48            ;;
49    esac
50done
51
52if [ $DEBUG -eq 1 ] && [ $FORCE -eq 1 ]; then
53    echo "ERROR: -d and -f are mutually exclusive."
54    exit 2
55fi
56if [ $FORCE -eq 1 ]; then
57    echo "Using -f is a terrible idea.  Press Ctrl-C to reconsider and"
58    echo "save your machine, or Enter to continue."
59    read r
60fi
61
62if [ $DEBUG -ne 1 ]; then
63    # Redirect output to a log file
64    # Unless we're in debug mode
65    exec >>/var/log/athena-upgrade 2>&1
66fi
67
68echo "Starting upgrade at $(date)"
69
70# Not supported on Debian
71if [ "$(lsb_release -si)" != "Ubuntu" ]; then
72    debug "Only supported on Ubuntu.".
73    exit 0
74fi
75
76# Skip other sanity checks in debug mode
77if [ $DEBUG -ne 1 ]; then
78    # Only run this on cluster
79    if [ "$(machtype -L)" != "debathena-cluster" ] && \
80       [ "$UPGRADE_ANYWAY" != "yes" ]; then
81        debug "Not a cluster machine and UPGRADE_ANYWAY != yes.".
82        exit 0
83    fi
84   
85    # Bail if someone is logged in (stolen from auto-update)
86    ttys=$(w -h -s | awk '{print $2}')
87    for tty in $ttys; do
88        pids=$(ps --no-heading -j -t "$tty" 2>/dev/null \
89            | awk '($1 == $3) {print $1}')
90        if [ -n "$pids" ]; then
91            debug "Users logged in, won't continue."
92            debug "PIDs: $pids"
93            maybe_quit
94        fi
95    done
96    # screen processes count as logins.
97    if pgrep '^screen' > /dev/null; then
98        debug "Screen processes found, won't continue."
99        maybe_quit
100    fi
101fi
102
103CLUSTERINFO=`getcluster -b $(lsb_release -sr)`
104[ $? != 0 ] && complain "Failed to get clusterinfo" && exit 1
105eval $CLUSTERINFO
106if [ ! -z "$NEW_PRODUCTION_RELEASE" ]; then
107    debug "Taking new production release: $NEW_PRODUCTION_RELEASE"
108    NEWCLUSTER=`getcluster -b $NEW_PRODUCTION_RELEASE`
109    [ $? != 0 ] && complain "Failed to get clusterinfo" && exit 1
110    eval $NEWCLUSTER
111elif [ ! -z "$NEW_TESTING_RELEASE" ] && \
112    [ "$UPGRADE_TO_TESTING" = "yes" ]; then
113    debug "Taking new testing release: $NEW_TESTING_RELEASE"
114    NEWCLUSTER=`getcluster -b $NEW_TESTING_RELEASE`
115    [ $? != 0 ] && complain "Failed to get clusterinfo" && exit 1
116    eval $NEWCLUSTER
117else
118    debug "No new releases."
119    exit 0
120fi
121if [ "$(lsb_release -sc)" = "$UBUNTU_RELEASE" ]; then
122    complain "Tried to upgrade to already running release; shouldn't happen"
123    exit 1
124fi
125debug "OK, trying to upgrade to $UBUNTU_RELEASE"
126
127# That's a space and then a tab inside the brackets
128if egrep -q '^flags[    ].* lm( |$)' /proc/cpuinfo; then
129    arch=amd64
130else
131    arch=i386
132fi
133debug "Arch: $arch"
134IPADDR=
135NETMASK=
136eval `ifconfig eth0 |perl -ne'/^\s+inet addr:([\d\.]+).*? Mask:([\d\.]+)/ && print "IPADDR=$1 NETMASK=$2"'`
137if [ -z "$IPADDR" ]; then
138    complain "Couldn't get IPADDR from ifconfig!"
139    exit 0
140fi
141GATEWAY=`route -n | awk '/^0\.0\.0\.0/ { print $2 }'`
142debug "Using IPADDR=$IPADDR"
143debug "Using NETMASK=$NETMASK"
144debug "Using GATEWAY=$GATEWAY"
145if [ -n "$NETMASK" ] && [ -n "$GATEWAY" ]; then
146  knetinfo="netcfg/get_hostname=$(hostname) \
147            netcfg/disable_dhcp=true \
148            netcfg/get_domain=mit.edu \
149            netcfg/get_nameservers=\"18.72.0.3 18.70.0.160\"\
150            netcfg/get_ipaddress=$IPADDR \
151            netcfg/get_netmask=$NETMASK \
152            netcfg/get_gateway=$GATEWAY \
153            netcfg/confirm_static=true"
154else
155  knetinfo="netcfg/dhcp_timeout=60 netcfg/get_hostname=$(hostname)"
156fi
157
158$MAYBE mkdir /h
159$MAYBE cd /h
160$MAYBE wget -N http://debathena.mit.edu/net-install/${UBUNTU_RELEASE}/${arch}/initrd.gz
161$MAYBE wget -N http://debathena.mit.edu/net-install/${UBUNTU_RELEASE}/${arch}/linux
162case "$UBUNTU_RELEASE" in
163    natty)
164        # Sigh
165        kbdcode="keyboard-configuration/layoutcode=us"
166        ;;
167    *)
168        kbdcode="console-setup/layoutcode=us"
169        ;;
170esac
171# This is just the guts of the hackboot script:
172dkargs="DEBCONF_DEBUG=5"
173kargs="$knetinfo locale=en_US $kbdcode \
174       interface=auto \
175       url=http://18.9.60.73/installer/${UBUNTU_RELEASE}/debathena.preseed \
176       debathena/pxetype=cluster --"
177if [ $(echo $kargs | wc -c) -ge 512 ]; then
178  complain "kargs exceeds 512 bytes.  That's not good."
179fi
180debug "Would execute kexec with --append=\"$dkargs $kargs\""
181if [ $DEBUG -eq 1 ]; then
182    exit 0
183fi
184# Don't kexec -e here, because modern Ubuntu is unable to kexec while
185# X is running.  Instead, kexec -l and let the init script take care of.
186# Until Oneiric, when this will probably stop working if kexec-tools hasn't
187# been Upstartified.
188/sbin/kexec -l linux --append="$dkargs $kargs" --initrd=initrd.gz
189if [ "`cat /sys/kernel/kexec_loaded`" != "1" ]; then
190  echo "Hrm, looks like kexec -l didn't work.  This upgrade might fail."
191fi
192reboot
193exit 0
Note: See TracBrowser for help on using the repository browser.