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