add nginx wrapper and refactor

This commit is contained in:
JOLIMAITRE Matthieu 2024-02-03 20:15:35 +01:00
parent 9216063aa0
commit 6f92727bb3
9 changed files with 530 additions and 92 deletions

View file

@ -5,6 +5,7 @@ import { lines, log_from, loop_process, run } from "./lib/utils.ts";
import { ContainerConfig } from "./lib/config.ts";
import { container_paths } from "./lib/paths.ts";
import { container_command } from "./lib/nspawn.ts";
import { LoopProcess } from "./lib/utils.ts";
const log = log_from("lib");
@ -66,28 +67,36 @@ export async function daemon_send(sock_path: string, command: Cmd) {
return await toText(request.readable);
}
export type Runner = Awaited<ReturnType<typeof start_runner>>;
export function start_runner(config: ContainerConfig) {
const { name } = config;
const paths = container_paths(name);
const command = container_command(name, paths.root, {
boot: true,
veth: true,
ports: config.redirects,
cmd_opts: {
stdin: "null",
stdout: "null",
},
syscall_filter: ["add_key", "keyctl", "bpf"],
});
const container_loop = loop_process(command, {
on_start: () => log("container", name, "started"),
on_stop: () => log("container", name, "stopped"),
});
return {
name,
config,
container_loop,
stop: () => container_loop.kill(),
};
export class Runner {
name;
config;
loop: LoopProcess | null;
constructor(config: ContainerConfig) {
this.name = config.name;
this.config = config;
this.loop = null;
}
start() {
const paths = container_paths(name);
const command = container_command(name, paths.root, {
boot: true,
veth: true,
redirections: this.config.redirections,
cmd_opts: {
stdin: "null",
stdout: "null",
},
syscall_filter: ["add_key", "keyctl", "bpf"],
});
this.loop = loop_process(command, {
on_start: () => log("container", name, "started"),
on_stop: () => log("container", name, "stopped"),
});
}
async stop() {
await this.loop?.kill();
}
}