Adhemerval Zanella | d3e8c48 | 2020-10-12 11:31:48 -0300 | [diff] [blame] | 1 | #!/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 | |
| 11 | import sys |
| 12 | import os |
| 13 | import argparse |
| 14 | import json |
| 15 | import tempfile |
| 16 | import logging |
David Spickett | aa155be | 2021-02-25 14:30:09 +0000 | [diff] [blame] | 17 | import shutil |
David Spickett | 8822bbd | 2023-06-12 14:02:41 +0100 | [diff] [blame^] | 18 | import time |
Adhemerval Zanella | d3e8c48 | 2020-10-12 11:31:48 -0300 | [diff] [blame] | 19 | from datetime import datetime, timedelta |
| 20 | # The requests allows HTTP keep-alive which re-uses the same TCP connection |
| 21 | # to download multiple files. |
| 22 | import requests |
David Spickett | 55449c6 | 2021-12-13 12:57:33 +0000 | [diff] [blame] | 23 | from textwrap import dedent |
Adhemerval Zanella | d3e8c48 | 2020-10-12 11:31:48 -0300 | [diff] [blame] | 24 | |
David Spickett | 30a986f | 2021-04-29 09:37:00 +0100 | [diff] [blame] | 25 | from buildkite_status import get_buildkite_bots_status |
| 26 | |
Adhemerval Zanella | d3e8c48 | 2020-10-12 11:31:48 -0300 | [diff] [blame] | 27 | def ignored(s): |
| 28 | return 'ignore' in s and s['ignore'] |
| 29 | def not_ignored(s): |
| 30 | return not ignored(s) |
| 31 | |
| 32 | |
David Spickett | e88fe59 | 2021-03-22 12:25:13 +0000 | [diff] [blame] | 33 | # Returns the parsed json URL or raises an exception |
Adhemerval Zanella | d3e8c48 | 2020-10-12 11:31:48 -0300 | [diff] [blame] | 34 | def wget(session, url): |
David Spickett | 8306ed8 | 2021-12-06 10:40:36 +0000 | [diff] [blame] | 35 | got = session.get(url) |
| 36 | got.raise_for_status() |
| 37 | return got.json() |
Adhemerval Zanella | d3e8c48 | 2020-10-12 11:31:48 -0300 | [diff] [blame] | 38 | |
| 39 | |
Oliver Stannard | 91688ff | 2021-01-07 10:27:27 +0000 | [diff] [blame] | 40 | # Map from buildbot status codes we want to treat as errors to the color they |
| 41 | # should be shown in. The codes are documented at |
| 42 | # https://docs.buildbot.net/latest/developer/results.html#build-result-codes, |
| 43 | # and these colors match the suggested ones there. |
| 44 | RESULT_COLORS = { |
| 45 | 2: 'red', # Error |
| 46 | 4: 'purple', # Exception |
| 47 | 5: 'purple', # Retry |
| 48 | 6: 'pink', # Cancelled |
| 49 | } |
| 50 | |
| 51 | def get_bot_failing_steps(session, base_url, buildid): |
David Spickett | e88fe59 | 2021-03-22 12:25:13 +0000 | [diff] [blame] | 52 | try: |
| 53 | contents = wget(session, "{}/api/v2/builds/{}/steps" |
| 54 | .format(base_url, buildid)) |
| 55 | except requests.exceptions.RequestException: |
Oliver Stannard | 91688ff | 2021-01-07 10:27:27 +0000 | [diff] [blame] | 56 | return "" |
David Spickett | e88fe59 | 2021-03-22 12:25:13 +0000 | [diff] [blame] | 57 | |
Oliver Stannard | 91688ff | 2021-01-07 10:27:27 +0000 | [diff] [blame] | 58 | for step in contents["steps"]: |
David Spickett | 7f18f4d | 2021-03-22 11:49:17 +0000 | [diff] [blame] | 59 | if step["results"] in RESULT_COLORS: |
Oliver Stannard | 91688ff | 2021-01-07 10:27:27 +0000 | [diff] [blame] | 60 | yield (step["name"], step["results"]) |
| 61 | |
| 62 | |
Adhemerval Zanella | d3e8c48 | 2020-10-12 11:31:48 -0300 | [diff] [blame] | 63 | # Get the status of a individual bot BOT. Returns a dict with the |
| 64 | # information. |
| 65 | def get_bot_status(session, bot, base_url, builder_url, build_url): |
David Spickett | e88fe59 | 2021-03-22 12:25:13 +0000 | [diff] [blame] | 66 | try: |
| 67 | builds = wget(session, |
| 68 | "{}/api/v2/{}/{}/{}" |
| 69 | .format(base_url, builder_url, bot, build_url)) |
| 70 | except requests.exceptions.RequestException as e: |
David Spickett | 7d47cea | 2022-06-01 14:13:32 +0100 | [diff] [blame] | 71 | logging.debug(" Couldn't get builds for bot {}!".format(bot)) |
| 72 | return {'valid': False} |
Adhemerval Zanella | d3e8c48 | 2020-10-12 11:31:48 -0300 | [diff] [blame] | 73 | |
Oliver Stannard | 46e9903 | 2021-01-05 10:30:56 +0000 | [diff] [blame] | 74 | reversed_builds = iter(sorted(builds['builds'], key=lambda b: -b["number"])) |
David Spickett | 86f2d47 | 2023-06-12 11:21:16 +0100 | [diff] [blame] | 75 | next_build = None |
Adhemerval Zanella | d3e8c48 | 2020-10-12 11:31:48 -0300 | [diff] [blame] | 76 | for build in reversed_builds: |
David Spickett | 86f2d47 | 2023-06-12 11:21:16 +0100 | [diff] [blame] | 77 | if not build['complete']: |
| 78 | next_build = build |
| 79 | continue |
David Spickett | 30a986f | 2021-04-29 09:37:00 +0100 | [diff] [blame] | 80 | |
David Spickett | 86f2d47 | 2023-06-12 11:21:16 +0100 | [diff] [blame] | 81 | time_since = (int(datetime.now().timestamp()) - int(build['complete_at'])) |
| 82 | duration = int(build['complete_at']) - int(build['started_at']) |
| 83 | agent_url = "{}/#/{}/{}".format(base_url, builder_url, build['builderid']) |
David Spickett | 30a986f | 2021-04-29 09:37:00 +0100 | [diff] [blame] | 84 | |
David Spickett | 86f2d47 | 2023-06-12 11:21:16 +0100 | [diff] [blame] | 85 | status = { |
| 86 | 'builder_url': agent_url, |
| 87 | 'number': build['number'], |
| 88 | 'build_url': "{}/builds/{}".format(agent_url, build['number']), |
| 89 | 'state': build['state_string'], |
| 90 | 'time_since': timedelta(seconds=time_since), |
| 91 | 'duration': timedelta(seconds=duration), |
| 92 | 'fail': build['state_string'] != 'build successful', |
| 93 | 'next_in_progress': next_build is not None |
| 94 | } |
David Spickett | 7f18f4d | 2021-03-22 11:49:17 +0000 | [diff] [blame] | 95 | |
David Spickett | 86f2d47 | 2023-06-12 11:21:16 +0100 | [diff] [blame] | 96 | if status['fail']: |
| 97 | buildid = build['buildid'] |
| 98 | status['steps'] = list(get_bot_failing_steps(session, base_url, |
| 99 | buildid)) |
| 100 | |
| 101 | return status |
Adhemerval Zanella | d3e8c48 | 2020-10-12 11:31:48 -0300 | [diff] [blame] | 102 | |
| 103 | |
David Spickett | 85355fa | 2021-03-22 15:41:41 +0000 | [diff] [blame] | 104 | # Get status for all bots named in the config |
| 105 | # Return a dictionary of (base_url, bot name) -> status info |
David Spickett | 30a986f | 2021-04-29 09:37:00 +0100 | [diff] [blame] | 106 | def get_buildbot_bots_status(config): |
Adhemerval Zanella | d3e8c48 | 2020-10-12 11:31:48 -0300 | [diff] [blame] | 107 | session = requests.Session() |
Adhemerval Zanella | d3e8c48 | 2020-10-12 11:31:48 -0300 | [diff] [blame] | 108 | bot_cache = {} |
David Spickett | f006c37 | 2021-03-22 12:54:12 +0000 | [diff] [blame] | 109 | |
Adhemerval Zanella | d3e8c48 | 2020-10-12 11:31:48 -0300 | [diff] [blame] | 110 | for server in filter(not_ignored, config): |
David Spickett | 30a986f | 2021-04-29 09:37:00 +0100 | [diff] [blame] | 111 | if server['name'] == "Buildkite": |
| 112 | continue |
| 113 | |
Adhemerval Zanella | d3e8c48 | 2020-10-12 11:31:48 -0300 | [diff] [blame] | 114 | base_url = server['base_url'] |
Adhemerval Zanella | d3e8c48 | 2020-10-12 11:31:48 -0300 | [diff] [blame] | 115 | logging.debug('Parsing server {}...'.format(server['name'])) |
| 116 | for builder in server['builders']: |
| 117 | logging.debug(' Parsing builders {}...'.format(builder['name'])) |
| 118 | for bot in builder['bots']: |
David Spickett | 85355fa | 2021-03-22 15:41:41 +0000 | [diff] [blame] | 119 | bot_key = (base_url, bot['name']) |
Adhemerval Zanella | d3e8c48 | 2020-10-12 11:31:48 -0300 | [diff] [blame] | 120 | if bot_key in bot_cache: |
| 121 | continue |
David Spickett | 85355fa | 2021-03-22 15:41:41 +0000 | [diff] [blame] | 122 | |
Adhemerval Zanella | d3e8c48 | 2020-10-12 11:31:48 -0300 | [diff] [blame] | 123 | logging.debug(' Parsing bot {}...'.format(bot['name'])) |
David Spickett | 85355fa | 2021-03-22 15:41:41 +0000 | [diff] [blame] | 124 | status = get_bot_status(session, bot['name'], base_url, server['builder_url'], |
| 125 | server['build_url']) |
David Spickett | f2c82dd | 2021-06-24 10:01:33 +0100 | [diff] [blame] | 126 | if status is not None: |
David Spickett | 7d47cea | 2022-06-01 14:13:32 +0100 | [diff] [blame] | 127 | if status.get("valid", True): |
| 128 | logging.debug(" Bot status: " + ("FAIL" if status['fail'] else "PASS")) |
David Spickett | f2c82dd | 2021-06-24 10:01:33 +0100 | [diff] [blame] | 129 | bot_cache[bot_key] = status |
Adhemerval Zanella | d3e8c48 | 2020-10-12 11:31:48 -0300 | [diff] [blame] | 130 | |
David Spickett | 85355fa | 2021-03-22 15:41:41 +0000 | [diff] [blame] | 131 | return bot_cache |
| 132 | |
| 133 | def write_bot_status(config, output_file, bots_status): |
| 134 | temp = tempfile.NamedTemporaryFile(mode='w+', delete=False) |
David Spickett | f006c37 | 2021-03-22 12:54:12 +0000 | [diff] [blame] | 135 | |
David Spickett | 55449c6 | 2021-12-13 12:57:33 +0000 | [diff] [blame] | 136 | temp.write(dedent("""\ |
David Spickett | 64161b0 | 2022-11-01 09:51:30 +0000 | [diff] [blame] | 137 | <!DOCTYPE html> |
David Spickett | 55449c6 | 2021-12-13 12:57:33 +0000 | [diff] [blame] | 138 | <style> |
| 139 | /* Combine the border between cells to prevent 1px gaps |
| 140 | in the row background colour. */ |
| 141 | table, td, th { |
| 142 | border-collapse: collapse; |
| 143 | } |
| 144 | /* Colour every other row in a table body grey. */ |
| 145 | tbody tr:nth-child(even) td { |
| 146 | background-color: #ededed; |
| 147 | } |
| 148 | </style>""")) |
| 149 | |
David Spickett | e44441b | 2023-06-12 12:23:28 +0100 | [diff] [blame] | 150 | column_titles = [ |
| 151 | "Buildbot", |
| 152 | "Status", |
| 153 | "T Since", |
| 154 | "Duration", |
| 155 | "Build", |
| 156 | "Failing steps", |
| 157 | "Build In Progress", |
| 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 | |
| 163 | # The first table should also say when this was generated. |
| 164 | # If we were to put this in its own header only table, it would |
| 165 | # not align with the rest because it has no content. |
| 166 | first = True |
| 167 | |
Adhemerval Zanella | d3e8c48 | 2020-10-12 11:31:48 -0300 | [diff] [blame] | 168 | # Dump all servers / bots |
| 169 | for server in filter(not_ignored, config): |
| 170 | base_url = server['base_url'] |
| 171 | builder_url = server['builder_url'] |
| 172 | build_url = server['build_url'] |
David Spickett | ec94cc2 | 2021-12-13 13:16:05 +0000 | [diff] [blame] | 173 | |
David Spickett | 55449c6 | 2021-12-13 12:57:33 +0000 | [diff] [blame] | 174 | temp.write("<table border=0 cellspacing=1 cellpadding=2>\n") |
David Spickett | ec94cc2 | 2021-12-13 13:16:05 +0000 | [diff] [blame] | 175 | temp.write("<tr><td colspan={}> </td><tr>\n".format(num_columns)) |
David Spickett | e44441b | 2023-06-12 12:23:28 +0100 | [diff] [blame] | 176 | if first: |
David Spickett | 8822bbd | 2023-06-12 14:02:41 +0100 | [diff] [blame^] | 177 | temp.write("<tr><th colspan={}>Generated {} ({})</td><tr>\n" |
| 178 | .format(num_columns, datetime.today().ctime(), |
| 179 | time.tzname[time.daylight])) |
David Spickett | e44441b | 2023-06-12 12:23:28 +0100 | [diff] [blame] | 180 | temp.write("<tr><td colspan={}> </td><tr>\n".format(num_columns)) |
| 181 | first = False |
| 182 | |
| 183 | temp.write("<tr><th colspan={}>{}</td><tr>\n" |
| 184 | .format(num_columns, server['name'])) |
Adhemerval Zanella | d3e8c48 | 2020-10-12 11:31:48 -0300 | [diff] [blame] | 185 | |
| 186 | for builder in server['builders']: |
David Spickett | ec94cc2 | 2021-12-13 13:16:05 +0000 | [diff] [blame] | 187 | temp.write("<tr><td colspan={}> </td><tr>\n".format(num_columns)) |
| 188 | temp.write("<tr><th colspan={}>{}</th><tr>\n".format(num_columns, builder['name'])) |
| 189 | temp.write(column_titles_html) |
David Spickett | 55449c6 | 2021-12-13 12:57:33 +0000 | [diff] [blame] | 190 | temp.write("<tbody>\n") |
Adhemerval Zanella | d3e8c48 | 2020-10-12 11:31:48 -0300 | [diff] [blame] | 191 | for bot in builder['bots']: |
| 192 | temp.write("<tr>\n") |
David Spickett | 7d47cea | 2022-06-01 14:13:32 +0100 | [diff] [blame] | 193 | logging.debug("Writing out status for {}".format(bot['name'])) |
David Spickett | f2c82dd | 2021-06-24 10:01:33 +0100 | [diff] [blame] | 194 | try: |
| 195 | status = bots_status[(base_url, bot['name'])] |
| 196 | except KeyError: |
David Spickett | ec94cc2 | 2021-12-13 13:16:05 +0000 | [diff] [blame] | 197 | temp.write(" <td colspan={}>{} is offline!</td>\n</tr>\n" |
| 198 | .format(num_columns, bot['name'])) |
David Spickett | f2c82dd | 2021-06-24 10:01:33 +0100 | [diff] [blame] | 199 | continue |
David Spickett | 30a986f | 2021-04-29 09:37:00 +0100 | [diff] [blame] | 200 | else: |
| 201 | if not status.get('valid', True): |
David Spickett | ec94cc2 | 2021-12-13 13:16:05 +0000 | [diff] [blame] | 202 | temp.write(" <td colspan={}>Could not read status for {}!</td>\n</tr>\n" |
| 203 | .format(num_columns, bot['name'])) |
David Spickett | 30a986f | 2021-04-29 09:37:00 +0100 | [diff] [blame] | 204 | continue |
David Spickett | f2c82dd | 2021-06-24 10:01:33 +0100 | [diff] [blame] | 205 | |
David Spickett | 30a986f | 2021-04-29 09:37:00 +0100 | [diff] [blame] | 206 | temp.write(" <td><a href='{}'>{}</a></td>\n".format( |
| 207 | status['builder_url'], bot['name'])) |
Adhemerval Zanella | d3e8c48 | 2020-10-12 11:31:48 -0300 | [diff] [blame] | 208 | temp.write(" <td><font color='{}'>{}</font></td>\n" |
| 209 | .format('red' if status['fail'] else 'green', |
| 210 | 'FAIL' if status['fail'] else 'PASS')) |
| 211 | empty_cell=" <td> </td>\n" |
Maxim Kuvyrkov | fdaa468 | 2021-04-14 13:13:14 +0000 | [diff] [blame] | 212 | if 'time_since' in status: |
David Spickett | 187f796 | 2022-02-09 12:35:00 +0000 | [diff] [blame] | 213 | time_since = status['time_since'] |
| 214 | # No build should be taking more than a day |
| 215 | if time_since > timedelta(hours=24): |
| 216 | time_since = "<p style=\"color:red\">{}</p>".format( |
| 217 | time_since) |
| 218 | else: |
| 219 | time_since = str(time_since) |
| 220 | |
| 221 | temp.write(" <td>{}</td>\n".format(time_since)) |
Maxim Kuvyrkov | fdaa468 | 2021-04-14 13:13:14 +0000 | [diff] [blame] | 222 | else: |
| 223 | temp.write(empty_cell) |
| 224 | if 'duration' in status: |
| 225 | temp.write(" <td>{}</td>\n".format(status['duration'])) |
Adhemerval Zanella | d3e8c48 | 2020-10-12 11:31:48 -0300 | [diff] [blame] | 226 | else: |
| 227 | temp.write(empty_cell) |
| 228 | if 'number' in status: |
David Spickett | 30a986f | 2021-04-29 09:37:00 +0100 | [diff] [blame] | 229 | temp.write(" <td><a href='{}'>{}</a></td>\n".format( |
| 230 | status['build_url'], status['number'])) |
Adhemerval Zanella | d3e8c48 | 2020-10-12 11:31:48 -0300 | [diff] [blame] | 231 | else: |
| 232 | temp.write(empty_cell) |
Oliver Stannard | 91688ff | 2021-01-07 10:27:27 +0000 | [diff] [blame] | 233 | if 'steps' in status and status['steps']: |
| 234 | def render_step(name, result): |
| 235 | return "<font color='{}'>{}</font>".format(RESULT_COLORS[result], name) |
| 236 | step_list = ', '.join(render_step(name, result) for name, result in status['steps']) |
| 237 | temp.write(" <td style=\"text-align:center\">{}</td>\n".format(step_list)) |
Adhemerval Zanella | d3e8c48 | 2020-10-12 11:31:48 -0300 | [diff] [blame] | 238 | else: |
| 239 | temp.write(empty_cell) |
David Spickett | 86f2d47 | 2023-06-12 11:21:16 +0100 | [diff] [blame] | 240 | if 'next_in_progress' in status: |
| 241 | temp.write(" <td>{}</td>\n".format( |
| 242 | "Yes" if status['next_in_progress'] else "No")) |
| 243 | else: |
| 244 | # No value means we don't know either way. |
| 245 | temp.write(empty_cell) |
Adhemerval Zanella | d3e8c48 | 2020-10-12 11:31:48 -0300 | [diff] [blame] | 246 | temp.write("</tr>\n") |
David Spickett | 55449c6 | 2021-12-13 12:57:33 +0000 | [diff] [blame] | 247 | temp.write("</tbody>\n") |
Adhemerval Zanella | d3e8c48 | 2020-10-12 11:31:48 -0300 | [diff] [blame] | 248 | temp.write("</table>\n") |
| 249 | |
| 250 | # Move temp to main (atomic change) |
| 251 | temp.close() |
David Spickett | 7f18f4d | 2021-03-22 11:49:17 +0000 | [diff] [blame] | 252 | shutil.move(temp.name, output_file) |
Adhemerval Zanella | d3e8c48 | 2020-10-12 11:31:48 -0300 | [diff] [blame] | 253 | |
| 254 | |
| 255 | if __name__ == "__main__": |
| 256 | parser = argparse.ArgumentParser() |
David Spickett | ec2166c | 2021-07-19 14:17:29 +0100 | [diff] [blame] | 257 | parser.add_argument('-d', dest='debug', action='store_true', |
| 258 | help='show debug log messages') |
Adhemerval Zanella | d3e8c48 | 2020-10-12 11:31:48 -0300 | [diff] [blame] | 259 | parser.add_argument('config_file', |
| 260 | help='Bots description in JSON format') |
| 261 | parser.add_argument('output_file', |
| 262 | help='output HTML path') |
| 263 | args = parser.parse_args() |
| 264 | |
| 265 | if args.debug: |
| 266 | logging.basicConfig(stream=sys.stderr, level=logging.DEBUG) |
| 267 | |
| 268 | try: |
| 269 | with open(args.config_file, "r") as f: |
| 270 | config = json.load(f) |
| 271 | except IOError as e: |
David Spickett | 7f18f4d | 2021-03-22 11:49:17 +0000 | [diff] [blame] | 272 | print("error: failed to read {} config file: {}".format(args.config_file, e)) |
Adhemerval Zanella | d3e8c48 | 2020-10-12 11:31:48 -0300 | [diff] [blame] | 273 | sys.exit(os.EX_CONFIG) |
| 274 | |
David Spickett | 30a986f | 2021-04-29 09:37:00 +0100 | [diff] [blame] | 275 | status = get_buildbot_bots_status(config) |
| 276 | status.update(get_buildkite_bots_status(config)) |
| 277 | write_bot_status(config, args.output_file, status) |