Scenario lifecycle: raise, exec, snapshot, restore, destroy
A declaration goes in and a disposable mesh comes out. Verified on a workstation, not asserted: two machines raised and addressed in 14.6s, snapshot 0.28s, restore-to-usable 11.6s, both families pinging with no loss, and the workstation with no route into any of it. The declaration layer implements the model in full — three positions a machine can be in, keyed on forwardability; gateways carrying the address the world sees them as; both address families; multi-homing; MTU; inter-segment policy. It is validated hard because the failures it prevents are silent: a private range on a public segment produces no error, the mesh simply never forms. Public segments are refused unless they use RFC 5737 or RFC 3849 space, and a range wider than the reserved block is refused too. 33 tests, all offline. The runtime implements less than the model, and refuses the difference. A scenario declaring gateways, published ports, policy, inbound deny or place is rejected at raise with every gap named. Raising it would produce a mesh that silently lacks what it declared, which is the fault this lab exists to catch — 04-ISSUES/003, where a firewall key is declared in five manifests and read by no code. Three bugs found by review and by running it, all of one family: The readiness check truthiness-tested incusOk's return. `exec … true` succeeds with EMPTY output, so every machine reported unreachable while incus exec on it worked perfectly. succeeds() now exists so the mistake is not available, and network delete had the same bug — it counted zero segments removed while removing them. list() split instance from machine on the last dash, so a machine called home-server absorbed half the instance id and destroy found nothing. Resources are now found by the metadata they carry, never by name. restore reported success in 0.79s while the machine's agent was still starting, so the next command failed. Both raise and restore now wait for usable and say how long that took — reporting the earlier number is transport reported as effect, which is the fault the lab is being built to find. Two incus behaviours worth recording. Its CLI reads a YAML definition from stdin when stdin is not a terminal, so a spawned command hangs until the timeout kills it and arrives with empty stderr — a failure with no explanation, on a command that works when typed. And it assigns a MAC at runtime without recording it in device config, so MACs are derived and set explicitly, which the guest needs anyway: it names interfaces by bus position, and matching by name configures the wrong one on a multi-homed machine. No build step; Node strips the types. The lifecycle has no unit tests because a fake hypervisor would assert that the fake behaves as expected, which is the shape of test this project exists to stop shipping.
This commit is contained in:
@@ -0,0 +1,116 @@
|
||||
/** YAML in, a validated Scenario out. Normalises the shorthands the design's examples use. */
|
||||
|
||||
import { readFileSync } from "node:fs";
|
||||
import { parse as parseYaml } from "yaml";
|
||||
import type { Attachment, Machine, Scenario, Segment } from "./types.ts";
|
||||
import { validate } from "./validate.ts";
|
||||
|
||||
/** `address: "1.2.3.4"` and `address: [...]` both mean a list. */
|
||||
function toList(value: unknown): string[] {
|
||||
if (value === undefined || value === null) return [];
|
||||
return Array.isArray(value) ? value.map(String) : [String(value)];
|
||||
}
|
||||
|
||||
function normaliseAttachment(raw: unknown): Attachment {
|
||||
const at = (raw ?? {}) as Record<string, unknown>;
|
||||
return { segment: String(at["segment"] ?? ""), address: toList(at["address"]) };
|
||||
}
|
||||
|
||||
function normaliseMachine(raw: unknown): Machine {
|
||||
const machine = (raw ?? {}) as Record<string, unknown>;
|
||||
const at = machine["at"];
|
||||
|
||||
const attachment: Machine["at"] =
|
||||
at === "detached"
|
||||
? "detached"
|
||||
: Array.isArray(at)
|
||||
? at.map(normaliseAttachment)
|
||||
: [normaliseAttachment(at)];
|
||||
|
||||
const result: Machine = { at: attachment };
|
||||
const published = machine["published"];
|
||||
if (Array.isArray(published)) {
|
||||
result.published = published.map((entry) => {
|
||||
const p = (entry ?? {}) as Record<string, unknown>;
|
||||
return { port: Number(p["port"]), on: String(p["on"] ?? "") };
|
||||
});
|
||||
}
|
||||
const inbound = machine["inbound"];
|
||||
if (inbound === "allow" || inbound === "deny") result.inbound = inbound;
|
||||
return result;
|
||||
}
|
||||
|
||||
function normaliseSegment(raw: unknown): Segment {
|
||||
const segment = (raw ?? {}) as Record<string, unknown>;
|
||||
const result: Segment = {
|
||||
kind: segment["kind"] === "public" ? "public" : "private",
|
||||
cidr: toList(segment["cidr"]),
|
||||
};
|
||||
if (segment["mtu"] !== undefined) result.mtu = Number(segment["mtu"]);
|
||||
|
||||
const gateway = segment["gateway"] as Record<string, unknown> | undefined;
|
||||
if (gateway) {
|
||||
const nat = toList(gateway["nat"]).filter((f): f is "v4" | "v6" => f === "v4" || f === "v6");
|
||||
result.gateway = {
|
||||
to: String(gateway["to"] ?? ""),
|
||||
address: toList(gateway["address"]),
|
||||
nat,
|
||||
// Absent means forwardable: a gateway you control is the ordinary case, and the
|
||||
// interesting one — carrier-grade NAT — should have to be stated.
|
||||
forwardable: gateway["forwardable"] !== false,
|
||||
};
|
||||
const ttl = gateway["mapping_ttl"] ?? gateway["mappingTtl"];
|
||||
if (ttl !== undefined) result.gateway.mappingTtl = String(ttl);
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
export function parseScenario(text: string): Scenario {
|
||||
const raw = (parseYaml(text) ?? {}) as Record<string, unknown>;
|
||||
|
||||
const segments: Record<string, Segment> = {};
|
||||
for (const [name, value] of Object.entries(
|
||||
(raw["segments"] ?? {}) as Record<string, unknown>,
|
||||
)) {
|
||||
segments[name] = normaliseSegment(value);
|
||||
}
|
||||
|
||||
const machines: Record<string, Machine> = {};
|
||||
for (const [name, value] of Object.entries(
|
||||
(raw["machines"] ?? {}) as Record<string, unknown>,
|
||||
)) {
|
||||
machines[name] = normaliseMachine(value);
|
||||
}
|
||||
|
||||
const scenario: Scenario = {
|
||||
scenario: String(raw["scenario"] ?? ""),
|
||||
segments,
|
||||
machines,
|
||||
};
|
||||
|
||||
const policy = raw["policy"];
|
||||
if (Array.isArray(policy)) {
|
||||
scenario.policy = policy.map((entry) => {
|
||||
const p = (entry ?? {}) as Record<string, unknown>;
|
||||
return { from: String(p["from"] ?? ""), to: String(p["to"] ?? ""), allow: p["allow"] !== false };
|
||||
});
|
||||
}
|
||||
|
||||
const place = raw["place"];
|
||||
if (place && typeof place === "object") {
|
||||
const normalised: Record<string, string[]> = {};
|
||||
for (const [key, value] of Object.entries(place as Record<string, unknown>)) {
|
||||
normalised[key] = toList(value);
|
||||
}
|
||||
scenario.place = normalised;
|
||||
}
|
||||
|
||||
if (raw["snapshot"] !== undefined) scenario.snapshot = String(raw["snapshot"]);
|
||||
|
||||
validate(scenario);
|
||||
return scenario;
|
||||
}
|
||||
|
||||
export function loadScenario(path: string): Scenario {
|
||||
return parseScenario(readFileSync(path, "utf-8"));
|
||||
}
|
||||
Reference in New Issue
Block a user