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
talosVersion: v1.11.3
kernelArgs:
@@ -19,3 +19,5 @@ dns:
- 8.8.8.8
ntp: nl.pool.ntp.org
install: false
patch: !patch
- hostname

View File

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