This repository was archived by the owner on Feb 29, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 13
/
Copy pathcheck-layout.py
executable file
·300 lines (236 loc) · 9.32 KB
/
check-layout.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
#!/usr/bin/env python
# Copyright 2014 OpenStack Foundation
# Copyright 2014 SUSE Linux Products GmbH
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
import argparse
import ConfigParser
import copy
import os
import re
import sys
import yaml
# These pipelines are required to have at least one job per project
REQUIRED_PIPELINES = [
'check_github',
'gate_github',
]
layout_yaml = 'roles/zuul-server/templates/etc/zuul/config/layout.yaml'
layout = yaml.safe_load(open(layout_yaml))
gerrit_yaml = 'gerrit/projects.yaml'
#gerrit_config = yaml.safe_load(open(gerrit_yaml))
gerrit_acl_pattern = 'gerrit/acls/%s.config'
def check_merge_template():
"""Check that each job has a merge-check template."""
errors = False
print("\nChecking for usage of merge template")
print("====================================")
for project in layout['projects']:
if project['name'] == 'z/tempest':
continue
try:
correct = False
for template in project['template']:
if template['name'] == 'merge-check':
correct = True
if not correct:
raise
except:
print("Project %s has no merge-check template" % project['name'])
errors = True
return errors
def normalize(s):
"Normalize string for comparison."
return s.lower().replace("_", "-")
def check_projects_sorted():
"""Check that the projects are in alphabetical order per section."""
print("Checking project list for alphabetical order")
print("============================================")
# Note that the file has different sections and we need to sort
# entries within these sections.
errors = False
# Skip all entries before the project list
firstEntry = True
last = ""
for line in open(layout_yaml, 'r'):
if line.startswith('projects:'):
last = ""
firstEntry = False
if line.startswith(' - name: ') and not firstEntry:
current = line[10:].strip()
if (normalize(last) > normalize(current)):
print(" Wrong alphabetical order: %(last)s, %(current)s" %
{"last": last, "current": current})
errors = True
last = current
return errors
def check_formatting():
errors = False
count = 1
print("Checking indents")
print("================")
for line in open(layout_yaml, 'r'):
if (len(line) - len(line.lstrip(' '))) % 2 != 0:
print("Line %(count)s not indented by multiple of 2:\n\t%(line)s" %
{"count": count, "line": line})
errors = True
count = count + 1
return errors
def grep(source, pattern):
"""Run regex PATTERN over each line in SOURCE and return
True if any match found"""
found = False
p = re.compile(pattern)
for line in source:
if p.match(line):
found = True
break
return found
def check_jobs(joblistfile):
"""Check that jobs have matches"""
errors = False
print("\nChecking job section regex expressions")
print("======================================")
# The job-list.txt file is created by tests/test-zuul-jobs.sh
if not os.path.isfile(joblistfile):
print("Job list file %s does not exist, not checking jobs section"
% joblistfile)
return False
with open(joblistfile, 'r') as f:
job_list = [line.rstrip() for line in f]
for jobs in layout['jobs']:
found = grep(job_list, jobs['name'])
if not found:
print ("Regex %s has no matches in job list" % jobs['name'])
errors = True
if len(jobs.keys()) == 1:
print ("Job %s has no attributes in job list" % jobs['name'])
errors = True
for project in layout.get('projects', []):
for pipeline in layout.get('pipelines', []):
jobs = collect_pipeline_jobs(project, pipeline['name']) or []
missing = [j for j in jobs if j not in job_list and j != 'noop']
if missing:
print (
'ERROR: Zuul project %s references non-existent job(s) in'
' pipeline %s: %s' % (project['name'], pipeline['name'],
', '.join(missing))
)
errors = True
return errors
def collect_pipeline_jobs(project, pipeline):
jobs = []
if pipeline in project:
# We need to copy the object here to prevent the loop
# below from modifying the project object.
jobs = copy.deepcopy(project[pipeline])
if 'template' in project:
for template in project['template']:
# The template dict has a key for each pipeline and a key for the
# name. We want to filter on the name to make sure it matches the
# one listed in the project's template list, then collect the
# specific pipeline we are currently looking at.
t_jobs = [ _template[pipeline]
for _template in layout['project-templates']
if _template['name'] == template['name']
and pipeline in _template ]
if t_jobs:
jobs.append(t_jobs)
return jobs
def check_empty_pipeline(pipeline_name):
'''Check that each project has at least one job in specified pipeline'''
print("\nChecking for projects with no %s jobs" % pipeline_name)
print("====================================")
for project in layout['projects']:
pipeline_jobs = collect_pipeline_jobs(project, pipeline_name)
if not pipeline_jobs:
print(
"Project %s has no %s jobs" % (project['name'], pipeline_name)
)
return True
return False
def check_empty_pipelines():
return True in [check_empty_pipeline(p) for p in REQUIRED_PIPELINES]
def check_mixed_noops():
'''Check that no project is mixing both noop and non-noop jobs'''
print("\nChecking for mixed noop and non-noop jobs")
print("====================================")
for project in layout['projects']:
for pipeline in ['gate', 'check']:
jobs = collect_pipeline_jobs(project, pipeline)
if 'noop' in jobs and len(jobs) > 1:
print("Project %s has both noop and non-noop jobs "
"in '%s' pipeline" % (project['name'], pipeline))
return True
return False
def check_gerrit_zuul_projects():
'''Check that gerrit projects have zuul pipelines and vice versa'''
errors = False
zuul_projects = [ x['name'] for x in layout['projects'] ]
gerrit_projects = [ x['project'] for x in gerrit_config ]
print("\nChecking for gerrit projects with no zuul pipelines")
print("===================================================")
for gp in gerrit_projects:
# Check the gerrit config for a different acl file
acls = [ x['acl-config'] if 'acl-config' in x else None \
for x in gerrit_config if x['project'] == gp ]
if len(acls) != 1:
errors = True
print("Duplicate acl config for %s" % gp)
break
acl_config = acls.pop()
if acl_config is None:
acl_file = gerrit_acl_pattern % gp
else:
acl_file = acl_config.replace('/home/gerrit2/acls', 'gerrit/acls')
config = ConfigParser.ConfigParser()
config.read(acl_file)
try:
ignore = config.get('project', 'state') == 'read only'
if ignore:
continue # Skip inactive projects
except ConfigParser.NoSectionError:
pass
if gp not in zuul_projects:
print("Project %s is not in %s" % (gp, layout_yaml))
errors = True
print("\nChecking for zuul pipelines with no gerrit project")
print("===================================================")
for zp in zuul_projects:
if zp == 'z/tempest':
continue # Ignore z/tempest
if zp not in gerrit_projects:
print("Project %s is not in %s" % (zp, gerrit_yaml))
errors = True
return errors
def check_all():
parser = argparse.ArgumentParser()
parser.add_argument(
'joblistfile',
help='Path to job-list.txt file',
)
args = parser.parse_args()
errors = check_projects_sorted()
#errors = check_merge_template() or errors
errors = check_formatting() or errors
errors = check_empty_pipelines() or errors
#errors = check_mixed_noops() or errors
#errors = check_gerrit_zuul_projects() or errors
errors = check_jobs(args.joblistfile) or errors
if errors:
print("\nFound errors in layout.yaml!")
else:
print("\nNo errors found in layout.yaml!")
return errors
if __name__ == "__main__":
sys.exit(check_all())