Improved how schematics are loaded

This commit is contained in:
2025-11-08 04:25:22 +01:00
parent 1b4eb34ec4
commit d163e0b9bc
3 changed files with 20 additions and 30 deletions

View File

@@ -1,4 +1,4 @@
schematicID: !schematic "_schematic.yaml" schematicID: !schematic default
arch: amd64 arch: amd64
talosVersion: v1.11.3 talosVersion: v1.11.3
kernelArgs: kernelArgs:
@@ -19,3 +19,5 @@ dns:
- 8.8.8.8 - 8.8.8.8
ntp: nl.pool.ntp.org ntp: nl.pool.ntp.org
install: false install: false
patch: !patch
- hostname

View File

@@ -2,7 +2,6 @@
# Adapted from: https://enix.io/en/blog/pxe-talos/ # Adapted from: https://enix.io/en/blog/pxe-talos/
import argparse
import functools import functools
import json import json
import pathlib import pathlib
@@ -10,6 +9,9 @@ import pathlib
import requests import requests
import yaml import yaml
NODES = pathlib.Path("nodes")
SCHEMATICS = pathlib.Path("schematics")
@functools.cache @functools.cache
def get_schematic_id(schematic: str): def get_schematic_id(schematic: str):
@@ -20,24 +22,20 @@ def get_schematic_id(schematic: str):
return data["id"] return data["id"]
def schematic_constructor(directory: pathlib.Path): def schematic_constructor(loader: yaml.SafeLoader, node: yaml.nodes.ScalarNode):
"""Load specified schematic file and get the assocatied schematic id""" """Load specified schematic file and get the assocatied schematic id"""
filename = loader.construct_scalar(node)
def constructor(loader: yaml.SafeLoader, node: yaml.nodes.ScalarNode):
filename = str(loader.construct_scalar(node))
try: try:
schematic = directory.joinpath(filename).read_text() schematic = SCHEMATICS.joinpath(filename).with_suffix(".yaml").read_text()
return get_schematic_id(schematic) return get_schematic_id(schematic)
except Exception: except Exception:
raise yaml.MarkedYAMLError("Failed to load schematic", node.start_mark) raise yaml.MarkedYAMLError("Failed to load schematic", node.start_mark)
return constructor
def get_loader():
def get_loader(directory: pathlib.Path):
"""Add special constructors to yaml loader""" """Add special constructors to yaml loader"""
loader = yaml.SafeLoader loader = yaml.SafeLoader
loader.add_constructor("!schematic", schematic_constructor(directory)) loader.add_constructor("!schematic", schematic_constructor)
return loader return loader
@@ -47,7 +45,7 @@ def get_defaults(directory: pathlib.Path, root: pathlib.Path):
"""Compute the defaults from the provided directory and parents.""" """Compute the defaults from the provided directory and parents."""
try: try:
with open(directory.joinpath("_defaults.yaml")) as fyaml: with open(directory.joinpath("_defaults.yaml")) as fyaml:
yml_data = yaml.load(fyaml, Loader=get_loader(directory)) yml_data = yaml.load(fyaml, Loader=get_loader())
except OSError: except OSError:
yml_data = {} yml_data = {}
@@ -67,23 +65,13 @@ def walk_files(root: pathlib.Path):
def main(): def main():
parser = argparse.ArgumentParser()
parser.add_argument("directory", type=pathlib.Path)
parser.add_argument("-f", "--filter")
args = parser.parse_args()
data = [] data = []
for fullname in walk_files(args.directory): for fullname in walk_files(NODES):
filename = ( filename = str(fullname.relative_to(NODES).parent) + "/" + fullname.stem
str(fullname.relative_to(args.directory).parent) + "/" + fullname.stem
)
if args.filter is not None and not filename.startswith(args.filter):
continue
with open(fullname) as fyaml: with open(fullname) as fyaml:
yml_data = yaml.load(fyaml, Loader=get_loader(fullname.parent)) yml_data = yaml.load(fyaml, Loader=get_loader())
yml_data = get_defaults(fullname.parent, args.directory) | yml_data yml_data = get_defaults(fullname.parent, NODES) | yml_data
yml_data["hostname"] = fullname.stem yml_data["hostname"] = fullname.stem
yml_data["filename"] = filename yml_data["filename"] = filename
data.append(yml_data) data.append(yml_data)