#!/bin/env -S deno run -A --unstable 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"; const log = log_from("control"); if (import.meta.main) await main(); async function main() { const [command, ...rest] = Deno.args; 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 (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)); log(res); return; } if (command === "disable") { const [name] = rest; if (name === undefined) log("expects NAME argument"); const res = await daemon_send(socket_path(), new_cmd_disable(name)); log(res); return; } 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); }