stabilize, add daemon state, add base build script
This commit is contained in:
parent
a1963cf491
commit
241d50e42a
12 changed files with 512 additions and 46 deletions
|
@ -1,31 +1,93 @@
|
|||
#!/bin/env -S deno run -A --unstable
|
||||
|
||||
import { daemon_send, new_cmd_disable, new_cmd_enable, new_cmd_status } from "./src/lib.ts";
|
||||
import { socket_path } from "./src/lib/paths.ts";
|
||||
import { daemon_send, new_cmd_disable, new_cmd_enable, new_cmd_status, new_cmd_stop } from "./src/lib.ts";
|
||||
import { load_all_configs, new_container_config, save_container_config } from "./src/lib/config.ts";
|
||||
import { load_base, new_container_context } from "./src/lib/create.ts";
|
||||
import { container_paths, socket_path } from "./src/lib/paths.ts";
|
||||
import { log_from, run } from "./src/lib/utils.ts";
|
||||
|
||||
await main();
|
||||
const log = log_from("control");
|
||||
|
||||
if (import.meta.main) await main();
|
||||
async function main() {
|
||||
const [kind, ...rest] = Deno.args;
|
||||
const [command, ...rest] = Deno.args;
|
||||
|
||||
if (kind === "status") {
|
||||
const res = await daemon_send(socket_path(), new_cmd_status());
|
||||
console.log({ res });
|
||||
if (["-h", "--help", "help"].includes(command)) {
|
||||
return console.log(`control.ts - controls BDMGRDTS daemon.
|
||||
Usage:
|
||||
./control.ts Command [ARG...]
|
||||
|
||||
Commands:
|
||||
help Prints a help message.
|
||||
stop Stops the daemon.
|
||||
status Get a list of enabled containers.
|
||||
create NAME BASE Create a new container from a specific base.
|
||||
enable NAME Enables a container.
|
||||
disable NAME Disables a container.
|
||||
reload NAME Reloads a container, equivalent to enable then disable.
|
||||
`);
|
||||
}
|
||||
|
||||
if (kind === "enable") {
|
||||
if (command === "create") {
|
||||
const [name, base] = rest;
|
||||
if (name === undefined) log("expects NAME argument");
|
||||
if (base === undefined) log("expects BASE argument");
|
||||
await create(name, base);
|
||||
return;
|
||||
}
|
||||
|
||||
if (command === "status") {
|
||||
const res = await daemon_send(socket_path(), new_cmd_status());
|
||||
log(res);
|
||||
return;
|
||||
}
|
||||
|
||||
if (command === "enable") {
|
||||
const [name] = rest;
|
||||
if (name === undefined) log("expects NAME argument");
|
||||
const res = await daemon_send(socket_path(), new_cmd_enable(name));
|
||||
console.log({ res });
|
||||
log(res);
|
||||
return;
|
||||
}
|
||||
|
||||
if (kind === "disable") {
|
||||
if (command === "disable") {
|
||||
const [name] = rest;
|
||||
if (name === undefined) log("expects NAME argument");
|
||||
const res = await daemon_send(socket_path(), new_cmd_disable(name));
|
||||
console.log({ res });
|
||||
log(res);
|
||||
return;
|
||||
}
|
||||
|
||||
if (kind === "stop") {
|
||||
const res = await daemon_send(socket_path(), new_cmd_status());
|
||||
console.log({ res });
|
||||
if (command === "reload") {
|
||||
const [name] = rest;
|
||||
if (name === undefined) log("expects NAME argument");
|
||||
const res = await daemon_send(socket_path(), new_cmd_enable(name));
|
||||
log(res);
|
||||
return;
|
||||
}
|
||||
|
||||
if (command === "stop") {
|
||||
const res = await daemon_send(socket_path(), new_cmd_stop());
|
||||
log(res);
|
||||
return;
|
||||
}
|
||||
|
||||
log("unknown command", command);
|
||||
}
|
||||
|
||||
export async function create(name: string, base_name: string) {
|
||||
log("loading base", base_name);
|
||||
const base = await load_base(base_name);
|
||||
const known_containers = await load_all_configs();
|
||||
|
||||
const paths = container_paths(name);
|
||||
await Deno.mkdir(paths.base);
|
||||
const configuration = new_container_config(name);
|
||||
|
||||
log("creating the container", name, "at", paths.base);
|
||||
const context = new_container_context(paths.root, configuration, known_containers);
|
||||
await Deno.mkdir(paths.root);
|
||||
await run("sudo", "chown", "root:root", paths.root);
|
||||
await base.build(context);
|
||||
await save_container_config(configuration);
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue