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

Revision 25585, 7.5 KB checked in by jdreed, 12 years ago (diff)
In auto-update: * Case blocks must end with a double semicolon
Line 
1#!/bin/bash
2#
3# This is designed to be called from cron
4
5DEBUG=0
6FORCE=0
7# You can set DEBUG_RELEASE in the environment for testing
8DEBUG_RELEASE=""
9VERBOSE=0
10FAKE=0
11UPGRADE_TO_TESTING=no
12UPGRADE_ANYWAY=no
13
14# Read defaults here so as not to clobber command-line args later
15[ -f /etc/default/debathena-auto-update ] && . /etc/default/debathena-auto-update
16
17while getopts "vfdt" opt; do
18    case "$opt" in
19        d)
20            DEBUG=1
21            FAKE=1
22            UPGRADE_ANYWAY=yes
23            ;;
24        f)
25            FORCE=1
26            UPGRADE_ANYWAY=yes
27            ;;
28        t)
29            UPGRADE_TO_TESTING=yes
30            ;;
31        v) # Spam level
32            VERBOSE=1
33            ;;
34        \?)
35            cat<<END
36    Usage: $0 [OPTION]
37
38            -d         debug mode, bypasses all checks, only prints
39            -t         upgrade to testing
40            -f         bypasses login checks
41            -v         echo the cmds that are run
42
43END
44           
45            ;;
46    esac
47done
48
49
50if [ $DEBUG -eq 1 ] && [ $FORCE -eq 1 ]; then
51    echo "ERROR: -d and -f are mutually exclusive."
52    exit 2
53fi
54if [ $FORCE -eq 1 ]; then
55    echo "Using -f is a terrible idea.  Press Ctrl-C to reconsider and"
56    echo "save your machine, or Enter to continue."
57    read r
58fi
59
60function pie_grub() {
61    # change DEFAULT in /etc/grub to SAVED
62    if [[ $FAKE == 1 ]]; then
63        echo perl -i.bak -pe 's/^GRUB_DEFAULT=.*$/GRUB_DEFAULT=saved/' /etc/default/grub
64    elif [[ $VERBOSE == 0 ]]; then
65        perl -i.bak -pe 's/^GRUB_DEFAULT=.*$/GRUB_DEFAULT=saved/' /etc/default/grub >/dev/null 2>&1
66    else
67        echo perl -i.bak -pe 's/^GRUB_DEFAULT=.*$/GRUB_DEFAULT=saved/' /etc/default/grub
68        perl -i.bak -pe 's/^GRUB_DEFAULT=.*$/GRUB_DEFAULT=saved/' /etc/default/grub
69    fi
70    if [[ $? -ne 0 ]]; then
71        complain "Setting DEFAULT in /etc/default/grub returned a non-zero exit status"
72        exit 1
73    fi
74
75}
76
77
78function complain() {
79    if [ $DEBUG -eq 1 ]; then
80        echo "ERROR: $*"
81    else
82        logger -t "athena-auto-upgrade" -p user.notice "$*"
83    fi
84}
85
86
87
88function runcmd()
89{
90    CMD=$1
91    if [[ $FAKE == 1 ]]; then
92        echo $CMD
93    elif [[ $VERBOSE == 0 ]]; then
94        $CMD > /dev/null 2>&1
95    else
96        echo $CMD
97        $CMD
98    fi
99    if [[ $? -ne 0 ]]; then
100        complain "$CMD returned a non-zero exit status"
101        exit 1
102    fi
103}
104
105function debug() {
106    [ $DEBUG -eq 1 ] && echo "DEBUG: $*"
107}
108
109function maybe_quit() {
110    if [ $FORCE -eq 1 ]; then
111        echo "Would normally quit here, but you passed -f, so we keep going..."
112    else
113        exit 0
114    fi
115}
116
117if [ $DEBUG -ne 1 ]; then
118    # Redirect output to a log file
119    # Unless we're in debug mode
120    exec >>/var/log/athena-upgrade 2>&1
121fi
122
123echo "Starting upgrade at $(date)"
124
125# Not supported on Debian
126if [ "$(lsb_release -si)" != "Ubuntu" ]; then
127    debug "Only supported on Ubuntu.".
128    exit 0
129fi
130# Skip other sanity checks in debug mode
131if [ $DEBUG -ne 1 ]; then
132    # Only run this on cluster
133    if [ "$(machtype -L)" != "debathena-cluster" ] && \
134       [ "$UPGRADE_ANYWAY" != "yes" ]; then
135        debug "Not a cluster machine and UPGRADE_ANYWAY != yes.".
136        exit 0
137    fi
138   
139    # Bail if someone is logged in (stolen from auto-update)
140    ttys=$(w -h -s | awk '{print $2}')
141    for tty in $ttys; do
142        pids=$(ps --no-heading -j -t "$tty" 2>/dev/null \
143            | awk '($1 == $3) {print $1}')
144        if [ -n "$pids" ]; then
145            debug "Users logged in, won't continue."
146            debug "PIDs: $pids"
147            maybe_quit
148        fi
149    done
150
151    # screen processes count as logins.
152    if pgrep '^screen' > /dev/null; then
153        debug "Screen processes found, won't continue."
154        maybe_quit
155    fi
156fi
157export CLUSTERINFO=$(getcluster -b $(lsb_release -sr))
158[ $? != 0 ] && complain "Failed to get clusterinfo" && exit 1
159eval $CLUSTERINFO
160if [ ! -z "$NEW_PRODUCTION_RELEASE" ]; then
161    debug "Taking new production release: $NEW_PRODUCTION_RELEASE"
162    NEWCLUSTER=`getcluster -b $NEW_PRODUCTION_RELEASE`
163    [ $? != 0 ] && complain "Failed to get clusterinfo" && exit 1
164    eval $NEWCLUSTER
165elif [ ! -z "$NEW_TESTING_RELEASE" ] && \
166    [ "$UPGRADE_TO_TESTING" = "yes" ]; then
167    debug "Taking new testing release: $NEW_TESTING_RELEASE"
168    NEWCLUSTER=`getcluster -b $NEW_TESTING_RELEASE`
169    [ $? != 0 ] && complain "Failed to get clusterinfo" && exit 1
170    eval $NEWCLUSTER
171else
172    if [[ ! -z "$DEBUG_RELEASE" ]]; then
173        UBUNTU_RELEASE=$DEBUG_RELEASE
174    else
175        debug "No new releases."
176        exit 0
177    fi
178fi
179if [ "$(lsb_release -sc)" = "$UBUNTU_RELEASE" ]; then
180    complain "Tried to upgrade to already running release; shouldn't happen"
181    exit 1
182fi
183debug "OK, trying to upgrade to $UBUNTU_RELEASE"
184
185# That's a space and then a tab inside the brackets
186if egrep -q '^flags[    ].* lm( |$)' /proc/cpuinfo; then
187    arch=amd64
188else
189    arch=i386
190fi
191debug "Arch: $arch"
192IPADDR=
193NETMASK=
194eval `ifconfig eth0 |perl -ne'/^\s+inet addr:([\d\.]+).*? Mask:([\d\.]+)/ && print "IPADDR=$1 NETMASK=$2"'`
195if [ -z "$IPADDR" ]; then
196    complain "Couldn't get IPADDR from ifconfig!"
197    exit 0
198fi
199GATEWAY=`route -n | awk '/^0\.0\.0\.0/ { print $2 }'`
200debug "Using IPADDR=$IPADDR"
201debug "Using NETMASK=$NETMASK"
202debug "Using GATEWAY=$GATEWAY"
203
204nodhcp="netcfg/disable_dhcp=true"
205case "$UBUNTU_RELEASE" in
206    oneiric|precise)
207        kbdcode="keyboard-configuration/layoutcode=us"
208        # "Yay"
209        nodhcp="netcfg/disable_autoconfig=true"
210        ;;
211    natty)
212        # Sigh
213        kbdcode="keyboard-configuration/layoutcode=us"
214        ;;
215    *)
216        kbdcode="console-setup/layoutcode=us"
217        ;;
218esac
219
220if [ -n "$NETMASK" ] && [ -n "$GATEWAY" ]; then
221  knetinfo="netcfg/get_hostname=$(hostname) \
222            $nodhcp \
223            netcfg/get_domain=mit.edu \
224            netcfg/get_nameservers=\"18.72.0.3 18.70.0.160\"\
225            netcfg/get_ipaddress=$IPADDR \
226            netcfg/get_netmask=$NETMASK \
227            netcfg/get_gateway=$GATEWAY \
228            netcfg/confirm_static=true"
229else
230  knetinfo="netcfg/dhcp_timeout=60 netcfg/get_hostname=$(hostname)"
231fi
232
233runcmd "rm -rf /auto-upgrade"
234runcmd "mkdir -p /auto-upgrade"
235runcmd "wget http://debathena.mit.edu/net-install/${UBUNTU_RELEASE}/${arch}/initrd.gz -O /auto-upgrade/initrd.gz"
236runcmd "wget http://debathena.mit.edu/net-install/${UBUNTU_RELEASE}/${arch}/linux -O /auto-upgrade/linux"
237
238# This is just the guts of the hackboot script:
239dkargs="DEBCONF_DEBUG=5"
240kargs="$knetinfo locale=en_US $kbdcode \
241       quiet \
242       panic=5 \
243       interface=auto \
244       url=http://18.9.60.73/installer/${UBUNTU_RELEASE}/debathena.preseed \
245       da/pxe=cluster --"
246
247debug "USING kargs=$kargs"
248
249if [ $(echo $kargs | wc -c) -ge 512 ]; then
250    complain "kargs exceeds 512 bytes.  That's not good."
251    exit 1
252fi
253
254tmpfile="/tmp/$(basename $0).$RANDOM.tmp"
255
256cat >"$tmpfile"<<END
257#!/bin/sh
258
259echo "Adding auto-upgrade /auto-upgrade/linux" >&2
260cat<<EOF
261menuentry "auto-upgrade" {
262  insmod ext2
263  insmod part_msdos
264  insmod lvm
265  search --no-floppy --fs-uuid --set=root \${GRUB_DEVICE_UUID}
266  echo "Loading auto-upgrade kernel for ${UBUNTU_RELEASE}"
267  linux /auto-upgrade/linux $kargs
268  echo "Loading auto-upgrade ramdisk for ${UBUNTU_RELEASE}"
269  initrd /auto-upgrade/initrd.gz
270}
271EOF
272
273END
274
275
276debug "$(cat $tmpfile)"
277runcmd "cp $tmpfile /etc/grub.d/49_auto-upgrade"
278rm -f $tmpfile
279runcmd "chmod 755 /etc/grub.d/49_auto-upgrade"
280
281pie_grub
282
283runcmd "/usr/sbin/grub-set-default 0"
284runcmd "/usr/sbin/grub-reboot auto-upgrade"
285runcmd "/usr/sbin/update-grub"
286
287# rebooting into the installer kernel
288runcmd "/sbin/reboot"
Note: See TracBrowser for help on using the repository browser.