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