@kvbox/cli 0.5.47 → 0.5.48
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 +655 -125
- package/dist/kvbox.js.map +1 -1
- package/package.json +1 -1
package/dist/kvbox.js
CHANGED
|
@@ -149,7 +149,7 @@ async function spin(message, work, opts = {}) {
|
|
|
149
149
|
}
|
|
150
150
|
|
|
151
151
|
// src/program.ts
|
|
152
|
-
import { Command as
|
|
152
|
+
import { Command as Command85 } from "commander";
|
|
153
153
|
|
|
154
154
|
// ../shared/src/version.ts
|
|
155
155
|
import { createRequire } from "module";
|
|
@@ -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.
|
|
167
|
+
var VERSION = versionOf(import.meta.url, true ? "0.5.48" : void 0);
|
|
168
168
|
|
|
169
169
|
// src/commands/vm/index.ts
|
|
170
170
|
import { Command as Command37 } from "commander";
|
|
@@ -426,7 +426,7 @@ import { openSync } from "fs";
|
|
|
426
426
|
import { mkdir as mkdir2, readFile as readFile3, stat } from "fs/promises";
|
|
427
427
|
import { setTimeout as sleep } from "timers/promises";
|
|
428
428
|
import { Agent, request } from "undici";
|
|
429
|
-
import { z as
|
|
429
|
+
import { z as z5 } from "zod";
|
|
430
430
|
|
|
431
431
|
// ../shared/src/api.ts
|
|
432
432
|
import { z as z2 } from "zod";
|
|
@@ -459,10 +459,17 @@ var PortForwardSchema = z2.object({
|
|
|
459
459
|
hostPort: z2.number().int(),
|
|
460
460
|
proto: PortProtoSchema,
|
|
461
461
|
/** The host's public address; only the control plane fills it in, as for `ssh`. */
|
|
462
|
-
address: z2.string().optional()
|
|
462
|
+
address: z2.string().optional(),
|
|
463
|
+
/**
|
|
464
|
+
* Closed but kept (#173): nothing reaches the box through it, and the host port stays the box's, so
|
|
465
|
+
* publishing the same box port again opens the same number. Absent: open while the box runs.
|
|
466
|
+
*/
|
|
467
|
+
closed: z2.literal(true).optional()
|
|
463
468
|
});
|
|
464
469
|
var PortsRequest = z2.object({
|
|
465
|
-
ports: z2.array(z2.object({ guestPort: z2.number().int().min(1).max(65535), proto: PortProtoSchema.optional() })).min(1).max(MAX_BOX_PORTS)
|
|
470
|
+
ports: z2.array(z2.object({ guestPort: z2.number().int().min(1).max(65535), proto: PortProtoSchema.optional() })).min(1).max(MAX_BOX_PORTS),
|
|
471
|
+
/** `DELETE` only (#173): close the ports but keep their host ports for the box (`closed`). */
|
|
472
|
+
keep: z2.boolean().optional()
|
|
466
473
|
});
|
|
467
474
|
var BoxPortSchema = z2.number().int().min(1).max(65535);
|
|
468
475
|
var BoxLinkSchema = z2.object({ from: z2.string(), port: BoxPortSchema });
|
|
@@ -646,6 +653,11 @@ var CreateVolumeRequest = z2.object({
|
|
|
646
653
|
var AttachVolumeRequest = z2.object({
|
|
647
654
|
box: z2.string()
|
|
648
655
|
});
|
|
656
|
+
var ResizeVolumeRequest = z2.object({
|
|
657
|
+
sizeGib: z2.number().int().min(1).max(MAX_VOLUME_GIB)
|
|
658
|
+
});
|
|
659
|
+
var RunScriptRequest = z2.object({ input: z2.unknown().optional() });
|
|
660
|
+
var ScriptOutputSchema = z2.object({ output: z2.unknown() });
|
|
649
661
|
var EnvVarSchema = z2.object({
|
|
650
662
|
name: z2.string(),
|
|
651
663
|
value: z2.string().optional(),
|
|
@@ -901,11 +913,31 @@ var VolumeUsageSchema = z2.object({
|
|
|
901
913
|
cost: z2.object({ amount: z2.number(), currency: z2.string() }).optional(),
|
|
902
914
|
historyFrom: z2.string().nullable()
|
|
903
915
|
});
|
|
916
|
+
var UsageDayItemSchema = z2.object({
|
|
917
|
+
id: z2.string(),
|
|
918
|
+
name: z2.string(),
|
|
919
|
+
owner: z2.string(),
|
|
920
|
+
/** Removed by the end of the report's window. */
|
|
921
|
+
removed: z2.boolean(),
|
|
922
|
+
cost: z2.number()
|
|
923
|
+
});
|
|
924
|
+
var UsageDaySchema = z2.object({
|
|
925
|
+
/** `YYYY-MM-DD`, UTC. */
|
|
926
|
+
date: z2.string(),
|
|
927
|
+
from: z2.string(),
|
|
928
|
+
to: z2.string(),
|
|
929
|
+
cost: z2.number(),
|
|
930
|
+
currency: z2.string(),
|
|
931
|
+
boxes: z2.array(UsageDayItemSchema),
|
|
932
|
+
volumes: z2.array(UsageDayItemSchema)
|
|
933
|
+
});
|
|
904
934
|
var UsageReportSchema = z2.object({
|
|
905
935
|
window: z2.object({ from: z2.string(), to: z2.string() }),
|
|
906
936
|
boxes: z2.array(BoxUsageSchema),
|
|
907
937
|
/** Absent from a side older than #91. */
|
|
908
938
|
volumes: z2.array(VolumeUsageSchema).optional(),
|
|
939
|
+
/** With `?by=day` only, oldest day first. */
|
|
940
|
+
days: z2.array(UsageDaySchema).optional(),
|
|
909
941
|
totals: z2.object({
|
|
910
942
|
seconds: MachineUsageSchema.shape.seconds,
|
|
911
943
|
vcpuHours: z2.number(),
|
|
@@ -1140,7 +1172,9 @@ var TrustCaReportSchema = z2.object({
|
|
|
1140
1172
|
pending: z2.array(z2.object({ box: z2.string(), when: SshKeysAppliedSchema, why: z2.string().optional() })),
|
|
1141
1173
|
failed: z2.array(z2.object({ box: z2.string(), error: z2.string() })),
|
|
1142
1174
|
/** Local boxes, untouched: they trust the host's key only. */
|
|
1143
|
-
local: z2.number().int()
|
|
1175
|
+
local: z2.number().int(),
|
|
1176
|
+
/** Cloud boxes made without a CA line (a managed service's, #173), untouched: no certificate opens them. Absent from older servers. */
|
|
1177
|
+
skipped: z2.array(z2.string()).optional()
|
|
1144
1178
|
});
|
|
1145
1179
|
var HostJoinRequestSchema = z2.object({
|
|
1146
1180
|
token: z2.string().min(1),
|
|
@@ -1175,6 +1209,128 @@ var HostInfoSchema = z2.object({
|
|
|
1175
1209
|
var HostPatchRequest = z2.object({ country: z2.string().nullable() });
|
|
1176
1210
|
var CountrySchema = z2.object({ code: z2.string(), name: z2.string() });
|
|
1177
1211
|
|
|
1212
|
+
// ../shared/src/managed.ts
|
|
1213
|
+
import { z as z4 } from "zod";
|
|
1214
|
+
|
|
1215
|
+
// ../shared/src/usage.ts
|
|
1216
|
+
import { z as z3 } from "zod";
|
|
1217
|
+
var PricingSchema = z3.object({
|
|
1218
|
+
currency: z3.string().min(1),
|
|
1219
|
+
vcpuHour: z3.number().nonnegative(),
|
|
1220
|
+
ramGibHour: z3.number().nonnegative(),
|
|
1221
|
+
diskGibHour: z3.number().nonnegative(),
|
|
1222
|
+
volumeHour: z3.number().nonnegative().default(0),
|
|
1223
|
+
volumeGibHour: z3.number().nonnegative().default(0),
|
|
1224
|
+
diskIncludedGib: z3.number().int().nonnegative().default(0)
|
|
1225
|
+
});
|
|
1226
|
+
var PriceListSchema = PricingSchema.extend({ welcomeCredit: z3.number().nonnegative() });
|
|
1227
|
+
var HOUR_MS = 36e5;
|
|
1228
|
+
var round = (n, places) => Math.round(n * 10 ** places) / 10 ** places;
|
|
1229
|
+
function costOf(t, pricing) {
|
|
1230
|
+
return t.vcpuHours * pricing.vcpuHour + t.ramGibHours * pricing.ramGibHour + t.diskGibHours * pricing.diskGibHour;
|
|
1231
|
+
}
|
|
1232
|
+
function sumUsage(items, pricing) {
|
|
1233
|
+
const sum = {
|
|
1234
|
+
seconds: { running: 0, standby: 0, hibernated: 0, stopped: 0 },
|
|
1235
|
+
vcpuHours: 0,
|
|
1236
|
+
ramGibHours: 0,
|
|
1237
|
+
diskGibHours: 0
|
|
1238
|
+
};
|
|
1239
|
+
for (const t of items) {
|
|
1240
|
+
for (const s of Object.keys(sum.seconds)) sum.seconds[s] += t.seconds[s];
|
|
1241
|
+
sum.vcpuHours += t.vcpuHours;
|
|
1242
|
+
sum.ramGibHours += t.ramGibHours;
|
|
1243
|
+
sum.diskGibHours += t.diskGibHours;
|
|
1244
|
+
}
|
|
1245
|
+
sum.vcpuHours = round(sum.vcpuHours, 3);
|
|
1246
|
+
sum.ramGibHours = round(sum.ramGibHours, 3);
|
|
1247
|
+
sum.diskGibHours = round(sum.diskGibHours, 3);
|
|
1248
|
+
if (pricing !== void 0) sum.cost = { amount: round(costOf(sum, pricing), 4), currency: pricing.currency };
|
|
1249
|
+
return sum;
|
|
1250
|
+
}
|
|
1251
|
+
var DAY_MS = 24 * HOUR_MS;
|
|
1252
|
+
|
|
1253
|
+
// ../shared/src/managed.ts
|
|
1254
|
+
var PriceSchema = z4.object({ perMonth: z4.number(), storagePerMonth: z4.number(), currency: z4.string() });
|
|
1255
|
+
var ManagedCatalogEntrySchema = z4.object({
|
|
1256
|
+
/** The kind, as in `/managed/<kind>`: `postgres`. */
|
|
1257
|
+
kind: z4.string(),
|
|
1258
|
+
/** The CLI command for it: `pg` (`kvbox pg create …`). */
|
|
1259
|
+
command: z4.string(),
|
|
1260
|
+
title: z4.string(),
|
|
1261
|
+
description: z4.string(),
|
|
1262
|
+
/** The version a new one runs, as shown: `18`. */
|
|
1263
|
+
version: z4.string(),
|
|
1264
|
+
/** The port it serves on, privately and (when public) behind a host port. */
|
|
1265
|
+
port: z4.number().int(),
|
|
1266
|
+
storage: z4.object({ defaultGib: z4.number().int(), minGib: z4.number().int(), maxGib: z4.number().int() }),
|
|
1267
|
+
/** What create takes beside the name, for a form: each optional, `default` in words. */
|
|
1268
|
+
options: z4.array(z4.object({ name: z4.string(), title: z4.string(), description: z4.string(), default: z4.string(), secret: z4.boolean().optional() })),
|
|
1269
|
+
/** A month of it running with the default storage, and each further GB of storage. */
|
|
1270
|
+
price: PriceSchema.extend({ storageGibPerMonth: z4.number() }),
|
|
1271
|
+
/** Whether a host answering now can run it. */
|
|
1272
|
+
available: z4.boolean()
|
|
1273
|
+
});
|
|
1274
|
+
var CreateManagedRequest = z4.object({
|
|
1275
|
+
name: z4.string(),
|
|
1276
|
+
database: z4.string().optional(),
|
|
1277
|
+
user: z4.string().optional(),
|
|
1278
|
+
password: z4.string().optional(),
|
|
1279
|
+
storageGib: z4.number().int().optional(),
|
|
1280
|
+
/** Place it on the host of this box, so the box can be linked to it. */
|
|
1281
|
+
near: z4.string().optional()
|
|
1282
|
+
});
|
|
1283
|
+
var ManagedHealthSchema = z4.object({
|
|
1284
|
+
/** Its first start has finished: the user and the database exist. */
|
|
1285
|
+
ready: z4.boolean(),
|
|
1286
|
+
/** It accepts connections. */
|
|
1287
|
+
accepting: z4.boolean(),
|
|
1288
|
+
version: z4.string(),
|
|
1289
|
+
connections: z4.number().int(),
|
|
1290
|
+
/** The size of the data itself. */
|
|
1291
|
+
dataBytes: z4.number().optional(),
|
|
1292
|
+
/** The storage volume: what is used of it and its size. */
|
|
1293
|
+
storageUsedBytes: z4.number().optional(),
|
|
1294
|
+
storageTotalBytes: z4.number().optional()
|
|
1295
|
+
});
|
|
1296
|
+
var UrlsSchema = z4.object({ private: z4.string(), public: z4.string().optional() });
|
|
1297
|
+
var ManagedSchema = z4.object({
|
|
1298
|
+
kind: z4.string(),
|
|
1299
|
+
name: z4.string(),
|
|
1300
|
+
/** The owning slug, bare. */
|
|
1301
|
+
owner: z4.string(),
|
|
1302
|
+
ownerKind: z4.enum(["user", "org"]),
|
|
1303
|
+
/** `running`, `stopped` (storage only is charged), `hibernated` or `standby` (out of credit), `unknown` (its host does not answer). */
|
|
1304
|
+
status: BoxStatusSchema,
|
|
1305
|
+
/** The catalog version: `18`. */
|
|
1306
|
+
version: z4.string(),
|
|
1307
|
+
database: z4.string(),
|
|
1308
|
+
user: z4.string(),
|
|
1309
|
+
storageGib: z4.number().int(),
|
|
1310
|
+
/** Where a linked box reaches it: `<name>.<slug>.kvbox` (resolves from a box of any slug it is linked to) and the port. */
|
|
1311
|
+
private: z4.object({ host: z4.string(), port: z4.number().int() }),
|
|
1312
|
+
/** Where the internet reaches it, null while it is not public. The port is its own for its whole life. */
|
|
1313
|
+
public: z4.object({ host: z4.string(), port: z4.number().int() }).nullable(),
|
|
1314
|
+
/** The boxes linked to it, as the caller would name them. */
|
|
1315
|
+
links: z4.array(z4.string()),
|
|
1316
|
+
/** The name of the host it runs on, for `--near` and links. */
|
|
1317
|
+
host: z4.string(),
|
|
1318
|
+
country: z4.string().optional(),
|
|
1319
|
+
createdBy: z4.string().nullable(),
|
|
1320
|
+
createdAt: z4.string(),
|
|
1321
|
+
/** Connection strings with the password as `****`. */
|
|
1322
|
+
urls: UrlsSchema,
|
|
1323
|
+
price: PriceSchema,
|
|
1324
|
+
/** Only in `GET /managed/<kind>/<name>` (asked live): null when it is not running or did not answer. */
|
|
1325
|
+
health: ManagedHealthSchema.nullable().optional()
|
|
1326
|
+
});
|
|
1327
|
+
var ManagedSecretSchema = z4.object({ user: z4.string(), password: z4.string(), urls: UrlsSchema });
|
|
1328
|
+
var CreatedManagedSchema = ManagedSchema.extend({ secret: ManagedSecretSchema });
|
|
1329
|
+
var ResetPasswordRequest = z4.object({ password: z4.string().optional() });
|
|
1330
|
+
var SetPublicRequest = z4.object({ on: z4.boolean() });
|
|
1331
|
+
var ManagedLinkRequest = z4.object({ box: z4.string() });
|
|
1332
|
+
var GrowManagedRequest = z4.object({ storageGib: z4.number().int() });
|
|
1333
|
+
|
|
1178
1334
|
// src/kvm.ts
|
|
1179
1335
|
import { accessSync, constants, readFileSync, statSync } from "fs";
|
|
1180
1336
|
import { userInfo } from "os";
|
|
@@ -1242,7 +1398,7 @@ function unitActive() {
|
|
|
1242
1398
|
// src/client.ts
|
|
1243
1399
|
var ORG_SLUG_RE = /^[a-z0-9][a-z0-9._-]{0,31}$/;
|
|
1244
1400
|
function orgOption(opts) {
|
|
1245
|
-
if (opts.org === void 0 || opts.org === "") return void 0;
|
|
1401
|
+
if (opts.org === void 0 || opts.org === "" || opts.org === "-") return void 0;
|
|
1246
1402
|
if (!ORG_SLUG_RE.test(opts.org)) throw new KvboxError(`${opts.org}: not an org slug (lowercase letters, digits, '.', '_' or '-')`);
|
|
1247
1403
|
return opts.org;
|
|
1248
1404
|
}
|
|
@@ -1329,7 +1485,7 @@ var ServerClient = class {
|
|
|
1329
1485
|
* `all`: every box on the platform (platform admins only, #78).
|
|
1330
1486
|
*/
|
|
1331
1487
|
listMachines(opts = {}) {
|
|
1332
|
-
return this.call("GET", `/machines${allQuery(opts)}`,
|
|
1488
|
+
return this.call("GET", `/machines${allQuery(opts)}`, z5.array(MachineSchema), void 0, opts.org);
|
|
1333
1489
|
}
|
|
1334
1490
|
createMachine(req) {
|
|
1335
1491
|
return this.call("POST", "/machines", MachineSchema, req);
|
|
@@ -1383,10 +1539,10 @@ var ServerClient = class {
|
|
|
1383
1539
|
}
|
|
1384
1540
|
/** Backups, the control plane's only: a box's disk in object storage. Newest first. */
|
|
1385
1541
|
listBackups() {
|
|
1386
|
-
return this.call("GET", "/backups",
|
|
1542
|
+
return this.call("GET", "/backups", z5.array(BackupSchema));
|
|
1387
1543
|
}
|
|
1388
1544
|
boxBackups(name) {
|
|
1389
|
-
return this.call("GET", `/machines/${encodeURIComponent(name)}/backups`,
|
|
1545
|
+
return this.call("GET", `/machines/${encodeURIComponent(name)}/backups`, z5.array(BackupSchema));
|
|
1390
1546
|
}
|
|
1391
1547
|
boxBackup(name, id) {
|
|
1392
1548
|
return this.call("GET", `/machines/${encodeURIComponent(name)}/backups/${encodeURIComponent(id)}`, BackupSchema);
|
|
@@ -1401,7 +1557,7 @@ var ServerClient = class {
|
|
|
1401
1557
|
}
|
|
1402
1558
|
/** Wake hooks (#101), the control plane's only: a URL and a secret that wake the box. */
|
|
1403
1559
|
boxHooks(name) {
|
|
1404
|
-
return this.call("GET", `/machines/${encodeURIComponent(name)}/hooks`,
|
|
1560
|
+
return this.call("GET", `/machines/${encodeURIComponent(name)}/hooks`, z5.array(WakeHookSchema));
|
|
1405
1561
|
}
|
|
1406
1562
|
createBoxHook(name) {
|
|
1407
1563
|
return this.call("POST", `/machines/${encodeURIComponent(name)}/hooks`, CreatedWakeHookSchema);
|
|
@@ -1429,7 +1585,7 @@ var ServerClient = class {
|
|
|
1429
1585
|
}
|
|
1430
1586
|
/** Every readable box's subdomains, one entry each. */
|
|
1431
1587
|
listSubdomains() {
|
|
1432
|
-
return this.call("GET", "/subdomains",
|
|
1588
|
+
return this.call("GET", "/subdomains", z5.array(SubdomainSchema));
|
|
1433
1589
|
}
|
|
1434
1590
|
/** Custom domains (#89), the control plane's only: serve the box at the user's own name. */
|
|
1435
1591
|
addDomain(name, req) {
|
|
@@ -1440,7 +1596,7 @@ var ServerClient = class {
|
|
|
1440
1596
|
}
|
|
1441
1597
|
/** Every readable box's custom domains. */
|
|
1442
1598
|
listDomains() {
|
|
1443
|
-
return this.call("GET", "/domains",
|
|
1599
|
+
return this.call("GET", "/domains", z5.array(DomainSchema));
|
|
1444
1600
|
}
|
|
1445
1601
|
/** A running box's own load, memory and disk, asked of its guest (#67). */
|
|
1446
1602
|
machineStats(name) {
|
|
@@ -1469,22 +1625,22 @@ var ServerClient = class {
|
|
|
1469
1625
|
return this.call("POST", "/billing/redeem", RedeemResponseSchema, { code });
|
|
1470
1626
|
}
|
|
1471
1627
|
deleteMachine(name) {
|
|
1472
|
-
return this.call("DELETE", `/machines/${encodeURIComponent(name)}`,
|
|
1628
|
+
return this.call("DELETE", `/machines/${encodeURIComponent(name)}`, z5.undefined());
|
|
1473
1629
|
}
|
|
1474
1630
|
/** Like `listMachines`, for snapshots. */
|
|
1475
1631
|
listSnapshots(opts = {}) {
|
|
1476
|
-
return this.call("GET", `/snapshots${allQuery(opts)}`,
|
|
1632
|
+
return this.call("GET", `/snapshots${allQuery(opts)}`, z5.array(SnapshotSchema), void 0, opts.org);
|
|
1477
1633
|
}
|
|
1478
1634
|
createSnapshot(req) {
|
|
1479
1635
|
return this.call("POST", "/snapshots", SnapshotSchema, req);
|
|
1480
1636
|
}
|
|
1481
1637
|
deleteSnapshot(name) {
|
|
1482
|
-
return this.call("DELETE", `/snapshots/${encodeURIComponent(name)}`,
|
|
1638
|
+
return this.call("DELETE", `/snapshots/${encodeURIComponent(name)}`, z5.undefined());
|
|
1483
1639
|
}
|
|
1484
1640
|
// ---- environment variables (#88), a control plane only: the caller's slug, or `--org`'s ----
|
|
1485
1641
|
/** `org`: that org's list instead of `--org`'s (or the caller's own). */
|
|
1486
1642
|
listEnv(org) {
|
|
1487
|
-
return this.call("GET", "/vars",
|
|
1643
|
+
return this.call("GET", "/vars", z5.array(EnvVarSchema), void 0, org);
|
|
1488
1644
|
}
|
|
1489
1645
|
setEnv(name, req) {
|
|
1490
1646
|
return this.call("PUT", `/vars/${encodeURIComponent(name)}`, EnvChangeSchema, req);
|
|
@@ -1504,13 +1660,13 @@ var ServerClient = class {
|
|
|
1504
1660
|
}
|
|
1505
1661
|
// ---- volumes (#52) ----
|
|
1506
1662
|
listVolumes(opts = {}) {
|
|
1507
|
-
return this.call("GET", `/volumes${allQuery(opts)}`,
|
|
1663
|
+
return this.call("GET", `/volumes${allQuery(opts)}`, z5.array(VolumeSchema), void 0, opts.org);
|
|
1508
1664
|
}
|
|
1509
1665
|
createVolume(req) {
|
|
1510
1666
|
return this.call("POST", "/volumes", VolumeSchema, req);
|
|
1511
1667
|
}
|
|
1512
1668
|
deleteVolume(name) {
|
|
1513
|
-
return this.call("DELETE", `/volumes/${encodeURIComponent(name)}`,
|
|
1669
|
+
return this.call("DELETE", `/volumes/${encodeURIComponent(name)}`, z5.undefined());
|
|
1514
1670
|
}
|
|
1515
1671
|
/** Both answer with the box as it is afterwards (restarted when it was running). */
|
|
1516
1672
|
attachVolume(name, req) {
|
|
@@ -1519,19 +1675,68 @@ var ServerClient = class {
|
|
|
1519
1675
|
detachVolume(name, req) {
|
|
1520
1676
|
return this.call("POST", `/volumes/${encodeURIComponent(name)}/detach`, MachineSchema, req);
|
|
1521
1677
|
}
|
|
1678
|
+
/** #147: answers the volume at its new size; a running box holding it has restarted by then. */
|
|
1679
|
+
resizeVolume(name, sizeGib) {
|
|
1680
|
+
return this.call("POST", `/volumes/${encodeURIComponent(name)}/resize`, VolumeSchema, { sizeGib });
|
|
1681
|
+
}
|
|
1682
|
+
// ---- managed services (#173): the control plane's alone ----
|
|
1683
|
+
managedPath(kind, name, rest = "") {
|
|
1684
|
+
return `/managed/${encodeURIComponent(kind)}${name === void 0 ? "" : `/${encodeURIComponent(name)}`}${rest}`;
|
|
1685
|
+
}
|
|
1686
|
+
managedCatalog() {
|
|
1687
|
+
return this.call("GET", "/managed/catalog", z5.array(ManagedCatalogEntrySchema));
|
|
1688
|
+
}
|
|
1689
|
+
/** One kind's services, or every kind's when `kind` is left out. */
|
|
1690
|
+
listManaged(kind, opts = {}) {
|
|
1691
|
+
return this.call("GET", `${kind === void 0 ? "/managed" : this.managedPath(kind)}${allQuery(opts)}`, z5.array(ManagedSchema), void 0, opts.org);
|
|
1692
|
+
}
|
|
1693
|
+
createManaged(kind, req) {
|
|
1694
|
+
return this.call("POST", this.managedPath(kind), CreatedManagedSchema, req);
|
|
1695
|
+
}
|
|
1696
|
+
/** With `health`, asked of the service itself. */
|
|
1697
|
+
getManaged(kind, name) {
|
|
1698
|
+
return this.call("GET", this.managedPath(kind, name), ManagedSchema);
|
|
1699
|
+
}
|
|
1700
|
+
deleteManaged(kind, name) {
|
|
1701
|
+
return this.call("DELETE", this.managedPath(kind, name), z5.undefined());
|
|
1702
|
+
}
|
|
1703
|
+
managedPassword(kind, name) {
|
|
1704
|
+
return this.call("GET", this.managedPath(kind, name, "/password"), ManagedSecretSchema);
|
|
1705
|
+
}
|
|
1706
|
+
resetManagedPassword(kind, name, password) {
|
|
1707
|
+
return this.call("POST", this.managedPath(kind, name, "/password"), ManagedSecretSchema, password === void 0 ? {} : { password });
|
|
1708
|
+
}
|
|
1709
|
+
setManagedPublic(kind, name, on) {
|
|
1710
|
+
return this.call("POST", this.managedPath(kind, name, "/public"), ManagedSchema, { on });
|
|
1711
|
+
}
|
|
1712
|
+
linkManaged(kind, name, box) {
|
|
1713
|
+
return this.call("POST", this.managedPath(kind, name, "/links"), ManagedSchema, { box });
|
|
1714
|
+
}
|
|
1715
|
+
unlinkManaged(kind, name, box) {
|
|
1716
|
+
return this.call("DELETE", this.managedPath(kind, name, "/links"), ManagedSchema, { box });
|
|
1717
|
+
}
|
|
1718
|
+
growManaged(kind, name, storageGib) {
|
|
1719
|
+
return this.call("POST", this.managedPath(kind, name, "/grow"), ManagedSchema, { storageGib });
|
|
1720
|
+
}
|
|
1721
|
+
stopManaged(kind, name) {
|
|
1722
|
+
return this.call("POST", this.managedPath(kind, name, "/stop"), ManagedSchema);
|
|
1723
|
+
}
|
|
1724
|
+
startManaged(kind, name) {
|
|
1725
|
+
return this.call("POST", this.managedPath(kind, name, "/start"), ManagedSchema);
|
|
1726
|
+
}
|
|
1522
1727
|
// ---- control plane only (`kvbox login`, `logout`, `whoami`) ----
|
|
1523
1728
|
login(req) {
|
|
1524
1729
|
return this.call("POST", "/login", LoginResponseSchema, req);
|
|
1525
1730
|
}
|
|
1526
1731
|
logout() {
|
|
1527
|
-
return this.call("POST", "/logout",
|
|
1732
|
+
return this.call("POST", "/logout", z5.unknown()).then(() => void 0);
|
|
1528
1733
|
}
|
|
1529
1734
|
me() {
|
|
1530
1735
|
return this.call("GET", "/me", MeResponseSchema);
|
|
1531
1736
|
}
|
|
1532
1737
|
// ---- orgs (`kvbox org …`) ----
|
|
1533
1738
|
listOrgs() {
|
|
1534
|
-
return this.call("GET", "/orgs",
|
|
1739
|
+
return this.call("GET", "/orgs", z5.array(OrgMembershipSchema));
|
|
1535
1740
|
}
|
|
1536
1741
|
createOrg(req) {
|
|
1537
1742
|
return this.call("POST", "/orgs", CloudOrgSchema, req);
|
|
@@ -1540,16 +1745,16 @@ var ServerClient = class {
|
|
|
1540
1745
|
return this.call("GET", `/orgs/${encodeURIComponent(slug)}`, OrgDetailSchema);
|
|
1541
1746
|
}
|
|
1542
1747
|
deleteOrg(slug) {
|
|
1543
|
-
return this.call("DELETE", `/orgs/${encodeURIComponent(slug)}`,
|
|
1748
|
+
return this.call("DELETE", `/orgs/${encodeURIComponent(slug)}`, z5.undefined());
|
|
1544
1749
|
}
|
|
1545
1750
|
addMember(slug, req) {
|
|
1546
|
-
return this.call("POST", `/orgs/${encodeURIComponent(slug)}/members`,
|
|
1751
|
+
return this.call("POST", `/orgs/${encodeURIComponent(slug)}/members`, z5.undefined(), req);
|
|
1547
1752
|
}
|
|
1548
1753
|
setRole(slug, username, role) {
|
|
1549
|
-
return this.call("PATCH", `/orgs/${encodeURIComponent(slug)}/members/${encodeURIComponent(username)}`,
|
|
1754
|
+
return this.call("PATCH", `/orgs/${encodeURIComponent(slug)}/members/${encodeURIComponent(username)}`, z5.unknown(), { role }).then(() => void 0);
|
|
1550
1755
|
}
|
|
1551
1756
|
removeMember(slug, username) {
|
|
1552
|
-
return this.call("DELETE", `/orgs/${encodeURIComponent(slug)}/members/${encodeURIComponent(username)}`,
|
|
1757
|
+
return this.call("DELETE", `/orgs/${encodeURIComponent(slug)}/members/${encodeURIComponent(username)}`, z5.undefined());
|
|
1553
1758
|
}
|
|
1554
1759
|
/** Sign this machine's key again (#51); the key must be one the user logged in with. */
|
|
1555
1760
|
sshCert(req) {
|
|
@@ -3727,44 +3932,6 @@ var orgCommand = new Command50("org").description("orgs: shared slugs that own b
|
|
|
3727
3932
|
|
|
3728
3933
|
// src/commands/usage.ts
|
|
3729
3934
|
import { Command as Command51 } from "commander";
|
|
3730
|
-
|
|
3731
|
-
// ../shared/src/usage.ts
|
|
3732
|
-
import { z as z4 } from "zod";
|
|
3733
|
-
var PricingSchema = z4.object({
|
|
3734
|
-
currency: z4.string().min(1),
|
|
3735
|
-
vcpuHour: z4.number().nonnegative(),
|
|
3736
|
-
ramGibHour: z4.number().nonnegative(),
|
|
3737
|
-
diskGibHour: z4.number().nonnegative(),
|
|
3738
|
-
volumeHour: z4.number().nonnegative().default(0),
|
|
3739
|
-
volumeGibHour: z4.number().nonnegative().default(0),
|
|
3740
|
-
diskIncludedGib: z4.number().int().nonnegative().default(0)
|
|
3741
|
-
});
|
|
3742
|
-
var PriceListSchema = PricingSchema.extend({ welcomeCredit: z4.number().nonnegative() });
|
|
3743
|
-
var round = (n, places) => Math.round(n * 10 ** places) / 10 ** places;
|
|
3744
|
-
function costOf(t, pricing) {
|
|
3745
|
-
return t.vcpuHours * pricing.vcpuHour + t.ramGibHours * pricing.ramGibHour + t.diskGibHours * pricing.diskGibHour;
|
|
3746
|
-
}
|
|
3747
|
-
function sumUsage(items, pricing) {
|
|
3748
|
-
const sum = {
|
|
3749
|
-
seconds: { running: 0, standby: 0, hibernated: 0, stopped: 0 },
|
|
3750
|
-
vcpuHours: 0,
|
|
3751
|
-
ramGibHours: 0,
|
|
3752
|
-
diskGibHours: 0
|
|
3753
|
-
};
|
|
3754
|
-
for (const t of items) {
|
|
3755
|
-
for (const s of Object.keys(sum.seconds)) sum.seconds[s] += t.seconds[s];
|
|
3756
|
-
sum.vcpuHours += t.vcpuHours;
|
|
3757
|
-
sum.ramGibHours += t.ramGibHours;
|
|
3758
|
-
sum.diskGibHours += t.diskGibHours;
|
|
3759
|
-
}
|
|
3760
|
-
sum.vcpuHours = round(sum.vcpuHours, 3);
|
|
3761
|
-
sum.ramGibHours = round(sum.ramGibHours, 3);
|
|
3762
|
-
sum.diskGibHours = round(sum.diskGibHours, 3);
|
|
3763
|
-
if (pricing !== void 0) sum.cost = { amount: round(costOf(sum, pricing), 4), currency: pricing.currency };
|
|
3764
|
-
return sum;
|
|
3765
|
-
}
|
|
3766
|
-
|
|
3767
|
-
// src/commands/usage.ts
|
|
3768
3935
|
var usageCommand2 = new Command51("usage").description("what your boxes and volumes used this month (or --since/--until): running time, vCPU-h, RAM and disk GiB-h, cost").option("--since <date>", "count from this date, e.g. 2026-10-01 (default: the 1st of this month)").option("--until <date>", "count up to this date (default: now)").option("--org <slug>", "one org's boxes only").option("-a, --all", "every box on the platform (platform admins only)").option("--by-user", "one line per user who made the boxes (e.g. who spent what in --org)").option("--json", "print as JSON").action(async (opts, cmd) => {
|
|
3769
3936
|
for (const [name, value] of [
|
|
3770
3937
|
["since", opts.since],
|
|
@@ -3968,7 +4135,7 @@ import { Command as Command54, InvalidArgumentError as InvalidArgumentError2, Op
|
|
|
3968
4135
|
// src/commands/deploy/config.ts
|
|
3969
4136
|
import { readFile as readFile6, writeFile as writeFile4 } from "fs/promises";
|
|
3970
4137
|
import { isAbsolute, join as join6, normalize, relative as relative2 } from "path";
|
|
3971
|
-
import { z as
|
|
4138
|
+
import { z as z6 } from "zod";
|
|
3972
4139
|
|
|
3973
4140
|
// src/commands/deploy/templates/docker.ts
|
|
3974
4141
|
import { readFile as readFile5 } from "fs/promises";
|
|
@@ -3980,6 +4147,19 @@ var SERVICE = "kvbox-app";
|
|
|
3980
4147
|
var START_WAIT_SEC = 30;
|
|
3981
4148
|
var APP_NOT_LISTENING = 4;
|
|
3982
4149
|
var LOG_MARKER = "--- kvbox-app log ---";
|
|
4150
|
+
var RELEASE_FAILED = 12;
|
|
4151
|
+
var RELEASE_LOG_LINES = 40;
|
|
4152
|
+
var DEP_WAIT_SEC = 60;
|
|
4153
|
+
var BOX_ENV_PLAIN = "/home/kvbox/.ssh/environment";
|
|
4154
|
+
var shq = (s) => `'${s.replaceAll("'", `'\\''`)}'`;
|
|
4155
|
+
function waitFor(deps) {
|
|
4156
|
+
return deps.map(
|
|
4157
|
+
(d) => `ExecStartPre=/bin/bash -c 'timeout ${DEP_WAIT_SEC} bash -c "until (exec 3<>/dev/tcp/127.0.0.1/${d.port}) 2>/dev/null; do sleep 1; done" || { echo "kvbox: the database ${d.unit} did not answer on port ${d.port} within ${DEP_WAIT_SEC} s, so this service was not started"; exit 1; }'`
|
|
4158
|
+
);
|
|
4159
|
+
}
|
|
4160
|
+
function releaseStep(run3, root) {
|
|
4161
|
+
return [`if ! ${run3} > ${root}/.release.log 2>&1 </dev/null; then echo '${LOG_MARKER}'; tail -n ${RELEASE_LOG_LINES} ${root}/.release.log; exit ${RELEASE_FAILED}; fi`];
|
|
4162
|
+
}
|
|
3983
4163
|
var DROP_CONTAINER = `if systemctl is-active --quiet docker && docker inspect ${SERVICE} >/dev/null 2>&1; then docker rm -f ${SERVICE} >/dev/null 2>&1 || true; docker rmi ${SERVICE} >/dev/null 2>&1 || true; sudo -n systemctl disable --quiet docker 2>/dev/null || true; fi`;
|
|
3984
4164
|
function commands(pm) {
|
|
3985
4165
|
switch (pm) {
|
|
@@ -4000,14 +4180,17 @@ function serverRemote(opts) {
|
|
|
4000
4180
|
const root = opts.target?.root ?? "/srv/app";
|
|
4001
4181
|
const appDir = opts.target ? `${root}/current` : APP_DIR;
|
|
4002
4182
|
const service = opts.target?.unit ?? SERVICE;
|
|
4183
|
+
const after = opts.target?.after ?? [];
|
|
4184
|
+
const release = opts.target?.release;
|
|
4003
4185
|
const unit = [
|
|
4004
4186
|
"[Unit]",
|
|
4005
4187
|
"Description=kvbox deploy: the app in " + appDir,
|
|
4006
|
-
"After=network.target",
|
|
4188
|
+
["After=network.target", ...after.map((d) => `${d.unit}.service`)].join(" "),
|
|
4007
4189
|
"",
|
|
4008
4190
|
"[Service]",
|
|
4009
4191
|
"User=kvbox",
|
|
4010
4192
|
`WorkingDirectory=${appDir}`,
|
|
4193
|
+
...waitFor(after),
|
|
4011
4194
|
// PORT on the command line: the proxied address points at it, and nothing in the environment
|
|
4012
4195
|
// file below may move the app off it (env's PORT, say, copied from a .env).
|
|
4013
4196
|
`ExecStart=/usr/bin/env PORT=${opts.port} ${c2.start}`,
|
|
@@ -4032,6 +4215,9 @@ function serverRemote(opts) {
|
|
|
4032
4215
|
...c2.setup,
|
|
4033
4216
|
c2.install,
|
|
4034
4217
|
...opts.build ? [c2.build] : [],
|
|
4218
|
+
// In the new version's directory, with what the service itself will get: the box's variables
|
|
4219
|
+
// (read from the file, a session opened right after a boot may not have them yet), PORT, NODE_ENV.
|
|
4220
|
+
...release !== void 0 ? releaseStep(`( if [ -r ${BOX_ENV_PLAIN} ]; then while IFS= read -r kv; do [ -z "$kv" ] || export "$kv"; done < ${BOX_ENV_PLAIN}; fi; export PORT=${opts.port} NODE_ENV=production; sh -c ${shq(release)} )`, root) : [],
|
|
4035
4221
|
"cd /",
|
|
4036
4222
|
// Before the unit is written: in a stack the single deploy's unit is the one being dropped.
|
|
4037
4223
|
...opts.target ? [DROP_SINGLE] : [],
|
|
@@ -4044,7 +4230,8 @@ function serverRemote(opts) {
|
|
|
4044
4230
|
`sudo -n systemctl enable --quiet ${service}`,
|
|
4045
4231
|
// A box that switched from the Docker template: its container holds the same box port.
|
|
4046
4232
|
...opts.target ? [] : [DROP_CONTAINER],
|
|
4047
|
-
|
|
4233
|
+
// With databases to wait for, the restart itself fails when one never comes: the wait below shows why.
|
|
4234
|
+
`sudo -n systemctl restart ${service}${after.length > 0 ? " || true" : ""}`,
|
|
4048
4235
|
`rm -rf ${root}/.old`,
|
|
4049
4236
|
// bash's /dev/tcp: no curl or nc in the image needed.
|
|
4050
4237
|
"i=0",
|
|
@@ -4121,18 +4308,20 @@ function dockerRemote(port, containerPort) {
|
|
|
4121
4308
|
"done"
|
|
4122
4309
|
].join("\n");
|
|
4123
4310
|
}
|
|
4124
|
-
var BOX_ENV_PLAIN = "/home/kvbox/.ssh/environment";
|
|
4125
4311
|
function dockerServiceRemote(port, target, dockerfile) {
|
|
4126
4312
|
const image = target.unit;
|
|
4313
|
+
const after = target.after ?? [];
|
|
4314
|
+
const built = target.release !== void 0 ? `${image}:next` : image;
|
|
4127
4315
|
const unit = [
|
|
4128
4316
|
"[Unit]",
|
|
4129
4317
|
`Description=kvbox deploy: the container ${image}`,
|
|
4130
|
-
"After=docker.service network-online.target",
|
|
4318
|
+
["After=docker.service network-online.target", ...after.map((d) => `${d.unit}.service`)].join(" "),
|
|
4131
4319
|
"Requires=docker.service",
|
|
4132
4320
|
"",
|
|
4133
4321
|
"[Service]",
|
|
4134
4322
|
// A box that never had a variable has no file yet, and docker refuses a missing --env-file.
|
|
4135
4323
|
`ExecStartPre=/bin/sh -c '[ -e ${BOX_ENV_PLAIN} ] || install -m 600 -o kvbox -g kvbox /dev/null ${BOX_ENV_PLAIN}'`,
|
|
4324
|
+
...waitFor(after),
|
|
4136
4325
|
`ExecStartPre=-/usr/bin/docker rm -f ${image}`,
|
|
4137
4326
|
`ExecStart=/usr/bin/docker run --rm --name ${image} --network host --env-file ${BOX_ENV_PLAIN} -e PORT=${port} ${image}`,
|
|
4138
4327
|
`ExecStop=/usr/bin/docker stop ${image}`,
|
|
@@ -4152,8 +4341,14 @@ function dockerServiceRemote(port, target, dockerfile) {
|
|
|
4152
4341
|
`tar -xf - -C ${root}/.incoming`,
|
|
4153
4342
|
`cd ${root}/.incoming`,
|
|
4154
4343
|
// Quoted: the path is the user's `dir`, which may hold a space.
|
|
4155
|
-
`docker build --progress=plain${dockerfile !== void 0 ? ` -f
|
|
4344
|
+
`docker build --progress=plain${dockerfile !== void 0 ? ` -f ${shq(dockerfile)}` : ""} -t ${built} . 2>&1 | tail -n ${BUILD_LOG_LINES}`,
|
|
4156
4345
|
"cd /",
|
|
4346
|
+
...target.release !== void 0 ? [
|
|
4347
|
+
`[ -e ${BOX_ENV_PLAIN} ] || install -m 600 /dev/null ${BOX_ENV_PLAIN}`,
|
|
4348
|
+
...releaseStep(`docker run --rm --network host --env-file ${BOX_ENV_PLAIN} -e PORT=${port} ${built} sh -c ${shq(target.release)}`, root),
|
|
4349
|
+
`docker tag ${built} ${image}:latest`,
|
|
4350
|
+
`docker rmi ${built} >/dev/null`
|
|
4351
|
+
] : [],
|
|
4157
4352
|
// The single deploy's app or container would hold its port and RAM beside the services.
|
|
4158
4353
|
DROP_SINGLE,
|
|
4159
4354
|
`sudo -n tee /etc/systemd/system/${target.unit}.service >/dev/null <<'UNIT'`,
|
|
@@ -4165,7 +4360,8 @@ function dockerServiceRemote(port, target, dockerfile) {
|
|
|
4165
4360
|
"sudo -n systemctl daemon-reload",
|
|
4166
4361
|
"sudo -n systemctl enable --quiet docker",
|
|
4167
4362
|
`sudo -n systemctl enable --quiet ${target.unit}`,
|
|
4168
|
-
|
|
4363
|
+
// With databases to wait for, the restart itself fails when one never comes: the wait below shows why.
|
|
4364
|
+
`sudo -n systemctl restart ${target.unit}${after.length > 0 ? " || true" : ""}`,
|
|
4169
4365
|
"docker image prune -f >/dev/null",
|
|
4170
4366
|
// The box's network: the port is the box's own, and nothing but the app answers on it.
|
|
4171
4367
|
"i=0",
|
|
@@ -4363,21 +4559,21 @@ function detectTemplates(pkg, dir = NO_FACTS) {
|
|
|
4363
4559
|
var DEPLOY_AUTO_HIBERNATE_SEC = 3600;
|
|
4364
4560
|
var CONFIG_FILE = "kvbox.json";
|
|
4365
4561
|
var BOX_NAME_RE = /^[a-z0-9][a-z0-9-]{0,11}$/;
|
|
4366
|
-
var DeployConfigSchema =
|
|
4367
|
-
template:
|
|
4368
|
-
name:
|
|
4562
|
+
var DeployConfigSchema = z6.object({
|
|
4563
|
+
template: z6.enum(TEMPLATE_IDS),
|
|
4564
|
+
name: z6.string().regex(BOX_NAME_RE, "1-12 chars, lowercase letters, digits and '-', starting with a letter or digit"),
|
|
4369
4565
|
/** The org whose box it is; absent for the user's own. */
|
|
4370
|
-
org:
|
|
4566
|
+
org: z6.string().regex(ORG_SLUG_RE, "not an org slug").optional(),
|
|
4371
4567
|
/** The HTTPS name (`<subdomain>.kvbox.app`); absent when deploying to this machine's own host. */
|
|
4372
|
-
subdomain:
|
|
4568
|
+
subdomain: z6.string().check((ctx) => {
|
|
4373
4569
|
const problem = subdomainProblem(ctx.value);
|
|
4374
4570
|
if (problem) ctx.issues.push({ code: "custom", message: problem, input: ctx.value });
|
|
4375
4571
|
}).optional(),
|
|
4376
|
-
size:
|
|
4572
|
+
size: z6.enum(SIZE_NAMES).optional(),
|
|
4377
4573
|
/** The build output directory, relative to the app; the template's default when absent. */
|
|
4378
|
-
dist:
|
|
4574
|
+
dist: z6.string().optional(),
|
|
4379
4575
|
/** The port a Next.js app or Node server serves; the template's (3000) when absent. */
|
|
4380
|
-
port:
|
|
4576
|
+
port: z6.number().int().min(1).max(65535).optional()
|
|
4381
4577
|
});
|
|
4382
4578
|
var SERVICE_KEY_RE = /^[a-z][a-z0-9-]{0,19}$/;
|
|
4383
4579
|
var RESERVED_SERVICE_KEYS = ["app", "agent", "init", "volumes"];
|
|
@@ -4389,32 +4585,38 @@ var POSTGRES_PORT = 5432;
|
|
|
4389
4585
|
var POSTGRES_IMAGE = "postgres:17";
|
|
4390
4586
|
var POSTGRES_ENV = "DATABASE_URL";
|
|
4391
4587
|
var IMAGE_RE = /^[a-z0-9][a-z0-9._/:@-]*$/;
|
|
4392
|
-
var ServiceSchema =
|
|
4393
|
-
template:
|
|
4588
|
+
var ServiceSchema = z6.strictObject({
|
|
4589
|
+
template: z6.enum([...TEMPLATE_IDS, POSTGRES]),
|
|
4394
4590
|
/** The service's directory, relative to the one holding kvbox.json; every template but postgres has one. */
|
|
4395
|
-
dir:
|
|
4591
|
+
dir: z6.string().min(1).optional(),
|
|
4396
4592
|
/** A postgres service's image, `postgres:17` when absent (`pgvector/pgvector:pg17` for vectors). */
|
|
4397
|
-
image:
|
|
4593
|
+
image: z6.string().regex(IMAGE_RE, "an image name like postgres:17").optional(),
|
|
4398
4594
|
/** The volume (a key of `volumes`) a postgres service keeps its data on. */
|
|
4399
|
-
volume:
|
|
4595
|
+
volume: z6.string().optional(),
|
|
4400
4596
|
/** The box variable a postgres service's URL is written to, `DATABASE_URL` when absent. */
|
|
4401
|
-
env:
|
|
4597
|
+
env: z6.string().optional(),
|
|
4402
4598
|
/** A docker service's build context, relative to kvbox.json, when it is not `dir` (a monorepo built from its root); the Dockerfile stays in `dir`. */
|
|
4403
|
-
context:
|
|
4404
|
-
dist:
|
|
4599
|
+
context: z6.string().min(1).optional(),
|
|
4600
|
+
dist: z6.string().optional(),
|
|
4601
|
+
/**
|
|
4602
|
+
* A command run once per deploy in the service's new version (a docker service: in a container of
|
|
4603
|
+
* the new image; node, nextjs: in the new source, installed and built), with the box's variables,
|
|
4604
|
+
* before it replaces the running one. Migrations, mostly. A failing one stops that service's deploy.
|
|
4605
|
+
*/
|
|
4606
|
+
release: z6.string().optional(),
|
|
4405
4607
|
/** The box port of a service with its own server; the template's (3000) when absent. */
|
|
4406
|
-
port:
|
|
4608
|
+
port: z6.number().int().min(1).max(65535).optional(),
|
|
4407
4609
|
/** The URL paths that go to this service, each with everything under it; the prefix is kept. */
|
|
4408
|
-
paths:
|
|
4610
|
+
paths: z6.array(z6.string().regex(ROUTE_RE, "a path like / or /api, no trailing slash")).min(1).optional()
|
|
4409
4611
|
});
|
|
4410
|
-
var StackConfigSchema =
|
|
4612
|
+
var StackConfigSchema = z6.strictObject({
|
|
4411
4613
|
name: DeployConfigSchema.shape.name.optional(),
|
|
4412
4614
|
org: DeployConfigSchema.shape.org,
|
|
4413
4615
|
subdomain: DeployConfigSchema.shape.subdomain,
|
|
4414
4616
|
size: DeployConfigSchema.shape.size,
|
|
4415
4617
|
/** The stack's volumes, name to size ("10GB"): made for its box and attached to it on the first deploy that needs one. */
|
|
4416
|
-
volumes:
|
|
4417
|
-
services:
|
|
4618
|
+
volumes: z6.record(z6.string(), z6.string()).optional(),
|
|
4619
|
+
services: z6.record(z6.string(), ServiceSchema)
|
|
4418
4620
|
});
|
|
4419
4621
|
function volumeGib(text2) {
|
|
4420
4622
|
const mib = parseMib(text2);
|
|
@@ -4456,6 +4658,10 @@ function stackServices(stack, defaultPort, exposed = /* @__PURE__ */ new Map())
|
|
|
4456
4658
|
const isStatic = STATIC_TEMPLATES.includes(config.template);
|
|
4457
4659
|
const paths2 = config.paths ?? [];
|
|
4458
4660
|
if (isStatic && config.port !== void 0) throw new KvboxError(`${at}.port: a ${config.template} service has no port of its own; nginx serves its files`);
|
|
4661
|
+
if (config.release !== void 0) {
|
|
4662
|
+
if (isStatic) throw new KvboxError(`${at}.release: a ${config.template} service is files only, nothing runs in the box; put the command on the service that has the server`);
|
|
4663
|
+
if (config.release.trim() === "" || /[\r\n]/.test(config.release)) throw new KvboxError(`${at}.release: one command on one line, like "release": "npm run migrate"`);
|
|
4664
|
+
}
|
|
4459
4665
|
if (isStatic && paths2.length === 0) throw new KvboxError(`${at}.paths: say where the site is served, for example "paths": ["/"]`);
|
|
4460
4666
|
let port;
|
|
4461
4667
|
if (!isStatic) {
|
|
@@ -4484,7 +4690,7 @@ function stackServices(stack, defaultPort, exposed = /* @__PURE__ */ new Map())
|
|
|
4484
4690
|
}
|
|
4485
4691
|
function postgresService(key, config, stack, { volumeUsers, envUsers }, ports) {
|
|
4486
4692
|
const at = `${CONFIG_FILE}: services.${key}`;
|
|
4487
|
-
for (const field of ["dir", "context", "dist", "paths"]) {
|
|
4693
|
+
for (const field of ["dir", "context", "dist", "release", "paths"]) {
|
|
4488
4694
|
if (config[field] !== void 0) throw new KvboxError(`${at}.${field}: a postgres service has none${field === "paths" ? "; it is reached by the other services on 127.0.0.1, never from the internet" : ""}`);
|
|
4489
4695
|
}
|
|
4490
4696
|
const env = config.env ?? POSTGRES_ENV;
|
|
@@ -4616,6 +4822,14 @@ ${result.output.trim()}` : ""}`, SSH_FAILED3);
|
|
|
4616
4822
|
`box ${ref} runs an image too old for the ${template.label} template (base v0.8.0 or newer). Update the box (kvbox vm update ${ref}) once its host has the new image, or deploy under another name (--name) to get a fresh box`
|
|
4617
4823
|
);
|
|
4618
4824
|
}
|
|
4825
|
+
if (result.status === RELEASE_FAILED) {
|
|
4826
|
+
const log = result.output.split(LOG_MARKER)[1]?.trim();
|
|
4827
|
+
throw new KvboxError(
|
|
4828
|
+
`${app}: its release command (${service?.release ?? "release"}) failed, so the new version was not started. Nothing in box ${ref} was replaced: a version of ${service?.key ?? "it"} already running keeps running${log ? `
|
|
4829
|
+
last lines:
|
|
4830
|
+
${log}` : ""}`
|
|
4831
|
+
);
|
|
4832
|
+
}
|
|
4619
4833
|
if (result.status === APP_NOT_LISTENING) {
|
|
4620
4834
|
const log = result.output.split(LOG_MARKER)[1]?.trim();
|
|
4621
4835
|
const where = built.containerPort !== void 0 && built.containerPort !== port ? `the container's port ${built.containerPort} (box port ${port})` : `port ${port}`;
|
|
@@ -4891,22 +5105,24 @@ async function deployStack(d) {
|
|
|
4891
5105
|
};
|
|
4892
5106
|
const databases = picked.filter((s) => s.postgres);
|
|
4893
5107
|
if (databases.length > 0) await deployDatabases({ client, ref, owner: machine.owner, name: machine.name, ep, volumes: config.volumes ?? {}, databases, renewOnce });
|
|
5108
|
+
const after = services.filter((s) => s.postgres).map((s) => ({ unit: `kvbox-${s.key}`, port: s.port }));
|
|
4894
5109
|
for (const { service, template, built, size } of all) {
|
|
4895
|
-
const
|
|
5110
|
+
const release = service.config.release;
|
|
5111
|
+
const target = { root: `/srv/app/${service.key}`, unit: `kvbox-${service.key}`, ...release !== void 0 ? { release } : {}, ...after.length > 0 && !service.static ? { after } : {} };
|
|
4896
5112
|
const port = service.port ?? FRONT_PORT;
|
|
4897
5113
|
const shipIt = () => shipping.ship(ep, built.outDir, template.remote(built, port, target), built.exclude);
|
|
4898
5114
|
const there = service.static ? "" : template.id === "docker" ? " and building the image there, a minute or two" : built.boxBuild ? " and installing and building there, a minute or two" : " and installing there, a minute or two";
|
|
4899
|
-
const doing = `${service.key}: shipping ${built.what} (${size.files} files, ${gb(size.bytes)}) to ${ref}${there}\u2026`;
|
|
5115
|
+
const doing = `${service.key}: shipping ${built.what} (${size.files} files, ${gb(size.bytes)}) to ${ref}${there}${release !== void 0 ? ", then its release command" : ""}\u2026`;
|
|
4900
5116
|
let result = await spin(doing, shipIt);
|
|
4901
5117
|
if (result.status === SSH_FAILED3 && await renewOnce()) result = await spin(doing, shipIt);
|
|
4902
|
-
refuseFailedShip(result, { ref, template, port, built, ...service.static ? {} : { service: { key: service.key, unit: target.unit } } });
|
|
5118
|
+
refuseFailedShip(result, { ref, template, port, built, ...service.static ? {} : { service: { key: service.key, unit: target.unit, ...release !== void 0 ? { release } : {} } } });
|
|
4903
5119
|
}
|
|
4904
5120
|
const front = await spin(`pointing the box's front door at ${services.length === 1 ? "the service" : `the ${services.length} services`}\u2026`, shipping.run(ep, frontRemote(stackSite(services))));
|
|
4905
5121
|
if (front.status === IMAGE_TOO_OLD) throw new KvboxError(`box ${ref} has no nginx: its image is too old (base v0.8.0 or newer). Update the box (kvbox vm update ${ref}), or deploy under another name (--name) to get a fresh box`);
|
|
4906
5122
|
if (front.status === SITE_REFUSED) {
|
|
4907
5123
|
const first = front.output.includes(NO_PREVIOUS_SITE);
|
|
4908
|
-
const
|
|
4909
|
-
throw new KvboxError(`nginx in box ${ref} refused the routes made from ${CONFIG_FILE}; ${
|
|
5124
|
+
const after2 = first ? "this was the box's first stack, so there are no previous routes to keep, and any single app it ran was stopped for the services: its address answers nothing until a deploy whose routes nginx takes" : "the previous ones keep serving";
|
|
5125
|
+
throw new KvboxError(`nginx in box ${ref} refused the routes made from ${CONFIG_FILE}; ${after2}. The services themselves were deployed.
|
|
4910
5126
|
${front.output.replace(NO_PREVIOUS_SITE, "").trim()}`);
|
|
4911
5127
|
}
|
|
4912
5128
|
if (front.status !== 0) throw new KvboxError(`setting up nginx in box ${ref} failed (exit ${front.status}):
|
|
@@ -4932,6 +5148,7 @@ ${front.output.trim()}`);
|
|
|
4932
5148
|
bytes: size.bytes,
|
|
4933
5149
|
...built.dist !== void 0 ? { dist: built.dist } : {},
|
|
4934
5150
|
...service.port !== void 0 ? { port: service.port } : {},
|
|
5151
|
+
...service.config.release !== void 0 ? { release: service.config.release } : {},
|
|
4935
5152
|
paths: service.paths
|
|
4936
5153
|
}))
|
|
4937
5154
|
],
|
|
@@ -5335,7 +5552,7 @@ async function settle2(dir, pkg, facts, saved, opts, orgFlag, cloud) {
|
|
|
5335
5552
|
}
|
|
5336
5553
|
|
|
5337
5554
|
// src/commands/volume/index.ts
|
|
5338
|
-
import { Command as
|
|
5555
|
+
import { Command as Command61 } from "commander";
|
|
5339
5556
|
|
|
5340
5557
|
// src/commands/volume/attach.ts
|
|
5341
5558
|
import { Command as Command55 } from "commander";
|
|
@@ -5363,11 +5580,11 @@ var attachCommand = new Command55("attach").description("attach a volume to a bo
|
|
|
5363
5580
|
|
|
5364
5581
|
// src/commands/volume/create.ts
|
|
5365
5582
|
import { Command as Command56 } from "commander";
|
|
5366
|
-
function parseGib2(text2) {
|
|
5583
|
+
function parseGib2(text2, label = "--size") {
|
|
5367
5584
|
const mib = parseMib(text2);
|
|
5368
|
-
if (mib === void 0 || mib % 1024 !== 0) throw new KvboxError(
|
|
5585
|
+
if (mib === void 0 || mib % 1024 !== 0) throw new KvboxError(`${label} ${text2}: write whole GB, like 50 or 50GB`);
|
|
5369
5586
|
const gib = mib / 1024;
|
|
5370
|
-
if (gib > MAX_VOLUME_GIB) throw new KvboxError(
|
|
5587
|
+
if (gib > MAX_VOLUME_GIB) throw new KvboxError(`${label} ${text2}: a volume is at most ${MAX_VOLUME_GIB} GB`);
|
|
5371
5588
|
return gib;
|
|
5372
5589
|
}
|
|
5373
5590
|
function sizeProblem(answer) {
|
|
@@ -5436,9 +5653,23 @@ var detachCommand = new Command57("detach").description("detach a volume from a
|
|
|
5436
5653
|
}
|
|
5437
5654
|
});
|
|
5438
5655
|
|
|
5439
|
-
// src/commands/volume/
|
|
5656
|
+
// src/commands/volume/grow.ts
|
|
5440
5657
|
import { Command as Command58 } from "commander";
|
|
5441
|
-
var
|
|
5658
|
+
var growCommand = new Command58("grow").alias("resize").description("make a volume larger (a running box holding it is restarted once)").argument("<volume>", "volume name").argument("<GB>", "the new size in GB, larger than what it has").option("--json", "print the volume as JSON").action(async (name, size, opts, cmd) => {
|
|
5659
|
+
const sizeGib = parseGib2(size, "size");
|
|
5660
|
+
const client = await clientFor(cmd.optsWithGlobals());
|
|
5661
|
+
try {
|
|
5662
|
+
const vol = await spin(`growing volume ${name} to ${sizeGib} GB\u2026`, client.resizeVolume(name, sizeGib));
|
|
5663
|
+
if (opts.json) json(vol);
|
|
5664
|
+
else info(`${color.green("\u2713")} volume ${name} is ${vol.sizeGib} GB${vol.attachedTo ? color.dim(` (box ${vol.attachedTo} was restarted if it was running)`) : ""}`);
|
|
5665
|
+
} finally {
|
|
5666
|
+
await client.close();
|
|
5667
|
+
}
|
|
5668
|
+
});
|
|
5669
|
+
|
|
5670
|
+
// src/commands/volume/ls.ts
|
|
5671
|
+
import { Command as Command59 } from "commander";
|
|
5672
|
+
var lsCommand8 = new Command59("ls").alias("list").description("list volumes: size, what they occupy, which box has them").option("-a, --all", "every volume on the platform (platform admins only)").option("--json", "print as JSON").action(async (opts, cmd) => {
|
|
5442
5673
|
const client = await clientFor(cmd.optsWithGlobals());
|
|
5443
5674
|
try {
|
|
5444
5675
|
const cloud = client.target.kind === "cloud";
|
|
@@ -5469,8 +5700,8 @@ var lsCommand8 = new Command58("ls").alias("list").description("list volumes: si
|
|
|
5469
5700
|
});
|
|
5470
5701
|
|
|
5471
5702
|
// src/commands/volume/rm.ts
|
|
5472
|
-
import { Command as
|
|
5473
|
-
var rmCommand7 = new
|
|
5703
|
+
import { Command as Command60 } from "commander";
|
|
5704
|
+
var rmCommand7 = new Command60("rm").alias("remove").description("delete a volume and everything on it (detach it first)").argument("<name>", "volume name").option("-y, --yes", "do not ask for confirmation").action(async (name, opts, cmd) => {
|
|
5474
5705
|
if (!await confirm(`Delete volume ${name} and everything on it?`, opts.yes ?? false)) {
|
|
5475
5706
|
info("cancelled");
|
|
5476
5707
|
process.exitCode = 1;
|
|
@@ -5486,14 +5717,309 @@ var rmCommand7 = new Command59("rm").alias("remove").description("delete a volum
|
|
|
5486
5717
|
});
|
|
5487
5718
|
|
|
5488
5719
|
// src/commands/volume/index.ts
|
|
5489
|
-
var volumeCommand = new
|
|
5720
|
+
var volumeCommand = new Command61("volume").aliases(["volumes", "vol"]).description("disks of their own: create one, attach it to a box at /mnt/<name>, move it to another box later").addCommand(createCommand3).addCommand(lsCommand8).addCommand(attachCommand).addCommand(detachCommand).addCommand(growCommand).addCommand(rmCommand7);
|
|
5490
5721
|
for (const cmd of volumeCommand.commands) cmd.option("--org <slug>", "act in this org (- for your personal volumes)");
|
|
5491
5722
|
|
|
5492
|
-
// src/commands/
|
|
5723
|
+
// src/commands/managed/kind.ts
|
|
5724
|
+
import { Command as Command74 } from "commander";
|
|
5725
|
+
|
|
5726
|
+
// src/commands/managed/create.ts
|
|
5727
|
+
import { Command as Command63 } from "commander";
|
|
5728
|
+
import { setTimeout as sleep3 } from "timers/promises";
|
|
5729
|
+
|
|
5730
|
+
// src/commands/managed/shared.ts
|
|
5731
|
+
import "commander";
|
|
5732
|
+
var POSTGRES2 = { kind: "postgres", command: "pg", aliases: ["postgres"], title: "PostgreSQL" };
|
|
5733
|
+
var KINDS = [POSTGRES2];
|
|
5734
|
+
async function managedClient(cmd) {
|
|
5735
|
+
const client = await clientFor(cmd.optsWithGlobals());
|
|
5736
|
+
if (client.target.kind !== "cloud") {
|
|
5737
|
+
await client.close();
|
|
5738
|
+
throw new KvboxError("managed databases run on the kvbox cloud: kvbox login first (this host's own socket has none)");
|
|
5739
|
+
}
|
|
5740
|
+
return client;
|
|
5741
|
+
}
|
|
5742
|
+
var shownName = (m, user) => m.owner === user ? m.name : `${m.owner}/${m.name}`;
|
|
5743
|
+
var storageText = (m) => m.health?.storageUsedBytes !== void 0 ? `${gb(m.health.storageUsedBytes)} of ${m.storageGib} GB used` : `${m.storageGib} GB`;
|
|
5744
|
+
var nearlyFull = (m) => m.health?.storageUsedBytes !== void 0 && m.health.storageTotalBytes !== void 0 && m.health.storageTotalBytes > 0 && m.health.storageUsedBytes / m.health.storageTotalBytes >= 0.9;
|
|
5745
|
+
function stateText(m) {
|
|
5746
|
+
if (m.status === "stopped") return `${status(m.status)} ${color.dim("(only its storage is charged)")}`;
|
|
5747
|
+
if (m.status !== "running" || m.health === void 0) return status(m.status);
|
|
5748
|
+
if (m.health === null) return `${status(m.status)}${color.dim(", not answering yet")}`;
|
|
5749
|
+
if (!m.health.ready) return `${status(m.status)}${color.dim(", still setting itself up")}`;
|
|
5750
|
+
return `${status(m.status)}, ${m.health.accepting ? "accepting connections" : color.yellow("not accepting connections")}`;
|
|
5751
|
+
}
|
|
5752
|
+
function infoRows(spec, m, user) {
|
|
5753
|
+
const name = shownName(m, user);
|
|
5754
|
+
const rows2 = [
|
|
5755
|
+
["name", color.bold(name)],
|
|
5756
|
+
["status", stateText(m)],
|
|
5757
|
+
["version", `${spec.title} ${m.health?.version ?? m.version}`]
|
|
5758
|
+
];
|
|
5759
|
+
if (user !== void 0 && m.owner !== user) rows2.push(["owner", m.owner]);
|
|
5760
|
+
if (m.health) rows2.push(["connections", String(m.health.connections)]);
|
|
5761
|
+
rows2.push(["storage", `${storageText(m)}${nearlyFull(m) ? color.yellow(` almost full: kvbox ${spec.command} grow ${name} <GB>`) : ""}`]);
|
|
5762
|
+
rows2.push(["database", m.database], ["user", m.user], ["password", color.dim(`kvbox ${spec.command} password ${name}`)]);
|
|
5763
|
+
rows2.push(["private", `${m.urls.private} ${color.dim(m.links.length > 0 ? `(from ${m.links.join(", ")})` : `(no box is linked: kvbox ${spec.command} link ${name} <box>)`)}`]);
|
|
5764
|
+
rows2.push(["public", m.urls.public ?? color.dim(`off (kvbox ${spec.command} public ${name} on)`)]);
|
|
5765
|
+
rows2.push(["cost", `${money(m.price.perMonth, m.price.currency)} a month while it runs, ${money(m.price.storagePerMonth, m.price.currency)} stopped ${color.dim("(estimated)")}`]);
|
|
5766
|
+
rows2.push(["created", `${age(m.createdAt)}${m.createdBy ? ` by ${m.createdBy}` : ""}`]);
|
|
5767
|
+
return rows2;
|
|
5768
|
+
}
|
|
5769
|
+
function printRows(rows2) {
|
|
5770
|
+
const width2 = Math.max(...rows2.map(([label]) => label.length));
|
|
5771
|
+
for (const [label, value] of rows2) process.stdout.write(`${color.dim(label.padEnd(width2))} ${value}
|
|
5772
|
+
`);
|
|
5773
|
+
}
|
|
5774
|
+
function secretRows(secret) {
|
|
5775
|
+
return [
|
|
5776
|
+
["user", secret.user],
|
|
5777
|
+
["password", secret.password],
|
|
5778
|
+
["private", secret.urls.private],
|
|
5779
|
+
...secret.urls.public ? [["public", secret.urls.public]] : []
|
|
5780
|
+
];
|
|
5781
|
+
}
|
|
5782
|
+
function parseStorage(text2, flag) {
|
|
5783
|
+
const m = /^\s*(\d+)\s*(g|gb|gib)?\s*$/i.exec(text2);
|
|
5784
|
+
if (!m || Number(m[1]) < 1) throw new KvboxError(`${flag} ${text2}: write whole GB, like 50 or 50GB`);
|
|
5785
|
+
return Number(m[1]);
|
|
5786
|
+
}
|
|
5787
|
+
|
|
5788
|
+
// src/commands/managed/create.ts
|
|
5789
|
+
var READY_WAIT_MS = 9e4;
|
|
5790
|
+
var READY_STEP_MS = 2e3;
|
|
5791
|
+
var createCommand4 = (spec) => new Command63("create").alias("new").description(`create a ${spec.title} database: a name is all it needs`).argument("<name>", "its name (1-12 chars, lowercase letters, digits and '-'); a linked box reaches it at <name>.<account>.kvbox").option("--database <name>", "the database created in it (default: the name)").option("--user <name>", "the user that owns it (default: the name)").option("--password <password>", "that user's password (default: generated)").option("--storage <gb>", "storage for the data in GB (default: 10; it can grow later)").option("--near <box>", "run it on the host of this box, so the box can be linked to it").option("--json", "print it as JSON, password included").action(async (name, opts, cmd) => {
|
|
5792
|
+
const req = {
|
|
5793
|
+
name,
|
|
5794
|
+
...opts.database !== void 0 ? { database: opts.database } : {},
|
|
5795
|
+
...opts.user !== void 0 ? { user: opts.user } : {},
|
|
5796
|
+
...opts.password !== void 0 ? { password: opts.password } : {},
|
|
5797
|
+
...opts.storage !== void 0 ? { storageGib: parseStorage(opts.storage, "--storage") } : {},
|
|
5798
|
+
...opts.near !== void 0 ? { near: opts.near } : {}
|
|
5799
|
+
};
|
|
5800
|
+
const client = await managedClient(cmd);
|
|
5801
|
+
try {
|
|
5802
|
+
const made = await spin(`creating ${spec.title} database ${name}\u2026`, client.createManaged(spec.kind, req));
|
|
5803
|
+
const shown = shownName(made, me(client));
|
|
5804
|
+
const ready = await spin(`starting ${spec.title}\u2026`, async () => {
|
|
5805
|
+
for (const until = Date.now() + READY_WAIT_MS; Date.now() < until; await sleep3(READY_STEP_MS)) {
|
|
5806
|
+
const now = await client.getManaged(spec.kind, shown).catch(() => void 0);
|
|
5807
|
+
if (now?.health?.ready && now.health.accepting) return true;
|
|
5808
|
+
}
|
|
5809
|
+
return false;
|
|
5810
|
+
});
|
|
5811
|
+
if (opts.json) {
|
|
5812
|
+
json({ ...made, ready });
|
|
5813
|
+
return;
|
|
5814
|
+
}
|
|
5815
|
+
info(`${color.green("\u2713")} database ${color.bold(shown)} ${ready ? "is ready" : "is created and still starting"} ${color.dim(`(${spec.title} ${made.version}, ${made.storageGib} GB of storage)`)}`);
|
|
5816
|
+
const rows2 = [["database", made.database], ...secretRows(made.secret)];
|
|
5817
|
+
printRows(rows2.map(([label, value]) => [` ${label}`, value]));
|
|
5818
|
+
info("");
|
|
5819
|
+
info("Nobody can reach it yet. To let something in:");
|
|
5820
|
+
info(` a box on the same host kvbox ${spec.command} link ${shown} <box> ${color.dim(`(a new one: kvbox new <box> --near ${shown})`)}`);
|
|
5821
|
+
info(` the internet kvbox ${spec.command} public ${shown} on`);
|
|
5822
|
+
info(color.dim(`It costs about ${money(made.price.perMonth, made.price.currency)} a month while it runs, ${money(made.price.storagePerMonth, made.price.currency)} stopped. The password again: kvbox ${spec.command} password ${shown}`));
|
|
5823
|
+
if (!ready) info(color.dim(`It was not answering after ${READY_WAIT_MS / 1e3} s; see how it is with: kvbox ${spec.command} info ${shown}`));
|
|
5824
|
+
} finally {
|
|
5825
|
+
await client.close();
|
|
5826
|
+
}
|
|
5827
|
+
});
|
|
5828
|
+
|
|
5829
|
+
// src/commands/managed/grow.ts
|
|
5493
5830
|
import { Command as Command64 } from "commander";
|
|
5831
|
+
var growCommand2 = (spec) => new Command64("grow").description("give a database more storage (it restarts once)").argument("<db>", "its name").argument("<GB>", "the new size in GB, larger than what it has").option("--json", "print as JSON").action(async (name, size, opts, cmd) => {
|
|
5832
|
+
const storageGib = parseStorage(size, "size");
|
|
5833
|
+
const client = await managedClient(cmd);
|
|
5834
|
+
try {
|
|
5835
|
+
const m = await spin(`growing ${name} to ${storageGib} GB (it restarts once)\u2026`, client.growManaged(spec.kind, name, storageGib));
|
|
5836
|
+
if (opts.json) json(m);
|
|
5837
|
+
else info(`${color.green("\u2713")} database ${name} has ${m.storageGib} GB of storage ${color.dim(`(about ${money(m.price.perMonth, m.price.currency)} a month while it runs)`)}`);
|
|
5838
|
+
} finally {
|
|
5839
|
+
await client.close();
|
|
5840
|
+
}
|
|
5841
|
+
});
|
|
5842
|
+
|
|
5843
|
+
// src/commands/managed/info.ts
|
|
5844
|
+
import { Command as Command65 } from "commander";
|
|
5845
|
+
var infoCommand = (spec) => new Command65("info").alias("get").description("show one database: status, storage used, how to connect").argument("<db>", "its name").option("--json", "print as JSON").action(async (name, opts, cmd) => {
|
|
5846
|
+
const client = await managedClient(cmd);
|
|
5847
|
+
try {
|
|
5848
|
+
const m = await client.getManaged(spec.kind, name);
|
|
5849
|
+
if (opts.json) json(m);
|
|
5850
|
+
else printRows(infoRows(spec, m, me(client)));
|
|
5851
|
+
} finally {
|
|
5852
|
+
await client.close();
|
|
5853
|
+
}
|
|
5854
|
+
});
|
|
5855
|
+
|
|
5856
|
+
// src/commands/managed/link.ts
|
|
5857
|
+
import { Command as Command66 } from "commander";
|
|
5858
|
+
var linkCommand2 = (spec) => new Command66("link").description("let a box reach a database privately (same host)").argument("<db>", "the database").argument("<box>", "the box that connects to it").option("--json", "print as JSON").action(async (name, box, opts, cmd) => {
|
|
5859
|
+
const client = await managedClient(cmd);
|
|
5860
|
+
try {
|
|
5861
|
+
const m = await spin(`linking ${box} to ${name}\u2026`, client.linkManaged(spec.kind, name, box));
|
|
5862
|
+
if (opts.json) {
|
|
5863
|
+
json(m);
|
|
5864
|
+
return;
|
|
5865
|
+
}
|
|
5866
|
+
info(`${color.green("\u2713")} box ${box} can reach database ${name}`);
|
|
5867
|
+
info(` inside ${box}: ${m.urls.private} ${color.dim(`(the password: kvbox ${spec.command} password ${name})`)}`);
|
|
5868
|
+
} finally {
|
|
5869
|
+
await client.close();
|
|
5870
|
+
}
|
|
5871
|
+
});
|
|
5872
|
+
|
|
5873
|
+
// src/commands/managed/ls.ts
|
|
5874
|
+
import { Command as Command67 } from "commander";
|
|
5875
|
+
function listRows(items, user, kindColumn) {
|
|
5876
|
+
return items.map((m) => ({
|
|
5877
|
+
owner: m.owner === user ? "private" : m.owner,
|
|
5878
|
+
name: boxName(m.name),
|
|
5879
|
+
...kindColumn ? { kind: KINDS.find((k) => k.kind === m.kind)?.title ?? m.kind } : {},
|
|
5880
|
+
status: status(m.status),
|
|
5881
|
+
version: m.version,
|
|
5882
|
+
storage: `${m.storageGib}GB`,
|
|
5883
|
+
public: m.public ? `${m.public.host}:${m.public.port}` : "-",
|
|
5884
|
+
linked: m.links.length > 0 ? m.links.join(", ") : "-",
|
|
5885
|
+
created: age(m.createdAt)
|
|
5886
|
+
}));
|
|
5887
|
+
}
|
|
5888
|
+
var lsCommand9 = (spec) => new Command67("ls").alias("list").description(spec ? `list your ${spec.title} databases` : "list every managed database, whatever its kind").option("-a, --all", "every one on the platform (platform admins only)").option("--json", "print as JSON").action(async (opts, cmd) => {
|
|
5889
|
+
const client = await managedClient(cmd);
|
|
5890
|
+
try {
|
|
5891
|
+
const items = await client.listManaged(spec?.kind, { all: opts.all });
|
|
5892
|
+
if (opts.json) {
|
|
5893
|
+
json(items);
|
|
5894
|
+
return;
|
|
5895
|
+
}
|
|
5896
|
+
if (items.length === 0) {
|
|
5897
|
+
info(`no ${spec ? `${spec.title} ` : "managed "}databases yet; make one with: kvbox ${spec?.command ?? KINDS[0].command} create <name>`);
|
|
5898
|
+
return;
|
|
5899
|
+
}
|
|
5900
|
+
const user = client.target.kind === "cloud" ? client.target.user : void 0;
|
|
5901
|
+
table(listRows(items, user, spec === void 0));
|
|
5902
|
+
} finally {
|
|
5903
|
+
await client.close();
|
|
5904
|
+
}
|
|
5905
|
+
});
|
|
5906
|
+
|
|
5907
|
+
// src/commands/managed/password.ts
|
|
5908
|
+
import { Command as Command68 } from "commander";
|
|
5909
|
+
var passwordCommand = (spec) => new Command68("password").description("show a database's password, or set a new one with --reset").argument("<db>", "its name").option("--reset", "make a new password; the old one stops working").option("--json", "print as JSON").action(async (name, opts, cmd) => {
|
|
5910
|
+
const client = await managedClient(cmd);
|
|
5911
|
+
try {
|
|
5912
|
+
const secret = opts.reset ? await spin(`setting a new password on ${name}\u2026`, client.resetManagedPassword(spec.kind, name)) : await client.managedPassword(spec.kind, name);
|
|
5913
|
+
if (opts.json) {
|
|
5914
|
+
json(secret);
|
|
5915
|
+
return;
|
|
5916
|
+
}
|
|
5917
|
+
if (opts.reset) info(`${color.green("\u2713")} database ${name} has a new password; the old one no longer works`);
|
|
5918
|
+
printRows(secretRows(secret));
|
|
5919
|
+
} finally {
|
|
5920
|
+
await client.close();
|
|
5921
|
+
}
|
|
5922
|
+
});
|
|
5923
|
+
|
|
5924
|
+
// src/commands/managed/public.ts
|
|
5925
|
+
import { Command as Command69 } from "commander";
|
|
5926
|
+
var publicCommand = (spec) => new Command69("public").description("open a database to the internet, or close it again").argument("<db>", "its name").argument("<state>", "on: reachable from anywhere with the password; off: only linked boxes").option("--json", "print as JSON").action(async (name, state, opts, cmd) => {
|
|
5927
|
+
if (state !== "on" && state !== "off") throw new KvboxError(`write on or off: kvbox ${spec.command} public ${name} on`);
|
|
5928
|
+
const client = await managedClient(cmd);
|
|
5929
|
+
try {
|
|
5930
|
+
const m = await spin(`${state === "on" ? "opening" : "closing"} ${name}\u2026`, client.setManagedPublic(spec.kind, name, state === "on"));
|
|
5931
|
+
if (opts.json) {
|
|
5932
|
+
json(m);
|
|
5933
|
+
return;
|
|
5934
|
+
}
|
|
5935
|
+
if (m.public) {
|
|
5936
|
+
info(`${color.green("\u2713")} database ${name} is reachable from the internet at ${color.bold(`${m.public.host}:${m.public.port}`)}`);
|
|
5937
|
+
info(` ${m.urls.public} ${color.dim(`(the password: kvbox ${spec.command} password ${name})`)}`);
|
|
5938
|
+
} else {
|
|
5939
|
+
info(`${color.green("\u2713")} database ${name} is closed to the internet; turning it on again gives the same address`);
|
|
5940
|
+
}
|
|
5941
|
+
} finally {
|
|
5942
|
+
await client.close();
|
|
5943
|
+
}
|
|
5944
|
+
});
|
|
5945
|
+
|
|
5946
|
+
// src/commands/managed/rm.ts
|
|
5947
|
+
import { Command as Command70 } from "commander";
|
|
5948
|
+
var rmCommand8 = (spec) => new Command70("rm").alias("remove").description("delete a database and all its data").argument("<db>", "its name").option("-y, --yes", "do not ask for confirmation").action(async (name, opts, cmd) => {
|
|
5949
|
+
const client = await managedClient(cmd);
|
|
5950
|
+
try {
|
|
5951
|
+
if (!await confirm(`Delete database ${name} and all its data? There is no backup.`, opts.yes ?? false)) {
|
|
5952
|
+
info("cancelled");
|
|
5953
|
+
process.exitCode = 1;
|
|
5954
|
+
return;
|
|
5955
|
+
}
|
|
5956
|
+
await spin(`deleting database ${name}\u2026`, client.deleteManaged(spec.kind, name));
|
|
5957
|
+
info(`${color.green("\u2713")} database ${name} deleted`);
|
|
5958
|
+
} finally {
|
|
5959
|
+
await client.close();
|
|
5960
|
+
}
|
|
5961
|
+
});
|
|
5962
|
+
|
|
5963
|
+
// src/commands/managed/start.ts
|
|
5964
|
+
import { Command as Command71 } from "commander";
|
|
5965
|
+
var startCommand2 = (spec) => new Command71("start").description("run a stopped database again").argument("<db>", "its name").option("--json", "print as JSON").action(async (name, opts, cmd) => {
|
|
5966
|
+
const client = await managedClient(cmd);
|
|
5967
|
+
try {
|
|
5968
|
+
const m = await spin(`starting ${name}\u2026`, client.startManaged(spec.kind, name));
|
|
5969
|
+
if (opts.json) json(m);
|
|
5970
|
+
else info(`${color.green("\u2713")} database ${name} is ${m.status}`);
|
|
5971
|
+
} finally {
|
|
5972
|
+
await client.close();
|
|
5973
|
+
}
|
|
5974
|
+
});
|
|
5975
|
+
|
|
5976
|
+
// src/commands/managed/stop.ts
|
|
5977
|
+
import { Command as Command72 } from "commander";
|
|
5978
|
+
var stopCommand2 = (spec) => new Command72("stop").description("shut a database down, keeping its data (only its storage is charged)").argument("<db>", "its name").option("--json", "print as JSON").action(async (name, opts, cmd) => {
|
|
5979
|
+
const client = await managedClient(cmd);
|
|
5980
|
+
try {
|
|
5981
|
+
const m = await spin(`stopping ${name}\u2026`, client.stopManaged(spec.kind, name));
|
|
5982
|
+
if (opts.json) json(m);
|
|
5983
|
+
else info(`${color.green("\u2713")} database ${name} is stopped; its data is kept ${color.dim(`(${money(m.price.storagePerMonth, m.price.currency)} a month for the storage). Start it with: kvbox ${spec.command} start ${name}`)}`);
|
|
5984
|
+
} finally {
|
|
5985
|
+
await client.close();
|
|
5986
|
+
}
|
|
5987
|
+
});
|
|
5988
|
+
|
|
5989
|
+
// src/commands/managed/unlink.ts
|
|
5990
|
+
import { Command as Command73 } from "commander";
|
|
5991
|
+
var unlinkCommand2 = (spec) => new Command73("unlink").description("stop a box from reaching a database").argument("<db>", "the database").argument("<box>", "the box that was linked to it").option("--json", "print as JSON").action(async (name, box, opts, cmd) => {
|
|
5992
|
+
const client = await managedClient(cmd);
|
|
5993
|
+
try {
|
|
5994
|
+
const m = await spin(`unlinking ${box} from ${name}\u2026`, client.unlinkManaged(spec.kind, name, box));
|
|
5995
|
+
if (opts.json) json(m);
|
|
5996
|
+
else info(`${color.green("\u2713")} box ${box} no longer reaches database ${name}`);
|
|
5997
|
+
} finally {
|
|
5998
|
+
await client.close();
|
|
5999
|
+
}
|
|
6000
|
+
});
|
|
6001
|
+
|
|
6002
|
+
// src/commands/managed/kind.ts
|
|
6003
|
+
function kindCommand(spec, description) {
|
|
6004
|
+
const command = new Command74(spec.command).aliases(spec.aliases).description(description);
|
|
6005
|
+
for (const make of [createCommand4, lsCommand9, infoCommand, passwordCommand, publicCommand, linkCommand2, unlinkCommand2, growCommand2, stopCommand2, startCommand2, rmCommand8]) {
|
|
6006
|
+
command.addCommand(make(spec).option("--org <slug>", "act in this org (- for your personal databases)"));
|
|
6007
|
+
}
|
|
6008
|
+
return command;
|
|
6009
|
+
}
|
|
6010
|
+
|
|
6011
|
+
// src/commands/pg/index.ts
|
|
6012
|
+
var pgCommand = kindCommand(POSTGRES2, "managed PostgreSQL: a database kvbox runs for you, private until you link a box or make it public");
|
|
6013
|
+
|
|
6014
|
+
// src/commands/managed/index.ts
|
|
6015
|
+
import { Command as Command75 } from "commander";
|
|
6016
|
+
var managedCommand = new Command75("managed").description("databases kvbox runs for you, every kind together (Postgres: kvbox pg)").addCommand(lsCommand9().option("--org <slug>", "act in this org (- for your personal databases)"));
|
|
6017
|
+
|
|
6018
|
+
// src/commands/env/index.ts
|
|
6019
|
+
import { Command as Command79 } from "commander";
|
|
5494
6020
|
|
|
5495
6021
|
// src/commands/env/ls.ts
|
|
5496
|
-
import { Command as
|
|
6022
|
+
import { Command as Command76 } from "commander";
|
|
5497
6023
|
|
|
5498
6024
|
// src/commands/env/shared.ts
|
|
5499
6025
|
function requireCloud4(client) {
|
|
@@ -5520,7 +6046,7 @@ function firstBootHint(box, name) {
|
|
|
5520
6046
|
}
|
|
5521
6047
|
|
|
5522
6048
|
// src/commands/env/ls.ts
|
|
5523
|
-
var
|
|
6049
|
+
var lsCommand10 = new Command76("ls").alias("list").description("list the variables (a secret shows as (sealed))").option("--box <box>", "what this box gets: its own variables and the ones it inherits").option("--json", "print as JSON").action(async (opts, cmd) => {
|
|
5524
6050
|
const client = await clientFor(cmd.optsWithGlobals(), { autoStart: false });
|
|
5525
6051
|
try {
|
|
5526
6052
|
if (opts.box !== void 0) {
|
|
@@ -5559,8 +6085,8 @@ var lsCommand9 = new Command61("ls").alias("list").description("list the variabl
|
|
|
5559
6085
|
});
|
|
5560
6086
|
|
|
5561
6087
|
// src/commands/env/rm.ts
|
|
5562
|
-
import { Command as
|
|
5563
|
-
var
|
|
6088
|
+
import { Command as Command77 } from "commander";
|
|
6089
|
+
var rmCommand9 = new Command77("rm").alias("remove").description("remove variables; boxes lose them when they next start").argument("<names...>", "variable names").option("-y, --yes", "do not ask for confirmation").option("--box <box>", "remove this box's own variables (yours, or the org's, of the same name come back)").option("--json", "print as JSON").action(async (names2, opts, cmd) => {
|
|
5564
6090
|
const client = await clientFor(cmd.optsWithGlobals(), { autoStart: false });
|
|
5565
6091
|
try {
|
|
5566
6092
|
const target = opts.box === void 0 ? void 0 : await findBox2(client, opts.box);
|
|
@@ -5597,8 +6123,8 @@ var rmCommand8 = new Command62("rm").alias("remove").description("remove variabl
|
|
|
5597
6123
|
});
|
|
5598
6124
|
|
|
5599
6125
|
// src/commands/env/set.ts
|
|
5600
|
-
import { Command as
|
|
5601
|
-
var setCommand2 = new
|
|
6126
|
+
import { Command as Command78 } from "commander";
|
|
6127
|
+
var setCommand2 = new Command78("set").description("add or change a variable; boxes get it when they next start").argument("[name]", "variable name, e.g. DATABASE_URL (asked for when left out)").argument("[value]", "its value (asked for when left out; hidden with --secret)").option("--secret", "write-only: the value is never shown again, by any command").option("--box <box>", "this box's own variable, over yours (or the org's) of the same name").option("--json", "print as JSON").action(async (nameArg, valueArg, opts, cmd) => {
|
|
5602
6128
|
const client = await clientFor(cmd.optsWithGlobals(), { autoStart: false });
|
|
5603
6129
|
try {
|
|
5604
6130
|
const owner = opts.box === void 0 ? requireCloud4(client) : void 0;
|
|
@@ -5635,14 +6161,14 @@ var setCommand2 = new Command63("set").description("add or change a variable; bo
|
|
|
5635
6161
|
});
|
|
5636
6162
|
|
|
5637
6163
|
// src/commands/env/index.ts
|
|
5638
|
-
var envCommand = new
|
|
6164
|
+
var envCommand = new Command79("env").description("environment variables every box of yours gets when it starts (secrets too)").addCommand(lsCommand10, { isDefault: true }).addCommand(setCommand2).addCommand(rmCommand9);
|
|
5639
6165
|
for (const cmd of envCommand.commands) cmd.option("--org <slug>", "the org's variables instead of yours");
|
|
5640
6166
|
|
|
5641
6167
|
// src/commands/domain/index.ts
|
|
5642
|
-
import { Command as
|
|
6168
|
+
import { Command as Command83 } from "commander";
|
|
5643
6169
|
|
|
5644
6170
|
// src/commands/domain/add.ts
|
|
5645
|
-
import { Command as
|
|
6171
|
+
import { Command as Command80 } from "commander";
|
|
5646
6172
|
|
|
5647
6173
|
// src/commands/domain/shared.ts
|
|
5648
6174
|
function requireCloud5(client) {
|
|
@@ -5662,7 +6188,7 @@ function hostIn(name, zone) {
|
|
|
5662
6188
|
}
|
|
5663
6189
|
|
|
5664
6190
|
// src/commands/domain/add.ts
|
|
5665
|
-
var addCommand3 = new
|
|
6191
|
+
var addCommand3 = new Command80("add").description("serve a box at a domain of your own, https://<domain>, with its certificate made for you").argument("<domain>", "a domain you control, like example.com or app.example.com").argument("<box>", "box name").option("--port <port>", "the box port the domain serves (default: the port of its kvbox.app address)").option("--json", "print the domain as JSON").action(async (domainArg, name, opts, cmd) => {
|
|
5666
6192
|
const domain = assertDomain(domainArg.trim().toLowerCase());
|
|
5667
6193
|
const port = opts.port === void 0 ? void 0 : parsePorts([opts.port])[0];
|
|
5668
6194
|
const client = await clientFor(cmd.optsWithGlobals());
|
|
@@ -5698,8 +6224,8 @@ kvbox checks every minute; ${domain} is served at ${d.url} once its records are
|
|
|
5698
6224
|
});
|
|
5699
6225
|
|
|
5700
6226
|
// src/commands/domain/ls.ts
|
|
5701
|
-
import { Command as
|
|
5702
|
-
var
|
|
6227
|
+
import { Command as Command81 } from "commander";
|
|
6228
|
+
var lsCommand11 = new Command81("ls").alias("list").description("list your custom domains and whether they are live").option("--json", "print as JSON").action(async (opts, cmd) => {
|
|
5703
6229
|
const client = await clientFor(cmd.optsWithGlobals());
|
|
5704
6230
|
try {
|
|
5705
6231
|
requireCloud5(client);
|
|
@@ -5728,8 +6254,8 @@ var lsCommand10 = new Command66("ls").alias("list").description("list your custo
|
|
|
5728
6254
|
});
|
|
5729
6255
|
|
|
5730
6256
|
// src/commands/domain/rm.ts
|
|
5731
|
-
import { Command as
|
|
5732
|
-
var
|
|
6257
|
+
import { Command as Command82 } from "commander";
|
|
6258
|
+
var rmCommand10 = new Command82("rm").alias("remove").description("stop serving a box at a custom domain").argument("<domain>", "the domain").option("-y, --yes", "do not ask for confirmation").action(async (domainArg, opts, cmd) => {
|
|
5733
6259
|
const domain = domainArg.trim().toLowerCase();
|
|
5734
6260
|
const client = await clientFor(cmd.optsWithGlobals());
|
|
5735
6261
|
try {
|
|
@@ -5750,7 +6276,7 @@ var rmCommand9 = new Command67("rm").alias("remove").description("stop serving a
|
|
|
5750
6276
|
});
|
|
5751
6277
|
|
|
5752
6278
|
// src/commands/domain/index.ts
|
|
5753
|
-
var domainCommand = new
|
|
6279
|
+
var domainCommand = new Command83("domain").alias("domains").description("serve a box at a domain of your own").addCommand(addCommand3).addCommand(lsCommand11).addCommand(rmCommand10);
|
|
5754
6280
|
for (const cmd of domainCommand.commands) cmd.option("--org <slug>", "act in this org (- for your personal boxes)");
|
|
5755
6281
|
|
|
5756
6282
|
// src/commands/update.ts
|
|
@@ -5758,7 +6284,7 @@ import { spawn as spawn6 } from "child_process";
|
|
|
5758
6284
|
import { mkdtempSync, rmSync, writeFileSync as writeFileSync2 } from "fs";
|
|
5759
6285
|
import { tmpdir } from "os";
|
|
5760
6286
|
import { join as join9 } from "path";
|
|
5761
|
-
import { Command as
|
|
6287
|
+
import { Command as Command84 } from "commander";
|
|
5762
6288
|
|
|
5763
6289
|
// src/update.ts
|
|
5764
6290
|
import { mkdirSync, readFileSync as readFileSync3, writeFileSync } from "fs";
|
|
@@ -5877,7 +6403,7 @@ function run2(cmd, args) {
|
|
|
5877
6403
|
child.on("exit", (code, signal) => resolve4(code ?? (signal ? 1 : 0)));
|
|
5878
6404
|
});
|
|
5879
6405
|
}
|
|
5880
|
-
var selfUpdateCommand = new
|
|
6406
|
+
var selfUpdateCommand = new Command84("update").description("update kvbox to the latest version (reruns the installer from kvbox.sh, agent skill included)").action(async () => {
|
|
5881
6407
|
suppressUpdateNotice();
|
|
5882
6408
|
const inst = installerFor(process.platform, process.env["KVBOX_INSTALL_URL"] || INSTALL_BASE);
|
|
5883
6409
|
let code;
|
|
@@ -5941,6 +6467,8 @@ var HELP_GROUPS = [
|
|
|
5941
6467
|
["vm", "everything about boxes: fork, rescale, ports, hooks, backups"],
|
|
5942
6468
|
["snapshots", "freeze a running box into a snapshot, start new boxes from it"],
|
|
5943
6469
|
["volume", "disks of their own that move between boxes"],
|
|
6470
|
+
["pg", "managed PostgreSQL: a database kvbox runs for you"],
|
|
6471
|
+
["managed", "every managed database, whatever its kind"],
|
|
5944
6472
|
["env", "environment variables and secrets every box of yours gets"],
|
|
5945
6473
|
["domain", "serve a box at a domain of your own"]
|
|
5946
6474
|
]
|
|
@@ -5998,10 +6526,12 @@ function installRootHelp(program) {
|
|
|
5998
6526
|
|
|
5999
6527
|
// src/program.ts
|
|
6000
6528
|
function buildProgram() {
|
|
6001
|
-
const program = new
|
|
6529
|
+
const program = new Command85("kvbox").description("self-hosted Firecracker microVMs with Docker-like ergonomics").version(VERSION, "-v, --version").option("--local", "use this machine's kvbox-server over its socket, ignoring kvbox login").option("--org <slug>", "the org: where kvbox new creates, and which box a name means when it is not one of yours; before the command here, or after it").showHelpAfterError().enablePositionalOptions();
|
|
6002
6530
|
program.addCommand(vmCommand);
|
|
6003
6531
|
program.addCommand(snapshotsCommand);
|
|
6004
6532
|
program.addCommand(volumeCommand);
|
|
6533
|
+
program.addCommand(pgCommand);
|
|
6534
|
+
program.addCommand(managedCommand);
|
|
6005
6535
|
program.addCommand(envCommand);
|
|
6006
6536
|
program.addCommand(domainCommand);
|
|
6007
6537
|
program.addCommand(loginCommand);
|