From f61c9dd3cab85fbde4ab44bad33a9394be6fcab0 Mon Sep 17 00:00:00 2001 From: Yuqian Yang Date: Mon, 22 Dec 2025 17:08:15 +0800 Subject: tools: add run vm command. --- deno/deno.json | 3 ++- deno/tools/deno.json | 1 + deno/tools/vm.ts | 48 +++++++++++++++++++++++++++++++++++++++++++++++- 3 files changed, 50 insertions(+), 2 deletions(-) diff --git a/deno/deno.json b/deno/deno.json index 286451e..9efebf7 100644 --- a/deno/deno.json +++ b/deno/deno.json @@ -1,7 +1,8 @@ { "workspace": ["./base", "./mail", "./tools"], "tasks": { - "compile:mail": "deno task --cwd=mail compile" + "compile:mail": "deno task --cwd=mail compile", + "compile:tools": "deno task --cwd=tools compile" }, "imports": { "@std/collections": "jsr:@std/collections@^1.1.1", diff --git a/deno/tools/deno.json b/deno/tools/deno.json index 355046a..b3405aa 100644 --- a/deno/tools/deno.json +++ b/deno/tools/deno.json @@ -1,6 +1,7 @@ { "version": "0.1.0", "tasks": { + "compile": "deno compile -o out/crupest-tools -A main.ts" }, "imports": { "mustache": "npm:mustache@^4.2.0" diff --git a/deno/tools/vm.ts b/deno/tools/vm.ts index b54c0d4..a7ee98e 100644 --- a/deno/tools/vm.ts +++ b/deno/tools/vm.ts @@ -215,11 +215,57 @@ const gen = defineYargsModule({ }, }); +function runVm(name: string) { + const vm = resolveVmSetup(name, MY_VMS); + if (vm == null) { + console.error(`No vm called ${name} is found.`); + Deno.exit(-1); + } + const preCommands = createPreCommands(vm); + const cli = createQemuArgs(vm); + const commands = [...preCommands, cli]; + const processes = commands.map((command) => { + console.log("Run command:", command.join(" ")); + return new Deno.Command(command[0], { + args: command.slice(1), + stdin: "null", + stdout: "inherit", + stderr: "inherit", + }).spawn(); + }); + Deno.addSignalListener("SIGINT", () => { + processes.forEach((process) => process.kill("SIGINT")); + }); + Deno.addSignalListener("SIGTERM", () => { + processes.forEach((process) => process.kill("SIGTERM")); + }); +} + +export const run = defineYargsModule({ + command: "run ", + describe: "run the vm", + builder: (builder) => { + return builder + .positional("name", { + describe: "name of the vm to run", + type: "string", + }) + .demandOption("name") + .strict(); + }, + handler: (argv) => { + runVm(argv.name); + }, +}); + export default defineYargsModule({ command: "vm", describe: "Manage (qemu) virtual machines.", builder: (builder) => { - return builder.command(gen).demandCommand(1, DEMAND_COMMAND_MESSAGE); + return builder.command(gen).command(run).demandCommand( + 1, + DEMAND_COMMAND_MESSAGE, + ); }, handler: () => {}, }); -- cgit v1.2.3