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

Revision 25859, 10.5 KB checked in by jdreed, 12 years ago (diff)
In auto-update: * Actually, don't bother cleaning up the headers by hand. Instead, just auto-remove packages (unless that would remove metapackages) for compatibility with old aptitude-based updater. (Trac: #1012) * Convert from CDBS to dh7 and build-dep on dh 7.0.50 * Update copyright file to format 1.0 and re-license under BSD 3-clause * Bump Standards-Version to 3.9.3 * Remove unneeded dependency on bsdutils (it's required) * Add $remote_fs dependency in init script because it's apparently still 1992 and /usr might be an NFS mount
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
174CLEANUP_OLD_KERNELS=no
175# Process defaults file
176[ -f /etc/default/debathena-auto-update ] && . /etc/default/debathena-auto-update
177# On cluster machines, force our desired settings
178# Ignore /etc/default/debathena-auto-update
179if dpkg-query --showformat '${Status}\n' -W "debathena-cluster" 2>/dev/null | grep -q ' installed$'; then
180    CLEANUP_OLD_KERNELS=yes
181    UPDATE_FORCE_CONFFILE=new
182    RUN_UPDATE_HOOK=yes
183fi
184
185UPDATE_HOOK_URL="https://athena10.mit.edu/update-hook/debathena-update-hook.sh"
186UPDATE_HOOK_SUM="https://athena10.mit.edu/update-hook/debathena-update-hook-sha256sum"
187MITCA="/usr/share/debathena-auto-update/mitCA.crt"
188UPDATE_HOOK="/var/run/debathena-update-hook.sh"
189
190rm -f $UPDATE_HOOK
191if [ "$RUN_UPDATE_HOOK" = "yes" ] && \
192   curl -sf -o $UPDATE_HOOK --cacert $MITCA $UPDATE_HOOK_URL; then
193   chmod 500 $UPDATE_HOOK
194   SHA256SUM="$(curl -sf --cacert $MITCA $UPDATE_HOOK_SUM)"
195   rv=$?
196   if [ $rv != 0 ]; then
197       complain "Failed to retrieve $UPDATE_HOOK_SUM (curl returned $rv)"
198       exit
199   fi
200   LOCALSUM="$(sha256sum $UPDATE_HOOK | awk '{print $1}')"
201   if [ "$SHA256SUM" != "$LOCALSUM" ]; then
202       complain "bad update hook checksum ($SHA256SUM != $LOCALSUM)"
203       exit
204   fi     
205   if ! [ -f "/var/lib/athena-update-hooks/$SHA256SUM" ]; then
206       if ! v $UPDATE_HOOK; then
207           complain "update hook returned non-zero status"
208           exit
209       else
210           touch "/var/lib/athena-update-hooks/$SHA256SUM"
211       fi
212   fi
213fi
214
215echo "Running apt-get install"
216if ! v apt-get --fix-broken --assume-yes install; then
217  # Don't fail, because maybe dpkg --configure -a will save us 
218  echo "ERROR: apt-get install failed, but continuing anyway"
219fi
220 
221
222# Configure any unconfigured packages (Trac #407)
223if ! v dpkg --configure -a; then
224  complain "Failed to configure unconfigured packages."
225  exit
226fi 
227
228# A recently configured package may want a reboot
229save_success "Rebooted after dpkg --configure -a"
230maybe_reboot
231
232# Ensure that the mirrors aren't broken
233# Google's apt repos are ridiculously picky
234urls=$(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)
235failed=""
236for u in $urls; do
237    curl -m 60 -sfL -o /dev/null $u
238    if [ $? != 0 ]; then
239        if [ -z "$failed" ]; then
240            failed=$u
241        else
242            failed="$failed $u"
243        fi
244    fi
245done
246if [ -n "$failed" ]; then
247    warn "Failed to contact mirror(s): $failed"
248    exit
249fi
250
251# Update the apt cache.
252if ! v apt-get --assume-yes update; then
253  complain "apt-get update failed"
254  exit
255fi
256
257# If new licenses were installed since the last update, deal.
258licenses=/usr/share/debathena-license-config/debathena-license-selections
259if [ -f /var/lib/debathena-license-config/reconfigure_required ]; then
260    rm -f /var/lib/debathena-license-config/reconfigure_required
261    if [ -f $licenses ]; then
262        for p in $(awk '{print $1}' $licenses); do
263            if dpkg-query --showformat '${Status}\n' -W $p 2>/dev/null | grep -q ' installed$'; then
264                if ! v dpkg-reconfigure -fnoninteractive $p; then
265                    # Don't fail here
266                    complain "Failed to dpkg-reconfigure $p"
267                fi
268            fi
269        done
270    else
271        complain "Could not find $licenses"
272        exit
273    fi
274fi
275
276
277# Exit quietly (except for perhaps rebooting) if there are no upgrades
278# to take.
279pattern='^0 upgraded, 0 newly installed, 0 to remove'
280if v apt-get --simulate --assume-yes dist-upgrade | grep -q "$pattern"; then
281  echo "Nothing to do!"
282  save_success "No updates"
283  maybe_reboot
284  exit
285fi
286
287# Download packages first.
288echo "Downloading packages..."
289if ! v apt-get --quiet --assume-yes --download-only dist-upgrade; then
290  complain "download failed"
291  exit
292fi
293
294APTGET_OPTS=
295case $UPDATE_FORCE_CONFFILE in
296    old)
297        APTGET_OPTS="-o Dpkg::Options::=--force-confold"
298        export UCF_FORCE_CONFFOLD=1
299        ;;
300    new)
301        APTGET_OPTS="-o Dpkg::Options::=--force-confnew"
302        export UCF_FORCE_CONFFNEW=1
303        ;;
304    *)
305        complain "Invalid value for UPDATE_FORCE_CONFFILE"
306        exit
307        ;;
308esac
309
310
311# Perform the update.  In some corner cases, apt-get might decide
312# that the best course of action is to remove the Debathena
313# metapackage, so be paranoid about that.
314result="$(apt-get --quiet --assume-yes --simulate dist-upgrade)"
315if [ $? -ne 0 ]; then
316    complain "apt-get simulation failed!"
317elif echo "$result" | egrep -q '^Remv debathena-(cluster|workstation|login-graphical|login|standard|auto-update) '; then
318    complain "metapackages would be removed by update:" $result
319else
320  if v apt-get $APTGET_OPTS --quiet --assume-yes dist-upgrade; then
321    # Successful update, reset $updlast
322    updlast=$(date +"%s")
323    save_success "Successful update"
324  else
325    complain "Simulation was successful, but update failed (shouldn't happen)"
326  fi
327fi
328
329# Cleanup old kernels
330# This can probably go away once /boot is no longer on its own
331# partition.
332if [ "$CLEANUP_OLD_KERNELS" = "yes" ]; then
333  echo "Cleaning up old kernels..."
334  kernels=$(dpkg-query -W -f '${Package}:${Status}\n' linux-image-\*-generic | \
335    awk -F ':' '$2=="install ok installed" {print $1;}' | \
336    sed -e 's/^linux-image-//' | sort -V)
337  numkernels=$(echo "$kernels" | wc -l)
338  echo "Found $numkernels kernels: $kernels"
339  if [ $numkernels -gt 2 ]; then
340    toremove=$(echo "$kernels" | head -$(($numkernels-2)))
341    kpkgs=
342    for k in $toremove; do
343      if [ "$(uname -r)" != "$k" ]; then
344        kpkgs="$kpkgs linux-image-$k"
345      fi
346    done
347    echo "Will remove these kernels: $kpkgs"
348    echo "Simulating..."
349    if v apt-get -y -s remove $kpkgs; then
350      echo "Simulation ok, proceeding..."
351      if ! v apt-get -y remove $kpkgs; then
352        warn "Something went wrong trying to remove kernels!"
353      fi
354    else
355        warn "Simulation failed!"
356    fi
357  fi
358fi
359
360# Auto-remove packages
361echo "Attempting to auto-remove old packages"
362echo "Simulating..."
363AUTOREMOVE="$(apt-get --assume-yes --simulate autoremove)"
364if [ $? -ne 0 ]; then
365    warn "Auto-remove simulation failed!"
366elif echo "$AUTOREMOVE" | egrep -q '^Remv debathena-(cluster|workstation|login-graphical|login|standard|auto-update) '; then
367    warn "Metapackages would be removed by auto-remove!"
368else
369    if ! v apt-get -y autoremove; then
370        complain "Auto-remove failed even though simulation succeeded (shouldn't happen)"
371    fi
372fi
373
374# Finally, update the apt-file cache
375v apt-file update
376
377maybe_reboot
378exit
Note: See TracBrowser for help on using the repository browser.