This repository has been archived by the owner on Nov 8, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
cloud-config-creator.py
executable file
·81 lines (61 loc) · 2.44 KB
/
cloud-config-creator.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
#!/usr/bin/env python3
import os
import yaml
import jinja2
from argparse import ArgumentParser
# From http://stackoverflow.com/a/7205107
def merge(a, b, path=None):
"""merges b into a"""
if path is None:
path = []
for key in b:
if key in a:
if isinstance(a[key], dict) and isinstance(b[key], dict):
merge(a[key], b[key], path + [str(key)])
elif a[key] == b[key]:
pass # same leaf value
else:
raise Exception('Conflict at %s' % '.'.join(path + [str(key)]))
else:
a[key] = b[key]
return a
def parse_args():
parser = ArgumentParser()
parser.add_argument("-t", "--templatefile", help="Path to template file", metavar="TEMPLATEFILE", required=True)
parser.add_argument("-f", "--valuesfile", help="Path to values files", metavar="VALUEFILE", required=True)
parser.add_argument("-o", "--outpath", help="Directory for created files (default: ./)", metavar="OUTPATH",
required=False)
parser.add_argument("-i", "--includepath", help="Directory for includes", metavar="INCLUDEPATH", default="")
return parser.parse_args()
def parse_valuesfile(f):
f_content = yaml.load(f)
global_values = f_content.get('global', {})
nodes = f_content.get('nodes', {})
for node in nodes:
if 'override' in global_values:
merge(node, global_values['override'])
return nodes
def main():
args = parse_args()
if args.outpath:
outpath = args.outpath
else:
outpath = os.getcwd()
with open(args.valuesfile, 'r') as f:
nodes = parse_valuesfile(f)
templateLoader = jinja2.FileSystemLoader(searchpath=[os.getcwd(), os.path.dirname(args.templatefile)] +
[arg for arg in args.includepath.split(',')], followlinks=True)
templateEnv = jinja2.Environment(loader=templateLoader, undefined=jinja2.StrictUndefined)
for node in nodes:
remaining_nodes = list(nodes)
remaining_nodes.remove(node)
template = templateEnv.get_template(args.templatefile)
with open(outpath + "/" + node['hostname'] + '.yml', "w") as f:
f.write(template.render({
"my": node,
"remaining_nodes": remaining_nodes,
"nodes": nodes
}))
if __name__ == "__main__":
# execute only if run as a script
main()