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