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 | e88fe59 | 2021-03-22 12:25:13 +0000 | [diff] [blame] | 36 | return session.get(url).json() |
Adhemerval Zanella | d3e8c48 | 2020-10-12 11:31:48 -0300 | [diff] [blame] | 37 | |
| 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>'. |
| 41 | def get_bot_failure_changes(session, base_url, buildid, prev_buildid): |
| 42 | def wget_build_rev(bid): |
David Spickett | e88fe59 | 2021-03-22 12:25:13 +0000 | [diff] [blame] | 43 | try: |
| 44 | contents = wget(session, |
| 45 | "{}/api/v2/builds/{}/changes" |
| 46 | .format(base_url, bid)) |
| 47 | except requests.exceptions.RequestException: |
David Spickett | 7f18f4d | 2021-03-22 11:49:17 +0000 | [diff] [blame] | 48 | return None |
David Spickett | e88fe59 | 2021-03-22 12:25:13 +0000 | [diff] [blame] | 49 | changes = contents['changes'] |
| 50 | if changes: |
| 51 | return changes[0]['revision'] |
| 52 | return None |
Adhemerval Zanella | d3e8c48 | 2020-10-12 11:31:48 -0300 | [diff] [blame] | 53 | |
David Spickett | 7f18f4d | 2021-03-22 11:49:17 +0000 | [diff] [blame] | 54 | 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 Zanella | d3e8c48 | 2020-10-12 11:31:48 -0300 | [diff] [blame] | 61 | else: |
David Spickett | 7f18f4d | 2021-03-22 11:49:17 +0000 | [diff] [blame] | 62 | return "{}-{}".format(revision, prev_revision[:GIT_SHORT_LEN]) |
Adhemerval Zanella | d3e8c48 | 2020-10-12 11:31:48 -0300 | [diff] [blame] | 63 | |
| 64 | |
Oliver Stannard | 91688ff | 2021-01-07 10:27:27 +0000 | [diff] [blame] | 65 | # 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. |
| 69 | RESULT_COLORS = { |
| 70 | 2: 'red', # Error |
| 71 | 4: 'purple', # Exception |
| 72 | 5: 'purple', # Retry |
| 73 | 6: 'pink', # Cancelled |
| 74 | } |
| 75 | |
| 76 | def get_bot_failing_steps(session, base_url, buildid): |
David Spickett | e88fe59 | 2021-03-22 12:25:13 +0000 | [diff] [blame] | 77 | try: |
| 78 | contents = wget(session, "{}/api/v2/builds/{}/steps" |
| 79 | .format(base_url, buildid)) |
| 80 | except requests.exceptions.RequestException: |
Oliver Stannard | 91688ff | 2021-01-07 10:27:27 +0000 | [diff] [blame] | 81 | return "" |
David Spickett | e88fe59 | 2021-03-22 12:25:13 +0000 | [diff] [blame] | 82 | |
Oliver Stannard | 91688ff | 2021-01-07 10:27:27 +0000 | [diff] [blame] | 83 | for step in contents["steps"]: |
David Spickett | 7f18f4d | 2021-03-22 11:49:17 +0000 | [diff] [blame] | 84 | if step["results"] in RESULT_COLORS: |
Oliver Stannard | 91688ff | 2021-01-07 10:27:27 +0000 | [diff] [blame] | 85 | yield (step["name"], step["results"]) |
| 86 | |
| 87 | |
Adhemerval Zanella | d3e8c48 | 2020-10-12 11:31:48 -0300 | [diff] [blame] | 88 | # Get the status of a individual bot BOT. Returns a dict with the |
| 89 | # information. |
| 90 | def get_bot_status(session, bot, base_url, builder_url, build_url): |
David Spickett | e88fe59 | 2021-03-22 12:25:13 +0000 | [diff] [blame] | 91 | 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 Zanella | d3e8c48 | 2020-10-12 11:31:48 -0300 | [diff] [blame] | 97 | |
Oliver Stannard | 46e9903 | 2021-01-05 10:30:56 +0000 | [diff] [blame] | 98 | reversed_builds = iter(sorted(builds['builds'], key=lambda b: -b["number"])) |
Adhemerval Zanella | d3e8c48 | 2020-10-12 11:31:48 -0300 | [diff] [blame] | 99 | for build in reversed_builds: |
| 100 | if build['complete']: |
Maxim Kuvyrkov | fdaa468 | 2021-04-14 13:13:14 +0000 | [diff] [blame] | 101 | time_since = (int(datetime.now().timestamp()) - int(build['complete_at'])) |
| 102 | duration = int(build['complete_at']) - int(build['started_at']) |
David Spickett | 30a986f | 2021-04-29 09:37:00 +0100 | [diff] [blame^] | 103 | agent_url = "{}/#/{}/{}".format(base_url, builder_url, build['builderid']) |
| 104 | |
David Spickett | 7f18f4d | 2021-03-22 11:49:17 +0000 | [diff] [blame] | 105 | status = { |
David Spickett | 30a986f | 2021-04-29 09:37:00 +0100 | [diff] [blame^] | 106 | 'builder_url': agent_url, |
David Spickett | 7f18f4d | 2021-03-22 11:49:17 +0000 | [diff] [blame] | 107 | 'number': build['number'], |
David Spickett | 30a986f | 2021-04-29 09:37:00 +0100 | [diff] [blame^] | 108 | 'build_url': "{}/builds/{}".format(agent_url, build['number']), |
David Spickett | 7f18f4d | 2021-03-22 11:49:17 +0000 | [diff] [blame] | 109 | 'state': build['state_string'], |
Maxim Kuvyrkov | fdaa468 | 2021-04-14 13:13:14 +0000 | [diff] [blame] | 110 | 'time_since': timedelta(seconds=time_since), |
| 111 | 'duration': timedelta(seconds=duration), |
David Spickett | 7f18f4d | 2021-03-22 11:49:17 +0000 | [diff] [blame] | 112 | 'fail': build['state_string'] != 'build successful', |
| 113 | } |
David Spickett | 30a986f | 2021-04-29 09:37:00 +0100 | [diff] [blame^] | 114 | |
David Spickett | 7f18f4d | 2021-03-22 11:49:17 +0000 | [diff] [blame] | 115 | if status['fail']: |
| 116 | buildid = build['buildid'] |
| 117 | prev_buildid = next(reversed_builds, None)['buildid'] |
Adhemerval Zanella | d3e8c48 | 2020-10-12 11:31:48 -0300 | [diff] [blame] | 118 | status['changes'] = get_bot_failure_changes(session, base_url, |
David Spickett | 7f18f4d | 2021-03-22 11:49:17 +0000 | [diff] [blame] | 119 | buildid, |
Adhemerval Zanella | d3e8c48 | 2020-10-12 11:31:48 -0300 | [diff] [blame] | 120 | prev_buildid) |
Oliver Stannard | 91688ff | 2021-01-07 10:27:27 +0000 | [diff] [blame] | 121 | status['steps'] = list(get_bot_failing_steps(session, base_url, |
David Spickett | 7f18f4d | 2021-03-22 11:49:17 +0000 | [diff] [blame] | 122 | buildid)) |
| 123 | |
| 124 | return status |
Adhemerval Zanella | d3e8c48 | 2020-10-12 11:31:48 -0300 | [diff] [blame] | 125 | |
| 126 | |
David Spickett | 85355fa | 2021-03-22 15:41:41 +0000 | [diff] [blame] | 127 | # Get status for all bots named in the config |
| 128 | # Return a dictionary of (base_url, bot name) -> status info |
David Spickett | 30a986f | 2021-04-29 09:37:00 +0100 | [diff] [blame^] | 129 | def get_buildbot_bots_status(config): |
Adhemerval Zanella | d3e8c48 | 2020-10-12 11:31:48 -0300 | [diff] [blame] | 130 | session = requests.Session() |
Adhemerval Zanella | d3e8c48 | 2020-10-12 11:31:48 -0300 | [diff] [blame] | 131 | bot_cache = {} |
David Spickett | f006c37 | 2021-03-22 12:54:12 +0000 | [diff] [blame] | 132 | |
Adhemerval Zanella | d3e8c48 | 2020-10-12 11:31:48 -0300 | [diff] [blame] | 133 | for server in filter(not_ignored, config): |
David Spickett | 30a986f | 2021-04-29 09:37:00 +0100 | [diff] [blame^] | 134 | if server['name'] == "Buildkite": |
| 135 | continue |
| 136 | |
Adhemerval Zanella | d3e8c48 | 2020-10-12 11:31:48 -0300 | [diff] [blame] | 137 | base_url = server['base_url'] |
Adhemerval Zanella | d3e8c48 | 2020-10-12 11:31:48 -0300 | [diff] [blame] | 138 | 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 Spickett | 85355fa | 2021-03-22 15:41:41 +0000 | [diff] [blame] | 142 | bot_key = (base_url, bot['name']) |
Adhemerval Zanella | d3e8c48 | 2020-10-12 11:31:48 -0300 | [diff] [blame] | 143 | if bot_key in bot_cache: |
| 144 | continue |
David Spickett | 85355fa | 2021-03-22 15:41:41 +0000 | [diff] [blame] | 145 | |
Adhemerval Zanella | d3e8c48 | 2020-10-12 11:31:48 -0300 | [diff] [blame] | 146 | logging.debug(' Parsing bot {}...'.format(bot['name'])) |
David Spickett | 85355fa | 2021-03-22 15:41:41 +0000 | [diff] [blame] | 147 | status = get_bot_status(session, bot['name'], base_url, server['builder_url'], |
| 148 | server['build_url']) |
David Spickett | f2c82dd | 2021-06-24 10:01:33 +0100 | [diff] [blame] | 149 | if status is not None: |
| 150 | logging.debug(" FAIL" if status['fail'] else " PASS") |
| 151 | bot_cache[bot_key] = status |
Adhemerval Zanella | d3e8c48 | 2020-10-12 11:31:48 -0300 | [diff] [blame] | 152 | |
David Spickett | 85355fa | 2021-03-22 15:41:41 +0000 | [diff] [blame] | 153 | return bot_cache |
| 154 | |
| 155 | def 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 Spickett | f006c37 | 2021-03-22 12:54:12 +0000 | [diff] [blame] | 160 | |
Adhemerval Zanella | d3e8c48 | 2020-10-12 11:31:48 -0300 | [diff] [blame] | 161 | # 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 Zanella | d3e8c48 | 2020-10-12 11:31:48 -0300 | [diff] [blame] | 166 | temp.write("<table cellspacing=1 cellpadding=2>\n") |
| 167 | temp.write("<tr><td colspan=5> </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> </td><tr>\n") |
| 173 | temp.write("<tr><th colspan=5>{}</td><tr>\n".format(builder['name'])) |
Maxim Kuvyrkov | fdaa468 | 2021-04-14 13:13:14 +0000 | [diff] [blame] | 174 | 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 Zanella | d3e8c48 | 2020-10-12 11:31:48 -0300 | [diff] [blame] | 177 | for bot in builder['bots']: |
| 178 | temp.write("<tr>\n") |
David Spickett | f2c82dd | 2021-06-24 10:01:33 +0100 | [diff] [blame] | 179 | try: |
| 180 | status = bots_status[(base_url, bot['name'])] |
| 181 | except KeyError: |
David Spickett | f2d4c48 | 2021-06-24 10:07:52 +0100 | [diff] [blame] | 182 | temp.write(" <td>{} is offline!</td>\n</tr>\n".format(bot['name'])) |
David Spickett | f2c82dd | 2021-06-24 10:01:33 +0100 | [diff] [blame] | 183 | continue |
David Spickett | 30a986f | 2021-04-29 09:37:00 +0100 | [diff] [blame^] | 184 | 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 Spickett | f2c82dd | 2021-06-24 10:01:33 +0100 | [diff] [blame] | 188 | |
David Spickett | 85355fa | 2021-03-22 15:41:41 +0000 | [diff] [blame] | 189 | found_failure |= status['fail'] |
David Spickett | 30a986f | 2021-04-29 09:37:00 +0100 | [diff] [blame^] | 190 | |
| 191 | temp.write(" <td><a href='{}'>{}</a></td>\n".format( |
| 192 | status['builder_url'], bot['name'])) |
Adhemerval Zanella | d3e8c48 | 2020-10-12 11:31:48 -0300 | [diff] [blame] | 193 | 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> </td>\n" |
Maxim Kuvyrkov | fdaa468 | 2021-04-14 13:13:14 +0000 | [diff] [blame] | 197 | 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 Zanella | d3e8c48 | 2020-10-12 11:31:48 -0300 | [diff] [blame] | 203 | else: |
| 204 | temp.write(empty_cell) |
| 205 | if 'number' in status: |
David Spickett | 30a986f | 2021-04-29 09:37:00 +0100 | [diff] [blame^] | 206 | temp.write(" <td><a href='{}'>{}</a></td>\n".format( |
| 207 | status['build_url'], status['number'])) |
Adhemerval Zanella | d3e8c48 | 2020-10-12 11:31:48 -0300 | [diff] [blame] | 208 | 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 Stannard | 91688ff | 2021-01-07 10:27:27 +0000 | [diff] [blame] | 214 | 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 Zanella | d3e8c48 | 2020-10-12 11:31:48 -0300 | [diff] [blame] | 219 | else: |
| 220 | temp.write(empty_cell) |
| 221 | temp.write("</tr>\n") |
| 222 | temp.write("</table>\n") |
| 223 | |
David Spickett | 85355fa | 2021-03-22 15:41:41 +0000 | [diff] [blame] | 224 | 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 Zanella | d3e8c48 | 2020-10-12 11:31:48 -0300 | [diff] [blame] | 228 | # Move temp to main (atomic change) |
| 229 | temp.close() |
David Spickett | 7f18f4d | 2021-03-22 11:49:17 +0000 | [diff] [blame] | 230 | shutil.move(temp.name, output_file) |
Adhemerval Zanella | d3e8c48 | 2020-10-12 11:31:48 -0300 | [diff] [blame] | 231 | |
| 232 | |
| 233 | if __name__ == "__main__": |
| 234 | parser = argparse.ArgumentParser() |
David Spickett | ec2166c | 2021-07-19 14:17:29 +0100 | [diff] [blame] | 235 | parser.add_argument('-d', dest='debug', action='store_true', |
| 236 | help='show debug log messages') |
Adhemerval Zanella | d3e8c48 | 2020-10-12 11:31:48 -0300 | [diff] [blame] | 237 | 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 Spickett | 7f18f4d | 2021-03-22 11:49:17 +0000 | [diff] [blame] | 250 | print("error: failed to read {} config file: {}".format(args.config_file, e)) |
Adhemerval Zanella | d3e8c48 | 2020-10-12 11:31:48 -0300 | [diff] [blame] | 251 | sys.exit(os.EX_CONFIG) |
| 252 | |
David Spickett | 30a986f | 2021-04-29 09:37:00 +0100 | [diff] [blame^] | 253 | status = get_buildbot_bots_status(config) |
| 254 | status.update(get_buildkite_bots_status(config)) |
| 255 | write_bot_status(config, args.output_file, status) |