generate_templates.py 3.95 KB
Newer Older
1 2 3
#!/bin/env python3

import yaml
4
import re
5 6
import sys
import tempfile
7 8 9

from pathlib import Path

10 11
sys.path.insert(0, './tools')
import ci_fairy  # noqa
12

13

14 15 16
def generate_template(configs, dest, template):
    '''
    Invoke ci-fairy generate-template with the given arguments.
17

18 19
    .. param:: configs
        An array of tuples of type (path, rootnode).
20

21 22 23 24 25 26
    '''
    config_args = []
    for config, root in configs:
        config_args += ['--config', f'{config}']
        if root is not None:
            config_args += ['--root', f'{root}']
27

28 29
    args = ['generate-template'] + config_args + ['--output-file', f'{dest}', f'{template}']
    ci_fairy.main(args, standalone_mode=False)
30

31

32 33
if __name__ == '__main__':
    src_folder = Path('src')
34
    config_folder = src_folder / 'config'
35 36
    ci_folder = Path('.gitlab-ci')
    ci_folder.mkdir(exist_ok=True)
37
    globals_file = config_folder / 'ci-globals.yml'
38

39 40 41 42
    def distribution_files():
        for f in config_folder.iterdir():
            if f.name.endswith('.yml') and not f.name.startswith('ci-'):
                yield f
43

44
    for distrib in distribution_files():
45 46 47 48
        name = distrib.name[:-4]  # drop .yml

        # generate the distribution's template file,
        # e.g. /templates/alpine.yml
49
        template = src_folder / 'distribution-template.tmpl'
50 51 52 53 54
        dest = Path('templates') / f'{name}.yml'
        config = [(f'{globals_file}', ''), (f'{distrib}', f'{name}')]
        generate_template(configs=config, dest=dest, template=template)

        # generate our CI file, e.g. /.gitlab-ci/alpine-ci.yml
55
        template = src_folder / 'distribution-template-ci.tmpl'
56 57 58 59 60 61 62 63
        dest = ci_folder / f'{name}-ci.yml'
        generate_template(configs=config, dest=dest, template=template)

    # Generate the bootstrap-ci file
    dest = ci_folder / 'bootstrap-ci.yml'
    template = src_folder / 'bootstrap.tmpl'
    config = [(f'{globals_file}', None)]
    generate_template(configs=config, dest=dest, template=template)
64

Peter Hutterer's avatar
Peter Hutterer committed
65
    # Generate the ci-fairy template and its ci file
66
    ci_fairy_file = config_folder / 'ci-fairy.yml'
Peter Hutterer's avatar
Peter Hutterer committed
67 68 69 70 71 72 73 74 75
    template = src_folder / 'ci-fairy.tmpl'
    dest = Path('templates') / 'ci-fairy.yml'
    config = [(f'{globals_file}', ''), (f'{ci_fairy_file}', 'ci-fairy')]
    generate_template(configs=config, dest=dest, template=template)

    template = src_folder / 'ci-fairy-ci.tmpl'
    dest = ci_folder / 'ci-fairy-ci.yml'
    generate_template(configs=config, dest=dest, template=template)

76 77 78 79
    # We've generated all the templates, search for any
    # full image reference $registry/$path:$imagespec anywhere in your
    # templates and add that to the remote_images so we can test those
    # during the CI pipeline
80 81
    with open(globals_file) as fd:
        config = yaml.load(fd, Loader=yaml.Loader)
82 83 84 85 86 87 88 89 90
    registry, path = config['ci_templates_registry'], config['ci_templates_registry_path']
    pattern = re.compile(f'^[^#]+({registry}{path}:[\\w:.-]+)')
    remote_images = []
    for template in Path('templates/').glob('*.yml'):
        for line in open(template):
            matches = pattern.match(line)
            if matches:
                remote_images.append(matches[1])

91 92 93 94 95 96
    # write out a temporary config file with the remote images and the
    # distribution names. Use that to generate our .gitlab-ci.yml file
    with tempfile.NamedTemporaryFile() as new_config:
        s = '", "'.join(sorted(set(remote_images)))
        new_config.write(f'remote_images: ["{s}"]\n'.encode('utf8'))

97
        distributions = [d.name[:-4] for d in distribution_files()]
98 99 100 101 102 103 104
        s = '", "'.join(sorted(set(distributions)))
        new_config.write(f'distribs: ["{s}"]\n'.encode('utf8'))
        new_config.flush()

        # now generate the .gitlab-ci.yml file
        dest = Path('.gitlab-ci.yml')
        template = src_folder / 'gitlab-ci.tmpl'
Peter Hutterer's avatar
Peter Hutterer committed
105 106 107
        config = [(f'{globals_file}', ''),
                  (f'{ci_fairy_file}', 'ci-fairy'),
                  (f'{new_config.name}', '')]
108
        generate_template(configs=config, dest=dest, template=template)