blob: 759e4994afa72e9568c697dec25fdc87a1617cfa [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 -030026def ignored(s):
27 return 'ignore' in s and s['ignore']
28def not_ignored(s):
29 return not ignored(s)
30
31
David Spickette88fe592021-03-22 12:25:13 +000032# Returns the parsed json URL or raises an exception
Adhemerval Zanellad3e8c482020-10-12 11:31:48 -030033def wget(session, url):
David Spickett8306ed82021-12-06 10:40:36 +000034 got = session.get(url)
35 got.raise_for_status()
36 return got.json()
Adhemerval Zanellad3e8c482020-10-12 11:31:48 -030037
38
Oliver Stannard91688ff2021-01-07 10:27:27 +000039# Map from buildbot status codes we want to treat as errors to the color they
40# should be shown in. The codes are documented at
41# https://docs.buildbot.net/latest/developer/results.html#build-result-codes,
42# and these colors match the suggested ones there.
43RESULT_COLORS = {
44 2: 'red', # Error
45 4: 'purple', # Exception
46 5: 'purple', # Retry
47 6: 'pink', # Cancelled
48}
49
50def get_bot_failing_steps(session, base_url, buildid):
David Spickette88fe592021-03-22 12:25:13 +000051 try:
52 contents = wget(session, "{}/api/v2/builds/{}/steps"
53 .format(base_url, buildid))
54 except requests.exceptions.RequestException:
Oliver Stannard91688ff2021-01-07 10:27:27 +000055 return ""
David Spickette88fe592021-03-22 12:25:13 +000056
Oliver Stannard91688ff2021-01-07 10:27:27 +000057 for step in contents["steps"]:
David Spickett7f18f4d2021-03-22 11:49:17 +000058 if step["results"] in RESULT_COLORS:
Oliver Stannard91688ff2021-01-07 10:27:27 +000059 yield (step["name"], step["results"])
60
61
Adhemerval Zanellad3e8c482020-10-12 11:31:48 -030062# Get the status of a individual bot BOT. Returns a dict with the
63# information.
64def get_bot_status(session, bot, base_url, builder_url, build_url):
David Spickette88fe592021-03-22 12:25:13 +000065 try:
66 builds = wget(session,
67 "{}/api/v2/{}/{}/{}"
68 .format(base_url, builder_url, bot, build_url))
69 except requests.exceptions.RequestException as e:
David Spickett7d47cea2022-06-01 14:13:32 +010070 logging.debug(" Couldn't get builds for bot {}!".format(bot))
71 return {'valid': False}
Adhemerval Zanellad3e8c482020-10-12 11:31:48 -030072
Oliver Stannard46e99032021-01-05 10:30:56 +000073 reversed_builds = iter(sorted(builds['builds'], key=lambda b: -b["number"]))
Adhemerval Zanellad3e8c482020-10-12 11:31:48 -030074 for build in reversed_builds:
75 if build['complete']:
Maxim Kuvyrkovfdaa4682021-04-14 13:13:14 +000076 time_since = (int(datetime.now().timestamp()) - int(build['complete_at']))
77 duration = int(build['complete_at']) - int(build['started_at'])
David Spickett30a986f2021-04-29 09:37:00 +010078 agent_url = "{}/#/{}/{}".format(base_url, builder_url, build['builderid'])
79
David Spickett7f18f4d2021-03-22 11:49:17 +000080 status = {
David Spickett30a986f2021-04-29 09:37:00 +010081 'builder_url': agent_url,
David Spickett7f18f4d2021-03-22 11:49:17 +000082 'number': build['number'],
David Spickett30a986f2021-04-29 09:37:00 +010083 'build_url': "{}/builds/{}".format(agent_url, build['number']),
David Spickett7f18f4d2021-03-22 11:49:17 +000084 'state': build['state_string'],
Maxim Kuvyrkovfdaa4682021-04-14 13:13:14 +000085 'time_since': timedelta(seconds=time_since),
86 'duration': timedelta(seconds=duration),
David Spickett7f18f4d2021-03-22 11:49:17 +000087 'fail': build['state_string'] != 'build successful',
88 }
David Spickett30a986f2021-04-29 09:37:00 +010089
David Spickett7f18f4d2021-03-22 11:49:17 +000090 if status['fail']:
91 buildid = build['buildid']
Oliver Stannard91688ff2021-01-07 10:27:27 +000092 status['steps'] = list(get_bot_failing_steps(session, base_url,
David Spickett7f18f4d2021-03-22 11:49:17 +000093 buildid))
94
95 return status
Adhemerval Zanellad3e8c482020-10-12 11:31:48 -030096
97
David Spickett85355fa2021-03-22 15:41:41 +000098# Get status for all bots named in the config
99# Return a dictionary of (base_url, bot name) -> status info
David Spickett30a986f2021-04-29 09:37:00 +0100100def get_buildbot_bots_status(config):
Adhemerval Zanellad3e8c482020-10-12 11:31:48 -0300101 session = requests.Session()
Adhemerval Zanellad3e8c482020-10-12 11:31:48 -0300102 bot_cache = {}
David Spickettf006c372021-03-22 12:54:12 +0000103
Adhemerval Zanellad3e8c482020-10-12 11:31:48 -0300104 for server in filter(not_ignored, config):
David Spickett30a986f2021-04-29 09:37:00 +0100105 if server['name'] == "Buildkite":
106 continue
107
Adhemerval Zanellad3e8c482020-10-12 11:31:48 -0300108 base_url = server['base_url']
Adhemerval Zanellad3e8c482020-10-12 11:31:48 -0300109 logging.debug('Parsing server {}...'.format(server['name']))
110 for builder in server['builders']:
111 logging.debug(' Parsing builders {}...'.format(builder['name']))
112 for bot in builder['bots']:
David Spickett85355fa2021-03-22 15:41:41 +0000113 bot_key = (base_url, bot['name'])
Adhemerval Zanellad3e8c482020-10-12 11:31:48 -0300114 if bot_key in bot_cache:
115 continue
David Spickett85355fa2021-03-22 15:41:41 +0000116
Adhemerval Zanellad3e8c482020-10-12 11:31:48 -0300117 logging.debug(' Parsing bot {}...'.format(bot['name']))
David Spickett85355fa2021-03-22 15:41:41 +0000118 status = get_bot_status(session, bot['name'], base_url, server['builder_url'],
119 server['build_url'])
David Spickettf2c82dd2021-06-24 10:01:33 +0100120 if status is not None:
David Spickett7d47cea2022-06-01 14:13:32 +0100121 if status.get("valid", True):
122 logging.debug(" Bot status: " + ("FAIL" if status['fail'] else "PASS"))
David Spickettf2c82dd2021-06-24 10:01:33 +0100123 bot_cache[bot_key] = status
Adhemerval Zanellad3e8c482020-10-12 11:31:48 -0300124
David Spickett85355fa2021-03-22 15:41:41 +0000125 return bot_cache
126
127def write_bot_status(config, output_file, bots_status):
128 temp = tempfile.NamedTemporaryFile(mode='w+', delete=False)
129 today = "{}\n".format(datetime.today().ctime())
David Spickettf006c372021-03-22 12:54:12 +0000130
David Spickett55449c62021-12-13 12:57:33 +0000131 temp.write(dedent("""\
David Spickett64161b02022-11-01 09:51:30 +0000132 <!DOCTYPE html>
David Spickett55449c62021-12-13 12:57:33 +0000133 <style>
134 /* Combine the border between cells to prevent 1px gaps
135 in the row background colour. */
136 table, td, th {
137 border-collapse: collapse;
138 }
139 /* Colour every other row in a table body grey. */
140 tbody tr:nth-child(even) td {
141 background-color: #ededed;
142 }
143 </style>"""))
144
Adhemerval Zanellad3e8c482020-10-12 11:31:48 -0300145 # Dump all servers / bots
146 for server in filter(not_ignored, config):
147 base_url = server['base_url']
148 builder_url = server['builder_url']
149 build_url = server['build_url']
David Spickettec94cc22021-12-13 13:16:05 +0000150
151 column_titles = [
152 "Buildbot",
153 "Status",
154 "T Since",
155 "Duration",
156 "Build #",
David Spickettec94cc22021-12-13 13:16:05 +0000157 "Failing steps"
158 ]
159 num_columns = len(column_titles)
160 column_titles_html = "<tr>{}</tr>\n".format(
161 "".join(["<th>{}</th>".format(t) for t in column_titles]))
162
David Spickett55449c62021-12-13 12:57:33 +0000163 temp.write("<table border=0 cellspacing=1 cellpadding=2>\n")
David Spickettec94cc22021-12-13 13:16:05 +0000164 temp.write("<tr><td colspan={}>&nbsp;</td><tr>\n".format(num_columns))
165 temp.write("<tr><th colspan={}>{} @ {}</td><tr>\n"
166 .format(num_columns, server['name'], today))
Adhemerval Zanellad3e8c482020-10-12 11:31:48 -0300167
168 for builder in server['builders']:
David Spickettec94cc22021-12-13 13:16:05 +0000169 temp.write("<tr><td colspan={}>&nbsp;</td><tr>\n".format(num_columns))
170 temp.write("<tr><th colspan={}>{}</th><tr>\n".format(num_columns, builder['name']))
171 temp.write(column_titles_html)
David Spickett55449c62021-12-13 12:57:33 +0000172 temp.write("<tbody>\n")
Adhemerval Zanellad3e8c482020-10-12 11:31:48 -0300173 for bot in builder['bots']:
174 temp.write("<tr>\n")
David Spickett7d47cea2022-06-01 14:13:32 +0100175 logging.debug("Writing out status for {}".format(bot['name']))
David Spickettf2c82dd2021-06-24 10:01:33 +0100176 try:
177 status = bots_status[(base_url, bot['name'])]
178 except KeyError:
David Spickettec94cc22021-12-13 13:16:05 +0000179 temp.write(" <td colspan={}>{} is offline!</td>\n</tr>\n"
180 .format(num_columns, bot['name']))
David Spickettf2c82dd2021-06-24 10:01:33 +0100181 continue
David Spickett30a986f2021-04-29 09:37:00 +0100182 else:
183 if not status.get('valid', True):
David Spickettec94cc22021-12-13 13:16:05 +0000184 temp.write(" <td colspan={}>Could not read status for {}!</td>\n</tr>\n"
185 .format(num_columns, bot['name']))
David Spickett30a986f2021-04-29 09:37:00 +0100186 continue
David Spickettf2c82dd2021-06-24 10:01:33 +0100187
David Spickett30a986f2021-04-29 09:37:00 +0100188 temp.write(" <td><a href='{}'>{}</a></td>\n".format(
189 status['builder_url'], bot['name']))
Adhemerval Zanellad3e8c482020-10-12 11:31:48 -0300190 temp.write(" <td><font color='{}'>{}</font></td>\n"
191 .format('red' if status['fail'] else 'green',
192 'FAIL' if status['fail'] else 'PASS'))
193 empty_cell=" <td>&nbsp;</td>\n"
Maxim Kuvyrkovfdaa4682021-04-14 13:13:14 +0000194 if 'time_since' in status:
David Spickett187f7962022-02-09 12:35:00 +0000195 time_since = status['time_since']
196 # No build should be taking more than a day
197 if time_since > timedelta(hours=24):
198 time_since = "<p style=\"color:red\">{}</p>".format(
199 time_since)
200 else:
201 time_since = str(time_since)
202
203 temp.write(" <td>{}</td>\n".format(time_since))
Maxim Kuvyrkovfdaa4682021-04-14 13:13:14 +0000204 else:
205 temp.write(empty_cell)
206 if 'duration' in status:
207 temp.write(" <td>{}</td>\n".format(status['duration']))
Adhemerval Zanellad3e8c482020-10-12 11:31:48 -0300208 else:
209 temp.write(empty_cell)
210 if 'number' in status:
David Spickett30a986f2021-04-29 09:37:00 +0100211 temp.write(" <td><a href='{}'>{}</a></td>\n".format(
212 status['build_url'], status['number']))
Adhemerval Zanellad3e8c482020-10-12 11:31:48 -0300213 else:
214 temp.write(empty_cell)
Oliver Stannard91688ff2021-01-07 10:27:27 +0000215 if 'steps' in status and status['steps']:
216 def render_step(name, result):
217 return "<font color='{}'>{}</font>".format(RESULT_COLORS[result], name)
218 step_list = ', '.join(render_step(name, result) for name, result in status['steps'])
219 temp.write(" <td style=\"text-align:center\">{}</td>\n".format(step_list))
Adhemerval Zanellad3e8c482020-10-12 11:31:48 -0300220 else:
221 temp.write(empty_cell)
222 temp.write("</tr>\n")
David Spickett55449c62021-12-13 12:57:33 +0000223 temp.write("</tbody>\n")
Adhemerval Zanellad3e8c482020-10-12 11:31:48 -0300224 temp.write("</table>\n")
225
226 # Move temp to main (atomic change)
227 temp.close()
David Spickett7f18f4d2021-03-22 11:49:17 +0000228 shutil.move(temp.name, output_file)
Adhemerval Zanellad3e8c482020-10-12 11:31:48 -0300229
230
231if __name__ == "__main__":
232 parser = argparse.ArgumentParser()
David Spickettec2166c2021-07-19 14:17:29 +0100233 parser.add_argument('-d', dest='debug', action='store_true',
234 help='show debug log messages')
Adhemerval Zanellad3e8c482020-10-12 11:31:48 -0300235 parser.add_argument('config_file',
236 help='Bots description in JSON format')
237 parser.add_argument('output_file',
238 help='output HTML path')
239 args = parser.parse_args()
240
241 if args.debug:
242 logging.basicConfig(stream=sys.stderr, level=logging.DEBUG)
243
244 try:
245 with open(args.config_file, "r") as f:
246 config = json.load(f)
247 except IOError as e:
David Spickett7f18f4d2021-03-22 11:49:17 +0000248 print("error: failed to read {} config file: {}".format(args.config_file, e))
Adhemerval Zanellad3e8c482020-10-12 11:31:48 -0300249 sys.exit(os.EX_CONFIG)
250
David Spickett30a986f2021-04-29 09:37:00 +0100251 status = get_buildbot_bots_status(config)
252 status.update(get_buildkite_bots_status(config))
253 write_bot_status(config, args.output_file, status)