switch to nspawn proxy

This commit is contained in:
JOLIMAITRE Matthieu 2023-12-11 00:18:21 +01:00
parent 36891699c4
commit b1ee328238
2 changed files with 13 additions and 10 deletions

View file

@ -74,26 +74,27 @@ export function start_runner(config: ContainerConfig) {
const command = container_command(name, paths.root, { const command = container_command(name, paths.root, {
boot: true, boot: true,
veth: true, veth: true,
ports: config.redirects,
cmd_opts: { cmd_opts: {
stdin: "null", stdin: "null",
stdout: "null", stdout: "null",
}, },
}); });
const proxies = [] as LoopProcess[]; // const proxies = [] as LoopProcess[];
const container_loop = loop_process(command, { const container_loop = loop_process(command, {
on_start: () => { on_start: () => {
log("container", name, "started"); log("container", name, "started");
(async () => { // (async () => {
await sleep(1_000); // await sleep(1_000);
const [address] = await get_container_addresses(name); // const [address] = await get_container_addresses(name);
proxies.push(...start_proxies(address, config.redirects)); // proxies.push(...start_proxies(address, config.redirects));
})(); // })();
}, },
on_stop: async () => { on_stop: async () => {
log("container", name, "stopped"); log("container", name, "stopped");
for (const p of proxies) await p.kill(); // for (const p of proxies) await p.kill();
await sleep(500); // await sleep(500);
proxies.splice(0, proxies.length); // proxies.splice(0, proxies.length);
}, },
}); });
return { return {
@ -101,7 +102,7 @@ export function start_runner(config: ContainerConfig) {
config, config,
container_loop, container_loop,
stop: async () => { stop: async () => {
for (const p of proxies) await p.kill(); // for (const p of proxies) await p.kill();
await container_loop.kill(); await container_loop.kill();
}, },
}; };

View file

@ -7,6 +7,7 @@ const log = log_from("nspawn");
export function container_command(name: string, directory: string, opts?: { export function container_command(name: string, directory: string, opts?: {
veth?: boolean; veth?: boolean;
boot?: boolean; boot?: boolean;
ports?: [number, number][];
cmd_opts?: Deno.CommandOptions; cmd_opts?: Deno.CommandOptions;
}) { }) {
const args = [ const args = [
@ -15,6 +16,7 @@ export function container_command(name: string, directory: string, opts?: {
]; ];
if (opts?.veth ?? false) args.push("--network-veth"); if (opts?.veth ?? false) args.push("--network-veth");
if (opts?.boot ?? false) args.push("--boot"); if (opts?.boot ?? false) args.push("--boot");
for (const [from, to] of opts?.ports ?? []) args.push(`--port=${from}:${to}`);
const command = new Deno.Command("systemd-nspawn", { ...opts?.cmd_opts, args }); const command = new Deno.Command("systemd-nspawn", { ...opts?.cmd_opts, args });
return command; return command;
} }