3a15e883ca5d63070014862da8be58cd2f0b2d87
[spamassassin_config.git] / update_spamassassin
1 #!/bin/sh
2
3 # This command updates the spamassassin configuration; tests the new
4 # configuration; then moves the updated configuration in place
5
6 set -e
7
8 SVN=$(which svn)
9
10 TMPDIR=$(mktemp -d);
11
12 HOSTNAME=$(hostname);
13
14 if [ "$HOSTNAME" = "rietz" ] || [ "$HOSTNAME" = "busoni" ]; then
15     BASEDIR="/org/bugs.debian.org"
16     USERCONF="bugs/user_prefs";
17 elif [ "$HOSTNAME" = "liszt" ]; then
18     BASEDIR="/var/list/.etc";
19     USERCONF="lists/user_prefs";
20 else
21     echo "Unknown hostname '$HOSTNAME'";
22     exit 1;
23 fi;
24
25 SACONFIG="$BASEDIR/spamassassin_config";
26 BAKDIR="$BASEDIR/spamassassin_config_bak";
27
28 # because alioth continuously fails, make sure that svn info will
29 # succeed first before cluttering the log. Ideally we would report
30 # this error, but it happens far too frequently, so we bail out here.
31 if ! svn info $(svn info $SACONFIG|awk '/^URL:/{print $2}') >/dev/null 2>&1; then
32     exit 0;
33 fi;
34
35 # figure out if there's an update to be made
36 ORIGREV=$(svn info $SACONFIG |awk '/^Revision:/{print $2}')
37 NEWREV=$(svn info $(svn info $SACONFIG|awk '/^URL:/{print $2}')|awk '/^Revision:/{print $2}')
38
39 # no changes, bug out.
40 if [ "0$ORIGREV" -ge "0$NEWREV" ]; then
41     exit 0;
42 fi;
43
44 if [ -e "$SACONFIG/.update_spamassassin" ] && kill -0 $(cat "$SACONFIG/.update_spamassassin") >/dev/null 2>&1; then
45     echo "Another update_spamassassin appears to be running"
46     exit 1;
47 else
48     echo $$ > "$SACONFIG/.update_spamassassin";
49 fi;
50
51 remove_pidfile () {
52     rm "$SACONFIG/.update_spamassassin";
53 }
54
55 trap remove_pidfile 0;
56
57 FAILUREMESSAGE="";
58 report_failure () {
59     # Hrm; a previous update failed; don't report again until the file
60     # is removed or it's more than a day old
61     if [ -e "$SACONFIG/failed_update" ] &&
62         [ $(($(date +%s) - $(stat -c '%Y' "$SACONFIG/failed_update"))) -lt 86400 ];  then
63         rm -rf "$TMPDIR"
64         exit 0;
65     fi;
66     echo "$FAILUREMESSAGE" > "$SACONFIG/failed_update"
67     echo "$FAILUREMESSAGE";
68     cat - <<EOF;
69
70 update_spamassassin will only report this error once a day, and
71 updates will not occur until this is fixed.
72
73 Please fix the problem and then remove
74 $SACONFIG/failed_update
75 EOF
76     rm -rf "$TMPDIR"
77     exit 1;
78 }
79
80 cp -ar $SACONFIG $TMPDIR/.
81 OLDDIR="$(pwd)"
82 cd $TMPDIR/spamassassin_config;
83 svn update -q --non-interactive; 
84 if svn status |grep -q '^C'; then
85     FAILUREMESSAGE=$'The following conflicts were found\n\n'
86     FAILUREMESSAGE="$FAILUREMESSAGE$(svn status|grep ^C)";
87     report_failure
88 fi;
89 cd "$(pwd)"
90
91 if ! spamassassin -p $TMPDIR/spamassassin_config/$USERCONF --lint > $TMPDIR/sa_lint 2>&1; then
92     FAILUREMESSAGE="$(echo -e 'spamassassin -p $TMPDIR/spamassassin_config/$USERCONF --lint\nfailed with\n'| cat - $TMPDIR/sa_lint)";
93     report_failure
94 fi;
95 mv "$SACONFIG" "$BAKDIR/spamassassin_config_r$ORIGREV"
96 mv "$TMPDIR/spamassassin_config" "$SACONFIG"
97 trap - ERR
98 remove_pidfile
99 rm -rf "$TMPDIR"
100
101 if [ "$HOSTNAME" == "rietz" ]; then
102     touch /home/debbugs/.spamassassin/user_prefs
103 elif [ "$HOSTNAME" == "liszt" ]; then
104     # do nothing
105     true;
106 else
107     echo "Unknown hostname '$HOSTNAME'";
108     exit 1;
109 fi;
110
111
112 exit 0;