@kvbox/cli 0.5.32 → 0.5.34

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.js CHANGED
@@ -164,7 +164,7 @@ function versionOf(srcFileUrl, injected) {
164
164
  }
165
165
 
166
166
  // src/version.ts
167
- var VERSION = versionOf(import.meta.url, true ? "0.5.32" : void 0);
167
+ var VERSION = versionOf(import.meta.url, true ? "0.5.34" : void 0);
168
168
 
169
169
  // src/commands/vm/index.ts
170
170
  import { Command as Command33 } from "commander";
@@ -526,6 +526,8 @@ var MachineSchema = z2.object({
526
526
  ownerKind: z2.enum(["user", "org"]).optional(),
527
527
  /** Seconds without network traffic before the host hibernates the box (#47). Absent: never. */
528
528
  autoHibernateSec: z2.number().int().positive().optional(),
529
+ /** A script inside the box that says it is idle (#102, exit 0): auto-hibernate asks it instead of watching the traffic. */
530
+ idleCheck: z2.string().optional(),
529
531
  /** Volumes attached to the box (#52), in drive order; each is mounted at `mountPath` while the box runs. Absent: none. */
530
532
  volumes: z2.array(z2.object({ name: z2.string(), sizeGib: z2.number().int(), mountPath: z2.string() })).optional(),
531
533
  /** Boxes that may reach this one, each on one port (#98). Absent: none. */
@@ -647,7 +649,9 @@ var RescaleMachineRequest = z2.object({
647
649
  });
648
650
  var SetMachineRequest = z2.object({
649
651
  /** Seconds without network traffic before the box is hibernated (#47); 0: never. */
650
- autoHibernateSec: z2.number().int().min(0).optional()
652
+ autoHibernateSec: z2.number().int().min(0).optional(),
653
+ /** The box's idle check (#102): an absolute path to a script inside the box, exit 0 = idle; "": none, back to network traffic. */
654
+ idleCheck: z2.union([z2.literal(""), z2.string().max(255).regex(/^\/[^\0\n]*$/, "an absolute path inside the box")]).optional()
651
655
  });
652
656
  var SnapshotSchema = z2.object({
653
657
  name: z2.string(),
@@ -1741,6 +1745,16 @@ function envValueProblem(value) {
1741
1745
  return null;
1742
1746
  }
1743
1747
 
1748
+ // ../shared/src/duration.ts
1749
+ var UNIT_SEC = { m: 60, h: 3600, d: 86400 };
1750
+ function parseDuration(text2) {
1751
+ const t = text2.trim().toLowerCase();
1752
+ if (!/^(\d+[mhd])+$/.test(t)) return void 0;
1753
+ let sec = 0;
1754
+ for (const [, n, unit] of t.matchAll(/(\d+)([mhd])/g)) sec += Number(n) * UNIT_SEC[unit];
1755
+ return sec;
1756
+ }
1757
+
1744
1758
  // src/commands/vm/new.ts
1745
1759
  var newCommand = new Command("new").description("boot a new box from an image (the base one, or e.g. postgres) or a snapshot").argument("[name]", "box name (box1, box2, \u2026 when omitted)").addOption(new Option("-s, --size <size>", "box size (default: server config, s)").choices(SIZE_NAMES)).option("-b, --base <image>", "the image to boot: base (default) or one the host has, e.g. postgres").option("-e, --env <NAME=value>", "a variable of the box's own, over yours (repeatable; change later with kvbox env set --box)", collect, []).option("-f, --from <snapshot>", "start from a snapshot instead of an image").option("--near <box>", "put it on the same host as this box, so the two can be linked (kvbox vm link)").option("--auto-hibernate <duration>", "hibernate the box after this long without network traffic (30m, 4h, 1d; off: never; default: server config, off)").option("--force", "skip the capacity guard").option("--json", "print the box as JSON").action(async (name, opts, cmd) => {
1746
1760
  const autoHibernateSec = opts.autoHibernate === void 0 ? void 0 : parseIdleTimeout(opts.autoHibernate);
@@ -1777,7 +1791,7 @@ var newCommand = new Command("new").description("boot a new box from an image (t
1777
1791
  info(`${color.green("\u2713")} ${describe(box, user)}`);
1778
1792
  if (opts.from !== void 0) info(` connect with: kvbox connect ${displayName(box, user)} (already up: everything that ran in the snapshot is running)`);
1779
1793
  else info(` connect with: kvbox connect ${displayName(box, user)} (SSH is up a couple of seconds after boot)`);
1780
- if (box.autoHibernateSec) info(` it ${autoHibernateText(box.autoHibernateSec)}`);
1794
+ if (box.autoHibernateSec) info(` it ${autoHibernateText(box.autoHibernateSec, box.idleCheck)}`);
1781
1795
  if (box.connect !== void 0) {
1782
1796
  info(` reach it at: ${box.connect}`);
1783
1797
  info(` from another box once linked: kvbox vm link <box> ${displayName(box, user)} --port <port>`);
@@ -1803,8 +1817,10 @@ function describe(box, user) {
1803
1817
  const from = box.source.startsWith("snapshot:") ? ` from snapshot ${shortRef(box.source.slice("snapshot:".length), box.owner)}` : box.source.startsWith("fork:") ? `, a fork of ${shortRef(box.source.slice("fork:".length), box.owner)}` : "";
1804
1818
  return `box ${color.bold(displayName(box, user))} is ${box.status} at ${box.ip}${from}: ${box.vcpu} vCPU, ${gb(box.memMib * 1024 ** 2)} RAM, ${gb(box.diskBytes)} disk`;
1805
1819
  }
1806
- function autoHibernateText(sec) {
1807
- return sec ? `hibernates after ${duration(sec)} without network traffic (kvbox connect wakes it)` : "never hibernates on its own";
1820
+ function autoHibernateText(sec, idleCheck) {
1821
+ if (!sec) return "never hibernates on its own";
1822
+ const quiet = idleCheck === void 0 ? "without network traffic" : `idle by ${idleCheck}`;
1823
+ return `hibernates after ${duration(sec)} ${quiet} (kvbox connect wakes it)`;
1808
1824
  }
1809
1825
  function collect(value, previous) {
1810
1826
  return [...previous, value];
@@ -1822,13 +1838,11 @@ function parseEnvFlags(flags) {
1822
1838
  }
1823
1839
  return [...out].map(([name, value]) => ({ name, value }));
1824
1840
  }
1825
- var UNIT_SEC = { m: 60, h: 3600, d: 86400 };
1826
1841
  function parseIdleTimeout(text2) {
1827
1842
  const t = text2.trim().toLowerCase();
1828
1843
  if (t === "off" || t === "never" || t === "0") return 0;
1829
- if (!/^(\d+[mhd])+$/.test(t)) throw new KvboxError(`--auto-hibernate ${text2}: give a duration like 30m, 4h or 1d, or off`);
1830
- let sec = 0;
1831
- for (const [, n, unit] of t.matchAll(/(\d+)([mhd])/g)) sec += Number(n) * UNIT_SEC[unit];
1844
+ const sec = parseDuration(t);
1845
+ if (sec === void 0) throw new KvboxError(`--auto-hibernate ${text2}: give a duration like 30m, 4h or 1d, or off`);
1832
1846
  return sec;
1833
1847
  }
1834
1848
 
@@ -1989,7 +2003,7 @@ var forkCommand = new Command4("fork").description("copy a box, running or stopp
1989
2003
  `);
1990
2004
  info(`${color.green("\u2713")} ${describe(box, user)}`);
1991
2005
  info(` connect with: kvbox connect ${displayName(box, user)}`);
1992
- info(` it ${autoHibernateText(box.autoHibernateSec)}; published ports and HTTPS names stay with ${source}`);
2006
+ info(` it ${autoHibernateText(box.autoHibernateSec, box.idleCheck)}; published ports and HTTPS names stay with ${source}`);
1993
2007
  } finally {
1994
2008
  await client.close();
1995
2009
  }
@@ -2116,7 +2130,9 @@ function getRows(box, sub, user) {
2116
2130
  const ports = rowsOf(box, user).map((r) => `${r.guestPort}/${r.proto} \u2192 ${reaches(r, true)}`);
2117
2131
  rows2.push(["ports", ports.length === 0 ? "-" : ports.join(", ")]);
2118
2132
  rows2.push(["volumes", box.volumes?.length ? box.volumes.map((v) => `${v.name} ${color.dim(`(${v.sizeGib} GB at ${v.mountPath})`)}`).join(", ") : "-"]);
2119
- rows2.push(["auto-hibernate", box.autoHibernateSec ? `after ${duration(box.autoHibernateSec)} without network traffic` : "off"]);
2133
+ const quiet = box.idleCheck === void 0 ? "without network traffic" : `idle by ${box.idleCheck}`;
2134
+ rows2.push(["auto-hibernate", box.autoHibernateSec ? `after ${duration(box.autoHibernateSec)} ${quiet}` : "off"]);
2135
+ if (!box.autoHibernateSec && box.idleCheck !== void 0) rows2.push(["idle check", `${box.idleCheck} ${color.dim("(used once auto-hibernate is on)")}`]);
2120
2136
  rows2.push(["source", box.source]);
2121
2137
  rows2.push(["base", box.image === void 0 ? box.base : `${box.image} ${box.base}`]);
2122
2138
  rows2.push(["created", `${age(box.createdAt)} ${color.dim(`(${box.createdAt})`)}`]);
@@ -2632,23 +2648,37 @@ var rmCommand3 = new Command24("rm").description("destroy a box (its disk is del
2632
2648
 
2633
2649
  // src/commands/vm/set.ts
2634
2650
  import { Command as Command25 } from "commander";
2635
- var setCommand = new Command25("set").description("change a box's settings, e.g. --auto-hibernate 30m (off: never)").argument("<name>", "box name").option("--auto-hibernate <duration>", "hibernate the box after this long without network traffic (30m, 4h, 1d; off: never)").option("--json", "print the box as JSON").action(async (name, opts, cmd) => {
2636
- if (opts.autoHibernate === void 0) throw new KvboxError("nothing to change: give --auto-hibernate");
2637
- const autoHibernateSec = parseIdleTimeout(opts.autoHibernate);
2651
+ var setCommand = new Command25("set").description("change a box's settings, e.g. --auto-hibernate 30m (off: never), --idle-check /home/kvbox/idle.sh").argument("<name>", "box name").option("--auto-hibernate <duration>", "hibernate the box after this long without network traffic (30m, 4h, 1d; off: never)").option("--idle-check <script>", "a script in the box that exits 0 when it is idle, asked instead of network traffic (off: none)").option("--json", "print the box as JSON").action(async (name, opts, cmd) => {
2652
+ if (opts.autoHibernate === void 0 && opts.idleCheck === void 0) throw new KvboxError("nothing to change: give --auto-hibernate or --idle-check");
2653
+ const autoHibernateSec = opts.autoHibernate === void 0 ? void 0 : parseIdleTimeout(opts.autoHibernate);
2654
+ const idleCheck = opts.idleCheck === void 0 ? void 0 : parseIdleCheck(opts.idleCheck);
2638
2655
  const client = await clientFor(cmd.optsWithGlobals());
2639
2656
  try {
2640
2657
  const found = pickRef(await boxesFor(client, name), name, me(client));
2641
2658
  if (!found) throw new KvboxError(`box ${name}: not found (kvbox ls)`);
2642
- const box = await spin(`updating ${name}\u2026`, client.setMachine(displayName(found, me(client)), { autoHibernateSec }));
2659
+ const box = await spin(`updating ${name}\u2026`, client.setMachine(displayName(found, me(client)), { ...autoHibernateSec !== void 0 ? { autoHibernateSec } : {}, ...idleCheck !== void 0 ? { idleCheck } : {} }));
2660
+ if (!idleCheckKept(idleCheck, box)) {
2661
+ throw new KvboxError(`box ${name}: its host runs a kvbox-server too old for --idle-check; it needs updating first${autoHibernateSec !== void 0 ? " (--auto-hibernate was applied)" : ""}`);
2662
+ }
2643
2663
  if (opts.json) {
2644
2664
  json(box);
2645
2665
  return;
2646
2666
  }
2647
- info(`${color.green("\u2713")} box ${color.bold(name)}: ${autoHibernateText(box.autoHibernateSec)}`);
2667
+ info(`${color.green("\u2713")} box ${color.bold(name)}: ${autoHibernateText(box.autoHibernateSec, box.idleCheck)}`);
2668
+ if (idleCheck && !box.autoHibernateSec) info(color.dim(` the idle check is used once auto-hibernate is on: kvbox vm set ${name} --auto-hibernate 10m`));
2648
2669
  } finally {
2649
2670
  await client.close();
2650
2671
  }
2651
2672
  });
2673
+ function idleCheckKept(asked, box) {
2674
+ return asked === void 0 || (box.idleCheck ?? "") === asked;
2675
+ }
2676
+ function parseIdleCheck(text2) {
2677
+ const t = text2.trim();
2678
+ if (t === "" || t === "off" || t === "none") return "";
2679
+ if (!t.startsWith("/")) throw new KvboxError(`--idle-check ${text2}: give the script's absolute path inside the box (/home/kvbox/idle.sh), or off`);
2680
+ return t;
2681
+ }
2652
2682
 
2653
2683
  // src/commands/vm/start.ts
2654
2684
  import { Command as Command26 } from "commander";