Use /bin/bash for the broken_report.sh script (not /bin/sh whish is dash)
[amitay/build-farm.git] / broken_report.sh
1 #!/bin/bash
2 # This shell script produces an email comparing current broken status
3 # to that of the last run of this script
4 #
5 # Copyright (C) Vance Lankhaar  <vance@samba.org>      2005
6 #
7 #   This program is free software; you can redistribute it and/or modify
8 #   it under the terms of the GNU General Public License as published by
9 #   the Free Software Foundation; either version 2 of the License, or
10 #   (at your option) any later version.
11 #   
12 #   This program is distributed in the hope that it will be useful,
13 #   but WITHOUT ANY WARRANTY; without even the implied warranty of
14 #   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15 #   GNU General Public License for more details.
16 #   
17 #   You should have received a copy of the GNU General Public License
18 #   along with this program; if not, write to the Free Software
19 #   Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
20
21 BASEDIR="/home/build/master"
22 RESULT_CACHE="$BASEDIR/cache/broken_results.txt"
23 RESULT_URL="http://build.samba.org/?function=Text_Summary"
24 FULL_RESULT_URL="http://build.samba.org/"
25
26 # split into two parts to obfuscate from spam engines
27 RESULT_EMAIL_USER="samba-cvs"
28 RESULT_EMAIL_DOMAIN="lists.samba.org"
29
30 # End editable variables
31 ##################################################
32
33
34 # exit immediately on failure
35 set -e
36
37 # fail on attempting to use unassigned variable
38 set -u
39
40 # clean up environment a little
41 PATH="/usr/local/bin:/usr/bin:/bin"
42 IFS="   
43 "
44
45
46 ##################################################
47 # get the broken report from the build farm
48 function get_results {
49     
50     rm -f "$RESULT_CACHE"
51
52     wget --output-file="$RESULT_CACHE".log \
53          --output-document="$RESULT_CACHE" \
54          "$RESULT_URL"
55
56     if [ $? -ne 0 ]; then
57         echo "Could not retrieve results:" >&2
58         cat "$RESULT_CACHE".log >&2
59         return 1;
60     else
61         rm -rf "$RESULT_CACHE".log
62     fi
63 }
64
65 ##################################################
66 # compare the results of build 
67 function compare_results {
68
69     if [ ! -e "$RESULT_CACHE" ]; then
70         echo "Could not locate currrent results. $RESULT_CACHE not found" >&2
71         return 1
72     fi
73
74     if [ ! -e "$RESULT_CACHE".old ]; then
75         echo "Could not locate old results. $RESULT_CACHE.old not found" >&2
76         return 1
77     fi
78     
79     diff -u "$RESULT_CACHE".old "$RESULT_CACHE" >> "$RESULT_CACHE".report || true
80     
81     return 0
82 }
83
84 ##################################################
85 # send report to the above-set email address
86 function send_report {
87
88     if [ ! -e "$RESULT_CACHE".report ]; then
89         echo "Results ($RESULT_CACHE.report) not found" >&2
90         return 1
91     fi
92
93     subject=$1
94     if [ x"$subject" = x ]; then 
95         subject="Build Farm Status"
96     fi
97
98     mail -s "$subject" "$RESULT_EMAIL_USER"@"$RESULT_EMAIL_DOMAIN" < "$RESULT_CACHE".report
99 }
100
101 ##################################################
102 ##################################################
103
104 if [ $# -gt 0 ]; then
105     if [ x"$1" = x"-h" ] || [ x"$1" = x"--help" ]; then
106         echo "Usage: broken_report.sh"
107         echo
108         echo "This shell script produces an email comparing current broken"
109         echo "status to that of the last run of this script."
110         echo
111         echo "The source and destination are configurable within the script."
112         exit 1
113     fi
114 fi
115     
116
117 ##################################################
118 # prepares the report, including subject and "blurb"
119 rm -f "$RESULT_CACHE".report "$RESULT_CACHE".old
120
121 if [ -e "$RESULT_CACHE" ]; then
122     mv -f "$RESULT_CACHE" "$RESULT_CACHE".old
123 else 
124     # get results if they don't exist
125     get_results
126     exit $?
127 fi
128     
129 get_results
130
131 if [ $? -ne 0 ]; then
132     echo "Failed to get the results. Bailing." >&2
133         
134     # remove any new results, as they're almost certainly foul
135     rm -f "$RESULT_CACHE"
136     mv -f "$RESULT_CACHE".old "$RESULT_CACHE"
137
138     exit 1
139 fi
140     
141
142 (
143     # set report subject to show updated time
144     echo "URL: $FULL_RESULT_URL"
145     echo
146 ) > "$RESULT_CACHE".report
147
148 compare_results
149
150 if [ $? -ne 0 ]; then
151     echo "Failed to compare results. Bailing." >&2
152
153     exit 1
154 fi
155
156 subject=$(head -n 1 "$RESULT_CACHE")
157 send_report "$subject"
158