Compare commits

...

3 Commits

6 changed files with 24 additions and 15 deletions

1
.gitattributes vendored
View File

@@ -1 +1,2 @@
_secrets.yaml filter=git-crypt diff=git-crypt
secrets.yaml filter=git-crypt diff=git-crypt

View File

@@ -1,3 +1,3 @@
machine:
network:
hostname: {{hostname}}
hostname: {{node.hostname}}

View File

@@ -1,3 +1,3 @@
machine:
install:
disk: {{installDisk}}
disk: {{node.installDisk}}

View File

@@ -1,10 +1,10 @@
machine:
network:
interfaces:
- interface: {{interface}}
- interface: {{node.interface}}
dhcp: false
addresses:
- {{ip}}
- {{node.ip}}
routes:
- network: 0.0.0.0/0
gateway: {{gateway}}
gateway: {{node.gateway}}

View File

@@ -1,6 +1,6 @@
machine:
network:
interfaces:
- interface: {{interface}}
- interface: {{node.interface}}
vip:
ip: {{cluster.controlPlaneIp}}
ip: {{node.cluster.controlPlaneIp}}

View File

@@ -35,12 +35,12 @@ TEMPLATES = Environment(
)
def render_templates(node: dict):
def render_templates(node: dict, args: dict):
class Inner(json.JSONEncoder):
def default(self, o):
if isinstance(o, Template):
try:
rendered = o.render(node)
rendered = o.render(args | {"node": node})
except Exception as e:
e.add_note(f"While rendering for: {node['hostname']}")
raise e
@@ -131,6 +131,14 @@ def walk_files(root: pathlib.Path):
def main():
with open(ROOT.joinpath("config.yaml")) as fyaml:
config = yaml.safe_load(fyaml)
with open(ROOT.joinpath("secrets.yaml")) as fyaml:
config |= yaml.safe_load(fyaml)
template_args = {"config": config, "root": ROOT}
nodes = []
for fullname in walk_files(NODES):
filename = str(fullname.relative_to(NODES).parent) + "/" + fullname.stem
@@ -145,7 +153,10 @@ def main():
# Quick and dirty way to resolve all the templates using a custom encoder
nodes = list(
map(
lambda node: json.loads(json.dumps(node, cls=render_templates(node))), nodes
lambda node: json.loads(
json.dumps(node, cls=render_templates(node, template_args))
),
nodes,
)
)
@@ -155,16 +166,13 @@ def main():
dict(s) for s in set(frozenset(node["cluster"].items()) for node in nodes)
]
with open(ROOT.joinpath("config.yaml")) as fyaml:
config = yaml.safe_load(fyaml)
template_args |= {"nodes": nodes, "clusters": clusters}
RENDERED.mkdir(exist_ok=True)
for template_name in TEMPLATES.list_templates():
template = TEMPLATES.get_template(template_name)
rendered = template.render(
nodes=nodes, clusters=clusters, config=config, root=ROOT
)
rendered = template.render(template_args)
with open(RENDERED.joinpath(template_name), "w") as f:
f.write(rendered)