@kvbox/server 0.5.41 → 0.5.43
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/kvbox-server.js +124 -50
- package/dist/kvbox-server.js.map +1 -1
- package/package.json +1 -1
package/dist/kvbox-server.js
CHANGED
|
@@ -612,7 +612,9 @@ var MachineSchema = z3.object({
|
|
|
612
612
|
diskBytes: z3.number().int(),
|
|
613
613
|
// the box's own writable disk, as the guest sees it
|
|
614
614
|
diskUsedBytes: z3.number().int(),
|
|
615
|
-
// what
|
|
615
|
+
// what the box uses of it: the filesystem's own count, like the guest's `df` (#115)
|
|
616
|
+
/** What the disk's sparse file occupies on the host. Grows while the box runs, `kvbox stop` trims it back. Hosts before 0.5.42 do not send it. */
|
|
617
|
+
diskHostBytes: z3.number().int().optional(),
|
|
616
618
|
base: z3.string(),
|
|
617
619
|
// version of the image the box's overlay stacks on
|
|
618
620
|
/** The image (#87) when it is not the base one: `postgres`. `base` is then that image's version. */
|
|
@@ -1174,11 +1176,32 @@ var isCaLine = (line) => /^cert-authority(,|\s)/.test(line.trim());
|
|
|
1174
1176
|
// src/core/exec.ts
|
|
1175
1177
|
import { execFile } from "node:child_process";
|
|
1176
1178
|
import { promisify } from "node:util";
|
|
1179
|
+
|
|
1180
|
+
// src/core/log.ts
|
|
1181
|
+
function formatLine(level, message, at = /* @__PURE__ */ new Date()) {
|
|
1182
|
+
return `[${at.toISOString()}][${level}]: ${message}`;
|
|
1183
|
+
}
|
|
1184
|
+
var log = {
|
|
1185
|
+
debug(message) {
|
|
1186
|
+
console.error(formatLine("debug", message));
|
|
1187
|
+
},
|
|
1188
|
+
info(message) {
|
|
1189
|
+
console.log(formatLine("info", message));
|
|
1190
|
+
},
|
|
1191
|
+
warn(message) {
|
|
1192
|
+
console.error(formatLine("warn", message));
|
|
1193
|
+
},
|
|
1194
|
+
error(message) {
|
|
1195
|
+
console.error(formatLine("error", message));
|
|
1196
|
+
}
|
|
1197
|
+
};
|
|
1198
|
+
|
|
1199
|
+
// src/core/exec.ts
|
|
1177
1200
|
var execFileAsync = promisify(execFile);
|
|
1178
1201
|
async function run(argv, opts = {}) {
|
|
1179
1202
|
const [cmd, ...args] = opts.sudo ? ["sudo", "-n", ...argv] : argv;
|
|
1180
1203
|
if (!cmd) throw new KvboxError("run: empty argv");
|
|
1181
|
-
if (process.env["KVBOX_DEBUG"])
|
|
1204
|
+
if (process.env["KVBOX_DEBUG"]) log.debug(`exec ${[cmd, ...args].join(" ")}`);
|
|
1182
1205
|
try {
|
|
1183
1206
|
const { stdout, stderr } = await execFileAsync(cmd, args, {
|
|
1184
1207
|
cwd: opts.cwd,
|
|
@@ -1841,7 +1864,7 @@ async function startDns(opts) {
|
|
|
1841
1864
|
|
|
1842
1865
|
// src/core/machines.ts
|
|
1843
1866
|
import { spawn } from "node:child_process";
|
|
1844
|
-
import { closeSync as
|
|
1867
|
+
import { closeSync as closeSync3, existsSync as existsSync6, openSync as openSync3, statSync as statSync2 } from "node:fs";
|
|
1845
1868
|
import { link as link2, mkdir as mkdir6, readFile as readFile6, rename as rename5, rm as rm3, truncate as truncate2, writeFile as writeFile5 } from "node:fs/promises";
|
|
1846
1869
|
import { setTimeout as sleep } from "node:timers/promises";
|
|
1847
1870
|
|
|
@@ -2000,7 +2023,7 @@ async function readOwnerEnv(owner, p = paths()) {
|
|
|
2000
2023
|
async function envForBoot(box, p = paths()) {
|
|
2001
2024
|
const env = await readOwnerEnv(box.owner, p).catch(() => null);
|
|
2002
2025
|
const own = await readBoxEnv(box, p).catch((err) => {
|
|
2003
|
-
|
|
2026
|
+
log.warn(`box ${shown(box)}: its own variables are unreadable, it boots without them: ${err.message}`);
|
|
2004
2027
|
return [];
|
|
2005
2028
|
});
|
|
2006
2029
|
const slug = env?.vars ?? (realmOf(box.owner) !== null ? [] : null);
|
|
@@ -2297,7 +2320,7 @@ function recordVolume(meta, by, p, at = (/* @__PURE__ */ new Date()).toISOString
|
|
|
2297
2320
|
try {
|
|
2298
2321
|
insertVolumeEvent({ volumeId: meta.id, owner: meta.owner, name: meta.name, at, sizeGib: meta.sizeGib, by }, p);
|
|
2299
2322
|
} catch (err) {
|
|
2300
|
-
|
|
2323
|
+
log.warn(`volume ledger: ${err.message}`);
|
|
2301
2324
|
}
|
|
2302
2325
|
}
|
|
2303
2326
|
async function attachments(owner, p) {
|
|
@@ -2338,7 +2361,7 @@ async function listVolumes(scope, p = paths()) {
|
|
|
2338
2361
|
}
|
|
2339
2362
|
out.push(toVolume(meta, attached.get(meta.name) ?? null, p));
|
|
2340
2363
|
} catch (err) {
|
|
2341
|
-
|
|
2364
|
+
log.warn(`skipping volumes/${ref.owner}/${ref.name}: ${err.message}`);
|
|
2342
2365
|
}
|
|
2343
2366
|
}
|
|
2344
2367
|
return out.sort((a, b) => a.owner.localeCompare(b.owner) || a.name.localeCompare(b.name));
|
|
@@ -2568,6 +2591,44 @@ async function assertRoomFor(vcpu, memMib, force, box, by, p = paths()) {
|
|
|
2568
2591
|
if (reason !== void 0) throw new KvboxError(reason);
|
|
2569
2592
|
}
|
|
2570
2593
|
|
|
2594
|
+
// src/core/ext4.ts
|
|
2595
|
+
import { closeSync as closeSync2, openSync as openSync2, readSync } from "node:fs";
|
|
2596
|
+
var SUPERBLOCK_OFFSET = 1024;
|
|
2597
|
+
var EXT4_MAGIC = 61267;
|
|
2598
|
+
var INCOMPAT_META_BG = 16;
|
|
2599
|
+
var INCOMPAT_64BIT = 128;
|
|
2600
|
+
function ext4Usage(file) {
|
|
2601
|
+
let fd;
|
|
2602
|
+
try {
|
|
2603
|
+
fd = openSync2(file, "r");
|
|
2604
|
+
const sb = Buffer.alloc(1024);
|
|
2605
|
+
if (readSync(fd, sb, 0, sb.length, SUPERBLOCK_OFFSET) !== sb.length) return null;
|
|
2606
|
+
if (sb.readUInt16LE(56) !== EXT4_MAGIC) return null;
|
|
2607
|
+
const incompat = sb.readUInt32LE(96);
|
|
2608
|
+
if (incompat & INCOMPAT_META_BG) return null;
|
|
2609
|
+
const is64 = (incompat & INCOMPAT_64BIT) !== 0;
|
|
2610
|
+
const blockSize = 1024 << sb.readUInt32LE(24);
|
|
2611
|
+
const blocks = sb.readUInt32LE(4) + (is64 ? sb.readUInt32LE(336) * 2 ** 32 : 0);
|
|
2612
|
+
const firstDataBlock = sb.readUInt32LE(20);
|
|
2613
|
+
const blocksPerGroup = sb.readUInt32LE(32);
|
|
2614
|
+
const descSize = is64 ? sb.readUInt16LE(254) : 32;
|
|
2615
|
+
if (blocksPerGroup === 0 || descSize < 32) return null;
|
|
2616
|
+
const groups = Math.ceil((blocks - firstDataBlock) / blocksPerGroup);
|
|
2617
|
+
const table = Buffer.alloc(groups * descSize);
|
|
2618
|
+
if (readSync(fd, table, 0, table.length, (firstDataBlock + 1) * blockSize) !== table.length) return null;
|
|
2619
|
+
let free = 0;
|
|
2620
|
+
for (let g = 0; g < groups; g++) {
|
|
2621
|
+
const at = g * descSize;
|
|
2622
|
+
free += table.readUInt16LE(at + 12) + (descSize >= 64 ? table.readUInt16LE(at + 44) * 65536 : 0);
|
|
2623
|
+
}
|
|
2624
|
+
return { sizeBytes: blocks * blockSize, usedBytes: (blocks - free) * blockSize };
|
|
2625
|
+
} catch {
|
|
2626
|
+
return null;
|
|
2627
|
+
} finally {
|
|
2628
|
+
if (fd !== void 0) closeSync2(fd);
|
|
2629
|
+
}
|
|
2630
|
+
}
|
|
2631
|
+
|
|
2571
2632
|
// src/core/snapshots.ts
|
|
2572
2633
|
import { existsSync as existsSync4 } from "node:fs";
|
|
2573
2634
|
import { link, mkdir as mkdir5, readdir as readdir5, rename as rename4, rm as rm2, stat as stat2 } from "node:fs/promises";
|
|
@@ -2707,7 +2768,7 @@ async function listSnapshots(scope, p = paths()) {
|
|
|
2707
2768
|
try {
|
|
2708
2769
|
out.push(toSnapshot(await readSnapshotMeta(ref, p)));
|
|
2709
2770
|
} catch (err) {
|
|
2710
|
-
|
|
2771
|
+
log.warn(`skipping snapshots/${refKey(ref)}: ${err.message}`);
|
|
2711
2772
|
}
|
|
2712
2773
|
}
|
|
2713
2774
|
return out;
|
|
@@ -2986,15 +3047,15 @@ async function standingLinks(meta, p) {
|
|
|
2986
3047
|
async function openPorts(meta, cfg, p) {
|
|
2987
3048
|
for (const proxy of boxProxies(meta)) await openProxy(proxy, cfg);
|
|
2988
3049
|
for (const link3 of await standingLinks(meta, p)) {
|
|
2989
|
-
await openLink(link3, cfg).catch((err) =>
|
|
3050
|
+
await openLink(link3, cfg).catch((err) => log.warn(`could not open the link from ${link3.fromIp}: ${err.message}`));
|
|
2990
3051
|
}
|
|
2991
3052
|
}
|
|
2992
3053
|
async function closePorts(meta, cfg) {
|
|
2993
3054
|
for (const proxy of boxProxies(meta)) {
|
|
2994
|
-
await closeProxy(proxy, cfg).catch((err) =>
|
|
3055
|
+
await closeProxy(proxy, cfg).catch((err) => log.warn(`could not close port ${proxy.hostPort}: ${err.message}`));
|
|
2995
3056
|
}
|
|
2996
3057
|
for (const link3 of boxLinks(meta)) {
|
|
2997
|
-
await closeLink(link3, cfg).catch((err) =>
|
|
3058
|
+
await closeLink(link3, cfg).catch((err) => log.warn(`could not close the link from ${link3.fromIp}: ${err.message}`));
|
|
2998
3059
|
}
|
|
2999
3060
|
}
|
|
3000
3061
|
var reservedPorts = /* @__PURE__ */ new Set();
|
|
@@ -3015,11 +3076,16 @@ async function drawPorts(count, p) {
|
|
|
3015
3076
|
function diskStats(disk) {
|
|
3016
3077
|
try {
|
|
3017
3078
|
const st = statSync2(disk);
|
|
3018
|
-
|
|
3079
|
+
const diskHostBytes = st.blocks * 512;
|
|
3080
|
+
const fs = ext4Usage(disk);
|
|
3081
|
+
return { diskBytes: st.size, diskUsedBytes: fs ? Math.min(fs.usedBytes, st.size) : diskHostBytes, diskHostBytes };
|
|
3019
3082
|
} catch {
|
|
3020
|
-
return { diskBytes: 0, diskUsedBytes: 0 };
|
|
3083
|
+
return { diskBytes: 0, diskUsedBytes: 0, diskHostBytes: 0 };
|
|
3021
3084
|
}
|
|
3022
3085
|
}
|
|
3086
|
+
async function compactDisk(file) {
|
|
3087
|
+
await run(["e2fsck", "-f", "-p", "-E", "discard", file], { okExitCodes: [1, 2], timeoutMs: 10 * 6e4 });
|
|
3088
|
+
}
|
|
3023
3089
|
function diskGibOf(meta, p) {
|
|
3024
3090
|
try {
|
|
3025
3091
|
return Math.round(statSync2(p.machine(meta.owner, meta.name).overlay).size / 1024 ** 3);
|
|
@@ -3031,7 +3097,7 @@ function recordState(meta, state, by, p, at = (/* @__PURE__ */ new Date()).toISO
|
|
|
3031
3097
|
try {
|
|
3032
3098
|
insertUsageEvent({ boxId: meta.id, owner: meta.owner, name: meta.name, at, state, by, vcpu: meta.vcpu, memMib: meta.memMib, diskGib: diskGibOf(meta, p) }, p);
|
|
3033
3099
|
} catch (err) {
|
|
3034
|
-
|
|
3100
|
+
log.warn(`usage ledger: ${err.message}`);
|
|
3035
3101
|
}
|
|
3036
3102
|
}
|
|
3037
3103
|
function isHibernated(meta, p) {
|
|
@@ -3050,7 +3116,7 @@ function toMachine(meta, p = paths()) {
|
|
|
3050
3116
|
try {
|
|
3051
3117
|
closeOpenRun(meta.id, (/* @__PURE__ */ new Date()).toISOString(), p);
|
|
3052
3118
|
} catch (err) {
|
|
3053
|
-
|
|
3119
|
+
log.warn(`usage ledger: ${err.message}`);
|
|
3054
3120
|
}
|
|
3055
3121
|
}
|
|
3056
3122
|
return {
|
|
@@ -3090,7 +3156,7 @@ async function readAllMeta(p) {
|
|
|
3090
3156
|
try {
|
|
3091
3157
|
metas.push(await readMachineMeta(ref, p));
|
|
3092
3158
|
} catch (err) {
|
|
3093
|
-
|
|
3159
|
+
log.warn(`skipping machines/${refKey(ref)}: ${err.message}`);
|
|
3094
3160
|
}
|
|
3095
3161
|
}
|
|
3096
3162
|
return metas;
|
|
@@ -3160,7 +3226,7 @@ async function spawnFirecracker(ref, cfg, p) {
|
|
|
3160
3226
|
const m = p.machine(ref.owner, ref.name);
|
|
3161
3227
|
await rm3(m.sock, { force: true });
|
|
3162
3228
|
await rm3(m.vsock, { force: true });
|
|
3163
|
-
const logFd =
|
|
3229
|
+
const logFd = openSync3(m.log, "a");
|
|
3164
3230
|
let child;
|
|
3165
3231
|
try {
|
|
3166
3232
|
child = spawn(cfg.firecracker, ["--api-sock", m.sock, "--id", ref.name, "--level", "Error"], {
|
|
@@ -3169,7 +3235,7 @@ async function spawnFirecracker(ref, cfg, p) {
|
|
|
3169
3235
|
stdio: ["ignore", logFd, logFd]
|
|
3170
3236
|
});
|
|
3171
3237
|
} finally {
|
|
3172
|
-
|
|
3238
|
+
closeSync3(logFd);
|
|
3173
3239
|
}
|
|
3174
3240
|
child.unref();
|
|
3175
3241
|
const pid = child.pid;
|
|
@@ -3509,7 +3575,7 @@ async function pushAuthorizedKeys(box, vsock, p) {
|
|
|
3509
3575
|
return void 0;
|
|
3510
3576
|
} catch (err) {
|
|
3511
3577
|
const why = isUnknownOp(err) ? `its image predates base v0.6.0, whose kvbox-agent can replace authorized_keys (kvbox vm update ${shown(box)} moves it onto the current base)` : err.message;
|
|
3512
|
-
|
|
3578
|
+
log.warn(`box ${shown(box)} keeps the authorized_keys it was frozen with: ${why}`);
|
|
3513
3579
|
return why;
|
|
3514
3580
|
}
|
|
3515
3581
|
}
|
|
@@ -3518,7 +3584,7 @@ async function dropGuestVolumes(box, vsock) {
|
|
|
3518
3584
|
await dropVolumes(vsock, AGENT_REPLY_MS);
|
|
3519
3585
|
} catch (err) {
|
|
3520
3586
|
const why = isUnknownOp(err) ? `its image predates base v0.9.0, whose kvbox-agent can unmount them (kvbox vm update ${shown(box)} moves it onto the current base)` : err.message;
|
|
3521
|
-
|
|
3587
|
+
log.warn(`box ${shown(box)} keeps the volume mounts it was frozen with, over empty disks now: ${why}`);
|
|
3522
3588
|
}
|
|
3523
3589
|
}
|
|
3524
3590
|
async function pushEnv(box, vsock, p) {
|
|
@@ -3533,10 +3599,10 @@ async function pushEnv(box, vsock, p) {
|
|
|
3533
3599
|
function warnEnv(box, vars, err) {
|
|
3534
3600
|
if (isUnknownOp(err)) {
|
|
3535
3601
|
if (vars.length === 0) return;
|
|
3536
|
-
|
|
3602
|
+
log.warn(`box ${shown(box)} did not get its environment variables: its image predates base v0.10.0, whose kvbox-agent takes them (kvbox vm update ${shown(box)} moves it onto the current base)`);
|
|
3537
3603
|
return;
|
|
3538
3604
|
}
|
|
3539
|
-
|
|
3605
|
+
log.warn(`box ${shown(box)} did not get its environment variables: ${err.message}`);
|
|
3540
3606
|
}
|
|
3541
3607
|
var ENV_BOOT_WAIT_MS = 12e4;
|
|
3542
3608
|
function pushEnvAfterBoot(box, pid, vsock, p, timing = { replyMs: AGENT_REPLY_MS, waitMs: ENV_BOOT_WAIT_MS }) {
|
|
@@ -3604,6 +3670,7 @@ var stopMachine = locked(async function stopMachine2(ref, p = paths()) {
|
|
|
3604
3670
|
const m = p.machine(ref.owner, ref.name);
|
|
3605
3671
|
if (!vmAlive(meta) && !isHibernated(meta, p)) throw new KvboxError(`box ${shown(ref)} is already stopped`);
|
|
3606
3672
|
await shutdown(meta, cfg, p, 1e4);
|
|
3673
|
+
await compactDisk(m.overlay).catch((err) => log.warn(`box ${shown(ref)}: disk not compacted: ${err.message}`));
|
|
3607
3674
|
await rm3(m.hibernateDir, { recursive: true, force: true });
|
|
3608
3675
|
await rm3(m.memBase, { force: true });
|
|
3609
3676
|
const stopped = { ...meta, pid: null, memSnapshots: void 0, paused: void 0 };
|
|
@@ -4037,12 +4104,12 @@ async function dropLinksFrom(gone, p) {
|
|
|
4037
4104
|
for (const l of meta.links.filter(isGone)) {
|
|
4038
4105
|
await closeLink({ box: meta.id, fromIp: l.ip, toIp: meta.ip, port: l.port }, cfg).catch((err) => {
|
|
4039
4106
|
stuck.add(l);
|
|
4040
|
-
|
|
4107
|
+
log.warn(`could not close the link from removed box ${gone.name} into ${meta.name}: ${err.message}`);
|
|
4041
4108
|
});
|
|
4042
4109
|
}
|
|
4043
4110
|
const kept = meta.links.filter((l) => !isGone(l) || stuck.has(l)).map((l) => stuck.has(l) ? { ...l, stale: true } : l);
|
|
4044
4111
|
await writeMachineMeta({ ...meta, links: kept.length > 0 ? kept : void 0 }, p);
|
|
4045
|
-
}).catch((err) =>
|
|
4112
|
+
}).catch((err) => log.warn(`links of removed box ${gone.name}: ${err.message}`));
|
|
4046
4113
|
}
|
|
4047
4114
|
}
|
|
4048
4115
|
var RECOVERY_CONCURRENCY = 2;
|
|
@@ -4056,7 +4123,7 @@ async function settleDeadBoxes(p = paths()) {
|
|
|
4056
4123
|
try {
|
|
4057
4124
|
closeRunCutByBoot(meta.id, bootAt, now, p);
|
|
4058
4125
|
} catch (err) {
|
|
4059
|
-
|
|
4126
|
+
log.warn(`usage ledger: ${err.message}`);
|
|
4060
4127
|
}
|
|
4061
4128
|
await rm3(p.machine(meta.owner, meta.name).memBase, { force: true });
|
|
4062
4129
|
if (meta.paused) {
|
|
@@ -4077,7 +4144,7 @@ async function pinRunningTaps(p = paths()) {
|
|
|
4077
4144
|
if (!bridgeExists(cfg.bridge)) return;
|
|
4078
4145
|
for (const meta of await readAllMeta(p)) {
|
|
4079
4146
|
if (!vmAlive(meta) || !existsSync6(`/sys/class/net/${tapName(meta.id)}`)) continue;
|
|
4080
|
-
await pinTap(meta, cfg).catch((err) =>
|
|
4147
|
+
await pinTap(meta, cfg).catch((err) => log.warn(`could not pin box ${meta.name}'s tap: ${err.message}`));
|
|
4081
4148
|
}
|
|
4082
4149
|
}
|
|
4083
4150
|
var recovery = null;
|
|
@@ -4601,7 +4668,7 @@ function startIdleWatch(opts = {}) {
|
|
|
4601
4668
|
const hibernate = opts.hibernate ?? ((box) => hibernateIfRunning(box, p));
|
|
4602
4669
|
const busy = opts.busy ?? ((box) => boxBusy(box) || restoreRunning(box));
|
|
4603
4670
|
const now = opts.now ?? Date.now;
|
|
4604
|
-
const
|
|
4671
|
+
const log2 = opts.log ?? ((line) => log.info(line));
|
|
4605
4672
|
const seen = /* @__PURE__ */ new Map();
|
|
4606
4673
|
const checkErrors = /* @__PURE__ */ new Map();
|
|
4607
4674
|
let timer;
|
|
@@ -4611,10 +4678,10 @@ function startIdleWatch(opts = {}) {
|
|
|
4611
4678
|
inflight.add(box.id);
|
|
4612
4679
|
const byImage = bytes < 0;
|
|
4613
4680
|
try {
|
|
4614
|
-
if (await hibernate(box))
|
|
4681
|
+
if (await hibernate(box)) log2(`box ${box.owner}/${box.name}: ${byImage ? "idle by its own check" : "no network traffic"} for ${quiet} min; hibernated it (kvbox connect wakes it)`);
|
|
4615
4682
|
seen.delete(box.id);
|
|
4616
4683
|
} catch (err) {
|
|
4617
|
-
|
|
4684
|
+
log2(`warning: box ${box.owner}/${box.name}: could not hibernate it after ${quiet} min ${byImage ? "idle" : "without traffic"}: ${err.message}`);
|
|
4618
4685
|
seen.set(box.id, { bytes, since: t });
|
|
4619
4686
|
} finally {
|
|
4620
4687
|
inflight.delete(box.id);
|
|
@@ -4623,7 +4690,7 @@ function startIdleWatch(opts = {}) {
|
|
|
4623
4690
|
const askIdle = (box) => guestIdle(box).then(
|
|
4624
4691
|
(v) => (checkErrors.delete(box.id), v),
|
|
4625
4692
|
(err) => {
|
|
4626
|
-
if (checkErrors.get(box.id) !== err.message)
|
|
4693
|
+
if (checkErrors.get(box.id) !== err.message) log2(`warning: box ${box.owner}/${box.name}: its idle check ${box.idleCheck ?? ""} failed, so it counts as busy and does not hibernate: ${err.message}`);
|
|
4627
4694
|
checkErrors.set(box.id, err.message);
|
|
4628
4695
|
return false;
|
|
4629
4696
|
}
|
|
@@ -4662,7 +4729,7 @@ function startIdleWatch(opts = {}) {
|
|
|
4662
4729
|
};
|
|
4663
4730
|
const schedule = () => {
|
|
4664
4731
|
if (stopped) return;
|
|
4665
|
-
timer = setTimeout(() => void check().catch((err) =>
|
|
4732
|
+
timer = setTimeout(() => void check().catch((err) => log2(`warning: idle watch: ${err.message}`)).finally(schedule), intervalMs);
|
|
4666
4733
|
timer.unref();
|
|
4667
4734
|
};
|
|
4668
4735
|
schedule();
|
|
@@ -4693,7 +4760,7 @@ function startRelayWatch(opts = {}) {
|
|
|
4693
4760
|
const misses = opts.misses ?? 3;
|
|
4694
4761
|
const probe = opts.probe ?? pingRelay;
|
|
4695
4762
|
const p = opts.p ?? paths();
|
|
4696
|
-
const
|
|
4763
|
+
const log2 = opts.log ?? ((line) => log.info(line));
|
|
4697
4764
|
const restart = opts.restart ?? (async () => netSetup(await loadConfig(p), ["relay", "restart"]));
|
|
4698
4765
|
report = { restarts: 0, lastRestartAt: null };
|
|
4699
4766
|
let missed = 0;
|
|
@@ -4707,25 +4774,25 @@ function startRelayWatch(opts = {}) {
|
|
|
4707
4774
|
return;
|
|
4708
4775
|
}
|
|
4709
4776
|
if (await probe()) {
|
|
4710
|
-
if (stuck)
|
|
4777
|
+
if (stuck) log2(`relay ${cfg.relay.name}: the tunnel carries traffic again`);
|
|
4711
4778
|
missed = 0;
|
|
4712
4779
|
stuck = false;
|
|
4713
4780
|
return;
|
|
4714
4781
|
}
|
|
4715
4782
|
if (++missed < misses) return;
|
|
4716
4783
|
missed = 0;
|
|
4717
|
-
if (!stuck)
|
|
4784
|
+
if (!stuck) log2(`relay ${cfg.relay.name}: no answer through the tunnel for ${misses * intervalMs / 1e3} s; restarting it`);
|
|
4718
4785
|
try {
|
|
4719
4786
|
await restart();
|
|
4720
4787
|
report = { restarts: report.restarts + 1, lastRestartAt: (/* @__PURE__ */ new Date()).toISOString() };
|
|
4721
4788
|
} catch (err) {
|
|
4722
|
-
if (!stuck)
|
|
4789
|
+
if (!stuck) log.warn(`relay ${cfg.relay.name}: could not restart the tunnel: ${err.message}`);
|
|
4723
4790
|
}
|
|
4724
4791
|
stuck = true;
|
|
4725
4792
|
};
|
|
4726
4793
|
const schedule = () => {
|
|
4727
4794
|
if (stopped) return;
|
|
4728
|
-
timer = setTimeout(() => void round2().catch((err) =>
|
|
4795
|
+
timer = setTimeout(() => void round2().catch((err) => log.warn(`relay watch: ${err.message}`)).finally(schedule), intervalMs);
|
|
4729
4796
|
timer.unref();
|
|
4730
4797
|
};
|
|
4731
4798
|
schedule();
|
|
@@ -4756,7 +4823,7 @@ function versionOf(srcFileUrl, injected) {
|
|
|
4756
4823
|
}
|
|
4757
4824
|
|
|
4758
4825
|
// src/version.ts
|
|
4759
|
-
var VERSION = versionOf(import.meta.url, true ? "0.5.
|
|
4826
|
+
var VERSION = versionOf(import.meta.url, true ? "0.5.43" : void 0);
|
|
4760
4827
|
|
|
4761
4828
|
// src/caller.ts
|
|
4762
4829
|
import { createHash as createHash2, timingSafeEqual } from "node:crypto";
|
|
@@ -5391,7 +5458,7 @@ function createApp(opts = {}) {
|
|
|
5391
5458
|
if (err instanceof KvboxError) {
|
|
5392
5459
|
return c.json({ error: err.message }, err.exitCode === 2 ? 501 : 400);
|
|
5393
5460
|
}
|
|
5394
|
-
|
|
5461
|
+
log.error(`${c.req.method} ${c.req.path}: ${err.stack ?? err.message}`);
|
|
5395
5462
|
return c.json({ error: err.message }, 500);
|
|
5396
5463
|
});
|
|
5397
5464
|
return app;
|
|
@@ -5461,38 +5528,40 @@ async function listen(opts = {}) {
|
|
|
5461
5528
|
}
|
|
5462
5529
|
|
|
5463
5530
|
// src/index.ts
|
|
5531
|
+
var daemon = false;
|
|
5464
5532
|
var program = new Command("kvbox-server").description("kvbox host daemon: owns Firecracker and ~/.kvbox, serves the kvbox API").version(VERSION, "-v, --version").showHelpAfterError();
|
|
5465
5533
|
program.command("start", { isDefault: true }).description("serve the API on the local Unix socket (foreground)").option("--socket <path>", "socket to listen on (default: $KVBOX_HOME/server.sock)").action(async (opts) => {
|
|
5534
|
+
daemon = true;
|
|
5466
5535
|
await requireKvm();
|
|
5467
5536
|
const settled = await settleDeadBoxes(paths());
|
|
5468
|
-
await clearStagedBackups(paths()).catch((err) =>
|
|
5537
|
+
await clearStagedBackups(paths()).catch((err) => log.warn(`staged backups not cleared: ${err.message}`));
|
|
5469
5538
|
const { socketPath, https } = await listen(opts.socket ? { socketPath: opts.socket } : {});
|
|
5470
|
-
|
|
5539
|
+
log.info(`kvbox-server ${VERSION} listening on ${socketPath} (pid ${process.pid})`);
|
|
5471
5540
|
if (https) {
|
|
5472
5541
|
const cfg = await loadConfig(paths());
|
|
5473
|
-
|
|
5542
|
+
log.info(`control plane ${cfg.controlPlane?.name}: answering on ${https.url}`);
|
|
5474
5543
|
}
|
|
5475
5544
|
try {
|
|
5476
5545
|
const cfg = await loadConfig(paths());
|
|
5477
5546
|
const state = await ensureHostNetwork(cfg);
|
|
5478
|
-
|
|
5547
|
+
log.info(`host network: bridge ${cfg.bridge} ${state}`);
|
|
5479
5548
|
await pinRunningTaps(paths());
|
|
5480
5549
|
} catch (err) {
|
|
5481
|
-
|
|
5550
|
+
log.warn(`host network not ready (${err.message}); run: kvbox-server net up`);
|
|
5482
5551
|
}
|
|
5483
5552
|
try {
|
|
5484
5553
|
const relay = await restoreRelay(paths());
|
|
5485
|
-
if (relay)
|
|
5554
|
+
if (relay) log.info(`relay ${relay.name}: the control plane may reach this host through the tunnel again`);
|
|
5486
5555
|
} catch (err) {
|
|
5487
|
-
|
|
5556
|
+
log.warn(`the control plane may not reach this host through its relay (${err.message}); run: kvbox server relay <join string>`);
|
|
5488
5557
|
}
|
|
5489
5558
|
let dnsWarned = false;
|
|
5490
5559
|
const bindDns = () => {
|
|
5491
5560
|
loadConfig(paths()).then(async (cfg) => {
|
|
5492
5561
|
await startDns({ host: cfg.gateway });
|
|
5493
|
-
|
|
5562
|
+
log.info(`box names: <name>.kvbox answered on ${cfg.gateway}`);
|
|
5494
5563
|
}).catch((err) => {
|
|
5495
|
-
if (!dnsWarned)
|
|
5564
|
+
if (!dnsWarned) log.warn(`box names (<name>.kvbox) do not resolve yet (${err.message}); retrying`);
|
|
5496
5565
|
dnsWarned = true;
|
|
5497
5566
|
setTimeout(bindDns, DNS_RETRY_MS).unref();
|
|
5498
5567
|
});
|
|
@@ -5502,9 +5571,9 @@ program.command("start", { isDefault: true }).description("serve the API on the
|
|
|
5502
5571
|
startIdleWatch();
|
|
5503
5572
|
const recovery2 = await bootRecovered(settled, paths());
|
|
5504
5573
|
if (recovery2) {
|
|
5505
|
-
for (const box of recovery2.booted)
|
|
5506
|
-
for (const box of recovery2.leftStopped)
|
|
5507
|
-
for (const f of recovery2.failed)
|
|
5574
|
+
for (const box of recovery2.booted) log.info(`box ${box} was running when it went down; booted it again`);
|
|
5575
|
+
for (const box of recovery2.leftStopped) log.info(`box ${box} was in standby when it went down; its memory is gone, left stopped`);
|
|
5576
|
+
for (const f of recovery2.failed) log.warn(`box ${f.box} was running when it went down and did not come back: ${f.error}`);
|
|
5508
5577
|
}
|
|
5509
5578
|
});
|
|
5510
5579
|
program.command("join <control-plane-url>").description("register this host with a control plane (needs a one-time token minted there); it answers the control plane from its next start").requiredOption("--token <token>", "one-time join token from the control plane").option("--advertise <host-or-ip>", "address the control plane reaches this host at (default: this host's default-route address)").option("--name <name>", "how the control plane lists this host (default: hostname)").option("--country <country>", "where this host is, a name or a code (romania, US); kvbox new --country places boxes by it").action(async (url, opts) => {
|
|
@@ -5541,9 +5610,14 @@ try {
|
|
|
5541
5610
|
await program.parseAsync(process.argv);
|
|
5542
5611
|
} catch (err) {
|
|
5543
5612
|
if (err instanceof KvboxError) {
|
|
5544
|
-
|
|
5613
|
+
if (daemon) log.error(err.message);
|
|
5614
|
+
else console.error(`error: ${err.message}`);
|
|
5545
5615
|
process.exit(err.exitCode);
|
|
5546
5616
|
}
|
|
5617
|
+
if (daemon) {
|
|
5618
|
+
log.error(err.stack ?? String(err));
|
|
5619
|
+
process.exit(1);
|
|
5620
|
+
}
|
|
5547
5621
|
throw err;
|
|
5548
5622
|
}
|
|
5549
5623
|
//# sourceMappingURL=kvbox-server.js.map
|