Skip to content

Commit 2db09a2

Browse files
committed
Query LXD instance state for proxy sync
1 parent e73d723 commit 2db09a2

1 file changed

Lines changed: 34 additions & 1 deletion

File tree

scripts/terrarium-traefik-sync.ts

Lines changed: 34 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,10 @@ type LxcInstance = {
2525
};
2626
};
2727

28+
type LxcState = {
29+
network?: Record<string, LxcNetwork>;
30+
};
31+
2832
type DesiredPort = {
2933
proto: "tcp" | "udp";
3034
port: number;
@@ -86,6 +90,35 @@ function loadUfwState(): DesiredPort[] {
8690
return readJsonFile<DesiredPort[]>(UFW_STATE_PATH, []);
8791
}
8892

93+
async function enrichInstanceState(containers: LxcInstance[]): Promise<LxcInstance[]> {
94+
const enriched: LxcInstance[] = [];
95+
96+
for (const container of containers) {
97+
if (container.state?.network || !container.name) {
98+
enriched.push(container);
99+
continue;
100+
}
101+
102+
const response = await runAllowFailure(["lxc", "query", `/1.0/instances/${container.name}/state`]);
103+
if (response.exitCode !== 0) {
104+
enriched.push(container);
105+
continue;
106+
}
107+
108+
try {
109+
const state = JSON.parse(response.stdout || "{}") as LxcState;
110+
enriched.push({
111+
...container,
112+
state
113+
});
114+
} catch {
115+
enriched.push(container);
116+
}
117+
}
118+
119+
return enriched;
120+
}
121+
89122
async function ensureUfwRule(proto: "tcp" | "udp", port: number): Promise<void> {
90123
await runText(
91124
["ufw", "allow", "proto", proto, "from", "any", "to", "any", "port", String(port), "comment", "terrarium-proxy"],
@@ -340,7 +373,7 @@ function buildDynamicConfig(containers: LxcInstance[]): {
340373

341374
export async function proxySyncCmd(configPath = DEFAULT_CONFIG_PATH): Promise<void> {
342375
const config = loadConfig(configPath, PREFIX);
343-
const containers = await runJson<LxcInstance[]>(["lxc", "list", "-f", "json"], PREFIX);
376+
const containers = await enrichInstanceState(await runJson<LxcInstance[]>(["lxc", "list", "-f", "json"], PREFIX));
344377
const { dynamicYaml, extraEntrypoints, ufwPorts, errors } = buildDynamicConfig(containers);
345378
const staticYaml = buildStaticConfig(config, extraEntrypoints);
346379

0 commit comments

Comments
 (0)