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 | |
| 33 | _CONFIG_HEADER = """# Defines buckets on luci-scheduler.appspot.com. |
| 34 | # |
| 35 | # For schema of this file and documentation see ProjectConfig message in |
| 36 | # https://github.com/luci/luci-go/blob/master/scheduler/appengine/messages/config.proto |
| 37 | |
| 38 | # Generated with chromite/scripts/gen_luci_scheduler |
| 39 | |
Don Garrett | ff45f4b | 2018-10-04 09:08:01 -0700 | [diff] [blame] | 40 | # Autodeployed with: |
| 41 | # http://cros-goldeneye/chromeos/legoland/builderHistory?buildConfig=luci-scheduler-updater |
| 42 | |
Don Garrett | be3608b | 2018-06-05 17:10:09 -0700 | [diff] [blame] | 43 | acl_sets { |
| 44 | name: "default" |
| 45 | acls { |
| 46 | role: READER |
| 47 | granted_to: "group:googlers" |
| 48 | } |
| 49 | acls { |
| 50 | role: OWNER |
| 51 | granted_to: "group:project-chromeos-admins" |
| 52 | } |
| 53 | } |
| 54 | """ |
| 55 | |
| 56 | |
| 57 | def genSchedulerJob(build_config): |
| 58 | """Generate the luci scheduler job for a given build config. |
| 59 | |
| 60 | Args: |
| 61 | build_config: config_lib.BuildConfig. |
| 62 | |
| 63 | Returns: |
| 64 | Multiline string to include in the luci scheduler configuration. |
| 65 | """ |
Don Garrett | 73148e5 | 2018-08-17 16:54:46 -0700 | [diff] [blame] | 66 | if 'schedule_branch' in build_config: |
| 67 | branch = build_config.schedule_branch |
| 68 | job_name = '%s-%s' % (branch, build_config.name) |
| 69 | else: |
| 70 | branch = 'master' |
| 71 | job_name = build_config.name |
| 72 | |
| 73 | tags = { |
| 74 | 'cbb_branch': branch, |
| 75 | 'cbb_config': build_config.name, |
| 76 | 'cbb_display_label': build_config.display_label, |
| 77 | 'cbb_workspace_branch': build_config.workspace_branch, |
Yoshisato Yanagisawa | 16285b4 | 2018-09-19 14:00:58 +0900 | [diff] [blame] | 78 | 'cbb_goma_client_type': build_config.goma_client_type, |
Don Garrett | 73148e5 | 2018-08-17 16:54:46 -0700 | [diff] [blame] | 79 | } |
| 80 | |
| 81 | # Filter out tags with no value set. |
| 82 | tags = {k: v for k, v in tags.iteritems() if v} |
| 83 | |
| 84 | |
| 85 | tag_lines = [' tags: "%s:%s"' % (k, tags[k]) |
| 86 | for k in sorted(tags.keys())] |
| 87 | prop_lines = [' properties: "%s:%s"' % (k, tags[k]) |
| 88 | for k in sorted(tags.keys())] |
| 89 | |
Don Garrett | be3608b | 2018-06-05 17:10:09 -0700 | [diff] [blame] | 90 | # TODO: Move --buildbot arg into recipe, and remove from here. |
| 91 | template = """ |
| 92 | job { |
Don Garrett | 8dace27 | 2018-07-19 14:07:42 -0700 | [diff] [blame] | 93 | id: "%(job_name)s" |
Don Garrett | be3608b | 2018-06-05 17:10:09 -0700 | [diff] [blame] | 94 | acl_sets: "default" |
| 95 | schedule: "%(schedule)s" |
| 96 | buildbucket: { |
| 97 | server: "cr-buildbucket.appspot.com" |
| 98 | bucket: "luci.chromeos.general" |
| 99 | builder: "%(builder)s" |
Don Garrett | 73148e5 | 2018-08-17 16:54:46 -0700 | [diff] [blame] | 100 | %(tag_lines)s |
| 101 | %(prop_lines)s |
Don Garrett | be3608b | 2018-06-05 17:10:09 -0700 | [diff] [blame] | 102 | properties: "cbb_extra_args:[\\"--buildbot\\"]" |
| 103 | } |
| 104 | } |
| 105 | """ |
Don Garrett | 8dace27 | 2018-07-19 14:07:42 -0700 | [diff] [blame] | 106 | |
Don Garrett | be3608b | 2018-06-05 17:10:09 -0700 | [diff] [blame] | 107 | return template % { |
Don Garrett | 8dace27 | 2018-07-19 14:07:42 -0700 | [diff] [blame] | 108 | 'job_name': job_name, |
Don Garrett | be3608b | 2018-06-05 17:10:09 -0700 | [diff] [blame] | 109 | 'builder': build_config.luci_builder, |
Don Garrett | be3608b | 2018-06-05 17:10:09 -0700 | [diff] [blame] | 110 | 'schedule': build_config.schedule, |
Don Garrett | 73148e5 | 2018-08-17 16:54:46 -0700 | [diff] [blame] | 111 | 'tag_lines': '\n'.join(tag_lines), |
| 112 | 'prop_lines': '\n'.join(prop_lines), |
Don Garrett | be3608b | 2018-06-05 17:10:09 -0700 | [diff] [blame] | 113 | } |
| 114 | |
| 115 | |
| 116 | def genSchedulerTrigger(trigger_name, repo, refs, builds): |
| 117 | """Generate the luci scheduler job for a given build config. |
| 118 | |
| 119 | Args: |
| 120 | trigger_name: Name of the trigger as a string. |
| 121 | repo: Gitiles URL git git repository. |
| 122 | refs: Iterable of git refs to check. May use regular expressions. |
| 123 | builds: Iterable of build config names to trigger. |
| 124 | |
| 125 | Returns: |
| 126 | Multiline string to include in the luci scheduler configuration. |
| 127 | """ |
| 128 | template = """ |
| 129 | trigger { |
| 130 | id: "%(trigger_name)s" |
| 131 | acl_sets: "default" |
| 132 | schedule: "with 5m interval" |
| 133 | gitiles: { |
| 134 | repo: "%(repo)s" |
| 135 | %(refs)s |
| 136 | } |
| 137 | %(triggers)s |
| 138 | } |
| 139 | """ |
| 140 | |
| 141 | return template % { |
| 142 | 'trigger_name': trigger_name, |
| 143 | 'repo': repo, |
| 144 | 'refs': '\n'.join(' refs: "%s"' % r for r in refs), |
| 145 | 'triggers': '\n'.join(' triggers: "%s"' % b for b in builds), |
| 146 | } |
| 147 | |
| 148 | |
Don Garrett | 8dace27 | 2018-07-19 14:07:42 -0700 | [diff] [blame] | 149 | def genLuciSchedulerConfig(site_config, branch_config): |
Don Garrett | be3608b | 2018-06-05 17:10:09 -0700 | [diff] [blame] | 150 | """Generate a luciSchedulerConfig as a string. |
| 151 | |
| 152 | Args: |
| 153 | site_config: A config_lib.SiteConfig instance. |
Don Garrett | 8dace27 | 2018-07-19 14:07:42 -0700 | [diff] [blame] | 154 | branch_config: A list of BuildConfig instances to schedule. |
Don Garrett | be3608b | 2018-06-05 17:10:09 -0700 | [diff] [blame] | 155 | |
| 156 | Returns: |
| 157 | The complete scheduler configuration contents as a string. |
| 158 | """ |
| 159 | # Trigger collection is used to collect together trigger information, so |
| 160 | # we can reuse the same trigger for multiple builds as needed. |
| 161 | # It maps gitiles_key to a set of build_names. |
| 162 | # A gitiles_key = (gitiles_url, tuple(ref_list)) |
| 163 | trigger_collection = {} |
| 164 | |
| 165 | jobs = [] |
Don Garrett | be3608b | 2018-06-05 17:10:09 -0700 | [diff] [blame] | 166 | |
Don Garrett | 8dace27 | 2018-07-19 14:07:42 -0700 | [diff] [blame] | 167 | # Order the configs consistently. |
| 168 | configs = [site_config[name] for name in sorted(site_config)] + branch_config |
| 169 | |
| 170 | for config in configs: |
Don Garrett | be3608b | 2018-06-05 17:10:09 -0700 | [diff] [blame] | 171 | # Populate jobs. |
| 172 | if config.schedule: |
| 173 | jobs.append(genSchedulerJob(config)) |
| 174 | |
| 175 | # Populate trigger_collection. |
| 176 | if config.triggered_gitiles: |
| 177 | for gitiles_url, ref_list in config.triggered_gitiles: |
| 178 | gitiles_key = (gitiles_url, tuple(ref_list)) |
| 179 | trigger_collection.setdefault(gitiles_key, set()) |
| 180 | trigger_collection[gitiles_key].add(config.name) |
| 181 | |
| 182 | # Populate triggers. |
| 183 | triggers = [] |
| 184 | trigger_counter = 0 |
| 185 | for gitiles_key in sorted(trigger_collection): |
| 186 | builds = sorted(trigger_collection[gitiles_key]) |
| 187 | |
| 188 | trigger_name = 'trigger_%s' % trigger_counter |
| 189 | gitiles_url, refs = gitiles_key |
| 190 | triggers.append(genSchedulerTrigger( |
| 191 | trigger_name, gitiles_url, refs, builds)) |
| 192 | trigger_counter += 1 |
| 193 | |
| 194 | return ''.join([_CONFIG_HEADER] + triggers + jobs) |
| 195 | |
| 196 | |
| 197 | def GetParser(): |
| 198 | """Creates the argparse parser.""" |
| 199 | parser = commandline.ArgumentParser(description=__doc__) |
| 200 | |
| 201 | parser.add_argument('-o', '--file_out', type='path', |
| 202 | help='Write output to specified file.') |
| 203 | |
| 204 | return parser |
| 205 | |
| 206 | |
| 207 | def main(argv): |
| 208 | parser = GetParser() |
| 209 | options = parser.parse_args(argv) |
| 210 | options.Freeze() |
| 211 | |
| 212 | site_config = config_lib.GetConfig() |
Don Garrett | 8dace27 | 2018-07-19 14:07:42 -0700 | [diff] [blame] | 213 | branch_config = chromeos_config.BranchScheduleConfig() |
Don Garrett | be3608b | 2018-06-05 17:10:09 -0700 | [diff] [blame] | 214 | |
| 215 | 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] | 216 | fh.write(genLuciSchedulerConfig(site_config, branch_config)) |