home-hosted 0.9.4 → 0.9.6
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/AGENTS.md +7 -6
- package/README.md +8 -1
- package/dist/cli.js +833 -31
- package/dist/cli.js.map +1 -1
- package/docs/RESTORE_DEMO.md +212 -0
- package/docs/UI_CREATION.md +1 -1
- package/docs/demo/blank-host-restore.sh +194 -0
- package/package.json +9 -9
- package/uis/stock/dist/ui.json +2 -2
package/dist/cli.js
CHANGED
|
@@ -4057,6 +4057,87 @@ function createControlRoute(deps) {
|
|
|
4057
4057
|
assertLocalCall(c, "restart a server");
|
|
4058
4058
|
const result = await requireWorkspace(deps.panel.requireWorkspace.bind(deps.panel), c.req.valid("query").workspace).supervisor.restart(c.req.param("id"), { actor: "user" });
|
|
4059
4059
|
return c.json(result, statusForAction(result));
|
|
4060
|
+
}).get("/backups", describeRoute({
|
|
4061
|
+
tags: ["panel"],
|
|
4062
|
+
summary: "List the stored backup archives (local token required)",
|
|
4063
|
+
responses: {
|
|
4064
|
+
200: {
|
|
4065
|
+
description: "The archives",
|
|
4066
|
+
content: jsonBody(backupsViewSchema)
|
|
4067
|
+
},
|
|
4068
|
+
403: { description: "Bad token, or not a local caller" }
|
|
4069
|
+
}
|
|
4070
|
+
}), (c) => {
|
|
4071
|
+
assertLocalCall(c, "list backups");
|
|
4072
|
+
return c.json(buildBackupsView(deps.backups));
|
|
4073
|
+
}).post("/backups", describeRoute({
|
|
4074
|
+
tags: ["panel"],
|
|
4075
|
+
summary: "Create a backup archive (local token required)",
|
|
4076
|
+
responses: {
|
|
4077
|
+
200: {
|
|
4078
|
+
description: "Created",
|
|
4079
|
+
content: jsonBody(backupCreatedResponseSchema)
|
|
4080
|
+
},
|
|
4081
|
+
400: ERROR_RESPONSES[400],
|
|
4082
|
+
403: { description: "Bad token, or not a local caller" }
|
|
4083
|
+
}
|
|
4084
|
+
}), async (c) => {
|
|
4085
|
+
assertLocalCall(c, "create a backup");
|
|
4086
|
+
const password = c.req.query("password");
|
|
4087
|
+
const include = c.req.query("include")?.split(",").filter((part) => part.length > 0);
|
|
4088
|
+
if (c.req.query("confirm") !== "true") {
|
|
4089
|
+
const view = buildBackupsView(deps.backups);
|
|
4090
|
+
const wanted = include === void 0 ? null : new Set(include);
|
|
4091
|
+
const entries = view.entries.filter((entry) => wanted === null || wanted.has(entry.id) || entry.workspaceId !== void 0 && wanted.has(`workspace:${entry.workspaceId}`));
|
|
4092
|
+
return c.json({
|
|
4093
|
+
apply: false,
|
|
4094
|
+
entries,
|
|
4095
|
+
includePaths: view.includePaths
|
|
4096
|
+
});
|
|
4097
|
+
}
|
|
4098
|
+
const result = await deps.backups.create({
|
|
4099
|
+
password,
|
|
4100
|
+
include
|
|
4101
|
+
});
|
|
4102
|
+
if (!result.ok) throw new DetailedError(result.error ?? "the backup failed", {
|
|
4103
|
+
statusCode: 400,
|
|
4104
|
+
code: "BACKUP_FAILED"
|
|
4105
|
+
});
|
|
4106
|
+
return c.json({
|
|
4107
|
+
file: result.file,
|
|
4108
|
+
files: deps.backups.list()
|
|
4109
|
+
});
|
|
4110
|
+
}).post("/backups/restore", describeRoute({
|
|
4111
|
+
tags: ["panel"],
|
|
4112
|
+
summary: "Plan or apply a restore from a stored archive (local token required)",
|
|
4113
|
+
responses: {
|
|
4114
|
+
200: {
|
|
4115
|
+
description: "The plan",
|
|
4116
|
+
content: jsonBody(restorePlanSchema)
|
|
4117
|
+
},
|
|
4118
|
+
400: ERROR_RESPONSES[400],
|
|
4119
|
+
403: { description: "Bad token, or not a local caller" },
|
|
4120
|
+
404: ERROR_RESPONSES[404]
|
|
4121
|
+
}
|
|
4122
|
+
}), async (c) => {
|
|
4123
|
+
assertLocalCall(c, "restore a backup");
|
|
4124
|
+
const confirm = c.req.query("confirm") === "true";
|
|
4125
|
+
const name = c.req.query("name");
|
|
4126
|
+
if (name === void 0 || name.length === 0) throw new DetailedError("name is required", {
|
|
4127
|
+
statusCode: 400,
|
|
4128
|
+
code: "MISSING_NAME"
|
|
4129
|
+
});
|
|
4130
|
+
const archive = deps.backups.resolve(name);
|
|
4131
|
+
if (archive === null) throw new DetailedError(`no backup named "${name}"`, {
|
|
4132
|
+
statusCode: 404,
|
|
4133
|
+
code: "BACKUP_NOT_FOUND"
|
|
4134
|
+
});
|
|
4135
|
+
const include = c.req.query("include")?.split(",").filter((part) => part.length > 0);
|
|
4136
|
+
return c.json(await deps.backups.restore(archive, {
|
|
4137
|
+
confirm,
|
|
4138
|
+
password: c.req.query("password"),
|
|
4139
|
+
include
|
|
4140
|
+
}));
|
|
4060
4141
|
});
|
|
4061
4142
|
}
|
|
4062
4143
|
var init_control = __esmMin((() => {
|
|
@@ -4069,6 +4150,7 @@ var init_control = __esmMin((() => {
|
|
|
4069
4150
|
init_validator();
|
|
4070
4151
|
init_workspace();
|
|
4071
4152
|
init_loopback();
|
|
4153
|
+
init_state$1();
|
|
4072
4154
|
init_contracts();
|
|
4073
4155
|
}));
|
|
4074
4156
|
//#endregion
|
|
@@ -7094,9 +7176,9 @@ var init_events = __esmMin((() => {
|
|
|
7094
7176
|
//#region src/api/health.ts
|
|
7095
7177
|
/**
|
|
7096
7178
|
* Liveness for external monitors. Mounted outside `/api`, so it answers without a
|
|
7097
|
-
* session: it reports whether the panel itself is serving, and 503 when any server
|
|
7098
|
-
*
|
|
7099
|
-
* only included for an authenticated caller.
|
|
7179
|
+
* session: it reports whether the panel itself is serving, and 503 when any server has crashed,
|
|
7180
|
+
* when the panel stopped one because it could not serve, or while a running one is failing its own
|
|
7181
|
+
* health check. Server details are only included for an authenticated caller.
|
|
7100
7182
|
*/
|
|
7101
7183
|
function createHealthRoute(deps) {
|
|
7102
7184
|
return appFactory.createApp().get("/healthz", describeRoute({
|
|
@@ -7107,7 +7189,7 @@ function createHealthRoute(deps) {
|
|
|
7107
7189
|
description: "Serving",
|
|
7108
7190
|
content: jsonBody(healthResponseSchema)
|
|
7109
7191
|
},
|
|
7110
|
-
503: { description: "A server
|
|
7192
|
+
503: { description: "A server crashed, or the panel stopped one that could not serve, or a running one is unhealthy" }
|
|
7111
7193
|
}
|
|
7112
7194
|
}), (c) => {
|
|
7113
7195
|
const state = deps.panel.getState();
|
|
@@ -8616,18 +8698,6 @@ var init_proxy$2 = __esmMin((() => {
|
|
|
8616
8698
|
//#endregion
|
|
8617
8699
|
//#region src/services/proxy-config.ts
|
|
8618
8700
|
/**
|
|
8619
|
-
* The issuers for the public names: ACME with the account address, then the engine's
|
|
8620
|
-
* own CA as a last resort.
|
|
8621
|
-
*
|
|
8622
|
-
* The fallback is what keeps a missing certificate from being a dead connection: the
|
|
8623
|
-
* handshake completes with an untrusted certificate (the browser offers to continue),
|
|
8624
|
-
* instead of failing with a protocol error the visitor cannot read past. The engine
|
|
8625
|
-
* keeps trying ACME, and the panel reports which of the two is being served.
|
|
8626
|
-
*
|
|
8627
|
-
* ZeroSSL is deliberately absent: it needs an EAB key the panel does not collect, so
|
|
8628
|
-
* listing it only added a failing issuer and a second retry on every name.
|
|
8629
|
-
*/
|
|
8630
|
-
/**
|
|
8631
8701
|
* The DNS-01 challenge block, which names the panel as the one that answers it. The
|
|
8632
8702
|
* engine holds no DNS credentials: it asks the panel over the ACMEProxy protocol, and
|
|
8633
8703
|
* the panel writes the record through the workspace account the route names.
|
|
@@ -8643,6 +8713,18 @@ function challengeBlock(dns01) {
|
|
|
8643
8713
|
...dns01.resolvers.length > 0 ? { resolvers: dns01.resolvers } : {}
|
|
8644
8714
|
} } };
|
|
8645
8715
|
}
|
|
8716
|
+
/**
|
|
8717
|
+
* The issuers for the public names: ACME with the account address, then the engine's
|
|
8718
|
+
* own CA as a last resort.
|
|
8719
|
+
*
|
|
8720
|
+
* The fallback is what keeps a missing certificate from being a dead connection: the
|
|
8721
|
+
* handshake completes with an untrusted certificate (the browser offers to continue),
|
|
8722
|
+
* instead of failing with a protocol error the visitor cannot read past. The engine
|
|
8723
|
+
* keeps trying ACME, and the panel reports which of the two is being served.
|
|
8724
|
+
*
|
|
8725
|
+
* ZeroSSL is deliberately absent: it needs an EAB key the panel does not collect, so
|
|
8726
|
+
* listing it only added a failing issuer and a second retry on every name.
|
|
8727
|
+
*/
|
|
8646
8728
|
function acmeIssuers(acme, challenge = {}) {
|
|
8647
8729
|
return [{
|
|
8648
8730
|
module: "acme",
|
|
@@ -9793,7 +9875,6 @@ var init_proxy$1 = __esmMin((() => {
|
|
|
9793
9875
|
logger.error(`proxy: could not start: ${this.lastError}`);
|
|
9794
9876
|
}
|
|
9795
9877
|
}
|
|
9796
|
-
/** True when a nanny this panel would recognise still owns the engine. */
|
|
9797
9878
|
/**
|
|
9798
9879
|
* True while a process from the installed binary is alive, however the panel's view
|
|
9799
9880
|
* reads. `live()` additionally requires a fresh heartbeat, so a nanny that died and
|
|
@@ -9821,9 +9902,12 @@ var init_proxy$1 = __esmMin((() => {
|
|
|
9821
9902
|
};
|
|
9822
9903
|
if (!config.enabled) return base;
|
|
9823
9904
|
const applied = this.readApplied();
|
|
9824
|
-
const
|
|
9825
|
-
const
|
|
9826
|
-
const
|
|
9905
|
+
const boundPortsNow = (port, configured) => port === void 0 ? configured : port;
|
|
9906
|
+
const urls = [];
|
|
9907
|
+
const httpPort = boundPortsNow(applied === null ? void 0 : applied.httpPort, config.httpPort);
|
|
9908
|
+
if (httpPort !== null) urls.push(`http://127.0.0.1:${httpPort}`);
|
|
9909
|
+
const httpsPort = boundPortsNow(applied === null ? void 0 : applied.httpsPort, config.httpsPort);
|
|
9910
|
+
if (httpsPort !== null) urls.push(`https://127.0.0.1:${httpsPort}`);
|
|
9827
9911
|
const certExpiryDays = this.certificateExpiryDays();
|
|
9828
9912
|
if (!this.installed()) return {
|
|
9829
9913
|
...base,
|
|
@@ -9881,11 +9965,11 @@ var init_proxy$1 = __esmMin((() => {
|
|
|
9881
9965
|
lastError: this.lastError
|
|
9882
9966
|
};
|
|
9883
9967
|
}
|
|
9884
|
-
/** Records why the last start or apply failed, for the frame the page reads. */
|
|
9885
9968
|
/** Backoff for a failing `sync()` re-apply; see the retry notes there. */
|
|
9886
9969
|
nextSyncAttemptAt = 0;
|
|
9887
9970
|
syncFailures = 0;
|
|
9888
9971
|
lastSyncError = null;
|
|
9972
|
+
/** Records why the last start or apply failed, for the frame the page reads. */
|
|
9889
9973
|
recordError(message) {
|
|
9890
9974
|
this.lastError = message;
|
|
9891
9975
|
this.options.onStateChange();
|
|
@@ -9895,7 +9979,6 @@ var init_proxy$1 = __esmMin((() => {
|
|
|
9895
9979
|
this.options.onStateChange();
|
|
9896
9980
|
return config;
|
|
9897
9981
|
}
|
|
9898
|
-
/** Renders the current route table and applies it; the engine keeps serving on failure. */
|
|
9899
9982
|
/**
|
|
9900
9983
|
* Makes the engine ask the CA again for one name, without restarting it.
|
|
9901
9984
|
*
|
|
@@ -9955,6 +10038,7 @@ var init_proxy$1 = __esmMin((() => {
|
|
|
9955
10038
|
});
|
|
9956
10039
|
}
|
|
9957
10040
|
}
|
|
10041
|
+
/** Renders the current route table and applies it; the engine keeps serving on failure. */
|
|
9958
10042
|
async apply(options = {}) {
|
|
9959
10043
|
if (!this.config.enabled || !this.installed()) return;
|
|
9960
10044
|
await this.assertDns01Module();
|
|
@@ -9972,8 +10056,7 @@ var init_proxy$1 = __esmMin((() => {
|
|
|
9972
10056
|
await this.assertPortsFree(this.config.httpPort, this.config.httpsPort);
|
|
9973
10057
|
await this.postConfiguration(rendered);
|
|
9974
10058
|
this.rememberConfig(text);
|
|
9975
|
-
|
|
9976
|
-
this.rememberApplied(bound.includes(this.config.httpPort) ? this.config.httpPort : null, bound.includes(this.config.httpsPort) ? this.config.httpsPort : null);
|
|
10059
|
+
this.recordApplied(rendered);
|
|
9977
10060
|
const swept = this.sweepUnroutedCertificates();
|
|
9978
10061
|
if (swept > 0) logger.info(`proxy: dropped ${swept} certificate(s) for names no longer routed`);
|
|
9979
10062
|
this.lastError = null;
|
|
@@ -10062,6 +10145,19 @@ var init_proxy$1 = __esmMin((() => {
|
|
|
10062
10145
|
return null;
|
|
10063
10146
|
}
|
|
10064
10147
|
}
|
|
10148
|
+
/**
|
|
10149
|
+
* Records what the engine is now bound to, derived from the document it was given.
|
|
10150
|
+
*
|
|
10151
|
+
* One implementation for both write sites (`apply()` and `start()`): a `tls: 'off'`-only table
|
|
10152
|
+
* binds no HTTPS listener, and a table with nothing servable binds neither, so a configured port
|
|
10153
|
+
* the document did not bind is recorded as `null` rather than claimed. The preflight reads this
|
|
10154
|
+
* record as its witness, so a wrong number here admits a stranger standing on that port.
|
|
10155
|
+
*/
|
|
10156
|
+
recordApplied(rendered) {
|
|
10157
|
+
const bound = boundPorts(rendered);
|
|
10158
|
+
const config = this.config;
|
|
10159
|
+
this.rememberApplied(bound.includes(config.httpPort) ? config.httpPort : null, bound.includes(config.httpsPort) ? config.httpsPort : null);
|
|
10160
|
+
}
|
|
10065
10161
|
rememberApplied(httpPort, httpsPort) {
|
|
10066
10162
|
writeFileAtomic(path.join(this.options.stateDir, "applied.json"), `${JSON.stringify({
|
|
10067
10163
|
httpPort,
|
|
@@ -10124,8 +10220,10 @@ var init_proxy$1 = __esmMin((() => {
|
|
|
10124
10220
|
});
|
|
10125
10221
|
const current = fs.existsSync(this.options.configPath) ? fs.readFileSync(this.options.configPath, "utf8") : null;
|
|
10126
10222
|
const previous = fs.readFileSync(this.options.previousConfigPath, "utf8");
|
|
10127
|
-
|
|
10223
|
+
const restored = JSON.parse(previous);
|
|
10224
|
+
await this.postConfiguration(restored);
|
|
10128
10225
|
writeFileAtomic(this.options.configPath, previous, { mode: 384 });
|
|
10226
|
+
this.recordApplied(restored);
|
|
10129
10227
|
if (current === null) fs.rmSync(this.options.previousConfigPath, { force: true });
|
|
10130
10228
|
else writeFileAtomic(this.options.previousConfigPath, current, { mode: 384 });
|
|
10131
10229
|
this.lastError = null;
|
|
@@ -10634,7 +10732,7 @@ var init_proxy$1 = __esmMin((() => {
|
|
|
10634
10732
|
code: "ENGINE_NOT_READY"
|
|
10635
10733
|
});
|
|
10636
10734
|
}
|
|
10637
|
-
this.
|
|
10735
|
+
this.recordApplied(rendered);
|
|
10638
10736
|
this.lastError = null;
|
|
10639
10737
|
logger.info(`proxy: ${this.engine.info.label} serving on :${this.config.httpPort} and :${this.config.httpsPort}`);
|
|
10640
10738
|
this.options.onStateChange();
|
|
@@ -12234,8 +12332,8 @@ async function requestShutdown(runtime, timeoutMs = 4e3) {
|
|
|
12234
12332
|
* The wait is generous on purpose: a start may have to run a bootstrap, which has a
|
|
12235
12333
|
* 120s default timeout of its own. Past this it is a wedged panel, not a slow one.
|
|
12236
12334
|
*/
|
|
12237
|
-
async function requestControl(runtime, path, timeoutMs = 15e4) {
|
|
12238
|
-
const answer = await localCall(runtime, path,
|
|
12335
|
+
async function requestControl(runtime, path, timeoutMs = 15e4, method = "POST") {
|
|
12336
|
+
const answer = await localCall(runtime, path, method, runtime.token, timeoutMs);
|
|
12239
12337
|
if (answer === null) return null;
|
|
12240
12338
|
let body = null;
|
|
12241
12339
|
try {
|
|
@@ -12511,6 +12609,113 @@ var init_secret_parts = __esmMin((() => {
|
|
|
12511
12609
|
EXTRA_FIELDS = { ddns: ["ddns", "ddnsKdf"] };
|
|
12512
12610
|
}));
|
|
12513
12611
|
//#endregion
|
|
12612
|
+
//#region src/helpers/restore-lock.ts
|
|
12613
|
+
function readRecord(file) {
|
|
12614
|
+
try {
|
|
12615
|
+
const parsed = JSON.parse(fs.readFileSync(file, "utf8"));
|
|
12616
|
+
if (typeof parsed.pid !== "number" || typeof parsed.startedAt !== "number") return null;
|
|
12617
|
+
return {
|
|
12618
|
+
pid: parsed.pid,
|
|
12619
|
+
startedAt: parsed.startedAt,
|
|
12620
|
+
action: typeof parsed.action === "string" ? parsed.action : "restore",
|
|
12621
|
+
birthAt: typeof parsed.birthAt === "number" ? parsed.birthAt : null
|
|
12622
|
+
};
|
|
12623
|
+
} catch {
|
|
12624
|
+
return null;
|
|
12625
|
+
}
|
|
12626
|
+
}
|
|
12627
|
+
/**
|
|
12628
|
+
* Is the recorded holder still the process that took it?
|
|
12629
|
+
*
|
|
12630
|
+
* Signal 0 alone is not enough — pids are recycled, and a zombie answers it — so the process's own
|
|
12631
|
+
* start time is compared with the recorded one where the platform can report it. Elsewhere the pid is
|
|
12632
|
+
* all there is, and an alive pid is taken as alive, which errs towards "somebody is working" rather
|
|
12633
|
+
* than stealing a live lock.
|
|
12634
|
+
*/
|
|
12635
|
+
function holderIsAlive(record) {
|
|
12636
|
+
try {
|
|
12637
|
+
process.kill(record.pid, 0);
|
|
12638
|
+
} catch (error) {
|
|
12639
|
+
return error.code === "EPERM";
|
|
12640
|
+
}
|
|
12641
|
+
const birth = processBirthSync(record.pid);
|
|
12642
|
+
if (birth === null) return true;
|
|
12643
|
+
if (record.birthAt != null) return Math.abs(birth - record.birthAt) <= BIRTH_TOLERANCE_MS;
|
|
12644
|
+
return birth <= record.startedAt + BIRTH_TOLERANCE_MS;
|
|
12645
|
+
}
|
|
12646
|
+
/** Wall-clock start of a pid in ms, or null where the platform cannot report one. */
|
|
12647
|
+
function processBirthSync(pid) {
|
|
12648
|
+
try {
|
|
12649
|
+
const stat = fs.readFileSync(`/proc/${pid}/stat`, "utf8");
|
|
12650
|
+
const fields = stat.slice(stat.lastIndexOf(")") + 2).split(" ");
|
|
12651
|
+
const ticks = Number.parseFloat(fields[19] ?? "");
|
|
12652
|
+
if (!Number.isFinite(ticks)) return null;
|
|
12653
|
+
const boot = /^btime (\d+)$/m.exec(fs.readFileSync("/proc/stat", "utf8"))?.[1];
|
|
12654
|
+
if (boot === void 0) return null;
|
|
12655
|
+
return Number.parseInt(boot, 10) * 1e3 + ticks / 100 * 1e3;
|
|
12656
|
+
} catch {
|
|
12657
|
+
return null;
|
|
12658
|
+
}
|
|
12659
|
+
}
|
|
12660
|
+
/**
|
|
12661
|
+
* Take the restore lock, or explain who holds it.
|
|
12662
|
+
*
|
|
12663
|
+
* `null` means somebody else is restoring. The caller reports it rather than queueing: a restore is
|
|
12664
|
+
* an interactive decision, and waiting on one you did not start is not what a person asked for.
|
|
12665
|
+
*/
|
|
12666
|
+
function acquireRestoreLock(hhDir, action = "restore") {
|
|
12667
|
+
const file = path.join(hhDir, "restore.lock");
|
|
12668
|
+
fs.mkdirSync(hhDir, { recursive: true });
|
|
12669
|
+
const mine = {
|
|
12670
|
+
pid: process.pid,
|
|
12671
|
+
startedAt: Date.now(),
|
|
12672
|
+
action,
|
|
12673
|
+
birthAt: processBirthSync(process.pid)
|
|
12674
|
+
};
|
|
12675
|
+
const release = () => {
|
|
12676
|
+
const current = readRecord(file);
|
|
12677
|
+
if (current !== null && (current.pid !== mine.pid || current.startedAt !== mine.startedAt)) return;
|
|
12678
|
+
fs.rmSync(file, { force: true });
|
|
12679
|
+
};
|
|
12680
|
+
const existing = readRecord(file);
|
|
12681
|
+
if (existing !== null) {
|
|
12682
|
+
if (!(!holderIsAlive(existing) || Date.now() - existing.startedAt > STALE_AFTER_MS)) return { heldBy: existing };
|
|
12683
|
+
writeFileAtomic(file, `${JSON.stringify(mine)}\n`, { mode: 384 });
|
|
12684
|
+
return {
|
|
12685
|
+
release,
|
|
12686
|
+
replaced: existing
|
|
12687
|
+
};
|
|
12688
|
+
}
|
|
12689
|
+
try {
|
|
12690
|
+
fs.writeFileSync(file, `${JSON.stringify(mine)}\n`, {
|
|
12691
|
+
flag: "wx",
|
|
12692
|
+
mode: 384
|
|
12693
|
+
});
|
|
12694
|
+
} catch (error) {
|
|
12695
|
+
if (error.code === "EEXIST") {
|
|
12696
|
+
const holder = readRecord(file);
|
|
12697
|
+
return holder === null ? { heldBy: {
|
|
12698
|
+
pid: 0,
|
|
12699
|
+
startedAt: Date.now(),
|
|
12700
|
+
action: "unreadable"
|
|
12701
|
+
} } : { heldBy: holder };
|
|
12702
|
+
}
|
|
12703
|
+
throw error;
|
|
12704
|
+
}
|
|
12705
|
+
let released = false;
|
|
12706
|
+
return { release: () => {
|
|
12707
|
+
if (released) return;
|
|
12708
|
+
released = true;
|
|
12709
|
+
release();
|
|
12710
|
+
} };
|
|
12711
|
+
}
|
|
12712
|
+
var STALE_AFTER_MS, BIRTH_TOLERANCE_MS;
|
|
12713
|
+
var init_restore_lock = __esmMin((() => {
|
|
12714
|
+
init_atomic();
|
|
12715
|
+
STALE_AFTER_MS = 18e5;
|
|
12716
|
+
BIRTH_TOLERANCE_MS = 2e3;
|
|
12717
|
+
}));
|
|
12718
|
+
//#endregion
|
|
12514
12719
|
//#region src/providers/archive.ts
|
|
12515
12720
|
/** Recognised by content, never by the file's name. */
|
|
12516
12721
|
function isZipArchive(file) {
|
|
@@ -13771,6 +13976,11 @@ var init_supervisor = __esmMin((() => {
|
|
|
13771
13976
|
error: `docker could not stop the container "${docker.container}" publishing port ${port} — check \`docker logs ${docker.container}\``
|
|
13772
13977
|
};
|
|
13773
13978
|
}
|
|
13979
|
+
if (!await this.waitForPortRelease(entry, port)) return {
|
|
13980
|
+
...empty,
|
|
13981
|
+
port,
|
|
13982
|
+
error: `port ${port} is still in use, but no listener could be attributed to it — this user cannot inspect that socket, so it cannot be freed from here`
|
|
13983
|
+
};
|
|
13774
13984
|
return {
|
|
13775
13985
|
...empty,
|
|
13776
13986
|
port,
|
|
@@ -15178,6 +15388,35 @@ function resolveBackupPaths(servers, includePaths = [], vars) {
|
|
|
15178
15388
|
});
|
|
15179
15389
|
}
|
|
15180
15390
|
/**
|
|
15391
|
+
* What a backup captures, from a workspace list and the panel-wide paths.
|
|
15392
|
+
*
|
|
15393
|
+
* The **one** implementation, called by the running panel and by the CLI when no panel is up. A
|
|
15394
|
+
* second copy is how an offline backup would quietly capture something different from an online one
|
|
15395
|
+
* — and the archive is the thing people restore from, so a silent difference is the worst kind.
|
|
15396
|
+
*
|
|
15397
|
+
* `runningServers` stays a callback rather than a list: the panel answers it from live supervisor
|
|
15398
|
+
* views, and an offline caller answers it from the nanny state on disk. It is optional because a
|
|
15399
|
+
* caller that cannot know leaves it out; the warning it feeds is then simply absent.
|
|
15400
|
+
*/
|
|
15401
|
+
function buildBackupSources(input) {
|
|
15402
|
+
return {
|
|
15403
|
+
globalSettingsPath: input.globalSettingsPath,
|
|
15404
|
+
globalSecretsPath: input.globalSecretsPath,
|
|
15405
|
+
tlsDir: input.tlsDir,
|
|
15406
|
+
registryPath: input.registryPath,
|
|
15407
|
+
includePaths: [...input.includePaths],
|
|
15408
|
+
...input.runningServers === void 0 ? {} : { runningServers: input.runningServers },
|
|
15409
|
+
workspaces: input.workspaces.map((workspace) => ({
|
|
15410
|
+
id: workspace.id,
|
|
15411
|
+
label: workspace.label,
|
|
15412
|
+
settingsPath: workspace.settingsPath,
|
|
15413
|
+
serversPath: workspace.serversPath,
|
|
15414
|
+
secretsPath: workspace.secretsPath,
|
|
15415
|
+
servers: workspace.servers
|
|
15416
|
+
}))
|
|
15417
|
+
};
|
|
15418
|
+
}
|
|
15419
|
+
/**
|
|
15181
15420
|
* A restored settings file is accepted when this release can read it — through
|
|
15182
15421
|
* the same tolerant parser the stores use, so an archive from a newer release
|
|
15183
15422
|
* keeps only the keys this one understands instead of being refused outright.
|
|
@@ -15309,6 +15548,7 @@ var init_backups = __esmMin((() => {
|
|
|
15309
15548
|
init_env_file();
|
|
15310
15549
|
init_logger();
|
|
15311
15550
|
init_paths();
|
|
15551
|
+
init_restore_lock();
|
|
15312
15552
|
init_template();
|
|
15313
15553
|
init_version();
|
|
15314
15554
|
init_archive();
|
|
@@ -15571,6 +15811,18 @@ var init_backups = __esmMin((() => {
|
|
|
15571
15811
|
* archive's manifest alone.
|
|
15572
15812
|
*/
|
|
15573
15813
|
async restore(archivePath, options) {
|
|
15814
|
+
const lock = options.confirm ? acquireRestoreLock(this.options.dataRoot) : null;
|
|
15815
|
+
if (lock !== null && "heldBy" in lock) throw new DetailedError(`another restore is running (pid ${lock.heldBy.pid}, since ${new Date(lock.heldBy.startedAt).toISOString()}) — wait for it, or remove ${path.join(this.options.dataRoot, "restore.lock")} if it is stale`, {
|
|
15816
|
+
statusCode: 409,
|
|
15817
|
+
code: "RESTORE_IN_PROGRESS"
|
|
15818
|
+
});
|
|
15819
|
+
try {
|
|
15820
|
+
return await this.applyRestore(archivePath, options);
|
|
15821
|
+
} finally {
|
|
15822
|
+
if (lock !== null && !("heldBy" in lock)) lock.release();
|
|
15823
|
+
}
|
|
15824
|
+
}
|
|
15825
|
+
async applyRestore(archivePath, options) {
|
|
15574
15826
|
const password = options.password !== void 0 && options.password.length > 0 ? options.password : null;
|
|
15575
15827
|
const plan = {
|
|
15576
15828
|
dryRun: !options.confirm,
|
|
@@ -17899,6 +18151,7 @@ var init_panel = __esmMin((() => {
|
|
|
17899
18151
|
init_paths();
|
|
17900
18152
|
init_version();
|
|
17901
18153
|
init_ddns$2();
|
|
18154
|
+
init_backups();
|
|
17902
18155
|
init_config_watch();
|
|
17903
18156
|
init_ddns();
|
|
17904
18157
|
init_history();
|
|
@@ -18017,7 +18270,7 @@ var init_panel = __esmMin((() => {
|
|
|
18017
18270
|
}
|
|
18018
18271
|
/** The sources a backup archive is built from and restored into. */
|
|
18019
18272
|
backupSources() {
|
|
18020
|
-
return {
|
|
18273
|
+
return buildBackupSources({
|
|
18021
18274
|
globalSettingsPath: this.options.settings.path,
|
|
18022
18275
|
globalSecretsPath: this.options.secrets.path,
|
|
18023
18276
|
tlsDir: this.options.tls.directory,
|
|
@@ -18035,7 +18288,7 @@ var init_panel = __esmMin((() => {
|
|
|
18035
18288
|
secretsPath: runtime.secrets.path,
|
|
18036
18289
|
servers: runtime.store.servers
|
|
18037
18290
|
}))
|
|
18038
|
-
};
|
|
18291
|
+
});
|
|
18039
18292
|
}
|
|
18040
18293
|
create(input) {
|
|
18041
18294
|
const workspace = this.options.registry.create(input);
|
|
@@ -20878,6 +21131,539 @@ var init_ui_revert = __esmMin((() => {
|
|
|
20878
21131
|
});
|
|
20879
21132
|
}));
|
|
20880
21133
|
//#endregion
|
|
21134
|
+
//#region src/cli/backup-offline.ts
|
|
21135
|
+
/**
|
|
21136
|
+
* A backup service for a host whose panel is **not running**.
|
|
21137
|
+
*
|
|
21138
|
+
* `backup` used to need the daemon because the service is built inside it, so backing up or
|
|
21139
|
+
* restoring meant leaving the panel up — the opposite of what someone migrating a machine wants.
|
|
21140
|
+
* Nothing in the service needs the daemon: every source is a file, and the one live fact it wants
|
|
21141
|
+
* (which entries are running) is on disk as the nanny state each entry writes.
|
|
21142
|
+
*
|
|
21143
|
+
* This builds the same sources the panel does, through the same `buildBackupSources`, so an offline
|
|
21144
|
+
* archive cannot differ from an online one. `runningServers` is answered from `.state/*.json` rather
|
|
21145
|
+
* than from supervisor views: a *persistent* entry keeps running when the panel stops, so "no panel"
|
|
21146
|
+
* does not mean "nothing is running", and the restore warning has to keep telling the truth.
|
|
21147
|
+
*/
|
|
21148
|
+
/** The workspace ids this host knows, from the registry on disk. */
|
|
21149
|
+
function workspaceIds(registry) {
|
|
21150
|
+
return registry.all().map((workspace) => ({
|
|
21151
|
+
id: workspace.id,
|
|
21152
|
+
label: workspace.label
|
|
21153
|
+
}));
|
|
21154
|
+
}
|
|
21155
|
+
/**
|
|
21156
|
+
* Entries whose nanny is alive right now, read from the state files they rewrite.
|
|
21157
|
+
*
|
|
21158
|
+
* Every path comes from `hhDir`, never from the module-level helpers: those resolve from this
|
|
21159
|
+
* process's own `HHOSTED_HOME`, so a caller passing `--home` elsewhere would read a different host's
|
|
21160
|
+
* state — which is exactly how a test came to depend on the developer's real `~/.home-hosted`.
|
|
21161
|
+
*/
|
|
21162
|
+
function runningFromDisk(hhDir, ids) {
|
|
21163
|
+
const running = [];
|
|
21164
|
+
for (const workspaceId of ids) {
|
|
21165
|
+
const stateDir = path.join(hhDir, workspaceId, ".state");
|
|
21166
|
+
let names;
|
|
21167
|
+
try {
|
|
21168
|
+
names = fs.readdirSync(stateDir);
|
|
21169
|
+
} catch {
|
|
21170
|
+
continue;
|
|
21171
|
+
}
|
|
21172
|
+
for (const name of names) {
|
|
21173
|
+
if (!name.endsWith(".json") || name.endsWith(".spec.json") || name.endsWith(".tmp")) continue;
|
|
21174
|
+
const serverId = name.replace(/\.run\.json$|\.json$/, "");
|
|
21175
|
+
if (serverId.length === 0) continue;
|
|
21176
|
+
const state = readNannyState(path.join(stateDir, name));
|
|
21177
|
+
if (state === null) continue;
|
|
21178
|
+
if (Date.now() - state.heartbeatAt <= 3e4) running.push({
|
|
21179
|
+
workspaceId,
|
|
21180
|
+
serverId
|
|
21181
|
+
});
|
|
21182
|
+
}
|
|
21183
|
+
}
|
|
21184
|
+
return running;
|
|
21185
|
+
}
|
|
21186
|
+
/**
|
|
21187
|
+
* Build the service against this host's files.
|
|
21188
|
+
*
|
|
21189
|
+
* `ensureWorkspace` and `onRestored` are the two panel hooks, and both are deliberately absent: with
|
|
21190
|
+
* no daemon there is nothing to reload, and a restore that brings a workspace this host lacks leaves
|
|
21191
|
+
* it on disk for the next `up` to pick up. The registry is written by the service itself, which is
|
|
21192
|
+
* what makes that work.
|
|
21193
|
+
*/
|
|
21194
|
+
function createOfflineBackup(options = {}) {
|
|
21195
|
+
const hhDir$1 = options.hhDir ?? hhDir;
|
|
21196
|
+
const settingsPath = options.settingsPath ?? globalSettingsPath;
|
|
21197
|
+
const registryPath = path.join(hhDir$1, path.basename(workspacesPath));
|
|
21198
|
+
const settings = new GlobalSettingsStore(settingsPath);
|
|
21199
|
+
settings.load();
|
|
21200
|
+
const registry = new WorkspaceRegistry(registryPath);
|
|
21201
|
+
registry.load();
|
|
21202
|
+
const workspaces = workspaceIds(registry);
|
|
21203
|
+
const ids = workspaces.map((workspace) => workspace.id);
|
|
21204
|
+
const loaded = workspaces.map((workspace) => {
|
|
21205
|
+
const dir = path.join(hhDir$1, workspace.id);
|
|
21206
|
+
const settingsPath = path.join(dir, "settings.json");
|
|
21207
|
+
const serversPath = path.join(dir, "servers.config.json");
|
|
21208
|
+
const secretsPath = path.join(dir, ".secrets.json");
|
|
21209
|
+
const store = new WorkspaceStore(workspace.id, settingsPath, serversPath);
|
|
21210
|
+
store.load();
|
|
21211
|
+
return {
|
|
21212
|
+
id: workspace.id,
|
|
21213
|
+
label: workspace.label,
|
|
21214
|
+
settingsPath,
|
|
21215
|
+
serversPath,
|
|
21216
|
+
secretsPath,
|
|
21217
|
+
servers: store.servers
|
|
21218
|
+
};
|
|
21219
|
+
});
|
|
21220
|
+
return {
|
|
21221
|
+
service: new BackupService({
|
|
21222
|
+
dataRoot: hhDir$1,
|
|
21223
|
+
getConfig: () => settings.backups,
|
|
21224
|
+
/**
|
|
21225
|
+
* Register a workspace the archive brought.
|
|
21226
|
+
*
|
|
21227
|
+
* A panel-only hook in the running case, because the panel creates the runtime itself. With no
|
|
21228
|
+
* panel there is nobody to do it, so the registry is written here — otherwise a restored
|
|
21229
|
+
* workspace's files land on disk unreachable, and the next `up` never looks at them.
|
|
21230
|
+
*/
|
|
21231
|
+
ensureWorkspace: (input) => {
|
|
21232
|
+
if (registry.has(input.id)) return;
|
|
21233
|
+
registry.create(input);
|
|
21234
|
+
},
|
|
21235
|
+
getSources: () => buildBackupSources({
|
|
21236
|
+
globalSettingsPath: settings.path,
|
|
21237
|
+
globalSecretsPath: path.join(hhDir$1, path.basename(globalSecretsPath)),
|
|
21238
|
+
tlsDir: path.join(hhDir$1, path.basename(tlsDir)),
|
|
21239
|
+
registryPath,
|
|
21240
|
+
includePaths: settings.backups.includePaths,
|
|
21241
|
+
runningServers: () => runningFromDisk(hhDir$1, ids),
|
|
21242
|
+
workspaces: loaded
|
|
21243
|
+
})
|
|
21244
|
+
}),
|
|
21245
|
+
workspaceIds: ids,
|
|
21246
|
+
runningNow: () => runningFromDisk(hhDir$1, ids)
|
|
21247
|
+
};
|
|
21248
|
+
}
|
|
21249
|
+
var init_backup_offline = __esmMin((() => {
|
|
21250
|
+
init_settings$1();
|
|
21251
|
+
init_store();
|
|
21252
|
+
init_workspaces();
|
|
21253
|
+
init_paths();
|
|
21254
|
+
init_nanny$2();
|
|
21255
|
+
init_backups();
|
|
21256
|
+
}));
|
|
21257
|
+
//#endregion
|
|
21258
|
+
//#region src/cli/backup.ts
|
|
21259
|
+
var backup_exports = /* @__PURE__ */ __exportAll({
|
|
21260
|
+
backupCommand: () => backupCommand,
|
|
21261
|
+
runBackupCreate: () => runBackupCreate,
|
|
21262
|
+
runBackupList: () => runBackupList,
|
|
21263
|
+
runBackupRestore: () => runBackupRestore
|
|
21264
|
+
});
|
|
21265
|
+
function human(bytes) {
|
|
21266
|
+
if (bytes < 1024) return `${bytes} B`;
|
|
21267
|
+
if (bytes < 1048576) return `${Math.round(bytes / 1024)} KB`;
|
|
21268
|
+
return `${(bytes / 1024 / 1024).toFixed(1)} MB`;
|
|
21269
|
+
}
|
|
21270
|
+
/** The running panel's record, or a message saying what to do instead. */
|
|
21271
|
+
async function livePanel() {
|
|
21272
|
+
const daemon = await Promise.resolve().then(() => (init_daemon(), daemon_exports));
|
|
21273
|
+
const runtime = daemon.readRuntime();
|
|
21274
|
+
if (runtime === null || !await daemon.runtimeIsAlive(runtime)) {
|
|
21275
|
+
if (runtime !== null && await daemon.runtimeIsGone(runtime)) daemon.clearRuntime();
|
|
21276
|
+
return null;
|
|
21277
|
+
}
|
|
21278
|
+
return Object.assign(runtime, { request: daemon.requestControl });
|
|
21279
|
+
}
|
|
21280
|
+
/**
|
|
21281
|
+
* Whether to talk to a panel or work on the files directly.
|
|
21282
|
+
*
|
|
21283
|
+
* Both paths run the same service, so the only question is who owns the state: a live panel is the
|
|
21284
|
+
* authority on what is running, and a stopped one has no opinion to disagree with.
|
|
21285
|
+
*/
|
|
21286
|
+
async function usesPanel() {
|
|
21287
|
+
const daemon = await Promise.resolve().then(() => (init_daemon(), daemon_exports));
|
|
21288
|
+
const runtime = daemon.readRuntime();
|
|
21289
|
+
if (runtime === null || !await daemon.runtimeIsAlive(runtime)) return false;
|
|
21290
|
+
return (await daemon.probeRuntime(runtime)).reachable;
|
|
21291
|
+
}
|
|
21292
|
+
/**
|
|
21293
|
+
* The offline service.
|
|
21294
|
+
*
|
|
21295
|
+
* `--config` is an `up`-only flag: it is not written to settings, and `run.json`'s `configPath` is the
|
|
21296
|
+
* state directory, not a servers file. So an offline run cannot know about one, and reads the default
|
|
21297
|
+
* workspace's `servers.config.json` — the same file the panel reads when started without `--config`.
|
|
21298
|
+
* A `--config` project has to start the panel first, which is the honest limit rather than a silent
|
|
21299
|
+
* difference between the two paths.
|
|
21300
|
+
*/
|
|
21301
|
+
async function offlineBackup() {
|
|
21302
|
+
return createOfflineBackup();
|
|
21303
|
+
}
|
|
21304
|
+
/** Turn a non-200 control answer into the panel's own message, or the status. */
|
|
21305
|
+
function refusal(answer) {
|
|
21306
|
+
const body = answer.body;
|
|
21307
|
+
if (body?.message !== void 0) fail(body.message);
|
|
21308
|
+
fail(`the panel answered ${answer.status}`);
|
|
21309
|
+
}
|
|
21310
|
+
/**
|
|
21311
|
+
* The flag combination, as far as it can be judged without asking the panel.
|
|
21312
|
+
*
|
|
21313
|
+
* `null` means "neither flag", which only the caller can resolve — it lists the available ids, and
|
|
21314
|
+
* that needs a panel. Every other case is decided here, before any lookup, so a typo is refused
|
|
21315
|
+
* whether or not anything is running.
|
|
21316
|
+
*/
|
|
21317
|
+
function parseScope(all, raw) {
|
|
21318
|
+
if (all && raw !== void 0) fail("--include and --all are exclusive — pass one or the other");
|
|
21319
|
+
if (all) return { kind: "all" };
|
|
21320
|
+
if (raw === void 0) return null;
|
|
21321
|
+
const ids = raw.split(",").map((part) => part.trim()).filter((part) => part.length > 0);
|
|
21322
|
+
if (ids.length === 0) fail("--include is empty — name the ids to act on, or pass --all for everything");
|
|
21323
|
+
return {
|
|
21324
|
+
kind: "include",
|
|
21325
|
+
ids
|
|
21326
|
+
};
|
|
21327
|
+
}
|
|
21328
|
+
/**
|
|
21329
|
+
* Neither flag given. Refused outright — the ids are a *bonus*, not the reason.
|
|
21330
|
+
*
|
|
21331
|
+
* The refusal must not depend on a panel being up: a mistyped command deserves the flag error, not
|
|
21332
|
+
* "home-hosted is not running". So the list is best-effort, and a failure to produce it is silent.
|
|
21333
|
+
*/
|
|
21334
|
+
async function refuseWithoutScope(action, suffix) {
|
|
21335
|
+
const ids = await availableIdsFor(action, suffix).catch(() => []);
|
|
21336
|
+
fail(`name what to act on — pass --include <ids> or --all${ids.length === 0 ? "" : `\n\navailable:\n${ids.map((id) => ` ${id}`).join("\n")}`}`);
|
|
21337
|
+
}
|
|
21338
|
+
/**
|
|
21339
|
+
* The ids this action could be scoped to.
|
|
21340
|
+
*
|
|
21341
|
+
* `create` asks what the entries declare; `restore` asks what the archive holds. Both come from the
|
|
21342
|
+
* panel, so the list in a refusal is the real one rather than a hardcoded guess. A panel that cannot
|
|
21343
|
+
* be reached yields no ids — the refusal still fires, it just cannot enumerate.
|
|
21344
|
+
*/
|
|
21345
|
+
async function availableIdsFor(action, suffix) {
|
|
21346
|
+
const daemon = await Promise.resolve().then(() => (init_daemon(), daemon_exports));
|
|
21347
|
+
const runtime = daemon.readRuntime();
|
|
21348
|
+
if (runtime === null) return [];
|
|
21349
|
+
const path = action === "create" ? "/_hh/backups" : "/_hh/backups/restore";
|
|
21350
|
+
const answer = await daemon.requestControl(runtime, `${path}${suffix}`, 12e4);
|
|
21351
|
+
if (answer === null || answer.status !== 200) return [];
|
|
21352
|
+
const body = answer.body;
|
|
21353
|
+
return ((action === "create" ? body?.entries : body?.items) ?? []).map((item) => item.id).filter((id) => typeof id === "string");
|
|
21354
|
+
}
|
|
21355
|
+
/** `?a=b`, or `''`. */
|
|
21356
|
+
function suffixOf(query) {
|
|
21357
|
+
return query.size === 0 ? "" : `?${query.toString()}`;
|
|
21358
|
+
}
|
|
21359
|
+
/** Appends one query parameter, keeping the `?`/`&` right. */
|
|
21360
|
+
function withParam(suffix, key, value) {
|
|
21361
|
+
return `${suffix}${suffix === "" ? "?" : "&"}${key}=${encodeURIComponent(value)}`;
|
|
21362
|
+
}
|
|
21363
|
+
/**
|
|
21364
|
+
* Ask before writing.
|
|
21365
|
+
*
|
|
21366
|
+
* A session that cannot be asked gets the plan and a one-line instruction — not a silent write, and
|
|
21367
|
+
* not a silent no-op that looks like one.
|
|
21368
|
+
*/
|
|
21369
|
+
async function consent(question) {
|
|
21370
|
+
if (process.stdin.isTTY !== true) {
|
|
21371
|
+
process.stdout.write("\nnot applied — this session cannot ask. Re-run with `--confirm` to apply.\n");
|
|
21372
|
+
return false;
|
|
21373
|
+
}
|
|
21374
|
+
const answer = await prompt(`${question} [y/N] `);
|
|
21375
|
+
if (/^yes$|^y$/i.test(answer.trim())) {
|
|
21376
|
+
process.stdout.write("\n");
|
|
21377
|
+
return true;
|
|
21378
|
+
}
|
|
21379
|
+
process.stdout.write("cancelled — nothing was written\n");
|
|
21380
|
+
return false;
|
|
21381
|
+
}
|
|
21382
|
+
/**
|
|
21383
|
+
* Run against the panel when one is up, and against the files when it is not.
|
|
21384
|
+
*
|
|
21385
|
+
* Both run the same `BackupService`, so the archive cannot differ; only the authority on what is
|
|
21386
|
+
* running changes. A stopped panel is exactly when someone wants to back up or restore — the services
|
|
21387
|
+
* are down because they stopped them on purpose.
|
|
21388
|
+
*/
|
|
21389
|
+
async function runBackupList(json) {
|
|
21390
|
+
if (await usesPanel()) {
|
|
21391
|
+
const runtime = await livePanel();
|
|
21392
|
+
if (runtime !== null) {
|
|
21393
|
+
const answer = await runtime.request(runtime, "/_hh/backups", 3e4, "GET");
|
|
21394
|
+
if (answer === null) fail(`the control panel is not answering on ${runtime.probeUrl}`);
|
|
21395
|
+
if (answer.status !== 200) refusal(answer);
|
|
21396
|
+
printList(answer.body, json);
|
|
21397
|
+
return;
|
|
21398
|
+
}
|
|
21399
|
+
}
|
|
21400
|
+
const { service } = await offlineBackup();
|
|
21401
|
+
await service.warm();
|
|
21402
|
+
printList(service.view(), json);
|
|
21403
|
+
}
|
|
21404
|
+
function printList(view, json) {
|
|
21405
|
+
const files = view?.files ?? [];
|
|
21406
|
+
if (json) {
|
|
21407
|
+
process.stdout.write(`${JSON.stringify(view, null, 2)}\n`);
|
|
21408
|
+
return;
|
|
21409
|
+
}
|
|
21410
|
+
if (files.length === 0) {
|
|
21411
|
+
process.stdout.write(`no backups yet — \`${invocation()} backup create --all\` takes one\n`);
|
|
21412
|
+
return;
|
|
21413
|
+
}
|
|
21414
|
+
const noun = files.length === 1 ? "archive" : "archives";
|
|
21415
|
+
process.stdout.write(`${bold(String(files.length))} ${noun}${view?.dir === void 0 ? "" : ` in ${dim(view.dir)}`}\n`);
|
|
21416
|
+
for (const file of files) {
|
|
21417
|
+
const when = file.createdAt == null ? "" : new Date(file.createdAt).toISOString().replace("T", " ").slice(0, 16);
|
|
21418
|
+
process.stdout.write(` ${file.name.padEnd(36)} ${human(file.sizeBytes).padStart(8)} ${dim(when)}${file.automatic ? dim(" auto") : ""}\n`);
|
|
21419
|
+
}
|
|
21420
|
+
}
|
|
21421
|
+
/**
|
|
21422
|
+
* Name the entries still running, without blocking.
|
|
21423
|
+
*
|
|
21424
|
+
* A **persistent** entry keeps running while the panel is stopped, so "panel off" is not "services
|
|
21425
|
+
* off" — and its data being captured mid-write is worth knowing before it is relied on. Warned rather
|
|
21426
|
+
* than refused: the user may know, and the panel's own backup has always proceeded here.
|
|
21427
|
+
*/
|
|
21428
|
+
function warnRunning(entries) {
|
|
21429
|
+
if (entries.length === 0) return;
|
|
21430
|
+
const named = entries.map((entry) => entry.serverId ?? entry.id ?? "").filter((name) => name.length > 0);
|
|
21431
|
+
const isOne = named.length === 1;
|
|
21432
|
+
process.stdout.write(`${dim(`! ${named.length} entr${isOne ? "y is" : "ies are"} still running (${named.join(", ")}) — its data may be captured mid-write`)}\n`);
|
|
21433
|
+
}
|
|
21434
|
+
async function runBackupCreate(options) {
|
|
21435
|
+
const scope = options.scope;
|
|
21436
|
+
const online = await usesPanel();
|
|
21437
|
+
const entries = await (async () => {
|
|
21438
|
+
if (online) {
|
|
21439
|
+
const runtime = await livePanel();
|
|
21440
|
+
if (runtime !== null) {
|
|
21441
|
+
const answer = await runtime.request(runtime, "/_hh/backups", 12e4);
|
|
21442
|
+
if (answer === null) fail(`the control panel is not answering on ${runtime.probeUrl}`);
|
|
21443
|
+
if (answer.status !== 200) refusal(answer);
|
|
21444
|
+
return answer.body?.entries ?? [];
|
|
21445
|
+
}
|
|
21446
|
+
}
|
|
21447
|
+
const { service } = await offlineBackup();
|
|
21448
|
+
await service.warm();
|
|
21449
|
+
return service.view().entries;
|
|
21450
|
+
})();
|
|
21451
|
+
const wanted = scope.kind === "all" ? entries : entries.filter((entry) => scope.ids.some((id) => entry.id === id || id.startsWith(`${entry.id}:`)));
|
|
21452
|
+
if (!options.confirm) {
|
|
21453
|
+
if (options.json) {
|
|
21454
|
+
process.stdout.write(`${JSON.stringify({
|
|
21455
|
+
action: "create",
|
|
21456
|
+
apply: false,
|
|
21457
|
+
entries: wanted
|
|
21458
|
+
}, null, 2)}\n`);
|
|
21459
|
+
return;
|
|
21460
|
+
}
|
|
21461
|
+
process.stdout.write(`${bold("Plan")} — what a backup would capture; nothing has been written yet\n`);
|
|
21462
|
+
for (const entry of wanted) {
|
|
21463
|
+
process.stdout.write(` ${String(entry.kind ?? "entry").padEnd(10)} ${entry.id}${entry.note == null ? "" : ` ${dim(`(${entry.note})`)}`}\n`);
|
|
21464
|
+
for (const item of entry.items ?? []) process.stdout.write(` ${dim("-")} ${item.id}\n`);
|
|
21465
|
+
}
|
|
21466
|
+
if (wanted.length === 0) {
|
|
21467
|
+
process.stdout.write(" (nothing matched — check --include, or what the entries declare)\n");
|
|
21468
|
+
return;
|
|
21469
|
+
}
|
|
21470
|
+
warnRunning(await runningEntries());
|
|
21471
|
+
if (!await consent("Take this backup?")) return;
|
|
21472
|
+
}
|
|
21473
|
+
const include = scope.kind === "include" ? scope.ids : void 0;
|
|
21474
|
+
const file = (await (async () => {
|
|
21475
|
+
if (online) {
|
|
21476
|
+
const runtime = await livePanel();
|
|
21477
|
+
if (runtime !== null) {
|
|
21478
|
+
const query = new URLSearchParams();
|
|
21479
|
+
if (options.password !== void 0 && options.password.length > 0) query.set("password", options.password);
|
|
21480
|
+
if (include !== void 0) query.set("include", include.join(","));
|
|
21481
|
+
const answer = await runtime.request(runtime, `/_hh/backups${withParam(suffixOf(query), "confirm", "true")}`, 6e5);
|
|
21482
|
+
if (answer === null) fail(`the control panel is not answering on ${runtime.probeUrl}`);
|
|
21483
|
+
if (answer.status !== 200) refusal(answer);
|
|
21484
|
+
return answer.body;
|
|
21485
|
+
}
|
|
21486
|
+
}
|
|
21487
|
+
const { service } = await offlineBackup();
|
|
21488
|
+
await service.warm();
|
|
21489
|
+
const result = await service.create({
|
|
21490
|
+
password: options.password,
|
|
21491
|
+
include
|
|
21492
|
+
});
|
|
21493
|
+
if (!result.ok) fail(result.error ?? "the backup failed");
|
|
21494
|
+
return { file: result.file };
|
|
21495
|
+
})())?.file;
|
|
21496
|
+
if (file === void 0) fail("the backup reported success but named no archive");
|
|
21497
|
+
if (options.json) {
|
|
21498
|
+
process.stdout.write(`${JSON.stringify({
|
|
21499
|
+
action: "create",
|
|
21500
|
+
apply: true,
|
|
21501
|
+
file
|
|
21502
|
+
}, null, 2)}\n`);
|
|
21503
|
+
return;
|
|
21504
|
+
}
|
|
21505
|
+
process.stdout.write(`${green("backup created")} ${file.name} ${dim(`(${human(file.sizeBytes)})`)}\n`);
|
|
21506
|
+
}
|
|
21507
|
+
/**
|
|
21508
|
+
* The entries running right now.
|
|
21509
|
+
*
|
|
21510
|
+
* Read from the state files in **both** cases: the panel's own backup view has no per-entry running
|
|
21511
|
+
* flag to read, and the state files are the same witness the panel's supervisor would give — a
|
|
21512
|
+
* persistent entry keeps its nanny alive and rewrites that file whether or not the panel is up.
|
|
21513
|
+
*/
|
|
21514
|
+
async function runningEntries() {
|
|
21515
|
+
return (await offlineBackup()).runningNow();
|
|
21516
|
+
}
|
|
21517
|
+
async function runBackupRestore(options) {
|
|
21518
|
+
const scope = options.scope;
|
|
21519
|
+
const service = await usesPanel() ? null : (await offlineBackup()).service;
|
|
21520
|
+
if (service !== null) await service.warm();
|
|
21521
|
+
const plan = await (async () => {
|
|
21522
|
+
if (service !== null) {
|
|
21523
|
+
const archive = service.resolve(options.name);
|
|
21524
|
+
if (archive === null) fail(`no backup named "${options.name}" — \`${invocation()} backup list\` shows them`);
|
|
21525
|
+
return await service.restore(archive, {
|
|
21526
|
+
confirm: false,
|
|
21527
|
+
password: options.password,
|
|
21528
|
+
include: scope.kind === "include" ? scope.ids : void 0
|
|
21529
|
+
});
|
|
21530
|
+
}
|
|
21531
|
+
const runtime = await livePanel();
|
|
21532
|
+
if (runtime === null) fail(`the control panel is not answering — try again once it is up`);
|
|
21533
|
+
const base = new URLSearchParams({ name: options.name });
|
|
21534
|
+
if (options.password !== void 0 && options.password.length > 0) base.set("password", options.password);
|
|
21535
|
+
if (scope.kind === "include") base.set("include", scope.ids.join(","));
|
|
21536
|
+
const answer = await runtime.request(runtime, `/_hh/backups/restore${suffixOf(base)}`, 12e4);
|
|
21537
|
+
if (answer === null) fail(`the control panel is not answering at ${runtime.probeUrl}`);
|
|
21538
|
+
if (answer.status !== 200) refusal(answer);
|
|
21539
|
+
return answer.body;
|
|
21540
|
+
})();
|
|
21541
|
+
const items = (plan?.items ?? []).filter((item) => item.selected !== false);
|
|
21542
|
+
if (options.json && !options.confirm) {
|
|
21543
|
+
process.stdout.write(`${JSON.stringify({
|
|
21544
|
+
action: "restore",
|
|
21545
|
+
apply: false,
|
|
21546
|
+
name: options.name,
|
|
21547
|
+
items,
|
|
21548
|
+
warnings: plan?.warnings ?? []
|
|
21549
|
+
}, null, 2)}\n`);
|
|
21550
|
+
return;
|
|
21551
|
+
}
|
|
21552
|
+
if (!options.json) {
|
|
21553
|
+
process.stdout.write(`${bold("Plan")} for ${options.name} — nothing has been written yet\n`);
|
|
21554
|
+
for (const item of items) process.stdout.write(` ${String(item.kind ?? "item").padEnd(10)} ${item.id ?? item.label ?? ""}\n`);
|
|
21555
|
+
for (const warning of plan?.warnings ?? []) process.stdout.write(` ${dim("!")} ${warning}\n`);
|
|
21556
|
+
if (items.length === 0) {
|
|
21557
|
+
process.stdout.write(" (nothing to restore — check --include, or what this host declares)\n");
|
|
21558
|
+
return;
|
|
21559
|
+
}
|
|
21560
|
+
}
|
|
21561
|
+
if (!options.confirm && !await consent(`Restore these ${items.length} item(s) over this host?`)) return;
|
|
21562
|
+
{
|
|
21563
|
+
const applied = await (async () => {
|
|
21564
|
+
if (service !== null) {
|
|
21565
|
+
const archive = service.resolve(options.name);
|
|
21566
|
+
return ((await service.restore(archive, {
|
|
21567
|
+
confirm: true,
|
|
21568
|
+
password: options.password,
|
|
21569
|
+
include: scope.kind === "include" ? scope.ids : void 0
|
|
21570
|
+
})).items ?? []).filter((item) => item.selected !== false);
|
|
21571
|
+
}
|
|
21572
|
+
const runtime = await livePanel();
|
|
21573
|
+
if (runtime === null) fail("the control panel stopped answering — nothing further was written");
|
|
21574
|
+
const base = new URLSearchParams({ name: options.name });
|
|
21575
|
+
if (options.password !== void 0 && options.password.length > 0) base.set("password", options.password);
|
|
21576
|
+
if (scope.kind === "include") base.set("include", scope.ids.join(","));
|
|
21577
|
+
const answer = await runtime.request(runtime, `/_hh/backups/restore${withParam(suffixOf(base), "confirm", "true")}`, 6e5);
|
|
21578
|
+
if (answer === null) fail(`the control panel is not answering at ${runtime.probeUrl}`);
|
|
21579
|
+
if (answer.status !== 200) refusal(answer);
|
|
21580
|
+
return (answer.body?.items ?? plan?.items ?? []).filter((item) => item.selected !== false);
|
|
21581
|
+
})();
|
|
21582
|
+
if (options.json) {
|
|
21583
|
+
process.stdout.write(`${JSON.stringify({
|
|
21584
|
+
action: "restore",
|
|
21585
|
+
apply: true,
|
|
21586
|
+
name: options.name,
|
|
21587
|
+
items: applied,
|
|
21588
|
+
warnings: plan?.warnings ?? []
|
|
21589
|
+
}, null, 2)}\n`);
|
|
21590
|
+
return;
|
|
21591
|
+
}
|
|
21592
|
+
process.stdout.write(`${green("restored")} ${applied.length} ${applied.length === 1 ? "item" : "items"} from ${options.name}\n`);
|
|
21593
|
+
for (const warning of plan?.warnings ?? []) process.stdout.write(` ${dim("!")} ${warning}\n`);
|
|
21594
|
+
if (service !== null) process.stdout.write(dim(`the panel is not running, so nothing was reloaded — the next \`${invocation()} up\` reads what was restored\n`));
|
|
21595
|
+
}
|
|
21596
|
+
}
|
|
21597
|
+
var backupCommand;
|
|
21598
|
+
var init_backup = __esmMin((() => {
|
|
21599
|
+
init_backup_offline();
|
|
21600
|
+
init_io();
|
|
21601
|
+
init_error_message();
|
|
21602
|
+
backupCommand = defineCommand({
|
|
21603
|
+
meta: { name: "backup" },
|
|
21604
|
+
args: {
|
|
21605
|
+
action: {
|
|
21606
|
+
type: "positional",
|
|
21607
|
+
description: "list (default) | create | restore",
|
|
21608
|
+
required: false
|
|
21609
|
+
},
|
|
21610
|
+
target: {
|
|
21611
|
+
type: "positional",
|
|
21612
|
+
description: "the archive name, for restore",
|
|
21613
|
+
required: false
|
|
21614
|
+
},
|
|
21615
|
+
password: {
|
|
21616
|
+
type: "string",
|
|
21617
|
+
description: "encrypt the archive, or unlock it on restore"
|
|
21618
|
+
},
|
|
21619
|
+
include: {
|
|
21620
|
+
type: "string",
|
|
21621
|
+
description: "comma-separated item ids to act on"
|
|
21622
|
+
},
|
|
21623
|
+
all: {
|
|
21624
|
+
type: "boolean",
|
|
21625
|
+
description: "act on everything, rather than naming ids"
|
|
21626
|
+
},
|
|
21627
|
+
confirm: {
|
|
21628
|
+
type: "boolean",
|
|
21629
|
+
description: "apply without asking (a restore writes over live data)"
|
|
21630
|
+
},
|
|
21631
|
+
json: {
|
|
21632
|
+
type: "boolean",
|
|
21633
|
+
description: "print the plan or the result as JSON"
|
|
21634
|
+
}
|
|
21635
|
+
},
|
|
21636
|
+
async run({ args }) {
|
|
21637
|
+
try {
|
|
21638
|
+
const action = args.action === void 0 || args.action.length === 0 ? "list" : args.action;
|
|
21639
|
+
const parsed = action === "create" || action === "restore" ? parseScope(args.all === true, args.include) : null;
|
|
21640
|
+
if (action === "list") await runBackupList(args.json === true);
|
|
21641
|
+
else if (action === "create") {
|
|
21642
|
+
const scope = parsed ?? await refuseWithoutScope("create", "");
|
|
21643
|
+
await runBackupCreate({
|
|
21644
|
+
password: args.password,
|
|
21645
|
+
scope,
|
|
21646
|
+
confirm: args.confirm === true,
|
|
21647
|
+
json: args.json === true
|
|
21648
|
+
});
|
|
21649
|
+
} else if (action === "restore") {
|
|
21650
|
+
if (args.target === void 0 || args.target.length === 0) fail("restore needs the archive name — `backup list` shows them");
|
|
21651
|
+
const scope = parsed ?? await refuseWithoutScope("restore", `?name=${encodeURIComponent(args.target)}`);
|
|
21652
|
+
await runBackupRestore({
|
|
21653
|
+
name: args.target,
|
|
21654
|
+
password: args.password,
|
|
21655
|
+
scope,
|
|
21656
|
+
confirm: args.confirm === true,
|
|
21657
|
+
json: args.json === true
|
|
21658
|
+
});
|
|
21659
|
+
} else fail(`unknown action "${action}" — use list, create or restore`);
|
|
21660
|
+
} catch (error) {
|
|
21661
|
+
fail(errorMessage(error));
|
|
21662
|
+
}
|
|
21663
|
+
}
|
|
21664
|
+
});
|
|
21665
|
+
}));
|
|
21666
|
+
//#endregion
|
|
20881
21667
|
//#region src/services/nanny.ts
|
|
20882
21668
|
var nanny_exports$1 = /* @__PURE__ */ __exportAll({ runNanny: () => runNanny });
|
|
20883
21669
|
/**
|
|
@@ -21062,6 +21848,7 @@ var SYNOPSIS = {
|
|
|
21062
21848
|
"restart": "home-hosted restart [<id>] [options]",
|
|
21063
21849
|
"status": "home-hosted status [--json]",
|
|
21064
21850
|
"logs": "home-hosted logs [--lines <n>] [--follow] [--json]",
|
|
21851
|
+
"backup": "home-hosted backup [list|create|restore] [options]",
|
|
21065
21852
|
"start": "home-hosted start <id> [--workspace <id>]",
|
|
21066
21853
|
"stop": "home-hosted stop <id> [--workspace <id>]",
|
|
21067
21854
|
"set-password": "home-hosted set-password",
|
|
@@ -21078,6 +21865,7 @@ var SUMMARIES = {
|
|
|
21078
21865
|
"restart": "down then up — or one server, given an id",
|
|
21079
21866
|
"status": "is it running, where, and how to reach it",
|
|
21080
21867
|
"logs": "show the panel's own console output",
|
|
21868
|
+
"backup": "list, create or restore a backup archive",
|
|
21081
21869
|
"start": "start one server, leaving the panel up",
|
|
21082
21870
|
"stop": "stop one server, leaving the panel up",
|
|
21083
21871
|
"set-password": "set the panel password without the API",
|
|
@@ -21114,6 +21902,18 @@ var LOGS_SECTION = {
|
|
|
21114
21902
|
["--json", "print machine-readable JSON (one object per line with --follow)"]
|
|
21115
21903
|
]
|
|
21116
21904
|
};
|
|
21905
|
+
var BACKUP_SECTION = {
|
|
21906
|
+
heading: "Arguments for backup",
|
|
21907
|
+
lines: [
|
|
21908
|
+
["list | create | restore", "what to do (default: list)"],
|
|
21909
|
+
["<name>", "the archive, for restore — `backup list` shows them"],
|
|
21910
|
+
["--include <ids>", "comma-separated item ids to act on (required for create/restore)"],
|
|
21911
|
+
["--all", "act on everything; one of --include or --all is required"],
|
|
21912
|
+
["--password <text>", "encrypt a new archive, or unlock one on restore"],
|
|
21913
|
+
["--confirm", "apply without asking (create and restore plan first)"],
|
|
21914
|
+
["--json", "print the plan or the result as JSON"]
|
|
21915
|
+
]
|
|
21916
|
+
};
|
|
21117
21917
|
var SERVER_SECTION = {
|
|
21118
21918
|
heading: "Arguments for start/stop",
|
|
21119
21919
|
lines: [["<id>", "the server id from servers.config.json"], ["-w, --workspace <id>", "the workspace to act in (default: the default workspace)"]]
|
|
@@ -21257,6 +22057,7 @@ var UP_COMMANDS = /* @__PURE__ */ new Set(["up", "restart"]);
|
|
|
21257
22057
|
var SECTIONS = {
|
|
21258
22058
|
"status": STATUS_SECTION,
|
|
21259
22059
|
"logs": LOGS_SECTION,
|
|
22060
|
+
"backup": BACKUP_SECTION,
|
|
21260
22061
|
"restart": RESTART_SECTION,
|
|
21261
22062
|
"start": SERVER_SECTION,
|
|
21262
22063
|
"stop": SERVER_SECTION,
|
|
@@ -21308,7 +22109,8 @@ var COMMANDS = {
|
|
|
21308
22109
|
"init": () => Promise.resolve().then(() => (init_init(), init_exports)).then((module) => module.initCommand),
|
|
21309
22110
|
"ui-switch": () => Promise.resolve().then(() => (init_ui_switch(), ui_switch_exports)).then((module) => module.uiSwitchCommand),
|
|
21310
22111
|
"ui-update": () => Promise.resolve().then(() => (init_ui_update(), ui_update_exports)).then((module) => module.uiUpdateCommand),
|
|
21311
|
-
"ui-revert": () => Promise.resolve().then(() => (init_ui_revert(), ui_revert_exports)).then((module) => module.uiRevertCommand)
|
|
22112
|
+
"ui-revert": () => Promise.resolve().then(() => (init_ui_revert(), ui_revert_exports)).then((module) => module.uiRevertCommand),
|
|
22113
|
+
"backup": () => Promise.resolve().then(() => (init_backup(), backup_exports)).then((module) => module.backupCommand)
|
|
21312
22114
|
};
|
|
21313
22115
|
var rootCommand = defineCommand({
|
|
21314
22116
|
meta: {
|