blob: 423e132baa1d90497e89bdfb1f422d2cadbeafab [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 Jones48ddfd92020-09-16 13:46:41 -060059 }
60 acls {
61 role: TRIGGERER
LaMont Jonesd1bb0002020-09-16 09:35:10 -060062 granted_to: "group:project-chromeos-buildbucket-schedulers"
Jason D. Clintona9e374a2018-10-18 18:41:00 -060063 }
Don Garrettbe3608b2018-06-05 17:10:09 -070064}
65"""
66
Don Garrett9a0d90c2018-10-30 12:47:14 -070067def buildJobName(build_config):
68 if 'schedule_branch' in build_config:
69 return '%s-%s' % (build_config.schedule_branch, build_config.name)
70 else:
71 return build_config.name
72
Don Garrettbe3608b2018-06-05 17:10:09 -070073
74def genSchedulerJob(build_config):
75 """Generate the luci scheduler job for a given build config.
76
77 Args:
78 build_config: config_lib.BuildConfig.
79
80 Returns:
81 Multiline string to include in the luci scheduler configuration.
82 """
Don Garrett9a0d90c2018-10-30 12:47:14 -070083 job_name = buildJobName(build_config)
Don Garrett73148e52018-08-17 16:54:46 -070084 if 'schedule_branch' in build_config:
85 branch = build_config.schedule_branch
Don Garrett73148e52018-08-17 16:54:46 -070086 else:
87 branch = 'master'
Don Garrett73148e52018-08-17 16:54:46 -070088
89 tags = {
90 'cbb_branch': branch,
91 'cbb_config': build_config.name,
92 'cbb_display_label': build_config.display_label,
93 'cbb_workspace_branch': build_config.workspace_branch,
Yoshisato Yanagisawa16285b42018-09-19 14:00:58 +090094 'cbb_goma_client_type': build_config.goma_client_type,
Don Garrett73148e52018-08-17 16:54:46 -070095 }
96
97 # Filter out tags with no value set.
Mike Frysinger0bdbc102019-06-13 15:27:29 -040098 tags = {k: v for k, v in tags.items() if v}
Don Garrett73148e52018-08-17 16:54:46 -070099
100
101 tag_lines = [' tags: "%s:%s"' % (k, tags[k])
102 for k in sorted(tags.keys())]
103 prop_lines = [' properties: "%s:%s"' % (k, tags[k])
104 for k in sorted(tags.keys())]
105
Don Garrettbe3608b2018-06-05 17:10:09 -0700106 # TODO: Move --buildbot arg into recipe, and remove from here.
107 template = """
108job {
Don Garrett8dace272018-07-19 14:07:42 -0700109 id: "%(job_name)s"
Don Garrettbe3608b2018-06-05 17:10:09 -0700110 acl_sets: "default"
111 schedule: "%(schedule)s"
112 buildbucket: {
113 server: "cr-buildbucket.appspot.com"
114 bucket: "luci.chromeos.general"
115 builder: "%(builder)s"
Don Garrett73148e52018-08-17 16:54:46 -0700116%(tag_lines)s
117%(prop_lines)s
Don Garrettbe3608b2018-06-05 17:10:09 -0700118 properties: "cbb_extra_args:[\\"--buildbot\\"]"
119 }
120}
121"""
Don Garrett8dace272018-07-19 14:07:42 -0700122
Don Garrettbe3608b2018-06-05 17:10:09 -0700123 return template % {
Don Garrett8dace272018-07-19 14:07:42 -0700124 'job_name': job_name,
Don Garrettbe3608b2018-06-05 17:10:09 -0700125 'builder': build_config.luci_builder,
Don Garrettbe3608b2018-06-05 17:10:09 -0700126 'schedule': build_config.schedule,
Don Garrett73148e52018-08-17 16:54:46 -0700127 'tag_lines': '\n'.join(tag_lines),
128 'prop_lines': '\n'.join(prop_lines),
Don Garrettbe3608b2018-06-05 17:10:09 -0700129 }
130
131
David Burgercba0d272020-06-24 16:21:26 -0600132def genSchedulerTrigger(trigger_name, repo, refs, path_regexps, builds):
Don Garrettbe3608b2018-06-05 17:10:09 -0700133 """Generate the luci scheduler job for a given build config.
134
135 Args:
136 trigger_name: Name of the trigger as a string.
137 repo: Gitiles URL git git repository.
138 refs: Iterable of git refs to check. May use regular expressions.
David Burgercba0d272020-06-24 16:21:26 -0600139 path_regexps: Iterable of path regular expressions of files to trigger on
140 or falsy to trigger on everything.
Don Garrettbe3608b2018-06-05 17:10:09 -0700141 builds: Iterable of build config names to trigger.
142
143 Returns:
144 Multiline string to include in the luci scheduler configuration.
145 """
146 template = """
147trigger {
148 id: "%(trigger_name)s"
149 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"""
David Burgercba0d272020-06-24 16:21:26 -0600158 if path_regexps:
159 path_regexps = '\n' + '\n'.join(' path_regexps: "%s"' %
160 r for r in path_regexps)
161 else:
162 path_regexps = ''
Don Garrettbe3608b2018-06-05 17:10:09 -0700163 return template % {
164 'trigger_name': trigger_name,
165 'repo': repo,
166 'refs': '\n'.join(' refs: "%s"' % r for r in refs),
David Burgercba0d272020-06-24 16:21:26 -0600167 'path_regexps': path_regexps,
Don Garrettbe3608b2018-06-05 17:10:09 -0700168 'triggers': '\n'.join(' triggers: "%s"' % b for b in builds),
169 }
170
171
Don Garrett8dace272018-07-19 14:07:42 -0700172def genLuciSchedulerConfig(site_config, branch_config):
Don Garrettbe3608b2018-06-05 17:10:09 -0700173 """Generate a luciSchedulerConfig as a string.
174
175 Args:
176 site_config: A config_lib.SiteConfig instance.
Don Garrett8dace272018-07-19 14:07:42 -0700177 branch_config: A list of BuildConfig instances to schedule.
Don Garrettbe3608b2018-06-05 17:10:09 -0700178
179 Returns:
180 The complete scheduler configuration contents as a string.
181 """
182 # Trigger collection is used to collect together trigger information, so
183 # we can reuse the same trigger for multiple builds as needed.
184 # It maps gitiles_key to a set of build_names.
185 # A gitiles_key = (gitiles_url, tuple(ref_list))
186 trigger_collection = {}
187
188 jobs = []
Don Garrettbe3608b2018-06-05 17:10:09 -0700189
Don Garrett8dace272018-07-19 14:07:42 -0700190 # Order the configs consistently.
191 configs = [site_config[name] for name in sorted(site_config)] + branch_config
192
193 for config in configs:
Don Garrettbe3608b2018-06-05 17:10:09 -0700194 # Populate jobs.
195 if config.schedule:
196 jobs.append(genSchedulerJob(config))
197
198 # Populate trigger_collection.
199 if config.triggered_gitiles:
David Burgercba0d272020-06-24 16:21:26 -0600200 for trigger in config.triggered_gitiles:
201 try:
202 gitiles_url, ref_list, path_regexps = trigger
203 except ValueError:
204 gitiles_url, ref_list = trigger
205 path_regexps = []
206 gitiles_key = (gitiles_url, tuple(ref_list), tuple(path_regexps))
Don Garrettbe3608b2018-06-05 17:10:09 -0700207 trigger_collection.setdefault(gitiles_key, set())
Don Garrett9a0d90c2018-10-30 12:47:14 -0700208 trigger_collection[gitiles_key].add(buildJobName(config))
Don Garrettbe3608b2018-06-05 17:10:09 -0700209
210 # Populate triggers.
211 triggers = []
212 trigger_counter = 0
213 for gitiles_key in sorted(trigger_collection):
214 builds = sorted(trigger_collection[gitiles_key])
215
216 trigger_name = 'trigger_%s' % trigger_counter
David Burgercba0d272020-06-24 16:21:26 -0600217 gitiles_url, refs, path_regexps = gitiles_key
Don Garrettbe3608b2018-06-05 17:10:09 -0700218 triggers.append(genSchedulerTrigger(
David Burgercba0d272020-06-24 16:21:26 -0600219 trigger_name, gitiles_url, refs, path_regexps, builds))
Don Garrettbe3608b2018-06-05 17:10:09 -0700220 trigger_counter += 1
221
222 return ''.join([_CONFIG_HEADER] + triggers + jobs)
223
224
225def GetParser():
226 """Creates the argparse parser."""
227 parser = commandline.ArgumentParser(description=__doc__)
228
229 parser.add_argument('-o', '--file_out', type='path',
230 help='Write output to specified file.')
231
232 return parser
233
234
235def main(argv):
236 parser = GetParser()
237 options = parser.parse_args(argv)
238 options.Freeze()
239
240 site_config = config_lib.GetConfig()
Don Garrett8dace272018-07-19 14:07:42 -0700241 branch_config = chromeos_config.BranchScheduleConfig()
Don Garrettbe3608b2018-06-05 17:10:09 -0700242
243 with (open(options.file_out, 'w') if options.file_out else sys.stdout) as fh:
Don Garrett8dace272018-07-19 14:07:42 -0700244 fh.write(genLuciSchedulerConfig(site_config, branch_config))