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