Sean McAllister | ffce55f | 2021-02-22 20:08:18 -0700 | [diff] [blame] | 1 | #!/usr/bin/env python3 |
| 2 | # Copyright 2021 The Chromium OS Authors. All rights reserved. |
| 3 | # Use of this source code is governed by a BSD-style license that can be |
| 4 | # found in the LICENSE file. |
| 5 | """Run an equivalent to the backfill pipeline locally and generate diffs. |
| 6 | |
| 7 | Parse the actual current builder configurations from BuildBucket and run |
| 8 | the join_config_payloads.py script locally. Generate a diff that shows any |
| 9 | changes using the tip-of-tree code vs what's running in production. |
| 10 | """ |
| 11 | |
| 12 | import argparse |
| 13 | import collections |
Sean McAllister | 9b5a33e | 2021-02-26 10:53:54 -0700 | [diff] [blame] | 14 | import functools |
Sean McAllister | ffce55f | 2021-02-22 20:08:18 -0700 | [diff] [blame] | 15 | import itertools |
| 16 | import json |
Sean McAllister | e820fc0 | 2021-03-20 18:34:16 -0600 | [diff] [blame] | 17 | import logging |
Sean McAllister | ffce55f | 2021-02-22 20:08:18 -0700 | [diff] [blame] | 18 | import multiprocessing |
| 19 | import multiprocessing.pool |
| 20 | import os |
| 21 | import pathlib |
| 22 | import subprocess |
| 23 | import sys |
| 24 | import tempfile |
| 25 | import time |
| 26 | |
Sean McAllister | 4b58908 | 2021-04-16 09:59:21 -0600 | [diff] [blame] | 27 | from common import utilities |
| 28 | |
Sean McAllister | ffce55f | 2021-02-22 20:08:18 -0700 | [diff] [blame] | 29 | # resolve relative directories |
| 30 | this_dir = pathlib.Path(os.path.dirname(os.path.abspath(__file__))) |
| 31 | hwid_path = (this_dir / "../../platform/chromeos-hwid/v3").resolve() |
| 32 | join_script = (this_dir / "../payload_utils/join_config_payloads.py").resolve() |
Sean McAllister | e820fc0 | 2021-03-20 18:34:16 -0600 | [diff] [blame] | 33 | merge_script = (this_dir / "../payload_utils/aggregate_messages.py").resolve() |
Sean McAllister | ffce55f | 2021-02-22 20:08:18 -0700 | [diff] [blame] | 34 | public_path = (this_dir / "../../overlays").resolve() |
| 35 | private_path = (this_dir / "../../private-overlays").resolve() |
| 36 | project_path = (this_dir / "../../project").resolve() |
| 37 | |
Sean McAllister | ffce55f | 2021-02-22 20:08:18 -0700 | [diff] [blame] | 38 | # record to store backfiller configuration in |
| 39 | BackfillConfig = collections.namedtuple('BackfillConfig', [ |
| 40 | 'program', |
| 41 | 'project', |
| 42 | 'hwid_key', |
| 43 | 'public_model', |
| 44 | 'private_repo', |
| 45 | 'private_model', |
| 46 | ]) |
| 47 | |
| 48 | |
Sean McAllister | ffce55f | 2021-02-22 20:08:18 -0700 | [diff] [blame] | 49 | def parse_build_property(build, name): |
| 50 | """Parse out a property value from a build and return its value. |
| 51 | |
| 52 | Properties are always JSON values, so we decode them and return the |
| 53 | resulting object |
| 54 | |
| 55 | Args: |
| 56 | build (dict): json object containing BuildBucket properties |
| 57 | name (str): name of the property to look up |
| 58 | |
| 59 | Return: |
| 60 | decoded property value or None if not found |
| 61 | """ |
Sean McAllister | f9d0a6b | 2021-04-09 08:28:47 -0600 | [diff] [blame] | 62 | return json.loads(build["config"]["properties"]).get(name) |
Sean McAllister | ffce55f | 2021-02-22 20:08:18 -0700 | [diff] [blame] | 63 | |
| 64 | |
Sean McAllister | e820fc0 | 2021-03-20 18:34:16 -0600 | [diff] [blame] | 65 | def run_backfill(config, logname=None, run_imported=True, run_joined=True): |
Sean McAllister | 9b5a33e | 2021-02-26 10:53:54 -0700 | [diff] [blame] | 66 | """Run a single backfill job, return diff of current and new output. |
| 67 | |
| 68 | Args: |
| 69 | config: BackfillConfig instance for the backfill operation. |
| 70 | logname: Filename to redirect stderr to from backfill |
Sean McAllister | e820fc0 | 2021-03-20 18:34:16 -0600 | [diff] [blame] | 71 | default is to suppress the output |
| 72 | run_imported: If True, generate a diff for the imported payload |
| 73 | run_joined: If True, generate a diff for the joined payload |
Sean McAllister | 9b5a33e | 2021-02-26 10:53:54 -0700 | [diff] [blame] | 74 | """ |
| 75 | |
Sean McAllister | e820fc0 | 2021-03-20 18:34:16 -0600 | [diff] [blame] | 76 | def run_diff(cmd, current, output): |
| 77 | """Execute cmd and diff the current and output files""" |
| 78 | logfile.write("running: {}\n".format(" ".join(map(str, cmd)))) |
| 79 | |
| 80 | subprocess.run(cmd, stderr=logfile, check=True) |
| 81 | |
| 82 | # if one or the other file doesn't exist, return the other as a diff |
| 83 | if current.exists() != output.exists(): |
| 84 | if current.exists(): |
| 85 | return open(current).read() |
| 86 | return open(output).read() |
| 87 | |
| 88 | # otherwise run diff |
Sean McAllister | 4b58908 | 2021-04-16 09:59:21 -0600 | [diff] [blame] | 89 | return utilities.jqdiff(current, output) |
Sean McAllister | e820fc0 | 2021-03-20 18:34:16 -0600 | [diff] [blame] | 90 | |
| 91 | #### start of function body |
| 92 | |
Sean McAllister | f658fb2 | 2021-03-22 10:39:41 -0600 | [diff] [blame] | 93 | # path to project repo and config bundle |
| 94 | path_repo = project_path / config.program / config.project |
| 95 | path_config = path_repo / "generated/config.jsonproto" |
| 96 | |
Sean McAllister | 9b5a33e | 2021-02-26 10:53:54 -0700 | [diff] [blame] | 97 | logfile = subprocess.DEVNULL |
| 98 | if logname: |
| 99 | logfile = open(logname, "a") |
Sean McAllister | ffce55f | 2021-02-22 20:08:18 -0700 | [diff] [blame] | 100 | |
| 101 | # reef is currently broken because it _needs_ a real portage environment |
| 102 | # to pull in common code. |
| 103 | # TODO(https://crbug.com/1144956): fix when reef is corrected |
Sean McAllister | ffce55f | 2021-02-22 20:08:18 -0700 | [diff] [blame] | 104 | if config.program == "reef": |
| 105 | return None |
| 106 | |
Sean McAllister | 9b5a33e | 2021-02-26 10:53:54 -0700 | [diff] [blame] | 107 | cmd = [join_script, "--l", "DEBUG"] |
Sean McAllister | ffce55f | 2021-02-22 20:08:18 -0700 | [diff] [blame] | 108 | cmd.extend(["--program-name", config.program]) |
| 109 | cmd.extend(["--project-name", config.project]) |
| 110 | |
Sean McAllister | f658fb2 | 2021-03-22 10:39:41 -0600 | [diff] [blame] | 111 | if path_config.exists(): |
| 112 | cmd.extend(["--config-bundle", path_config]) |
| 113 | |
Sean McAllister | ffce55f | 2021-02-22 20:08:18 -0700 | [diff] [blame] | 114 | if config.hwid_key: |
| 115 | cmd.extend(["--hwid", hwid_path / config.hwid_key]) |
| 116 | |
| 117 | if config.public_model: |
| 118 | cmd.extend(["--public-model", public_path / config.public_model]) |
| 119 | |
| 120 | if config.private_model: |
| 121 | overlay = config.private_repo.split('/')[-1] |
| 122 | cmd.extend( |
| 123 | ["--private-model", private_path / overlay / config.private_model]) |
| 124 | |
| 125 | # create temporary directory for output |
Sean McAllister | e820fc0 | 2021-03-20 18:34:16 -0600 | [diff] [blame] | 126 | diff_imported = "" |
| 127 | diff_joined = "" |
| 128 | with tempfile.TemporaryDirectory() as scratch: |
| 129 | scratch = pathlib.Path(scratch) |
Sean McAllister | ffce55f | 2021-02-22 20:08:18 -0700 | [diff] [blame] | 130 | |
Sean McAllister | e820fc0 | 2021-03-20 18:34:16 -0600 | [diff] [blame] | 131 | # generate diff of imported payloads |
| 132 | path_imported_old = path_repo / "generated/imported.jsonproto" |
| 133 | path_imported_new = scratch / "imported.jsonproto" |
Sean McAllister | ffce55f | 2021-02-22 20:08:18 -0700 | [diff] [blame] | 134 | |
Sean McAllister | e820fc0 | 2021-03-20 18:34:16 -0600 | [diff] [blame] | 135 | if run_imported: |
| 136 | diff_imported = run_diff( |
Sean McAllister | f658fb2 | 2021-03-22 10:39:41 -0600 | [diff] [blame] | 137 | cmd + ["--import-only", "--output", path_imported_new], |
Sean McAllister | e820fc0 | 2021-03-20 18:34:16 -0600 | [diff] [blame] | 138 | path_imported_old, |
| 139 | path_imported_new, |
| 140 | ) |
| 141 | |
| 142 | # generate diff of joined payloads |
| 143 | if run_joined and path_config.exists(): |
| 144 | path_joined_old = path_repo / "generated/joined.jsonproto" |
| 145 | path_joined_new = scratch / "joined.jsonproto" |
| 146 | |
Sean McAllister | f658fb2 | 2021-03-22 10:39:41 -0600 | [diff] [blame] | 147 | diff_joined = run_diff(cmd + ["--output", path_joined_new], |
| 148 | path_joined_old, path_joined_new) |
Sean McAllister | e820fc0 | 2021-03-20 18:34:16 -0600 | [diff] [blame] | 149 | |
| 150 | return ("{}-{}".format(config.program, |
| 151 | config.project), diff_imported, diff_joined) |
Sean McAllister | ffce55f | 2021-02-22 20:08:18 -0700 | [diff] [blame] | 152 | |
| 153 | |
| 154 | def run_backfills(args, configs): |
| 155 | """Run backfill pipeline for each builder in configs. |
| 156 | |
| 157 | Generate an über diff showing the changes that the current ToT |
| 158 | join_config_payloads code would generate vs what's currently committed. |
| 159 | |
| 160 | Write the result to the output file specified on the command line. |
| 161 | |
| 162 | Args: |
| 163 | args: command line arguments from argparse |
| 164 | configs: list of BackfillConfig instances to execute |
| 165 | |
| 166 | Return: |
| 167 | nothing |
| 168 | """ |
| 169 | |
Sean McAllister | 9b5a33e | 2021-02-26 10:53:54 -0700 | [diff] [blame] | 170 | # create a logfile if requested |
| 171 | kwargs = {} |
Sean McAllister | e820fc0 | 2021-03-20 18:34:16 -0600 | [diff] [blame] | 172 | kwargs["run_joined"] = args.joined_diff is not None |
Sean McAllister | 9b5a33e | 2021-02-26 10:53:54 -0700 | [diff] [blame] | 173 | if args.logfile: |
| 174 | # open and close the logfile to truncate it so backfills can append |
| 175 | # We can't pickle the file object and send it as an argument with |
| 176 | # multiprocessing, so this is a workaround for that limitation |
| 177 | with open(args.logfile, "w"): |
| 178 | kwargs["logname"] = args.logfile |
| 179 | |
Sean McAllister | ffce55f | 2021-02-22 20:08:18 -0700 | [diff] [blame] | 180 | nproc = 32 |
| 181 | nconfig = len(configs) |
Sean McAllister | e820fc0 | 2021-03-20 18:34:16 -0600 | [diff] [blame] | 182 | imported_diffs = {} |
| 183 | joined_diffs = {} |
Sean McAllister | ffce55f | 2021-02-22 20:08:18 -0700 | [diff] [blame] | 184 | with multiprocessing.Pool(processes=nproc) as pool: |
Sean McAllister | 9b5a33e | 2021-02-26 10:53:54 -0700 | [diff] [blame] | 185 | results = pool.imap_unordered( |
| 186 | functools.partial(run_backfill, **kwargs), configs, chunksize=1) |
Sean McAllister | ffce55f | 2021-02-22 20:08:18 -0700 | [diff] [blame] | 187 | for ii, result in enumerate(results, 1): |
| 188 | sys.stderr.write( |
Sean McAllister | 4b58908 | 2021-04-16 09:59:21 -0600 | [diff] [blame] | 189 | utilities.clear_line("[{}/{}] Processing backfills".format( |
| 190 | ii, nconfig))) |
Sean McAllister | ffce55f | 2021-02-22 20:08:18 -0700 | [diff] [blame] | 191 | |
| 192 | if result: |
Sean McAllister | e820fc0 | 2021-03-20 18:34:16 -0600 | [diff] [blame] | 193 | key, imported, joined = result |
| 194 | imported_diffs[key] = imported |
| 195 | joined_diffs[key] = joined |
Sean McAllister | ffce55f | 2021-02-22 20:08:18 -0700 | [diff] [blame] | 196 | |
Sean McAllister | 4b58908 | 2021-04-16 09:59:21 -0600 | [diff] [blame] | 197 | sys.stderr.write(utilities.clear_line("Processing backfills")) |
Sean McAllister | ffce55f | 2021-02-22 20:08:18 -0700 | [diff] [blame] | 198 | |
| 199 | # generate final über diff showing all the changes |
Sean McAllister | e820fc0 | 2021-03-20 18:34:16 -0600 | [diff] [blame] | 200 | with open(args.imported_diff, "w") as ofile: |
| 201 | for name, result in sorted(imported_diffs.items()): |
Sean McAllister | ffce55f | 2021-02-22 20:08:18 -0700 | [diff] [blame] | 202 | ofile.write("## ---------------------\n") |
| 203 | ofile.write("## diff for {}\n".format(name)) |
| 204 | ofile.write("\n") |
| 205 | ofile.write(result + "\n") |
| 206 | |
Sean McAllister | e820fc0 | 2021-03-20 18:34:16 -0600 | [diff] [blame] | 207 | if args.joined_diff: |
| 208 | with open(args.joined_diff, "w") as ofile: |
| 209 | for name, result in sorted(joined_diffs.items()): |
| 210 | ofile.write("## ---------------------\n") |
| 211 | ofile.write("## diff for {}\n".format(name)) |
| 212 | ofile.write("\n") |
| 213 | ofile.write(result + "\n") |
Sean McAllister | ffce55f | 2021-02-22 20:08:18 -0700 | [diff] [blame] | 214 | |
| 215 | |
| 216 | def main(): |
| 217 | parser = argparse.ArgumentParser( |
| 218 | description=__doc__, |
| 219 | formatter_class=argparse.RawTextHelpFormatter, |
| 220 | ) |
| 221 | |
| 222 | parser.add_argument( |
Sean McAllister | e820fc0 | 2021-03-20 18:34:16 -0600 | [diff] [blame] | 223 | "--imported-diff", |
Sean McAllister | ffce55f | 2021-02-22 20:08:18 -0700 | [diff] [blame] | 224 | type=str, |
| 225 | required=True, |
Sean McAllister | e820fc0 | 2021-03-20 18:34:16 -0600 | [diff] [blame] | 226 | help="target file for diff on imported.jsonproto payload", |
Sean McAllister | ffce55f | 2021-02-22 20:08:18 -0700 | [diff] [blame] | 227 | ) |
Sean McAllister | e820fc0 | 2021-03-20 18:34:16 -0600 | [diff] [blame] | 228 | |
| 229 | parser.add_argument( |
| 230 | "--joined-diff", |
| 231 | type=str, |
| 232 | help="target file for diff on joined.jsonproto payload", |
| 233 | ) |
| 234 | |
Sean McAllister | 9b5a33e | 2021-02-26 10:53:54 -0700 | [diff] [blame] | 235 | parser.add_argument( |
| 236 | "-l", |
| 237 | "--logfile", |
| 238 | type=str, |
| 239 | help="target file to log output from backfills", |
| 240 | ) |
Sean McAllister | ffce55f | 2021-02-22 20:08:18 -0700 | [diff] [blame] | 241 | args = parser.parse_args() |
| 242 | |
| 243 | # query BuildBucket for current builder configurations in the infra bucket |
Sean McAllister | 4b58908 | 2021-04-16 09:59:21 -0600 | [diff] [blame] | 244 | data, status = utilities.call_and_spin( |
Sean McAllister | f9d0a6b | 2021-04-09 08:28:47 -0600 | [diff] [blame] | 245 | "Listing backfill builder", |
Sean McAllister | ffce55f | 2021-02-22 20:08:18 -0700 | [diff] [blame] | 246 | json.dumps({ |
Sean McAllister | 4b58908 | 2021-04-16 09:59:21 -0600 | [diff] [blame] | 247 | "id": { |
| 248 | "project": "chromeos", |
| 249 | "bucket": "infra", |
| 250 | "builder": "backfiller" |
| 251 | } |
Sean McAllister | ffce55f | 2021-02-22 20:08:18 -0700 | [diff] [blame] | 252 | }), |
| 253 | "prpc", |
| 254 | "call", |
| 255 | "cr-buildbucket.appspot.com", |
Sean McAllister | f9d0a6b | 2021-04-09 08:28:47 -0600 | [diff] [blame] | 256 | "buildbucket.v2.Builders.GetBuilder", |
Sean McAllister | ffce55f | 2021-02-22 20:08:18 -0700 | [diff] [blame] | 257 | ) |
| 258 | |
| 259 | if status != 0: |
| 260 | print( |
| 261 | "Error executing prpc call to list builders. Try 'prpc login' first.", |
| 262 | file=sys.stderr, |
| 263 | ) |
| 264 | sys.exit(status) |
| 265 | |
Sean McAllister | f9d0a6b | 2021-04-09 08:28:47 -0600 | [diff] [blame] | 266 | builder = json.loads(data) |
Sean McAllister | ffce55f | 2021-02-22 20:08:18 -0700 | [diff] [blame] | 267 | |
| 268 | # construct backfill config from the configured builder properties |
| 269 | configs = [] |
Sean McAllister | f9d0a6b | 2021-04-09 08:28:47 -0600 | [diff] [blame] | 270 | for builder_config in parse_build_property(builder, "configs"): |
Sean McAllister | e820fc0 | 2021-03-20 18:34:16 -0600 | [diff] [blame] | 271 | config = BackfillConfig( |
Sean McAllister | f9d0a6b | 2021-04-09 08:28:47 -0600 | [diff] [blame] | 272 | program=builder_config["program_name"], |
| 273 | project=builder_config["project_name"], |
| 274 | hwid_key=builder_config.get("hwid_key"), |
| 275 | public_model=builder_config.get("public_yaml_path"), |
| 276 | private_repo=builder_config.get("private_yaml", {}).get("repo"), |
| 277 | private_model=builder_config.get("private_yaml", {}).get("path"), |
Sean McAllister | e820fc0 | 2021-03-20 18:34:16 -0600 | [diff] [blame] | 278 | ) |
| 279 | |
| 280 | path_repo = project_path / config.program / config.project |
| 281 | if not path_repo.exists(): |
| 282 | logging.warning("{}/{} does not exist locally, skipping".format( |
| 283 | config.program, config.project)) |
| 284 | continue |
| 285 | |
| 286 | configs.append(config) |
Sean McAllister | ffce55f | 2021-02-22 20:08:18 -0700 | [diff] [blame] | 287 | |
| 288 | run_backfills(args, configs) |
| 289 | |
| 290 | |
| 291 | if __name__ == "__main__": |
| 292 | main() |