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