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

Revision 24522, 6.2 KB checked in by jdreed, 14 years ago (diff)
In auto-update: * Change update-status to give seconds since the epoch which is easier for Nagios to parse
Line 
1#!/bin/sh
2
3complain() {
4  logger -t "athena-auto-update" -p user.notice "$*"
5  updstatus="failed"
6  updmsg="$*"
7}
8
9save_success() {
10  updstatus="ok"
11  updmsg=$1
12}
13
14save_state() {
15  rm -f $statfile
16  echo "$updlast|$(date +"%s")|$updstatus|$updmsg" > $statfile
17
18
19maybe_reboot() {
20  if [ "$SHOULD_REBOOT" = "no" ]; then
21      return
22  fi
23  if [ -f /var/run/reboot-required ]; then
24    # A package wants us to reboot the machine.  Do so if no one is
25    # logged in.  Be paranoid about stale utmp entries.
26    ttys=$(w -h -s | awk '{print $2}')
27    for tty in $ttys; do
28      pids=$(ps --no-heading -j -t "$tty" 2>/dev/null \
29             | awk '($1 == $3) {print $1}')
30      if [ -n "$pids" ]; then
31        return
32      fi
33    done
34    # screen processes count as logins.
35    if pgrep '^screen' > /dev/null; then
36      return
37    fi
38    save_state
39    reboot
40    exit
41  fi
42}
43
44SHOULD_REBOOT="yes"
45if [ "$1" = "-n" ]; then
46    SHOULD_REBOOT="no"
47fi
48
49if [ 0 != "$(id -u)" ]; then
50  echo "This script must be run as root." >&2
51  exit 1
52fi
53
54# Don't run updates during a cluster login.
55if [ -e /var/run/athena-login ]; then
56  exit 0
57fi
58
59# Avoid confusing the system by running two updates at once.
60pidfile=/var/run/athena-update.pid
61if [ -e $pidfile ]; then
62  if ! kill -0 "$(cat $pidfile)" 2>/dev/null; then
63    rm -f $pidfile
64  fi
65fi
66(set -o noclobber; echo $$ > $pidfile) 2>/dev/null || exit
67
68trap 'rm -f $pidfile' EXIT
69
70statfile="/var/lib/athena-update-status"
71updstatus="unknown"
72updmsg=""
73updlast=$(date +"%s")
74
75# Get the last successful update
76if [ -f $statfile ]; then
77    updlast=$(awk -F\| '{print $1;}' $statfile)
78fi
79
80# Make sure nothing expects input on stdin.
81exec </dev/null
82
83# Redirect further output to a log file.
84exec >>/var/log/athena-update 2>&1
85
86# Write a log header now and a footer at exit.
87# Also write a target for cluster's /etc/nologin symlink.
88echo "-----"
89echo "** Beginning Athena auto-update at $(date)"
90
91cat > /var/run/athena-nologin << NOLOGIN
92This system is currently taking software updates.
93Please log in to a different system.
94NOLOGIN
95
96finish() {
97    echo "** Ending Athena auto-update at $(date)"
98    echo "-----"
99    echo
100    rm -f $pidfile
101    rm -f /var/run/athena-nologin
102    save_state
103    exit
104}
105trap finish EXIT
106
107v() {
108  echo "** Running:" "$@"
109  "$@"
110}
111
112
113# Allow hesiod cluster info to specify the debathena apt release.
114# (Will do nothing if debathena-clusterinfo isn't installed.)
115[ -x /usr/sbin/save-cluster-info ] && v /usr/sbin/save-cluster-info
116cinfo=/var/run/athena-clusterinfo.sh
117slist=/etc/apt/sources.list.d/debathena.clusterinfo.list
118if [ -r "$cinfo" ] && ( [ ! -e "$slist" ] || [ -w "$slist" ] ); then
119  echo "** Updating debathena.clusterinfo.list"
120  [ -e "$slist" ] && rm "$slist"
121  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)"
122  if [ -n "$dsource" ]; then
123    (. $cinfo
124     echo "# This file is automatically updated by debathena-auto-update"
125     echo "# based on your Hesiod cluster information. If you want to"
126     echo "# make changes, do so in another file."
127     echo
128     case $APT_RELEASE in
129       production)  ;;
130       proposed)    echo "$dsource" | awk '$3 !~ /-/ {$3 = $3 "-proposed"; print}' ;;
131       development) echo "$dsource" | awk '$3 != /-/ {$3 = $3 "-proposed"; print}'
132                    echo "$dsource" | awk '$3 != /-/ {$3 = $3 "-development"; print}' ;;
133     esac
134    ) > $slist
135  else
136    echo "Never mind, I can't figure out which sources.list line is Debathena's"
137  fi
138else
139  echo "** Skipping update of debathena.clusterinfo.list"
140fi
141
142# Tell apt not to expect user input during package installation.
143export DEBIAN_FRONTEND=noninteractive
144
145# Configure any unconfigured packages (Trac #407)
146if ! v dpkg --configure -a; then
147  complain "Failed to configure unconfigured packages."
148  exit
149fi 
150
151# A recently configured package may want a reboot
152save_success "Rebooted after dpkg --configure -a"
153maybe_reboot
154
155# Update the aptitude cache.
156if ! v aptitude --quiet --assume-yes update; then
157  complain "aptitude update failed"
158  exit
159fi
160
161# Exit quietly (except for perhaps rebooting) if there are no upgrades
162# to take.
163pattern='^0 packages upgraded, 0 newly installed, 0 to remove'
164if v aptitude --simulate --assume-yes full-upgrade | grep -q "$pattern"; then
165  echo "Nothing to do!"
166  save_success "No updates"
167  maybe_reboot
168  exit
169fi
170
171# Download packages first.
172if ! v aptitude --quiet --assume-yes --download-only full-upgrade; then
173  complain "download failed"
174  exit
175fi
176
177# If the debathena-reactivate package is installed, call into the
178# login snapshot script to create a root snapshot for the next update.
179if [ -x /usr/sbin/athena-login-snapshot ]; then
180  echo "** Creating root snapshot"
181  /usr/sbin/athena-login-snapshot update-start
182fi
183
184APTITUDE_OPTS=
185# On cluster machines, force the new conffile if there's a conflict
186if dpkg-query --showformat '${Status}\n' -W "debathena-cluster" 2>/dev/null | grep -q ' installed$'; then
187    APTITUDE_OPTS="-o Dpkg::Options::=--force-confnew"
188fi
189
190# Perform the update.  In some corner cases, aptitude might decide
191# that the best course of action is to remove the Debathena
192# metapackage, so be paranoid about that.
193v aptitude --quiet --assume-yes keep-all
194v aptitude --quiet --assume-yes --download-only dist-upgrade
195if result=$(aptitude -F "%p" search '~i!~VTARGET(^debathena-cluster$ | ^debathena-workstation$ | ^debathena-login$ | ^debathena-standard$ | ^openafs-modules-~D^linux-image-)')
196  [ -n "$result" ]; then
197  echo "** metapackages would be removed by the update, aborting:" $result
198  v aptitude --quiet --assume-yes keep-all
199  complain "metapackages would be removed by update:" $result
200elif result=$(aptitude -F "%p" search '~b')
201  [ -n "$result" ]; then
202  echo "** packages would be broken by the update, aborting:" $result
203  v aptitude --quiet --assume-yes keep-all
204  complain "packages would be broken by update:" $result
205else
206  v aptitude $APTITUDE_OPTS --quiet --assume-yes install
207  # Successful update, reset $updlast
208  updlast=$(date +"%s")
209  save_success "Successful update"
210fi
211
212# Finally, update the apt-file cache
213v apt-file update
214
215if [ -x /usr/sbin/athena-login-snapshot ]; then
216  echo "** Cleaning up root snapshot"
217  /usr/sbin/athena-login-snapshot update-end
218fi
219
220maybe_reboot
221exit
Note: See TracBrowser for help on using the repository browser.