1 | #!/bin/bash |
---|
2 | |
---|
3 | # This script is intended to be run from a cron job by a local account |
---|
4 | # on a build machine--either debuild on debuild.mit.edu or builder on |
---|
5 | # linux-build-10.mit.edu. The account should have: |
---|
6 | # |
---|
7 | # * A keytab installed in $HOME/keytab containing a keytab for |
---|
8 | # daemon/HOSTNAME, which should have write access to the build |
---|
9 | # directory and apt repository. |
---|
10 | # |
---|
11 | # * A GPG secret key capable of signing packages for the apt |
---|
12 | # repository. |
---|
13 | # |
---|
14 | # * A file named $HOME/autodebathenify.config specifying shell |
---|
15 | # variable assignments for: |
---|
16 | # - error_addr: An email address to send errors to |
---|
17 | # - scripts_dir: Directory containing Debathena scripts (da, etc.) |
---|
18 | # - build_dir: Build directory location, up through "third". |
---|
19 | # - packages: A list of packages to keep up to date |
---|
20 | # It should also specify an environment variable assignment for |
---|
21 | # DEBATHENA_APT. |
---|
22 | |
---|
23 | # Send all output to a logfile. |
---|
24 | mkdir -p "$HOME/logs" |
---|
25 | logfile=$HOME/logs/$(date '+%Y-%m-%d-%H').log |
---|
26 | exec </dev/null >>$logfile 2>&1 |
---|
27 | |
---|
28 | # Read configuration; if it's not complete, do nothing. |
---|
29 | config=$HOME/autodebathenify.config |
---|
30 | . $HOME/autodebathenify.config |
---|
31 | if [ ! -n "$error_addr" -o ! -n "$scripts_dir" -o ! -n "$build_dir" -o \ |
---|
32 | ! -n "$packages" ]; then |
---|
33 | echo "Incomplete $config; doing nothing." |
---|
34 | exit |
---|
35 | fi |
---|
36 | |
---|
37 | suppress=$HOME/autodebathenify.suppress |
---|
38 | if [ -e "$suppress" ]; then |
---|
39 | if [ $(($(date +%s) - $(stat -c %Y "$suppress"))) -ge 86400 ]; then |
---|
40 | /usr/sbin/sendmail -t << EOM |
---|
41 | From: Autodebathenify <$error_addr> |
---|
42 | To: Autodebathenify <$error_addr> |
---|
43 | Subject: WARNING: Previous autodebathenify failure has not been addressed |
---|
44 | |
---|
45 | Autodebathenify previously experienced a failure on $(stat -c %y "$suppress") |
---|
46 | |
---|
47 | Please correct this failure immediately. |
---|
48 | |
---|
49 | Autodebathenify will stop running until $suppress is removed. |
---|
50 | EOM |
---|
51 | fi |
---|
52 | echo "$suppress exists; not running." |
---|
53 | exit |
---|
54 | fi |
---|
55 | |
---|
56 | # Send an error report to $error_addr and suppress future runs until a |
---|
57 | # human has checked things out. This function does not remove the run |
---|
58 | # file and does not exit the script. |
---|
59 | error() { |
---|
60 | touch "$suppress" |
---|
61 | /usr/sbin/sendmail -t << EOM |
---|
62 | From: Autodebathenify <$error_addr> |
---|
63 | To: Autodebathenify <$error_addr> |
---|
64 | Subject: Autodebathenify failure: $1 |
---|
65 | |
---|
66 | Autodebathenify has experienced a failure. |
---|
67 | |
---|
68 | Please see $logfile on $(hostname) for more details. |
---|
69 | |
---|
70 | Autodebathenify will stop running until $suppress is removed. |
---|
71 | EOM |
---|
72 | } |
---|
73 | |
---|
74 | # Similar to error, but do not suppress future runs. |
---|
75 | error_transient() { |
---|
76 | /usr/sbin/sendmail -t << EOM |
---|
77 | From: Autodebathenify <$error_addr> |
---|
78 | To: Autodebathenify <$error_addr> |
---|
79 | Subject: Autodebathenify failure: $1 |
---|
80 | |
---|
81 | Autodebathenify has experienced a transient failure. |
---|
82 | |
---|
83 | Autodebathenify will continue running; investigation is not required |
---|
84 | unless this failure repeats itself. If that happens, see $logfile on |
---|
85 | $(hostname) for more details. |
---|
86 | EOM |
---|
87 | } |
---|
88 | |
---|
89 | # Exit handler. If a subsidiary command exited with status 3, we |
---|
90 | # treat it as a transient failure; otherwise we treat it as a failure |
---|
91 | # requiring human investigation. In either case, remove the run file. |
---|
92 | on_failure() { |
---|
93 | if [ 3 -eq "$?" ]; then |
---|
94 | error_transient "$state" |
---|
95 | else |
---|
96 | error "$state" |
---|
97 | fi |
---|
98 | rm -f "$runfile" |
---|
99 | } |
---|
100 | |
---|
101 | # Make sure runs of this script do not overlap. |
---|
102 | runfile=$HOME/autodebathenify.running |
---|
103 | if [ -e "$runfile" ]; then |
---|
104 | # A previous invocation appears to still be running. |
---|
105 | pid=$(cat "$runfile") |
---|
106 | if ! kill -0 "$pid" 2>/dev/null; then |
---|
107 | # Okay, it's not really running. Perhaps the machine has |
---|
108 | # rebooted. Wait for manual intervention before running again. |
---|
109 | error "Stale runfile" |
---|
110 | else |
---|
111 | echo "$runfile exists; not running." |
---|
112 | fi |
---|
113 | exit |
---|
114 | fi |
---|
115 | echo $$ > $runfile |
---|
116 | |
---|
117 | trap on_failure EXIT |
---|
118 | set -e |
---|
119 | |
---|
120 | # Acquire credentials. |
---|
121 | state="acquiring credentials" |
---|
122 | export KRB5CCNAME=$HOME/autodebathenify.ccache |
---|
123 | kinit -k -t "$HOME/keytab" daemon/$(hostname --fqdn) |
---|
124 | aklog |
---|
125 | |
---|
126 | export PATH=${scripts_dir}:$PATH |
---|
127 | . "$scripts_dir"/debian-versions.sh |
---|
128 | |
---|
129 | state="changing to build dir" |
---|
130 | cd "$build_dir" |
---|
131 | for pkg in $packages; do |
---|
132 | state="changing to $pkg in build dir" |
---|
133 | cd "$pkg" |
---|
134 | for code in $DEBIAN_CODES; do |
---|
135 | state="updating $pkg for $code" |
---|
136 | echo "--- Updating $pkg for $code (amd64) ---" |
---|
137 | ./debathenify-"$pkg" "$code"-amd64 -A source binary upload |
---|
138 | echo "--- Updating $pkg for $code (i386) ---" |
---|
139 | ./debathenify-"$pkg" "$code"-i386 source binary upload |
---|
140 | done |
---|
141 | cd .. |
---|
142 | done |
---|
143 | |
---|
144 | state="cleaning up" |
---|
145 | kdestroy |
---|
146 | rm -f "$runfile" |
---|
147 | trap '' EXIT |
---|