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 |
| 22 | |
David Spickett | 30a986f | 2021-04-29 09:37:00 +0100 | [diff] [blame] | 23 | from buildkite_status import get_buildkite_bots_status |
| 24 | |
Adhemerval Zanella | d3e8c48 | 2020-10-12 11:31:48 -0300 | [diff] [blame] | 25 | # The GIT revision length used on 'Commits' error display. |
| 26 | GIT_SHORT_LEN=7 |
| 27 | |
| 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 | |
| 41 | # Returns a string with the GIT revision usesd on build BUILDID and |
| 42 | # PREV_BUILDID in the form '<id_buildid>-<id_prev_buildid>'. |
| 43 | def get_bot_failure_changes(session, base_url, buildid, prev_buildid): |
| 44 | def wget_build_rev(bid): |
David Spickett | e88fe59 | 2021-03-22 12:25:13 +0000 | [diff] [blame] | 45 | try: |
| 46 | contents = wget(session, |
| 47 | "{}/api/v2/builds/{}/changes" |
| 48 | .format(base_url, bid)) |
| 49 | except requests.exceptions.RequestException: |
David Spickett | 8306ed8 | 2021-12-06 10:40:36 +0000 | [diff] [blame^] | 50 | logging.debug(" Couldn't get changes for build {}!".format(buildid)) |
David Spickett | 7f18f4d | 2021-03-22 11:49:17 +0000 | [diff] [blame] | 51 | return None |
David Spickett | e88fe59 | 2021-03-22 12:25:13 +0000 | [diff] [blame] | 52 | changes = contents['changes'] |
| 53 | if changes: |
| 54 | return changes[0]['revision'] |
| 55 | return None |
Adhemerval Zanella | d3e8c48 | 2020-10-12 11:31:48 -0300 | [diff] [blame] | 56 | |
David Spickett | 7f18f4d | 2021-03-22 11:49:17 +0000 | [diff] [blame] | 57 | revision = wget_build_rev(buildid)[:GIT_SHORT_LEN] |
| 58 | prev_revision = None |
| 59 | if prev_buildid is not None: |
| 60 | prev_revision = wget_build_rev(prev_buildid) |
| 61 | |
| 62 | if prev_revision is None: |
| 63 | return "{}".format(revision) |
Adhemerval Zanella | d3e8c48 | 2020-10-12 11:31:48 -0300 | [diff] [blame] | 64 | else: |
David Spickett | 7f18f4d | 2021-03-22 11:49:17 +0000 | [diff] [blame] | 65 | return "{}-{}".format(revision, prev_revision[:GIT_SHORT_LEN]) |
Adhemerval Zanella | d3e8c48 | 2020-10-12 11:31:48 -0300 | [diff] [blame] | 66 | |
| 67 | |
Oliver Stannard | 91688ff | 2021-01-07 10:27:27 +0000 | [diff] [blame] | 68 | # Map from buildbot status codes we want to treat as errors to the color they |
| 69 | # should be shown in. The codes are documented at |
| 70 | # https://docs.buildbot.net/latest/developer/results.html#build-result-codes, |
| 71 | # and these colors match the suggested ones there. |
| 72 | RESULT_COLORS = { |
| 73 | 2: 'red', # Error |
| 74 | 4: 'purple', # Exception |
| 75 | 5: 'purple', # Retry |
| 76 | 6: 'pink', # Cancelled |
| 77 | } |
| 78 | |
| 79 | def get_bot_failing_steps(session, base_url, buildid): |
David Spickett | e88fe59 | 2021-03-22 12:25:13 +0000 | [diff] [blame] | 80 | try: |
| 81 | contents = wget(session, "{}/api/v2/builds/{}/steps" |
| 82 | .format(base_url, buildid)) |
| 83 | except requests.exceptions.RequestException: |
Oliver Stannard | 91688ff | 2021-01-07 10:27:27 +0000 | [diff] [blame] | 84 | return "" |
David Spickett | e88fe59 | 2021-03-22 12:25:13 +0000 | [diff] [blame] | 85 | |
Oliver Stannard | 91688ff | 2021-01-07 10:27:27 +0000 | [diff] [blame] | 86 | for step in contents["steps"]: |
David Spickett | 7f18f4d | 2021-03-22 11:49:17 +0000 | [diff] [blame] | 87 | if step["results"] in RESULT_COLORS: |
Oliver Stannard | 91688ff | 2021-01-07 10:27:27 +0000 | [diff] [blame] | 88 | yield (step["name"], step["results"]) |
| 89 | |
| 90 | |
Adhemerval Zanella | d3e8c48 | 2020-10-12 11:31:48 -0300 | [diff] [blame] | 91 | # Get the status of a individual bot BOT. Returns a dict with the |
| 92 | # information. |
| 93 | def get_bot_status(session, bot, base_url, builder_url, build_url): |
David Spickett | e88fe59 | 2021-03-22 12:25:13 +0000 | [diff] [blame] | 94 | try: |
| 95 | builds = wget(session, |
| 96 | "{}/api/v2/{}/{}/{}" |
| 97 | .format(base_url, builder_url, bot, build_url)) |
| 98 | except requests.exceptions.RequestException as e: |
| 99 | return {'fail': True} |
Adhemerval Zanella | d3e8c48 | 2020-10-12 11:31:48 -0300 | [diff] [blame] | 100 | |
Oliver Stannard | 46e9903 | 2021-01-05 10:30:56 +0000 | [diff] [blame] | 101 | reversed_builds = iter(sorted(builds['builds'], key=lambda b: -b["number"])) |
Adhemerval Zanella | d3e8c48 | 2020-10-12 11:31:48 -0300 | [diff] [blame] | 102 | for build in reversed_builds: |
| 103 | if build['complete']: |
Maxim Kuvyrkov | fdaa468 | 2021-04-14 13:13:14 +0000 | [diff] [blame] | 104 | time_since = (int(datetime.now().timestamp()) - int(build['complete_at'])) |
| 105 | duration = int(build['complete_at']) - int(build['started_at']) |
David Spickett | 30a986f | 2021-04-29 09:37:00 +0100 | [diff] [blame] | 106 | agent_url = "{}/#/{}/{}".format(base_url, builder_url, build['builderid']) |
| 107 | |
David Spickett | 7f18f4d | 2021-03-22 11:49:17 +0000 | [diff] [blame] | 108 | status = { |
David Spickett | 30a986f | 2021-04-29 09:37:00 +0100 | [diff] [blame] | 109 | 'builder_url': agent_url, |
David Spickett | 7f18f4d | 2021-03-22 11:49:17 +0000 | [diff] [blame] | 110 | 'number': build['number'], |
David Spickett | 30a986f | 2021-04-29 09:37:00 +0100 | [diff] [blame] | 111 | 'build_url': "{}/builds/{}".format(agent_url, build['number']), |
David Spickett | 7f18f4d | 2021-03-22 11:49:17 +0000 | [diff] [blame] | 112 | 'state': build['state_string'], |
Maxim Kuvyrkov | fdaa468 | 2021-04-14 13:13:14 +0000 | [diff] [blame] | 113 | 'time_since': timedelta(seconds=time_since), |
| 114 | 'duration': timedelta(seconds=duration), |
David Spickett | 7f18f4d | 2021-03-22 11:49:17 +0000 | [diff] [blame] | 115 | 'fail': build['state_string'] != 'build successful', |
| 116 | } |
David Spickett | 30a986f | 2021-04-29 09:37:00 +0100 | [diff] [blame] | 117 | |
David Spickett | 7f18f4d | 2021-03-22 11:49:17 +0000 | [diff] [blame] | 118 | if status['fail']: |
| 119 | buildid = build['buildid'] |
| 120 | prev_buildid = next(reversed_builds, None)['buildid'] |
Adhemerval Zanella | d3e8c48 | 2020-10-12 11:31:48 -0300 | [diff] [blame] | 121 | status['changes'] = get_bot_failure_changes(session, base_url, |
David Spickett | 7f18f4d | 2021-03-22 11:49:17 +0000 | [diff] [blame] | 122 | buildid, |
Adhemerval Zanella | d3e8c48 | 2020-10-12 11:31:48 -0300 | [diff] [blame] | 123 | prev_buildid) |
Oliver Stannard | 91688ff | 2021-01-07 10:27:27 +0000 | [diff] [blame] | 124 | status['steps'] = list(get_bot_failing_steps(session, base_url, |
David Spickett | 7f18f4d | 2021-03-22 11:49:17 +0000 | [diff] [blame] | 125 | buildid)) |
| 126 | |
| 127 | return status |
Adhemerval Zanella | d3e8c48 | 2020-10-12 11:31:48 -0300 | [diff] [blame] | 128 | |
| 129 | |
David Spickett | 85355fa | 2021-03-22 15:41:41 +0000 | [diff] [blame] | 130 | # Get status for all bots named in the config |
| 131 | # Return a dictionary of (base_url, bot name) -> status info |
David Spickett | 30a986f | 2021-04-29 09:37:00 +0100 | [diff] [blame] | 132 | def get_buildbot_bots_status(config): |
Adhemerval Zanella | d3e8c48 | 2020-10-12 11:31:48 -0300 | [diff] [blame] | 133 | session = requests.Session() |
Adhemerval Zanella | d3e8c48 | 2020-10-12 11:31:48 -0300 | [diff] [blame] | 134 | bot_cache = {} |
David Spickett | f006c37 | 2021-03-22 12:54:12 +0000 | [diff] [blame] | 135 | |
Adhemerval Zanella | d3e8c48 | 2020-10-12 11:31:48 -0300 | [diff] [blame] | 136 | for server in filter(not_ignored, config): |
David Spickett | 30a986f | 2021-04-29 09:37:00 +0100 | [diff] [blame] | 137 | if server['name'] == "Buildkite": |
| 138 | continue |
| 139 | |
Adhemerval Zanella | d3e8c48 | 2020-10-12 11:31:48 -0300 | [diff] [blame] | 140 | base_url = server['base_url'] |
Adhemerval Zanella | d3e8c48 | 2020-10-12 11:31:48 -0300 | [diff] [blame] | 141 | logging.debug('Parsing server {}...'.format(server['name'])) |
| 142 | for builder in server['builders']: |
| 143 | logging.debug(' Parsing builders {}...'.format(builder['name'])) |
| 144 | for bot in builder['bots']: |
David Spickett | 85355fa | 2021-03-22 15:41:41 +0000 | [diff] [blame] | 145 | bot_key = (base_url, bot['name']) |
Adhemerval Zanella | d3e8c48 | 2020-10-12 11:31:48 -0300 | [diff] [blame] | 146 | if bot_key in bot_cache: |
| 147 | continue |
David Spickett | 85355fa | 2021-03-22 15:41:41 +0000 | [diff] [blame] | 148 | |
Adhemerval Zanella | d3e8c48 | 2020-10-12 11:31:48 -0300 | [diff] [blame] | 149 | logging.debug(' Parsing bot {}...'.format(bot['name'])) |
David Spickett | 85355fa | 2021-03-22 15:41:41 +0000 | [diff] [blame] | 150 | status = get_bot_status(session, bot['name'], base_url, server['builder_url'], |
| 151 | server['build_url']) |
David Spickett | f2c82dd | 2021-06-24 10:01:33 +0100 | [diff] [blame] | 152 | if status is not None: |
David Spickett | 8306ed8 | 2021-12-06 10:40:36 +0000 | [diff] [blame^] | 153 | logging.debug(" Bot status: " + ("FAIL" if status['fail'] else "PASS")) |
David Spickett | f2c82dd | 2021-06-24 10:01:33 +0100 | [diff] [blame] | 154 | bot_cache[bot_key] = status |
Adhemerval Zanella | d3e8c48 | 2020-10-12 11:31:48 -0300 | [diff] [blame] | 155 | |
David Spickett | 85355fa | 2021-03-22 15:41:41 +0000 | [diff] [blame] | 156 | return bot_cache |
| 157 | |
| 158 | def write_bot_status(config, output_file, bots_status): |
| 159 | temp = tempfile.NamedTemporaryFile(mode='w+', delete=False) |
| 160 | today = "{}\n".format(datetime.today().ctime()) |
| 161 | # Whether we use the fail favicon or not |
| 162 | found_failure = False |
David Spickett | f006c37 | 2021-03-22 12:54:12 +0000 | [diff] [blame] | 163 | |
Adhemerval Zanella | d3e8c48 | 2020-10-12 11:31:48 -0300 | [diff] [blame] | 164 | # Dump all servers / bots |
| 165 | for server in filter(not_ignored, config): |
| 166 | base_url = server['base_url'] |
| 167 | builder_url = server['builder_url'] |
| 168 | build_url = server['build_url'] |
Adhemerval Zanella | d3e8c48 | 2020-10-12 11:31:48 -0300 | [diff] [blame] | 169 | temp.write("<table cellspacing=1 cellpadding=2>\n") |
| 170 | temp.write("<tr><td colspan=5> </td><tr>\n") |
| 171 | temp.write("<tr><th colspan=5>{} @ {}</td><tr>\n" |
| 172 | .format(server['name'], today)) |
| 173 | |
| 174 | for builder in server['builders']: |
| 175 | temp.write("<tr><td colspan=5> </td><tr>\n") |
| 176 | temp.write("<tr><th colspan=5>{}</td><tr>\n".format(builder['name'])) |
Maxim Kuvyrkov | fdaa468 | 2021-04-14 13:13:14 +0000 | [diff] [blame] | 177 | temp.write("<tr><th>Buildbot</th><th>Status</th><th>T Since</th>" |
| 178 | "<th>Duration</th><th>Build #</th><th>Commits</th>" |
| 179 | "<th>Failing steps</th></tr>\n") |
Adhemerval Zanella | d3e8c48 | 2020-10-12 11:31:48 -0300 | [diff] [blame] | 180 | for bot in builder['bots']: |
| 181 | temp.write("<tr>\n") |
David Spickett | f2c82dd | 2021-06-24 10:01:33 +0100 | [diff] [blame] | 182 | try: |
| 183 | status = bots_status[(base_url, bot['name'])] |
| 184 | except KeyError: |
David Spickett | f2d4c48 | 2021-06-24 10:07:52 +0100 | [diff] [blame] | 185 | temp.write(" <td>{} is offline!</td>\n</tr>\n".format(bot['name'])) |
David Spickett | f2c82dd | 2021-06-24 10:01:33 +0100 | [diff] [blame] | 186 | continue |
David Spickett | 30a986f | 2021-04-29 09:37:00 +0100 | [diff] [blame] | 187 | else: |
| 188 | if not status.get('valid', True): |
| 189 | temp.write(" <td>Could not read status for {}!</td>\n</tr>\n".format(bot['name'])) |
| 190 | continue |
David Spickett | f2c82dd | 2021-06-24 10:01:33 +0100 | [diff] [blame] | 191 | |
David Spickett | 85355fa | 2021-03-22 15:41:41 +0000 | [diff] [blame] | 192 | found_failure |= status['fail'] |
David Spickett | 30a986f | 2021-04-29 09:37:00 +0100 | [diff] [blame] | 193 | |
| 194 | temp.write(" <td><a href='{}'>{}</a></td>\n".format( |
| 195 | status['builder_url'], bot['name'])) |
Adhemerval Zanella | d3e8c48 | 2020-10-12 11:31:48 -0300 | [diff] [blame] | 196 | temp.write(" <td><font color='{}'>{}</font></td>\n" |
| 197 | .format('red' if status['fail'] else 'green', |
| 198 | 'FAIL' if status['fail'] else 'PASS')) |
| 199 | empty_cell=" <td> </td>\n" |
Maxim Kuvyrkov | fdaa468 | 2021-04-14 13:13:14 +0000 | [diff] [blame] | 200 | if 'time_since' in status: |
| 201 | temp.write(" <td>{}</td>\n".format(status['time_since'])) |
| 202 | else: |
| 203 | temp.write(empty_cell) |
| 204 | if 'duration' in status: |
| 205 | temp.write(" <td>{}</td>\n".format(status['duration'])) |
Adhemerval Zanella | d3e8c48 | 2020-10-12 11:31:48 -0300 | [diff] [blame] | 206 | else: |
| 207 | temp.write(empty_cell) |
| 208 | if 'number' in status: |
David Spickett | 30a986f | 2021-04-29 09:37:00 +0100 | [diff] [blame] | 209 | temp.write(" <td><a href='{}'>{}</a></td>\n".format( |
| 210 | status['build_url'], status['number'])) |
Adhemerval Zanella | d3e8c48 | 2020-10-12 11:31:48 -0300 | [diff] [blame] | 211 | else: |
| 212 | temp.write(empty_cell) |
| 213 | if 'changes' in status: |
| 214 | temp.write(" <td>{}</td>\n".format(status['changes'])) |
| 215 | else: |
| 216 | temp.write(empty_cell) |
Oliver Stannard | 91688ff | 2021-01-07 10:27:27 +0000 | [diff] [blame] | 217 | if 'steps' in status and status['steps']: |
| 218 | def render_step(name, result): |
| 219 | return "<font color='{}'>{}</font>".format(RESULT_COLORS[result], name) |
| 220 | step_list = ', '.join(render_step(name, result) for name, result in status['steps']) |
| 221 | temp.write(" <td style=\"text-align:center\">{}</td>\n".format(step_list)) |
Adhemerval Zanella | d3e8c48 | 2020-10-12 11:31:48 -0300 | [diff] [blame] | 222 | else: |
| 223 | temp.write(empty_cell) |
| 224 | temp.write("</tr>\n") |
| 225 | temp.write("</table>\n") |
| 226 | |
David Spickett | 85355fa | 2021-03-22 15:41:41 +0000 | [diff] [blame] | 227 | temp.write("<link rel=\"shortcut icon\" href=\"{}\" " |
| 228 | "type=\"image/x-icon\"/>\n".format( |
| 229 | 'fail.ico' if found_failure else 'ok.ico')) |
| 230 | |
Adhemerval Zanella | d3e8c48 | 2020-10-12 11:31:48 -0300 | [diff] [blame] | 231 | # Move temp to main (atomic change) |
| 232 | temp.close() |
David Spickett | 7f18f4d | 2021-03-22 11:49:17 +0000 | [diff] [blame] | 233 | shutil.move(temp.name, output_file) |
Adhemerval Zanella | d3e8c48 | 2020-10-12 11:31:48 -0300 | [diff] [blame] | 234 | |
| 235 | |
| 236 | if __name__ == "__main__": |
| 237 | parser = argparse.ArgumentParser() |
David Spickett | ec2166c | 2021-07-19 14:17:29 +0100 | [diff] [blame] | 238 | parser.add_argument('-d', dest='debug', action='store_true', |
| 239 | help='show debug log messages') |
Adhemerval Zanella | d3e8c48 | 2020-10-12 11:31:48 -0300 | [diff] [blame] | 240 | parser.add_argument('config_file', |
| 241 | help='Bots description in JSON format') |
| 242 | parser.add_argument('output_file', |
| 243 | help='output HTML path') |
| 244 | args = parser.parse_args() |
| 245 | |
| 246 | if args.debug: |
| 247 | logging.basicConfig(stream=sys.stderr, level=logging.DEBUG) |
| 248 | |
| 249 | try: |
| 250 | with open(args.config_file, "r") as f: |
| 251 | config = json.load(f) |
| 252 | except IOError as e: |
David Spickett | 7f18f4d | 2021-03-22 11:49:17 +0000 | [diff] [blame] | 253 | print("error: failed to read {} config file: {}".format(args.config_file, e)) |
Adhemerval Zanella | d3e8c48 | 2020-10-12 11:31:48 -0300 | [diff] [blame] | 254 | sys.exit(os.EX_CONFIG) |
| 255 | |
David Spickett | 30a986f | 2021-04-29 09:37:00 +0100 | [diff] [blame] | 256 | status = get_buildbot_bots_status(config) |
| 257 | status.update(get_buildkite_bots_status(config)) |
| 258 | write_bot_status(config, args.output_file, status) |