@kvbox/server 0.5.47 → 0.5.49
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 +173 -17
- package/dist/kvbox-server.js.map +1 -1
- package/package.json +1 -1
package/dist/kvbox-server.js
CHANGED
|
@@ -225,7 +225,8 @@ function computeUsage(events, w) {
|
|
|
225
225
|
vcpuHours: round(vcpuHours, 3),
|
|
226
226
|
ramGibHours: round(ramGibHours, 3),
|
|
227
227
|
diskGibHours: round(diskGibHours, 3),
|
|
228
|
-
|
|
228
|
+
// Inside the window only: rows may be dated after `to` (a ledger counted up to its last pull, #172).
|
|
229
|
+
estimatedStops: sorted.filter((e) => e.state === "stopped" && e.by === "server" && e.at >= w.from && e.at < w.to).length,
|
|
229
230
|
historyFrom: sorted[0]?.at ?? null
|
|
230
231
|
};
|
|
231
232
|
if (w.pricing !== void 0) totals.cost = { amount: round(costOf(totals, w.pricing), 4), currency: w.pricing.currency };
|
|
@@ -338,6 +339,7 @@ function reportTotals(boxes, volumes, pricing) {
|
|
|
338
339
|
if (pricing !== void 0) return { ...out, cost: { amount: round(costOf(sum, pricing) + volumeCostOf(vol, pricing), 4), currency: pricing.currency } };
|
|
339
340
|
return out;
|
|
340
341
|
}
|
|
342
|
+
var DAY_MS = 24 * HOUR_MS;
|
|
341
343
|
|
|
342
344
|
// src/core/refs.ts
|
|
343
345
|
import { userInfo } from "node:os";
|
|
@@ -685,7 +687,7 @@ var MachineMetaSchema = z3.object({
|
|
|
685
687
|
* each host port the box's for its life. DNATed while the box runs, rebuilt from here with its
|
|
686
688
|
* current IP every time it comes up. Absent: none.
|
|
687
689
|
*/
|
|
688
|
-
ports: z3.array(z3.object({ guestPort: z3.number().int(), hostPort: z3.number().int(), proto: z3.enum(["tcp", "udp"]) })).optional(),
|
|
690
|
+
ports: z3.array(z3.object({ guestPort: z3.number().int(), hostPort: z3.number().int(), proto: z3.enum(["tcp", "udp"]), closed: z3.literal(true).optional() })).optional(),
|
|
689
691
|
/** Seconds without traffic on the box's tap before the server hibernates it (#47, core/idle.ts). Absent: never. */
|
|
690
692
|
autoHibernateSec: z3.number().int().positive().optional(),
|
|
691
693
|
/**
|
|
@@ -992,10 +994,17 @@ var PortForwardSchema = z4.object({
|
|
|
992
994
|
hostPort: z4.number().int(),
|
|
993
995
|
proto: PortProtoSchema,
|
|
994
996
|
/** The host's public address; only the control plane fills it in, as for `ssh`. */
|
|
995
|
-
address: z4.string().optional()
|
|
997
|
+
address: z4.string().optional(),
|
|
998
|
+
/**
|
|
999
|
+
* Closed but kept (#173): nothing reaches the box through it, and the host port stays the box's, so
|
|
1000
|
+
* publishing the same box port again opens the same number. Absent: open while the box runs.
|
|
1001
|
+
*/
|
|
1002
|
+
closed: z4.literal(true).optional()
|
|
996
1003
|
});
|
|
997
1004
|
var PortsRequest = z4.object({
|
|
998
|
-
ports: z4.array(z4.object({ guestPort: z4.number().int().min(1).max(65535), proto: PortProtoSchema.optional() })).min(1).max(MAX_BOX_PORTS)
|
|
1005
|
+
ports: z4.array(z4.object({ guestPort: z4.number().int().min(1).max(65535), proto: PortProtoSchema.optional() })).min(1).max(MAX_BOX_PORTS),
|
|
1006
|
+
/** `DELETE` only (#173): close the ports but keep their host ports for the box (`closed`). */
|
|
1007
|
+
keep: z4.boolean().optional()
|
|
999
1008
|
});
|
|
1000
1009
|
var BoxPortSchema = z4.number().int().min(1).max(65535);
|
|
1001
1010
|
var MAX_BOX_LINKS = 32;
|
|
@@ -1180,6 +1189,11 @@ var CreateVolumeRequest = z4.object({
|
|
|
1180
1189
|
var AttachVolumeRequest = z4.object({
|
|
1181
1190
|
box: z4.string()
|
|
1182
1191
|
});
|
|
1192
|
+
var ResizeVolumeRequest = z4.object({
|
|
1193
|
+
sizeGib: z4.number().int().min(1).max(MAX_VOLUME_GIB)
|
|
1194
|
+
});
|
|
1195
|
+
var RunScriptRequest = z4.object({ input: z4.unknown().optional() });
|
|
1196
|
+
var ScriptOutputSchema = z4.object({ output: z4.unknown() });
|
|
1183
1197
|
var EnvVarSchema = z4.object({
|
|
1184
1198
|
name: z4.string(),
|
|
1185
1199
|
value: z4.string().optional(),
|
|
@@ -1324,6 +1338,15 @@ var CapacitySchema = z4.object({
|
|
|
1324
1338
|
version: z4.string().optional()
|
|
1325
1339
|
});
|
|
1326
1340
|
var ImageSchema = z4.object({ name: z4.string(), version: z4.string().nullable() });
|
|
1341
|
+
var ImageIndexSchema = z4.object({
|
|
1342
|
+
updatedAt: z4.string(),
|
|
1343
|
+
images: z4.record(z4.string(), z4.object({ version: z4.string().min(1), minServer: z4.string().min(1).optional() }))
|
|
1344
|
+
});
|
|
1345
|
+
var LatestImagesSchema = z4.object({
|
|
1346
|
+
updatedAt: z4.string().nullable(),
|
|
1347
|
+
images: ImageIndexSchema.shape.images,
|
|
1348
|
+
checkedAt: z4.string().nullable()
|
|
1349
|
+
});
|
|
1327
1350
|
var RecoveryReportSchema = z4.object({
|
|
1328
1351
|
at: z4.string(),
|
|
1329
1352
|
booted: z4.array(z4.string()),
|
|
@@ -1435,11 +1458,31 @@ var VolumeUsageSchema = z4.object({
|
|
|
1435
1458
|
cost: z4.object({ amount: z4.number(), currency: z4.string() }).optional(),
|
|
1436
1459
|
historyFrom: z4.string().nullable()
|
|
1437
1460
|
});
|
|
1461
|
+
var UsageDayItemSchema = z4.object({
|
|
1462
|
+
id: z4.string(),
|
|
1463
|
+
name: z4.string(),
|
|
1464
|
+
owner: z4.string(),
|
|
1465
|
+
/** Removed by the end of the report's window. */
|
|
1466
|
+
removed: z4.boolean(),
|
|
1467
|
+
cost: z4.number()
|
|
1468
|
+
});
|
|
1469
|
+
var UsageDaySchema = z4.object({
|
|
1470
|
+
/** `YYYY-MM-DD`, UTC. */
|
|
1471
|
+
date: z4.string(),
|
|
1472
|
+
from: z4.string(),
|
|
1473
|
+
to: z4.string(),
|
|
1474
|
+
cost: z4.number(),
|
|
1475
|
+
currency: z4.string(),
|
|
1476
|
+
boxes: z4.array(UsageDayItemSchema),
|
|
1477
|
+
volumes: z4.array(UsageDayItemSchema)
|
|
1478
|
+
});
|
|
1438
1479
|
var UsageReportSchema = z4.object({
|
|
1439
1480
|
window: z4.object({ from: z4.string(), to: z4.string() }),
|
|
1440
1481
|
boxes: z4.array(BoxUsageSchema),
|
|
1441
1482
|
/** Absent from a side older than #91. */
|
|
1442
1483
|
volumes: z4.array(VolumeUsageSchema).optional(),
|
|
1484
|
+
/** With `?by=day` only, oldest day first. */
|
|
1485
|
+
days: z4.array(UsageDaySchema).optional(),
|
|
1443
1486
|
totals: z4.object({
|
|
1444
1487
|
seconds: MachineUsageSchema.shape.seconds,
|
|
1445
1488
|
vcpuHours: z4.number(),
|
|
@@ -1673,7 +1716,9 @@ var TrustCaReportSchema = z4.object({
|
|
|
1673
1716
|
pending: z4.array(z4.object({ box: z4.string(), when: SshKeysAppliedSchema, why: z4.string().optional() })),
|
|
1674
1717
|
failed: z4.array(z4.object({ box: z4.string(), error: z4.string() })),
|
|
1675
1718
|
/** Local boxes, untouched: they trust the host's key only. */
|
|
1676
|
-
local: z4.number().int()
|
|
1719
|
+
local: z4.number().int(),
|
|
1720
|
+
/** Cloud boxes made without a CA line (a managed service's, #173), untouched: no certificate opens them. Absent from older servers. */
|
|
1721
|
+
skipped: z4.array(z4.string()).optional()
|
|
1677
1722
|
});
|
|
1678
1723
|
var HostJoinRequestSchema = z4.object({
|
|
1679
1724
|
token: z4.string().min(1),
|
|
@@ -1795,6 +1840,9 @@ function setEnv(udsPath, vars, timeoutMs) {
|
|
|
1795
1840
|
async function idle(udsPath, script, timeoutMs = 15e3) {
|
|
1796
1841
|
return (await agentCall(udsPath, { op: "idle", path: script }, timeoutMs))["idle"] === true;
|
|
1797
1842
|
}
|
|
1843
|
+
async function runScript(udsPath, path, input, timeoutMs = 35e3) {
|
|
1844
|
+
return (await agentCall(udsPath, { op: "script", path, input: input ?? null }, timeoutMs))["output"];
|
|
1845
|
+
}
|
|
1798
1846
|
var isUnknownOp = (err) => /unknown op/.test(err.message ?? "");
|
|
1799
1847
|
var isGuestBooting = (err) => /did not accept the vsock connection/.test(err.message ?? "");
|
|
1800
1848
|
var isNoAgent = (err) => /vsock connect refused|vsock connection closed before the guest accepted|did not accept the vsock connection|kvbox-agent unreachable: connect (ENOENT|ECONNREFUSED)/.test(err.message ?? "");
|
|
@@ -2131,7 +2179,12 @@ var ImageManifestSchema = z6.object({
|
|
|
2131
2179
|
/** Variables the host fills with a random password when the caller gave none. */
|
|
2132
2180
|
generate: z6.array(z6.string()).optional(),
|
|
2133
2181
|
/** How to reach what the box serves, `${NAME}` taken from its variables and `${BOX}` its name. */
|
|
2134
|
-
connect: z6.string().optional()
|
|
2182
|
+
connect: z6.string().optional(),
|
|
2183
|
+
/**
|
|
2184
|
+
* Scripts in the box the host may run by name through kvbox-agent (#173): `status`, `password`, …
|
|
2185
|
+
* The host knows the names only; what each does and answers is the image's.
|
|
2186
|
+
*/
|
|
2187
|
+
scripts: z6.record(z6.string(), z6.string().startsWith("/")).optional()
|
|
2135
2188
|
});
|
|
2136
2189
|
var manifests = /* @__PURE__ */ new Map();
|
|
2137
2190
|
async function imageManifest(file) {
|
|
@@ -2356,6 +2409,33 @@ async function deleteVolume(ref, p = paths()) {
|
|
|
2356
2409
|
}
|
|
2357
2410
|
});
|
|
2358
2411
|
}
|
|
2412
|
+
async function assertCanGrow(ref, sizeGib, p = paths()) {
|
|
2413
|
+
const meta = await readVolume(ref, p);
|
|
2414
|
+
if (!Number.isInteger(sizeGib) || sizeGib > MAX_VOLUME_GIB) throw new KvboxError(`volume size: whole GB, at most ${MAX_VOLUME_GIB}`);
|
|
2415
|
+
if (sizeGib <= meta.sizeGib) {
|
|
2416
|
+
throw new KvboxError(`volume ${shown(ref)} is ${meta.sizeGib} GB and a volume only grows; ${sizeGib} GB is ${sizeGib === meta.sizeGib ? "what it has" : "smaller"}`);
|
|
2417
|
+
}
|
|
2418
|
+
const disk = await diskCapacity(p);
|
|
2419
|
+
const more = sizeGib - meta.sizeGib;
|
|
2420
|
+
if (!hasDiskRoom(disk, more)) {
|
|
2421
|
+
throw new KvboxError(`not enough disk on this host to grow volume ${shown(ref)} by ${more} GB: it can still promise ${volumeRoomGib(disk)} GB`);
|
|
2422
|
+
}
|
|
2423
|
+
return meta;
|
|
2424
|
+
}
|
|
2425
|
+
async function growVolume(ref, sizeGib, p = paths(), check) {
|
|
2426
|
+
return withHostLock("volumes", async () => {
|
|
2427
|
+
const meta = await assertCanGrow(ref, sizeGib, p);
|
|
2428
|
+
await check?.();
|
|
2429
|
+
const disk = p.volume(ref.owner, ref.name).disk;
|
|
2430
|
+
await truncate(disk, sizeGib * 1024 ** 3);
|
|
2431
|
+
await run(["e2fsck", "-f", "-p", disk], { okExitCodes: [1] });
|
|
2432
|
+
await run(["resize2fs", disk]);
|
|
2433
|
+
const grown = { ...meta, sizeGib };
|
|
2434
|
+
await writeVolumeMeta(grown, p);
|
|
2435
|
+
recordVolume(grown, "resize", p);
|
|
2436
|
+
return toVolume(grown, (await attachments(ref.owner, p)).get(ref.name) ?? null, p);
|
|
2437
|
+
});
|
|
2438
|
+
}
|
|
2359
2439
|
function volumeRef(box, name) {
|
|
2360
2440
|
return parseRef(name, box.owner);
|
|
2361
2441
|
}
|
|
@@ -2941,7 +3021,7 @@ var publicPortsInUse = (metas) => metas.flatMap((m) => [...m.sshPort === void 0
|
|
|
2941
3021
|
var isCloudBox = (owner) => realmOf(owner) !== null;
|
|
2942
3022
|
function boxProxies(meta) {
|
|
2943
3023
|
const ssh = meta.sshPort === void 0 ? [] : [{ box: meta.id, boxIp: meta.ip, hostPort: meta.sshPort, guestPort: 22 }];
|
|
2944
|
-
return [...ssh, ...(meta.ports ?? []).map((f) => ({ box: meta.id, boxIp: meta.ip, hostPort: f.hostPort, guestPort: f.guestPort, proto: f.proto }))];
|
|
3024
|
+
return [...ssh, ...(meta.ports ?? []).filter((f) => !f.closed).map((f) => ({ box: meta.id, boxIp: meta.ip, hostPort: f.hostPort, guestPort: f.guestPort, proto: f.proto }))];
|
|
2945
3025
|
}
|
|
2946
3026
|
function boxLinks(meta) {
|
|
2947
3027
|
return (meta.links ?? []).map((l) => ({ box: meta.id, fromIp: l.ip, toIp: meta.ip, port: l.port }));
|
|
@@ -3640,7 +3720,7 @@ async function applySshKeys(meta, sshKeys, p) {
|
|
|
3640
3720
|
}
|
|
3641
3721
|
async function trustCa(publicKeyText, p = paths()) {
|
|
3642
3722
|
const publicKey = parseSshPublicKey(publicKeyText);
|
|
3643
|
-
const report2 = { publicKey, fingerprint: sshKeyFingerprint(publicKey), updated: [], pending: [], failed: [], local: 0 };
|
|
3723
|
+
const report2 = { publicKey, fingerprint: sshKeyFingerprint(publicKey), updated: [], pending: [], failed: [], local: 0, skipped: [] };
|
|
3644
3724
|
const hostKey = await readHostPubkey(p);
|
|
3645
3725
|
for (const listed of await readAllMeta(p)) {
|
|
3646
3726
|
if (!isCloudBox(listed.owner)) {
|
|
@@ -3651,6 +3731,10 @@ async function trustCa(publicKeyText, p = paths()) {
|
|
|
3651
3731
|
try {
|
|
3652
3732
|
await withBoxLock(listed, async () => {
|
|
3653
3733
|
const meta = await readMeta(listed, p);
|
|
3734
|
+
if (!(meta.sshKeys ?? []).some(isCaLine)) {
|
|
3735
|
+
report2.skipped.push(box);
|
|
3736
|
+
return;
|
|
3737
|
+
}
|
|
3654
3738
|
const sshKeys = [...(meta.sshKeys ?? []).filter((l) => !isCaLine(l)), caLineFor(meta.owner, publicKey)];
|
|
3655
3739
|
authorizedKeysLines(hostKey, sshKeys);
|
|
3656
3740
|
const { applied, why } = await applySshKeys(meta, sshKeys, p);
|
|
@@ -3775,7 +3859,21 @@ async function applyAtNextBoot(meta, changes, by, cfg, p, whileStopped) {
|
|
|
3775
3859
|
}
|
|
3776
3860
|
const updated = { ...meta, ...changes, pid: null, memSnapshots: void 0, paused: void 0 };
|
|
3777
3861
|
await writeMachineMeta(updated, p);
|
|
3778
|
-
if (whileStopped)
|
|
3862
|
+
if (whileStopped) {
|
|
3863
|
+
try {
|
|
3864
|
+
await whileStopped();
|
|
3865
|
+
} catch (err) {
|
|
3866
|
+
const before = { ...meta, pid: null, memSnapshots: void 0, paused: void 0 };
|
|
3867
|
+
await writeMachineMeta(before, p);
|
|
3868
|
+
if (wasRunning) {
|
|
3869
|
+
recordState(before, "stopped", by, p);
|
|
3870
|
+
await startMachine(meta, p, true).catch((e) => {
|
|
3871
|
+
throw new KvboxError(`${err.message}; and box ${shown(meta)} could not be started again: ${e.message}`);
|
|
3872
|
+
});
|
|
3873
|
+
}
|
|
3874
|
+
throw err;
|
|
3875
|
+
}
|
|
3876
|
+
}
|
|
3779
3877
|
recordState(updated, "stopped", by, p);
|
|
3780
3878
|
return wasRunning ? startMachine(meta, p, true) : toMachine(updated, p);
|
|
3781
3879
|
}
|
|
@@ -3857,6 +3955,28 @@ var detachVolume = locked(async function detachVolume2(ref, volume, p = paths())
|
|
|
3857
3955
|
const kept = meta.volumes.filter((v) => v !== volume.name);
|
|
3858
3956
|
return applyAtNextBoot(meta, { volumes: kept.length > 0 ? kept : void 0 }, "detach", cfg, p, () => rm3(p.machine(ref.owner, ref.name).volumeDrive(volume.name), { force: true }));
|
|
3859
3957
|
});
|
|
3958
|
+
async function resizeVolume(volume, sizeGib, p = paths()) {
|
|
3959
|
+
await assertCanGrow(volume, sizeGib, p);
|
|
3960
|
+
const holder = await attachedBox(volume, p);
|
|
3961
|
+
if (holder === null) {
|
|
3962
|
+
return growVolume(volume, sizeGib, p, async () => {
|
|
3963
|
+
const now = await attachedBox(volume, p);
|
|
3964
|
+
if (now !== null) throw new KvboxError(`volume ${shown(volume)} was just attached to box ${now}; try again`);
|
|
3965
|
+
});
|
|
3966
|
+
}
|
|
3967
|
+
const box = { owner: volume.owner, name: holder };
|
|
3968
|
+
return withBoxLock(box, async () => {
|
|
3969
|
+
const meta = await readMeta(box, p);
|
|
3970
|
+
if (!meta.volumes?.includes(volume.name)) throw new KvboxError(`volume ${shown(volume)} was just detached from box ${holder}; try again`);
|
|
3971
|
+
assertNotHibernated(meta, p);
|
|
3972
|
+
const cfg = await loadConfig(p);
|
|
3973
|
+
let grown;
|
|
3974
|
+
await applyAtNextBoot(meta, {}, "grow", cfg, p, async () => {
|
|
3975
|
+
grown = await growVolume(volume, sizeGib, p);
|
|
3976
|
+
});
|
|
3977
|
+
return { ...grown, attachedTo: holder };
|
|
3978
|
+
});
|
|
3979
|
+
}
|
|
3860
3980
|
var setMachine = locked(async function setMachine2(ref, req, p = paths()) {
|
|
3861
3981
|
const meta = await readMeta(ref, p);
|
|
3862
3982
|
const next = { ...meta };
|
|
@@ -3875,25 +3995,30 @@ var addPorts = locked(async function addPorts2(ref, req, p = paths()) {
|
|
|
3875
3995
|
const cfg = await loadConfig(p);
|
|
3876
3996
|
const current = meta.ports ?? [];
|
|
3877
3997
|
const wanted = portKeys(req);
|
|
3878
|
-
const
|
|
3998
|
+
const isClosed = (k) => current.some((f) => samePort(k)(f) && f.closed);
|
|
3999
|
+
const reopened = wanted.filter(isClosed);
|
|
4000
|
+
const fresh = wanted.filter((w) => !current.some(samePort(w)));
|
|
4001
|
+
const already = wanted.find((w) => current.some(samePort(w)) && !isClosed(w));
|
|
3879
4002
|
if (already) throw new KvboxError(`box ${shown(ref)} already publishes port ${already.guestPort} (${already.proto})`);
|
|
3880
4003
|
if (meta.sshPort !== void 0 && wanted.some(samePort({ guestPort: 22, proto: "tcp" }))) {
|
|
3881
4004
|
throw new KvboxError(`box ${shown(ref)} already reaches port 22 from outside on port ${meta.sshPort} (kvbox connect)`);
|
|
3882
4005
|
}
|
|
3883
|
-
if (current.length +
|
|
4006
|
+
if (current.length + fresh.length > MAX_BOX_PORTS) {
|
|
3884
4007
|
throw new KvboxError(`a box publishes at most ${MAX_BOX_PORTS} ports; box ${shown(ref)} has ${current.length}`);
|
|
3885
4008
|
}
|
|
3886
|
-
const drawn = await drawPorts(
|
|
4009
|
+
const drawn = await drawPorts(fresh.length, p);
|
|
3887
4010
|
const opened = [];
|
|
3888
4011
|
try {
|
|
3889
|
-
const added =
|
|
4012
|
+
const added = fresh.map((w, i) => ({ ...w, hostPort: drawn.ports[i] }));
|
|
4013
|
+
const open3 = ({ closed: _closed, ...f }) => f;
|
|
4014
|
+
const again = current.filter((f) => reopened.some(samePort(f))).map(open3);
|
|
3890
4015
|
if (vmAlive(meta)) {
|
|
3891
|
-
for (const proxy of boxProxies({ id: meta.id, ip: meta.ip, ports: added })) {
|
|
4016
|
+
for (const proxy of boxProxies({ id: meta.id, ip: meta.ip, ports: [...again, ...added] })) {
|
|
3892
4017
|
await openProxy(proxy, cfg);
|
|
3893
4018
|
opened.push(proxy);
|
|
3894
4019
|
}
|
|
3895
4020
|
}
|
|
3896
|
-
const next = { ...meta, ports: [...current, ...added] };
|
|
4021
|
+
const next = { ...meta, ports: [...current.map((f) => reopened.some(samePort(f)) ? open3(f) : f), ...added] };
|
|
3897
4022
|
await writeMachineMeta(next, p);
|
|
3898
4023
|
return toMachine(next, p);
|
|
3899
4024
|
} catch (err) {
|
|
@@ -3912,7 +4037,7 @@ var removePorts = locked(async function removePorts2(ref, req, p = paths()) {
|
|
|
3912
4037
|
if (missing) throw new KvboxError(`box ${shown(ref)} does not publish port ${missing.guestPort} (${missing.proto})`);
|
|
3913
4038
|
const removed = current.filter((f) => gone.some(samePort(f)));
|
|
3914
4039
|
for (const proxy of boxProxies({ id: meta.id, ip: meta.ip, ports: removed })) await closeProxy(proxy, cfg);
|
|
3915
|
-
const kept = current.filter((f) => !gone.some(samePort(f)));
|
|
4040
|
+
const kept = req.keep ? current.map((f) => gone.some(samePort(f)) ? { ...f, closed: true } : f) : current.filter((f) => !gone.some(samePort(f)));
|
|
3916
4041
|
const next = { ...meta, ports: kept.length > 0 ? kept : void 0 };
|
|
3917
4042
|
await writeMachineMeta(next, p);
|
|
3918
4043
|
return toMachine(next, p);
|
|
@@ -5167,7 +5292,7 @@ function versionOf(srcFileUrl, injected) {
|
|
|
5167
5292
|
}
|
|
5168
5293
|
|
|
5169
5294
|
// src/version.ts
|
|
5170
|
-
var VERSION = versionOf(import.meta.url, true ? "0.5.
|
|
5295
|
+
var VERSION = versionOf(import.meta.url, true ? "0.5.49" : void 0);
|
|
5171
5296
|
|
|
5172
5297
|
// src/caller.ts
|
|
5173
5298
|
import { createHash as createHash2, timingSafeEqual } from "node:crypto";
|
|
@@ -5601,6 +5726,27 @@ async function machineStats(ref, p = paths(), replyMs = STATS_REPLY_MS) {
|
|
|
5601
5726
|
}
|
|
5602
5727
|
}
|
|
5603
5728
|
|
|
5729
|
+
// src/core/scripts.ts
|
|
5730
|
+
async function runBoxScript(ref, script, input, p = paths()) {
|
|
5731
|
+
const meta = await readMachineMeta(ref, p).catch((err) => {
|
|
5732
|
+
if (err.code === "ENOENT") throw new NotFoundError(`box ${shown(ref)}`);
|
|
5733
|
+
throw err;
|
|
5734
|
+
});
|
|
5735
|
+
const manifest = await imageManifest(imageFileFor(meta, p));
|
|
5736
|
+
const path = Object.hasOwn(manifest.scripts ?? {}, script) ? manifest.scripts[script] : void 0;
|
|
5737
|
+
if (path === void 0) throw new KvboxError(`box ${shown(ref)} runs on ${imageLabel(meta.image)} ${meta.base}, which has no "${script}" script`);
|
|
5738
|
+
const status = statusOf(meta, p);
|
|
5739
|
+
if (status !== "running") throw new KvboxError(`${stateMessage(ref, status)}; only a running box can be asked`);
|
|
5740
|
+
try {
|
|
5741
|
+
return { output: await runScript(p.machine(meta.owner, meta.name).vsock, path, input) };
|
|
5742
|
+
} catch (err) {
|
|
5743
|
+
if (isUnknownOp(err) || isNoAgent(err)) {
|
|
5744
|
+
throw new KvboxError(`box ${shown(ref)} did not take the request: its image's kvbox-agent is too old or still starting (${err.message})`);
|
|
5745
|
+
}
|
|
5746
|
+
throw err;
|
|
5747
|
+
}
|
|
5748
|
+
}
|
|
5749
|
+
|
|
5604
5750
|
// src/core/usage.ts
|
|
5605
5751
|
async function machineUsage(ref, since, p = paths()) {
|
|
5606
5752
|
const box = await getMachine(ref, p);
|
|
@@ -5709,6 +5855,10 @@ function machineRoutes() {
|
|
|
5709
5855
|
const box = await getMachine(ref);
|
|
5710
5856
|
return c.json({ box: ref.name, name, running: box.status !== "stopped" });
|
|
5711
5857
|
});
|
|
5858
|
+
r.post("/:name/scripts/:script", async (c) => {
|
|
5859
|
+
const body = RunScriptRequest.parse(await c.req.json().catch(() => ({})));
|
|
5860
|
+
return c.json(await runBoxScript(parseRef(c.req.param("name"), c.get("owner")), c.req.param("script"), body.input));
|
|
5861
|
+
});
|
|
5712
5862
|
r.get("/:name/stats", async (c) => c.json(await machineStats(parseRef(c.req.param("name"), c.get("owner")))));
|
|
5713
5863
|
r.delete("/:name", async (c) => {
|
|
5714
5864
|
await destroyMachine(parseRef(c.req.param("name"), c.get("owner")));
|
|
@@ -5810,6 +5960,10 @@ function volumeRoutes() {
|
|
|
5810
5960
|
const owner = c.get("owner");
|
|
5811
5961
|
return c.json(await detachVolume(parseRef(body.box, owner), parseRef(c.req.param("name"), owner)));
|
|
5812
5962
|
});
|
|
5963
|
+
r.post("/:name/resize", async (c) => {
|
|
5964
|
+
const body = ResizeVolumeRequest.parse(await c.req.json().catch(() => ({})));
|
|
5965
|
+
return c.json(await resizeVolume(parseRef(c.req.param("name"), c.get("owner")), body.sizeGib));
|
|
5966
|
+
});
|
|
5813
5967
|
return r;
|
|
5814
5968
|
}
|
|
5815
5969
|
|
|
@@ -6222,6 +6376,8 @@ function describeReport(report2) {
|
|
|
6222
6376
|
for (const p of report2.pending) console.log(`box ${p.box} ${WHEN[p.when]}${p.why ? ` (${p.why})` : ""}`);
|
|
6223
6377
|
for (const f of report2.failed) console.error(`box ${f.box}: ${f.error}`);
|
|
6224
6378
|
const parts = [`${report2.updated.length} box(es) updated now`, `${report2.pending.length} pending`, `${report2.failed.length} failed`];
|
|
6379
|
+
const skipped = report2.skipped ?? [];
|
|
6380
|
+
if (skipped.length > 0) parts.push(`${skipped.length} cloud box(es) without a CA line untouched (managed services: no certificate opens them)`);
|
|
6225
6381
|
if (report2.local > 0) parts.push(`${report2.local} local box(es) untouched (they trust this host's key only)`);
|
|
6226
6382
|
console.log(parts.join(", "));
|
|
6227
6383
|
}
|