James E. Blair | 8a3729d | 2017-01-26 09:55:33 -0800 | [diff] [blame] | 1 | #!/usr/bin/env python |
| 2 | |
| 3 | # Licensed under the Apache License, Version 2.0 (the "License"); you may |
| 4 | # not use this file except in compliance with the License. You may obtain |
| 5 | # a copy of the License at |
| 6 | # |
| 7 | # http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | # |
| 9 | # Unless required by applicable law or agreed to in writing, software |
| 10 | # distributed under the License is distributed on an "AS IS" BASIS, WITHOUT |
| 11 | # WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the |
| 12 | # License for the specific language governing permissions and limitations |
| 13 | # under the License. |
| 14 | |
| 15 | import os |
| 16 | |
Clint Byrum | 88fdfde | 2017-03-28 16:22:42 -0700 | [diff] [blame] | 17 | from zuul.lib import yamlutil as yaml |
James E. Blair | 8a3729d | 2017-01-26 09:55:33 -0800 | [diff] [blame] | 18 | |
| 19 | FIXTURE_DIR = os.path.join(os.path.dirname(__file__), |
| 20 | 'fixtures') |
| 21 | CONFIG_DIR = os.path.join(FIXTURE_DIR, 'config') |
| 22 | |
| 23 | |
| 24 | def make_playbook(path): |
| 25 | d = os.path.dirname(path) |
| 26 | try: |
| 27 | os.makedirs(d) |
| 28 | except OSError: |
| 29 | pass |
| 30 | with open(path, 'w') as f: |
| 31 | f.write('- hosts: all\n') |
| 32 | f.write(' tasks: []\n') |
| 33 | |
| 34 | |
| 35 | def handle_repo(path): |
| 36 | print('Repo: %s' % path) |
| 37 | config_path = None |
| 38 | for fn in ['zuul.yaml', '.zuul.yaml']: |
| 39 | if os.path.exists(os.path.join(path, fn)): |
| 40 | config_path = os.path.join(path, fn) |
| 41 | break |
James E. Blair | b9c0d77 | 2017-03-03 14:34:49 -0800 | [diff] [blame] | 42 | try: |
| 43 | config = yaml.safe_load(open(config_path)) |
| 44 | except Exception: |
| 45 | print(" Has yaml errors") |
| 46 | return |
James E. Blair | 8a3729d | 2017-01-26 09:55:33 -0800 | [diff] [blame] | 47 | for block in config: |
| 48 | if 'job' not in block: |
| 49 | continue |
| 50 | job = block['job']['name'] |
| 51 | playbook = os.path.join(path, 'playbooks', job + '.yaml') |
| 52 | if not os.path.exists(playbook): |
| 53 | print(' Creating: %s' % job) |
| 54 | make_playbook(playbook) |
| 55 | |
| 56 | |
| 57 | def main(): |
| 58 | repo_dirs = [] |
| 59 | |
| 60 | for root, dirs, files in os.walk(CONFIG_DIR): |
| 61 | if 'zuul.yaml' in files or '.zuul.yaml' in files: |
| 62 | repo_dirs.append(root) |
| 63 | |
| 64 | for path in repo_dirs: |
| 65 | handle_repo(path) |
| 66 | |
| 67 | |
| 68 | if __name__ == '__main__': |
| 69 | main() |