Mike Frysinger | f1ba7ad | 2022-09-12 05:42:57 -0400 | [diff] [blame] | 1 | # Copyright 2018 The ChromiumOS Authors |
Don Garrett | be3608b | 2018-06-05 17:10:09 -0700 | [diff] [blame] | 2 | # Use of this source code is governed by a BSD-style license that can be |
| 3 | # found in the LICENSE file. |
| 4 | |
Don Garrett | ff45f4b | 2018-10-04 09:08:01 -0700 | [diff] [blame] | 5 | # pylint: disable=line-too-long |
Mike Frysinger | a2527f8 | 2023-02-03 04:32:15 -0500 | [diff] [blame] | 6 | |
Don Garrett | be3608b | 2018-06-05 17:10:09 -0700 | [diff] [blame] | 7 | """Generate LUCI Scheduler config file. |
| 8 | |
| 9 | This generates the LUCI Scheduler configuration file for ChromeOS builds based |
| 10 | on the chromeos_config contents. |
| 11 | |
Don Garrett | ff45f4b | 2018-10-04 09:08:01 -0700 | [diff] [blame] | 12 | Changes to chromite/config/luci-scheduler.cfg will be autodeployed: |
Mike Nichols | b575c61 | 2021-04-09 10:04:43 -0600 | [diff] [blame] | 13 | https://data.corp.google.com/sites/chromeos_ci_cros_ci_builds/utility/?f=board_name:in:luci-scheduler-updater |
Don Garrett | be3608b | 2018-06-05 17:10:09 -0700 | [diff] [blame] | 14 | |
Don Garrett | c8323dc | 2018-08-22 15:58:26 -0700 | [diff] [blame] | 15 | Notes: |
| 16 | Normal builds are scheduled based on the builder values for |
| 17 | 'schedule' and 'triggered_gitiles' in config/chromeos_config.py. |
| 18 | |
| 19 | Branched builds are scheduled based on the function |
| 20 | chromeos_config.BranchScheduleConfig() |
Don Garrett | be3608b | 2018-06-05 17:10:09 -0700 | [diff] [blame] | 21 | """ |
Don Garrett | ff45f4b | 2018-10-04 09:08:01 -0700 | [diff] [blame] | 22 | # pylint: enable=line-too-long |
Don Garrett | be3608b | 2018-06-05 17:10:09 -0700 | [diff] [blame] | 23 | |
Don Garrett | be3608b | 2018-06-05 17:10:09 -0700 | [diff] [blame] | 24 | import sys |
| 25 | |
Don Garrett | 8dace27 | 2018-07-19 14:07:42 -0700 | [diff] [blame] | 26 | from chromite.config import chromeos_config |
Don Garrett | be3608b | 2018-06-05 17:10:09 -0700 | [diff] [blame] | 27 | from chromite.lib import commandline |
| 28 | from chromite.lib import config_lib |
| 29 | |
| 30 | |
| 31 | _CONFIG_HEADER = """# Defines buckets on luci-scheduler.appspot.com. |
| 32 | # |
| 33 | # For schema of this file and documentation see ProjectConfig message in |
Mike Frysinger | 92dc649 | 2021-02-17 16:01:09 -0500 | [diff] [blame] | 34 | # https://github.com/luci/luci-go/blob/HEAD/scheduler/appengine/messages/config.proto |
Don Garrett | be3608b | 2018-06-05 17:10:09 -0700 | [diff] [blame] | 35 | |
| 36 | # Generated with chromite/scripts/gen_luci_scheduler |
| 37 | |
Don Garrett | ff45f4b | 2018-10-04 09:08:01 -0700 | [diff] [blame] | 38 | # Autodeployed with: |
Mike Nichols | b575c61 | 2021-04-09 10:04:43 -0600 | [diff] [blame] | 39 | # https://data.corp.google.com/sites/chromeos_ci_cros_ci_builds/utility/?f=board_name:in:luci-scheduler-updater |
Don Garrett | ff45f4b | 2018-10-04 09:08:01 -0700 | [diff] [blame] | 40 | |
Don Garrett | be3608b | 2018-06-05 17:10:09 -0700 | [diff] [blame] | 41 | acl_sets { |
| 42 | name: "default" |
| 43 | acls { |
| 44 | role: READER |
| 45 | granted_to: "group:googlers" |
| 46 | } |
| 47 | acls { |
| 48 | role: OWNER |
| 49 | granted_to: "group:project-chromeos-admins" |
| 50 | } |
Jason D. Clinton | a9e374a | 2018-10-18 18:41:00 -0600 | [diff] [blame] | 51 | acls { |
| 52 | role: TRIGGERER |
| 53 | granted_to: "group:mdb/chromeos-build-access" |
LaMont Jones | 48ddfd9 | 2020-09-16 13:46:41 -0600 | [diff] [blame] | 54 | } |
| 55 | acls { |
| 56 | role: TRIGGERER |
LaMont Jones | d1bb000 | 2020-09-16 09:35:10 -0600 | [diff] [blame] | 57 | granted_to: "group:project-chromeos-buildbucket-schedulers" |
Jason D. Clinton | a9e374a | 2018-10-18 18:41:00 -0600 | [diff] [blame] | 58 | } |
Don Garrett | be3608b | 2018-06-05 17:10:09 -0700 | [diff] [blame] | 59 | } |
| 60 | """ |
| 61 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 62 | |
Don Garrett | 9a0d90c | 2018-10-30 12:47:14 -0700 | [diff] [blame] | 63 | def buildJobName(build_config): |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 64 | if "schedule_branch" in build_config: |
| 65 | return "%s-%s" % (build_config.schedule_branch, build_config.name) |
| 66 | else: |
| 67 | return build_config.name |
Don Garrett | 9a0d90c | 2018-10-30 12:47:14 -0700 | [diff] [blame] | 68 | |
Don Garrett | be3608b | 2018-06-05 17:10:09 -0700 | [diff] [blame] | 69 | |
| 70 | def genSchedulerJob(build_config): |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 71 | """Generate the luci scheduler job for a given build config. |
Don Garrett | be3608b | 2018-06-05 17:10:09 -0700 | [diff] [blame] | 72 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 73 | Args: |
Trent Apted | 66736d8 | 2023-05-25 10:38:28 +1000 | [diff] [blame^] | 74 | build_config: config_lib.BuildConfig. |
Don Garrett | be3608b | 2018-06-05 17:10:09 -0700 | [diff] [blame] | 75 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 76 | Returns: |
Trent Apted | 66736d8 | 2023-05-25 10:38:28 +1000 | [diff] [blame^] | 77 | Multiline string to include in the luci scheduler configuration. |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 78 | """ |
| 79 | job_name = buildJobName(build_config) |
| 80 | if "schedule_branch" in build_config: |
| 81 | branch = build_config.schedule_branch |
| 82 | else: |
| 83 | branch = "main" |
Don Garrett | 73148e5 | 2018-08-17 16:54:46 -0700 | [diff] [blame] | 84 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 85 | tags = { |
| 86 | "cbb_branch": branch, |
| 87 | "cbb_config": build_config.name, |
| 88 | "cbb_display_label": build_config.display_label, |
| 89 | "cbb_workspace_branch": build_config.workspace_branch, |
| 90 | "cbb_goma_client_type": build_config.goma_client_type, |
| 91 | } |
Don Garrett | 73148e5 | 2018-08-17 16:54:46 -0700 | [diff] [blame] | 92 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 93 | # Filter out tags with no value set. |
| 94 | tags = {k: v for k, v in tags.items() if v} |
Don Garrett | 73148e5 | 2018-08-17 16:54:46 -0700 | [diff] [blame] | 95 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 96 | tag_lines = [ |
| 97 | ' tags: "%s:%s"' % (k, tags[k]) for k in sorted(tags.keys()) |
| 98 | ] |
| 99 | prop_lines = [ |
| 100 | ' properties: "%s:%s"' % (k, tags[k]) for k in sorted(tags.keys()) |
| 101 | ] |
Don Garrett | 73148e5 | 2018-08-17 16:54:46 -0700 | [diff] [blame] | 102 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 103 | # TODO: Move --buildbot arg into recipe, and remove from here. |
| 104 | template = """ |
Don Garrett | be3608b | 2018-06-05 17:10:09 -0700 | [diff] [blame] | 105 | job { |
Don Garrett | 8dace27 | 2018-07-19 14:07:42 -0700 | [diff] [blame] | 106 | id: "%(job_name)s" |
Vadim Shtayura | 6398e00 | 2021-01-11 12:02:11 -0800 | [diff] [blame] | 107 | realm: "cbb-jobs" |
Don Garrett | be3608b | 2018-06-05 17:10:09 -0700 | [diff] [blame] | 108 | acl_sets: "default" |
| 109 | schedule: "%(schedule)s" |
| 110 | buildbucket: { |
| 111 | server: "cr-buildbucket.appspot.com" |
Vadim Shtayura | 959cb0f | 2022-02-03 12:45:58 -0800 | [diff] [blame] | 112 | bucket: "general" |
Don Garrett | be3608b | 2018-06-05 17:10:09 -0700 | [diff] [blame] | 113 | builder: "%(builder)s" |
Don Garrett | 73148e5 | 2018-08-17 16:54:46 -0700 | [diff] [blame] | 114 | %(tag_lines)s |
| 115 | %(prop_lines)s |
Don Garrett | be3608b | 2018-06-05 17:10:09 -0700 | [diff] [blame] | 116 | properties: "cbb_extra_args:[\\"--buildbot\\"]" |
| 117 | } |
| 118 | } |
| 119 | """ |
Don Garrett | 8dace27 | 2018-07-19 14:07:42 -0700 | [diff] [blame] | 120 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 121 | return template % { |
| 122 | "job_name": job_name, |
| 123 | "builder": build_config.luci_builder, |
| 124 | "schedule": build_config.schedule, |
| 125 | "tag_lines": "\n".join(tag_lines), |
| 126 | "prop_lines": "\n".join(prop_lines), |
| 127 | } |
Don Garrett | be3608b | 2018-06-05 17:10:09 -0700 | [diff] [blame] | 128 | |
| 129 | |
David Burger | cba0d27 | 2020-06-24 16:21:26 -0600 | [diff] [blame] | 130 | def genSchedulerTrigger(trigger_name, repo, refs, path_regexps, builds): |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 131 | """Generate the luci scheduler job for a given build config. |
Don Garrett | be3608b | 2018-06-05 17:10:09 -0700 | [diff] [blame] | 132 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 133 | Args: |
Trent Apted | 66736d8 | 2023-05-25 10:38:28 +1000 | [diff] [blame^] | 134 | trigger_name: Name of the trigger as a string. |
| 135 | repo: Gitiles URL git git repository. |
| 136 | refs: Iterable of git refs to check. May use regular expressions. |
| 137 | path_regexps: Iterable of path regular expressions of files to trigger |
| 138 | on or falsey to trigger on everything. |
| 139 | builds: Iterable of build config names to trigger. |
Don Garrett | be3608b | 2018-06-05 17:10:09 -0700 | [diff] [blame] | 140 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 141 | Returns: |
Trent Apted | 66736d8 | 2023-05-25 10:38:28 +1000 | [diff] [blame^] | 142 | Multiline string to include in the luci scheduler configuration. |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 143 | """ |
| 144 | template = """ |
Don Garrett | be3608b | 2018-06-05 17:10:09 -0700 | [diff] [blame] | 145 | trigger { |
| 146 | id: "%(trigger_name)s" |
Vadim Shtayura | 6398e00 | 2021-01-11 12:02:11 -0800 | [diff] [blame] | 147 | realm: "cbb-jobs" |
Don Garrett | be3608b | 2018-06-05 17:10:09 -0700 | [diff] [blame] | 148 | acl_sets: "default" |
| 149 | schedule: "with 5m interval" |
| 150 | gitiles: { |
| 151 | repo: "%(repo)s" |
David Burger | cba0d27 | 2020-06-24 16:21:26 -0600 | [diff] [blame] | 152 | %(refs)s%(path_regexps)s |
Don Garrett | be3608b | 2018-06-05 17:10:09 -0700 | [diff] [blame] | 153 | } |
| 154 | %(triggers)s |
| 155 | } |
| 156 | """ |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 157 | if path_regexps: |
| 158 | path_regexps = "\n" + "\n".join( |
| 159 | ' path_regexps: "%s"' % r for r in path_regexps |
| 160 | ) |
| 161 | else: |
| 162 | path_regexps = "" |
| 163 | return template % { |
| 164 | "trigger_name": trigger_name, |
| 165 | "repo": repo, |
| 166 | "refs": "\n".join(' refs: "%s"' % r for r in refs), |
| 167 | "path_regexps": path_regexps, |
| 168 | "triggers": "\n".join(' triggers: "%s"' % b for b in builds), |
| 169 | } |
Don Garrett | be3608b | 2018-06-05 17:10:09 -0700 | [diff] [blame] | 170 | |
| 171 | |
Don Garrett | 8dace27 | 2018-07-19 14:07:42 -0700 | [diff] [blame] | 172 | def genLuciSchedulerConfig(site_config, branch_config): |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 173 | """Generate a luciSchedulerConfig as a string. |
Don Garrett | be3608b | 2018-06-05 17:10:09 -0700 | [diff] [blame] | 174 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 175 | Args: |
Trent Apted | 66736d8 | 2023-05-25 10:38:28 +1000 | [diff] [blame^] | 176 | site_config: A config_lib.SiteConfig instance. |
| 177 | branch_config: A list of BuildConfig instances to schedule. |
Don Garrett | be3608b | 2018-06-05 17:10:09 -0700 | [diff] [blame] | 178 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 179 | Returns: |
Trent Apted | 66736d8 | 2023-05-25 10:38:28 +1000 | [diff] [blame^] | 180 | The complete scheduler configuration contents as a string. |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 181 | """ |
| 182 | # Trigger collection is used to collect together trigger information, so |
| 183 | # we can reuse the same trigger for multiple builds as needed. |
| 184 | # It maps gitiles_key to a set of build_names. |
| 185 | # A gitiles_key = (gitiles_url, tuple(ref_list)) |
| 186 | trigger_collection = {} |
Don Garrett | be3608b | 2018-06-05 17:10:09 -0700 | [diff] [blame] | 187 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 188 | jobs = [] |
Don Garrett | be3608b | 2018-06-05 17:10:09 -0700 | [diff] [blame] | 189 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 190 | # Order the configs consistently. |
| 191 | configs = [ |
| 192 | site_config[name] for name in sorted(site_config) |
| 193 | ] + branch_config |
Don Garrett | 8dace27 | 2018-07-19 14:07:42 -0700 | [diff] [blame] | 194 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 195 | for config in configs: |
| 196 | # Populate jobs. |
| 197 | if config.schedule: |
| 198 | jobs.append(genSchedulerJob(config)) |
Don Garrett | be3608b | 2018-06-05 17:10:09 -0700 | [diff] [blame] | 199 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 200 | # Populate trigger_collection. |
| 201 | if config.triggered_gitiles: |
| 202 | for trigger in config.triggered_gitiles: |
| 203 | try: |
| 204 | gitiles_url, ref_list, path_regexps = trigger |
| 205 | except ValueError: |
| 206 | gitiles_url, ref_list = trigger |
| 207 | path_regexps = [] |
| 208 | gitiles_key = ( |
| 209 | gitiles_url, |
| 210 | tuple(ref_list), |
| 211 | tuple(path_regexps), |
| 212 | ) |
| 213 | trigger_collection.setdefault(gitiles_key, set()) |
| 214 | trigger_collection[gitiles_key].add(buildJobName(config)) |
Don Garrett | be3608b | 2018-06-05 17:10:09 -0700 | [diff] [blame] | 215 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 216 | # Populate triggers. |
| 217 | triggers = [] |
| 218 | trigger_counter = 0 |
| 219 | for gitiles_key in sorted(trigger_collection): |
| 220 | builds = sorted(trigger_collection[gitiles_key]) |
Don Garrett | be3608b | 2018-06-05 17:10:09 -0700 | [diff] [blame] | 221 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 222 | trigger_name = "trigger_%s" % trigger_counter |
| 223 | gitiles_url, refs, path_regexps = gitiles_key |
| 224 | triggers.append( |
| 225 | genSchedulerTrigger( |
| 226 | trigger_name, gitiles_url, refs, path_regexps, builds |
| 227 | ) |
| 228 | ) |
| 229 | trigger_counter += 1 |
Don Garrett | be3608b | 2018-06-05 17:10:09 -0700 | [diff] [blame] | 230 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 231 | return "".join([_CONFIG_HEADER] + triggers + jobs) |
Don Garrett | be3608b | 2018-06-05 17:10:09 -0700 | [diff] [blame] | 232 | |
| 233 | |
| 234 | def GetParser(): |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 235 | """Creates the argparse parser.""" |
| 236 | parser = commandline.ArgumentParser(description=__doc__) |
Don Garrett | be3608b | 2018-06-05 17:10:09 -0700 | [diff] [blame] | 237 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 238 | parser.add_argument( |
| 239 | "-o", "--file_out", type="path", help="Write output to specified file." |
| 240 | ) |
Don Garrett | be3608b | 2018-06-05 17:10:09 -0700 | [diff] [blame] | 241 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 242 | return parser |
Don Garrett | be3608b | 2018-06-05 17:10:09 -0700 | [diff] [blame] | 243 | |
| 244 | |
| 245 | def main(argv): |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 246 | parser = GetParser() |
| 247 | options = parser.parse_args(argv) |
| 248 | options.Freeze() |
Don Garrett | be3608b | 2018-06-05 17:10:09 -0700 | [diff] [blame] | 249 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 250 | site_config = config_lib.GetConfig() |
| 251 | branch_config = chromeos_config.BranchScheduleConfig() |
Don Garrett | be3608b | 2018-06-05 17:10:09 -0700 | [diff] [blame] | 252 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 253 | with ( |
Mike Frysinger | 31fdddd | 2023-02-24 15:50:55 -0500 | [diff] [blame] | 254 | open(options.file_out, "w", encoding="utf-8") |
| 255 | if options.file_out |
| 256 | else sys.stdout |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 257 | ) as fh: |
| 258 | fh.write(genLuciSchedulerConfig(site_config, branch_config)) |