Created
July 5, 2016 23:00
-
-
Save mgagne/d3d7c4e0cec4d770b5f0b95baba5d4eb to your computer and use it in GitHub Desktop.
Check Zuul Layout
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python | |
from __future__ import print_function | |
import ConfigParser | |
import argparse | |
import os | |
import sys | |
import yaml | |
import yamlordereddictloader | |
import zuul.lib.connections | |
import zuul.scheduler | |
def list_names_from_zuul_layout_entries(entries): | |
return [p['name'] for p in entries] | |
def validate_alphabetical_order(data): | |
previous = '' | |
errors = [] | |
for current in data: | |
if previous > current: | |
error = (previous, current) | |
errors.append(error) | |
previous = current | |
return errors | |
def load_data(filename): | |
with open(filename, 'r') as fd: | |
return yaml.load(fd, Loader=yamlordereddictloader.Loader) or {} | |
def load_jenkins_jobs_from_file(filename): | |
jobs = set() | |
for line in open(filename): | |
v = line.strip() | |
if v: | |
jobs.add(v) | |
return jobs | |
def test_zuul_layout(zuul_config, zuul_layout, jenkins_jobs): | |
config = ConfigParser.ConfigParser() | |
config.read(os.path.expanduser(zuul_config)) | |
scheduler = zuul.scheduler.Scheduler(zuul_layout) | |
connections = zuul.lib.connections.configure_connections(config) | |
jobs = load_jenkins_jobs_from_file(jenkins_jobs) | |
jobs.add('noop') | |
layout = scheduler.testConfig(zuul_layout, connections) | |
errors = [] | |
for job in sorted(layout.jobs): | |
if job not in jobs: | |
errors.append(job) | |
return errors | |
def main(): | |
parser = argparse.ArgumentParser( | |
description='Zuul projects check') | |
parser.add_argument('zuul_config', | |
help='Path to zuul/zuul.conf') | |
parser.add_argument('zuul_layout', | |
help='Path to zuul/layout.yaml') | |
parser.add_argument('jenkins_jobs', | |
help='Path to file with list of Jenkins jobs') | |
args = parser.parse_args() | |
failed = False | |
errors = test_zuul_layout(args.zuul_config, args.zuul_layout, | |
args.jenkins_jobs) | |
if errors: | |
print('Zuul jobs not defined in Jenkins!') | |
for error in errors: | |
print(error) | |
failed = True | |
zuul_layout = load_data(args.zuul_layout) | |
fields = ['jobs', 'project-templates', 'projects'] | |
for field in fields: | |
entries = zuul_layout.get(field, []) | |
names = list_names_from_zuul_layout_entries(entries) | |
errors = validate_alphabetical_order(names) | |
if errors: | |
print("Zuul '{}' are in wrong alphabetical order!".format(field)) | |
for error in errors: | |
print(error) | |
failed = True | |
return int(failed) | |
if __name__ == '__main__': | |
sys.exit(main()) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment