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