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