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 | echo "$suppress exists; not running." |
---|
40 | exit |
---|
41 | fi |
---|
42 | |
---|
43 | # Send an error report to $error_addr and suppress future runs until a |
---|
44 | # human has checked things out. This function does not remove the run |
---|
45 | # file and does not exit the script. |
---|
46 | error() { |
---|
47 | touch "$suppress" |
---|
48 | /usr/sbin/sendmail -t << EOM |
---|
49 | From: Autodebathenify <$error_addr> |
---|
50 | To: Autodebathenify <$error_addr> |
---|
51 | Subject: Autodebathenify failure: $1 |
---|
52 | |
---|
53 | Autodebathenify has experienced a failure. |
---|
54 | |
---|
55 | Please see $logfile on $(hostname) for more details. |
---|
56 | |
---|
57 | Autodebathenify will stop running until $suppress is removed. |
---|
58 | EOM |
---|
59 | } |
---|
60 | |
---|
61 | # Similar to error, but do not suppress future runs. |
---|
62 | error_transient() { |
---|
63 | /usr/sbin/sendmail -t << EOM |
---|
64 | From: Autodebathenify <$error_addr> |
---|
65 | To: Autodebathenify <$error_addr> |
---|
66 | Subject: Autodebathenify failure: $1 |
---|
67 | |
---|
68 | Autodebathenify has experienced a transient failure. |
---|
69 | |
---|
70 | Autodebathenify will continue running; investigation is not required |
---|
71 | unless this failure repeats itself. If that happens, see $logfile on |
---|
72 | $(hostname) for more details. |
---|
73 | EOM |
---|
74 | } |
---|
75 | |
---|
76 | # Exit handler. If a subsidiary command exited with status 3, we |
---|
77 | # treat it as a transient failure; otherwise we treat it as a failure |
---|
78 | # requiring human investigation. In either case, remove the run file. |
---|
79 | on_failure() { |
---|
80 | if [ 3 -eq "$?" ]; then |
---|
81 | error_transient "$state" |
---|
82 | else |
---|
83 | error "$state" |
---|
84 | fi |
---|
85 | rm -f "$runfile" |
---|
86 | } |
---|
87 | |
---|
88 | # Make sure runs of this script do not overlap. |
---|
89 | runfile=$HOME/autodebathenify.running |
---|
90 | if [ -e "$runfile" ]; then |
---|
91 | # A previous invocation appears to still be running. |
---|
92 | pid=$(cat "$runfile") |
---|
93 | if ! kill -0 "$pid" 2>/dev/null; then |
---|
94 | # Okay, it's not really running. Perhaps the machine has |
---|
95 | # rebooted. Wait for manual intervention before running again. |
---|
96 | error "Stale runfile" |
---|
97 | else |
---|
98 | echo "$runfile exists; not running." |
---|
99 | fi |
---|
100 | exit |
---|
101 | fi |
---|
102 | echo $$ > $runfile |
---|
103 | |
---|
104 | trap on_failure EXIT |
---|
105 | set -e |
---|
106 | |
---|
107 | # Acquire credentials. |
---|
108 | state="acquiring credentials" |
---|
109 | export KRB5CCNAME=$HOME/autodebathenify.ccache |
---|
110 | kinit -k -t "$HOME/keytab" daemon/$(hostname --fqdn) |
---|
111 | aklog |
---|
112 | |
---|
113 | export PATH=${scripts_dir}:$PATH |
---|
114 | . "$scripts_dir"/debian-versions.sh |
---|
115 | |
---|
116 | state="changing to build dir" |
---|
117 | cd "$build_dir" |
---|
118 | for pkg in $packages; do |
---|
119 | state="changing to $pkg in build dir" |
---|
120 | cd "$pkg" |
---|
121 | for code in $DEBIAN_CODES; do |
---|
122 | state="updating $pkg for $code" |
---|
123 | echo "--- Updating $pkg for $code (amd64) ---" |
---|
124 | ./debathenify-"$pkg" "$code"-amd64 -A source binary upload |
---|
125 | echo "--- Updating $pkg for $code (i386) ---" |
---|
126 | ./debathenify-"$pkg" "$code"-i386 source binary upload |
---|
127 | done |
---|
128 | cd .. |
---|
129 | done |
---|
130 | |
---|
131 | state="cleaning up" |
---|
132 | kdestroy |
---|
133 | rm -f "$runfile" |
---|
134 | trap '' EXIT |
---|