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 | d1bb000 | 2020-09-16 09:35:10 -0600 | [diff] [blame^] | 59 | granted_to: "group:project-chromeos-buildbucket-schedulers" |
Jason D. Clinton | a9e374a | 2018-10-18 18:41:00 -0600 | [diff] [blame] | 60 | } |
Don Garrett | be3608b | 2018-06-05 17:10:09 -0700 | [diff] [blame] | 61 | } |
| 62 | """ |
| 63 | |
Don Garrett | 9a0d90c | 2018-10-30 12:47:14 -0700 | [diff] [blame] | 64 | def buildJobName(build_config): |
| 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 |
| 69 | |
Don Garrett | be3608b | 2018-06-05 17:10:09 -0700 | [diff] [blame] | 70 | |
| 71 | def genSchedulerJob(build_config): |
| 72 | """Generate the luci scheduler job for a given build config. |
| 73 | |
| 74 | Args: |
| 75 | build_config: config_lib.BuildConfig. |
| 76 | |
| 77 | Returns: |
| 78 | Multiline string to include in the luci scheduler configuration. |
| 79 | """ |
Don Garrett | 9a0d90c | 2018-10-30 12:47:14 -0700 | [diff] [blame] | 80 | job_name = buildJobName(build_config) |
Don Garrett | 73148e5 | 2018-08-17 16:54:46 -0700 | [diff] [blame] | 81 | if 'schedule_branch' in build_config: |
| 82 | branch = build_config.schedule_branch |
Don Garrett | 73148e5 | 2018-08-17 16:54:46 -0700 | [diff] [blame] | 83 | else: |
| 84 | branch = 'master' |
Don Garrett | 73148e5 | 2018-08-17 16:54:46 -0700 | [diff] [blame] | 85 | |
| 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, |
Yoshisato Yanagisawa | 16285b4 | 2018-09-19 14:00:58 +0900 | [diff] [blame] | 91 | 'cbb_goma_client_type': build_config.goma_client_type, |
Don Garrett | 73148e5 | 2018-08-17 16:54:46 -0700 | [diff] [blame] | 92 | } |
| 93 | |
| 94 | # Filter out tags with no value set. |
Mike Frysinger | 0bdbc10 | 2019-06-13 15:27:29 -0400 | [diff] [blame] | 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 | |
| 97 | |
| 98 | tag_lines = [' tags: "%s:%s"' % (k, tags[k]) |
| 99 | for k in sorted(tags.keys())] |
| 100 | prop_lines = [' properties: "%s:%s"' % (k, tags[k]) |
| 101 | for k in sorted(tags.keys())] |
| 102 | |
Don Garrett | be3608b | 2018-06-05 17:10:09 -0700 | [diff] [blame] | 103 | # TODO: Move --buildbot arg into recipe, and remove from here. |
| 104 | template = """ |
| 105 | job { |
Don Garrett | 8dace27 | 2018-07-19 14:07:42 -0700 | [diff] [blame] | 106 | id: "%(job_name)s" |
Don Garrett | be3608b | 2018-06-05 17:10:09 -0700 | [diff] [blame] | 107 | acl_sets: "default" |
| 108 | schedule: "%(schedule)s" |
| 109 | buildbucket: { |
| 110 | server: "cr-buildbucket.appspot.com" |
| 111 | bucket: "luci.chromeos.general" |
| 112 | builder: "%(builder)s" |
Don Garrett | 73148e5 | 2018-08-17 16:54:46 -0700 | [diff] [blame] | 113 | %(tag_lines)s |
| 114 | %(prop_lines)s |
Don Garrett | be3608b | 2018-06-05 17:10:09 -0700 | [diff] [blame] | 115 | properties: "cbb_extra_args:[\\"--buildbot\\"]" |
| 116 | } |
| 117 | } |
| 118 | """ |
Don Garrett | 8dace27 | 2018-07-19 14:07:42 -0700 | [diff] [blame] | 119 | |
Don Garrett | be3608b | 2018-06-05 17:10:09 -0700 | [diff] [blame] | 120 | return template % { |
Don Garrett | 8dace27 | 2018-07-19 14:07:42 -0700 | [diff] [blame] | 121 | 'job_name': job_name, |
Don Garrett | be3608b | 2018-06-05 17:10:09 -0700 | [diff] [blame] | 122 | 'builder': build_config.luci_builder, |
Don Garrett | be3608b | 2018-06-05 17:10:09 -0700 | [diff] [blame] | 123 | 'schedule': build_config.schedule, |
Don Garrett | 73148e5 | 2018-08-17 16:54:46 -0700 | [diff] [blame] | 124 | 'tag_lines': '\n'.join(tag_lines), |
| 125 | 'prop_lines': '\n'.join(prop_lines), |
Don Garrett | be3608b | 2018-06-05 17:10:09 -0700 | [diff] [blame] | 126 | } |
| 127 | |
| 128 | |
David Burger | cba0d27 | 2020-06-24 16:21:26 -0600 | [diff] [blame] | 129 | def genSchedulerTrigger(trigger_name, repo, refs, path_regexps, builds): |
Don Garrett | be3608b | 2018-06-05 17:10:09 -0700 | [diff] [blame] | 130 | """Generate the luci scheduler job for a given build config. |
| 131 | |
| 132 | Args: |
| 133 | trigger_name: Name of the trigger as a string. |
| 134 | repo: Gitiles URL git git repository. |
| 135 | refs: Iterable of git refs to check. May use regular expressions. |
David Burger | cba0d27 | 2020-06-24 16:21:26 -0600 | [diff] [blame] | 136 | path_regexps: Iterable of path regular expressions of files to trigger on |
| 137 | or falsy to trigger on everything. |
Don Garrett | be3608b | 2018-06-05 17:10:09 -0700 | [diff] [blame] | 138 | builds: Iterable of build config names to trigger. |
| 139 | |
| 140 | Returns: |
| 141 | Multiline string to include in the luci scheduler configuration. |
| 142 | """ |
| 143 | template = """ |
| 144 | trigger { |
| 145 | id: "%(trigger_name)s" |
| 146 | acl_sets: "default" |
| 147 | schedule: "with 5m interval" |
| 148 | gitiles: { |
| 149 | repo: "%(repo)s" |
David Burger | cba0d27 | 2020-06-24 16:21:26 -0600 | [diff] [blame] | 150 | %(refs)s%(path_regexps)s |
Don Garrett | be3608b | 2018-06-05 17:10:09 -0700 | [diff] [blame] | 151 | } |
| 152 | %(triggers)s |
| 153 | } |
| 154 | """ |
David Burger | cba0d27 | 2020-06-24 16:21:26 -0600 | [diff] [blame] | 155 | if path_regexps: |
| 156 | path_regexps = '\n' + '\n'.join(' path_regexps: "%s"' % |
| 157 | r for r in path_regexps) |
| 158 | else: |
| 159 | path_regexps = '' |
Don Garrett | be3608b | 2018-06-05 17:10:09 -0700 | [diff] [blame] | 160 | return template % { |
| 161 | 'trigger_name': trigger_name, |
| 162 | 'repo': repo, |
| 163 | 'refs': '\n'.join(' refs: "%s"' % r for r in refs), |
David Burger | cba0d27 | 2020-06-24 16:21:26 -0600 | [diff] [blame] | 164 | 'path_regexps': path_regexps, |
Don Garrett | be3608b | 2018-06-05 17:10:09 -0700 | [diff] [blame] | 165 | 'triggers': '\n'.join(' triggers: "%s"' % b for b in builds), |
| 166 | } |
| 167 | |
| 168 | |
Don Garrett | 8dace27 | 2018-07-19 14:07:42 -0700 | [diff] [blame] | 169 | def genLuciSchedulerConfig(site_config, branch_config): |
Don Garrett | be3608b | 2018-06-05 17:10:09 -0700 | [diff] [blame] | 170 | """Generate a luciSchedulerConfig as a string. |
| 171 | |
| 172 | Args: |
| 173 | site_config: A config_lib.SiteConfig instance. |
Don Garrett | 8dace27 | 2018-07-19 14:07:42 -0700 | [diff] [blame] | 174 | branch_config: A list of BuildConfig instances to schedule. |
Don Garrett | be3608b | 2018-06-05 17:10:09 -0700 | [diff] [blame] | 175 | |
| 176 | Returns: |
| 177 | The complete scheduler configuration contents as a string. |
| 178 | """ |
| 179 | # Trigger collection is used to collect together trigger information, so |
| 180 | # we can reuse the same trigger for multiple builds as needed. |
| 181 | # It maps gitiles_key to a set of build_names. |
| 182 | # A gitiles_key = (gitiles_url, tuple(ref_list)) |
| 183 | trigger_collection = {} |
| 184 | |
| 185 | jobs = [] |
Don Garrett | be3608b | 2018-06-05 17:10:09 -0700 | [diff] [blame] | 186 | |
Don Garrett | 8dace27 | 2018-07-19 14:07:42 -0700 | [diff] [blame] | 187 | # Order the configs consistently. |
| 188 | configs = [site_config[name] for name in sorted(site_config)] + branch_config |
| 189 | |
| 190 | for config in configs: |
Don Garrett | be3608b | 2018-06-05 17:10:09 -0700 | [diff] [blame] | 191 | # Populate jobs. |
| 192 | if config.schedule: |
| 193 | jobs.append(genSchedulerJob(config)) |
| 194 | |
| 195 | # Populate trigger_collection. |
| 196 | if config.triggered_gitiles: |
David Burger | cba0d27 | 2020-06-24 16:21:26 -0600 | [diff] [blame] | 197 | for trigger in config.triggered_gitiles: |
| 198 | try: |
| 199 | gitiles_url, ref_list, path_regexps = trigger |
| 200 | except ValueError: |
| 201 | gitiles_url, ref_list = trigger |
| 202 | path_regexps = [] |
| 203 | gitiles_key = (gitiles_url, tuple(ref_list), tuple(path_regexps)) |
Don Garrett | be3608b | 2018-06-05 17:10:09 -0700 | [diff] [blame] | 204 | trigger_collection.setdefault(gitiles_key, set()) |
Don Garrett | 9a0d90c | 2018-10-30 12:47:14 -0700 | [diff] [blame] | 205 | trigger_collection[gitiles_key].add(buildJobName(config)) |
Don Garrett | be3608b | 2018-06-05 17:10:09 -0700 | [diff] [blame] | 206 | |
| 207 | # Populate triggers. |
| 208 | triggers = [] |
| 209 | trigger_counter = 0 |
| 210 | for gitiles_key in sorted(trigger_collection): |
| 211 | builds = sorted(trigger_collection[gitiles_key]) |
| 212 | |
| 213 | trigger_name = 'trigger_%s' % trigger_counter |
David Burger | cba0d27 | 2020-06-24 16:21:26 -0600 | [diff] [blame] | 214 | gitiles_url, refs, path_regexps = gitiles_key |
Don Garrett | be3608b | 2018-06-05 17:10:09 -0700 | [diff] [blame] | 215 | triggers.append(genSchedulerTrigger( |
David Burger | cba0d27 | 2020-06-24 16:21:26 -0600 | [diff] [blame] | 216 | trigger_name, gitiles_url, refs, path_regexps, builds)) |
Don Garrett | be3608b | 2018-06-05 17:10:09 -0700 | [diff] [blame] | 217 | trigger_counter += 1 |
| 218 | |
| 219 | return ''.join([_CONFIG_HEADER] + triggers + jobs) |
| 220 | |
| 221 | |
| 222 | def GetParser(): |
| 223 | """Creates the argparse parser.""" |
| 224 | parser = commandline.ArgumentParser(description=__doc__) |
| 225 | |
| 226 | parser.add_argument('-o', '--file_out', type='path', |
| 227 | help='Write output to specified file.') |
| 228 | |
| 229 | return parser |
| 230 | |
| 231 | |
| 232 | def main(argv): |
| 233 | parser = GetParser() |
| 234 | options = parser.parse_args(argv) |
| 235 | options.Freeze() |
| 236 | |
| 237 | site_config = config_lib.GetConfig() |
Don Garrett | 8dace27 | 2018-07-19 14:07:42 -0700 | [diff] [blame] | 238 | branch_config = chromeos_config.BranchScheduleConfig() |
Don Garrett | be3608b | 2018-06-05 17:10:09 -0700 | [diff] [blame] | 239 | |
| 240 | 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] | 241 | fh.write(genLuciSchedulerConfig(site_config, branch_config)) |