@kvbox/server 0.6.21 → 0.6.22
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 +96 -13
- package/dist/kvbox-server.js.map +1 -1
- package/package.json +1 -1
- package/scripts/net-setup.sh +99 -4
package/dist/kvbox-server.js
CHANGED
|
@@ -735,6 +735,22 @@ async function openFarLink(link4, cfg) {
|
|
|
735
735
|
async function closeFarLink(link4, cfg) {
|
|
736
736
|
await netRun(cfg, ["farlink", "rm", link4.box, link4.fromIp, link4.standIn, String(link4.port)]);
|
|
737
737
|
}
|
|
738
|
+
function carriesClusters() {
|
|
739
|
+
try {
|
|
740
|
+
return readFileSync(netSetupScript(), "utf8").includes("cluster add");
|
|
741
|
+
} catch {
|
|
742
|
+
return false;
|
|
743
|
+
}
|
|
744
|
+
}
|
|
745
|
+
async function joinCluster(member, cfg) {
|
|
746
|
+
await netRun(cfg, ["cluster", "add", member.box, member.ip, member.cluster]).catch((err) => {
|
|
747
|
+
if (!err.message.includes("usage:")) throw err;
|
|
748
|
+
throw new KvboxError("this host's network setup is from before clusters; its operator finishes the update by running install-server.sh on it (the root step)");
|
|
749
|
+
});
|
|
750
|
+
}
|
|
751
|
+
async function leaveCluster(member, cfg) {
|
|
752
|
+
await netRun(cfg, ["cluster", "rm", member.box, member.ip]);
|
|
753
|
+
}
|
|
738
754
|
async function closeProxy(proxy, cfg) {
|
|
739
755
|
await netRun(cfg, ["proxy", "rm", ...proxyArgs(proxy)]);
|
|
740
756
|
}
|
|
@@ -1111,6 +1127,9 @@ var firecrackerDriver = {
|
|
|
1111
1127
|
farListenHost: (cfg) => cfg.gateway,
|
|
1112
1128
|
openFarLink,
|
|
1113
1129
|
closeFarLink,
|
|
1130
|
+
carriesClusters,
|
|
1131
|
+
joinCluster,
|
|
1132
|
+
leaveCluster,
|
|
1114
1133
|
relay: netSetup,
|
|
1115
1134
|
setupScript: netSetupScript
|
|
1116
1135
|
},
|
|
@@ -1410,7 +1429,13 @@ var MachineMetaSchema = z3.object({
|
|
|
1410
1429
|
* for one each time it needs it (core/machines/integrations.ts) and is answered only for a name in this
|
|
1411
1430
|
* list. Plain strings, so a name a newer server wrote still reads. Absent: none.
|
|
1412
1431
|
*/
|
|
1413
|
-
integrations: z3.array(z3.string()).optional()
|
|
1432
|
+
integrations: z3.array(z3.string()).optional(),
|
|
1433
|
+
/**
|
|
1434
|
+
* The cluster the box is a part of (kvbox k8s, #215), given at create by the control plane and never
|
|
1435
|
+
* changed: while it runs, it and the other boxes of its owner with the same id reach each other on any
|
|
1436
|
+
* port (core/machines/cluster.ts). A fork or a box from a snapshot never has it. Absent: none.
|
|
1437
|
+
*/
|
|
1438
|
+
cluster: z3.string().optional()
|
|
1414
1439
|
});
|
|
1415
1440
|
var VolumeMetaSchema = z3.object({
|
|
1416
1441
|
/** Host-unique like a box id (#91): the ledger's key, so a name removed and made again is two volumes. */
|
|
@@ -2478,6 +2503,7 @@ var HostEnvRequest = z6.object({
|
|
|
2478
2503
|
vars: z6.array(z6.object({ name: z6.string(), value: z6.string() }))
|
|
2479
2504
|
});
|
|
2480
2505
|
var HostEnvVersionsSchema = z6.array(z6.object({ owner: z6.string(), version: z6.number().int() }));
|
|
2506
|
+
var CLUSTER_ID_RE = /^[a-z0-9][a-z0-9_-]{0,39}$/;
|
|
2481
2507
|
var CreateMachineRequest = z6.object({
|
|
2482
2508
|
name: z6.string().optional(),
|
|
2483
2509
|
/** Ignored over the local socket (the caller is the host owner); the control plane sets it over HTTPS. */
|
|
@@ -2521,7 +2547,13 @@ var CreateMachineRequest = z6.object({
|
|
|
2521
2547
|
* The integrations turned on for the box (#205, `kvbox new --integration github`). Absent: none, or with
|
|
2522
2548
|
* `from` / `fromBackup` the list the snapshot or the backup was taken with.
|
|
2523
2549
|
*/
|
|
2524
|
-
integrations: IntegrationListSchema.optional()
|
|
2550
|
+
integrations: IntegrationListSchema.optional(),
|
|
2551
|
+
/**
|
|
2552
|
+
* The cluster the box is a part of (kvbox k8s, #215): on its host, the boxes of one owner with the same id
|
|
2553
|
+
* reach each other on any port. An id the control plane makes, never a user's: the control plane refuses
|
|
2554
|
+
* it from anyone it did not make it for, and a host takes it only from a control plane.
|
|
2555
|
+
*/
|
|
2556
|
+
cluster: z6.string().regex(CLUSTER_ID_RE).optional()
|
|
2525
2557
|
});
|
|
2526
2558
|
var HostFromBackupRequest = CreateMachineRequest.omit({ from: true, fromBackup: true, image: true, env: true }).extend({ backup: HostRestoreRequest });
|
|
2527
2559
|
var ForkMachineRequest = z6.object({
|
|
@@ -2618,6 +2650,8 @@ var CapacitySchema = z6.object({
|
|
|
2618
2650
|
integrations: z6.boolean().optional(),
|
|
2619
2651
|
/** True on a kvbox-server that carries a link to a box on another host (#207); the control plane links across hosts only between two such. */
|
|
2620
2652
|
farLinks: z6.boolean().optional(),
|
|
2653
|
+
/** True on a host whose network puts the boxes of one cluster in reach of each other (#215); the control plane places a cluster's box only on such a host. */
|
|
2654
|
+
clusters: z6.boolean().optional(),
|
|
2621
2655
|
/** The integrations this kvbox-server knows (`hostKnowsIntegrations`). Absent with `integrations`: the first one's, `github` alone. */
|
|
2622
2656
|
integrationIds: z6.array(z6.string()).optional()
|
|
2623
2657
|
});
|
|
@@ -3768,6 +3802,8 @@ async function capacity(p = paths()) {
|
|
|
3768
3802
|
// A link to a box on another host is carried here (#207): the control plane links across hosts only between two such.
|
|
3769
3803
|
// Only once the host's privileged network script knows them: an update without its root step does not.
|
|
3770
3804
|
farLinks: driver().net.carriesFarLinks(),
|
|
3805
|
+
// The boxes of a cluster reach each other here (#215), on the same condition: the control plane places a cluster's box only on such a host.
|
|
3806
|
+
clusters: driver().net.carriesClusters(),
|
|
3771
3807
|
// A box's integrations are kept here (#205): the control plane sends a list only to such a host.
|
|
3772
3808
|
integrations: true,
|
|
3773
3809
|
// ...and which (the control plane places a box with `claude` only on a host that names it).
|
|
@@ -4779,6 +4815,42 @@ function pickSshPort(taken) {
|
|
|
4779
4815
|
throw new KvboxError(`no free port left between ${SSH_PORT_MIN} and ${SSH_PORT_MAX} on this host`);
|
|
4780
4816
|
}
|
|
4781
4817
|
|
|
4818
|
+
// src/core/machines/cluster.ts
|
|
4819
|
+
import { createHash as createHash3 } from "node:crypto";
|
|
4820
|
+
var clusterKey = (owner, cluster) => createHash3("sha256").update(`${owner}
|
|
4821
|
+
${cluster}`).digest("hex").slice(0, 16);
|
|
4822
|
+
function requestedCluster(req, owner, fromBackup = false) {
|
|
4823
|
+
if (req.cluster === void 0) return void 0;
|
|
4824
|
+
if (!isCloudBox(owner)) throw new KvboxError("a box is put in a cluster by a control plane; this host's own boxes have none");
|
|
4825
|
+
if (req.from !== void 0 || fromBackup) throw new KvboxError("a box of a cluster is made from its image, never from a snapshot or a backup");
|
|
4826
|
+
if (!driver().net.carriesClusters()) {
|
|
4827
|
+
throw new KvboxError("this host's network setup is from before clusters; its operator finishes the update by running install-server.sh on it (the root step)");
|
|
4828
|
+
}
|
|
4829
|
+
return req.cluster;
|
|
4830
|
+
}
|
|
4831
|
+
async function joinCluster2(meta, cfg) {
|
|
4832
|
+
if (meta.cluster === void 0 || meta.owner === void 0) return;
|
|
4833
|
+
await driver().net.joinCluster({ box: meta.id, ip: meta.ip, cluster: clusterKey(meta.owner, meta.cluster) }, cfg);
|
|
4834
|
+
}
|
|
4835
|
+
async function leaveCluster2(meta, cfg) {
|
|
4836
|
+
await leftCluster(meta, cfg).catch((err) => log.warn(`could not take box ${meta.id} out of its cluster: ${err.message}`));
|
|
4837
|
+
}
|
|
4838
|
+
async function leftCluster(meta, cfg) {
|
|
4839
|
+
if (meta.cluster === void 0) return;
|
|
4840
|
+
await driver().net.leaveCluster({ box: meta.id, ip: meta.ip }, cfg);
|
|
4841
|
+
}
|
|
4842
|
+
async function rejoinClusters(p = paths()) {
|
|
4843
|
+
const cfg = await loadConfig(p);
|
|
4844
|
+
for (const listed of await readAllMeta(p)) {
|
|
4845
|
+
if (!vmAlive(listed) || listed.cluster === void 0) continue;
|
|
4846
|
+
await withBoxLock(listed, async () => {
|
|
4847
|
+
const meta = await readMachineMeta(listed, p).catch(() => null);
|
|
4848
|
+
if (meta === null || !vmAlive(meta)) return;
|
|
4849
|
+
await joinCluster2(meta, cfg).catch((err) => log.warn(`box ${meta.name} is cut off from its cluster: ${err.message}`));
|
|
4850
|
+
});
|
|
4851
|
+
}
|
|
4852
|
+
}
|
|
4853
|
+
|
|
4782
4854
|
// src/core/farTunnel.ts
|
|
4783
4855
|
import { createServer as createServer2 } from "node:net";
|
|
4784
4856
|
import { connect as tlsConnect } from "node:tls";
|
|
@@ -5120,6 +5192,7 @@ async function openPorts(meta, cfg, p) {
|
|
|
5120
5192
|
}
|
|
5121
5193
|
const far = reaching(meta);
|
|
5122
5194
|
if (far) await openFarTargets(far, cfg, p);
|
|
5195
|
+
await joinCluster2(meta, cfg);
|
|
5123
5196
|
}
|
|
5124
5197
|
async function closePorts(meta, cfg) {
|
|
5125
5198
|
for (const proxy of boxProxies(meta)) {
|
|
@@ -5130,6 +5203,7 @@ async function closePorts(meta, cfg) {
|
|
|
5130
5203
|
}
|
|
5131
5204
|
const far = reaching(meta);
|
|
5132
5205
|
if (far) await closeFarTargets(far, cfg);
|
|
5206
|
+
await leaveCluster2(meta, cfg);
|
|
5133
5207
|
}
|
|
5134
5208
|
var reservedPorts = /* @__PURE__ */ new Set();
|
|
5135
5209
|
async function drawPorts(count, p) {
|
|
@@ -5286,7 +5360,7 @@ async function bootInto(box, prepare, origin, cfg, p) {
|
|
|
5286
5360
|
const spec = { ...box, sshPort };
|
|
5287
5361
|
const bootedAt = (/* @__PURE__ */ new Date()).toISOString();
|
|
5288
5362
|
pid = await launch(spec, tap, cfg, keysB64, p);
|
|
5289
|
-
await openPorts(spec, cfg, p);
|
|
5363
|
+
await openPorts({ ...spec, cluster: origin.cluster }, cfg, p);
|
|
5290
5364
|
void pushEnvAfterBoot(spec, pid, m.vsock, p);
|
|
5291
5365
|
const meta = { ...spec, pid, bootId: currentBootId(), bootedAt, createdAt: (/* @__PURE__ */ new Date()).toISOString(), ...origin };
|
|
5292
5366
|
await writeMachineMeta(meta, p);
|
|
@@ -5296,7 +5370,7 @@ async function bootInto(box, prepare, origin, cfg, p) {
|
|
|
5296
5370
|
} catch (err) {
|
|
5297
5371
|
if (pid !== void 0) killQuietly(pid);
|
|
5298
5372
|
await stopListeningForGuest(box, p);
|
|
5299
|
-
await closePorts({ id, ip, sshPort }, cfg);
|
|
5373
|
+
await closePorts({ id, ip, sshPort, cluster: origin.cluster }, cfg);
|
|
5300
5374
|
if (tapCreated) await driver().net.remove(id, cfg).catch(() => void 0);
|
|
5301
5375
|
await rm6(m.dir, { recursive: true, force: true });
|
|
5302
5376
|
boxListChanged();
|
|
@@ -5356,7 +5430,7 @@ async function restoreInto(box, prepare, origin, cfg, p, keysOrFail) {
|
|
|
5356
5430
|
import { rm as rm7 } from "node:fs/promises";
|
|
5357
5431
|
|
|
5358
5432
|
// src/core/machines/links.ts
|
|
5359
|
-
import { createHash as
|
|
5433
|
+
import { createHash as createHash4, timingSafeEqual } from "node:crypto";
|
|
5360
5434
|
function linkSource(req, caller, target) {
|
|
5361
5435
|
if (req.fromOwner !== void 0) return { owner: ownerFor(req.fromOwner, caller), name: req.from, display: `${req.fromOwner}/${req.from}` };
|
|
5362
5436
|
const ref = parseRef(req.from, caller);
|
|
@@ -5389,7 +5463,7 @@ var addLink = locked(async function addLink2(ref, req, caller, p = paths()) {
|
|
|
5389
5463
|
}
|
|
5390
5464
|
return toMachine(next, p);
|
|
5391
5465
|
});
|
|
5392
|
-
var keyHash = (key) =>
|
|
5466
|
+
var keyHash = (key) => createHash4("sha256").update(key).digest("hex");
|
|
5393
5467
|
async function addFarLink(meta, from, port, key, p) {
|
|
5394
5468
|
if (realmOf(meta.owner) === null || realmOf(from.owner) !== realmOf(meta.owner)) {
|
|
5395
5469
|
throw new KvboxError(`a link from a box on another host joins two boxes of one control plane; box ${shown(from)} and box ${meta.name} are not`);
|
|
@@ -5535,7 +5609,10 @@ var destroyBox = locked(async function destroyBox2(ref, p = paths()) {
|
|
|
5535
5609
|
const meta = await readMeta(ref, p);
|
|
5536
5610
|
const cfg = await loadConfig(p);
|
|
5537
5611
|
await shutdown2(meta, cfg, p, 2e3);
|
|
5538
|
-
await
|
|
5612
|
+
await (async () => {
|
|
5613
|
+
await closeLinksFrom(meta, cfg, p);
|
|
5614
|
+
await leftCluster(meta, cfg);
|
|
5615
|
+
})().catch((err) => {
|
|
5539
5616
|
recordState(meta, "stopped", "stop", p);
|
|
5540
5617
|
throw err;
|
|
5541
5618
|
});
|
|
@@ -5625,7 +5702,10 @@ var replaceDisk = locked(async function replaceDisk2(ref, file, disk, p = paths(
|
|
|
5625
5702
|
var setMachine = locked(async function setMachine2(ref, req, p = paths()) {
|
|
5626
5703
|
const meta = await readMeta(ref, p);
|
|
5627
5704
|
const next = { ...meta };
|
|
5628
|
-
if (req.autoHibernateSec !== void 0)
|
|
5705
|
+
if (req.autoHibernateSec !== void 0) {
|
|
5706
|
+
if (req.autoHibernateSec > 0 && meta.cluster !== void 0) throw new KvboxError(`box ${shown(ref)} is part of a cluster, whose boxes stay up; it cannot hibernate by itself`);
|
|
5707
|
+
next.autoHibernateSec = req.autoHibernateSec || void 0;
|
|
5708
|
+
}
|
|
5629
5709
|
if (req.idleCheck !== void 0) next.idleCheck = req.idleCheck || void 0;
|
|
5630
5710
|
if (req.integrations !== void 0) {
|
|
5631
5711
|
next.integrations = requestedIntegrations(req.integrations);
|
|
@@ -5717,6 +5797,7 @@ async function checkNewBox(req, owner, diskBytes, p = paths()) {
|
|
|
5717
5797
|
}
|
|
5718
5798
|
async function createMachine(req, owner, p = paths(), from) {
|
|
5719
5799
|
const cfg = await loadConfig(p);
|
|
5800
|
+
const cluster = requestedCluster(req, owner, from !== void 0);
|
|
5720
5801
|
if (req.from !== void 0) return restoreMachine(req.from, owner, req, cfg, p);
|
|
5721
5802
|
const size2 = sizeOf(req, cfg.defaultSize, from ? (await stat3(from.disk)).size : void 0);
|
|
5722
5803
|
assertAssets(cfg);
|
|
@@ -5733,7 +5814,7 @@ async function createMachine(req, owner, p = paths(), from) {
|
|
|
5733
5814
|
const manifest = await imageManifest(base.file);
|
|
5734
5815
|
const env = from ? null : imageEnv(manifest, name, req.env ?? [], (await readOwnerEnv(owner, p).catch(() => null))?.vars ?? []);
|
|
5735
5816
|
const now = (/* @__PURE__ */ new Date()).toISOString();
|
|
5736
|
-
const autoHibernateSec = autoHibernateFor(req, cfg, manifest);
|
|
5817
|
+
const autoHibernateSec = cluster === void 0 ? autoHibernateFor(req, cfg, manifest) : void 0;
|
|
5737
5818
|
const vars = env === null ? from?.vars ?? [] : env.vars.map((v) => ({ ...v, secret: env.secrets.includes(v.name), updatedAt: now }));
|
|
5738
5819
|
const machine = await bootInto(
|
|
5739
5820
|
{ id, name, owner, ip, mac, vcpu: size2.vcpu, memMib: size2.memMib, base: base.version, image, sshKeys },
|
|
@@ -5745,7 +5826,7 @@ async function createMachine(req, owner, p = paths(), from) {
|
|
|
5745
5826
|
} else await createOverlay(m.overlay, size2.diskGib);
|
|
5746
5827
|
if (vars.length > 0) await writeBoxEnv({ owner, name }, vars, p);
|
|
5747
5828
|
},
|
|
5748
|
-
{ source: from ? `backup:${from.id}` : "base", autoHibernateSec, idleCheck: manifest.idle, url: maskedUrl(manifest, vars), integrations: requestedIntegrations(req.integrations, { owner, name, base: base.version, image }) },
|
|
5829
|
+
{ source: from ? `backup:${from.id}` : "base", autoHibernateSec, idleCheck: manifest.idle, url: maskedUrl(manifest, vars), integrations: requestedIntegrations(req.integrations, { owner, name, base: base.version, image }), cluster },
|
|
5749
5830
|
cfg,
|
|
5750
5831
|
p
|
|
5751
5832
|
);
|
|
@@ -6665,6 +6746,7 @@ async function restoreOnce(ref, req, p) {
|
|
|
6665
6746
|
async function createFromBackup(req, owner, p = paths()) {
|
|
6666
6747
|
const { backup, ...create } = req;
|
|
6667
6748
|
checkId(backup.id);
|
|
6749
|
+
requestedCluster(create, owner, true);
|
|
6668
6750
|
const base = await baseFor(backup, p);
|
|
6669
6751
|
await checkNewBox(create, owner, backup.sizeBytes, p);
|
|
6670
6752
|
const keys = backupKeys(backup.key);
|
|
@@ -7809,10 +7891,10 @@ function versionOf(srcFileUrl, injected) {
|
|
|
7809
7891
|
}
|
|
7810
7892
|
|
|
7811
7893
|
// src/version.ts
|
|
7812
|
-
var VERSION = versionOf(import.meta.url, true ? "0.6.
|
|
7894
|
+
var VERSION = versionOf(import.meta.url, true ? "0.6.22" : void 0);
|
|
7813
7895
|
|
|
7814
7896
|
// src/caller.ts
|
|
7815
|
-
import { createHash as
|
|
7897
|
+
import { createHash as createHash5, timingSafeEqual as timingSafeEqual2 } from "node:crypto";
|
|
7816
7898
|
var OWNER_HEADER = "x-kvbox-owner";
|
|
7817
7899
|
function resolveCaller(opts = {}) {
|
|
7818
7900
|
const cp = opts.controlPlane;
|
|
@@ -7866,7 +7948,7 @@ function bearerCheck(token) {
|
|
|
7866
7948
|
};
|
|
7867
7949
|
}
|
|
7868
7950
|
function digest(secret) {
|
|
7869
|
-
return
|
|
7951
|
+
return createHash5("sha256").update(secret).digest();
|
|
7870
7952
|
}
|
|
7871
7953
|
|
|
7872
7954
|
// src/core/update.ts
|
|
@@ -8814,6 +8896,7 @@ var startCommand = new Command("start").description("run the daemon in the foreg
|
|
|
8814
8896
|
const state = await driver().net.prepare(cfg);
|
|
8815
8897
|
log.info(`host network: bridge ${cfg.bridge} ${state}`);
|
|
8816
8898
|
await pinRunningTaps(paths());
|
|
8899
|
+
await rejoinClusters(paths()).catch((err) => log.warn(`running boxes may be cut off from their clusters: ${err.message}`));
|
|
8817
8900
|
await reopenFarTargets(paths()).catch((err) => log.warn(`running boxes cannot reach the boxes they are linked to on other hosts: ${err.message}`));
|
|
8818
8901
|
} catch (err) {
|
|
8819
8902
|
log.warn(`host network not ready (${err.message}); run: kvbox-server net up`);
|