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