blob: f2d26661c473af4ae36175f733a5e8e09af1777f [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
33_CONFIG_HEADER = """# Defines buckets on luci-scheduler.appspot.com.
34#
35# For schema of this file and documentation see ProjectConfig message in
36# https://github.com/luci/luci-go/blob/master/scheduler/appengine/messages/config.proto
37
38# Generated with chromite/scripts/gen_luci_scheduler
39
Don Garrettff45f4b2018-10-04 09:08:01 -070040# Autodeployed with:
41# http://cros-goldeneye/chromeos/legoland/builderHistory?buildConfig=luci-scheduler-updater
42
Don Garrettbe3608b2018-06-05 17:10:09 -070043acl_sets {
44 name: "default"
45 acls {
46 role: READER
47 granted_to: "group:googlers"
48 }
49 acls {
50 role: OWNER
51 granted_to: "group:project-chromeos-admins"
52 }
Jason D. Clintona9e374a2018-10-18 18:41:00 -060053 acls {
54 role: TRIGGERER
55 granted_to: "group:mdb/chromeos-build-access"
56 }
Don Garrettbe3608b2018-06-05 17:10:09 -070057}
58"""
59
60
61def genSchedulerJob(build_config):
62 """Generate the luci scheduler job for a given build config.
63
64 Args:
65 build_config: config_lib.BuildConfig.
66
67 Returns:
68 Multiline string to include in the luci scheduler configuration.
69 """
Don Garrett73148e52018-08-17 16:54:46 -070070 if 'schedule_branch' in build_config:
71 branch = build_config.schedule_branch
72 job_name = '%s-%s' % (branch, build_config.name)
73 else:
74 branch = 'master'
75 job_name = build_config.name
76
77 tags = {
78 'cbb_branch': branch,
79 'cbb_config': build_config.name,
80 'cbb_display_label': build_config.display_label,
81 'cbb_workspace_branch': build_config.workspace_branch,
Yoshisato Yanagisawa16285b42018-09-19 14:00:58 +090082 'cbb_goma_client_type': build_config.goma_client_type,
Don Garrett73148e52018-08-17 16:54:46 -070083 }
84
85 # Filter out tags with no value set.
86 tags = {k: v for k, v in tags.iteritems() if v}
87
88
89 tag_lines = [' tags: "%s:%s"' % (k, tags[k])
90 for k in sorted(tags.keys())]
91 prop_lines = [' properties: "%s:%s"' % (k, tags[k])
92 for k in sorted(tags.keys())]
93
Don Garrettbe3608b2018-06-05 17:10:09 -070094 # TODO: Move --buildbot arg into recipe, and remove from here.
95 template = """
96job {
Don Garrett8dace272018-07-19 14:07:42 -070097 id: "%(job_name)s"
Don Garrettbe3608b2018-06-05 17:10:09 -070098 acl_sets: "default"
99 schedule: "%(schedule)s"
100 buildbucket: {
101 server: "cr-buildbucket.appspot.com"
102 bucket: "luci.chromeos.general"
103 builder: "%(builder)s"
Don Garrett73148e52018-08-17 16:54:46 -0700104%(tag_lines)s
105%(prop_lines)s
Don Garrettbe3608b2018-06-05 17:10:09 -0700106 properties: "cbb_extra_args:[\\"--buildbot\\"]"
107 }
108}
109"""
Don Garrett8dace272018-07-19 14:07:42 -0700110
Don Garrettbe3608b2018-06-05 17:10:09 -0700111 return template % {
Don Garrett8dace272018-07-19 14:07:42 -0700112 'job_name': job_name,
Don Garrettbe3608b2018-06-05 17:10:09 -0700113 'builder': build_config.luci_builder,
Don Garrettbe3608b2018-06-05 17:10:09 -0700114 'schedule': build_config.schedule,
Don Garrett73148e52018-08-17 16:54:46 -0700115 'tag_lines': '\n'.join(tag_lines),
116 'prop_lines': '\n'.join(prop_lines),
Don Garrettbe3608b2018-06-05 17:10:09 -0700117 }
118
119
120def genSchedulerTrigger(trigger_name, repo, refs, builds):
121 """Generate the luci scheduler job for a given build config.
122
123 Args:
124 trigger_name: Name of the trigger as a string.
125 repo: Gitiles URL git git repository.
126 refs: Iterable of git refs to check. May use regular expressions.
127 builds: Iterable of build config names to trigger.
128
129 Returns:
130 Multiline string to include in the luci scheduler configuration.
131 """
132 template = """
133trigger {
134 id: "%(trigger_name)s"
135 acl_sets: "default"
136 schedule: "with 5m interval"
137 gitiles: {
138 repo: "%(repo)s"
139%(refs)s
140 }
141%(triggers)s
142}
143"""
144
145 return template % {
146 'trigger_name': trigger_name,
147 'repo': repo,
148 'refs': '\n'.join(' refs: "%s"' % r for r in refs),
149 'triggers': '\n'.join(' triggers: "%s"' % b for b in builds),
150 }
151
152
Don Garrett8dace272018-07-19 14:07:42 -0700153def genLuciSchedulerConfig(site_config, branch_config):
Don Garrettbe3608b2018-06-05 17:10:09 -0700154 """Generate a luciSchedulerConfig as a string.
155
156 Args:
157 site_config: A config_lib.SiteConfig instance.
Don Garrett8dace272018-07-19 14:07:42 -0700158 branch_config: A list of BuildConfig instances to schedule.
Don Garrettbe3608b2018-06-05 17:10:09 -0700159
160 Returns:
161 The complete scheduler configuration contents as a string.
162 """
163 # Trigger collection is used to collect together trigger information, so
164 # we can reuse the same trigger for multiple builds as needed.
165 # It maps gitiles_key to a set of build_names.
166 # A gitiles_key = (gitiles_url, tuple(ref_list))
167 trigger_collection = {}
168
169 jobs = []
Don Garrettbe3608b2018-06-05 17:10:09 -0700170
Don Garrett8dace272018-07-19 14:07:42 -0700171 # Order the configs consistently.
172 configs = [site_config[name] for name in sorted(site_config)] + branch_config
173
174 for config in configs:
Don Garrettbe3608b2018-06-05 17:10:09 -0700175 # Populate jobs.
176 if config.schedule:
177 jobs.append(genSchedulerJob(config))
178
179 # Populate trigger_collection.
180 if config.triggered_gitiles:
181 for gitiles_url, ref_list in config.triggered_gitiles:
182 gitiles_key = (gitiles_url, tuple(ref_list))
183 trigger_collection.setdefault(gitiles_key, set())
184 trigger_collection[gitiles_key].add(config.name)
185
186 # Populate triggers.
187 triggers = []
188 trigger_counter = 0
189 for gitiles_key in sorted(trigger_collection):
190 builds = sorted(trigger_collection[gitiles_key])
191
192 trigger_name = 'trigger_%s' % trigger_counter
193 gitiles_url, refs = gitiles_key
194 triggers.append(genSchedulerTrigger(
195 trigger_name, gitiles_url, refs, builds))
196 trigger_counter += 1
197
198 return ''.join([_CONFIG_HEADER] + triggers + jobs)
199
200
201def GetParser():
202 """Creates the argparse parser."""
203 parser = commandline.ArgumentParser(description=__doc__)
204
205 parser.add_argument('-o', '--file_out', type='path',
206 help='Write output to specified file.')
207
208 return parser
209
210
211def main(argv):
212 parser = GetParser()
213 options = parser.parse_args(argv)
214 options.Freeze()
215
216 site_config = config_lib.GetConfig()
Don Garrett8dace272018-07-19 14:07:42 -0700217 branch_config = chromeos_config.BranchScheduleConfig()
Don Garrettbe3608b2018-06-05 17:10:09 -0700218
219 with (open(options.file_out, 'w') if options.file_out else sys.stdout) as fh:
Don Garrett8dace272018-07-19 14:07:42 -0700220 fh.write(genLuciSchedulerConfig(site_config, branch_config))