@kvbox/server 0.6.21 → 0.6.23

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.
@@ -230,7 +230,12 @@ var PricingSchema = z.object({
230
230
  * backup you switch on is charged…"): their switch is off until someone turns it on. A managed database's
231
231
  * nightly backup, on from its creation, is included in its price. A price list from before it charges nothing.
232
232
  */
233
- scheduledBackup: z.number().nonnegative().default(0)
233
+ scheduledBackup: z.number().nonnegative().default(0),
234
+ /**
235
+ * A Kubernetes cluster (#222): an hour of one existing, on top of its boxes. A cluster is priced as the
236
+ * boxes it is (its master and its nodes), so this is 0 until a flat fee is wanted.
237
+ */
238
+ clusterHour: z.number().nonnegative().default(0)
234
239
  });
235
240
  var PriceListSchema = PricingSchema.extend({ welcomeCredit: z.number().nonnegative() });
236
241
  var HOUR_MS = 36e5;
@@ -735,6 +740,22 @@ async function openFarLink(link4, cfg) {
735
740
  async function closeFarLink(link4, cfg) {
736
741
  await netRun(cfg, ["farlink", "rm", link4.box, link4.fromIp, link4.standIn, String(link4.port)]);
737
742
  }
743
+ function carriesClusters() {
744
+ try {
745
+ return readFileSync(netSetupScript(), "utf8").includes("cluster add");
746
+ } catch {
747
+ return false;
748
+ }
749
+ }
750
+ async function joinCluster(member, cfg) {
751
+ await netRun(cfg, ["cluster", "add", member.box, member.ip, member.cluster]).catch((err) => {
752
+ if (!err.message.includes("usage:")) throw err;
753
+ 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)");
754
+ });
755
+ }
756
+ async function leaveCluster(member, cfg) {
757
+ await netRun(cfg, ["cluster", "rm", member.box, member.ip]);
758
+ }
738
759
  async function closeProxy(proxy, cfg) {
739
760
  await netRun(cfg, ["proxy", "rm", ...proxyArgs(proxy)]);
740
761
  }
@@ -1111,6 +1132,9 @@ var firecrackerDriver = {
1111
1132
  farListenHost: (cfg) => cfg.gateway,
1112
1133
  openFarLink,
1113
1134
  closeFarLink,
1135
+ carriesClusters,
1136
+ joinCluster,
1137
+ leaveCluster,
1114
1138
  relay: netSetup,
1115
1139
  setupScript: netSetupScript
1116
1140
  },
@@ -1410,7 +1434,13 @@ var MachineMetaSchema = z3.object({
1410
1434
  * for one each time it needs it (core/machines/integrations.ts) and is answered only for a name in this
1411
1435
  * list. Plain strings, so a name a newer server wrote still reads. Absent: none.
1412
1436
  */
1413
- integrations: z3.array(z3.string()).optional()
1437
+ integrations: z3.array(z3.string()).optional(),
1438
+ /**
1439
+ * The cluster the box is a part of (kvbox k8s, #215), given at create by the control plane and never
1440
+ * changed: while it runs, it and the other boxes of its owner with the same id reach each other on any
1441
+ * port (core/machines/cluster.ts). A fork or a box from a snapshot never has it. Absent: none.
1442
+ */
1443
+ cluster: z3.string().optional()
1414
1444
  });
1415
1445
  var VolumeMetaSchema = z3.object({
1416
1446
  /** Host-unique like a box id (#91): the ledger's key, so a name removed and made again is two volumes. */
@@ -2337,6 +2367,14 @@ var MachineSchema = z6.object({
2337
2367
  * each time it is needed. Absent: none. A host before integrations never sends it.
2338
2368
  */
2339
2369
  integrations: z6.array(z6.string()).optional(),
2370
+ /** The id of the cluster the box is a part of on its host (#215), as the host keeps it. Absent: none, or a host from before it said so. */
2371
+ clusterId: z6.string().optional(),
2372
+ /**
2373
+ * The Kubernetes cluster the box is a node of, and its pool (#218). Only the control plane fills it in, on
2374
+ * `GET /machines`. Such a box is opened like any other, but what would change it is its pool's to do
2375
+ * (`kvbox k8s pool`): the control plane refuses it on the box.
2376
+ */
2377
+ cluster: z6.object({ name: z6.string(), pool: z6.string() }).optional(),
2340
2378
  /** How to reach what the box serves, its secrets masked (#87): `postgres://postgres:****@db.kvbox:5432/postgres`. */
2341
2379
  url: z6.string().optional(),
2342
2380
  /**
@@ -2478,6 +2516,7 @@ var HostEnvRequest = z6.object({
2478
2516
  vars: z6.array(z6.object({ name: z6.string(), value: z6.string() }))
2479
2517
  });
2480
2518
  var HostEnvVersionsSchema = z6.array(z6.object({ owner: z6.string(), version: z6.number().int() }));
2519
+ var CLUSTER_ID_RE = /^[a-z0-9][a-z0-9_-]{0,39}$/;
2481
2520
  var CreateMachineRequest = z6.object({
2482
2521
  name: z6.string().optional(),
2483
2522
  /** Ignored over the local socket (the caller is the host owner); the control plane sets it over HTTPS. */
@@ -2510,8 +2549,11 @@ var CreateMachineRequest = z6.object({
2510
2549
  host: z6.string().optional(),
2511
2550
  /** The image to boot (#87, `kvbox new --base postgres`). Absent: the base image. */
2512
2551
  image: z6.string().optional(),
2513
- /** The box's own environment variables (#87, `kvbox new -e NAME=value`), over its owner's. */
2514
- env: z6.array(z6.object({ name: z6.string(), value: z6.string() })).optional(),
2552
+ /**
2553
+ * The box's own environment variables (#87, `kvbox new -e NAME=value`), over its owner's. `secret`: kept as a
2554
+ * secret, whose value no route answers with (a cluster's join token, #216); a host from before it keeps it plain.
2555
+ */
2556
+ env: z6.array(z6.object({ name: z6.string(), value: z6.string(), secret: z6.boolean().optional() })).optional(),
2515
2557
  /**
2516
2558
  * Start the box on this backup's disk (#141, `kvbox new --from-backup <id>`), a removed box's too: its image,
2517
2559
  * version and variables come from the backup, its size too unless given. Only the control plane reads it.
@@ -2521,7 +2563,13 @@ var CreateMachineRequest = z6.object({
2521
2563
  * The integrations turned on for the box (#205, `kvbox new --integration github`). Absent: none, or with
2522
2564
  * `from` / `fromBackup` the list the snapshot or the backup was taken with.
2523
2565
  */
2524
- integrations: IntegrationListSchema.optional()
2566
+ integrations: IntegrationListSchema.optional(),
2567
+ /**
2568
+ * The cluster the box is a part of (kvbox k8s, #215): on its host, the boxes of one owner with the same id
2569
+ * reach each other on any port. An id the control plane makes, never a user's: the control plane refuses
2570
+ * it from anyone it did not make it for, and a host takes it only from a control plane.
2571
+ */
2572
+ cluster: z6.string().regex(CLUSTER_ID_RE).optional()
2525
2573
  });
2526
2574
  var HostFromBackupRequest = CreateMachineRequest.omit({ from: true, fromBackup: true, image: true, env: true }).extend({ backup: HostRestoreRequest });
2527
2575
  var ForkMachineRequest = z6.object({
@@ -2618,6 +2666,8 @@ var CapacitySchema = z6.object({
2618
2666
  integrations: z6.boolean().optional(),
2619
2667
  /** 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
2668
  farLinks: z6.boolean().optional(),
2669
+ /** 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. */
2670
+ clusters: z6.boolean().optional(),
2621
2671
  /** The integrations this kvbox-server knows (`hostKnowsIntegrations`). Absent with `integrations`: the first one's, `github` alone. */
2622
2672
  integrationIds: z6.array(z6.string()).optional()
2623
2673
  });
@@ -2754,6 +2804,32 @@ var CronUsageSchema = z6.object({
2754
2804
  /** When it was made. */
2755
2805
  historyFrom: z6.string().nullable()
2756
2806
  });
2807
+ var ClusterUsageSchema = z6.object({
2808
+ id: z6.string(),
2809
+ name: z6.string(),
2810
+ owner: z6.string(),
2811
+ ownerKind: z6.enum(["user", "org"]).optional(),
2812
+ /** `removed` once its master is. */
2813
+ status: z6.enum(["present", "removed"]),
2814
+ /** The hours its master existed in the window, running or not. */
2815
+ hours: z6.number(),
2816
+ master: z6.object({
2817
+ seconds: MachineUsageSchema.shape.seconds,
2818
+ vcpuHours: z6.number(),
2819
+ ramGibHours: z6.number(),
2820
+ diskGibHours: z6.number(),
2821
+ volumeHours: z6.number(),
2822
+ volumeGibHours: z6.number(),
2823
+ cost: z6.object({ amount: z6.number(), currency: z6.string() }).optional()
2824
+ }),
2825
+ nodes: z6.array(BoxUsageSchema.extend({
2826
+ /** The pool it is, or was, a node of. */
2827
+ pool: z6.string().optional()
2828
+ })),
2829
+ fee: z6.object({ amount: z6.number(), currency: z6.string() }).optional(),
2830
+ cost: z6.object({ amount: z6.number(), currency: z6.string() }).optional(),
2831
+ historyFrom: z6.string().nullable()
2832
+ });
2757
2833
  var BackupUsageSchema = z6.object({
2758
2834
  id: z6.string(),
2759
2835
  kind: z6.string(),
@@ -2787,6 +2863,8 @@ var UsageDaySchema = z6.object({
2787
2863
  storage: z6.array(UsageDayItemSchema).optional(),
2788
2864
  /** Cron services that existed that day (#190); absent on a day without any. */
2789
2865
  cron: z6.array(UsageDayItemSchema).optional(),
2866
+ /** Kubernetes clusters that existed that day (#222), each with its master and nodes together; absent on a day without any. */
2867
+ clusters: z6.array(UsageDayItemSchema).optional(),
2790
2868
  /** Paid backups that turned ready that day, summed per thing backed up (`id` is `<owner>/<kind>:<name>`); absent on a day without any. */
2791
2869
  backups: z6.array(UsageDayItemSchema).optional()
2792
2870
  });
@@ -2803,6 +2881,11 @@ var UsageReportSchema = z6.object({
2803
2881
  storage: z6.array(VolumeUsageSchema).optional(),
2804
2882
  /** Cron services (#190, control plane only): one row per service a slug has or had. Absent when there is none. */
2805
2883
  cron: z6.array(CronUsageSchema).optional(),
2884
+ /**
2885
+ * Kubernetes clusters (#222, control plane only): one row per cluster a slug has or had, with its master
2886
+ * and its nodes; those boxes are in no other list. Absent when there is none.
2887
+ */
2888
+ clusters: z6.array(ClusterUsageSchema).optional(),
2806
2889
  /** Paid backups (control plane only): one row per box, volume or service backed up by hand in the window. Absent when there is none. */
2807
2890
  backups: z6.array(BackupUsageSchema).optional(),
2808
2891
  /** With `?by=day` only, oldest day first. */
@@ -2814,7 +2897,7 @@ var UsageReportSchema = z6.object({
2814
2897
  diskGibHours: z6.number(),
2815
2898
  volumeHours: z6.number().optional(),
2816
2899
  volumeGibHours: z6.number().optional(),
2817
- /** Boxes, volumes, object storage, cron services and paid backups together. */
2900
+ /** Boxes, volumes, object storage, cron services, Kubernetes clusters and paid backups together; the hours above are those of `boxes` and `volumes` alone. */
2818
2901
  cost: z6.object({ amount: z6.number(), currency: z6.string() }).optional()
2819
2902
  })
2820
2903
  });
@@ -2849,7 +2932,7 @@ var VolumeEventsResponseSchema = z6.object({
2849
2932
  events: z6.array(VolumeEventRowSchema),
2850
2933
  next: z6.number().int()
2851
2934
  });
2852
- var API_ERROR_CODES = ["account-inactive", "org-inactive", "out-of-credit", "box-outdated", "storage-outdated", "object-exists", "storage-waking"];
2935
+ var API_ERROR_CODES = ["account-inactive", "org-inactive", "out-of-credit", "box-outdated", "storage-outdated", "object-exists", "storage-waking", "cluster-busy", "node-has-data"];
2853
2936
  var ApiErrorCodeSchema = z6.enum(API_ERROR_CODES);
2854
2937
  var ApiErrorSchema = z6.object({
2855
2938
  error: z6.string(),
@@ -3018,6 +3101,16 @@ var LoginResponseSchema = z6.object({
3018
3101
  /** Whose boxes the certificate opens: `<slug>@<realm>` for the user and each org they are in (orgs phase 4). */
3019
3102
  sshCertPrincipals: z6.array(z6.string()).optional()
3020
3103
  });
3104
+ var PasswordResetRequestSchema = z6.object({
3105
+ email: z6.string().min(1).max(320)
3106
+ });
3107
+ var PasswordResetCheckSchema = z6.object({
3108
+ token: z6.string().min(1).max(200)
3109
+ });
3110
+ var PasswordResetConfirmSchema = z6.object({
3111
+ token: z6.string().min(1).max(200),
3112
+ password: z6.string().min(1)
3113
+ });
3021
3114
  var DeviceLoginRequestSchema = z6.object({
3022
3115
  // Capped: anyone may open a login, and the key is stored until it expires (an RSA-16384 key is ~2.8 KB).
3023
3116
  publicKey: z6.string().max(16384).optional(),
@@ -3768,6 +3861,8 @@ async function capacity(p = paths()) {
3768
3861
  // A link to a box on another host is carried here (#207): the control plane links across hosts only between two such.
3769
3862
  // Only once the host's privileged network script knows them: an update without its root step does not.
3770
3863
  farLinks: driver().net.carriesFarLinks(),
3864
+ // 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.
3865
+ clusters: driver().net.carriesClusters(),
3771
3866
  // A box's integrations are kept here (#205): the control plane sends a list only to such a host.
3772
3867
  integrations: true,
3773
3868
  // ...and which (the control plane places a box with `claude` only on a host that names it).
@@ -3900,7 +3995,7 @@ async function mergeBoxEnv(box, given, p = paths()) {
3900
3995
  const names2 = new Set(given.map((v) => v.name));
3901
3996
  const now = (/* @__PURE__ */ new Date()).toISOString();
3902
3997
  const kept = (await readBoxEnv(box, p)).filter((v) => !names2.has(v.name));
3903
- await writeBoxEnv(box, [...kept, ...given.map((v) => ({ ...v, secret: false, updatedAt: now }))], p);
3998
+ await writeBoxEnv(box, [...kept, ...given.map((v) => ({ name: v.name, value: v.value, secret: v.secret === true, updatedAt: now }))], p);
3904
3999
  }
3905
4000
  async function copyBoxEnv(from, to) {
3906
4001
  if (!existsSync7(from)) return;
@@ -4544,6 +4639,7 @@ function toMachine(meta, p = paths()) {
4544
4639
  ...meta.autoHibernateSec ? { autoHibernateSec: meta.autoHibernateSec } : {},
4545
4640
  ...meta.idleCheck !== void 0 ? { idleCheck: meta.idleCheck } : {},
4546
4641
  ...meta.integrations?.length ? { integrations: [...meta.integrations] } : {},
4642
+ ...meta.cluster !== void 0 ? { clusterId: meta.cluster } : {},
4547
4643
  ...meta.volumes?.length ? { volumes: volumeSummaries(meta, p) } : {},
4548
4644
  ...linksOf(meta)
4549
4645
  };
@@ -4779,6 +4875,42 @@ function pickSshPort(taken) {
4779
4875
  throw new KvboxError(`no free port left between ${SSH_PORT_MIN} and ${SSH_PORT_MAX} on this host`);
4780
4876
  }
4781
4877
 
4878
+ // src/core/machines/cluster.ts
4879
+ import { createHash as createHash3 } from "node:crypto";
4880
+ var clusterKey = (owner, cluster) => createHash3("sha256").update(`${owner}
4881
+ ${cluster}`).digest("hex").slice(0, 16);
4882
+ function requestedCluster(req, owner, fromBackup = false) {
4883
+ if (req.cluster === void 0) return void 0;
4884
+ if (!isCloudBox(owner)) throw new KvboxError("a box is put in a cluster by a control plane; this host's own boxes have none");
4885
+ 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");
4886
+ if (!driver().net.carriesClusters()) {
4887
+ 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)");
4888
+ }
4889
+ return req.cluster;
4890
+ }
4891
+ async function joinCluster2(meta, cfg) {
4892
+ if (meta.cluster === void 0 || meta.owner === void 0) return;
4893
+ await driver().net.joinCluster({ box: meta.id, ip: meta.ip, cluster: clusterKey(meta.owner, meta.cluster) }, cfg);
4894
+ }
4895
+ async function leaveCluster2(meta, cfg) {
4896
+ await leftCluster(meta, cfg).catch((err) => log.warn(`could not take box ${meta.id} out of its cluster: ${err.message}`));
4897
+ }
4898
+ async function leftCluster(meta, cfg) {
4899
+ if (meta.cluster === void 0) return;
4900
+ await driver().net.leaveCluster({ box: meta.id, ip: meta.ip }, cfg);
4901
+ }
4902
+ async function rejoinClusters(p = paths()) {
4903
+ const cfg = await loadConfig(p);
4904
+ for (const listed of await readAllMeta(p)) {
4905
+ if (!vmAlive(listed) || listed.cluster === void 0) continue;
4906
+ await withBoxLock(listed, async () => {
4907
+ const meta = await readMachineMeta(listed, p).catch(() => null);
4908
+ if (meta === null || !vmAlive(meta)) return;
4909
+ await joinCluster2(meta, cfg).catch((err) => log.warn(`box ${meta.name} is cut off from its cluster: ${err.message}`));
4910
+ });
4911
+ }
4912
+ }
4913
+
4782
4914
  // src/core/farTunnel.ts
4783
4915
  import { createServer as createServer2 } from "node:net";
4784
4916
  import { connect as tlsConnect } from "node:tls";
@@ -5120,6 +5252,7 @@ async function openPorts(meta, cfg, p) {
5120
5252
  }
5121
5253
  const far = reaching(meta);
5122
5254
  if (far) await openFarTargets(far, cfg, p);
5255
+ await joinCluster2(meta, cfg);
5123
5256
  }
5124
5257
  async function closePorts(meta, cfg) {
5125
5258
  for (const proxy of boxProxies(meta)) {
@@ -5130,6 +5263,7 @@ async function closePorts(meta, cfg) {
5130
5263
  }
5131
5264
  const far = reaching(meta);
5132
5265
  if (far) await closeFarTargets(far, cfg);
5266
+ await leaveCluster2(meta, cfg);
5133
5267
  }
5134
5268
  var reservedPorts = /* @__PURE__ */ new Set();
5135
5269
  async function drawPorts(count, p) {
@@ -5286,7 +5420,7 @@ async function bootInto(box, prepare, origin, cfg, p) {
5286
5420
  const spec = { ...box, sshPort };
5287
5421
  const bootedAt = (/* @__PURE__ */ new Date()).toISOString();
5288
5422
  pid = await launch(spec, tap, cfg, keysB64, p);
5289
- await openPorts(spec, cfg, p);
5423
+ await openPorts({ ...spec, cluster: origin.cluster }, cfg, p);
5290
5424
  void pushEnvAfterBoot(spec, pid, m.vsock, p);
5291
5425
  const meta = { ...spec, pid, bootId: currentBootId(), bootedAt, createdAt: (/* @__PURE__ */ new Date()).toISOString(), ...origin };
5292
5426
  await writeMachineMeta(meta, p);
@@ -5296,7 +5430,7 @@ async function bootInto(box, prepare, origin, cfg, p) {
5296
5430
  } catch (err) {
5297
5431
  if (pid !== void 0) killQuietly(pid);
5298
5432
  await stopListeningForGuest(box, p);
5299
- await closePorts({ id, ip, sshPort }, cfg);
5433
+ await closePorts({ id, ip, sshPort, cluster: origin.cluster }, cfg);
5300
5434
  if (tapCreated) await driver().net.remove(id, cfg).catch(() => void 0);
5301
5435
  await rm6(m.dir, { recursive: true, force: true });
5302
5436
  boxListChanged();
@@ -5356,7 +5490,7 @@ async function restoreInto(box, prepare, origin, cfg, p, keysOrFail) {
5356
5490
  import { rm as rm7 } from "node:fs/promises";
5357
5491
 
5358
5492
  // src/core/machines/links.ts
5359
- import { createHash as createHash3, timingSafeEqual } from "node:crypto";
5493
+ import { createHash as createHash4, timingSafeEqual } from "node:crypto";
5360
5494
  function linkSource(req, caller, target) {
5361
5495
  if (req.fromOwner !== void 0) return { owner: ownerFor(req.fromOwner, caller), name: req.from, display: `${req.fromOwner}/${req.from}` };
5362
5496
  const ref = parseRef(req.from, caller);
@@ -5389,7 +5523,7 @@ var addLink = locked(async function addLink2(ref, req, caller, p = paths()) {
5389
5523
  }
5390
5524
  return toMachine(next, p);
5391
5525
  });
5392
- var keyHash = (key) => createHash3("sha256").update(key).digest("hex");
5526
+ var keyHash = (key) => createHash4("sha256").update(key).digest("hex");
5393
5527
  async function addFarLink(meta, from, port, key, p) {
5394
5528
  if (realmOf(meta.owner) === null || realmOf(from.owner) !== realmOf(meta.owner)) {
5395
5529
  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 +5669,10 @@ var destroyBox = locked(async function destroyBox2(ref, p = paths()) {
5535
5669
  const meta = await readMeta(ref, p);
5536
5670
  const cfg = await loadConfig(p);
5537
5671
  await shutdown2(meta, cfg, p, 2e3);
5538
- await closeLinksFrom(meta, cfg, p).catch((err) => {
5672
+ await (async () => {
5673
+ await closeLinksFrom(meta, cfg, p);
5674
+ await leftCluster(meta, cfg);
5675
+ })().catch((err) => {
5539
5676
  recordState(meta, "stopped", "stop", p);
5540
5677
  throw err;
5541
5678
  });
@@ -5580,6 +5717,7 @@ var updateMachine = locked(async function updateMachine2(ref, p = paths()) {
5580
5717
  assertAssets(cfg);
5581
5718
  const base = await currentImage(meta.image, cfg, p);
5582
5719
  if (base.version === meta.base) return toMachine(meta, p);
5720
+ if (meta.cluster !== void 0) throw new KvboxError(`box ${shown(ref)} is part of a cluster and keeps its cluster's version; it is not updated alone`);
5583
5721
  await assertOverlayCapable(base.file, meta.image, base.version);
5584
5722
  return applyAtNextBoot(meta, { base: base.version }, "update", cfg, p);
5585
5723
  });
@@ -5625,7 +5763,10 @@ var replaceDisk = locked(async function replaceDisk2(ref, file, disk, p = paths(
5625
5763
  var setMachine = locked(async function setMachine2(ref, req, p = paths()) {
5626
5764
  const meta = await readMeta(ref, p);
5627
5765
  const next = { ...meta };
5628
- if (req.autoHibernateSec !== void 0) next.autoHibernateSec = req.autoHibernateSec || void 0;
5766
+ if (req.autoHibernateSec !== void 0) {
5767
+ 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`);
5768
+ next.autoHibernateSec = req.autoHibernateSec || void 0;
5769
+ }
5629
5770
  if (req.idleCheck !== void 0) next.idleCheck = req.idleCheck || void 0;
5630
5771
  if (req.integrations !== void 0) {
5631
5772
  next.integrations = requestedIntegrations(req.integrations);
@@ -5717,6 +5858,7 @@ async function checkNewBox(req, owner, diskBytes, p = paths()) {
5717
5858
  }
5718
5859
  async function createMachine(req, owner, p = paths(), from) {
5719
5860
  const cfg = await loadConfig(p);
5861
+ const cluster = requestedCluster(req, owner, from !== void 0);
5720
5862
  if (req.from !== void 0) return restoreMachine(req.from, owner, req, cfg, p);
5721
5863
  const size2 = sizeOf(req, cfg.defaultSize, from ? (await stat3(from.disk)).size : void 0);
5722
5864
  assertAssets(cfg);
@@ -5733,8 +5875,9 @@ async function createMachine(req, owner, p = paths(), from) {
5733
5875
  const manifest = await imageManifest(base.file);
5734
5876
  const env = from ? null : imageEnv(manifest, name, req.env ?? [], (await readOwnerEnv(owner, p).catch(() => null))?.vars ?? []);
5735
5877
  const now = (/* @__PURE__ */ new Date()).toISOString();
5736
- const autoHibernateSec = autoHibernateFor(req, cfg, manifest);
5737
- const vars = env === null ? from?.vars ?? [] : env.vars.map((v) => ({ ...v, secret: env.secrets.includes(v.name), updatedAt: now }));
5878
+ const autoHibernateSec = cluster === void 0 ? autoHibernateFor(req, cfg, manifest) : void 0;
5879
+ const secret = /* @__PURE__ */ new Set([...env?.secrets ?? [], ...(req.env ?? []).filter((v) => v.secret === true).map((v) => v.name)]);
5880
+ const vars = env === null ? from?.vars ?? [] : env.vars.map((v) => ({ name: v.name, value: v.value, secret: secret.has(v.name), updatedAt: now }));
5738
5881
  const machine = await bootInto(
5739
5882
  { id, name, owner, ip, mac, vcpu: size2.vcpu, memMib: size2.memMib, base: base.version, image, sshKeys },
5740
5883
  // The image is shared read-only by every box; only the box's own writes land in its overlay.
@@ -5745,7 +5888,7 @@ async function createMachine(req, owner, p = paths(), from) {
5745
5888
  } else await createOverlay(m.overlay, size2.diskGib);
5746
5889
  if (vars.length > 0) await writeBoxEnv({ owner, name }, vars, p);
5747
5890
  },
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 }) },
5891
+ { 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
5892
  cfg,
5750
5893
  p
5751
5894
  );
@@ -6665,6 +6808,7 @@ async function restoreOnce(ref, req, p) {
6665
6808
  async function createFromBackup(req, owner, p = paths()) {
6666
6809
  const { backup, ...create } = req;
6667
6810
  checkId(backup.id);
6811
+ requestedCluster(create, owner, true);
6668
6812
  const base = await baseFor(backup, p);
6669
6813
  await checkNewBox(create, owner, backup.sizeBytes, p);
6670
6814
  const keys = backupKeys(backup.key);
@@ -7809,10 +7953,10 @@ function versionOf(srcFileUrl, injected) {
7809
7953
  }
7810
7954
 
7811
7955
  // src/version.ts
7812
- var VERSION = versionOf(import.meta.url, true ? "0.6.21" : void 0);
7956
+ var VERSION = versionOf(import.meta.url, true ? "0.6.23" : void 0);
7813
7957
 
7814
7958
  // src/caller.ts
7815
- import { createHash as createHash4, timingSafeEqual as timingSafeEqual2 } from "node:crypto";
7959
+ import { createHash as createHash5, timingSafeEqual as timingSafeEqual2 } from "node:crypto";
7816
7960
  var OWNER_HEADER = "x-kvbox-owner";
7817
7961
  function resolveCaller(opts = {}) {
7818
7962
  const cp = opts.controlPlane;
@@ -7866,7 +8010,7 @@ function bearerCheck(token) {
7866
8010
  };
7867
8011
  }
7868
8012
  function digest(secret) {
7869
- return createHash4("sha256").update(secret).digest();
8013
+ return createHash5("sha256").update(secret).digest();
7870
8014
  }
7871
8015
 
7872
8016
  // src/core/update.ts
@@ -8814,6 +8958,7 @@ var startCommand = new Command("start").description("run the daemon in the foreg
8814
8958
  const state = await driver().net.prepare(cfg);
8815
8959
  log.info(`host network: bridge ${cfg.bridge} ${state}`);
8816
8960
  await pinRunningTaps(paths());
8961
+ await rejoinClusters(paths()).catch((err) => log.warn(`running boxes may be cut off from their clusters: ${err.message}`));
8817
8962
  await reopenFarTargets(paths()).catch((err) => log.warn(`running boxes cannot reach the boxes they are linked to on other hosts: ${err.message}`));
8818
8963
  } catch (err) {
8819
8964
  log.warn(`host network not ready (${err.message}); run: kvbox-server net up`);