blob: be7851cb773ed9aa129a860d707087c360c30837 [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 Spickett55449c62021-12-13 12:57:33 +0000183 temp.write("<table border=0 cellspacing=1 cellpadding=2>\n")
184 temp.write("<tr><td colspan=7>&nbsp;</td><tr>\n")
185 temp.write("<tr><th colspan=7>{} @ {}</td><tr>\n"
Adhemerval Zanellad3e8c482020-10-12 11:31:48 -0300186 .format(server['name'], today))
187
188 for builder in server['builders']:
David Spickett55449c62021-12-13 12:57:33 +0000189 temp.write("<tr><td colspan=7>&nbsp;</td><tr>\n")
190 temp.write("<tr><th colspan=7>{}</th><tr>\n".format(builder['name']))
Maxim Kuvyrkovfdaa4682021-04-14 13:13:14 +0000191 temp.write("<tr><th>Buildbot</th><th>Status</th><th>T Since</th>"
192 "<th>Duration</th><th>Build #</th><th>Commits</th>"
193 "<th>Failing steps</th></tr>\n")
David Spickett55449c62021-12-13 12:57:33 +0000194 temp.write("<tbody>\n")
Adhemerval Zanellad3e8c482020-10-12 11:31:48 -0300195 for bot in builder['bots']:
196 temp.write("<tr>\n")
David Spickettf2c82dd2021-06-24 10:01:33 +0100197 try:
198 status = bots_status[(base_url, bot['name'])]
199 except KeyError:
David Spickettf2d4c482021-06-24 10:07:52 +0100200 temp.write(" <td>{} is offline!</td>\n</tr>\n".format(bot['name']))
David Spickettf2c82dd2021-06-24 10:01:33 +0100201 continue
David Spickett30a986f2021-04-29 09:37:00 +0100202 else:
203 if not status.get('valid', True):
204 temp.write(" <td>Could not read status for {}!</td>\n</tr>\n".format(bot['name']))
205 continue
David Spickettf2c82dd2021-06-24 10:01:33 +0100206
David Spickett85355fa2021-03-22 15:41:41 +0000207 found_failure |= status['fail']
David Spickett30a986f2021-04-29 09:37:00 +0100208
209 temp.write(" <td><a href='{}'>{}</a></td>\n".format(
210 status['builder_url'], bot['name']))
Adhemerval Zanellad3e8c482020-10-12 11:31:48 -0300211 temp.write(" <td><font color='{}'>{}</font></td>\n"
212 .format('red' if status['fail'] else 'green',
213 'FAIL' if status['fail'] else 'PASS'))
214 empty_cell=" <td>&nbsp;</td>\n"
Maxim Kuvyrkovfdaa4682021-04-14 13:13:14 +0000215 if 'time_since' in status:
216 temp.write(" <td>{}</td>\n".format(status['time_since']))
217 else:
218 temp.write(empty_cell)
219 if 'duration' in status:
220 temp.write(" <td>{}</td>\n".format(status['duration']))
Adhemerval Zanellad3e8c482020-10-12 11:31:48 -0300221 else:
222 temp.write(empty_cell)
223 if 'number' in status:
David Spickett30a986f2021-04-29 09:37:00 +0100224 temp.write(" <td><a href='{}'>{}</a></td>\n".format(
225 status['build_url'], status['number']))
Adhemerval Zanellad3e8c482020-10-12 11:31:48 -0300226 else:
227 temp.write(empty_cell)
228 if 'changes' in status:
229 temp.write(" <td>{}</td>\n".format(status['changes']))
230 else:
231 temp.write(empty_cell)
Oliver Stannard91688ff2021-01-07 10:27:27 +0000232 if 'steps' in status and status['steps']:
233 def render_step(name, result):
234 return "<font color='{}'>{}</font>".format(RESULT_COLORS[result], name)
235 step_list = ', '.join(render_step(name, result) for name, result in status['steps'])
236 temp.write(" <td style=\"text-align:center\">{}</td>\n".format(step_list))
Adhemerval Zanellad3e8c482020-10-12 11:31:48 -0300237 else:
238 temp.write(empty_cell)
239 temp.write("</tr>\n")
David Spickett55449c62021-12-13 12:57:33 +0000240 temp.write("</tbody>\n")
Adhemerval Zanellad3e8c482020-10-12 11:31:48 -0300241 temp.write("</table>\n")
242
David Spickett85355fa2021-03-22 15:41:41 +0000243 temp.write("<link rel=\"shortcut icon\" href=\"{}\" "
244 "type=\"image/x-icon\"/>\n".format(
245 'fail.ico' if found_failure else 'ok.ico'))
246
Adhemerval Zanellad3e8c482020-10-12 11:31:48 -0300247 # Move temp to main (atomic change)
248 temp.close()
David Spickett7f18f4d2021-03-22 11:49:17 +0000249 shutil.move(temp.name, output_file)
Adhemerval Zanellad3e8c482020-10-12 11:31:48 -0300250
251
252if __name__ == "__main__":
253 parser = argparse.ArgumentParser()
David Spickettec2166c2021-07-19 14:17:29 +0100254 parser.add_argument('-d', dest='debug', action='store_true',
255 help='show debug log messages')
Adhemerval Zanellad3e8c482020-10-12 11:31:48 -0300256 parser.add_argument('config_file',
257 help='Bots description in JSON format')
258 parser.add_argument('output_file',
259 help='output HTML path')
260 args = parser.parse_args()
261
262 if args.debug:
263 logging.basicConfig(stream=sys.stderr, level=logging.DEBUG)
264
265 try:
266 with open(args.config_file, "r") as f:
267 config = json.load(f)
268 except IOError as e:
David Spickett7f18f4d2021-03-22 11:49:17 +0000269 print("error: failed to read {} config file: {}".format(args.config_file, e))
Adhemerval Zanellad3e8c482020-10-12 11:31:48 -0300270 sys.exit(os.EX_CONFIG)
271
David Spickett30a986f2021-04-29 09:37:00 +0100272 status = get_buildbot_bots_status(config)
273 status.update(get_buildkite_bots_status(config))
274 write_bot_status(config, args.output_file, status)