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

Revision 25768, 8.9 KB checked in by jdreed, 12 years ago (diff)
In auto-update: * Ensure there are no double slashes in URLs, because Google hates them * Make the mirror check non-fatal -- merely warn
Line 
1#!/bin/sh
2
3complain() {
4  echo "ERROR: $*"
5  logger -t "athena-auto-update" -p user.notice "$*"
6  updstatus="failed"
7  updmsg="$*"
8}
9
10warn() {
11  updstatus="warning"
12  updmsg="$*"
13  echo "WARNING: $*"
14}
15
16save_success() {
17  # We used to check for 'warning' here for non-fatal warnings.
18  # There is no longer any such thing.
19  updstatus="ok"
20  updmsg="$*"
21}
22
23save_state() {
24  rm -f $statfile
25  echo "$updlast|$(date +"%s")|$updstatus|$updmsg" > $statfile
26
27
28maybe_reboot() {
29  if [ "$SKIP_REBOOT" = "y" ]; then
30      return
31  fi
32  if [ -f /var/run/reboot-required ]; then
33    # A package wants us to reboot the machine.  Do so if no one is
34    # logged in.  Be paranoid about stale utmp entries.
35    ttys=$(w -h -s | awk '{print $2}')
36    for tty in $ttys; do
37      pids=$(ps --no-heading -j -t "$tty" 2>/dev/null \
38             | awk '($1 == $3) {print $1}')
39      if [ -n "$pids" ]; then
40        return
41      fi
42    done
43    # screen processes count as logins.
44    if pgrep '^screen' > /dev/null; then
45      return
46    fi
47    save_state
48    reboot
49    exit
50  fi
51}
52
53SKIP_REBOOT="n"
54DEBUG="n"
55VERBOSE="n"
56while getopts "nvd" opt; do
57    case "$opt" in
58        d) DEBUG="y";;
59        v) VERBOSE="y";;
60        n) SKIP_REBOOT="y";;
61        \?)
62            echo "Usage: $0 [ -d ] [ -n ] [ -v ]"
63            ;;
64    esac
65done
66
67[ "$DEBUG" = "y" ] && VERBOSE="y"
68
69if [ 0 != "$(id -u)" ]; then
70  echo "This script must be run as root." >&2
71  exit 1
72fi
73
74# Don't run updates during a cluster login.
75# Unless forced
76if [ -e /var/run/athena-login ] && [ "$DEBUG" != "y" ]; then
77  exit 0
78fi
79
80# Avoid confusing the system by running two updates at once.
81pidfile=/var/run/athena-update.pid
82if [ -e $pidfile ]; then
83  if ! kill -0 "$(cat $pidfile)" 2>/dev/null; then
84    rm -f $pidfile
85  fi
86fi
87(set -o noclobber; echo $$ > $pidfile) 2>/dev/null || exit
88
89trap 'rm -f $pidfile' EXIT
90
91statfile="/var/lib/athena-update-status"
92updstatus="unknown"
93updmsg=""
94updlast=$(date +"%s")
95
96# Get the last successful update
97if [ -f $statfile ]; then
98    updlast=$(awk -F\| '{print $1;}' $statfile)
99    updstatus=$(awk -F\| '{print $3;}' $statfile)
100fi
101
102# Make sure nothing expects input on stdin.
103exec </dev/null
104
105# Save a reference to STDOUT
106exec 3>&1
107
108# Redirect further output to a log file.
109exec >>/var/log/athena-update 2>&1
110
111# Write a log header now and a footer at exit.
112# Also write a target for cluster's /etc/nologin symlink.
113echo "-----"
114echo "** Beginning Athena auto-update at $(date)"
115
116cat > /var/run/athena-nologin << NOLOGIN
117This system is currently taking software updates.
118Please log in to a different system.
119NOLOGIN
120
121finish() {
122    echo "** Ending Athena auto-update at $(date)"
123    echo "-----"
124    echo
125    rm -f $pidfile
126    rm -f /var/run/athena-nologin
127    save_state
128    exit
129}
130trap finish EXIT
131
132v() {
133  [ "$VERBOSE" = "y" ] && echo "Running" "$@" >&3
134  echo "** Running:" "$@"
135  "$@"
136}
137
138
139# Allow hesiod cluster info to specify the debathena apt release.
140# (Will do nothing if debathena-clusterinfo isn't installed.)
141[ -x /usr/sbin/save-cluster-info ] && v /usr/sbin/save-cluster-info
142cinfo=/var/run/athena-clusterinfo.sh
143slist=/etc/apt/sources.list.d/debathena.clusterinfo.list
144if [ -r "$cinfo" ] && ( [ ! -e "$slist" ] || [ -w "$slist" ] ); then
145  echo "** Updating debathena.clusterinfo.list"
146  [ -e "$slist" ] && rm "$slist"
147  dsource="$(egrep -h '^deb(-src)? http://debathena\.mit\.edu/apt ' /etc/apt/sources.list /etc/apt/sources.list.d/*.list 2>/dev/null | sort -u)"
148  if [ -n "$dsource" ]; then
149    (. $cinfo
150     echo "# This file is automatically updated by debathena-auto-update"
151     echo "# based on your Hesiod cluster information. If you want to"
152     echo "# make changes, do so in another file."
153     echo
154     case $APT_RELEASE in
155       production)  ;;
156       proposed)    echo "$dsource" | awk '$3 !~ /-/ {$3 = $3 "-proposed"; print}' ;;
157       development) echo "$dsource" | awk '$3 !~ /-/ {$3 = $3 "-proposed"; print}'
158                    echo "$dsource" | awk '$3 !~ /-/ {$3 = $3 "-development"; print}' ;;
159     esac
160    ) > $slist
161  else
162    echo "Never mind, I can't figure out which sources.list line is Debathena's"
163  fi
164else
165  echo "** Skipping update of debathena.clusterinfo.list"
166fi
167
168# Tell apt not to expect user input during package installation.
169export DEBIAN_FRONTEND=noninteractive
170
171# Set conservative defaults in case file is missing
172UPDATE_FORCE_CONFFILE=old
173RUN_UPDATE_HOOK=no
174# Process defaults file
175[ -f /etc/default/debathena-auto-update ] && . /etc/default/debathena-auto-update
176# On cluster machines, force our desired settings
177# Ignore /etc/default/debathena-auto-update
178if dpkg-query --showformat '${Status}\n' -W "debathena-cluster" 2>/dev/null | grep -q ' installed$'; then
179    UPDATE_FORCE_CONFFILE=new
180    RUN_UPDATE_HOOK=yes
181fi
182
183UPDATE_HOOK_URL="https://athena10.mit.edu/update-hook/debathena-update-hook.sh"
184UPDATE_HOOK_SUM="https://athena10.mit.edu/update-hook/debathena-update-hook-sha256sum"
185MITCA="/usr/share/debathena-auto-update/mitCA.crt"
186UPDATE_HOOK="/var/run/debathena-update-hook.sh"
187
188rm -f $UPDATE_HOOK
189if [ "$RUN_UPDATE_HOOK" = "yes" ] && \
190   curl -sf -o $UPDATE_HOOK --cacert $MITCA $UPDATE_HOOK_URL; then
191   chmod 500 $UPDATE_HOOK
192   SHA256SUM="$(curl -sf --cacert $MITCA $UPDATE_HOOK_SUM)"
193   rv=$?
194   if [ $rv != 0 ]; then
195       complain "Failed to retrieve $UPDATE_HOOK_SUM (curl returned $rv)"
196       exit
197   fi
198   LOCALSUM="$(sha256sum $UPDATE_HOOK | awk '{print $1}')"
199   if [ "$SHA256SUM" != "$LOCALSUM" ]; then
200       complain "bad update hook checksum ($SHA256SUM != $LOCALSUM)"
201       exit
202   fi     
203   if ! [ -f "/var/lib/athena-update-hooks/$SHA256SUM" ]; then
204       if ! v $UPDATE_HOOK; then
205           complain "update hook returned non-zero status"
206           exit
207       else
208           touch "/var/lib/athena-update-hooks/$SHA256SUM"
209       fi
210   fi
211fi
212
213echo "Running apt-get install"
214if ! v apt-get --fix-broken --assume-yes install; then
215  # Don't fail, because maybe dpkg --configure -a will save us 
216  echo "ERROR: apt-get install failed, but continuing anyway"
217fi
218 
219
220# Configure any unconfigured packages (Trac #407)
221if ! v dpkg --configure -a; then
222  complain "Failed to configure unconfigured packages."
223  exit
224fi 
225
226# A recently configured package may want a reboot
227save_success "Rebooted after dpkg --configure -a"
228maybe_reboot
229
230# Ensure that the mirrors aren't broken
231# Google's apt repos are ridiculously picky
232urls=$(cat /etc/apt/sources.list /etc/apt/sources.list.d/*.list | awk '!/^deb/ {next;} $2 ~ /^https/ {printf $2; if ($2 !~ /\/$/) printf "/"; printf "dists/"$3"/Release\n"}' | sort -u)
233failed=""
234for u in $urls; do
235    curl -m 60 -sfL -o /dev/null $u
236    if [ $? != 0 ]; then
237        if [ -z "$failed" ]; then
238            failed=$u
239        else
240            failed="$failed $u"
241        fi
242    fi
243done
244if [ -n "$failed" ]; then
245    warn "Failed to contact mirror(s): $failed"
246    exit
247fi
248
249# Update the apt cache.
250if ! v apt-get --assume-yes update; then
251  complain "apt-get update failed"
252  exit
253fi
254
255# If new licenses were installed since the last update, deal.
256licenses=/usr/share/debathena-license-config/debathena-license-selections
257if [ -f /var/lib/debathena-license-config/reconfigure_required ]; then
258    rm -f /var/lib/debathena-license-config/reconfigure_required
259    if [ -f $licenses ]; then
260        for p in $(awk '{print $1}' $licenses); do
261            if dpkg-query --showformat '${Status}\n' -W $p 2>/dev/null | grep -q ' installed$'; then
262                if ! v dpkg-reconfigure -fnoninteractive $p; then
263                    # Don't fail here
264                    complain "Failed to dpkg-reconfigure $p"
265                fi
266            fi
267        done
268    else
269        complain "Could not find $licenses"
270        exit
271    fi
272fi
273
274
275# Exit quietly (except for perhaps rebooting) if there are no upgrades
276# to take.
277pattern='^0 upgraded, 0 newly installed, 0 to remove'
278if v apt-get --simulate --assume-yes dist-upgrade | grep -q "$pattern"; then
279  echo "Nothing to do!"
280  save_success "No updates"
281  maybe_reboot
282  exit
283fi
284
285# Download packages first.
286echo "Downloading packages..."
287if ! v apt-get --quiet --assume-yes --download-only dist-upgrade; then
288  complain "download failed"
289  exit
290fi
291
292APTGET_OPTS=
293case $UPDATE_FORCE_CONFFILE in
294    old)
295        APTGET_OPTS="-o Dpkg::Options::=--force-confold"
296        export UCF_FORCE_CONFFOLD=1
297        ;;
298    new)
299        APTGET_OPTS="-o Dpkg::Options::=--force-confnew"
300        export UCF_FORCE_CONFFNEW=1
301        ;;
302    *)
303        complain "Invalid value for UPDATE_FORCE_CONFFILE"
304        exit
305        ;;
306esac
307
308
309# Perform the update.  In some corner cases, apt-get might decide
310# that the best course of action is to remove the Debathena
311# metapackage, so be paranoid about that.
312result="$(apt-get --quiet --assume-yes --simulate dist-upgrade)"
313if [ $? -ne 0 ]; then
314    complain "apt-get simulation failed!"
315elif echo "$result" | egrep -q '^Remv debathena-(cluster|workstation|login-graphical|login|standard|auto-update) '; then
316    complain "metapackages would be removed by update:" $result
317else
318  if v apt-get $APTGET_OPTS --quiet --assume-yes dist-upgrade; then
319    # Successful update, reset $updlast
320    updlast=$(date +"%s")
321    save_success "Successful update"
322  else
323    complain "Simulation was successful, but update failed (shouldn't happen)"
324  fi
325fi
326
327# Finally, update the apt-file cache
328v apt-file update
329
330maybe_reboot
331exit
Note: See TracBrowser for help on using the repository browser.