nspawn_ports #1

Merged
mb merged 3 commits from nspawn_ports into master 2024-02-03 15:27:33 +01:00
2 changed files with 13 additions and 10 deletions
Showing only changes of commit b1ee328238 - Show all commits

View file

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