@hallohuman/hightea 0.1.17 → 0.1.31
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/LICENSE +5 -0
- package/dist/bin.js +24 -255
- package/dist/chunk-19pwmgzg.js +5 -0
- package/dist/chunk-5s85pw9m.js +13 -0
- package/dist/chunk-5zb06f9b.js +2 -0
- package/dist/chunk-69dva4cx.js +58 -0
- package/dist/chunk-8rbp82rh.js +139 -0
- package/dist/chunk-8x5daqht.js +1 -0
- package/dist/chunk-9pv1nvfv.js +5 -0
- package/dist/chunk-db8mr7mp.js +1 -0
- package/dist/chunk-dkzzzkex.js +13 -0
- package/dist/chunk-f04t1q6v.js +2 -0
- package/dist/chunk-gcqhggbh.js +1 -0
- package/dist/chunk-jdzvh3gk.js +1 -0
- package/dist/chunk-p616vcdm.js +4 -0
- package/dist/chunk-r9yjtfhp.js +5 -0
- package/dist/chunk-xj3c5jgm.js +5 -0
- package/dist/chunk-xy8j3npr.js +1 -0
- package/dist/plugin/plugin.json +8 -1
- package/dist/plugin/plugin.mjs +51 -13812
- package/package.json +9 -5
- package/dist/chunk-0ab6h39c.js +0 -402
- package/dist/chunk-1xykqbdm.js +0 -14
- package/dist/chunk-3ah56pdt.js +0 -7328
- package/dist/chunk-5mezgvms.js +0 -139
- package/dist/chunk-6ae0ftqh.js +0 -347
- package/dist/chunk-d5h204vj.js +0 -14
- package/dist/chunk-h5s3wwdz.js +0 -8043
- package/dist/chunk-hxs6jv2m.js +0 -91
- package/dist/chunk-rs0vvh3x.js +0 -78
- package/dist/chunk-sbqmqp8e.js +0 -123
package/dist/chunk-5mezgvms.js
DELETED
|
@@ -1,139 +0,0 @@
|
|
|
1
|
-
import"./chunk-0ab6h39c.js";
|
|
2
|
-
import {
|
|
3
|
-
ownerIo,
|
|
4
|
-
sendOwner
|
|
5
|
-
} from "./chunk-sbqmqp8e.js";
|
|
6
|
-
|
|
7
|
-
// src/source.ts
|
|
8
|
-
var USAGE = `hightea source bind --origin https://your-deployment --workspace SLUG --source SOURCE --host HOST [--refresh 1h] [--session-file FILE] [--json]
|
|
9
|
-
` + `hightea source unbind --origin https://your-deployment --workspace SLUG --source SOURCE [--session-file FILE] [--json]
|
|
10
|
-
` + `hightea source set-refresh --origin https://your-deployment --workspace SLUG --source SOURCE --every 1h [--session-file FILE] [--json]
|
|
11
|
-
` + "hightea source show --origin https://your-deployment --workspace SLUG --source SOURCE [--session-file FILE] [--json]";
|
|
12
|
-
function parseRefreshInterval(text) {
|
|
13
|
-
const raw = text.trim().toLowerCase();
|
|
14
|
-
if (raw === "")
|
|
15
|
-
return "INVALID";
|
|
16
|
-
if (raw === "none" || raw === "off")
|
|
17
|
-
return null;
|
|
18
|
-
const match = /^(\d+)(s|m|h|d)$/u.exec(raw);
|
|
19
|
-
if (match === null)
|
|
20
|
-
return "INVALID";
|
|
21
|
-
const unit = { s: 1000, m: 60000, h: 3600000, d: 86400000 }[match[2]];
|
|
22
|
-
const ms = Number(match[1]) * unit;
|
|
23
|
-
return Number.isSafeInteger(ms) && ms > 0 ? ms : "INVALID";
|
|
24
|
-
}
|
|
25
|
-
var BOUND_ELSEWHERE = { SOURCE_BINDING_CONFLICT: 4 };
|
|
26
|
-
function defaults() {
|
|
27
|
-
return ownerIo();
|
|
28
|
-
}
|
|
29
|
-
async function send(target, io, request) {
|
|
30
|
-
if (target.workspace.trim().length === 0 || target.source.trim().length === 0) {
|
|
31
|
-
return { exit: 1, error: `SOURCE_INPUT: --workspace and --source are required.
|
|
32
|
-
${USAGE}` };
|
|
33
|
-
}
|
|
34
|
-
return sendOwner(target, io, {
|
|
35
|
-
...request,
|
|
36
|
-
label: "SOURCE",
|
|
37
|
-
extraExits: BOUND_ELSEWHERE,
|
|
38
|
-
path: `/sources/${encodeURIComponent(target.source)}${request.path}`
|
|
39
|
-
});
|
|
40
|
-
}
|
|
41
|
-
function datasetLine(source) {
|
|
42
|
-
const field = (name) => source[name] === null || source[name] === undefined ? "none" : String(source[name]);
|
|
43
|
-
const listing = source.listing === true ? ", first list running" : source.listing === false ? ", no list running" : "";
|
|
44
|
-
return `${field("status")} on ${field("hostId")} as dataset ${field("dataset")} (freshness ${field("freshness")}${listing})`;
|
|
45
|
-
}
|
|
46
|
-
async function sourceBind(target, io = defaults()) {
|
|
47
|
-
if (target.host.trim().length === 0) {
|
|
48
|
-
io.err(`SOURCE_INPUT: --host is required (the host id that serves the source).
|
|
49
|
-
${USAGE}`);
|
|
50
|
-
return 1;
|
|
51
|
-
}
|
|
52
|
-
const refreshMs = target.refresh === undefined ? undefined : parseRefreshInterval(target.refresh);
|
|
53
|
-
if (refreshMs === "INVALID" || refreshMs === null) {
|
|
54
|
-
io.err(`SOURCE_INPUT: --refresh takes a whole count and one unit — 30m, 1h, 2d — and a bind never clears a schedule.
|
|
55
|
-
${USAGE}`);
|
|
56
|
-
return 1;
|
|
57
|
-
}
|
|
58
|
-
const sent = await send(target, io, {
|
|
59
|
-
method: "POST",
|
|
60
|
-
path: "/bind",
|
|
61
|
-
body: refreshMs === undefined ? { hostId: target.host.trim() } : { hostId: target.host.trim(), refreshMs },
|
|
62
|
-
what: "bind"
|
|
63
|
-
});
|
|
64
|
-
if ("error" in sent) {
|
|
65
|
-
io.err(sent.error);
|
|
66
|
-
return sent.exit;
|
|
67
|
-
}
|
|
68
|
-
if (target.json) {
|
|
69
|
-
io.out(JSON.stringify(sent.answer));
|
|
70
|
-
return 0;
|
|
71
|
-
}
|
|
72
|
-
const source = sent.answer.source ?? {};
|
|
73
|
-
io.out(`bound ${String(source.sourceId ?? target.source)} to ${String(source.hostId ?? target.host)}: ${datasetLine(source)}`);
|
|
74
|
-
io.out("The source's first list fills its dataset; run `hightea source show` to watch its freshness.");
|
|
75
|
-
return 0;
|
|
76
|
-
}
|
|
77
|
-
async function sourceUnbind(target, io = defaults()) {
|
|
78
|
-
const sent = await send(target, io, { method: "POST", path: "/unbind", what: "unbind" });
|
|
79
|
-
if ("error" in sent) {
|
|
80
|
-
io.err(sent.error);
|
|
81
|
-
return sent.exit;
|
|
82
|
-
}
|
|
83
|
-
if (target.json) {
|
|
84
|
-
io.out(JSON.stringify(sent.answer));
|
|
85
|
-
return 0;
|
|
86
|
-
}
|
|
87
|
-
const source = sent.answer.source ?? {};
|
|
88
|
-
io.out(`unbound ${String(source.sourceId ?? target.source)} from ${String(source.hostId ?? "its host")}: ${datasetLine(source)}`);
|
|
89
|
-
io.out("Rows kept; rebind refreshes them.");
|
|
90
|
-
return 0;
|
|
91
|
-
}
|
|
92
|
-
async function sourceShow(target, io = defaults()) {
|
|
93
|
-
const sent = await send(target, io, { method: "GET", path: "", what: "read" });
|
|
94
|
-
if ("error" in sent) {
|
|
95
|
-
io.err(sent.error);
|
|
96
|
-
return sent.exit;
|
|
97
|
-
}
|
|
98
|
-
if (target.json) {
|
|
99
|
-
io.out(JSON.stringify(sent.answer));
|
|
100
|
-
return 0;
|
|
101
|
-
}
|
|
102
|
-
const source = sent.answer.source ?? {};
|
|
103
|
-
const position = source.position;
|
|
104
|
-
io.out(`${String(source.sourceId ?? target.source)}: ${datasetLine(source)}`);
|
|
105
|
-
io.out(`position ${position === null || position === undefined ? "none" : `${String(position.epoch)}/${String(position.ordinal)}`}, ` + `last error ${source.lastError === null || source.lastError === undefined ? "none" : String(source.lastError)}, ` + `dropped ${String(source.dropped ?? 0)}, duplicates ${String(source.duplicates ?? 0)}`);
|
|
106
|
-
return 0;
|
|
107
|
-
}
|
|
108
|
-
async function sourceSetRefresh(target, io = defaults()) {
|
|
109
|
-
if (target.every.trim().length === 0) {
|
|
110
|
-
io.err(`SOURCE_INPUT: --every is required (1h, 30m, 2d, or none).
|
|
111
|
-
${USAGE}`);
|
|
112
|
-
return 1;
|
|
113
|
-
}
|
|
114
|
-
const everyMs = parseRefreshInterval(target.every);
|
|
115
|
-
if (everyMs === "INVALID") {
|
|
116
|
-
io.err(`SOURCE_INPUT: --every takes a whole count and one unit — 45s, 30m, 1h, 2d — or none.
|
|
117
|
-
${USAGE}`);
|
|
118
|
-
return 1;
|
|
119
|
-
}
|
|
120
|
-
const sent = await send(target, io, { method: "POST", path: "/schedule", body: { everyMs }, what: "set its refresh interval" });
|
|
121
|
-
if ("error" in sent) {
|
|
122
|
-
io.err(sent.error);
|
|
123
|
-
return sent.exit;
|
|
124
|
-
}
|
|
125
|
-
if (target.json) {
|
|
126
|
-
io.out(JSON.stringify(sent.answer));
|
|
127
|
-
return 0;
|
|
128
|
-
}
|
|
129
|
-
const source = sent.answer.source ?? {};
|
|
130
|
-
const declared = source.reconcileMs === null || source.reconcileMs === undefined ? "no schedule (refreshes only when the host reports a gap)" : `refresh every ${Math.round(Number(source.reconcileMs) / 60000)} min`;
|
|
131
|
-
io.out(`${String(source.sourceId ?? target.source)}: ${declared}; ${datasetLine(source)}`);
|
|
132
|
-
return 0;
|
|
133
|
-
}
|
|
134
|
-
export {
|
|
135
|
-
sourceBind,
|
|
136
|
-
sourceSetRefresh,
|
|
137
|
-
sourceShow,
|
|
138
|
-
sourceUnbind
|
|
139
|
-
};
|
package/dist/chunk-6ae0ftqh.js
DELETED
|
@@ -1,347 +0,0 @@
|
|
|
1
|
-
import"./chunk-0ab6h39c.js";
|
|
2
|
-
import {
|
|
3
|
-
ownerIo,
|
|
4
|
-
sendOwner
|
|
5
|
-
} from "./chunk-sbqmqp8e.js";
|
|
6
|
-
|
|
7
|
-
// src/service.ts
|
|
8
|
-
var USAGE = `hightea service list (--host HOST --origin URL --workspace SLUG | --local [--host HOST]) [--json]
|
|
9
|
-
` + `hightea service add|change NAME [--command CMD] [--cwd DIR] [--patch JSON] (--host … | --local)
|
|
10
|
-
` + `hightea service remove|start|stop|restart NAME (--host … | --local)
|
|
11
|
-
` + `hightea service logs NAME [--limit N] [--end N] (--host … | --local)
|
|
12
|
-
` + `hightea service acknowledge (--host … | --local)
|
|
13
|
-
` + `hightea service backup --host HOST --origin URL --workspace SLUG [--json]
|
|
14
|
-
` + `hightea service restore [NAME...] --from OLD_HOST (--host NEW_HOST | --local) --origin URL --workspace SLUG [--yes] [--json]
|
|
15
|
-
` + "hightea service enable|disable [--host HOST] (run on the machine)";
|
|
16
|
-
var asAnswer = (value) => typeof value === "object" && value !== null && !Array.isArray(value) && typeof value.ok === "boolean" ? value : undefined;
|
|
17
|
-
function remoteDriver(target, io) {
|
|
18
|
-
const host = encodeURIComponent((target.host ?? "").trim());
|
|
19
|
-
const send = async (method, path, what, body) => {
|
|
20
|
-
const sent = await sendOwner(target, io, { method, path: `/hosts/${host}/services${path}`, what, label: "SERVICE", ...body === undefined ? {} : { body } });
|
|
21
|
-
if ("error" in sent) {
|
|
22
|
-
const answer = asAnswer(sent.body?.answer);
|
|
23
|
-
return answer === undefined ? sent : { answer };
|
|
24
|
-
}
|
|
25
|
-
const answer = asAnswer(sent.answer.answer);
|
|
26
|
-
return answer === undefined ? { exit: 1, error: `SERVICE_ANSWER_INVALID: ${what} answered without the host's answer: ${JSON.stringify(sent.answer).slice(0, 300)}` } : { answer };
|
|
27
|
-
};
|
|
28
|
-
const name = (value) => `/${encodeURIComponent(value)}`;
|
|
29
|
-
return {
|
|
30
|
-
machine: (target.host ?? "").trim(),
|
|
31
|
-
list: () => send("GET", "", "list"),
|
|
32
|
-
put: (params) => send("PUT", name(String(params.service.name)), "put", params),
|
|
33
|
-
remove: (params) => send("DELETE", name(params.name), "remove", { ifVersion: params.ifVersion }),
|
|
34
|
-
control: (service, verb) => send("POST", `${name(service)}/${verb}`, verb),
|
|
35
|
-
logs: (params) => {
|
|
36
|
-
const query = new URLSearchParams;
|
|
37
|
-
if (params.end !== undefined)
|
|
38
|
-
query.set("end", String(params.end));
|
|
39
|
-
if (params.limit !== undefined)
|
|
40
|
-
query.set("limit", String(params.limit));
|
|
41
|
-
const suffix = query.size === 0 ? "" : `?${query}`;
|
|
42
|
-
return send("GET", `${name(params.name)}/logs${suffix}`, "logs");
|
|
43
|
-
},
|
|
44
|
-
acknowledge: (params) => send("POST", "/acknowledge", "acknowledge", params)
|
|
45
|
-
};
|
|
46
|
-
}
|
|
47
|
-
async function localDriver(target) {
|
|
48
|
-
const { localHost } = await import("./chunk-1xykqbdm.js");
|
|
49
|
-
const found = await localHost(target.host);
|
|
50
|
-
if ("error" in found)
|
|
51
|
-
return found;
|
|
52
|
-
const { localServices, LOG_LINES_MAX } = await import("@hallohuman/hightea-services");
|
|
53
|
-
const local = localServices(found.configDir);
|
|
54
|
-
if (typeof local === "string")
|
|
55
|
-
return { error: `SERVICE_NOT_CONFIGURED: ${local}` };
|
|
56
|
-
const reached = (answer) => ({ answer });
|
|
57
|
-
return {
|
|
58
|
-
machine: local.machine.name,
|
|
59
|
-
list: async () => reached(local.list()),
|
|
60
|
-
put: async (params) => reached(await local.put(params)),
|
|
61
|
-
remove: async (params) => reached(await local.remove(params)),
|
|
62
|
-
control: async (name, verb) => reached(await local.control(name, verb)),
|
|
63
|
-
logs: async (params) => reached(await local.logs({
|
|
64
|
-
name: params.name,
|
|
65
|
-
endOffset: params.end ?? 0,
|
|
66
|
-
limit: Math.min(params.limit ?? 100, LOG_LINES_MAX)
|
|
67
|
-
})),
|
|
68
|
-
acknowledge: async (params) => reached(await local.acknowledge(params))
|
|
69
|
-
};
|
|
70
|
-
}
|
|
71
|
-
async function serviceCommand(target, input, io = ownerIo(), drivers = {}) {
|
|
72
|
-
const remote = !target.local;
|
|
73
|
-
if (remote && ((target.host ?? "").trim() === "" || target.workspace.trim() === "")) {
|
|
74
|
-
io.err(`SERVICE_INPUT: name the host with --host and --workspace, or act on this machine with --local.
|
|
75
|
-
${USAGE}`);
|
|
76
|
-
return 1;
|
|
77
|
-
}
|
|
78
|
-
const needsName = input.command !== "list" && input.command !== "acknowledge";
|
|
79
|
-
const name = input.name?.trim() ?? "";
|
|
80
|
-
if (needsName && name === "") {
|
|
81
|
-
io.err(`SERVICE_INPUT: ${input.command} names one service.
|
|
82
|
-
${USAGE}`);
|
|
83
|
-
return 1;
|
|
84
|
-
}
|
|
85
|
-
const driver = remote ? remoteDriver(target, io) : await (drivers.local ?? localDriver)(target);
|
|
86
|
-
if ("error" in driver) {
|
|
87
|
-
io.err(driver.error);
|
|
88
|
-
return 1;
|
|
89
|
-
}
|
|
90
|
-
const done = (reached, print) => {
|
|
91
|
-
if ("error" in reached) {
|
|
92
|
-
io.err(reached.error);
|
|
93
|
-
return reached.exit;
|
|
94
|
-
}
|
|
95
|
-
const answer = reached.answer;
|
|
96
|
-
if (!answer.ok) {
|
|
97
|
-
io.err(`${String(answer.code ?? "SERVICE_REFUSED")}: ${String(answer.message ?? "the machine refused")}`);
|
|
98
|
-
if (typeof answer.diff === "string" && answer.diff !== "")
|
|
99
|
-
io.err(answer.diff);
|
|
100
|
-
if (target.json)
|
|
101
|
-
io.out(JSON.stringify(answer));
|
|
102
|
-
return 1;
|
|
103
|
-
}
|
|
104
|
-
if (target.json)
|
|
105
|
-
io.out(JSON.stringify(answer));
|
|
106
|
-
else
|
|
107
|
-
print(answer);
|
|
108
|
-
return 0;
|
|
109
|
-
};
|
|
110
|
-
switch (input.command) {
|
|
111
|
-
case "list":
|
|
112
|
-
return done(await driver.list(), (answer) => io.out(listing(answer, driver.machine)));
|
|
113
|
-
case "start":
|
|
114
|
-
case "stop":
|
|
115
|
-
case "restart":
|
|
116
|
-
return done(await driver.control(name, input.command), () => io.out(`${input.command}: ${name} on ${driver.machine}`));
|
|
117
|
-
case "logs": {
|
|
118
|
-
const end = wholeNumber(input.end, 0), limit = wholeNumber(input.limit, 1);
|
|
119
|
-
if (end === null || limit === null) {
|
|
120
|
-
io.err("SERVICE_INPUT: --end and --limit are whole numbers of lines; --limit is at least 1");
|
|
121
|
-
return 1;
|
|
122
|
-
}
|
|
123
|
-
const reached = await driver.logs({ name, ...end === undefined ? {} : { end }, ...limit === undefined ? {} : { limit } });
|
|
124
|
-
return done(reached, (answer) => {
|
|
125
|
-
io.out(`raw output from ${name} on ${driver.machine}:`);
|
|
126
|
-
for (const line of Array.isArray(answer.lines) ? answer.lines : [])
|
|
127
|
-
io.out(String(line));
|
|
128
|
-
});
|
|
129
|
-
}
|
|
130
|
-
case "acknowledge":
|
|
131
|
-
case "add":
|
|
132
|
-
case "change":
|
|
133
|
-
case "remove": {
|
|
134
|
-
const listed = await driver.list();
|
|
135
|
-
if ("error" in listed) {
|
|
136
|
-
io.err(listed.error);
|
|
137
|
-
return listed.exit;
|
|
138
|
-
}
|
|
139
|
-
if (!listed.answer.ok)
|
|
140
|
-
return done(listed, () => {});
|
|
141
|
-
const ifVersion = String(listed.answer.ifVersion ?? "");
|
|
142
|
-
const services = Array.isArray(listed.answer.services) ? listed.answer.services : [];
|
|
143
|
-
const exists = services.some((service) => service.name === name);
|
|
144
|
-
if (input.command === "acknowledge") {
|
|
145
|
-
const hashes = listed.answer.hashes ?? {};
|
|
146
|
-
return done(await driver.acknowledge({ hashes }), () => io.out(`acknowledged ${Object.keys(hashes).join(", ") || "no files"} on ${driver.machine} as they are now`));
|
|
147
|
-
}
|
|
148
|
-
if (input.command === "remove") {
|
|
149
|
-
return done(await driver.remove({ name, ifVersion }), () => io.out(`removed ${name} on ${driver.machine}`));
|
|
150
|
-
}
|
|
151
|
-
if (input.command === "add" && exists) {
|
|
152
|
-
io.err(`SERVICE_EXISTS: ${name} is already a service on ${driver.machine}; use \`hightea service change\``);
|
|
153
|
-
return 1;
|
|
154
|
-
}
|
|
155
|
-
if (input.command === "change" && !exists) {
|
|
156
|
-
io.err(`SERVICE_NOT_FOUND: ${name} is not a service on ${driver.machine}; use \`hightea service add\``);
|
|
157
|
-
return 1;
|
|
158
|
-
}
|
|
159
|
-
const service = patchOf(name, input.fields ?? {});
|
|
160
|
-
if (typeof service === "string") {
|
|
161
|
-
io.err(`SERVICE_INPUT: ${service}`);
|
|
162
|
-
return 1;
|
|
163
|
-
}
|
|
164
|
-
if (input.command === "add" && service.command === undefined) {
|
|
165
|
-
io.err("SERVICE_INPUT: a new service needs --command");
|
|
166
|
-
return 1;
|
|
167
|
-
}
|
|
168
|
-
return done(await driver.put({ service, ifVersion }), (answer) => io.out(answer.changed === false ? `${name} on ${driver.machine} already reads so; nothing was written` : `${input.command === "add" ? "added" : "changed"} ${name} on ${driver.machine}`));
|
|
169
|
-
}
|
|
170
|
-
}
|
|
171
|
-
}
|
|
172
|
-
function patchOf(name, fields) {
|
|
173
|
-
let extra = {};
|
|
174
|
-
if (fields.patch !== undefined) {
|
|
175
|
-
try {
|
|
176
|
-
extra = JSON.parse(fields.patch);
|
|
177
|
-
} catch {
|
|
178
|
-
return "--patch is a JSON object of service fields";
|
|
179
|
-
}
|
|
180
|
-
if (typeof extra !== "object" || extra === null || Array.isArray(extra))
|
|
181
|
-
return "--patch is a JSON object of service fields";
|
|
182
|
-
if (extra.name !== undefined && extra.name !== name)
|
|
183
|
-
return "--patch names another service";
|
|
184
|
-
}
|
|
185
|
-
return {
|
|
186
|
-
...extra,
|
|
187
|
-
name,
|
|
188
|
-
...fields.command === undefined ? {} : { command: fields.command },
|
|
189
|
-
...fields.cwd === undefined ? {} : { cwd: fields.cwd }
|
|
190
|
-
};
|
|
191
|
-
}
|
|
192
|
-
function wholeNumber(raw, least) {
|
|
193
|
-
if (raw === undefined)
|
|
194
|
-
return;
|
|
195
|
-
return /^\d{1,7}$/u.test(raw.trim()) && Number(raw) >= least ? Number(raw) : null;
|
|
196
|
-
}
|
|
197
|
-
function listing(answer, machine) {
|
|
198
|
-
const services = Array.isArray(answer.services) ? answer.services : [];
|
|
199
|
-
const readOnly = Array.isArray(answer.readOnly) ? answer.readOnly : [];
|
|
200
|
-
const lines = services.map((service) => {
|
|
201
|
-
const plain = Array.isArray(service.plainEnv) && service.plainEnv.length > 0 ? ` (plain env: ${service.plainEnv.join("; ")})` : "";
|
|
202
|
-
return `${String(service.name)} ${String(service.command ?? "")}${plain}`;
|
|
203
|
-
});
|
|
204
|
-
for (const entry of readOnly)
|
|
205
|
-
lines.push(`${String(entry.name)} read-only: ${String(entry.reason)}`);
|
|
206
|
-
if (lines.length === 0)
|
|
207
|
-
return `no services on ${machine} (${String(answer.file ?? "its file")})`;
|
|
208
|
-
return lines.join(`
|
|
209
|
-
`);
|
|
210
|
-
}
|
|
211
|
-
var isObject = (value) => typeof value === "object" && value !== null && !Array.isArray(value);
|
|
212
|
-
async function readBackup(target, io, host) {
|
|
213
|
-
const sent = await sendOwner(target, io, { method: "GET", path: `/hosts/${encodeURIComponent(host)}/services/backup`, what: "backup", label: "SERVICE" });
|
|
214
|
-
if ("error" in sent)
|
|
215
|
-
return sent;
|
|
216
|
-
const backup = sent.answer.backup;
|
|
217
|
-
if (!isObject(backup) || typeof backup.label !== "string" || !isObject(backup.listing) || !Array.isArray(backup.lines)) {
|
|
218
|
-
return { exit: 1, error: `SERVICE_ANSWER_INVALID: backup answered without a backup: ${JSON.stringify(sent.answer).slice(0, 300)}` };
|
|
219
|
-
}
|
|
220
|
-
return { backup };
|
|
221
|
-
}
|
|
222
|
-
function describeBackupLine(line) {
|
|
223
|
-
if (line.unreadable === true)
|
|
224
|
-
return `unreadable line at ${String(line.offset)} (${String(line.bytes)} bytes)`;
|
|
225
|
-
if (line.boundary === true)
|
|
226
|
-
return `--- ${String(line.message)} ---`;
|
|
227
|
-
const refused = isObject(line.refused) ? ` refused ${String(line.refused.code)}` : "";
|
|
228
|
-
const name = typeof line.name === "string" && line.name !== "" ? ` ${line.name}` : "";
|
|
229
|
-
return `${String(line.at ?? "")} ${String(line.principal ?? "")} ${String(line.op ?? "")}${name}${refused}`;
|
|
230
|
-
}
|
|
231
|
-
async function serviceBackupCommand(target, io = ownerIo()) {
|
|
232
|
-
const host = (target.host ?? "").trim();
|
|
233
|
-
if (host === "" || target.workspace.trim() === "" || target.local) {
|
|
234
|
-
io.err(`SERVICE_INPUT: name the host with --host and --workspace; a backup is read from Hightea, never --local.
|
|
235
|
-
${USAGE}`);
|
|
236
|
-
return 1;
|
|
237
|
-
}
|
|
238
|
-
const read = await readBackup(target, io, host);
|
|
239
|
-
if ("error" in read) {
|
|
240
|
-
io.err(read.error);
|
|
241
|
-
return read.exit;
|
|
242
|
-
}
|
|
243
|
-
const { backup } = read;
|
|
244
|
-
if (target.json) {
|
|
245
|
-
io.out(JSON.stringify(backup));
|
|
246
|
-
return 0;
|
|
247
|
-
}
|
|
248
|
-
const state = backup.hostStatus === "revoked" ? `revoked ${backup.revokedAt ?? ""}`.trim() : backup.connected ? "connected" : "offline";
|
|
249
|
-
io.out(`${host} (${state}): ${backup.label}`);
|
|
250
|
-
if (typeof backup.reason === "string")
|
|
251
|
-
io.out(`last pull: ${backup.reason}`);
|
|
252
|
-
io.out(listing({ ok: true, ...backup.listing }, host));
|
|
253
|
-
io.out(backup.lines.length === 0 ? "no changes backed up" : `changes, oldest first (${backup.lines.length}):`);
|
|
254
|
-
for (const { line } of backup.lines)
|
|
255
|
-
io.out(` ${describeBackupLine(line)}`);
|
|
256
|
-
return 0;
|
|
257
|
-
}
|
|
258
|
-
function restoreIo() {
|
|
259
|
-
return {
|
|
260
|
-
...ownerIo(),
|
|
261
|
-
confirm: async (question) => {
|
|
262
|
-
const { createInterface } = await import("node:readline/promises");
|
|
263
|
-
const prompt = createInterface({ input: process.stdin, output: process.stderr });
|
|
264
|
-
try {
|
|
265
|
-
return /^y(es)?$/iu.test((await prompt.question(question)).trim());
|
|
266
|
-
} finally {
|
|
267
|
-
prompt.close();
|
|
268
|
-
}
|
|
269
|
-
}
|
|
270
|
-
};
|
|
271
|
-
}
|
|
272
|
-
async function serviceRestore(target, input, io = restoreIo(), drivers = {}) {
|
|
273
|
-
const from = input.from.trim();
|
|
274
|
-
const to = (target.host ?? "").trim();
|
|
275
|
-
if (from === "" || target.workspace.trim() === "" || !target.local && to === "") {
|
|
276
|
-
io.err(`SERVICE_INPUT: restore names the old host with --from, the workspace with --workspace, and the new host with --host or --local.
|
|
277
|
-
${USAGE}`);
|
|
278
|
-
return 1;
|
|
279
|
-
}
|
|
280
|
-
const read = await readBackup(target, io, from);
|
|
281
|
-
if ("error" in read) {
|
|
282
|
-
io.err(read.error);
|
|
283
|
-
return read.exit;
|
|
284
|
-
}
|
|
285
|
-
const { backup } = read;
|
|
286
|
-
const all = Array.isArray(backup.listing.services) ? backup.listing.services.filter(isObject) : [];
|
|
287
|
-
const unknown = input.names.filter((name) => !all.some((service) => service.name === name));
|
|
288
|
-
if (unknown.length > 0) {
|
|
289
|
-
io.err(`SERVICE_NOT_IN_BACKUP: ${unknown.join(", ")} ${unknown.length === 1 ? "is" : "are"} not in ${from}'s backup (${backup.label}); it holds ${all.map((service) => String(service.name)).join(", ") || "no services"}`);
|
|
290
|
-
return 1;
|
|
291
|
-
}
|
|
292
|
-
const chosen = input.names.length === 0 ? all : all.filter((service) => input.names.includes(String(service.name)));
|
|
293
|
-
if (chosen.length === 0) {
|
|
294
|
-
io.err(`SERVICE_NOTHING_TO_RESTORE: ${from}'s backup (${backup.label}) holds no services`);
|
|
295
|
-
return 1;
|
|
296
|
-
}
|
|
297
|
-
const driver = target.local ? await (drivers.local ?? localDriver)(target) : remoteDriver(target, io);
|
|
298
|
-
if ("error" in driver) {
|
|
299
|
-
io.err(driver.error);
|
|
300
|
-
return 1;
|
|
301
|
-
}
|
|
302
|
-
const secretsOf = (service) => isObject(service.env) ? Object.entries(service.env).map(([key, value]) => `${key} (${String(isObject(value) ? value.$secret : value)})`) : [];
|
|
303
|
-
const plainOf = (service) => Array.isArray(service.plainEnv) ? service.plainEnv.map(String) : [];
|
|
304
|
-
io.err(`restore from ${from} (${backup.label}) to ${driver.machine}, create only:`);
|
|
305
|
-
for (const service of chosen) {
|
|
306
|
-
io.err(` ${String(service.name)} ${String(service.command ?? "")}`);
|
|
307
|
-
const secrets = secretsOf(service);
|
|
308
|
-
if (secrets.length > 0)
|
|
309
|
-
io.err(` secrets to enter again on ${driver.machine}: ${secrets.join(", ")}`);
|
|
310
|
-
}
|
|
311
|
-
if (!input.yes && !await io.confirm(`create ${chosen.length} service${chosen.length === 1 ? "" : "s"} on ${driver.machine}? [y/N] `)) {
|
|
312
|
-
io.err("SERVICE_RESTORE_DECLINED: nothing was sent");
|
|
313
|
-
return 1;
|
|
314
|
-
}
|
|
315
|
-
const results = [];
|
|
316
|
-
for (const service of chosen) {
|
|
317
|
-
const { plainEnv: _keysOnly, ...record } = service;
|
|
318
|
-
const name = String(service.name);
|
|
319
|
-
const secrets = secretsOf(service), plainEnv = plainOf(service);
|
|
320
|
-
const reached = await driver.put({ service: record, ifVersion: "absent" });
|
|
321
|
-
if ("error" in reached) {
|
|
322
|
-
const [code = "SERVICE_FAILED", ...rest] = reached.error.split(": ");
|
|
323
|
-
results.push({ name, outcome: "failed", code, message: rest.join(": "), secrets, plainEnv });
|
|
324
|
-
} else if (!reached.answer.ok) {
|
|
325
|
-
results.push({ name, outcome: "refused", code: String(reached.answer.code ?? "SERVICE_REFUSED"), message: String(reached.answer.message ?? ""), secrets, plainEnv });
|
|
326
|
-
} else {
|
|
327
|
-
results.push({ name, outcome: "created", secrets, plainEnv });
|
|
328
|
-
}
|
|
329
|
-
}
|
|
330
|
-
if (target.json)
|
|
331
|
-
io.out(JSON.stringify({ from, to: driver.machine, label: backup.label, results }));
|
|
332
|
-
else {
|
|
333
|
-
for (const result of results) {
|
|
334
|
-
io.out(result.outcome === "created" ? `created ${result.name}` : `${result.outcome} ${result.name}: ${result.code}: ${result.message}`);
|
|
335
|
-
if (result.outcome === "created" && result.secrets.length > 0)
|
|
336
|
-
io.out(` secrets to enter again: ${result.secrets.join(", ")}`);
|
|
337
|
-
if (result.outcome === "created" && result.plainEnv.length > 0)
|
|
338
|
-
io.out(` plain values never backed up: ${result.plainEnv.join("; ")}`);
|
|
339
|
-
}
|
|
340
|
-
}
|
|
341
|
-
return results.every((result) => result.outcome === "created") ? 0 : 1;
|
|
342
|
-
}
|
|
343
|
-
export {
|
|
344
|
-
serviceBackupCommand,
|
|
345
|
-
serviceCommand,
|
|
346
|
-
serviceRestore
|
|
347
|
-
};
|
package/dist/chunk-d5h204vj.js
DELETED
|
@@ -1,14 +0,0 @@
|
|
|
1
|
-
import {
|
|
2
|
-
checkedOrigin2,
|
|
3
|
-
ensureCredential2,
|
|
4
|
-
authLogin2,
|
|
5
|
-
authStatus2,
|
|
6
|
-
authLogout2
|
|
7
|
-
} from "./chunk-0ab6h39c.js";
|
|
8
|
-
export {
|
|
9
|
-
authLogin2 as authLogin,
|
|
10
|
-
authLogout2 as authLogout,
|
|
11
|
-
authStatus2 as authStatus,
|
|
12
|
-
checkedOrigin2 as checkedOrigin,
|
|
13
|
-
ensureCredential2 as ensureCredential
|
|
14
|
-
};
|