blob: b685111c4bd5b04287c09efc670fc6a274d58bee [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,
78 }
79
80 # Filter out tags with no value set.
81 tags = {k: v for k, v in tags.iteritems() if v}
82
83
84 tag_lines = [' tags: "%s:%s"' % (k, tags[k])
85 for k in sorted(tags.keys())]
86 prop_lines = [' properties: "%s:%s"' % (k, tags[k])
87 for k in sorted(tags.keys())]
88
Don Garrettbe3608b2018-06-05 17:10:09 -070089 # TODO: Move --buildbot arg into recipe, and remove from here.
90 template = """
91job {
Don Garrett8dace272018-07-19 14:07:42 -070092 id: "%(job_name)s"
Don Garrettbe3608b2018-06-05 17:10:09 -070093 acl_sets: "default"
94 schedule: "%(schedule)s"
95 buildbucket: {
96 server: "cr-buildbucket.appspot.com"
97 bucket: "luci.chromeos.general"
98 builder: "%(builder)s"
Don Garrett73148e52018-08-17 16:54:46 -070099%(tag_lines)s
100%(prop_lines)s
Don Garrettbe3608b2018-06-05 17:10:09 -0700101 properties: "cbb_extra_args:[\\"--buildbot\\"]"
102 }
103}
104"""
Don Garrett8dace272018-07-19 14:07:42 -0700105
Don Garrettbe3608b2018-06-05 17:10:09 -0700106 return template % {
Don Garrett8dace272018-07-19 14:07:42 -0700107 'job_name': job_name,
Don Garrettbe3608b2018-06-05 17:10:09 -0700108 'builder': build_config.luci_builder,
Don Garrettbe3608b2018-06-05 17:10:09 -0700109 'schedule': build_config.schedule,
Don Garrett73148e52018-08-17 16:54:46 -0700110 'tag_lines': '\n'.join(tag_lines),
111 'prop_lines': '\n'.join(prop_lines),
Don Garrettbe3608b2018-06-05 17:10:09 -0700112 }
113
114
115def genSchedulerTrigger(trigger_name, repo, refs, builds):
116 """Generate the luci scheduler job for a given build config.
117
118 Args:
119 trigger_name: Name of the trigger as a string.
120 repo: Gitiles URL git git repository.
121 refs: Iterable of git refs to check. May use regular expressions.
122 builds: Iterable of build config names to trigger.
123
124 Returns:
125 Multiline string to include in the luci scheduler configuration.
126 """
127 template = """
128trigger {
129 id: "%(trigger_name)s"
130 acl_sets: "default"
131 schedule: "with 5m interval"
132 gitiles: {
133 repo: "%(repo)s"
134%(refs)s
135 }
136%(triggers)s
137}
138"""
139
140 return template % {
141 'trigger_name': trigger_name,
142 'repo': repo,
143 'refs': '\n'.join(' refs: "%s"' % r for r in refs),
144 'triggers': '\n'.join(' triggers: "%s"' % b for b in builds),
145 }
146
147
Don Garrett8dace272018-07-19 14:07:42 -0700148def genLuciSchedulerConfig(site_config, branch_config):
Don Garrettbe3608b2018-06-05 17:10:09 -0700149 """Generate a luciSchedulerConfig as a string.
150
151 Args:
152 site_config: A config_lib.SiteConfig instance.
Don Garrett8dace272018-07-19 14:07:42 -0700153 branch_config: A list of BuildConfig instances to schedule.
Don Garrettbe3608b2018-06-05 17:10:09 -0700154
155 Returns:
156 The complete scheduler configuration contents as a string.
157 """
158 # Trigger collection is used to collect together trigger information, so
159 # we can reuse the same trigger for multiple builds as needed.
160 # It maps gitiles_key to a set of build_names.
161 # A gitiles_key = (gitiles_url, tuple(ref_list))
162 trigger_collection = {}
163
164 jobs = []
Don Garrettbe3608b2018-06-05 17:10:09 -0700165
Don Garrett8dace272018-07-19 14:07:42 -0700166 # Order the configs consistently.
167 configs = [site_config[name] for name in sorted(site_config)] + branch_config
168
169 for config in configs:
Don Garrettbe3608b2018-06-05 17:10:09 -0700170 # Populate jobs.
171 if config.schedule:
172 jobs.append(genSchedulerJob(config))
173
174 # Populate trigger_collection.
175 if config.triggered_gitiles:
176 for gitiles_url, ref_list in config.triggered_gitiles:
177 gitiles_key = (gitiles_url, tuple(ref_list))
178 trigger_collection.setdefault(gitiles_key, set())
179 trigger_collection[gitiles_key].add(config.name)
180
181 # Populate triggers.
182 triggers = []
183 trigger_counter = 0
184 for gitiles_key in sorted(trigger_collection):
185 builds = sorted(trigger_collection[gitiles_key])
186
187 trigger_name = 'trigger_%s' % trigger_counter
188 gitiles_url, refs = gitiles_key
189 triggers.append(genSchedulerTrigger(
190 trigger_name, gitiles_url, refs, builds))
191 trigger_counter += 1
192
193 return ''.join([_CONFIG_HEADER] + triggers + jobs)
194
195
196def GetParser():
197 """Creates the argparse parser."""
198 parser = commandline.ArgumentParser(description=__doc__)
199
200 parser.add_argument('-o', '--file_out', type='path',
201 help='Write output to specified file.')
202
203 return parser
204
205
206def main(argv):
207 parser = GetParser()
208 options = parser.parse_args(argv)
209 options.Freeze()
210
211 site_config = config_lib.GetConfig()
Don Garrett8dace272018-07-19 14:07:42 -0700212 branch_config = chromeos_config.BranchScheduleConfig()
Don Garrettbe3608b2018-06-05 17:10:09 -0700213
214 with (open(options.file_out, 'w') if options.file_out else sys.stdout) as fh:
Don Garrett8dace272018-07-19 14:07:42 -0700215 fh.write(genLuciSchedulerConfig(site_config, branch_config))