blob: 7d6b7c79b874a21df4e95f4bae85d4d991082ee5 [file] [log] [blame]
Don Garrettbe3608b2018-06-05 17:10:09 -07001# -*- 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 Garrettff45f4b2018-10-04 09:08:01 -07006# pylint: disable=line-too-long
Don Garrettbe3608b2018-06-05 17:10:09 -07007"""Generate LUCI Scheduler config file.
8
9This generates the LUCI Scheduler configuration file for ChromeOS builds based
10on the chromeos_config contents.
11
Don Garrettff45f4b2018-10-04 09:08:01 -070012Changes to chromite/config/luci-scheduler.cfg will be autodeployed:
13 http://cros-goldeneye/chromeos/legoland/builderHistory?buildConfig=luci-scheduler-updater
Don Garrettbe3608b2018-06-05 17:10:09 -070014
Don Garrettc8323dc2018-08-22 15:58:26 -070015Notes:
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 Garrettbe3608b2018-06-05 17:10:09 -070021"""
Don Garrettff45f4b2018-10-04 09:08:01 -070022# pylint: enable=line-too-long
Don Garrettbe3608b2018-06-05 17:10:09 -070023
24from __future__ import print_function
25
26import sys
27
Don Garrett8dace272018-07-19 14:07:42 -070028from chromite.config import chromeos_config
Don Garrettbe3608b2018-06-05 17:10:09 -070029from chromite.lib import commandline
30from chromite.lib import config_lib
31
32
Mike Frysinger74a6cc82020-02-14 14:16:22 -050033assert sys.version_info >= (3, 6), 'This module requires Python 3.6+'
34
35
Don Garrettbe3608b2018-06-05 17:10:09 -070036_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 Garrettff45f4b2018-10-04 09:08:01 -070043# Autodeployed with:
44# http://cros-goldeneye/chromeos/legoland/builderHistory?buildConfig=luci-scheduler-updater
45
Don Garrettbe3608b2018-06-05 17:10:09 -070046acl_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. Clintona9e374a2018-10-18 18:41:00 -060056 acls {
57 role: TRIGGERER
58 granted_to: "group:mdb/chromeos-build-access"
LaMont Jonesd1bb0002020-09-16 09:35:10 -060059 granted_to: "group:project-chromeos-buildbucket-schedulers"
Jason D. Clintona9e374a2018-10-18 18:41:00 -060060 }
Don Garrettbe3608b2018-06-05 17:10:09 -070061}
62"""
63
Don Garrett9a0d90c2018-10-30 12:47:14 -070064def 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 Garrettbe3608b2018-06-05 17:10:09 -070070
71def 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 Garrett9a0d90c2018-10-30 12:47:14 -070080 job_name = buildJobName(build_config)
Don Garrett73148e52018-08-17 16:54:46 -070081 if 'schedule_branch' in build_config:
82 branch = build_config.schedule_branch
Don Garrett73148e52018-08-17 16:54:46 -070083 else:
84 branch = 'master'
Don Garrett73148e52018-08-17 16:54:46 -070085
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 Yanagisawa16285b42018-09-19 14:00:58 +090091 'cbb_goma_client_type': build_config.goma_client_type,
Don Garrett73148e52018-08-17 16:54:46 -070092 }
93
94 # Filter out tags with no value set.
Mike Frysinger0bdbc102019-06-13 15:27:29 -040095 tags = {k: v for k, v in tags.items() if v}
Don Garrett73148e52018-08-17 16:54:46 -070096
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 Garrettbe3608b2018-06-05 17:10:09 -0700103 # TODO: Move --buildbot arg into recipe, and remove from here.
104 template = """
105job {
Don Garrett8dace272018-07-19 14:07:42 -0700106 id: "%(job_name)s"
Don Garrettbe3608b2018-06-05 17:10:09 -0700107 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 Garrett73148e52018-08-17 16:54:46 -0700113%(tag_lines)s
114%(prop_lines)s
Don Garrettbe3608b2018-06-05 17:10:09 -0700115 properties: "cbb_extra_args:[\\"--buildbot\\"]"
116 }
117}
118"""
Don Garrett8dace272018-07-19 14:07:42 -0700119
Don Garrettbe3608b2018-06-05 17:10:09 -0700120 return template % {
Don Garrett8dace272018-07-19 14:07:42 -0700121 'job_name': job_name,
Don Garrettbe3608b2018-06-05 17:10:09 -0700122 'builder': build_config.luci_builder,
Don Garrettbe3608b2018-06-05 17:10:09 -0700123 'schedule': build_config.schedule,
Don Garrett73148e52018-08-17 16:54:46 -0700124 'tag_lines': '\n'.join(tag_lines),
125 'prop_lines': '\n'.join(prop_lines),
Don Garrettbe3608b2018-06-05 17:10:09 -0700126 }
127
128
David Burgercba0d272020-06-24 16:21:26 -0600129def genSchedulerTrigger(trigger_name, repo, refs, path_regexps, builds):
Don Garrettbe3608b2018-06-05 17:10:09 -0700130 """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 Burgercba0d272020-06-24 16:21:26 -0600136 path_regexps: Iterable of path regular expressions of files to trigger on
137 or falsy to trigger on everything.
Don Garrettbe3608b2018-06-05 17:10:09 -0700138 builds: Iterable of build config names to trigger.
139
140 Returns:
141 Multiline string to include in the luci scheduler configuration.
142 """
143 template = """
144trigger {
145 id: "%(trigger_name)s"
146 acl_sets: "default"
147 schedule: "with 5m interval"
148 gitiles: {
149 repo: "%(repo)s"
David Burgercba0d272020-06-24 16:21:26 -0600150%(refs)s%(path_regexps)s
Don Garrettbe3608b2018-06-05 17:10:09 -0700151 }
152%(triggers)s
153}
154"""
David Burgercba0d272020-06-24 16:21:26 -0600155 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 Garrettbe3608b2018-06-05 17:10:09 -0700160 return template % {
161 'trigger_name': trigger_name,
162 'repo': repo,
163 'refs': '\n'.join(' refs: "%s"' % r for r in refs),
David Burgercba0d272020-06-24 16:21:26 -0600164 'path_regexps': path_regexps,
Don Garrettbe3608b2018-06-05 17:10:09 -0700165 'triggers': '\n'.join(' triggers: "%s"' % b for b in builds),
166 }
167
168
Don Garrett8dace272018-07-19 14:07:42 -0700169def genLuciSchedulerConfig(site_config, branch_config):
Don Garrettbe3608b2018-06-05 17:10:09 -0700170 """Generate a luciSchedulerConfig as a string.
171
172 Args:
173 site_config: A config_lib.SiteConfig instance.
Don Garrett8dace272018-07-19 14:07:42 -0700174 branch_config: A list of BuildConfig instances to schedule.
Don Garrettbe3608b2018-06-05 17:10:09 -0700175
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 Garrettbe3608b2018-06-05 17:10:09 -0700186
Don Garrett8dace272018-07-19 14:07:42 -0700187 # Order the configs consistently.
188 configs = [site_config[name] for name in sorted(site_config)] + branch_config
189
190 for config in configs:
Don Garrettbe3608b2018-06-05 17:10:09 -0700191 # Populate jobs.
192 if config.schedule:
193 jobs.append(genSchedulerJob(config))
194
195 # Populate trigger_collection.
196 if config.triggered_gitiles:
David Burgercba0d272020-06-24 16:21:26 -0600197 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 Garrettbe3608b2018-06-05 17:10:09 -0700204 trigger_collection.setdefault(gitiles_key, set())
Don Garrett9a0d90c2018-10-30 12:47:14 -0700205 trigger_collection[gitiles_key].add(buildJobName(config))
Don Garrettbe3608b2018-06-05 17:10:09 -0700206
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 Burgercba0d272020-06-24 16:21:26 -0600214 gitiles_url, refs, path_regexps = gitiles_key
Don Garrettbe3608b2018-06-05 17:10:09 -0700215 triggers.append(genSchedulerTrigger(
David Burgercba0d272020-06-24 16:21:26 -0600216 trigger_name, gitiles_url, refs, path_regexps, builds))
Don Garrettbe3608b2018-06-05 17:10:09 -0700217 trigger_counter += 1
218
219 return ''.join([_CONFIG_HEADER] + triggers + jobs)
220
221
222def 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
232def main(argv):
233 parser = GetParser()
234 options = parser.parse_args(argv)
235 options.Freeze()
236
237 site_config = config_lib.GetConfig()
Don Garrett8dace272018-07-19 14:07:42 -0700238 branch_config = chromeos_config.BranchScheduleConfig()
Don Garrettbe3608b2018-06-05 17:10:09 -0700239
240 with (open(options.file_out, 'w') if options.file_out else sys.stdout) as fh:
Don Garrett8dace272018-07-19 14:07:42 -0700241 fh.write(genLuciSchedulerConfig(site_config, branch_config))