@kvbox/server 0.5.34 → 0.5.35
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 +47 -20
- package/dist/kvbox-server.js.map +1 -1
- package/package.json +1 -1
package/dist/kvbox-server.js
CHANGED
|
@@ -665,7 +665,12 @@ var RescaleMachineRequest = z3.object({
|
|
|
665
665
|
size: SizeSchema.optional(),
|
|
666
666
|
vcpu: z3.number().int().min(1).max(64).optional(),
|
|
667
667
|
memMib: z3.number().int().min(256).optional(),
|
|
668
|
-
diskGib: z3.number().int().min(1).optional()
|
|
668
|
+
diskGib: z3.number().int().min(1).optional(),
|
|
669
|
+
/** Skip the capacity guard when a running box grows (#37). */
|
|
670
|
+
force: z3.boolean().optional()
|
|
671
|
+
});
|
|
672
|
+
var StartMachineRequest = z3.object({
|
|
673
|
+
force: z3.boolean().optional()
|
|
669
674
|
});
|
|
670
675
|
var SetMachineRequest = z3.object({
|
|
671
676
|
/** Seconds without network traffic before the box is hibernated (#47); 0: never. */
|
|
@@ -2381,7 +2386,7 @@ async function runningReservations(p = paths()) {
|
|
|
2381
2386
|
for (const ref of await listMachineRefs(p)) {
|
|
2382
2387
|
try {
|
|
2383
2388
|
const meta = await readMachineMeta(ref, p);
|
|
2384
|
-
if (vmAlive(meta)) out.push({ name: meta.name, vcpu: meta.vcpu, memMib: meta.memMib });
|
|
2389
|
+
if (vmAlive(meta)) out.push({ owner: meta.owner, name: meta.name, vcpu: meta.vcpu, memMib: meta.memMib });
|
|
2385
2390
|
} catch {
|
|
2386
2391
|
}
|
|
2387
2392
|
}
|
|
@@ -2405,30 +2410,41 @@ function gb(mib) {
|
|
|
2405
2410
|
const g = mib / 1024;
|
|
2406
2411
|
return `${Number.isInteger(g) ? g : g.toFixed(1)} GB`;
|
|
2407
2412
|
}
|
|
2408
|
-
function describeRunning(running) {
|
|
2413
|
+
function describeRunning(running, viewer) {
|
|
2409
2414
|
if (running.length === 0) return "no box is running";
|
|
2410
|
-
const
|
|
2415
|
+
const own = viewer === void 0 ? running : running.filter((r) => r.owner === viewer);
|
|
2416
|
+
const others = running.length - own.length;
|
|
2417
|
+
const named = own.map((r) => `${r.name} ${r.vcpu} vCPU/${gb(r.memMib)}`);
|
|
2418
|
+
if (others > 0) named.push(`${others} ${others === 1 ? "box" : "boxes"} of other users`);
|
|
2419
|
+
const list = named.join(", ");
|
|
2411
2420
|
const vcpu = running.reduce((n, r) => n + r.vcpu, 0);
|
|
2412
2421
|
const mem = running.reduce((n, r) => n + r.memMib, 0);
|
|
2413
2422
|
return `${running.length} running ${running.length === 1 ? "box reserves" : "boxes reserve"} ${vcpu} vCPU and ${gb(mem)} (${list})`;
|
|
2414
2423
|
}
|
|
2415
|
-
|
|
2424
|
+
var HINTS = {
|
|
2425
|
+
new: "stop or remove a box (kvbox stop, kvbox rm), pick a smaller --size, or pass --force to start it anyway",
|
|
2426
|
+
start: "stop or remove a box (kvbox stop, kvbox rm), or pass --force to start it anyway",
|
|
2427
|
+
rescale: "stop or remove a box (kvbox stop, kvbox rm), ask for less, or pass --force to rescale it anyway"
|
|
2428
|
+
};
|
|
2429
|
+
function roomFor(host, running, want, opts = {}) {
|
|
2430
|
+
const { viewer, by = "new" } = opts;
|
|
2416
2431
|
const reservedMem = running.reduce((n, r) => n + r.memMib, 0);
|
|
2417
2432
|
const reservedVcpu = running.reduce((n, r) => n + r.vcpu, 0);
|
|
2418
2433
|
const memLimit = host.memMib - MEM_HEADROOM_MIB;
|
|
2419
2434
|
const vcpuLimit = host.cores * VCPU_OVERSUBSCRIPTION;
|
|
2420
|
-
const hint =
|
|
2435
|
+
const hint = HINTS[by];
|
|
2421
2436
|
if (reservedMem + want.memMib > memLimit) {
|
|
2422
|
-
return `not enough memory for a ${gb(want.memMib)} box: ${describeRunning(running)}; this host has ${gb(host.memMib)} and keeps ${gb(MEM_HEADROOM_MIB)} for itself, so boxes may reserve ${gb(memLimit)} in total. ` + hint;
|
|
2437
|
+
return `not enough memory for a ${gb(want.memMib)} box: ${describeRunning(running, viewer)}; this host has ${gb(host.memMib)} and keeps ${gb(MEM_HEADROOM_MIB)} for itself, so boxes may reserve ${gb(memLimit)} in total. ` + hint;
|
|
2423
2438
|
}
|
|
2424
2439
|
if (reservedVcpu + want.vcpu > vcpuLimit) {
|
|
2425
|
-
return `too many vCPUs for a ${want.vcpu} vCPU box: ${describeRunning(running)}; this host has ${host.cores} cores and allows ${vcpuLimit} vCPUs in total (${VCPU_OVERSUBSCRIPTION}x). ` + hint;
|
|
2440
|
+
return `too many vCPUs for a ${want.vcpu} vCPU box: ${describeRunning(running, viewer)}; this host has ${host.cores} cores and allows ${vcpuLimit} vCPUs in total (${VCPU_OVERSUBSCRIPTION}x). ` + hint;
|
|
2426
2441
|
}
|
|
2427
2442
|
return void 0;
|
|
2428
2443
|
}
|
|
2429
|
-
async function assertRoomFor(vcpu, memMib, force, p = paths()) {
|
|
2444
|
+
async function assertRoomFor(vcpu, memMib, force, box, by, p = paths()) {
|
|
2430
2445
|
if (force) return;
|
|
2431
|
-
const
|
|
2446
|
+
const running = (await runningReservations(p)).filter((r) => box.name === void 0 || !(r.owner === box.owner && r.name === box.name));
|
|
2447
|
+
const reason = roomFor(hostResources(), running, { vcpu, memMib }, { viewer: box.owner, by });
|
|
2432
2448
|
if (reason !== void 0) throw new KvboxError(reason);
|
|
2433
2449
|
}
|
|
2434
2450
|
|
|
@@ -3121,7 +3137,7 @@ async function createMachine(req, owner, p = paths()) {
|
|
|
3121
3137
|
if (!isSizeName(sizeName)) throw new KvboxError(`unknown size "${sizeName}"; use one of ${Object.keys(SIZES).join(", ")}`);
|
|
3122
3138
|
const size = SIZES[sizeName];
|
|
3123
3139
|
assertAssets(cfg);
|
|
3124
|
-
await assertRoomFor(size.vcpu, size.memMib, req.force ?? false, p);
|
|
3140
|
+
await assertRoomFor(size.vcpu, size.memMib, req.force ?? false, { owner }, "new", p);
|
|
3125
3141
|
const sshKeys = isCloudBox(owner) && req.sshKeys !== void 0 && req.sshKeys.length > 0 ? req.sshKeys : void 0;
|
|
3126
3142
|
authorizedKeysB64(await readHostPubkey(p), sshKeys ?? []);
|
|
3127
3143
|
const existing = await readAllMeta(p);
|
|
@@ -3213,7 +3229,7 @@ async function restoreFrom(from, owner, req, cfg, p) {
|
|
|
3213
3229
|
if (req.image !== void 0 && imageKey(req.image) !== snap.image) {
|
|
3214
3230
|
throw new KvboxError(`--base cannot be combined with --from: a restored box has the snapshot's image (${shown(from)}: ${imageLabel(snap.image)})`);
|
|
3215
3231
|
}
|
|
3216
|
-
await assertRoomFor(snap.vcpu, snap.memMib, req.force ?? false, p);
|
|
3232
|
+
await assertRoomFor(snap.vcpu, snap.memMib, req.force ?? false, { owner }, "new", p);
|
|
3217
3233
|
const sshKeys = isCloudBox(owner) && req.sshKeys !== void 0 && req.sshKeys.length > 0 ? req.sshKeys : void 0;
|
|
3218
3234
|
authorizedKeysB64(await readHostPubkey(p), sshKeys ?? []);
|
|
3219
3235
|
const existing = await readAllMeta(p);
|
|
@@ -3303,7 +3319,7 @@ var forkMachine = locked(async function forkMachine2(ref, owner, req, p = paths(
|
|
|
3303
3319
|
}
|
|
3304
3320
|
assertAssets(cfg);
|
|
3305
3321
|
imageFileFor(src, p);
|
|
3306
|
-
await assertRoomFor(shape.vcpu, shape.memMib, req.force ?? false, p);
|
|
3322
|
+
await assertRoomFor(shape.vcpu, shape.memMib, req.force ?? false, { owner }, "new", p);
|
|
3307
3323
|
const sshKeys = isCloudBox(owner) && req.sshKeys !== void 0 && req.sshKeys.length > 0 ? req.sshKeys : void 0;
|
|
3308
3324
|
authorizedKeysB64(await readHostPubkey(p), sshKeys ?? []);
|
|
3309
3325
|
const existing = await readAllMeta(p);
|
|
@@ -3475,7 +3491,7 @@ var stopMachine = locked(async function stopMachine2(ref, p = paths()) {
|
|
|
3475
3491
|
recordState(stopped, "stopped", "stop", p);
|
|
3476
3492
|
return toMachine(stopped, p);
|
|
3477
3493
|
});
|
|
3478
|
-
var startMachine = locked(async function startMachine2(ref, p = paths()) {
|
|
3494
|
+
var startMachine = locked(async function startMachine2(ref, p = paths(), force = false) {
|
|
3479
3495
|
const meta = await readMeta(ref, p);
|
|
3480
3496
|
const cfg = await loadConfig(p);
|
|
3481
3497
|
if (vmAlive(meta)) throw new KvboxError(`box ${shown(ref)} is already ${meta.paused ? "up, in standby (kvbox resume " + shown(ref) + ")" : "running"}`);
|
|
@@ -3483,6 +3499,7 @@ var startMachine = locked(async function startMachine2(ref, p = paths()) {
|
|
|
3483
3499
|
throw new KvboxError(`${stateMessage(ref, "hibernated")}: kvbox wake ${shown(ref)} continues where it left off; to boot fresh instead, kvbox stop ${shown(ref)} first (discards its memory)`);
|
|
3484
3500
|
}
|
|
3485
3501
|
assertAssets(cfg);
|
|
3502
|
+
await assertRoomFor(meta.vcpu, meta.memMib, force, meta, "start", p);
|
|
3486
3503
|
const keysB64 = authorizedKeysB64(await readHostPubkey(p), meta.sshKeys ?? []);
|
|
3487
3504
|
await ensureHostNetwork(cfg);
|
|
3488
3505
|
const tap = await createTap(meta, cfg);
|
|
@@ -3613,7 +3630,7 @@ var hibernateMachine = locked(async function hibernateMachine2(ref, p = paths(),
|
|
|
3613
3630
|
recordState(hibernated, "hibernated", by, p);
|
|
3614
3631
|
return toMachine(hibernated, p);
|
|
3615
3632
|
});
|
|
3616
|
-
var wakeMachine = locked(async function wakeMachine2(ref, p = paths()) {
|
|
3633
|
+
var wakeMachine = locked(async function wakeMachine2(ref, p = paths(), force = false) {
|
|
3617
3634
|
const meta = await readMeta(ref, p);
|
|
3618
3635
|
const cfg = await loadConfig(p);
|
|
3619
3636
|
const status = statusOf(meta, p);
|
|
@@ -3623,6 +3640,7 @@ var wakeMachine = locked(async function wakeMachine2(ref, p = paths()) {
|
|
|
3623
3640
|
if (!existsSync6(imageFile(meta.image, meta.base, p))) {
|
|
3624
3641
|
throw new KvboxError(`box ${shown(ref)} stacks on ${imageLabel(meta.image)} ${meta.base}, which is gone from ${p.assets}`);
|
|
3625
3642
|
}
|
|
3643
|
+
await assertRoomFor(meta.vcpu, meta.memMib, force, meta, "start", p);
|
|
3626
3644
|
const m = p.machine(ref.owner, ref.name);
|
|
3627
3645
|
await ensureHostNetwork(cfg);
|
|
3628
3646
|
const tap = await createTap(meta, cfg, { detached: true });
|
|
@@ -3663,7 +3681,7 @@ async function applyAtNextBoot(meta, changes, by, cfg, p, whileStopped) {
|
|
|
3663
3681
|
await writeMachineMeta(updated, p);
|
|
3664
3682
|
if (whileStopped) await whileStopped();
|
|
3665
3683
|
recordState(updated, "stopped", by, p);
|
|
3666
|
-
return wasRunning ? startMachine(meta, p) : toMachine(updated, p);
|
|
3684
|
+
return wasRunning ? startMachine(meta, p, true) : toMachine(updated, p);
|
|
3667
3685
|
}
|
|
3668
3686
|
function assertNotHibernated(meta, p) {
|
|
3669
3687
|
if (isHibernated(meta, p)) {
|
|
@@ -3700,6 +3718,9 @@ var rescaleMachine = locked(async function rescaleMachine2(ref, req, p = paths()
|
|
|
3700
3718
|
if (target.memMib !== meta.memMib) changes.memMib = target.memMib;
|
|
3701
3719
|
const growTo = target.diskGib > currentDiskGib ? target.diskGib : void 0;
|
|
3702
3720
|
if (Object.keys(changes).length === 0 && growTo === void 0) return toMachine(meta, p);
|
|
3721
|
+
if (vmAlive(meta) && (target.vcpu > meta.vcpu || target.memMib > meta.memMib)) {
|
|
3722
|
+
await assertRoomFor(target.vcpu, target.memMib, req.force ?? false, meta, "rescale", p);
|
|
3723
|
+
}
|
|
3703
3724
|
return applyAtNextBoot(meta, changes, "rescale", cfg, p, growTo === void 0 ? void 0 : () => growDisk(disk, growTo));
|
|
3704
3725
|
});
|
|
3705
3726
|
var attachVolume = locked(async function attachVolume2(ref, volume, p = paths()) {
|
|
@@ -3945,7 +3966,7 @@ async function bootRecovered(settled, p = paths()) {
|
|
|
3945
3966
|
for (let ref = queue.shift(); ref !== void 0 && !recoveryStopping; ref = queue.shift()) {
|
|
3946
3967
|
try {
|
|
3947
3968
|
if (!cutByReboot(await readMeta(ref, p))) continue;
|
|
3948
|
-
await startMachine(ref, p);
|
|
3969
|
+
await startMachine(ref, p, true);
|
|
3949
3970
|
booted.push(refKey(ref));
|
|
3950
3971
|
} catch (err) {
|
|
3951
3972
|
failed.push({ box: refKey(ref), error: err.message });
|
|
@@ -4234,7 +4255,7 @@ function versionOf(srcFileUrl, injected) {
|
|
|
4234
4255
|
}
|
|
4235
4256
|
|
|
4236
4257
|
// src/version.ts
|
|
4237
|
-
var VERSION = versionOf(import.meta.url, true ? "0.5.
|
|
4258
|
+
var VERSION = versionOf(import.meta.url, true ? "0.5.35" : void 0);
|
|
4238
4259
|
|
|
4239
4260
|
// src/caller.ts
|
|
4240
4261
|
import { createHash as createHash2, timingSafeEqual } from "node:crypto";
|
|
@@ -4623,11 +4644,17 @@ function machineRoutes() {
|
|
|
4623
4644
|
return c.json(await forkMachine(parseRef(c.req.param("name"), caller), into, body), 201);
|
|
4624
4645
|
});
|
|
4625
4646
|
r.post("/:name/stop", async (c) => c.json(await stopMachine(parseRef(c.req.param("name"), c.get("owner")))));
|
|
4626
|
-
r.post("/:name/start", async (c) =>
|
|
4647
|
+
r.post("/:name/start", async (c) => {
|
|
4648
|
+
const body = StartMachineRequest.parse(await c.req.json().catch(() => ({})));
|
|
4649
|
+
return c.json(await startMachine(parseRef(c.req.param("name"), c.get("owner")), void 0, body.force ?? false));
|
|
4650
|
+
});
|
|
4627
4651
|
r.post("/:name/pause", async (c) => c.json(await pauseMachine(parseRef(c.req.param("name"), c.get("owner")))));
|
|
4628
4652
|
r.post("/:name/resume", async (c) => c.json(await resumeMachine(parseRef(c.req.param("name"), c.get("owner")))));
|
|
4629
4653
|
r.post("/:name/hibernate", async (c) => c.json(await hibernateMachine(parseRef(c.req.param("name"), c.get("owner")))));
|
|
4630
|
-
r.post("/:name/wake", async (c) =>
|
|
4654
|
+
r.post("/:name/wake", async (c) => {
|
|
4655
|
+
const body = StartMachineRequest.parse(await c.req.json().catch(() => ({})));
|
|
4656
|
+
return c.json(await wakeMachine(parseRef(c.req.param("name"), c.get("owner")), void 0, body.force ?? false));
|
|
4657
|
+
});
|
|
4631
4658
|
r.post("/:name/update", async (c) => c.json(await updateMachine(parseRef(c.req.param("name"), c.get("owner")))));
|
|
4632
4659
|
r.post("/:name/rescale", async (c) => {
|
|
4633
4660
|
const body = RescaleMachineRequest.parse(await c.req.json().catch(() => ({})));
|