Turn talos patches into templates and resolve them
This commit is contained in:
45
tools/merge
45
tools/merge
@@ -8,9 +8,29 @@ import pathlib
|
||||
|
||||
import requests
|
||||
import yaml
|
||||
from jinja2 import Environment, FileSystemLoader, StrictUndefined, Template
|
||||
|
||||
NODES = pathlib.Path("nodes")
|
||||
SCHEMATICS = pathlib.Path("schematics")
|
||||
PATCHES = Environment(loader=FileSystemLoader("patches"), undefined=StrictUndefined)
|
||||
TEMPLATES = Environment(loader=FileSystemLoader("templates"), undefined=StrictUndefined)
|
||||
|
||||
|
||||
def node_encoder(node: dict):
|
||||
class Inner(json.JSONEncoder):
|
||||
def default(self, o):
|
||||
if isinstance(o, Template):
|
||||
try:
|
||||
rendered = o.render(node)
|
||||
except Exception as e:
|
||||
e.add_note(f"While rendering for: {node['hostname']}")
|
||||
raise e
|
||||
# Parse the rendered yaml and convert it to a json patch
|
||||
return json.dumps(yaml.safe_load(rendered))
|
||||
|
||||
return super().default(o)
|
||||
|
||||
return Inner
|
||||
|
||||
|
||||
@functools.cache
|
||||
@@ -24,18 +44,28 @@ def get_schematic_id(schematic: str):
|
||||
|
||||
def schematic_constructor(loader: yaml.SafeLoader, node: yaml.nodes.ScalarNode):
|
||||
"""Load specified schematic file and get the assocatied schematic id"""
|
||||
filename = loader.construct_scalar(node)
|
||||
schematic_name = loader.construct_yaml_str(node)
|
||||
try:
|
||||
schematic = SCHEMATICS.joinpath(filename).with_suffix(".yaml").read_text()
|
||||
schematic = SCHEMATICS.joinpath(schematic_name).with_suffix(".yaml").read_text()
|
||||
return get_schematic_id(schematic)
|
||||
except Exception:
|
||||
raise yaml.MarkedYAMLError("Failed to load schematic", node.start_mark)
|
||||
|
||||
|
||||
def patch_constructor(loader: yaml.SafeLoader, node: yaml.nodes.ScalarNode):
|
||||
patch_name = loader.construct_scalar(node)
|
||||
try:
|
||||
template = PATCHES.get_template(f"{patch_name}.yaml")
|
||||
return template
|
||||
except Exception:
|
||||
raise yaml.MarkedYAMLError("Failed to load patch", node.start_mark)
|
||||
|
||||
|
||||
def get_loader():
|
||||
"""Add special constructors to yaml loader"""
|
||||
loader = yaml.SafeLoader
|
||||
loader.add_constructor("!schematic", schematic_constructor)
|
||||
loader.add_constructor("!patch", patch_constructor)
|
||||
|
||||
return loader
|
||||
|
||||
@@ -65,7 +95,7 @@ def walk_files(root: pathlib.Path):
|
||||
|
||||
|
||||
def main():
|
||||
data = []
|
||||
nodes = []
|
||||
for fullname in walk_files(NODES):
|
||||
filename = str(fullname.relative_to(NODES).parent) + "/" + fullname.stem
|
||||
|
||||
@@ -74,10 +104,15 @@ def main():
|
||||
yml_data = get_defaults(fullname.parent, NODES) | yml_data
|
||||
yml_data["hostname"] = fullname.stem
|
||||
yml_data["filename"] = filename
|
||||
data.append(yml_data)
|
||||
nodes.append(yml_data)
|
||||
|
||||
final_nodes = []
|
||||
for node in nodes:
|
||||
# Quick and dirty way to resolve all the templates using a custom encoder
|
||||
final_nodes.append(json.loads(json.dumps(node, cls=node_encoder(node))))
|
||||
|
||||
# Dump everything to json
|
||||
print(json.dumps(data, indent=4))
|
||||
print(json.dumps(final_nodes, indent=4))
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
|
||||
Reference in New Issue
Block a user