blob: 9a9f75b4adbd23010ed11c3c7355118c48b7dbef [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
128def genSchedulerTrigger(trigger_name, repo, refs, builds):
129 """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.
135 builds: Iterable of build config names to trigger.
136
137 Returns:
138 Multiline string to include in the luci scheduler configuration.
139 """
140 template = """
141trigger {
142 id: "%(trigger_name)s"
143 acl_sets: "default"
144 schedule: "with 5m interval"
145 gitiles: {
146 repo: "%(repo)s"
147%(refs)s
148 }
149%(triggers)s
150}
151"""
152
153 return template % {
154 'trigger_name': trigger_name,
155 'repo': repo,
156 'refs': '\n'.join(' refs: "%s"' % r for r in refs),
157 'triggers': '\n'.join(' triggers: "%s"' % b for b in builds),
158 }
159
160
Don Garrett8dace272018-07-19 14:07:42 -0700161def genLuciSchedulerConfig(site_config, branch_config):
Don Garrettbe3608b2018-06-05 17:10:09 -0700162 """Generate a luciSchedulerConfig as a string.
163
164 Args:
165 site_config: A config_lib.SiteConfig instance.
Don Garrett8dace272018-07-19 14:07:42 -0700166 branch_config: A list of BuildConfig instances to schedule.
Don Garrettbe3608b2018-06-05 17:10:09 -0700167
168 Returns:
169 The complete scheduler configuration contents as a string.
170 """
171 # Trigger collection is used to collect together trigger information, so
172 # we can reuse the same trigger for multiple builds as needed.
173 # It maps gitiles_key to a set of build_names.
174 # A gitiles_key = (gitiles_url, tuple(ref_list))
175 trigger_collection = {}
176
177 jobs = []
Don Garrettbe3608b2018-06-05 17:10:09 -0700178
Don Garrett8dace272018-07-19 14:07:42 -0700179 # Order the configs consistently.
180 configs = [site_config[name] for name in sorted(site_config)] + branch_config
181
182 for config in configs:
Don Garrettbe3608b2018-06-05 17:10:09 -0700183 # Populate jobs.
184 if config.schedule:
185 jobs.append(genSchedulerJob(config))
186
187 # Populate trigger_collection.
188 if config.triggered_gitiles:
189 for gitiles_url, ref_list in config.triggered_gitiles:
190 gitiles_key = (gitiles_url, tuple(ref_list))
191 trigger_collection.setdefault(gitiles_key, set())
Don Garrett9a0d90c2018-10-30 12:47:14 -0700192 trigger_collection[gitiles_key].add(buildJobName(config))
Don Garrettbe3608b2018-06-05 17:10:09 -0700193
194 # Populate triggers.
195 triggers = []
196 trigger_counter = 0
197 for gitiles_key in sorted(trigger_collection):
198 builds = sorted(trigger_collection[gitiles_key])
199
200 trigger_name = 'trigger_%s' % trigger_counter
201 gitiles_url, refs = gitiles_key
202 triggers.append(genSchedulerTrigger(
203 trigger_name, gitiles_url, refs, builds))
204 trigger_counter += 1
205
206 return ''.join([_CONFIG_HEADER] + triggers + jobs)
207
208
209def GetParser():
210 """Creates the argparse parser."""
211 parser = commandline.ArgumentParser(description=__doc__)
212
213 parser.add_argument('-o', '--file_out', type='path',
214 help='Write output to specified file.')
215
216 return parser
217
218
219def main(argv):
220 parser = GetParser()
221 options = parser.parse_args(argv)
222 options.Freeze()
223
224 site_config = config_lib.GetConfig()
Don Garrett8dace272018-07-19 14:07:42 -0700225 branch_config = chromeos_config.BranchScheduleConfig()
Don Garrettbe3608b2018-06-05 17:10:09 -0700226
227 with (open(options.file_out, 'w') if options.file_out else sys.stdout) as fh:
Don Garrett8dace272018-07-19 14:07:42 -0700228 fh.write(genLuciSchedulerConfig(site_config, branch_config))