blob: 397dc1d295057cfc9b50ac3307d34fbb500c00cf [file] [log] [blame]
Mike Frysingerf1ba7ad2022-09-12 05:42:57 -04001# Copyright 2018 The ChromiumOS Authors
Don Garrettbe3608b2018-06-05 17:10:09 -07002# Use of this source code is governed by a BSD-style license that can be
3# found in the LICENSE file.
4
Don Garrettff45f4b2018-10-04 09:08:01 -07005# pylint: disable=line-too-long
Mike Frysingera2527f82023-02-03 04:32:15 -05006
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:
Mike Nicholsb575c612021-04-09 10:04:43 -060013 https://data.corp.google.com/sites/chromeos_ci_cros_ci_builds/utility/?f=board_name:in: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"""
Mike Frysinger93afe3e2023-09-05 14:18:52 -040022
Don Garrettff45f4b2018-10-04 09:08:01 -070023# pylint: enable=line-too-long
Don Garrettbe3608b2018-06-05 17:10:09 -070024
Don Garrettbe3608b2018-06-05 17:10:09 -070025import sys
26
Don Garrett8dace272018-07-19 14:07:42 -070027from chromite.config import chromeos_config
Don Garrettbe3608b2018-06-05 17:10:09 -070028from chromite.lib import commandline
29from chromite.lib import config_lib
30
31
32_CONFIG_HEADER = """# Defines buckets on luci-scheduler.appspot.com.
33#
34# For schema of this file and documentation see ProjectConfig message in
Mike Frysinger92dc6492021-02-17 16:01:09 -050035# https://github.com/luci/luci-go/blob/HEAD/scheduler/appengine/messages/config.proto
Don Garrettbe3608b2018-06-05 17:10:09 -070036
37# Generated with chromite/scripts/gen_luci_scheduler
38
Don Garrettff45f4b2018-10-04 09:08:01 -070039# Autodeployed with:
Mike Nicholsb575c612021-04-09 10:04:43 -060040# https://data.corp.google.com/sites/chromeos_ci_cros_ci_builds/utility/?f=board_name:in:luci-scheduler-updater
Don Garrettff45f4b2018-10-04 09:08:01 -070041
Don Garrettbe3608b2018-06-05 17:10:09 -070042acl_sets {
43 name: "default"
44 acls {
45 role: READER
46 granted_to: "group:googlers"
47 }
48 acls {
49 role: OWNER
50 granted_to: "group:project-chromeos-admins"
51 }
Jason D. Clintona9e374a2018-10-18 18:41:00 -060052 acls {
53 role: TRIGGERER
54 granted_to: "group:mdb/chromeos-build-access"
LaMont Jones48ddfd92020-09-16 13:46:41 -060055 }
56 acls {
57 role: TRIGGERER
LaMont Jonesd1bb0002020-09-16 09:35:10 -060058 granted_to: "group:project-chromeos-buildbucket-schedulers"
Jason D. Clintona9e374a2018-10-18 18:41:00 -060059 }
Don Garrettbe3608b2018-06-05 17:10:09 -070060}
61"""
62
Alex Klein1699fab2022-09-08 08:46:06 -060063
Don Garrett9a0d90c2018-10-30 12:47:14 -070064def buildJobName(build_config):
Alex Klein1699fab2022-09-08 08:46:06 -060065 if "schedule_branch" in build_config:
66 return "%s-%s" % (build_config.schedule_branch, build_config.name)
67 else:
68 return build_config.name
Don Garrett9a0d90c2018-10-30 12:47:14 -070069
Don Garrettbe3608b2018-06-05 17:10:09 -070070
71def genSchedulerJob(build_config):
Alex Klein1699fab2022-09-08 08:46:06 -060072 """Generate the luci scheduler job for a given build config.
Don Garrettbe3608b2018-06-05 17:10:09 -070073
Alex Klein1699fab2022-09-08 08:46:06 -060074 Args:
Trent Apted66736d82023-05-25 10:38:28 +100075 build_config: config_lib.BuildConfig.
Don Garrettbe3608b2018-06-05 17:10:09 -070076
Alex Klein1699fab2022-09-08 08:46:06 -060077 Returns:
Trent Apted66736d82023-05-25 10:38:28 +100078 Multiline string to include in the luci scheduler configuration.
Alex Klein1699fab2022-09-08 08:46:06 -060079 """
80 job_name = buildJobName(build_config)
81 if "schedule_branch" in build_config:
82 branch = build_config.schedule_branch
83 else:
84 branch = "main"
Don Garrett73148e52018-08-17 16:54:46 -070085
Alex Klein1699fab2022-09-08 08:46:06 -060086 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,
Mike Frysinger6976b832023-07-10 12:14:50 -040091 "cbb_goma_client_type": None,
Alex Klein1699fab2022-09-08 08:46:06 -060092 }
Don Garrett73148e52018-08-17 16:54:46 -070093
Alex Klein1699fab2022-09-08 08:46:06 -060094 # Filter out tags with no value set.
95 tags = {k: v for k, v in tags.items() if v}
Don Garrett73148e52018-08-17 16:54:46 -070096
Alex Klein1699fab2022-09-08 08:46:06 -060097 tag_lines = [
98 ' tags: "%s:%s"' % (k, tags[k]) for k in sorted(tags.keys())
99 ]
100 prop_lines = [
101 ' properties: "%s:%s"' % (k, tags[k]) for k in sorted(tags.keys())
102 ]
Don Garrett73148e52018-08-17 16:54:46 -0700103
Alex Klein1699fab2022-09-08 08:46:06 -0600104 # TODO: Move --buildbot arg into recipe, and remove from here.
105 template = """
Don Garrettbe3608b2018-06-05 17:10:09 -0700106job {
Don Garrett8dace272018-07-19 14:07:42 -0700107 id: "%(job_name)s"
Vadim Shtayura6398e002021-01-11 12:02:11 -0800108 realm: "cbb-jobs"
Don Garrettbe3608b2018-06-05 17:10:09 -0700109 acl_sets: "default"
110 schedule: "%(schedule)s"
111 buildbucket: {
112 server: "cr-buildbucket.appspot.com"
Vadim Shtayura959cb0f2022-02-03 12:45:58 -0800113 bucket: "general"
Don Garrettbe3608b2018-06-05 17:10:09 -0700114 builder: "%(builder)s"
Don Garrett73148e52018-08-17 16:54:46 -0700115%(tag_lines)s
116%(prop_lines)s
Don Garrettbe3608b2018-06-05 17:10:09 -0700117 properties: "cbb_extra_args:[\\"--buildbot\\"]"
118 }
119}
120"""
Don Garrett8dace272018-07-19 14:07:42 -0700121
Alex Klein1699fab2022-09-08 08:46:06 -0600122 return template % {
123 "job_name": job_name,
124 "builder": build_config.luci_builder,
125 "schedule": build_config.schedule,
126 "tag_lines": "\n".join(tag_lines),
127 "prop_lines": "\n".join(prop_lines),
128 }
Don Garrettbe3608b2018-06-05 17:10:09 -0700129
130
David Burgercba0d272020-06-24 16:21:26 -0600131def genSchedulerTrigger(trigger_name, repo, refs, path_regexps, builds):
Alex Klein1699fab2022-09-08 08:46:06 -0600132 """Generate the luci scheduler job for a given build config.
Don Garrettbe3608b2018-06-05 17:10:09 -0700133
Alex Klein1699fab2022-09-08 08:46:06 -0600134 Args:
Trent Apted66736d82023-05-25 10:38:28 +1000135 trigger_name: Name of the trigger as a string.
136 repo: Gitiles URL git git repository.
137 refs: Iterable of git refs to check. May use regular expressions.
138 path_regexps: Iterable of path regular expressions of files to trigger
139 on or falsey to trigger on everything.
140 builds: Iterable of build config names to trigger.
Don Garrettbe3608b2018-06-05 17:10:09 -0700141
Alex Klein1699fab2022-09-08 08:46:06 -0600142 Returns:
Trent Apted66736d82023-05-25 10:38:28 +1000143 Multiline string to include in the luci scheduler configuration.
Alex Klein1699fab2022-09-08 08:46:06 -0600144 """
145 template = """
Don Garrettbe3608b2018-06-05 17:10:09 -0700146trigger {
147 id: "%(trigger_name)s"
Vadim Shtayura6398e002021-01-11 12:02:11 -0800148 realm: "cbb-jobs"
Don Garrettbe3608b2018-06-05 17:10:09 -0700149 acl_sets: "default"
150 schedule: "with 5m interval"
151 gitiles: {
152 repo: "%(repo)s"
David Burgercba0d272020-06-24 16:21:26 -0600153%(refs)s%(path_regexps)s
Don Garrettbe3608b2018-06-05 17:10:09 -0700154 }
155%(triggers)s
156}
157"""
Alex Klein1699fab2022-09-08 08:46:06 -0600158 if path_regexps:
159 path_regexps = "\n" + "\n".join(
160 ' path_regexps: "%s"' % r for r in path_regexps
161 )
162 else:
163 path_regexps = ""
164 return template % {
165 "trigger_name": trigger_name,
166 "repo": repo,
167 "refs": "\n".join(' refs: "%s"' % r for r in refs),
168 "path_regexps": path_regexps,
169 "triggers": "\n".join(' triggers: "%s"' % b for b in builds),
170 }
Don Garrettbe3608b2018-06-05 17:10:09 -0700171
172
Don Garrett8dace272018-07-19 14:07:42 -0700173def genLuciSchedulerConfig(site_config, branch_config):
Alex Klein1699fab2022-09-08 08:46:06 -0600174 """Generate a luciSchedulerConfig as a string.
Don Garrettbe3608b2018-06-05 17:10:09 -0700175
Alex Klein1699fab2022-09-08 08:46:06 -0600176 Args:
Trent Apted66736d82023-05-25 10:38:28 +1000177 site_config: A config_lib.SiteConfig instance.
178 branch_config: A list of BuildConfig instances to schedule.
Don Garrettbe3608b2018-06-05 17:10:09 -0700179
Alex Klein1699fab2022-09-08 08:46:06 -0600180 Returns:
Trent Apted66736d82023-05-25 10:38:28 +1000181 The complete scheduler configuration contents as a string.
Alex Klein1699fab2022-09-08 08:46:06 -0600182 """
183 # Trigger collection is used to collect together trigger information, so
184 # we can reuse the same trigger for multiple builds as needed.
185 # It maps gitiles_key to a set of build_names.
186 # A gitiles_key = (gitiles_url, tuple(ref_list))
187 trigger_collection = {}
Don Garrettbe3608b2018-06-05 17:10:09 -0700188
Alex Klein1699fab2022-09-08 08:46:06 -0600189 jobs = []
Don Garrettbe3608b2018-06-05 17:10:09 -0700190
Alex Klein1699fab2022-09-08 08:46:06 -0600191 # Order the configs consistently.
192 configs = [
193 site_config[name] for name in sorted(site_config)
194 ] + branch_config
Don Garrett8dace272018-07-19 14:07:42 -0700195
Alex Klein1699fab2022-09-08 08:46:06 -0600196 for config in configs:
197 # Populate jobs.
198 if config.schedule:
199 jobs.append(genSchedulerJob(config))
Don Garrettbe3608b2018-06-05 17:10:09 -0700200
Alex Klein1699fab2022-09-08 08:46:06 -0600201 # Populate trigger_collection.
202 if config.triggered_gitiles:
203 for trigger in config.triggered_gitiles:
204 try:
205 gitiles_url, ref_list, path_regexps = trigger
206 except ValueError:
207 gitiles_url, ref_list = trigger
208 path_regexps = []
209 gitiles_key = (
210 gitiles_url,
211 tuple(ref_list),
212 tuple(path_regexps),
213 )
214 trigger_collection.setdefault(gitiles_key, set())
215 trigger_collection[gitiles_key].add(buildJobName(config))
Don Garrettbe3608b2018-06-05 17:10:09 -0700216
Alex Klein1699fab2022-09-08 08:46:06 -0600217 # Populate triggers.
218 triggers = []
219 trigger_counter = 0
220 for gitiles_key in sorted(trigger_collection):
221 builds = sorted(trigger_collection[gitiles_key])
Don Garrettbe3608b2018-06-05 17:10:09 -0700222
Alex Klein1699fab2022-09-08 08:46:06 -0600223 trigger_name = "trigger_%s" % trigger_counter
224 gitiles_url, refs, path_regexps = gitiles_key
225 triggers.append(
226 genSchedulerTrigger(
227 trigger_name, gitiles_url, refs, path_regexps, builds
228 )
229 )
230 trigger_counter += 1
Don Garrettbe3608b2018-06-05 17:10:09 -0700231
Alex Klein1699fab2022-09-08 08:46:06 -0600232 return "".join([_CONFIG_HEADER] + triggers + jobs)
Don Garrettbe3608b2018-06-05 17:10:09 -0700233
234
235def GetParser():
Alex Klein1699fab2022-09-08 08:46:06 -0600236 """Creates the argparse parser."""
237 parser = commandline.ArgumentParser(description=__doc__)
Don Garrettbe3608b2018-06-05 17:10:09 -0700238
Alex Klein1699fab2022-09-08 08:46:06 -0600239 parser.add_argument(
240 "-o", "--file_out", type="path", help="Write output to specified file."
241 )
Don Garrettbe3608b2018-06-05 17:10:09 -0700242
Alex Klein1699fab2022-09-08 08:46:06 -0600243 return parser
Don Garrettbe3608b2018-06-05 17:10:09 -0700244
245
246def main(argv):
Alex Klein1699fab2022-09-08 08:46:06 -0600247 parser = GetParser()
248 options = parser.parse_args(argv)
249 options.Freeze()
Don Garrettbe3608b2018-06-05 17:10:09 -0700250
Alex Klein1699fab2022-09-08 08:46:06 -0600251 site_config = config_lib.GetConfig()
252 branch_config = chromeos_config.BranchScheduleConfig()
Don Garrettbe3608b2018-06-05 17:10:09 -0700253
Alex Klein1699fab2022-09-08 08:46:06 -0600254 with (
Mike Frysinger31fdddd2023-02-24 15:50:55 -0500255 open(options.file_out, "w", encoding="utf-8")
256 if options.file_out
257 else sys.stdout
Alex Klein1699fab2022-09-08 08:46:06 -0600258 ) as fh:
259 fh.write(genLuciSchedulerConfig(site_config, branch_config))