blob: 2f553ea9d18ca058bf0f4112676aa72c8997236a [file] [log] [blame]
Adhemerval Zanellad3e8c482020-10-12 11:31:48 -03001#!/usr/bin/env python3
2
3# This script greps the JSON files for the buildbots on the LLVM official
4# build master by name and prints an HTML page with the links to the bots
5# and the status.
6#
7# Multiple masters can be used, as well as multiple groups of bots and
8# multiple bots per group, all in a json file. See linaro.json in this
9# repository to have an idea how the config file is.
10
11import sys
12import os
13import argparse
14import json
15import tempfile
16import logging
David Spickettaa155be2021-02-25 14:30:09 +000017import shutil
Adhemerval Zanellad3e8c482020-10-12 11:31:48 -030018from datetime import datetime, timedelta
19# The requests allows HTTP keep-alive which re-uses the same TCP connection
20# to download multiple files.
21import requests
David Spickett55449c62021-12-13 12:57:33 +000022from textwrap import dedent
Adhemerval Zanellad3e8c482020-10-12 11:31:48 -030023
David Spickett30a986f2021-04-29 09:37:00 +010024from buildkite_status import get_buildkite_bots_status
25
Adhemerval Zanellad3e8c482020-10-12 11:31:48 -030026# The GIT revision length used on 'Commits' error display.
27GIT_SHORT_LEN=7
28
29def ignored(s):
30 return 'ignore' in s and s['ignore']
31def not_ignored(s):
32 return not ignored(s)
33
34
David Spickette88fe592021-03-22 12:25:13 +000035# Returns the parsed json URL or raises an exception
Adhemerval Zanellad3e8c482020-10-12 11:31:48 -030036def wget(session, url):
David Spickett8306ed82021-12-06 10:40:36 +000037 got = session.get(url)
38 got.raise_for_status()
39 return got.json()
Adhemerval Zanellad3e8c482020-10-12 11:31:48 -030040
41
42# Returns a string with the GIT revision usesd on build BUILDID and
43# PREV_BUILDID in the form '<id_buildid>-<id_prev_buildid>'.
44def get_bot_failure_changes(session, base_url, buildid, prev_buildid):
45 def wget_build_rev(bid):
David Spickette88fe592021-03-22 12:25:13 +000046 try:
47 contents = wget(session,
48 "{}/api/v2/builds/{}/changes"
49 .format(base_url, bid))
50 except requests.exceptions.RequestException:
David Spickett8306ed82021-12-06 10:40:36 +000051 logging.debug(" Couldn't get changes for build {}!".format(buildid))
David Spickett7f18f4d2021-03-22 11:49:17 +000052 return None
David Spickette88fe592021-03-22 12:25:13 +000053 changes = contents['changes']
54 if changes:
55 return changes[0]['revision']
56 return None
Adhemerval Zanellad3e8c482020-10-12 11:31:48 -030057
David Spickett7f18f4d2021-03-22 11:49:17 +000058 revision = wget_build_rev(buildid)[:GIT_SHORT_LEN]
59 prev_revision = None
60 if prev_buildid is not None:
61 prev_revision = wget_build_rev(prev_buildid)
62
63 if prev_revision is None:
64 return "{}".format(revision)
Adhemerval Zanellad3e8c482020-10-12 11:31:48 -030065 else:
David Spickett7f18f4d2021-03-22 11:49:17 +000066 return "{}-{}".format(revision, prev_revision[:GIT_SHORT_LEN])
Adhemerval Zanellad3e8c482020-10-12 11:31:48 -030067
68
Oliver Stannard91688ff2021-01-07 10:27:27 +000069# Map from buildbot status codes we want to treat as errors to the color they
70# should be shown in. The codes are documented at
71# https://docs.buildbot.net/latest/developer/results.html#build-result-codes,
72# and these colors match the suggested ones there.
73RESULT_COLORS = {
74 2: 'red', # Error
75 4: 'purple', # Exception
76 5: 'purple', # Retry
77 6: 'pink', # Cancelled
78}
79
80def get_bot_failing_steps(session, base_url, buildid):
David Spickette88fe592021-03-22 12:25:13 +000081 try:
82 contents = wget(session, "{}/api/v2/builds/{}/steps"
83 .format(base_url, buildid))
84 except requests.exceptions.RequestException:
Oliver Stannard91688ff2021-01-07 10:27:27 +000085 return ""
David Spickette88fe592021-03-22 12:25:13 +000086
Oliver Stannard91688ff2021-01-07 10:27:27 +000087 for step in contents["steps"]:
David Spickett7f18f4d2021-03-22 11:49:17 +000088 if step["results"] in RESULT_COLORS:
Oliver Stannard91688ff2021-01-07 10:27:27 +000089 yield (step["name"], step["results"])
90
91
Adhemerval Zanellad3e8c482020-10-12 11:31:48 -030092# Get the status of a individual bot BOT. Returns a dict with the
93# information.
94def get_bot_status(session, bot, base_url, builder_url, build_url):
David Spickette88fe592021-03-22 12:25:13 +000095 try:
96 builds = wget(session,
97 "{}/api/v2/{}/{}/{}"
98 .format(base_url, builder_url, bot, build_url))
99 except requests.exceptions.RequestException as e:
100 return {'fail': True}
Adhemerval Zanellad3e8c482020-10-12 11:31:48 -0300101
Oliver Stannard46e99032021-01-05 10:30:56 +0000102 reversed_builds = iter(sorted(builds['builds'], key=lambda b: -b["number"]))
Adhemerval Zanellad3e8c482020-10-12 11:31:48 -0300103 for build in reversed_builds:
104 if build['complete']:
Maxim Kuvyrkovfdaa4682021-04-14 13:13:14 +0000105 time_since = (int(datetime.now().timestamp()) - int(build['complete_at']))
106 duration = int(build['complete_at']) - int(build['started_at'])
David Spickett30a986f2021-04-29 09:37:00 +0100107 agent_url = "{}/#/{}/{}".format(base_url, builder_url, build['builderid'])
108
David Spickett7f18f4d2021-03-22 11:49:17 +0000109 status = {
David Spickett30a986f2021-04-29 09:37:00 +0100110 'builder_url': agent_url,
David Spickett7f18f4d2021-03-22 11:49:17 +0000111 'number': build['number'],
David Spickett30a986f2021-04-29 09:37:00 +0100112 'build_url': "{}/builds/{}".format(agent_url, build['number']),
David Spickett7f18f4d2021-03-22 11:49:17 +0000113 'state': build['state_string'],
Maxim Kuvyrkovfdaa4682021-04-14 13:13:14 +0000114 'time_since': timedelta(seconds=time_since),
115 'duration': timedelta(seconds=duration),
David Spickett7f18f4d2021-03-22 11:49:17 +0000116 'fail': build['state_string'] != 'build successful',
117 }
David Spickett30a986f2021-04-29 09:37:00 +0100118
David Spickett7f18f4d2021-03-22 11:49:17 +0000119 if status['fail']:
120 buildid = build['buildid']
121 prev_buildid = next(reversed_builds, None)['buildid']
Adhemerval Zanellad3e8c482020-10-12 11:31:48 -0300122 status['changes'] = get_bot_failure_changes(session, base_url,
David Spickett7f18f4d2021-03-22 11:49:17 +0000123 buildid,
Adhemerval Zanellad3e8c482020-10-12 11:31:48 -0300124 prev_buildid)
Oliver Stannard91688ff2021-01-07 10:27:27 +0000125 status['steps'] = list(get_bot_failing_steps(session, base_url,
David Spickett7f18f4d2021-03-22 11:49:17 +0000126 buildid))
127
128 return status
Adhemerval Zanellad3e8c482020-10-12 11:31:48 -0300129
130
David Spickett85355fa2021-03-22 15:41:41 +0000131# Get status for all bots named in the config
132# Return a dictionary of (base_url, bot name) -> status info
David Spickett30a986f2021-04-29 09:37:00 +0100133def get_buildbot_bots_status(config):
Adhemerval Zanellad3e8c482020-10-12 11:31:48 -0300134 session = requests.Session()
Adhemerval Zanellad3e8c482020-10-12 11:31:48 -0300135 bot_cache = {}
David Spickettf006c372021-03-22 12:54:12 +0000136
Adhemerval Zanellad3e8c482020-10-12 11:31:48 -0300137 for server in filter(not_ignored, config):
David Spickett30a986f2021-04-29 09:37:00 +0100138 if server['name'] == "Buildkite":
139 continue
140
Adhemerval Zanellad3e8c482020-10-12 11:31:48 -0300141 base_url = server['base_url']
Adhemerval Zanellad3e8c482020-10-12 11:31:48 -0300142 logging.debug('Parsing server {}...'.format(server['name']))
143 for builder in server['builders']:
144 logging.debug(' Parsing builders {}...'.format(builder['name']))
145 for bot in builder['bots']:
David Spickett85355fa2021-03-22 15:41:41 +0000146 bot_key = (base_url, bot['name'])
Adhemerval Zanellad3e8c482020-10-12 11:31:48 -0300147 if bot_key in bot_cache:
148 continue
David Spickett85355fa2021-03-22 15:41:41 +0000149
Adhemerval Zanellad3e8c482020-10-12 11:31:48 -0300150 logging.debug(' Parsing bot {}...'.format(bot['name']))
David Spickett85355fa2021-03-22 15:41:41 +0000151 status = get_bot_status(session, bot['name'], base_url, server['builder_url'],
152 server['build_url'])
David Spickettf2c82dd2021-06-24 10:01:33 +0100153 if status is not None:
David Spickett8306ed82021-12-06 10:40:36 +0000154 logging.debug(" Bot status: " + ("FAIL" if status['fail'] else "PASS"))
David Spickettf2c82dd2021-06-24 10:01:33 +0100155 bot_cache[bot_key] = status
Adhemerval Zanellad3e8c482020-10-12 11:31:48 -0300156
David Spickett85355fa2021-03-22 15:41:41 +0000157 return bot_cache
158
159def write_bot_status(config, output_file, bots_status):
160 temp = tempfile.NamedTemporaryFile(mode='w+', delete=False)
161 today = "{}\n".format(datetime.today().ctime())
162 # Whether we use the fail favicon or not
163 found_failure = False
David Spickettf006c372021-03-22 12:54:12 +0000164
David Spickett55449c62021-12-13 12:57:33 +0000165 temp.write(dedent("""\
166 <style>
167 /* Combine the border between cells to prevent 1px gaps
168 in the row background colour. */
169 table, td, th {
170 border-collapse: collapse;
171 }
172 /* Colour every other row in a table body grey. */
173 tbody tr:nth-child(even) td {
174 background-color: #ededed;
175 }
176 </style>"""))
177
Adhemerval Zanellad3e8c482020-10-12 11:31:48 -0300178 # Dump all servers / bots
179 for server in filter(not_ignored, config):
180 base_url = server['base_url']
181 builder_url = server['builder_url']
182 build_url = server['build_url']
David Spickettec94cc22021-12-13 13:16:05 +0000183
184 column_titles = [
185 "Buildbot",
186 "Status",
187 "T Since",
188 "Duration",
189 "Build #",
190 "Commits",
191 "Failing steps"
192 ]
193 num_columns = len(column_titles)
194 column_titles_html = "<tr>{}</tr>\n".format(
195 "".join(["<th>{}</th>".format(t) for t in column_titles]))
196
David Spickett55449c62021-12-13 12:57:33 +0000197 temp.write("<table border=0 cellspacing=1 cellpadding=2>\n")
David Spickettec94cc22021-12-13 13:16:05 +0000198 temp.write("<tr><td colspan={}>&nbsp;</td><tr>\n".format(num_columns))
199 temp.write("<tr><th colspan={}>{} @ {}</td><tr>\n"
200 .format(num_columns, server['name'], today))
Adhemerval Zanellad3e8c482020-10-12 11:31:48 -0300201
202 for builder in server['builders']:
David Spickettec94cc22021-12-13 13:16:05 +0000203 temp.write("<tr><td colspan={}>&nbsp;</td><tr>\n".format(num_columns))
204 temp.write("<tr><th colspan={}>{}</th><tr>\n".format(num_columns, builder['name']))
205 temp.write(column_titles_html)
David Spickett55449c62021-12-13 12:57:33 +0000206 temp.write("<tbody>\n")
Adhemerval Zanellad3e8c482020-10-12 11:31:48 -0300207 for bot in builder['bots']:
208 temp.write("<tr>\n")
David Spickettf2c82dd2021-06-24 10:01:33 +0100209 try:
210 status = bots_status[(base_url, bot['name'])]
211 except KeyError:
David Spickettec94cc22021-12-13 13:16:05 +0000212 temp.write(" <td colspan={}>{} is offline!</td>\n</tr>\n"
213 .format(num_columns, bot['name']))
David Spickettf2c82dd2021-06-24 10:01:33 +0100214 continue
David Spickett30a986f2021-04-29 09:37:00 +0100215 else:
216 if not status.get('valid', True):
David Spickettec94cc22021-12-13 13:16:05 +0000217 temp.write(" <td colspan={}>Could not read status for {}!</td>\n</tr>\n"
218 .format(num_columns, bot['name']))
David Spickett30a986f2021-04-29 09:37:00 +0100219 continue
David Spickettf2c82dd2021-06-24 10:01:33 +0100220
David Spickett85355fa2021-03-22 15:41:41 +0000221 found_failure |= status['fail']
David Spickett30a986f2021-04-29 09:37:00 +0100222
223 temp.write(" <td><a href='{}'>{}</a></td>\n".format(
224 status['builder_url'], bot['name']))
Adhemerval Zanellad3e8c482020-10-12 11:31:48 -0300225 temp.write(" <td><font color='{}'>{}</font></td>\n"
226 .format('red' if status['fail'] else 'green',
227 'FAIL' if status['fail'] else 'PASS'))
228 empty_cell=" <td>&nbsp;</td>\n"
Maxim Kuvyrkovfdaa4682021-04-14 13:13:14 +0000229 if 'time_since' in status:
David Spickett187f7962022-02-09 12:35:00 +0000230 time_since = status['time_since']
231 # No build should be taking more than a day
232 if time_since > timedelta(hours=24):
233 time_since = "<p style=\"color:red\">{}</p>".format(
234 time_since)
235 else:
236 time_since = str(time_since)
237
238 temp.write(" <td>{}</td>\n".format(time_since))
Maxim Kuvyrkovfdaa4682021-04-14 13:13:14 +0000239 else:
240 temp.write(empty_cell)
241 if 'duration' in status:
242 temp.write(" <td>{}</td>\n".format(status['duration']))
Adhemerval Zanellad3e8c482020-10-12 11:31:48 -0300243 else:
244 temp.write(empty_cell)
245 if 'number' in status:
David Spickett30a986f2021-04-29 09:37:00 +0100246 temp.write(" <td><a href='{}'>{}</a></td>\n".format(
247 status['build_url'], status['number']))
Adhemerval Zanellad3e8c482020-10-12 11:31:48 -0300248 else:
249 temp.write(empty_cell)
250 if 'changes' in status:
251 temp.write(" <td>{}</td>\n".format(status['changes']))
252 else:
253 temp.write(empty_cell)
Oliver Stannard91688ff2021-01-07 10:27:27 +0000254 if 'steps' in status and status['steps']:
255 def render_step(name, result):
256 return "<font color='{}'>{}</font>".format(RESULT_COLORS[result], name)
257 step_list = ', '.join(render_step(name, result) for name, result in status['steps'])
258 temp.write(" <td style=\"text-align:center\">{}</td>\n".format(step_list))
Adhemerval Zanellad3e8c482020-10-12 11:31:48 -0300259 else:
260 temp.write(empty_cell)
261 temp.write("</tr>\n")
David Spickett55449c62021-12-13 12:57:33 +0000262 temp.write("</tbody>\n")
Adhemerval Zanellad3e8c482020-10-12 11:31:48 -0300263 temp.write("</table>\n")
264
David Spickett85355fa2021-03-22 15:41:41 +0000265 temp.write("<link rel=\"shortcut icon\" href=\"{}\" "
266 "type=\"image/x-icon\"/>\n".format(
267 'fail.ico' if found_failure else 'ok.ico'))
268
Adhemerval Zanellad3e8c482020-10-12 11:31:48 -0300269 # Move temp to main (atomic change)
270 temp.close()
David Spickett7f18f4d2021-03-22 11:49:17 +0000271 shutil.move(temp.name, output_file)
Adhemerval Zanellad3e8c482020-10-12 11:31:48 -0300272
273
274if __name__ == "__main__":
275 parser = argparse.ArgumentParser()
David Spickettec2166c2021-07-19 14:17:29 +0100276 parser.add_argument('-d', dest='debug', action='store_true',
277 help='show debug log messages')
Adhemerval Zanellad3e8c482020-10-12 11:31:48 -0300278 parser.add_argument('config_file',
279 help='Bots description in JSON format')
280 parser.add_argument('output_file',
281 help='output HTML path')
282 args = parser.parse_args()
283
284 if args.debug:
285 logging.basicConfig(stream=sys.stderr, level=logging.DEBUG)
286
287 try:
288 with open(args.config_file, "r") as f:
289 config = json.load(f)
290 except IOError as e:
David Spickett7f18f4d2021-03-22 11:49:17 +0000291 print("error: failed to read {} config file: {}".format(args.config_file, e))
Adhemerval Zanellad3e8c482020-10-12 11:31:48 -0300292 sys.exit(os.EX_CONFIG)
293
David Spickett30a986f2021-04-29 09:37:00 +0100294 status = get_buildbot_bots_status(config)
295 status.update(get_buildkite_bots_status(config))
296 write_bot_status(config, args.output_file, status)