@kvbox/server 0.6.19 → 0.6.20

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.
@@ -1332,6 +1332,12 @@ var MachineMetaSchema = z3.object({
1332
1332
  vcpu: z3.number().int(),
1333
1333
  memMib: z3.number().int(),
1334
1334
  createdAt: z3.string(),
1335
+ /**
1336
+ * When the guest last got its owner's variables at a start (#155): set at every cold boot and at a warm `new --from`
1337
+ * or fork (both hand the guest the list of that moment, core/machines/guest.ts); a wake or resume keeps it.
1338
+ * Absent: not booted since this field came.
1339
+ */
1340
+ bootedAt: z3.string().optional(),
1335
1341
  source: z3.string(),
1336
1342
  /** Version of the image the box's overlay stacks on: assets/rootfs-<version>.ext4 for the base one. */
1337
1343
  base: z3.string(),
@@ -2070,6 +2076,7 @@ var WakeHookSchema = z6.object({
2070
2076
  createdAt: z6.string(),
2071
2077
  lastCalledAt: z6.string().nullable()
2072
2078
  });
2079
+ var WakeHookListSchema = z6.array(WakeHookSchema);
2073
2080
  var CreatedWakeHookSchema = WakeHookSchema.extend({ secret: z6.string() });
2074
2081
  var WakeHookCallSchema = z6.object({
2075
2082
  box: z6.string(),
@@ -2280,6 +2287,13 @@ var MachineSchema = z6.object({
2280
2287
  */
2281
2288
  latestBase: z6.string().optional(),
2282
2289
  createdAt: z6.string(),
2290
+ /**
2291
+ * When the guest last got its variables by a fresh start (#155): the host's clock at a cold boot (`new`, `start`
2292
+ * and the restarts behind rescale, update, volume attach), or at a warm `new --from`/fork, which hands the copy
2293
+ * its variables of that moment. A wake or resume keeps it. A variable changed after it waits for the next start.
2294
+ * Absent: a host before it, or a box not booted since.
2295
+ */
2296
+ bootedAt: z6.string().optional(),
2283
2297
  source: z6.string(),
2284
2298
  // "base", "snapshot:<owner/name>", "fork:<owner/name>" (#79) or "backup:<id>" (#141)
2285
2299
  status: BoxStatusSchema,
@@ -2344,7 +2358,9 @@ var VolumeSchema = z6.object({
2344
2358
  /** Whether the owner slug is a user or an org. Only the control plane knows; it fills it in on `GET /volumes`. */
2345
2359
  ownerKind: z6.enum(["user", "org"]).optional(),
2346
2360
  /** The name of the host the volume lives on; only the control plane fills it in. It attaches only to a box there. */
2347
- host: z6.string().optional()
2361
+ host: z6.string().optional(),
2362
+ /** The country code of that host (`RO`, `US`), when its admin gave one (#140). Only the control plane fills it in. */
2363
+ country: z6.string().optional()
2348
2364
  });
2349
2365
  var MAX_VOLUME_GIB = 4096;
2350
2366
  var VOLUME_NAME_RE = /^[a-z0-9][a-z0-9-]{0,11}$/;
@@ -2615,6 +2631,7 @@ var LatestImagesSchema = z6.object({
2615
2631
  images: ImageIndexSchema.shape.images,
2616
2632
  checkedAt: z6.string().nullable()
2617
2633
  });
2634
+ var LatestServerSchema = z6.object({ version: z6.string().nullable(), checkedAt: z6.string().nullable() });
2618
2635
  var RecoveryReportSchema = z6.object({
2619
2636
  at: z6.string(),
2620
2637
  booted: z6.array(z6.string()),
@@ -2894,6 +2911,18 @@ var BalanceSchema = z6.object({
2894
2911
  balance: z6.number(),
2895
2912
  currency: z6.string(),
2896
2913
  topUps: z6.number(),
2914
+ /**
2915
+ * `topUps` split by where the money came from (#144), so a page can say "you added $X" and an org can
2916
+ * tell a top-up from a transfer: `topUps = added + welcome + transferredIn - transferredOut`. `added` is
2917
+ * card payments and an admin's top-ups (an admin's correction, negative, included); `welcome` the
2918
+ * welcome credit of a first card (`trial` rows); `transferredIn` what an owner moved into this org and
2919
+ * `transferredOut` what this user moved to their orgs, both positive. Promo credit is in none of them
2920
+ * (`promo`). Absent from a control plane older than these fields: 0.
2921
+ */
2922
+ added: z6.number().default(0),
2923
+ welcome: z6.number().default(0),
2924
+ transferredIn: z6.number().default(0),
2925
+ transferredOut: z6.number().default(0),
2897
2926
  spent: z6.number(),
2898
2927
  /** The first moment usage counts from: the slug's first top-up or redeemed code; null when it has none. */
2899
2928
  since: z6.string().nullable(),
@@ -4493,6 +4522,7 @@ function toMachine(meta, p = paths()) {
4493
4522
  ...meta.image !== void 0 ? { image: meta.image } : {},
4494
4523
  ...meta.url !== void 0 ? { url: urlFor(meta.url, meta.name) } : {},
4495
4524
  createdAt: meta.createdAt,
4525
+ ...meta.bootedAt !== void 0 ? { bootedAt: meta.bootedAt } : {},
4496
4526
  source: meta.source,
4497
4527
  status: statusOf(meta, p),
4498
4528
  ...meta.sshPort === void 0 ? {} : { sshPort: meta.sshPort },
@@ -5175,10 +5205,11 @@ var startMachine = locked(async function startMachine2(ref, p = paths(), force =
5175
5205
  const tap = await driver().net.create(meta, cfg);
5176
5206
  let pid;
5177
5207
  try {
5208
+ const bootedAt = (/* @__PURE__ */ new Date()).toISOString();
5178
5209
  pid = await launch(meta, tap, cfg, keysB64, p);
5179
5210
  await openPorts(meta, cfg, p);
5180
5211
  void pushEnvAfterBoot(meta, pid, p.machine(meta.owner, meta.name).vsock, p);
5181
- const started = { ...meta, pid, bootId: currentBootId(), sshKeysStale: void 0, paused: void 0 };
5212
+ const started = { ...meta, pid, bootId: currentBootId(), bootedAt, sshKeysStale: void 0, paused: void 0 };
5182
5213
  await writeMachineMeta(started, p);
5183
5214
  recordState(started, "running", "start", p);
5184
5215
  void tellIntegrationsWhenUp(started, pid, p.machine(meta.owner, meta.name).vsock, p);
@@ -5239,10 +5270,11 @@ async function bootInto(box, prepare, origin, cfg, p) {
5239
5270
  const tap = await driver().net.create(box, cfg);
5240
5271
  tapCreated = true;
5241
5272
  const spec = { ...box, sshPort };
5273
+ const bootedAt = (/* @__PURE__ */ new Date()).toISOString();
5242
5274
  pid = await launch(spec, tap, cfg, keysB64, p);
5243
5275
  await openPorts(spec, cfg, p);
5244
5276
  void pushEnvAfterBoot(spec, pid, m.vsock, p);
5245
- const meta = { ...spec, pid, bootId: currentBootId(), createdAt: (/* @__PURE__ */ new Date()).toISOString(), ...origin };
5277
+ const meta = { ...spec, pid, bootId: currentBootId(), bootedAt, createdAt: (/* @__PURE__ */ new Date()).toISOString(), ...origin };
5246
5278
  await writeMachineMeta(meta, p);
5247
5279
  recordState(meta, "running", "new", p, meta.createdAt);
5248
5280
  void tellIntegrationsWhenUp(meta, pid, m.vsock, p);
@@ -5284,10 +5316,11 @@ async function restoreInto(box, prepare, origin, cfg, p, keysOrFail) {
5284
5316
  const stale = await pushAuthorizedKeys(box, m.vsock, p);
5285
5317
  if (stale && keysOrFail) throw new KvboxError(`${keysOrFail}: ${stale}`);
5286
5318
  if (image.volumes?.length) await dropGuestVolumes(box, m.vsock);
5319
+ const bootedAt = (/* @__PURE__ */ new Date()).toISOString();
5287
5320
  await pushEnv(box, m.vsock, p);
5288
5321
  await driver().net.attach(box, cfg);
5289
5322
  await openPorts(spec, cfg, p);
5290
- const meta = { ...spec, pid, bootId: currentBootId(), createdAt: (/* @__PURE__ */ new Date()).toISOString(), ...origin, memSnapshots: 1, sshKeysStale: stale ? true : void 0 };
5323
+ const meta = { ...spec, pid, bootId: currentBootId(), bootedAt, createdAt: (/* @__PURE__ */ new Date()).toISOString(), ...origin, memSnapshots: 1, sshKeysStale: stale ? true : void 0 };
5291
5324
  await writeMachineMeta(meta, p);
5292
5325
  recordState(meta, "running", "new", p, meta.createdAt);
5293
5326
  void tellIntegrationsWhenUp(meta, pid, m.vsock, p);
@@ -5460,19 +5493,22 @@ async function cleanupProcess(meta, cfg, p) {
5460
5493
  }
5461
5494
  }
5462
5495
  var stopMachine = locked(async function stopMachine2(ref, p = paths()) {
5496
+ return toMachine(await bringDown(ref, p, true), p);
5497
+ });
5498
+ async function bringDown(ref, p, compact) {
5463
5499
  const meta = await readMeta(ref, p);
5464
5500
  const cfg = await loadConfig(p);
5465
5501
  const m = p.machine(ref.owner, ref.name);
5466
5502
  if (!vmAlive(meta) && !isHibernated(meta, p)) throw new KvboxError(`box ${shown(ref)} is already stopped`);
5467
5503
  await shutdown2(meta, cfg, p, 1e4);
5468
- await compactDisk(m.overlay).catch((err) => log.warn(`box ${shown(ref)}: disk not compacted: ${err.message}`));
5504
+ if (compact) await compactDisk(m.overlay).catch((err) => log.warn(`box ${shown(ref)}: disk not compacted: ${err.message}`));
5469
5505
  await rm7(m.hibernateDir, { recursive: true, force: true });
5470
5506
  await rm7(m.memBase, { force: true });
5471
5507
  const stopped = { ...meta, pid: null, memSnapshots: void 0, paused: void 0 };
5472
5508
  await writeMachineMeta(stopped, p);
5473
5509
  recordState(stopped, "stopped", "stop", p);
5474
- return toMachine(stopped, p);
5475
- });
5510
+ return stopped;
5511
+ }
5476
5512
  async function destroyMachine(ref, p = paths()) {
5477
5513
  const gone = await destroyBox(ref, p);
5478
5514
  try {
@@ -6097,6 +6133,20 @@ var wakeMachine = locked(async function wakeMachine2(ref, p = paths(), force = f
6097
6133
  return toMachine(running, p);
6098
6134
  });
6099
6135
 
6136
+ // src/core/machines/restart.ts
6137
+ var restartMachine = locked(async function restartMachine2(ref, p = paths(), force = false) {
6138
+ const meta = await readMeta(ref, p);
6139
+ if (vmAlive(meta) || isHibernated(meta, p)) {
6140
+ assertAssets(await loadConfig(p));
6141
+ await assertRoomFor(meta.vcpu, meta.memMib, force, meta, "start", p);
6142
+ await bringDown(ref, p, false);
6143
+ return startMachine(ref, p, force).catch((e) => {
6144
+ throw new KvboxError(`box ${shown(ref)} is stopped: it went down for the restart but could not boot again: ${e.message}`, e instanceof KvboxError ? e.exitCode : 1);
6145
+ });
6146
+ }
6147
+ return startMachine(ref, p, force);
6148
+ });
6149
+
6100
6150
  // src/core/machines/volumes.ts
6101
6151
  import { statSync as statSync5 } from "node:fs";
6102
6152
  import { rename as rename8, rm as rm13 } from "node:fs/promises";
@@ -7725,7 +7775,7 @@ function versionOf(srcFileUrl, injected) {
7725
7775
  }
7726
7776
 
7727
7777
  // src/version.ts
7728
- var VERSION = versionOf(import.meta.url, true ? "0.6.19" : void 0);
7778
+ var VERSION = versionOf(import.meta.url, true ? "0.6.20" : void 0);
7729
7779
 
7730
7780
  // src/caller.ts
7731
7781
  import { createHash as createHash4, timingSafeEqual as timingSafeEqual2 } from "node:crypto";
@@ -8206,6 +8256,10 @@ function machineRoutes() {
8206
8256
  const body = StartMachineRequest.parse(await c.req.json().catch(() => ({})));
8207
8257
  return c.json(await startMachine(parseRef(c.req.param("name"), c.get("owner")), void 0, body.force ?? false));
8208
8258
  });
8259
+ r.post("/:name/restart", async (c) => {
8260
+ const body = StartMachineRequest.parse(await c.req.json().catch(() => ({})));
8261
+ return c.json(await restartMachine(parseRef(c.req.param("name"), c.get("owner")), void 0, body.force ?? false));
8262
+ });
8209
8263
  r.post("/:name/pause", async (c) => c.json(await pauseMachine(parseRef(c.req.param("name"), c.get("owner")))));
8210
8264
  r.post("/:name/resume", async (c) => c.json(await resumeMachine(parseRef(c.req.param("name"), c.get("owner")))));
8211
8265
  r.post("/:name/hibernate", async (c) => c.json(await hibernateMachine(parseRef(c.req.param("name"), c.get("owner")))));