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

Revision 25663, 7.5 KB checked in by jdreed, 12 years ago (diff)
In auto-update: * Be more verbose in auto-upgrade * rotate auto-upgrade logs
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    echo "ERROR: $*"
80    if [ $DEBUG -ne 1 ]; then
81        logger -t "athena-auto-upgrade" -p user.notice "$*"
82    fi
83}
84
85
86
87function runcmd()
88{
89    CMD=$1
90    if [[ $FAKE == 1 ]]; then
91        echo $CMD
92    elif [[ $VERBOSE == 0 ]]; then
93        $CMD > /dev/null 2>&1
94    else
95        echo $CMD
96        $CMD
97    fi
98    if [[ $? -ne 0 ]]; then
99        complain "$CMD returned a non-zero exit status"
100        exit 1
101    fi
102}
103
104function debug() {
105    [ $DEBUG -eq 1 ] && echo "DEBUG: $*"
106}
107
108function maybe_quit() {
109    if [ $FORCE -eq 1 ]; then
110        echo "Would normally quit here, but you passed -f, so we keep going..."
111    else
112        echo "Ending upgrade at $(date)"
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
124echo "Starting upgrade at $(date)"
125
126# Not supported on Debian
127if [ "$(lsb_release -si)" != "Ubuntu" ]; then
128    debug "Only supported on Ubuntu.".
129    exit 0
130fi
131# Skip other sanity checks in debug mode
132if [ $DEBUG -ne 1 ]; then
133    # Only run this on cluster
134    if [ "$(machtype -L)" != "debathena-cluster" ] && \
135       [ "$UPGRADE_ANYWAY" != "yes" ]; then
136        debug "Not a cluster machine and UPGRADE_ANYWAY != yes.".
137        exit 0
138    fi
139   
140    # Bail if someone is logged in (stolen from auto-update)
141    ttys=$(w -h -s | awk '{print $2}')
142    for tty in $ttys; do
143        pids=$(ps --no-heading -j -t "$tty" 2>/dev/null \
144            | awk '($1 == $3) {print $1}')
145        if [ -n "$pids" ]; then
146            debug "Users logged in, won't continue."
147            debug "PIDs: $pids"
148            maybe_quit
149        fi
150    done
151
152    # screen processes count as logins.
153    if pgrep '^screen' > /dev/null; then
154        debug "Screen processes found, won't continue."
155        maybe_quit
156    fi
157fi
158export CLUSTERINFO=$(getcluster -b $(lsb_release -sr))
159[ $? != 0 ] && complain "Failed to get clusterinfo" && exit 1
160eval $CLUSTERINFO
161if [ ! -z "$NEW_PRODUCTION_RELEASE" ]; then
162    debug "Taking new production release: $NEW_PRODUCTION_RELEASE"
163    NEWCLUSTER=`getcluster -b $NEW_PRODUCTION_RELEASE`
164    [ $? != 0 ] && complain "Failed to get clusterinfo" && exit 1
165    eval $NEWCLUSTER
166elif [ ! -z "$NEW_TESTING_RELEASE" ] && \
167    [ "$UPGRADE_TO_TESTING" = "yes" ]; then
168    debug "Taking new testing release: $NEW_TESTING_RELEASE"
169    NEWCLUSTER=`getcluster -b $NEW_TESTING_RELEASE`
170    [ $? != 0 ] && complain "Failed to get clusterinfo" && exit 1
171    eval $NEWCLUSTER
172else
173    if [[ ! -z "$DEBUG_RELEASE" ]]; then
174        UBUNTU_RELEASE=$DEBUG_RELEASE
175    else
176        echo "No new releases found."
177        exit 0
178    fi
179fi
180if [ "$(lsb_release -sc)" = "$UBUNTU_RELEASE" ]; then
181    complain "Tried to upgrade to already running release; shouldn't happen"
182    exit 1
183fi
184echo "Found $UBUNTU_RELEASE, starting upgrade"
185
186# That's a space and then a tab inside the brackets
187if egrep -q '^flags[    ].* lm( |$)' /proc/cpuinfo; then
188    arch=amd64
189else
190    arch=i386
191fi
192debug "Arch: $arch"
193IPADDR=
194NETMASK=
195eval `ifconfig eth0 |perl -ne'/^\s+inet addr:([\d\.]+).*? Mask:([\d\.]+)/ && print "IPADDR=$1 NETMASK=$2"'`
196if [ -z "$IPADDR" ]; then
197    complain "Couldn't get IPADDR from ifconfig!"
198    exit 0
199fi
200GATEWAY=`route -n | awk '/^0\.0\.0\.0/ { print $2 }'`
201debug "Using IPADDR=$IPADDR"
202debug "Using NETMASK=$NETMASK"
203debug "Using GATEWAY=$GATEWAY"
204
205nodhcp="netcfg/disable_dhcp=true"
206case "$UBUNTU_RELEASE" in
207    oneiric|precise)
208        kbdcode="keyboard-configuration/layoutcode=us"
209        # "Yay"
210        nodhcp="netcfg/disable_autoconfig=true"
211        ;;
212    natty)
213        # Sigh
214        kbdcode="keyboard-configuration/layoutcode=us"
215        ;;
216    *)
217        kbdcode="console-setup/layoutcode=us"
218        ;;
219esac
220
221if [ -n "$NETMASK" ] && [ -n "$GATEWAY" ]; then
222  knetinfo="netcfg/get_hostname=$(hostname) \
223            $nodhcp \
224            netcfg/get_domain=mit.edu \
225            netcfg/get_nameservers=\"18.72.0.3 18.70.0.160\"\
226            netcfg/get_ipaddress=$IPADDR \
227            netcfg/get_netmask=$NETMASK \
228            netcfg/get_gateway=$GATEWAY \
229            netcfg/confirm_static=true"
230else
231  knetinfo="netcfg/dhcp_timeout=60 netcfg/get_hostname=$(hostname)"
232fi
233
234runcmd "rm -rf /auto-upgrade"
235runcmd "mkdir -p /auto-upgrade"
236runcmd "wget http://debathena.mit.edu/net-install/${UBUNTU_RELEASE}/${arch}/initrd.gz -O /auto-upgrade/initrd.gz"
237runcmd "wget http://debathena.mit.edu/net-install/${UBUNTU_RELEASE}/${arch}/linux -O /auto-upgrade/linux"
238
239# This is just the guts of the hackboot script:
240dkargs="DEBCONF_DEBUG=5"
241kargs="$knetinfo locale=en_US $kbdcode \
242       quiet \
243       panic=5 \
244       interface=auto \
245       url=http://18.9.60.73/installer/${UBUNTU_RELEASE}/debathena.preseed \
246       da/pxe=cluster --"
247
248debug "USING kargs=$kargs"
249
250if [ $(echo $kargs | wc -c) -ge 512 ]; then
251    complain "kargs exceeds 512 bytes.  That's not good."
252    exit 1
253fi
254
255tmpfile="/tmp/$(basename $0).$RANDOM.tmp"
256
257cat >"$tmpfile"<<END
258#!/bin/sh
259
260echo "Adding auto-upgrade /auto-upgrade/linux" >&2
261cat<<EOF
262menuentry "auto-upgrade" {
263  insmod ext2
264  insmod part_msdos
265  insmod lvm
266  search --no-floppy --fs-uuid --set=root \${GRUB_DEVICE_UUID}
267  echo "Loading auto-upgrade kernel for ${UBUNTU_RELEASE}"
268  linux /auto-upgrade/linux $kargs
269  echo "Loading auto-upgrade ramdisk for ${UBUNTU_RELEASE}"
270  initrd /auto-upgrade/initrd.gz
271}
272EOF
273
274END
275
276
277debug "$(cat $tmpfile)"
278runcmd "cp $tmpfile /etc/grub.d/49_auto-upgrade"
279rm -f $tmpfile
280runcmd "chmod 755 /etc/grub.d/49_auto-upgrade"
281
282pie_grub
283
284runcmd "/usr/sbin/grub-set-default 0"
285runcmd "/usr/sbin/grub-reboot auto-upgrade"
286runcmd "/usr/sbin/update-grub"
287
288# rebooting into the installer kernel
289runcmd "/sbin/reboot"
Note: See TracBrowser for help on using the repository browser.