-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathfake-anka.mjs
More file actions
executable file
·95 lines (88 loc) · 3.03 KB
/
Copy pathfake-anka.mjs
File metadata and controls
executable file
·95 lines (88 loc) · 3.03 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
#!/usr/bin/env node
// A fake `anka` CLI used by the test suite so the local backend can be exercised
// without a real Anka install. Behavior is tuned via env vars:
// FAKE_ANKA_LOG - append each invocation's argv (JSON) to this file
// FAKE_ANKA_RUNNING - number of extra "running" VMs reported by `list`
// FAKE_ANKA_RUNNING_VM - name that `show` reports as running (with an IP)
// FAKE_ANKA_FAIL - command name that should exit non-zero with an error
// FAKE_ANKA_IP_AFTER - number of `show` polls reporting running-without-IP
// before the IP appears (requires FAKE_ANKA_COUNT_FILE)
// FAKE_ANKA_COUNT_FILE - file used to persist the `show` poll counter
import fs from "node:fs";
const argv = process.argv.slice(2);
if (process.env.FAKE_ANKA_LOG) {
fs.appendFileSync(process.env.FAKE_ANKA_LOG, JSON.stringify(argv) + "\n");
}
const emit = (obj) => process.stdout.write(JSON.stringify(obj));
const ok = (body) => {
emit({ status: "OK", message: "", body });
process.exit(0);
};
const fail = (message, code = 1) => {
emit({ status: "ERROR", message });
process.exit(code);
};
if (argv[0] === "--version") {
process.stdout.write("Anka fake 1.0\n");
process.exit(0);
}
let args = argv;
if (args[0] === "-j") args = args.slice(1);
const cmd = args[0];
if (process.env.FAKE_ANKA_FAIL && process.env.FAKE_ANKA_FAIL === cmd) {
fail(`simulated failure for ${cmd}`);
}
switch (cmd) {
case "list": {
const running = Number.parseInt(process.env.FAKE_ANKA_RUNNING || "0", 10);
const vms = [
{ name: "base-template", uuid: "uuid-base", status: "stopped" },
{ name: "other", uuid: "uuid-other", version: "v1", status: "stopped" }
];
for (let i = 0; i < running; i += 1) {
vms.push({ name: `running-${i}`, uuid: `uuid-run-${i}`, status: "running" });
}
ok(vms);
break;
}
case "show": {
const name = args[1];
if (name === "missing") fail(`VM ${name} not found`);
if (name === process.env.FAKE_ANKA_RUNNING_VM) {
const ipAfter = Number.parseInt(process.env.FAKE_ANKA_IP_AFTER || "0", 10);
if (ipAfter > 0 && process.env.FAKE_ANKA_COUNT_FILE) {
let count = 0;
try {
count = Number.parseInt(fs.readFileSync(process.env.FAKE_ANKA_COUNT_FILE, "utf8"), 10) || 0;
} catch {
count = 0;
}
count += 1;
fs.writeFileSync(process.env.FAKE_ANKA_COUNT_FILE, String(count));
if (count <= ipAfter) {
ok({ uuid: "uuid-x", name, status: "running", port_forwarding: [] });
}
}
ok({ uuid: "uuid-x", name, status: "running", ip: "192.168.64.50", port_forwarding: [] });
}
ok({ uuid: "uuid-x", name, status: "stopped" });
break;
}
case "clone":
ok({ uuid: "uuid-clone", name: args[2] });
break;
case "start":
ok({});
break;
case "delete": // delete --yes <name>
ok({});
break;
case "cp":
process.exit(0);
break;
case "run":
process.exit(0);
break;
default:
fail(`unknown command: ${cmd}`);
}