@kvbox/server 0.6.11 → 0.6.12
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 +123 -39
- package/dist/kvbox-server.js.map +1 -1
- package/package.json +1 -1
package/dist/kvbox-server.js
CHANGED
|
@@ -1837,6 +1837,12 @@ var BackupSchema = z5.object({
|
|
|
1837
1837
|
id: z5.string(),
|
|
1838
1838
|
owner: z5.string(),
|
|
1839
1839
|
box: z5.string(),
|
|
1840
|
+
/**
|
|
1841
|
+
* The box it was taken of has been removed (#141): a box called `box` now is another box, which this backup
|
|
1842
|
+
* never restores into; `kvbox new <name> --from-backup <id>` makes a box from it. Absent from a control plane
|
|
1843
|
+
* before #141.
|
|
1844
|
+
*/
|
|
1845
|
+
boxRemoved: z5.boolean().optional(),
|
|
1840
1846
|
status: z5.enum(BACKUP_STATUSES),
|
|
1841
1847
|
/** Why it failed; null otherwise. */
|
|
1842
1848
|
error: z5.string().nullable(),
|
|
@@ -2017,7 +2023,7 @@ var MachineSchema = z5.object({
|
|
|
2017
2023
|
image: z5.string().optional(),
|
|
2018
2024
|
createdAt: z5.string(),
|
|
2019
2025
|
source: z5.string(),
|
|
2020
|
-
// "base", "snapshot:<owner/name>"
|
|
2026
|
+
// "base", "snapshot:<owner/name>", "fork:<owner/name>" (#79) or "backup:<id>" (#141)
|
|
2021
2027
|
status: BoxStatusSchema,
|
|
2022
2028
|
/**
|
|
2023
2029
|
* Public TCP port on the host that reaches the box's SSH while it runs (#49). Only boxes made
|
|
@@ -2226,8 +2232,14 @@ var CreateMachineRequest = z5.object({
|
|
|
2226
2232
|
/** The image to boot (#87, `kvbox new --base postgres`). Absent: the base image. */
|
|
2227
2233
|
image: z5.string().optional(),
|
|
2228
2234
|
/** The box's own environment variables (#87, `kvbox new -e NAME=value`), over its owner's. */
|
|
2229
|
-
env: z5.array(z5.object({ name: z5.string(), value: z5.string() })).optional()
|
|
2235
|
+
env: z5.array(z5.object({ name: z5.string(), value: z5.string() })).optional(),
|
|
2236
|
+
/**
|
|
2237
|
+
* Start the box on this backup's disk (#141, `kvbox new --from-backup <id>`), a removed box's too: its image,
|
|
2238
|
+
* version and variables come from the backup, its size too unless given. Only the control plane reads it.
|
|
2239
|
+
*/
|
|
2240
|
+
fromBackup: z5.string().optional()
|
|
2230
2241
|
});
|
|
2242
|
+
var HostFromBackupRequest = CreateMachineRequest.omit({ from: true, fromBackup: true, image: true, env: true }).extend({ backup: HostRestoreRequest });
|
|
2231
2243
|
var ForkMachineRequest = z5.object({
|
|
2232
2244
|
name: z5.string().optional(),
|
|
2233
2245
|
/**
|
|
@@ -2313,7 +2325,9 @@ var CapacitySchema = z5.object({
|
|
|
2313
2325
|
/** Present on a host set up as a backup store: the disk its Garage keeps chunks on, and whether Garage runs. */
|
|
2314
2326
|
backupStore: z5.object({ totalBytes: z5.number().int(), freeBytes: z5.number().int(), running: z5.boolean() }).optional(),
|
|
2315
2327
|
/** True on a kvbox-server that reaches a backup store by its certificate's `pin`; older ones omit it and cannot back up to a store. */
|
|
2316
|
-
storeClient: z5.boolean().optional()
|
|
2328
|
+
storeClient: z5.boolean().optional(),
|
|
2329
|
+
/** True on a kvbox-server with `POST /machines/from-backup` (#141, `kvbox new --from-backup`); older ones omit it. */
|
|
2330
|
+
fromBackup: z5.boolean().optional()
|
|
2317
2331
|
});
|
|
2318
2332
|
var ImageSchema = z5.object({ name: z5.string(), version: z5.string().nullable() });
|
|
2319
2333
|
var ImageIndexSchema = z5.object({
|
|
@@ -3438,7 +3452,9 @@ async function capacity(p = paths()) {
|
|
|
3438
3452
|
images: await listImages(cfg, p),
|
|
3439
3453
|
...await backupStoreCapacity(p).then((store) => store ? { backupStore: store } : {}),
|
|
3440
3454
|
// Backups and restores here reach a store by its certificate's pin (core/storeFetch.ts).
|
|
3441
|
-
storeClient: true
|
|
3455
|
+
storeClient: true,
|
|
3456
|
+
// A new box on a backup's disk (#141): the control plane places `new --from-backup` only here.
|
|
3457
|
+
fromBackup: true
|
|
3442
3458
|
};
|
|
3443
3459
|
}
|
|
3444
3460
|
function gb(mib) {
|
|
@@ -4469,6 +4485,9 @@ var setMachine = locked(async function setMachine2(ref, req, p = paths()) {
|
|
|
4469
4485
|
return toMachine(next, p);
|
|
4470
4486
|
});
|
|
4471
4487
|
|
|
4488
|
+
// src/core/machines/create.ts
|
|
4489
|
+
import { rename as rename6, stat as stat3 } from "node:fs/promises";
|
|
4490
|
+
|
|
4472
4491
|
// src/core/machines/newbox.ts
|
|
4473
4492
|
import { mkdir as mkdir5, rm as rm7 } from "node:fs/promises";
|
|
4474
4493
|
var NAME_RE = /^[a-z0-9][a-z0-9-]{0,11}$/;
|
|
@@ -4630,12 +4649,27 @@ async function restoreFrom(from, owner, req, cfg, p) {
|
|
|
4630
4649
|
async function currentBaseVersion(p = paths()) {
|
|
4631
4650
|
return (await currentImage(void 0, await loadConfig(p), p)).version;
|
|
4632
4651
|
}
|
|
4633
|
-
|
|
4634
|
-
const
|
|
4635
|
-
if (req.from !== void 0) return restoreMachine(req.from, owner, req, cfg, p);
|
|
4636
|
-
const sizeName = req.size ?? cfg.defaultSize;
|
|
4652
|
+
function sizeOf(req, defaultSize, diskBytes) {
|
|
4653
|
+
const sizeName = req.size ?? defaultSize;
|
|
4637
4654
|
if (!isSizeName(sizeName)) throw new KvboxError(`unknown size "${sizeName}"; use one of ${Object.keys(SIZES).join(", ")}`);
|
|
4638
4655
|
const size2 = SIZES[sizeName];
|
|
4656
|
+
if (diskBytes !== void 0 && diskBytes > size2.diskGib * 1024 ** 3) {
|
|
4657
|
+
const fits = SIZE_NAMES.find((n) => diskBytes <= SIZES[n].diskGib * 1024 ** 3);
|
|
4658
|
+
throw new KvboxError(`this backup's disk is ${Math.ceil(diskBytes / 1024 ** 3)} GB, more than a ${sizeName} box's ${size2.diskGib} GB${fits ? `; use --size ${fits}` : ""}`);
|
|
4659
|
+
}
|
|
4660
|
+
return size2;
|
|
4661
|
+
}
|
|
4662
|
+
async function checkNewBox(req, owner, diskBytes, p = paths()) {
|
|
4663
|
+
const cfg = await loadConfig(p);
|
|
4664
|
+
const size2 = sizeOf(req, cfg.defaultSize, diskBytes);
|
|
4665
|
+
assertAssets(cfg);
|
|
4666
|
+
await assertRoomFor(size2.vcpu, size2.memMib, req.force ?? false, { owner }, "new", p);
|
|
4667
|
+
if (req.name !== void 0) pickName(req.name, new Set((await readAllMeta(p)).filter((m) => m.owner === owner).map((m) => m.name)));
|
|
4668
|
+
}
|
|
4669
|
+
async function createMachine(req, owner, p = paths(), from) {
|
|
4670
|
+
const cfg = await loadConfig(p);
|
|
4671
|
+
if (req.from !== void 0) return restoreMachine(req.from, owner, req, cfg, p);
|
|
4672
|
+
const size2 = sizeOf(req, cfg.defaultSize, from ? (await stat3(from.disk)).size : void 0);
|
|
4639
4673
|
assertAssets(cfg);
|
|
4640
4674
|
await assertRoomFor(size2.vcpu, size2.memMib, req.force ?? false, { owner }, "new", p);
|
|
4641
4675
|
const sshKeys = isCloudBox(owner) && req.sshKeys !== void 0 && req.sshKeys.length > 0 ? req.sshKeys : void 0;
|
|
@@ -4644,26 +4678,29 @@ async function createMachine(req, owner, p = paths()) {
|
|
|
4644
4678
|
const name = pickName(req.name, new Set(existing.filter((m) => m.owner === owner).map((m) => m.name)));
|
|
4645
4679
|
const id = newBoxId(idsInUse(existing));
|
|
4646
4680
|
const { ip, mac } = driver().net.allocate(cfg, existing.map((m) => m.ip));
|
|
4647
|
-
const image = imageKey(req.image);
|
|
4648
|
-
const base = await currentImage(image, cfg, p);
|
|
4681
|
+
const image = imageKey(from ? from.image : req.image);
|
|
4682
|
+
const base = from ? { version: from.base, file: imageFile(image, from.base, p) } : await currentImage(image, cfg, p);
|
|
4649
4683
|
await assertOverlayCapable(base.file, image, base.version);
|
|
4650
4684
|
const manifest = await imageManifest(base.file);
|
|
4651
|
-
const env = imageEnv(manifest, name, req.env ?? [], (await readOwnerEnv(owner, p).catch(() => null))?.vars ?? []);
|
|
4685
|
+
const env = from ? null : imageEnv(manifest, name, req.env ?? [], (await readOwnerEnv(owner, p).catch(() => null))?.vars ?? []);
|
|
4652
4686
|
const now = (/* @__PURE__ */ new Date()).toISOString();
|
|
4653
4687
|
const autoHibernateSec = autoHibernateFor(req, cfg, manifest);
|
|
4654
|
-
const vars = env.vars.map((v) => ({ ...v, secret: env.secrets.includes(v.name), updatedAt: now }));
|
|
4688
|
+
const vars = env === null ? from?.vars ?? [] : env.vars.map((v) => ({ ...v, secret: env.secrets.includes(v.name), updatedAt: now }));
|
|
4655
4689
|
const machine = await bootInto(
|
|
4656
4690
|
{ id, name, owner, ip, mac, vcpu: size2.vcpu, memMib: size2.memMib, base: base.version, image, sshKeys },
|
|
4657
4691
|
// The image is shared read-only by every box; only the box's own writes land in its overlay.
|
|
4658
4692
|
async (m) => {
|
|
4659
|
-
|
|
4693
|
+
if (from) {
|
|
4694
|
+
await rename6(from.disk, m.overlay);
|
|
4695
|
+
if ((await stat3(m.overlay)).size < size2.diskGib * 1024 ** 3) await growDisk(m.overlay, size2.diskGib);
|
|
4696
|
+
} else await createOverlay(m.overlay, size2.diskGib);
|
|
4660
4697
|
if (vars.length > 0) await writeBoxEnv({ owner, name }, vars, p);
|
|
4661
4698
|
},
|
|
4662
|
-
{ source: "base", autoHibernateSec, idleCheck: manifest.idle, url: maskedUrl(manifest, vars) },
|
|
4699
|
+
{ source: from ? `backup:${from.id}` : "base", autoHibernateSec, idleCheck: manifest.idle, url: maskedUrl(manifest, vars) },
|
|
4663
4700
|
cfg,
|
|
4664
4701
|
p
|
|
4665
4702
|
);
|
|
4666
|
-
return env
|
|
4703
|
+
return env?.connect === void 0 ? machine : { ...machine, connect: env.connect };
|
|
4667
4704
|
}
|
|
4668
4705
|
|
|
4669
4706
|
// src/core/machines/fork.ts
|
|
@@ -4672,7 +4709,7 @@ import { rm as rm9 } from "node:fs/promises";
|
|
|
4672
4709
|
|
|
4673
4710
|
// src/core/snapshots.ts
|
|
4674
4711
|
import { existsSync as existsSync11 } from "node:fs";
|
|
4675
|
-
import { link as link2, mkdir as mkdir6, readdir as readdir5, rename as
|
|
4712
|
+
import { link as link2, mkdir as mkdir6, readdir as readdir5, rename as rename7, rm as rm8, stat as stat4 } from "node:fs/promises";
|
|
4676
4713
|
var NAME_RE2 = /^[a-z0-9][a-z0-9._-]{0,63}$/;
|
|
4677
4714
|
function toSnapshot(meta) {
|
|
4678
4715
|
return {
|
|
@@ -4702,7 +4739,7 @@ async function listSnapshots(scope, p = paths()) {
|
|
|
4702
4739
|
async function usedBytes(dir) {
|
|
4703
4740
|
let total = 0;
|
|
4704
4741
|
for (const entry of await readdir5(dir)) {
|
|
4705
|
-
total += (await
|
|
4742
|
+
total += (await stat4(`${dir}/${entry}`)).blocks * 512;
|
|
4706
4743
|
}
|
|
4707
4744
|
return total;
|
|
4708
4745
|
}
|
|
@@ -4765,11 +4802,11 @@ async function swapIn(ref, from, boxId, p) {
|
|
|
4765
4802
|
await withSnapshotLock(ref, async () => {
|
|
4766
4803
|
await rm8(old, { recursive: true, force: true });
|
|
4767
4804
|
const hadOld = existsSync11(to);
|
|
4768
|
-
if (hadOld) await
|
|
4805
|
+
if (hadOld) await rename7(to, old);
|
|
4769
4806
|
try {
|
|
4770
|
-
await
|
|
4807
|
+
await rename7(from, to);
|
|
4771
4808
|
} catch (err) {
|
|
4772
|
-
if (hadOld) await
|
|
4809
|
+
if (hadOld) await rename7(old, to);
|
|
4773
4810
|
throw err;
|
|
4774
4811
|
}
|
|
4775
4812
|
});
|
|
@@ -4814,7 +4851,7 @@ async function mergeOntoPrevious(previous, diffFile, editor) {
|
|
|
4814
4851
|
const merged = `${diffFile}.merged`;
|
|
4815
4852
|
await run(["cp", "--sparse=always", "--reflink=auto", previous, merged]);
|
|
4816
4853
|
await run([editor, "edit-memory", "rebase", "--memory-path", merged, "--diff-path", diffFile]);
|
|
4817
|
-
await
|
|
4854
|
+
await rename7(merged, diffFile);
|
|
4818
4855
|
}
|
|
4819
4856
|
async function deleteSnapshot(ref, p = paths()) {
|
|
4820
4857
|
const dir = p.snapshot(ref.owner, ref.name).dir;
|
|
@@ -5059,7 +5096,7 @@ var wakeMachine = locked(async function wakeMachine2(ref, p = paths(), force = f
|
|
|
5059
5096
|
|
|
5060
5097
|
// src/core/machines/volumes.ts
|
|
5061
5098
|
import { statSync as statSync5 } from "node:fs";
|
|
5062
|
-
import { rename as
|
|
5099
|
+
import { rename as rename8, rm as rm12 } from "node:fs/promises";
|
|
5063
5100
|
var attachVolume = locked(async function attachVolume2(ref, volume, p = paths()) {
|
|
5064
5101
|
const cfg = await loadConfig(p);
|
|
5065
5102
|
const claimed = await withHostLock("volumes", async () => {
|
|
@@ -5114,7 +5151,7 @@ async function replaceVolumeDisk(volume, file, p = paths()) {
|
|
|
5114
5151
|
await grow();
|
|
5115
5152
|
const swap = async () => {
|
|
5116
5153
|
await grow();
|
|
5117
|
-
await
|
|
5154
|
+
await rename8(file, p.volume(volume.owner, volume.name).disk);
|
|
5118
5155
|
};
|
|
5119
5156
|
const holder = await attachedBox(volume, p);
|
|
5120
5157
|
if (holder === null) {
|
|
@@ -5138,7 +5175,7 @@ async function replaceVolumeDisk(volume, file, p = paths()) {
|
|
|
5138
5175
|
// src/core/backups.ts
|
|
5139
5176
|
import { randomBytes as randomBytes5 } from "node:crypto";
|
|
5140
5177
|
import { existsSync as existsSync13 } from "node:fs";
|
|
5141
|
-
import { mkdir as mkdir8, open, readdir as readdir6, readFile as readFile9, rm as rm13, stat as
|
|
5178
|
+
import { mkdir as mkdir8, open, readdir as readdir6, readFile as readFile9, rm as rm13, stat as stat5, statfs as statfs3, writeFile as writeFile7 } from "node:fs/promises";
|
|
5142
5179
|
import { basename as basename2, join as join3 } from "node:path";
|
|
5143
5180
|
import { setTimeout as sleep4 } from "node:timers/promises";
|
|
5144
5181
|
|
|
@@ -5282,7 +5319,7 @@ async function readBox2(ref, p) {
|
|
|
5282
5319
|
}
|
|
5283
5320
|
}
|
|
5284
5321
|
async function chunkNames(file, keys) {
|
|
5285
|
-
const { size: size2 } = await
|
|
5322
|
+
const { size: size2 } = await stat5(file);
|
|
5286
5323
|
const chunks = [];
|
|
5287
5324
|
const fh = await open(file, "r");
|
|
5288
5325
|
try {
|
|
@@ -5422,7 +5459,7 @@ async function uploadOnce(id, req, signal, p) {
|
|
|
5422
5459
|
if (index === void 0) throw new KvboxError(`backup ${id} has no chunk ${name.slice(0, 12)}`);
|
|
5423
5460
|
return { name, url, index };
|
|
5424
5461
|
});
|
|
5425
|
-
const { size: size2 } = await
|
|
5462
|
+
const { size: size2 } = await stat5(s.disk);
|
|
5426
5463
|
const fetchChunk = await chunkFetcher(req.pin, work[0]?.url, p);
|
|
5427
5464
|
const fh = await open(s.disk, "r");
|
|
5428
5465
|
let bytes = 0;
|
|
@@ -5515,7 +5552,7 @@ async function restoreOnce(ref, req, p) {
|
|
|
5515
5552
|
}
|
|
5516
5553
|
const keys = backupKeys(req.key);
|
|
5517
5554
|
const m = p.machine(ref.owner, ref.name);
|
|
5518
|
-
const current2 = (await
|
|
5555
|
+
const current2 = (await stat5(m.overlay)).size;
|
|
5519
5556
|
if (req.sizeBytes > current2) {
|
|
5520
5557
|
const gb2 = (n) => `${Math.ceil(n / 1024 ** 3)}GB`;
|
|
5521
5558
|
throw new KvboxError(`this backup's disk is ${gb2(req.sizeBytes)} and box ${shown(ref)}'s is ${gb2(current2)}: kvbox vm rescale ${shown(ref)} --disk ${gb2(req.sizeBytes)} first`);
|
|
@@ -5537,6 +5574,48 @@ async function restoreOnce(ref, req, p) {
|
|
|
5537
5574
|
await rm13(envFile, { force: true });
|
|
5538
5575
|
}
|
|
5539
5576
|
}
|
|
5577
|
+
async function createFromBackup(req, owner, p = paths()) {
|
|
5578
|
+
const { backup, ...create } = req;
|
|
5579
|
+
checkId(backup.id);
|
|
5580
|
+
const base = await baseFor(backup, p);
|
|
5581
|
+
await checkNewBox(create, owner, backup.sizeBytes, p);
|
|
5582
|
+
const keys = backupKeys(backup.key);
|
|
5583
|
+
const dir = join3(p.backups, `new-${randomBytes5(4).toString("hex")}`);
|
|
5584
|
+
await mkdir8(dir, { recursive: true, mode: 448 });
|
|
5585
|
+
try {
|
|
5586
|
+
let vars = null;
|
|
5587
|
+
if (backup.env !== null) {
|
|
5588
|
+
const envFile = join3(dir, "env.json");
|
|
5589
|
+
await writeFile7(envFile, openText(keys, backup.env, `env:${backup.id}`), { mode: 384 });
|
|
5590
|
+
vars = await readBoxEnvFile(envFile);
|
|
5591
|
+
checkBoxEnv(vars);
|
|
5592
|
+
}
|
|
5593
|
+
await assertRoom(dir, backup.chunks.length * CHUNK_SIZE, `a box from backup ${backup.id}`, p);
|
|
5594
|
+
const disk = join3(dir, "disk.ext4");
|
|
5595
|
+
await downloadDisk(disk, backup, p);
|
|
5596
|
+
const sent = create;
|
|
5597
|
+
return await createMachine(sent, owner, p, { disk, base, image: backup.image, vars, id: backup.id });
|
|
5598
|
+
} finally {
|
|
5599
|
+
await rm13(dir, { recursive: true, force: true });
|
|
5600
|
+
}
|
|
5601
|
+
}
|
|
5602
|
+
async function baseFor(backup, p) {
|
|
5603
|
+
if (existsSync13(imageFile(backup.image, backup.base, p))) return backup.base;
|
|
5604
|
+
const current2 = (await currentImage(imageKey(backup.image), await loadConfig(p), p)).version;
|
|
5605
|
+
if (compareVersions(current2, backup.base) < 0) {
|
|
5606
|
+
throw new KvboxError(`this backup stacks on ${imageLabel(backup.image)} ${backup.base}, newer than this host's ${current2}; the host needs updating first`);
|
|
5607
|
+
}
|
|
5608
|
+
return current2;
|
|
5609
|
+
}
|
|
5610
|
+
function compareVersions(a, b) {
|
|
5611
|
+
const parts = (v) => v.replace(/^v/, "").split(/[.-]/).map((x2) => Number.parseInt(x2, 10) || 0);
|
|
5612
|
+
const [x, y] = [parts(a), parts(b)];
|
|
5613
|
+
for (let i = 0; i < Math.max(x.length, y.length); i++) {
|
|
5614
|
+
const d = (x[i] ?? 0) - (y[i] ?? 0);
|
|
5615
|
+
if (d !== 0) return d;
|
|
5616
|
+
}
|
|
5617
|
+
return 0;
|
|
5618
|
+
}
|
|
5540
5619
|
async function restoreVolumeBackup(ref, req, p = paths()) {
|
|
5541
5620
|
checkId(req.id);
|
|
5542
5621
|
const k = `volume:${refKey(ref)}`;
|
|
@@ -6122,7 +6201,7 @@ function startRelayWatch(opts = {}) {
|
|
|
6122
6201
|
// src/daemon.ts
|
|
6123
6202
|
import { spawn as spawn3 } from "node:child_process";
|
|
6124
6203
|
import { accessSync, constants as constants2, openSync as openSync5, readFileSync as readFileSync3, statSync as statSync6 } from "node:fs";
|
|
6125
|
-
import { mkdir as mkdir11, readFile as readFile11, stat as
|
|
6204
|
+
import { mkdir as mkdir11, readFile as readFile11, stat as stat6 } from "node:fs/promises";
|
|
6126
6205
|
import { userInfo as userInfo3 } from "node:os";
|
|
6127
6206
|
import { setTimeout as sleep5 } from "node:timers/promises";
|
|
6128
6207
|
|
|
@@ -6263,7 +6342,7 @@ async function stopDaemon(pid, timeoutMs = 1e4) {
|
|
|
6263
6342
|
}
|
|
6264
6343
|
}
|
|
6265
6344
|
async function answering(p = paths()) {
|
|
6266
|
-
if (!await
|
|
6345
|
+
if (!await stat6(p.serverSock).then(() => true, () => false)) return null;
|
|
6267
6346
|
const probe = new SocketClient(p.serverSock);
|
|
6268
6347
|
try {
|
|
6269
6348
|
const health = await probe.health();
|
|
@@ -6301,7 +6380,7 @@ async function restartDaemon(p = paths()) {
|
|
|
6301
6380
|
return startDetached(p);
|
|
6302
6381
|
}
|
|
6303
6382
|
async function startUnit(p, timeoutMs) {
|
|
6304
|
-
const logFrom = await
|
|
6383
|
+
const logFrom = await stat6(p.serverLog).then((s) => s.size, () => 0);
|
|
6305
6384
|
const res = startUnitProcess();
|
|
6306
6385
|
if (!res.ok) {
|
|
6307
6386
|
const why = res.out || await lastErrorLine(p.serverLog, logFrom) || "systemctl start failed";
|
|
@@ -6322,7 +6401,7 @@ async function serving(p, timeoutMs, check) {
|
|
|
6322
6401
|
const deadline = Date.now() + timeoutMs;
|
|
6323
6402
|
while (Date.now() < deadline) {
|
|
6324
6403
|
await check?.();
|
|
6325
|
-
if (await
|
|
6404
|
+
if (await stat6(p.serverSock).then(() => true, () => false)) {
|
|
6326
6405
|
const probe = new SocketClient(p.serverSock);
|
|
6327
6406
|
try {
|
|
6328
6407
|
await probe.health();
|
|
@@ -6426,7 +6505,7 @@ function versionOf(srcFileUrl, injected) {
|
|
|
6426
6505
|
}
|
|
6427
6506
|
|
|
6428
6507
|
// src/version.ts
|
|
6429
|
-
var VERSION = versionOf(import.meta.url, true ? "0.6.
|
|
6508
|
+
var VERSION = versionOf(import.meta.url, true ? "0.6.12" : void 0);
|
|
6430
6509
|
|
|
6431
6510
|
// src/caller.ts
|
|
6432
6511
|
import { createHash as createHash3, timingSafeEqual } from "node:crypto";
|
|
@@ -6537,6 +6616,11 @@ async function startUpdate(version, p = paths(), deps = {}) {
|
|
|
6537
6616
|
import { Hono } from "hono";
|
|
6538
6617
|
function backupMachineRoutes() {
|
|
6539
6618
|
const r = new Hono();
|
|
6619
|
+
r.post("/from-backup", async (c) => {
|
|
6620
|
+
const body = HostFromBackupRequest.safeParse(await c.req.json().catch(() => ({})));
|
|
6621
|
+
if (!body.success) throw new KvboxError("from-backup: expected a new box's fields and {backup: {id, key, sizeBytes, base, env, chunks}}");
|
|
6622
|
+
return c.json(await createFromBackup(body.data, c.get("owner")), 201);
|
|
6623
|
+
});
|
|
6540
6624
|
r.post("/:name/backup", async (c) => {
|
|
6541
6625
|
const body = HostBackupRequest.safeParse(await c.req.json().catch(() => ({})));
|
|
6542
6626
|
if (!body.success) throw new KvboxError("backup: expected {id, key}");
|
|
@@ -7457,7 +7541,7 @@ var leaveCommand = new Command6("leave").description("take this host out from be
|
|
|
7457
7541
|
});
|
|
7458
7542
|
|
|
7459
7543
|
// src/commands/logs.ts
|
|
7460
|
-
import { open as open2, readFile as readFile14, stat as
|
|
7544
|
+
import { open as open2, readFile as readFile14, stat as stat7 } from "node:fs/promises";
|
|
7461
7545
|
import { setTimeout as sleep6 } from "node:timers/promises";
|
|
7462
7546
|
import { Command as Command7 } from "commander";
|
|
7463
7547
|
var logsCommand = new Command7("logs").description("show the daemon's log ($KVBOX_HOME/server.log); -f keeps printing as it grows").option("-n, --lines <n>", "how many lines to show from the end", "50").option("-f, --follow", "keep printing new lines until Ctrl-C").action(async (opts) => {
|
|
@@ -7484,9 +7568,9 @@ function tailLines(text, n) {
|
|
|
7484
7568
|
}
|
|
7485
7569
|
async function follow(path, write, opts = {}) {
|
|
7486
7570
|
const intervalMs = opts.intervalMs ?? 250;
|
|
7487
|
-
let offset = opts.from ?? await
|
|
7571
|
+
let offset = opts.from ?? await stat7(path).then((s) => s.size, () => 0);
|
|
7488
7572
|
while (!opts.signal?.aborted) {
|
|
7489
|
-
const size2 = await
|
|
7573
|
+
const size2 = await stat7(path).then((s) => s.size, () => 0);
|
|
7490
7574
|
if (size2 < offset) offset = 0;
|
|
7491
7575
|
if (size2 > offset) {
|
|
7492
7576
|
const fh = await open2(path, "r");
|
|
@@ -7533,7 +7617,7 @@ var relayCommand = new Command9("relay").description("reach the internet through
|
|
|
7533
7617
|
import { Command as Command11 } from "commander";
|
|
7534
7618
|
|
|
7535
7619
|
// src/core/realm.ts
|
|
7536
|
-
import { lstat as lstat2, readFile as readFile15, readdir as readdir7, readlink, rename as
|
|
7620
|
+
import { lstat as lstat2, readFile as readFile15, readdir as readdir7, readlink, rename as rename9, rm as rm16, symlink as symlink2, writeFile as writeFile11 } from "node:fs/promises";
|
|
7537
7621
|
import { z as z9 } from "zod";
|
|
7538
7622
|
var esc = (s) => s.replace(/[.*+?^${}()|[\]\\]/g, "\\$&");
|
|
7539
7623
|
async function ownersIn(root, from, suffix = "") {
|
|
@@ -7612,11 +7696,11 @@ async function countRealm(from, to, p = paths()) {
|
|
|
7612
7696
|
async function writeJson(file, value) {
|
|
7613
7697
|
const tmp = `${file}.tmp`;
|
|
7614
7698
|
await writeFile11(tmp, JSON.stringify(value, null, 2) + "\n");
|
|
7615
|
-
await
|
|
7699
|
+
await rename9(tmp, file);
|
|
7616
7700
|
}
|
|
7617
7701
|
async function move(entries, metaOf, ownerDir, owners, to) {
|
|
7618
7702
|
for (const e of entries) await writeJson(metaOf(e.oldOwner, e.name), e.meta);
|
|
7619
|
-
for (const o of owners) await
|
|
7703
|
+
for (const o of owners) await rename9(ownerDir(o), ownerDir(to(o)));
|
|
7620
7704
|
}
|
|
7621
7705
|
async function renameRealm(from, to, p = paths()) {
|
|
7622
7706
|
const planned = await plan(from, to, p);
|
|
@@ -7635,7 +7719,7 @@ async function renameRealm(from, to, p = paths()) {
|
|
|
7635
7719
|
await symlink2(p.volume(box.meta.owner, volume).disk, link4);
|
|
7636
7720
|
}
|
|
7637
7721
|
}
|
|
7638
|
-
for (const o of planned.envOwners) await
|
|
7722
|
+
for (const o of planned.envOwners) await rename9(p.ownerEnv(o), p.ownerEnv(owner(o)));
|
|
7639
7723
|
renameLedgerRealm(from, to, p);
|
|
7640
7724
|
closeDb(p);
|
|
7641
7725
|
const cfg = await loadConfig(p);
|