A convenient CLI tool to quickly spin up DragonflyBSD virtual machines using QEMU with sensible defaults.
1import { createId } from "@paralleldrive/cuid2";
2import { Effect, pipe } from "effect";
3import { saveImage } from "../images.ts";
4import { getInstanceState, type VirtualMachine } from "../mod.ts";
5import { du, extractTag } from "../utils.ts";
6
7const failIfNoVM = (
8 [vm, tag]: [VirtualMachine | undefined, string],
9) =>
10 Effect.gen(function* () {
11 if (!vm) {
12 throw new Error(`VM with name ${name} not found`);
13 }
14 if (!vm.drivePath) {
15 throw new Error(`VM with name ${name} has no drive attached`);
16 }
17
18 const size = yield* du(vm.drivePath);
19
20 return [vm, tag, size] as [VirtualMachine, string, number];
21 });
22
23export default async function (name: string, image: string) {
24 await Effect.runPromise(
25 pipe(
26 Effect.all([getInstanceState(name), extractTag(image)]),
27 Effect.flatMap(failIfNoVM),
28 Effect.flatMap(([vm, tag, size]) =>
29 saveImage({
30 id: createId(),
31 repository: image.split(":")[0],
32 tag,
33 size,
34 path: vm.drivePath!,
35 format: vm.diskFormat,
36 })
37 ),
38 Effect.catchAll((error) =>
39 Effect.sync(() => {
40 console.error(`Failed to tag image: ${error.cause}`);
41 Deno.exit(1);
42 })
43 ),
44 ),
45 );
46}