blob: 2c064819bfd8340c056d1d652817c1e05fa042ae [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"
59 }
Don Garrettbe3608b2018-06-05 17:10:09 -070060}
61"""
62
Don Garrett9a0d90c2018-10-30 12:47:14 -070063def 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 Garrettbe3608b2018-06-05 17:10:09 -070069
70def 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 Garrett9a0d90c2018-10-30 12:47:14 -070079 job_name = buildJobName(build_config)
Don Garrett73148e52018-08-17 16:54:46 -070080 if 'schedule_branch' in build_config:
81 branch = build_config.schedule_branch
Don Garrett73148e52018-08-17 16:54:46 -070082 else:
83 branch = 'master'
Don Garrett73148e52018-08-17 16:54:46 -070084
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 Yanagisawa16285b42018-09-19 14:00:58 +090090 'cbb_goma_client_type': build_config.goma_client_type,
Don Garrett73148e52018-08-17 16:54:46 -070091 }
92
93 # Filter out tags with no value set.
Mike Frysinger0bdbc102019-06-13 15:27:29 -040094 tags = {k: v for k, v in tags.items() if v}
Don Garrett73148e52018-08-17 16:54:46 -070095
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 Garrettbe3608b2018-06-05 17:10:09 -0700102 # TODO: Move --buildbot arg into recipe, and remove from here.
103 template = """
104job {
Don Garrett8dace272018-07-19 14:07:42 -0700105 id: "%(job_name)s"
Don Garrettbe3608b2018-06-05 17:10:09 -0700106 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 Garrett73148e52018-08-17 16:54:46 -0700112%(tag_lines)s
113%(prop_lines)s
Don Garrettbe3608b2018-06-05 17:10:09 -0700114 properties: "cbb_extra_args:[\\"--buildbot\\"]"
115 }
116}
117"""
Don Garrett8dace272018-07-19 14:07:42 -0700118
Don Garrettbe3608b2018-06-05 17:10:09 -0700119 return template % {
Don Garrett8dace272018-07-19 14:07:42 -0700120 'job_name': job_name,
Don Garrettbe3608b2018-06-05 17:10:09 -0700121 'builder': build_config.luci_builder,
Don Garrettbe3608b2018-06-05 17:10:09 -0700122 'schedule': build_config.schedule,
Don Garrett73148e52018-08-17 16:54:46 -0700123 'tag_lines': '\n'.join(tag_lines),
124 'prop_lines': '\n'.join(prop_lines),
Don Garrettbe3608b2018-06-05 17:10:09 -0700125 }
126
127
David Burgercba0d272020-06-24 16:21:26 -0600128def genSchedulerTrigger(trigger_name, repo, refs, path_regexps, builds):
Don Garrettbe3608b2018-06-05 17:10:09 -0700129 """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.
David Burgercba0d272020-06-24 16:21:26 -0600135 path_regexps: Iterable of path regular expressions of files to trigger on
136 or falsy to trigger on everything.
Don Garrettbe3608b2018-06-05 17:10:09 -0700137 builds: Iterable of build config names to trigger.
138
139 Returns:
140 Multiline string to include in the luci scheduler configuration.
141 """
142 template = """
143trigger {
144 id: "%(trigger_name)s"
145 acl_sets: "default"
146 schedule: "with 5m interval"
147 gitiles: {
148 repo: "%(repo)s"
David Burgercba0d272020-06-24 16:21:26 -0600149%(refs)s%(path_regexps)s
Don Garrettbe3608b2018-06-05 17:10:09 -0700150 }
151%(triggers)s
152}
153"""
David Burgercba0d272020-06-24 16:21:26 -0600154 if path_regexps:
155 path_regexps = '\n' + '\n'.join(' path_regexps: "%s"' %
156 r for r in path_regexps)
157 else:
158 path_regexps = ''
Don Garrettbe3608b2018-06-05 17:10:09 -0700159 return template % {
160 'trigger_name': trigger_name,
161 'repo': repo,
162 'refs': '\n'.join(' refs: "%s"' % r for r in refs),
David Burgercba0d272020-06-24 16:21:26 -0600163 'path_regexps': path_regexps,
Don Garrettbe3608b2018-06-05 17:10:09 -0700164 'triggers': '\n'.join(' triggers: "%s"' % b for b in builds),
165 }
166
167
Don Garrett8dace272018-07-19 14:07:42 -0700168def genLuciSchedulerConfig(site_config, branch_config):
Don Garrettbe3608b2018-06-05 17:10:09 -0700169 """Generate a luciSchedulerConfig as a string.
170
171 Args:
172 site_config: A config_lib.SiteConfig instance.
Don Garrett8dace272018-07-19 14:07:42 -0700173 branch_config: A list of BuildConfig instances to schedule.
Don Garrettbe3608b2018-06-05 17:10:09 -0700174
175 Returns:
176 The complete scheduler configuration contents as a string.
177 """
178 # Trigger collection is used to collect together trigger information, so
179 # we can reuse the same trigger for multiple builds as needed.
180 # It maps gitiles_key to a set of build_names.
181 # A gitiles_key = (gitiles_url, tuple(ref_list))
182 trigger_collection = {}
183
184 jobs = []
Don Garrettbe3608b2018-06-05 17:10:09 -0700185
Don Garrett8dace272018-07-19 14:07:42 -0700186 # Order the configs consistently.
187 configs = [site_config[name] for name in sorted(site_config)] + branch_config
188
189 for config in configs:
Don Garrettbe3608b2018-06-05 17:10:09 -0700190 # Populate jobs.
191 if config.schedule:
192 jobs.append(genSchedulerJob(config))
193
194 # Populate trigger_collection.
195 if config.triggered_gitiles:
David Burgercba0d272020-06-24 16:21:26 -0600196 for trigger in config.triggered_gitiles:
197 try:
198 gitiles_url, ref_list, path_regexps = trigger
199 except ValueError:
200 gitiles_url, ref_list = trigger
201 path_regexps = []
202 gitiles_key = (gitiles_url, tuple(ref_list), tuple(path_regexps))
Don Garrettbe3608b2018-06-05 17:10:09 -0700203 trigger_collection.setdefault(gitiles_key, set())
Don Garrett9a0d90c2018-10-30 12:47:14 -0700204 trigger_collection[gitiles_key].add(buildJobName(config))
Don Garrettbe3608b2018-06-05 17:10:09 -0700205
206 # Populate triggers.
207 triggers = []
208 trigger_counter = 0
209 for gitiles_key in sorted(trigger_collection):
210 builds = sorted(trigger_collection[gitiles_key])
211
212 trigger_name = 'trigger_%s' % trigger_counter
David Burgercba0d272020-06-24 16:21:26 -0600213 gitiles_url, refs, path_regexps = gitiles_key
Don Garrettbe3608b2018-06-05 17:10:09 -0700214 triggers.append(genSchedulerTrigger(
David Burgercba0d272020-06-24 16:21:26 -0600215 trigger_name, gitiles_url, refs, path_regexps, builds))
Don Garrettbe3608b2018-06-05 17:10:09 -0700216 trigger_counter += 1
217
218 return ''.join([_CONFIG_HEADER] + triggers + jobs)
219
220
221def GetParser():
222 """Creates the argparse parser."""
223 parser = commandline.ArgumentParser(description=__doc__)
224
225 parser.add_argument('-o', '--file_out', type='path',
226 help='Write output to specified file.')
227
228 return parser
229
230
231def main(argv):
232 parser = GetParser()
233 options = parser.parse_args(argv)
234 options.Freeze()
235
236 site_config = config_lib.GetConfig()
Don Garrett8dace272018-07-19 14:07:42 -0700237 branch_config = chromeos_config.BranchScheduleConfig()
Don Garrettbe3608b2018-06-05 17:10:09 -0700238
239 with (open(options.file_out, 'w') if options.file_out else sys.stdout) as fh:
Don Garrett8dace272018-07-19 14:07:42 -0700240 fh.write(genLuciSchedulerConfig(site_config, branch_config))