summaryrefslogtreecommitdiff
path: root/extensions/mason/mason-report.sh
blob: f6cca0ef5406c65c5b3cadc8c3201705f7851866 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
#!/bin/bash

set -x

. /etc/mason.conf

REPORT_PATH=/var/mason/report.html
SERVER_PATH=/srv/mason
SERVER_REPORT_PATH="$SERVER_PATH/index.html"

sed_escape() {
    printf "%s\n" "$1" | sed -e 's/\W/\\&/g'
}

create_report() {
cat > $REPORT_PATH <<'EOF'
<html>
<head>
<meta charset="UTF-8">
<meta http-equiv="refresh" content="60">
<style>
html, body {
	margin: 0;
	padding: 0;
}
p.branding {
	background: black;
	color: #fff;
	padding: 0.4em;
	margin: 0;
	font-weight: bold;
}
h1 {
	background: #225588;
	color: white;
	margin: 0;
	padding: 0.6em;
}
table {
	width: 90%;
	margin: 1em auto 6em auto;
	border: 1px solid black;
	border-spacing: 0;
}
table tr.headings {
	background: #555;
	color: white;
}
table tr.pass {
	background: #aaffaa;
}
table tr.pass:hover {
	background: #bbffbb;
}
table tr.fail {
	background: #ffaaaa;
}
table tr.fail:hover {
	background: #ffbbbb;
}
table tr.nonet {
	background: #ffdd99;
}
table tr.nonet:hover {
	background: #ffeeaa;
}
table tr.progress {
	background: #00CCFF;
}
table tr.progress:hover {
	background: #91E9FF;
}
table tr.headings th {
	font-weight: bold;
	text-align: left;
	padding: 3px 2px;
}
table td {
	padding: 2px;
}
td.result {
	font-weight: bold;
	text-transform: uppercase;
}
td.result a {
	text-decoration: none;
}
td.result a:before {
	content: "➫ ";
}
tr.pass td.result a {
	color: #252;
}
tr.pass td.result a:hover {
	color: #373;
}
tr.fail td.result a {
	color: #622;
}
tr.fail td.result a:hover {
	color: #933;
}
tr.nonet td.result a {
	color: #641;
}
tr.nonet td.result a:hover {
	color: #962;
}
tr.progress td.result a {
	color: #000066;
}
tr.progress td.result a:hover {
	color: #0000CC;
}
td.ref {
	font-family: monospace;
}
td.ref a {
	color: #333;
}
td.ref a:hover {
	color: #555;
}
table tr.pass td, table tr.fail td {
	border-top: solid white 1px;
}
p {
	margin: 1.3em;
}
code {
	padding: 0.3em 0.5em;
	background: #eee;
	border: 1px solid #bbb;
	border-radius: 1em;
}
#footer {
	margin: 0;
	background: #aaa;
	color: #222;
	border-top: #888 1px solid;
	font-size: 80%;
	padding: 0;
	position: fixed;
	bottom: 0;
	width: 100%;
	display: table;
}
#footer p {
	padding: 1.3em;
	display: table-cell;
}
#footer p code {
	font-size: 110%;
}
#footer p.about {
	text-align: right;
}
</style>
</head>
<body>
<p class="branding">Mason</p>
<h1>Baserock: Continuous Delivery</h1>
<p>Build log of changes to <code>BRANCH</code> from <code>TROVE</code>.  Most recent first.</p>
<table>
<tr class="headings">
	<th>Started</th>
	<th>Ref</th>
	<th>Duration</th>
	<th>Result</th>
</tr>
<!--INSERTION POINT-->
</table>
<div id="footer">
<p>Last checked for updates at: <code>....-..-.. ..:..:..</code></p>
<p class="about">Generated by Mason | Powered by Baserock</p>
</div>
</body>
</html>
EOF

    sed -i 's/BRANCH/'"$(sed_escape "$1")"'/' $REPORT_PATH
    sed -i 's/TROVE/'"$(sed_escape "$2")"'/' $REPORT_PATH
}

update_report() {
    # Give function params sensible names
    build_start_time="$1"
    build_trove_host="$2"
    build_ref="$3"
    build_sha1="$4"
    build_duration="$5"
    build_result="$6"
    report_path="$7"
    build_log="$8"

    # Generate template if report file is not there
    if [ ! -f $REPORT_PATH ]; then
        create_report $build_ref $build_trove_host
    fi

    # Build table row for insertion into report file
    if [ "$build_result" = nonet ]; then
        msg='<tr class="'"${build_result}"'"><td>'"${build_start_time}"'</td><td class="ref">Failed to contact '"${build_trove_host}"'</a></td><td>'"${build_duration}s"'</td><td class="result"><a href="'"${build_log}"'">'"${build_result}"'</a></td></tr>'
    else
        msg='<tr class="'"${build_result}"'"><td>'"${build_start_time}"'</td><td class="ref"><a href="http://'"${build_trove_host}"'/cgi-bin/cgit.cgi/baserock/baserock/definitions.git/commit/?h='"${build_ref}"'&id='"${build_sha1}"'">'"${build_sha1}"'</a></td><td>'"${build_duration}s"'</td><td class="result"><a href="'"${build_log}"'">'"${build_result}"'</a></td></tr>'
    fi

    # Insert report line, newest at top
    sed -i 's/<!--INSERTION POINT-->/<!--INSERTION POINT-->\n'"$(sed_escape "$msg")"'/' $report_path
}

update_report_time() {
    # Give function params sensible names
    build_start_time="$1"

    # If the report file exists, update the last-checked-for-updates time
    if [ -f $REPORT_PATH ]; then
        sed -i 's/<code>....-..-.. ..:..:..<\/code>/<code>'"$(sed_escape "$build_start_time")"'<\/code>/' $REPORT_PATH
    fi
}

START_TIME=`date +%Y-%m-%d\ %T`

update_report_time "$START_TIME"
cp "$REPORT_PATH" "$SERVER_PATH/index.html"

logfile="$(mktemp)"

#Update current.log symlink to point to the current build log
ln -sf "$logfile" "$SERVER_PATH"/current.log

#Copy current server report, to restore when result is "skip"
cp "$SERVER_REPORT_PATH" "$SERVER_REPORT_PATH".bak

update_report "$START_TIME" \
              "$UPSTREAM_TROVE_ADDRESS" \
              "$DEFINITIONS_REF" \
              "" \
              " - " \
              "progress" \
              "$SERVER_REPORT_PATH" \
              "current.log"


/usr/lib/mason/mason.sh 2>&1 | tee "$logfile"
case "${PIPESTATUS[0]}" in
0)
    RESULT=pass
    ;;
33)
    RESULT=skip
    ;;
42)
    RESULT=nonet
    ;;
*)
    RESULT=fail
    ;;
esac

# TODO: Update page with last executed time
if [ "$RESULT" = skip ]; then
    # Restore copied server report, otherwise the 'progress' row will
    # be still present with a broken link after we remove the $logfile
    mv "$SERVER_REPORT_PATH".bak "$SERVER_REPORT_PATH"

    rm "$logfile"
    exit 0
fi

DURATION=$(( $(date +%s) - $(date --date="$START_TIME" +%s) ))
SHA1="$(cd "/ws/mason-definitions-$DEFINITIONS_REF" && git rev-parse HEAD)"
BUILD_LOG="log/${SHA1}--${START_TIME}.log"

update_report "$START_TIME" \
              "$UPSTREAM_TROVE_ADDRESS" \
              "$DEFINITIONS_REF" \
              "$SHA1" \
              "$DURATION" \
              "$RESULT" \
              "$REPORT_PATH" \
              "$BUILD_LOG"


#
# Copy report into server directory
#

cp "$REPORT_PATH" "$SERVER_REPORT_PATH"
mkdir "$SERVER_PATH/log"
mv "$logfile" "$SERVER_PATH/$BUILD_LOG"

# Cleanup

mkdir -p /srv/distbuild/remove
find /srv/distbuild/ -not \( -name "remove" -o -name "trees.cache.pickle" \) -mindepth 1 -maxdepth 1 -exec  mv '{}' /srv/distbuild/remove \;
find /srv/distbuild/remove -delete