Last active
March 24, 2020 19:47
-
-
Save baby-gnu/2fa3745e874444eda5de775d6269023d to your computer and use it in GitHub Desktop.
SaltStack formula: converte YAML files to `parameters/` directories
This file contains hidden or 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/python3 | |
import glob | |
import os | |
import re | |
import sys | |
from string import Template | |
from ruamel.yaml import YAML | |
if not os.path.isfile('defaults.yaml'): | |
raise Exception('You must run this script in the same directory as defaults.yaml') | |
yaml = YAML(typ='rt') | |
yaml.indent(offset=2) | |
yaml.preserve_quotes = True | |
yaml.default_flow_style = False | |
yaml.explicit_start = True | |
yaml.explicit_end = True | |
def load_yaml(filename): | |
with open(filename, 'r') as yaml_file: | |
yaml_str = yaml.load(yaml_file) | |
return yaml_str | |
if not os.path.isdir('parameters'): | |
os.makedirs('parameters') | |
yaml_files = glob.glob('os*.yaml') | |
config_name_map = {'osfamily': 'os_family'} | |
## defaults.yaml | |
print("Move 'defaults.yaml' to 'parameters/defaults.yaml'") | |
defaults = load_yaml('defaults.yaml') | |
defaults_keys = defaults.keys() | |
if len(defaults_keys) != 1: | |
raise Exception(f'Wrong number of top level keys in defaults.yaml, found: {", ".join(defaults.keys())}') | |
values_items = defaults.popitem() | |
values = {'values': values_items[1]} | |
defaults_header = """# -*- coding: utf-8 -*- | |
# vim: ft=yaml | |
""" | |
with open('parameters/defaults.yaml', 'w') as defaults_fh: | |
defaults_fh.write(defaults_header) | |
yaml.dump(values, defaults_fh) | |
## os*map.yaml | |
config_header = Template("""# -*- coding: utf-8 -*- | |
# vim: ft=yaml | |
# | |
# Setup variables specific to salt['config.get']('${config_name}') == ${config_value}. | |
# You just need to add the key:values for this `${config_name}` that differ | |
# from ${config_differ}. | |
# | |
# If you do not need to provide defaults via the `${config_name}` config, | |
# you can remove this file or provide at least an empty dict, e.g. | |
# values: {} | |
""") | |
config_differ_map = {'osarch': '`defaults.yaml`', | |
'os': '`defaults.yaml` + `<osarch>.yaml` + `<os_family>.yaml`', | |
'os_family': '`defaults.yaml` + `<osarch>.yaml`', | |
'os_finger': '`defaults.yaml` + `<osarch>.yaml` + `<os_family>.yaml` + `<osmap>.yaml`'} | |
for yaml_file in yaml_files: | |
config_name = yaml_file.replace('map.yaml', '') | |
# Some YAML files do not have the proper config name | |
config_name = config_name_map.get(config_name, config_name) | |
config_dir = os.path.join('parameters', config_name) | |
if not os.path.isdir(config_dir): | |
os.makedirs(config_dir) | |
print(f"Split '{yaml_file}' to 'parameters/{config_name}/'") | |
yaml_content = load_yaml(yaml_file) | |
for item in yaml_content: | |
if not yaml_content[item]: | |
print(f"-> skip empty file 'parameters/{config_name}/{item}.yaml'") | |
else: | |
print(f"-> write file 'parameters/{config_name}/{item}.yaml'") | |
with open(os.path.join(config_dir, f'{item}.yaml'), 'w') as config_fh: | |
config_differ = config_differ_map.get(config_name, '`defaults.yaml`') | |
config_fh.write(config_header.substitute({'config_name': config_name, | |
'config_value': item, | |
'config_differ': config_differ})) | |
values = {'values': yaml_content[item]} | |
yaml.dump(values, config_fh) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment