blob: 4eaa3d3915a6fddfe622cc8acb84230aa6171f49 [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
6"""Generate LUCI Scheduler config file.
7
8This generates the LUCI Scheduler configuration file for ChromeOS builds based
9on the chromeos_config contents.
10
11To update the production config:
12 cd $REPO/manifest-internal
13 git checkout infra/config
14 $REPO/chromite/scripts/gen_luci_scheduler > luci-scheduler.cfg
15 git commit -a
16 git cl upload (yes, really) (Say Yes to removing commit-msg hook)
17 repo abandon infra/config
18
Don Garrettc8323dc2018-08-22 15:58:26 -070019Notes:
20 Normal builds are scheduled based on the builder values for
21 'schedule' and 'triggered_gitiles' in config/chromeos_config.py.
22
23 Branched builds are scheduled based on the function
24 chromeos_config.BranchScheduleConfig()
Don Garrettbe3608b2018-06-05 17:10:09 -070025"""
26
27from __future__ import print_function
28
29import sys
30
Don Garrett8dace272018-07-19 14:07:42 -070031from chromite.config import chromeos_config
Don Garrettbe3608b2018-06-05 17:10:09 -070032from chromite.lib import commandline
33from chromite.lib import config_lib
34
35
36_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
43acl_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 }
53}
54"""
55
56
57def genSchedulerJob(build_config):
58 """Generate the luci scheduler job for a given build config.
59
60 Args:
61 build_config: config_lib.BuildConfig.
62
63 Returns:
64 Multiline string to include in the luci scheduler configuration.
65 """
Don Garrett73148e52018-08-17 16:54:46 -070066 if 'schedule_branch' in build_config:
67 branch = build_config.schedule_branch
68 job_name = '%s-%s' % (branch, build_config.name)
69 else:
70 branch = 'master'
71 job_name = build_config.name
72
73 tags = {
74 'cbb_branch': branch,
75 'cbb_config': build_config.name,
76 'cbb_display_label': build_config.display_label,
77 'cbb_workspace_branch': build_config.workspace_branch,
Yoshisato Yanagisawa16285b42018-09-19 14:00:58 +090078 'cbb_goma_client_type': build_config.goma_client_type,
Don Garrett73148e52018-08-17 16:54:46 -070079 }
80
81 # Filter out tags with no value set.
82 tags = {k: v for k, v in tags.iteritems() if v}
83
84
85 tag_lines = [' tags: "%s:%s"' % (k, tags[k])
86 for k in sorted(tags.keys())]
87 prop_lines = [' properties: "%s:%s"' % (k, tags[k])
88 for k in sorted(tags.keys())]
89
Don Garrettbe3608b2018-06-05 17:10:09 -070090 # TODO: Move --buildbot arg into recipe, and remove from here.
91 template = """
92job {
Don Garrett8dace272018-07-19 14:07:42 -070093 id: "%(job_name)s"
Don Garrettbe3608b2018-06-05 17:10:09 -070094 acl_sets: "default"
95 schedule: "%(schedule)s"
96 buildbucket: {
97 server: "cr-buildbucket.appspot.com"
98 bucket: "luci.chromeos.general"
99 builder: "%(builder)s"
Don Garrett73148e52018-08-17 16:54:46 -0700100%(tag_lines)s
101%(prop_lines)s
Don Garrettbe3608b2018-06-05 17:10:09 -0700102 properties: "cbb_extra_args:[\\"--buildbot\\"]"
103 }
104}
105"""
Don Garrett8dace272018-07-19 14:07:42 -0700106
Don Garrettbe3608b2018-06-05 17:10:09 -0700107 return template % {
Don Garrett8dace272018-07-19 14:07:42 -0700108 'job_name': job_name,
Don Garrettbe3608b2018-06-05 17:10:09 -0700109 'builder': build_config.luci_builder,
Don Garrettbe3608b2018-06-05 17:10:09 -0700110 'schedule': build_config.schedule,
Don Garrett73148e52018-08-17 16:54:46 -0700111 'tag_lines': '\n'.join(tag_lines),
112 'prop_lines': '\n'.join(prop_lines),
Don Garrettbe3608b2018-06-05 17:10:09 -0700113 }
114
115
116def genSchedulerTrigger(trigger_name, repo, refs, builds):
117 """Generate the luci scheduler job for a given build config.
118
119 Args:
120 trigger_name: Name of the trigger as a string.
121 repo: Gitiles URL git git repository.
122 refs: Iterable of git refs to check. May use regular expressions.
123 builds: Iterable of build config names to trigger.
124
125 Returns:
126 Multiline string to include in the luci scheduler configuration.
127 """
128 template = """
129trigger {
130 id: "%(trigger_name)s"
131 acl_sets: "default"
132 schedule: "with 5m interval"
133 gitiles: {
134 repo: "%(repo)s"
135%(refs)s
136 }
137%(triggers)s
138}
139"""
140
141 return template % {
142 'trigger_name': trigger_name,
143 'repo': repo,
144 'refs': '\n'.join(' refs: "%s"' % r for r in refs),
145 'triggers': '\n'.join(' triggers: "%s"' % b for b in builds),
146 }
147
148
Don Garrett8dace272018-07-19 14:07:42 -0700149def genLuciSchedulerConfig(site_config, branch_config):
Don Garrettbe3608b2018-06-05 17:10:09 -0700150 """Generate a luciSchedulerConfig as a string.
151
152 Args:
153 site_config: A config_lib.SiteConfig instance.
Don Garrett8dace272018-07-19 14:07:42 -0700154 branch_config: A list of BuildConfig instances to schedule.
Don Garrettbe3608b2018-06-05 17:10:09 -0700155
156 Returns:
157 The complete scheduler configuration contents as a string.
158 """
159 # Trigger collection is used to collect together trigger information, so
160 # we can reuse the same trigger for multiple builds as needed.
161 # It maps gitiles_key to a set of build_names.
162 # A gitiles_key = (gitiles_url, tuple(ref_list))
163 trigger_collection = {}
164
165 jobs = []
Don Garrettbe3608b2018-06-05 17:10:09 -0700166
Don Garrett8dace272018-07-19 14:07:42 -0700167 # Order the configs consistently.
168 configs = [site_config[name] for name in sorted(site_config)] + branch_config
169
170 for config in configs:
Don Garrettbe3608b2018-06-05 17:10:09 -0700171 # Populate jobs.
172 if config.schedule:
173 jobs.append(genSchedulerJob(config))
174
175 # Populate trigger_collection.
176 if config.triggered_gitiles:
177 for gitiles_url, ref_list in config.triggered_gitiles:
178 gitiles_key = (gitiles_url, tuple(ref_list))
179 trigger_collection.setdefault(gitiles_key, set())
180 trigger_collection[gitiles_key].add(config.name)
181
182 # Populate triggers.
183 triggers = []
184 trigger_counter = 0
185 for gitiles_key in sorted(trigger_collection):
186 builds = sorted(trigger_collection[gitiles_key])
187
188 trigger_name = 'trigger_%s' % trigger_counter
189 gitiles_url, refs = gitiles_key
190 triggers.append(genSchedulerTrigger(
191 trigger_name, gitiles_url, refs, builds))
192 trigger_counter += 1
193
194 return ''.join([_CONFIG_HEADER] + triggers + jobs)
195
196
197def GetParser():
198 """Creates the argparse parser."""
199 parser = commandline.ArgumentParser(description=__doc__)
200
201 parser.add_argument('-o', '--file_out', type='path',
202 help='Write output to specified file.')
203
204 return parser
205
206
207def main(argv):
208 parser = GetParser()
209 options = parser.parse_args(argv)
210 options.Freeze()
211
212 site_config = config_lib.GetConfig()
Don Garrett8dace272018-07-19 14:07:42 -0700213 branch_config = chromeos_config.BranchScheduleConfig()
Don Garrettbe3608b2018-06-05 17:10:09 -0700214
215 with (open(options.file_out, 'w') if options.file_out else sys.stdout) as fh:
Don Garrett8dace272018-07-19 14:07:42 -0700216 fh.write(genLuciSchedulerConfig(site_config, branch_config))