@kvbox/server 0.6.9 → 0.6.11

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.
@@ -74,6 +74,8 @@ function paths(home = kvboxHome()) {
74
74
  },
75
75
  assets: join(home, "assets"),
76
76
  kernel: join(home, "assets", "vmlinux"),
77
+ /** A kernel an image asks for by name in its image.json (`"kernel": "android"`): assets/vmlinux-<name>, put there by install-server.sh. */
78
+ namedKernel: (name) => join(home, "assets", `vmlinux-${name}`),
77
79
  /** Symlink to the current base image, assets/rootfs-<version>.ext4; boxes keep using the version they were made from. */
78
80
  rootfs: join(home, "assets", "rootfs.ext4"),
79
81
  baseImage: (version) => join(home, "assets", `rootfs-${version}.ext4`),
@@ -211,7 +213,12 @@ var PricingSchema = z.object({
211
213
  storageGibHour: z.number().nonnegative().default(0),
212
214
  storageIncludedGib: z.number().int().nonnegative().default(0),
213
215
  /** The cron service (#190): an hour of one cron service existing, whatever it holds or fires. A price list from before it charges nothing. */
214
- cronHour: z.number().nonnegative().default(0)
216
+ cronHour: z.number().nonnegative().default(0),
217
+ /**
218
+ * One backup a person starts, of anything, charged once it is ready (decision "A backup taken by hand
219
+ * costs a flat price…"); a failed one and a scheduled one cost nothing. A price list from before it charges nothing.
220
+ */
221
+ backup: z.number().nonnegative().default(0)
215
222
  });
216
223
  var PriceListSchema = PricingSchema.extend({ welcomeCredit: z.number().nonnegative() });
217
224
  var HOUR_MS = 36e5;
@@ -459,6 +466,12 @@ var ConfigSchema = z2.object({
459
466
  * refused when it could not fill up with that much still free. 0 only on a host whose disk is for tests.
460
467
  */
461
468
  volumeHeadroomGib: z2.number().int().min(0).default(20),
469
+ /**
470
+ * MiB of RAM the host keeps back from its boxes: they may reserve the host's RAM less this. 0 by default
471
+ * (boxes rarely touch all they are sized at); raise it on a host whose boxes do fill their memory. Read at
472
+ * every check, so an edit needs no restart; reported in `/capacity`, which is what placement goes by.
473
+ */
474
+ memHeadroomMib: z2.number().int().min(0).default(0),
462
475
  /** Absent until the host joins a control plane; then the server also listens on HTTPS. */
463
476
  controlPlane: ControlPlaneSchema.optional(),
464
477
  /** Absent unless the host sits behind a relay. */
@@ -926,7 +939,7 @@ async function boot(spec, disks, tap, cfg, p) {
926
939
  const pid = await spawnFirecracker(spec, cfg, p);
927
940
  try {
928
941
  await withClient(spec, p, async (fc) => {
929
- await fc.bootSource(cfg.kernel, bootArgs(spec.name, spec.ip, cfg, disks.pubkeyB64));
942
+ await fc.bootSource(disks.kernel, bootArgs(spec.name, spec.ip, cfg, disks.pubkeyB64));
930
943
  await fc.rootDrive(disks.root);
931
944
  await fc.overlayDrive(OVERLAY_REL);
932
945
  for (const v of disks.volumes) await fc.volumeDrive(v.id, v.path);
@@ -1650,7 +1663,13 @@ var ImageManifestSchema = z4.object({
1650
1663
  * Scripts in the box the host may run by name through kvbox-agent (#173): `status`, `password`, …
1651
1664
  * The host knows the names only; what each does and answers is the image's.
1652
1665
  */
1653
- scripts: z4.record(z4.string(), z4.string().startsWith("/")).optional()
1666
+ scripts: z4.record(z4.string(), z4.string().startsWith("/")).optional(),
1667
+ /**
1668
+ * The kernel a box of this image boots, by name (`android`: assets/vmlinux-android), instead of the
1669
+ * host's one. Lets one image have what the others must not (binder for Redroid) without changing
1670
+ * every box's kernel. Only a cold boot reads it: a restored box keeps the kernel in its memory image.
1671
+ */
1672
+ kernel: z4.string().regex(IMAGE_RE, "a kernel name like android").optional()
1654
1673
  });
1655
1674
  var manifests = /* @__PURE__ */ new Map();
1656
1675
  async function imageManifest(file) {
@@ -1668,6 +1687,12 @@ async function imageManifest(file) {
1668
1687
  manifests.set(file, manifest);
1669
1688
  return manifest;
1670
1689
  }
1690
+ function kernelFor(manifest, cfg, p) {
1691
+ if (manifest.kernel === void 0) return cfg.kernel;
1692
+ const file = p.namedKernel(manifest.kernel);
1693
+ if (!existsSync4(file)) throw new KvboxError(`this image boots the ${manifest.kernel} kernel, which this host does not have at ${file}; install-server.sh --update installs it`);
1694
+ return file;
1695
+ }
1671
1696
  function expand(text, vars) {
1672
1697
  return text.replace(/\$\{([A-Za-z_][A-Za-z0-9_]*)\}/g, (_, name) => vars.get(name) ?? "");
1673
1698
  }
@@ -1725,7 +1750,6 @@ import { rename as rename5, rm as rm6 } from "node:fs/promises";
1725
1750
  import { cpus, totalmem } from "node:os";
1726
1751
 
1727
1752
  // ../shared/src/capacity.ts
1728
- var MEM_HEADROOM_MIB = 2048;
1729
1753
  function sellableGib(d) {
1730
1754
  return Math.max(0, d.freeGib + d.volumesUsedGib - d.headroomGib);
1731
1755
  }
@@ -1831,6 +1855,12 @@ var BackupSchema = z5.object({
1831
1855
  createdAt: z5.string(),
1832
1856
  finishedAt: z5.string().nullable()
1833
1857
  });
1858
+ var BackupLimitSchema = z5.object({
1859
+ perDay: z5.number().int(),
1860
+ used: z5.number().int(),
1861
+ nextAt: z5.string().nullable()
1862
+ });
1863
+ var NamedBackupLimitSchema = BackupLimitSchema.extend({ owner: z5.string(), name: z5.string() });
1834
1864
  var HostBackupRequest = z5.object({ id: z5.string(), key: z5.string() });
1835
1865
  var HostBackupManifestSchema = z5.object({
1836
1866
  id: z5.string(),
@@ -1873,6 +1903,16 @@ var VolumeBackupSchema = z5.object({
1873
1903
  createdAt: z5.string(),
1874
1904
  finishedAt: z5.string().nullable()
1875
1905
  });
1906
+ var BackupScheduleSchema = z5.object({
1907
+ enabled: z5.boolean(),
1908
+ /** 0-23, UTC. Kept while it is off, so turning it on again keeps the hour. */
1909
+ hour: z5.number().int().min(0).max(23),
1910
+ nextAt: z5.string().nullable(),
1911
+ lastAt: z5.string().nullable(),
1912
+ lastBackup: z5.string().nullable(),
1913
+ lastError: z5.string().nullable()
1914
+ });
1915
+ var SetBackupScheduleRequest = z5.object({ enabled: z5.boolean().optional(), hour: z5.number().int().min(0).max(23).optional() });
1876
1916
  var HostVolumeBackupManifestSchema = z5.object({ id: z5.string(), sizeBytes: z5.number().int(), chunks: z5.array(z5.string().nullable()) });
1877
1917
  var HostVolumeRestoreRequest = z5.object({
1878
1918
  id: z5.string(),
@@ -2256,6 +2296,11 @@ var CapacitySchema = z5.object({
2256
2296
  reservedMemMib: z5.number().int(),
2257
2297
  reservedVcpu: z5.number().int(),
2258
2298
  running: z5.number().int(),
2299
+ /**
2300
+ * MiB of RAM the host keeps back from its boxes (config.json `memHeadroomMib`). Absent on a kvbox-server
2301
+ * from before the setting, which keeps 2 GB (`OLD_HOST_MEM_HEADROOM_MIB`).
2302
+ */
2303
+ memHeadroomMib: z5.number().int().optional(),
2259
2304
  /** Absent on a host from before volumes (#91): it cannot take one. */
2260
2305
  disk: DiskCapacitySchema.optional(),
2261
2306
  /**
@@ -2402,6 +2447,16 @@ var CronUsageSchema = z5.object({
2402
2447
  /** When it was made. */
2403
2448
  historyFrom: z5.string().nullable()
2404
2449
  });
2450
+ var BackupUsageSchema = z5.object({
2451
+ id: z5.string(),
2452
+ kind: z5.string(),
2453
+ name: z5.string(),
2454
+ owner: z5.string(),
2455
+ ownerKind: z5.enum(["user", "org"]).optional(),
2456
+ /** How many paid backups turned ready in the window. */
2457
+ count: z5.number().int(),
2458
+ cost: z5.object({ amount: z5.number(), currency: z5.string() })
2459
+ });
2405
2460
  var UsageDayItemSchema = z5.object({
2406
2461
  id: z5.string(),
2407
2462
  name: z5.string(),
@@ -2422,7 +2477,9 @@ var UsageDaySchema = z5.object({
2422
2477
  /** Object storage that existed that day (#182); absent on a day without any. */
2423
2478
  storage: z5.array(UsageDayItemSchema).optional(),
2424
2479
  /** Cron services that existed that day (#190); absent on a day without any. */
2425
- cron: z5.array(UsageDayItemSchema).optional()
2480
+ cron: z5.array(UsageDayItemSchema).optional(),
2481
+ /** Paid backups that turned ready that day, summed per thing backed up (`id` is `<owner>/<kind>:<name>`); absent on a day without any. */
2482
+ backups: z5.array(UsageDayItemSchema).optional()
2426
2483
  });
2427
2484
  var UsageReportSchema = z5.object({
2428
2485
  window: z5.object({ from: z5.string(), to: z5.string() }),
@@ -2437,6 +2494,8 @@ var UsageReportSchema = z5.object({
2437
2494
  storage: z5.array(VolumeUsageSchema).optional(),
2438
2495
  /** Cron services (#190, control plane only): one row per service a slug has or had. Absent when there is none. */
2439
2496
  cron: z5.array(CronUsageSchema).optional(),
2497
+ /** Paid backups (control plane only): one row per box, volume or service backed up by hand in the window. Absent when there is none. */
2498
+ backups: z5.array(BackupUsageSchema).optional(),
2440
2499
  /** With `?by=day` only, oldest day first. */
2441
2500
  days: z5.array(UsageDaySchema).optional(),
2442
2501
  totals: z5.object({
@@ -2446,7 +2505,7 @@ var UsageReportSchema = z5.object({
2446
2505
  diskGibHours: z5.number(),
2447
2506
  volumeHours: z5.number().optional(),
2448
2507
  volumeGibHours: z5.number().optional(),
2449
- /** Boxes, volumes, object storage and cron services together. */
2508
+ /** Boxes, volumes, object storage, cron services and paid backups together. */
2450
2509
  cost: z5.object({ amount: z5.number(), currency: z5.string() }).optional()
2451
2510
  })
2452
2511
  });
@@ -3366,15 +3425,17 @@ async function runningReservations(p = paths()) {
3366
3425
  async function capacity(p = paths()) {
3367
3426
  const host = hostResources();
3368
3427
  const running = await runningReservations(p);
3428
+ const cfg = await loadConfig(p);
3369
3429
  return {
3370
3430
  hostMemMib: host.memMib,
3431
+ memHeadroomMib: cfg.memHeadroomMib,
3371
3432
  hostCores: host.cores,
3372
3433
  reservedMemMib: running.reduce((n, r) => n + r.memMib, 0),
3373
3434
  reservedVcpu: running.reduce((n, r) => n + r.vcpu, 0),
3374
3435
  running: running.length,
3375
3436
  disk: await diskCapacity(p),
3376
3437
  // What a box can be made from here (#87): the control plane places `--base postgres` by it.
3377
- images: await listImages(await loadConfig(p), p),
3438
+ images: await listImages(cfg, p),
3378
3439
  ...await backupStoreCapacity(p).then((store) => store ? { backupStore: store } : {}),
3379
3440
  // Backups and restores here reach a store by its certificate's pin (core/storeFetch.ts).
3380
3441
  storeClient: true
@@ -3404,11 +3465,12 @@ function roomFor(host, running, want, opts = {}) {
3404
3465
  const { viewer, by = "new" } = opts;
3405
3466
  const reservedMem = running.reduce((n, r) => n + r.memMib, 0);
3406
3467
  const reservedVcpu = running.reduce((n, r) => n + r.vcpu, 0);
3407
- const memLimit = host.memMib - MEM_HEADROOM_MIB;
3468
+ const headroom = host.headroomMib ?? 0;
3469
+ const memLimit = host.memMib - headroom;
3408
3470
  const vcpuLimit = host.cores * VCPU_OVERSUBSCRIPTION;
3409
3471
  const hint = HINTS[by];
3410
3472
  if (reservedMem + want.memMib > memLimit) {
3411
- return `not enough memory for a ${gb(want.memMib)} box: ${describeRunning(running, viewer)}; this host has ${gb(host.memMib)} and keeps ${gb(MEM_HEADROOM_MIB)} for itself, so boxes may reserve ${gb(memLimit)} in total. ` + hint;
3473
+ return `not enough memory for a ${gb(want.memMib)} box: ${describeRunning(running, viewer)}; ` + (headroom > 0 ? `this host has ${gb(host.memMib)} and keeps ${gb(headroom)} for itself, so boxes may reserve ${gb(memLimit)} in total. ` : `this host has ${gb(host.memMib)}, all of it for its boxes. `) + hint;
3412
3474
  }
3413
3475
  if (reservedVcpu + want.vcpu > vcpuLimit) {
3414
3476
  return `too many vCPUs for a ${want.vcpu} vCPU box: ${describeRunning(running, viewer)}; this host has ${host.cores} cores and allows ${vcpuLimit} vCPUs in total (${VCPU_OVERSUBSCRIPTION}x). ` + hint;
@@ -3418,7 +3480,8 @@ function roomFor(host, running, want, opts = {}) {
3418
3480
  async function assertRoomFor(vcpu, memMib, force, box, by, p = paths()) {
3419
3481
  if (force) return;
3420
3482
  const running = (await runningReservations(p)).filter((r) => box.name === void 0 || !(r.owner === box.owner && r.name === box.name));
3421
- const reason = roomFor(hostResources(), running, { vcpu, memMib }, { viewer: box.owner, by });
3483
+ const host = { ...hostResources(), headroomMib: (await loadConfig(p)).memHeadroomMib };
3484
+ const reason = roomFor(host, running, { vcpu, memMib }, { viewer: box.owner, by });
3422
3485
  if (reason !== void 0) throw new KvboxError(reason);
3423
3486
  }
3424
3487
 
@@ -3750,8 +3813,9 @@ async function getMachine(ref, p = paths()) {
3750
3813
  // src/core/machines/boot.ts
3751
3814
  async function launch(spec, nic, cfg, pubkeyB64, p) {
3752
3815
  const root = imageFileFor(spec, p);
3816
+ const kernel = kernelFor(await imageManifest(root), cfg, p);
3753
3817
  const volumes = (await placeVolumeDrives(spec, p)).map((v) => ({ id: volumeDriveId(v.name), path: volumeDriveRel(v.name) }));
3754
- return driver().vm.boot(spec, { root, volumes, pubkeyB64 }, nic, cfg, p);
3818
+ return driver().vm.boot(spec, { kernel, root, volumes, pubkeyB64 }, nic, cfg, p);
3755
3819
  }
3756
3820
  async function loadSnapshot(spec, image, nic, cfg, p) {
3757
3821
  return driver().vm.restore(spec, { vmstate: image.vmstate, mem: image.mem }, nic, cfg, p);
@@ -6362,7 +6426,7 @@ function versionOf(srcFileUrl, injected) {
6362
6426
  }
6363
6427
 
6364
6428
  // src/version.ts
6365
- var VERSION = versionOf(import.meta.url, true ? "0.6.9" : void 0);
6429
+ var VERSION = versionOf(import.meta.url, true ? "0.6.11" : void 0);
6366
6430
 
6367
6431
  // src/caller.ts
6368
6432
  import { createHash as createHash3, timingSafeEqual } from "node:crypto";