blob: e38bc59cfbab4ac54f75eadc0de61a31ee12e298 [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
22
David Spickett30a986f2021-04-29 09:37:00 +010023from buildkite_status import get_buildkite_bots_status
24
Adhemerval Zanellad3e8c482020-10-12 11:31:48 -030025# The GIT revision length used on 'Commits' error display.
26GIT_SHORT_LEN=7
27
28def ignored(s):
29 return 'ignore' in s and s['ignore']
30def not_ignored(s):
31 return not ignored(s)
32
33
David Spickette88fe592021-03-22 12:25:13 +000034# Returns the parsed json URL or raises an exception
Adhemerval Zanellad3e8c482020-10-12 11:31:48 -030035def wget(session, url):
David Spickette88fe592021-03-22 12:25:13 +000036 return session.get(url).json()
Adhemerval Zanellad3e8c482020-10-12 11:31:48 -030037
38
39# Returns a string with the GIT revision usesd on build BUILDID and
40# PREV_BUILDID in the form '<id_buildid>-<id_prev_buildid>'.
41def get_bot_failure_changes(session, base_url, buildid, prev_buildid):
42 def wget_build_rev(bid):
David Spickette88fe592021-03-22 12:25:13 +000043 try:
44 contents = wget(session,
45 "{}/api/v2/builds/{}/changes"
46 .format(base_url, bid))
47 except requests.exceptions.RequestException:
David Spickett7f18f4d2021-03-22 11:49:17 +000048 return None
David Spickette88fe592021-03-22 12:25:13 +000049 changes = contents['changes']
50 if changes:
51 return changes[0]['revision']
52 return None
Adhemerval Zanellad3e8c482020-10-12 11:31:48 -030053
David Spickett7f18f4d2021-03-22 11:49:17 +000054 revision = wget_build_rev(buildid)[:GIT_SHORT_LEN]
55 prev_revision = None
56 if prev_buildid is not None:
57 prev_revision = wget_build_rev(prev_buildid)
58
59 if prev_revision is None:
60 return "{}".format(revision)
Adhemerval Zanellad3e8c482020-10-12 11:31:48 -030061 else:
David Spickett7f18f4d2021-03-22 11:49:17 +000062 return "{}-{}".format(revision, prev_revision[:GIT_SHORT_LEN])
Adhemerval Zanellad3e8c482020-10-12 11:31:48 -030063
64
Oliver Stannard91688ff2021-01-07 10:27:27 +000065# Map from buildbot status codes we want to treat as errors to the color they
66# should be shown in. The codes are documented at
67# https://docs.buildbot.net/latest/developer/results.html#build-result-codes,
68# and these colors match the suggested ones there.
69RESULT_COLORS = {
70 2: 'red', # Error
71 4: 'purple', # Exception
72 5: 'purple', # Retry
73 6: 'pink', # Cancelled
74}
75
76def get_bot_failing_steps(session, base_url, buildid):
David Spickette88fe592021-03-22 12:25:13 +000077 try:
78 contents = wget(session, "{}/api/v2/builds/{}/steps"
79 .format(base_url, buildid))
80 except requests.exceptions.RequestException:
Oliver Stannard91688ff2021-01-07 10:27:27 +000081 return ""
David Spickette88fe592021-03-22 12:25:13 +000082
Oliver Stannard91688ff2021-01-07 10:27:27 +000083 for step in contents["steps"]:
David Spickett7f18f4d2021-03-22 11:49:17 +000084 if step["results"] in RESULT_COLORS:
Oliver Stannard91688ff2021-01-07 10:27:27 +000085 yield (step["name"], step["results"])
86
87
Adhemerval Zanellad3e8c482020-10-12 11:31:48 -030088# Get the status of a individual bot BOT. Returns a dict with the
89# information.
90def get_bot_status(session, bot, base_url, builder_url, build_url):
David Spickette88fe592021-03-22 12:25:13 +000091 try:
92 builds = wget(session,
93 "{}/api/v2/{}/{}/{}"
94 .format(base_url, builder_url, bot, build_url))
95 except requests.exceptions.RequestException as e:
96 return {'fail': True}
Adhemerval Zanellad3e8c482020-10-12 11:31:48 -030097
Oliver Stannard46e99032021-01-05 10:30:56 +000098 reversed_builds = iter(sorted(builds['builds'], key=lambda b: -b["number"]))
Adhemerval Zanellad3e8c482020-10-12 11:31:48 -030099 for build in reversed_builds:
100 if build['complete']:
Maxim Kuvyrkovfdaa4682021-04-14 13:13:14 +0000101 time_since = (int(datetime.now().timestamp()) - int(build['complete_at']))
102 duration = int(build['complete_at']) - int(build['started_at'])
David Spickett30a986f2021-04-29 09:37:00 +0100103 agent_url = "{}/#/{}/{}".format(base_url, builder_url, build['builderid'])
104
David Spickett7f18f4d2021-03-22 11:49:17 +0000105 status = {
David Spickett30a986f2021-04-29 09:37:00 +0100106 'builder_url': agent_url,
David Spickett7f18f4d2021-03-22 11:49:17 +0000107 'number': build['number'],
David Spickett30a986f2021-04-29 09:37:00 +0100108 'build_url': "{}/builds/{}".format(agent_url, build['number']),
David Spickett7f18f4d2021-03-22 11:49:17 +0000109 'state': build['state_string'],
Maxim Kuvyrkovfdaa4682021-04-14 13:13:14 +0000110 'time_since': timedelta(seconds=time_since),
111 'duration': timedelta(seconds=duration),
David Spickett7f18f4d2021-03-22 11:49:17 +0000112 'fail': build['state_string'] != 'build successful',
113 }
David Spickett30a986f2021-04-29 09:37:00 +0100114
David Spickett7f18f4d2021-03-22 11:49:17 +0000115 if status['fail']:
116 buildid = build['buildid']
117 prev_buildid = next(reversed_builds, None)['buildid']
Adhemerval Zanellad3e8c482020-10-12 11:31:48 -0300118 status['changes'] = get_bot_failure_changes(session, base_url,
David Spickett7f18f4d2021-03-22 11:49:17 +0000119 buildid,
Adhemerval Zanellad3e8c482020-10-12 11:31:48 -0300120 prev_buildid)
Oliver Stannard91688ff2021-01-07 10:27:27 +0000121 status['steps'] = list(get_bot_failing_steps(session, base_url,
David Spickett7f18f4d2021-03-22 11:49:17 +0000122 buildid))
123
124 return status
Adhemerval Zanellad3e8c482020-10-12 11:31:48 -0300125
126
David Spickett85355fa2021-03-22 15:41:41 +0000127# Get status for all bots named in the config
128# Return a dictionary of (base_url, bot name) -> status info
David Spickett30a986f2021-04-29 09:37:00 +0100129def get_buildbot_bots_status(config):
Adhemerval Zanellad3e8c482020-10-12 11:31:48 -0300130 session = requests.Session()
Adhemerval Zanellad3e8c482020-10-12 11:31:48 -0300131 bot_cache = {}
David Spickettf006c372021-03-22 12:54:12 +0000132
Adhemerval Zanellad3e8c482020-10-12 11:31:48 -0300133 for server in filter(not_ignored, config):
David Spickett30a986f2021-04-29 09:37:00 +0100134 if server['name'] == "Buildkite":
135 continue
136
Adhemerval Zanellad3e8c482020-10-12 11:31:48 -0300137 base_url = server['base_url']
Adhemerval Zanellad3e8c482020-10-12 11:31:48 -0300138 logging.debug('Parsing server {}...'.format(server['name']))
139 for builder in server['builders']:
140 logging.debug(' Parsing builders {}...'.format(builder['name']))
141 for bot in builder['bots']:
David Spickett85355fa2021-03-22 15:41:41 +0000142 bot_key = (base_url, bot['name'])
Adhemerval Zanellad3e8c482020-10-12 11:31:48 -0300143 if bot_key in bot_cache:
144 continue
David Spickett85355fa2021-03-22 15:41:41 +0000145
Adhemerval Zanellad3e8c482020-10-12 11:31:48 -0300146 logging.debug(' Parsing bot {}...'.format(bot['name']))
David Spickett85355fa2021-03-22 15:41:41 +0000147 status = get_bot_status(session, bot['name'], base_url, server['builder_url'],
148 server['build_url'])
David Spickettf2c82dd2021-06-24 10:01:33 +0100149 if status is not None:
150 logging.debug(" FAIL" if status['fail'] else " PASS")
151 bot_cache[bot_key] = status
Adhemerval Zanellad3e8c482020-10-12 11:31:48 -0300152
David Spickett85355fa2021-03-22 15:41:41 +0000153 return bot_cache
154
155def write_bot_status(config, output_file, bots_status):
156 temp = tempfile.NamedTemporaryFile(mode='w+', delete=False)
157 today = "{}\n".format(datetime.today().ctime())
158 # Whether we use the fail favicon or not
159 found_failure = False
David Spickettf006c372021-03-22 12:54:12 +0000160
Adhemerval Zanellad3e8c482020-10-12 11:31:48 -0300161 # Dump all servers / bots
162 for server in filter(not_ignored, config):
163 base_url = server['base_url']
164 builder_url = server['builder_url']
165 build_url = server['build_url']
Adhemerval Zanellad3e8c482020-10-12 11:31:48 -0300166 temp.write("<table cellspacing=1 cellpadding=2>\n")
167 temp.write("<tr><td colspan=5>&nbsp;</td><tr>\n")
168 temp.write("<tr><th colspan=5>{} @ {}</td><tr>\n"
169 .format(server['name'], today))
170
171 for builder in server['builders']:
172 temp.write("<tr><td colspan=5>&nbsp;</td><tr>\n")
173 temp.write("<tr><th colspan=5>{}</td><tr>\n".format(builder['name']))
Maxim Kuvyrkovfdaa4682021-04-14 13:13:14 +0000174 temp.write("<tr><th>Buildbot</th><th>Status</th><th>T Since</th>"
175 "<th>Duration</th><th>Build #</th><th>Commits</th>"
176 "<th>Failing steps</th></tr>\n")
Adhemerval Zanellad3e8c482020-10-12 11:31:48 -0300177 for bot in builder['bots']:
178 temp.write("<tr>\n")
David Spickettf2c82dd2021-06-24 10:01:33 +0100179 try:
180 status = bots_status[(base_url, bot['name'])]
181 except KeyError:
David Spickettf2d4c482021-06-24 10:07:52 +0100182 temp.write(" <td>{} is offline!</td>\n</tr>\n".format(bot['name']))
David Spickettf2c82dd2021-06-24 10:01:33 +0100183 continue
David Spickett30a986f2021-04-29 09:37:00 +0100184 else:
185 if not status.get('valid', True):
186 temp.write(" <td>Could not read status for {}!</td>\n</tr>\n".format(bot['name']))
187 continue
David Spickettf2c82dd2021-06-24 10:01:33 +0100188
David Spickett85355fa2021-03-22 15:41:41 +0000189 found_failure |= status['fail']
David Spickett30a986f2021-04-29 09:37:00 +0100190
191 temp.write(" <td><a href='{}'>{}</a></td>\n".format(
192 status['builder_url'], bot['name']))
Adhemerval Zanellad3e8c482020-10-12 11:31:48 -0300193 temp.write(" <td><font color='{}'>{}</font></td>\n"
194 .format('red' if status['fail'] else 'green',
195 'FAIL' if status['fail'] else 'PASS'))
196 empty_cell=" <td>&nbsp;</td>\n"
Maxim Kuvyrkovfdaa4682021-04-14 13:13:14 +0000197 if 'time_since' in status:
198 temp.write(" <td>{}</td>\n".format(status['time_since']))
199 else:
200 temp.write(empty_cell)
201 if 'duration' in status:
202 temp.write(" <td>{}</td>\n".format(status['duration']))
Adhemerval Zanellad3e8c482020-10-12 11:31:48 -0300203 else:
204 temp.write(empty_cell)
205 if 'number' in status:
David Spickett30a986f2021-04-29 09:37:00 +0100206 temp.write(" <td><a href='{}'>{}</a></td>\n".format(
207 status['build_url'], status['number']))
Adhemerval Zanellad3e8c482020-10-12 11:31:48 -0300208 else:
209 temp.write(empty_cell)
210 if 'changes' in status:
211 temp.write(" <td>{}</td>\n".format(status['changes']))
212 else:
213 temp.write(empty_cell)
Oliver Stannard91688ff2021-01-07 10:27:27 +0000214 if 'steps' in status and status['steps']:
215 def render_step(name, result):
216 return "<font color='{}'>{}</font>".format(RESULT_COLORS[result], name)
217 step_list = ', '.join(render_step(name, result) for name, result in status['steps'])
218 temp.write(" <td style=\"text-align:center\">{}</td>\n".format(step_list))
Adhemerval Zanellad3e8c482020-10-12 11:31:48 -0300219 else:
220 temp.write(empty_cell)
221 temp.write("</tr>\n")
222 temp.write("</table>\n")
223
David Spickett85355fa2021-03-22 15:41:41 +0000224 temp.write("<link rel=\"shortcut icon\" href=\"{}\" "
225 "type=\"image/x-icon\"/>\n".format(
226 'fail.ico' if found_failure else 'ok.ico'))
227
Adhemerval Zanellad3e8c482020-10-12 11:31:48 -0300228 # Move temp to main (atomic change)
229 temp.close()
David Spickett7f18f4d2021-03-22 11:49:17 +0000230 shutil.move(temp.name, output_file)
Adhemerval Zanellad3e8c482020-10-12 11:31:48 -0300231
232
233if __name__ == "__main__":
234 parser = argparse.ArgumentParser()
David Spickettec2166c2021-07-19 14:17:29 +0100235 parser.add_argument('-d', dest='debug', action='store_true',
236 help='show debug log messages')
Adhemerval Zanellad3e8c482020-10-12 11:31:48 -0300237 parser.add_argument('config_file',
238 help='Bots description in JSON format')
239 parser.add_argument('output_file',
240 help='output HTML path')
241 args = parser.parse_args()
242
243 if args.debug:
244 logging.basicConfig(stream=sys.stderr, level=logging.DEBUG)
245
246 try:
247 with open(args.config_file, "r") as f:
248 config = json.load(f)
249 except IOError as e:
David Spickett7f18f4d2021-03-22 11:49:17 +0000250 print("error: failed to read {} config file: {}".format(args.config_file, e))
Adhemerval Zanellad3e8c482020-10-12 11:31:48 -0300251 sys.exit(os.EX_CONFIG)
252
David Spickett30a986f2021-04-29 09:37:00 +0100253 status = get_buildbot_bots_status(config)
254 status.update(get_buildkite_bots_status(config))
255 write_bot_status(config, args.output_file, status)