@yunuseon/kayra 0.16.1 → 0.17.0
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/README.md +4 -0
- package/cli/dist/browser/assets/index-9a-SE2rU.js +7 -0
- package/cli/dist/browser/assets/{index-3W_NFATj.css → index-Ct0Eudk4.css} +1 -1
- package/cli/dist/browser/assets/xterm-CerOhKYV.js +16 -0
- package/cli/dist/browser/index.html +2 -2
- package/cli/dist/clients/api/api.js +269 -0
- package/cli/dist/{web → clients/api}/auth.js +9 -9
- package/cli/dist/clients/api/commands.js +27 -0
- package/cli/dist/clients/api/contract.gen.js +1 -0
- package/cli/dist/clients/api/contract.js +89 -0
- package/cli/dist/clients/api/directories.js +20 -0
- package/cli/dist/clients/api/discovery.js +38 -0
- package/cli/dist/clients/api/protocol.js +61 -0
- package/cli/dist/clients/api/queries.js +20 -0
- package/cli/dist/{web → clients/api}/runs.js +4 -2
- package/cli/dist/{ui → clients/cli}/explain.js +5 -5
- package/cli/dist/clients/cli/framed.js +64 -0
- package/cli/dist/clients/cli/invocation.js +83 -0
- package/cli/dist/clients/cli/paint.js +23 -0
- package/cli/dist/{cli.js → clients/cli/parser.js} +31 -25
- package/cli/dist/clients/cli/questions.js +88 -0
- package/cli/dist/{ui → clients/cli}/terminal.js +1 -13
- package/cli/dist/clients/ui/assets.js +36 -0
- package/cli/dist/{brand.js → core/brand.js} +1 -1
- package/cli/dist/{commands → core/commands}/app-check.js +1 -1
- package/cli/dist/{commands → core/commands}/server.js +26 -17
- package/cli/dist/{commands → core/commands}/status.js +3 -3
- package/cli/dist/{commands → core/commands}/validate.js +1 -1
- package/cli/dist/{fleet.js → core/fleet.js} +1 -1
- package/cli/dist/core/interpret.js +276 -0
- package/cli/dist/{load.js → core/load.js} +1 -1
- package/cli/dist/core/operations/catalog.js +152 -0
- package/cli/dist/core/operations/run.js +139 -0
- package/cli/dist/{plan.js → core/plan.js} +11 -4
- package/cli/dist/core/present/line.js +17 -0
- package/cli/dist/{ui → core/present}/progress.js +2 -61
- package/cli/dist/{schemas.js → core/schemas.js} +1 -1
- package/cli/dist/core/updates.js +13 -0
- package/cli/dist/{validate.js → core/validate.js} +1 -1
- package/cli/dist/main.js +90 -91
- package/cli/dist/serve.js +77 -0
- package/package.json +3 -2
- package/scripts/agent/guard.sh +9 -2
- package/scripts/local/packed-runs.sh +20 -5
- package/scripts/local/setup-hosts.sh +9 -2
- package/cli/dist/browser/assets/index-DRFX9Q77.js +0 -2
- package/cli/dist/commands/index.js +0 -52
- package/cli/dist/interpret.js +0 -141
- package/cli/dist/ui/line.js +0 -39
- package/cli/dist/web/commands.js +0 -102
- package/cli/dist/web/contract.js +0 -7
- package/cli/dist/web/server.js +0 -336
- /package/cli/dist/{web → clients/ui}/opener.js +0 -0
- /package/cli/dist/{commands → core/commands}/doctor.js +0 -0
- /package/cli/dist/{gather.js → core/gather.js} +0 -0
- /package/cli/dist/{net.js → core/net.js} +0 -0
- /package/cli/dist/{ui → core/present}/text.js +0 -0
- /package/cli/dist/{ui → core/present}/views.js +0 -0
- /package/cli/dist/{settings.js → core/settings.js} +0 -0
|
@@ -1,7 +1,6 @@
|
|
|
1
1
|
import { commandOf, kindOf, titleOf } from "../plan.js";
|
|
2
|
-
import {
|
|
3
|
-
import {
|
|
4
|
-
import { duration, spread, truncate } from "./text.js";
|
|
2
|
+
import { active, bad, muted, ok, strong, warn } from "./line.js";
|
|
3
|
+
import { duration } from "./text.js";
|
|
5
4
|
/** Nothing around the steps: for commands whose output is the point (ssh, logs), and for tests. */
|
|
6
5
|
export const silent = {
|
|
7
6
|
plan: (_plan, work) => work(),
|
|
@@ -14,8 +13,6 @@ export const position = (plan, index) => {
|
|
|
14
13
|
const opens = part !== undefined && parts(plan).length > 1 && plan[index - 1]?.part !== part;
|
|
15
14
|
return { index, total: plan.length, ...(opens ? { opens: part } : {}) };
|
|
16
15
|
};
|
|
17
|
-
/** A run of this long earns a notification when it ends: the operator has looked away. */
|
|
18
|
-
const worthNotifying = 30_000;
|
|
19
16
|
/** "8 steps", or "15 checks" for a plan of nothing else (doctor). */
|
|
20
17
|
export const steps = (plan) => {
|
|
21
18
|
const noun = plan.every((step) => step.kind === "check") ? "check" : "step";
|
|
@@ -42,62 +39,6 @@ export const footer = (invocation, plan, ended, ms) => {
|
|
|
42
39
|
...(alone ? [[" ", active("→"), " ", strong(`kayra ${part} ${invocation.server ?? ""}`), muted(" runs just this part")]] : []),
|
|
43
40
|
];
|
|
44
41
|
};
|
|
45
|
-
/** Frames on stderr: a heading, each step as it runs and how it went, a footer; on a terminal also spinners, the window title, a notification. */
|
|
46
|
-
export const framed = (screen, write, invocation) => {
|
|
47
|
-
const columns = Math.min(screen.columns, 80);
|
|
48
|
-
const say = (text) => {
|
|
49
|
-
write(`${paint(text, screen.colors)}\n`);
|
|
50
|
-
};
|
|
51
|
-
const tell = (raw, when) => {
|
|
52
|
-
if (when)
|
|
53
|
-
write(raw);
|
|
54
|
-
};
|
|
55
|
-
return {
|
|
56
|
-
plan: async (plan, work) => {
|
|
57
|
-
const started = performance.now();
|
|
58
|
-
tell(escapes.pushTitle + escapes.title(invocation.command), screen.title);
|
|
59
|
-
say([strong(invocation.command), muted([...invocation.about, steps(plan)].map((each) => ` · ${each}`).join(""))]);
|
|
60
|
-
say("");
|
|
61
|
-
const ended = await work();
|
|
62
|
-
const ms = performance.now() - started;
|
|
63
|
-
say("");
|
|
64
|
-
footer(invocation, plan, ended, ms).forEach(say);
|
|
65
|
-
tell(escapes.popTitle, screen.title);
|
|
66
|
-
tell(escapes.progressDone, screen.progressBar);
|
|
67
|
-
const failed = ended.failures > 0 || ended.stop?.why === "failed";
|
|
68
|
-
tell(escapes.notify(`${invocation.command} ${failed ? "failed" : "is done"} after ${duration(ms)}`), screen.notifications && ms >= worthNotifying);
|
|
69
|
-
return ended;
|
|
70
|
-
},
|
|
71
|
-
step: async (step, at, work) => {
|
|
72
|
-
if (step.kind === "confirm" || step.kind === "note")
|
|
73
|
-
return work();
|
|
74
|
-
if (at.opens !== undefined) {
|
|
75
|
-
if (at.index > 0)
|
|
76
|
-
say("");
|
|
77
|
-
say([accent(at.opens)]);
|
|
78
|
-
}
|
|
79
|
-
const title = titleOf(step);
|
|
80
|
-
const counter = `${String(at.index + 1)}/${String(at.total)}`;
|
|
81
|
-
tell(escapes.title(`${invocation.command} · ${counter} ${title}`), screen.title);
|
|
82
|
-
tell(escapes.progress((at.index / at.total) * 100), screen.progressBar);
|
|
83
|
-
const shown = step.kind === "run" && step.stdout === "show";
|
|
84
|
-
if (shown)
|
|
85
|
-
say(spread([" ", active("◆"), " ", strong(title)], [muted(counter)], columns));
|
|
86
|
-
const stop = !shown && screen.live
|
|
87
|
-
? spinner(write, (frame, ms) => paint(spread(truncate([" ", active(frame), " ", title], columns - 8), [muted(duration(ms))], columns), screen.colors))
|
|
88
|
-
: () => undefined;
|
|
89
|
-
const started = performance.now();
|
|
90
|
-
const done = await work();
|
|
91
|
-
stop();
|
|
92
|
-
// A diagnosis reports itself on stdout: that line is doctor's output.
|
|
93
|
-
if (step.kind === "check" && step.onFailure === "continue")
|
|
94
|
-
return done;
|
|
95
|
-
const took = duration(performance.now() - started);
|
|
96
|
-
say(spread([" ", done.failed ? bad("✗") : ok("✓"), " ", title], [muted(done.why === undefined ? took : `${done.why} · ${took}`)], columns));
|
|
97
|
-
return done;
|
|
98
|
-
},
|
|
99
|
-
};
|
|
100
|
-
};
|
|
101
42
|
/** Every step of a plan, its command's paths named by their root. */
|
|
102
43
|
export const listed = (plan, short) => plan.map((step, index) => {
|
|
103
44
|
const command = commandOf(step);
|
|
@@ -27,7 +27,7 @@ export const settingsPage = () => [
|
|
|
27
27
|
"description: Every setting a fleet can give its servers, generated from kayra's schemas.",
|
|
28
28
|
"---",
|
|
29
29
|
"",
|
|
30
|
-
"{/* Generated by `npm run schemas` from cli/src/settings.ts — edit there, not here. */}",
|
|
30
|
+
"{/* Generated by `npm run schemas` from cli/src/core/settings.ts — edit there, not here. */}",
|
|
31
31
|
"",
|
|
32
32
|
"A server's `vars.yml` may set the settings of its kind and nothing else: an unknown key stops every",
|
|
33
33
|
"kayra command, with a suggestion where it looks like a typo. `servers/defaults.yml` holds settings",
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
/** The state all the updates make, drawn nowhere: for a stream that is no terminal. */
|
|
2
|
+
export const settle = async (initial, updates) => (await Promise.all(updates)).reduce((state, update) => update(state), initial);
|
|
3
|
+
/** Each state as the updates land, in the order they land: for a viewer elsewhere (the web interface). */
|
|
4
|
+
export const follow = async (initial, updates, each) => {
|
|
5
|
+
const go = async (state, waiting) => {
|
|
6
|
+
each(state);
|
|
7
|
+
if (waiting.length === 0)
|
|
8
|
+
return state;
|
|
9
|
+
const next = await Promise.race(waiting.map((one) => one.landed));
|
|
10
|
+
return go(next.update(state), waiting.filter((one) => one.id !== next.id));
|
|
11
|
+
};
|
|
12
|
+
return go(initial, updates.map((update, id) => ({ id, landed: update.then((done) => ({ id, update: done })) })));
|
|
13
|
+
};
|
package/cli/dist/main.js
CHANGED
|
@@ -1,29 +1,32 @@
|
|
|
1
1
|
#!/usr/bin/env node
|
|
2
2
|
import { run } from "@optique/run";
|
|
3
|
-
import { createInterface } from "node:readline/promises";
|
|
4
3
|
import { existsSync, readFileSync } from "node:fs";
|
|
5
4
|
import { homedir } from "node:os";
|
|
6
|
-
import { join
|
|
7
|
-
import { absolutePath, fleetRoot, kayraRoot, within } from "./brand.js";
|
|
8
|
-
import {
|
|
9
|
-
import {
|
|
10
|
-
import {
|
|
11
|
-
import { verbose } from "./commands/server.js";
|
|
12
|
-
import {
|
|
13
|
-
import {
|
|
14
|
-
import {
|
|
15
|
-
import {
|
|
16
|
-
import {
|
|
17
|
-
import {
|
|
18
|
-
import {
|
|
19
|
-
import {
|
|
20
|
-
import {
|
|
5
|
+
import { join } from "node:path";
|
|
6
|
+
import { absolutePath, fleetRoot, kayraRoot, within } from "./core/brand.js";
|
|
7
|
+
import { questions } from "./clients/cli/questions.js";
|
|
8
|
+
import { fromCommand } from "./clients/cli/invocation.js";
|
|
9
|
+
import { parser, runOptions } from "./clients/cli/parser.js";
|
|
10
|
+
import { verbose } from "./core/commands/server.js";
|
|
11
|
+
import { opentofu, statusJson, statusView } from "./core/commands/status.js";
|
|
12
|
+
import { overall, validateJson, validateView } from "./core/commands/validate.js";
|
|
13
|
+
import { interpret } from "./core/interpret.js";
|
|
14
|
+
import { findFleetRoot, loadFleet } from "./core/load.js";
|
|
15
|
+
import { operation as describe } from "./core/operations/catalog.js";
|
|
16
|
+
import { hasPlan, planOf, query, serverOf, stream } from "./core/operations/run.js";
|
|
17
|
+
import { systemBrowser } from "./clients/ui/opener.js";
|
|
18
|
+
import { serve } from "./serve.js";
|
|
19
|
+
import { network } from "./core/net.js";
|
|
20
|
+
import { render } from "./core/plan.js";
|
|
21
21
|
import { flatMap, map, ok } from "./result.js";
|
|
22
|
-
import { explain } from "./
|
|
23
|
-
import { bad, muted,
|
|
24
|
-
import {
|
|
25
|
-
import {
|
|
26
|
-
import {
|
|
22
|
+
import { explain } from "./clients/cli/explain.js";
|
|
23
|
+
import { bad, muted, strong } from "./core/present/line.js";
|
|
24
|
+
import { silent } from "./core/present/progress.js";
|
|
25
|
+
import { settle } from "./core/updates.js";
|
|
26
|
+
import { framed } from "./clients/cli/framed.js";
|
|
27
|
+
import { paint } from "./clients/cli/paint.js";
|
|
28
|
+
import { screen, watch } from "./clients/cli/terminal.js";
|
|
29
|
+
import { shortener, truncate } from "./core/present/text.js";
|
|
27
30
|
import { z } from "zod";
|
|
28
31
|
const here = absolutePath(process.cwd());
|
|
29
32
|
const engine = absolutePath(import.meta.dirname);
|
|
@@ -32,6 +35,7 @@ if (!here.ok || !engine.ok)
|
|
|
32
35
|
const kayra = kayraRoot(within(engine.value, "..", ".."));
|
|
33
36
|
const version = z.object({ version: z.string() }).parse(JSON.parse(readFileSync(join(kayra, "package.json"), "utf8"))).version;
|
|
34
37
|
const screens = { out: screen(process.stdout, process.env), err: screen(process.stderr, process.env) };
|
|
38
|
+
const input = questions((text) => paint(text, screens.err.colors));
|
|
35
39
|
const io = {
|
|
36
40
|
out: (text) => {
|
|
37
41
|
console.log(paint(text, screens.out.colors, screens.out.links));
|
|
@@ -39,23 +43,7 @@ const io = {
|
|
|
39
43
|
err: (text) => {
|
|
40
44
|
console.error(paint(text, screens.err.colors, screens.err.links));
|
|
41
45
|
},
|
|
42
|
-
ask:
|
|
43
|
-
const terminal = createInterface({ input: process.stdin, output: process.stderr });
|
|
44
|
-
// Ctrl-C at the prompt, or no more input: no answer, so nothing happens.
|
|
45
|
-
const answer = await new Promise((resolve) => {
|
|
46
|
-
terminal.once("SIGINT", () => {
|
|
47
|
-
resolve("");
|
|
48
|
-
});
|
|
49
|
-
terminal.once("close", () => {
|
|
50
|
-
resolve("");
|
|
51
|
-
});
|
|
52
|
-
void terminal.question(paint(prompt, screens.err.colors)).then(resolve, () => {
|
|
53
|
-
resolve("");
|
|
54
|
-
});
|
|
55
|
-
});
|
|
56
|
-
terminal.close();
|
|
57
|
-
return answer;
|
|
58
|
-
},
|
|
46
|
+
ask: (question) => input.ask(question),
|
|
59
47
|
};
|
|
60
48
|
const fail = (message) => {
|
|
61
49
|
io.err(screens.err.live ? [bad("✗"), " ", message] : `kayra: ${message}`);
|
|
@@ -82,79 +70,90 @@ const execute = async (steps, about, framing) => {
|
|
|
82
70
|
const progress = framing
|
|
83
71
|
? framed(screens.err, (raw) => process.stderr.write(raw), about)
|
|
84
72
|
: silent;
|
|
85
|
-
const passed = await interpret(steps, io, progress, () => interrupt.signal.aborted);
|
|
73
|
+
const passed = await interpret(steps, { ...io, signal: interrupt.signal }, progress, () => interrupt.signal.aborted).finally(input.close);
|
|
86
74
|
// eslint-disable-next-line functional/immutable-data -- the exit status; process.exit() could cut piped output short
|
|
87
75
|
process.exitCode = interrupt.signal.aborted ? 130 : passed ? 0 : 1;
|
|
88
76
|
};
|
|
89
77
|
const found = flatMap(findFleetRoot(here.value), loadFleet);
|
|
90
|
-
// Outside a fleet only
|
|
78
|
+
// Outside a fleet only what works in an app's repository runs (app check).
|
|
91
79
|
const fleet = found.ok ? found : ok({ root: fleetRoot(here.value), servers: [] });
|
|
92
80
|
if (!fleet.ok)
|
|
93
81
|
fail(fleet.error);
|
|
94
82
|
else {
|
|
95
83
|
const args = run(parser(fleet.value), runOptions(version));
|
|
96
|
-
if (!found.ok && args.command.name !== "app check")
|
|
97
|
-
fail(found.error);
|
|
98
84
|
const short = shortener([
|
|
99
85
|
[kayra, "<kayra>"],
|
|
100
86
|
...(found.ok ? [[fleet.value.root, "<fleet>"]] : []),
|
|
101
87
|
[homedir(), "~"],
|
|
102
88
|
]);
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
const { json, offline } = args.command;
|
|
116
|
-
const { initial, updates } = look(fleet.value, opentofu, offline ? undefined : network(2_000));
|
|
117
|
-
// On a terminal the table is there at once, its cells filled in as the answers arrive; the report itself goes to stdout.
|
|
118
|
-
const draw = (looks, frame) => statusView(looks, { frame, room: screens.err.columns - 1, offline }).map((line) => paint(truncate(line, screens.err.columns - 1), screens.err.colors));
|
|
119
|
-
const looks = !json && screens.out.live && screens.err.live
|
|
120
|
-
? await watch((raw) => process.stderr.write(raw), initial, updates, draw)
|
|
121
|
-
: await settle(initial, updates);
|
|
122
|
-
(json
|
|
123
|
-
? [JSON.stringify(statusJson(looks), null, 2)]
|
|
124
|
-
: statusView(looks, { frame: "", room: screens.out.live ? screens.out.columns : Infinity, offline })).forEach(io.out);
|
|
125
|
-
}
|
|
126
|
-
else if (args.command.name === "ui") {
|
|
89
|
+
const context = {
|
|
90
|
+
engine: { kayra, fleet: fleet.value.root },
|
|
91
|
+
fleet: fleet.value,
|
|
92
|
+
version,
|
|
93
|
+
here: here.value,
|
|
94
|
+
read: (path) => (existsSync(path) ? readFileSync(path, "utf8") : undefined),
|
|
95
|
+
network: network(2_000),
|
|
96
|
+
tofu: opentofu,
|
|
97
|
+
};
|
|
98
|
+
if (args.command.name === "serve") {
|
|
99
|
+
if (!found.ok)
|
|
100
|
+
fail(found.error);
|
|
127
101
|
// The browser side: web/dist in a checkout, cli/dist/browser in the package.
|
|
128
102
|
const assets = [join(kayra, "web", "dist"), join(kayra, "cli", "dist", "browser")].find((dir) => existsSync(join(dir, "index.html")));
|
|
129
103
|
const { port, noOpen } = args.command;
|
|
130
|
-
const
|
|
131
|
-
|
|
104
|
+
const clients = { ui: args.command.ui || !args.command.api, api: args.command.api };
|
|
105
|
+
const listening = await serve({ kayra, fleet: fleet.value, version, short, network: context.network }, assets, port, clients);
|
|
106
|
+
io.err([strong("kayra serve"), muted(` · ${fleet.value.servers.length.toString()} servers · kayra ${version} · Ctrl-C stops it`)]);
|
|
132
107
|
io.out(listening.url);
|
|
133
|
-
if (
|
|
108
|
+
if (listening.discovery !== undefined)
|
|
109
|
+
io.err([muted(`API credentials: ${listening.discovery}`)]);
|
|
110
|
+
if (clients.ui && !noOpen)
|
|
134
111
|
systemBrowser.open(listening.url);
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
}
|
|
139
|
-
else if (args.command.name === "validate") {
|
|
140
|
-
const { server: chosen, json } = args.command;
|
|
141
|
-
const servers = fleet.value.servers.filter((each) => chosen === undefined || each.name === chosen);
|
|
142
|
-
const reports = await Promise.all(servers.map(async (each) => ({ server: each, findings: findings(await gather(fleet.value.root, each)) })));
|
|
143
|
-
(json ? [JSON.stringify(validateJson(reports), null, 2)] : validateView(reports)).forEach(io.out);
|
|
144
|
-
// eslint-disable-next-line functional/immutable-data -- the exit status; process.exit() could cut piped output short
|
|
145
|
-
process.exitCode = reports.some((each) => overall(each.findings) === "problem") ? 1 : 0;
|
|
112
|
+
const stop = () => { void listening.close().then(() => process.exit(0)); };
|
|
113
|
+
process.once("SIGINT", stop);
|
|
114
|
+
process.once("SIGTERM", stop);
|
|
146
115
|
}
|
|
147
116
|
else {
|
|
148
|
-
const {
|
|
149
|
-
const
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
const
|
|
153
|
-
if (
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
117
|
+
const { invocation: call, json } = fromCommand(args.command);
|
|
118
|
+
const operation = describe(call.name);
|
|
119
|
+
if (!found.ok && operation.scope !== "repo")
|
|
120
|
+
fail(found.error);
|
|
121
|
+
const target = serverOf(context, call);
|
|
122
|
+
if (hasPlan(call)) {
|
|
123
|
+
const steps = map(planOf(context, call), (each) => (args.verbose ? verbose(each) : each));
|
|
124
|
+
// An operation whose output is its answer (ssh, the deployer's) gets nothing drawn around it.
|
|
125
|
+
if (!steps.ok)
|
|
126
|
+
fail(steps.error);
|
|
127
|
+
else if (args.dryRun)
|
|
128
|
+
show(steps.value, invocation(target), short);
|
|
129
|
+
else
|
|
130
|
+
await execute(steps.value, invocation(target), operation.bare !== true);
|
|
131
|
+
}
|
|
132
|
+
else if (call.name === "status") {
|
|
133
|
+
const { offline } = call.input;
|
|
134
|
+
const { initial, updates } = stream(context, "status", call.input);
|
|
135
|
+
// On a terminal the table is there at once, its cells filled in as the answers arrive; the report itself goes to stdout.
|
|
136
|
+
const draw = (looks, frame) => statusView(looks, { frame, room: screens.err.columns - 1, offline }).map((line) => paint(truncate(line, screens.err.columns - 1), screens.err.colors));
|
|
137
|
+
const looks = !json && screens.out.live && screens.err.live
|
|
138
|
+
? await watch((raw) => process.stderr.write(raw), initial, updates, draw)
|
|
139
|
+
: await settle(initial, updates);
|
|
140
|
+
(json
|
|
141
|
+
? [JSON.stringify(statusJson(looks), null, 2)]
|
|
142
|
+
: statusView(looks, { frame: "", room: screens.out.live ? screens.out.columns : Infinity, offline })).forEach(io.out);
|
|
143
|
+
}
|
|
144
|
+
else if (call.name === "validate") {
|
|
145
|
+
const reports = await query(context, "validate", call.input);
|
|
146
|
+
if (!reports.ok)
|
|
147
|
+
fail(reports.error);
|
|
148
|
+
else {
|
|
149
|
+
(json ? [JSON.stringify(validateJson(reports.value), null, 2)] : validateView(reports.value)).forEach(io.out);
|
|
150
|
+
// eslint-disable-next-line functional/immutable-data -- the exit status; process.exit() could cut piped output short
|
|
151
|
+
process.exitCode = reports.value.some((each) => overall(each.findings) === "problem") ? 1 : 0;
|
|
152
|
+
}
|
|
153
|
+
}
|
|
154
|
+
else {
|
|
155
|
+
// version: Optique answers `kayra version` and `--version` itself, before this.
|
|
156
|
+
io.out(version);
|
|
157
|
+
}
|
|
159
158
|
}
|
|
160
159
|
}
|
|
@@ -0,0 +1,77 @@
|
|
|
1
|
+
import { randomBytes } from "node:crypto";
|
|
2
|
+
import { createServer } from "node:http";
|
|
3
|
+
import { within } from "./core/brand.js";
|
|
4
|
+
import { createApi } from "./clients/api/api.js";
|
|
5
|
+
import { admit, cookieName, tokenIn } from "./clients/api/auth.js";
|
|
6
|
+
import { discover } from "./clients/api/discovery.js";
|
|
7
|
+
import { serveAsset } from "./clients/ui/assets.js";
|
|
8
|
+
import { send } from "./deployer/http.js";
|
|
9
|
+
export const serve = (config, assets, port, clients = { ui: true, api: false }) => {
|
|
10
|
+
const token = randomBytes(32).toString("base64url");
|
|
11
|
+
const api = createApi(config);
|
|
12
|
+
return new Promise((resolve, reject) => {
|
|
13
|
+
const server = createServer((request, response) => {
|
|
14
|
+
const at = listening();
|
|
15
|
+
const url = new URL(request.url ?? "/", `http://127.0.0.1:${String(at)}`);
|
|
16
|
+
const admission = admit({ method: request.method ?? "GET", path: url.pathname, host: request.headers.host, origin: request.headers.origin, cookie: request.headers.cookie, authorization: request.headers.authorization }, at, token, clients);
|
|
17
|
+
if (!admission.admitted) {
|
|
18
|
+
send(response, admission.status, `${admission.why}\n`);
|
|
19
|
+
return;
|
|
20
|
+
}
|
|
21
|
+
if (url.pathname === "/login" && clients.ui) {
|
|
22
|
+
if (!tokenIn(url.searchParams, token)) {
|
|
23
|
+
send(response, 401, "not this launch's token: open the URL kayra printed\n");
|
|
24
|
+
return;
|
|
25
|
+
}
|
|
26
|
+
response.writeHead(303, { "Set-Cookie": `${cookieName(at)}=${token}; HttpOnly; SameSite=Strict; Path=/`, Location: "/", "Cache-Control": "no-store", "Referrer-Policy": "no-referrer" });
|
|
27
|
+
response.end();
|
|
28
|
+
return;
|
|
29
|
+
}
|
|
30
|
+
if (url.pathname.startsWith("/api/")) {
|
|
31
|
+
api.handle(request, response, url).catch(() => {
|
|
32
|
+
if (!response.headersSent)
|
|
33
|
+
send(response, 500, "kayra could not complete this request\n");
|
|
34
|
+
else
|
|
35
|
+
response.end();
|
|
36
|
+
});
|
|
37
|
+
}
|
|
38
|
+
else if (clients.ui)
|
|
39
|
+
serveAsset(assets, url.pathname, response);
|
|
40
|
+
else
|
|
41
|
+
send(response, 404, "the browser interface is not enabled\n");
|
|
42
|
+
});
|
|
43
|
+
const listening = () => {
|
|
44
|
+
const address = server.address();
|
|
45
|
+
return typeof address === "object" && address !== null ? address.port : port;
|
|
46
|
+
};
|
|
47
|
+
server.once("error", reject);
|
|
48
|
+
server.listen(port, "127.0.0.1", () => {
|
|
49
|
+
const actual = listening();
|
|
50
|
+
const base = `http://127.0.0.1:${String(actual)}`;
|
|
51
|
+
const file = within(config.fleet.root, ".cache", "serve.json");
|
|
52
|
+
try {
|
|
53
|
+
const discovery = clients.api ? discover(file, `${base}/api`, token) : undefined;
|
|
54
|
+
if (discovery !== undefined && !discovery.ok) {
|
|
55
|
+
server.close();
|
|
56
|
+
reject(new Error(discovery.error));
|
|
57
|
+
return;
|
|
58
|
+
}
|
|
59
|
+
resolve({
|
|
60
|
+
url: clients.ui ? `${base}/login?token=${token}` : `${base}/api`, port: actual,
|
|
61
|
+
...(clients.api ? { discovery: file } : {}),
|
|
62
|
+
close: async () => {
|
|
63
|
+
const stopping = api.stop();
|
|
64
|
+
server.closeAllConnections();
|
|
65
|
+
await Promise.all([stopping, new Promise((done) => { server.close(() => { done(); }); })]);
|
|
66
|
+
if (discovery?.ok === true)
|
|
67
|
+
discovery.value.remove();
|
|
68
|
+
},
|
|
69
|
+
});
|
|
70
|
+
}
|
|
71
|
+
catch (error) {
|
|
72
|
+
server.close();
|
|
73
|
+
reject(error instanceof Error ? error : new Error(String(error)));
|
|
74
|
+
}
|
|
75
|
+
});
|
|
76
|
+
});
|
|
77
|
+
};
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@yunuseon/kayra",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.17.0",
|
|
4
4
|
"description": "A self-hosted app platform and the CLI that runs it: Traefik, Authelia, a registry, rootless BuildKit and a small deployer per server, set up with OpenTofu and Ansible.",
|
|
5
5
|
"license": "MIT",
|
|
6
6
|
"author": "Yunus Öztürk",
|
|
@@ -39,7 +39,8 @@
|
|
|
39
39
|
"typecheck": "tsc -p cli --noEmit",
|
|
40
40
|
"lint": "cd cli && eslint --flag unstable_native_nodejs_ts_config .",
|
|
41
41
|
"test": "node --test 'cli/test/**/*.test.ts'",
|
|
42
|
-
"schemas": "node cli/scripts/schemas.ts"
|
|
42
|
+
"schemas": "node cli/scripts/schemas.ts",
|
|
43
|
+
"contract": "node cli/scripts/contract.ts"
|
|
43
44
|
},
|
|
44
45
|
"dependencies": {
|
|
45
46
|
"@optique/core": "1.3.3",
|
package/scripts/agent/guard.sh
CHANGED
|
@@ -57,8 +57,15 @@ if has "${kayra} +(--[a-z-]+ +)*${verbs}( |$)" && ! has '--dry-run'; then
|
|
|
57
57
|
|| target="$(sed -n 's/^target: *\([a-z]*\).*/\1/p' "${project}/servers/${server}/server.yml")"
|
|
58
58
|
[ "$target" = multipass ] || ask "kayra changes ${server:-a server}${target:+ (target ${target})}"
|
|
59
59
|
fi
|
|
60
|
-
# kayra
|
|
61
|
-
has "${kayra} +(--[a-z-]+ +)*
|
|
60
|
+
# kayra serve runs any command on any server from a browser, where this guard sees none of them.
|
|
61
|
+
has "${kayra} +(--[a-z-]+ +)*serve( |$)" && ask "kayra serve runs commands on every server from a browser, out of this guard's sight"
|
|
62
|
+
|
|
63
|
+
# Local API writes can change any server; the guard cannot inspect their JSON safely.
|
|
64
|
+
if has 'https?://(127\.0\.0\.1|localhost)(:[0-9]+)?/api(/|[?" ])' \
|
|
65
|
+
&& { has 'curl .*((-X|--request)[ =]*(POST|PUT|PATCH|DELETE)|(-d|--data[a-z-]*|--json|--form)([ =]|$))' \
|
|
66
|
+
|| { has 'fetch[ (]' && has '(POST|PUT|PATCH|DELETE)'; }; }; then
|
|
67
|
+
ask "a local kayra API write can change a real server"
|
|
68
|
+
fi
|
|
62
69
|
|
|
63
70
|
# ── Ansible and the vaults ──────────────────────────────────────────────────
|
|
64
71
|
has '(^|[;&|( ])ansible-vault +(decrypt|view|edit|encrypt|encrypt_string|rekey|create)( |$)' \
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
#!/usr/bin/env bash
|
|
2
2
|
# Packs kayra as npm publishes it, installs the tarball the way a fleet does,
|
|
3
|
-
# and runs it: its version, its help, and kayra
|
|
3
|
+
# and runs it: its version, its help, and kayra serve answering with its browser
|
|
4
4
|
# side. Run by release.yml before npm publish, and by hand before a release.
|
|
5
5
|
# bash scripts/local/packed-runs.sh
|
|
6
6
|
set -euo pipefail
|
|
@@ -19,10 +19,25 @@ expected="$(node -p "require('${here}/package.json').version")"
|
|
|
19
19
|
[ "$version" = "$expected" ] || { echo "✗ the packed kayra says ${version}, not ${expected}" >&2; exit 1; }
|
|
20
20
|
(cd "${work}/fleet" && node "$kayra" --help >/dev/null)
|
|
21
21
|
|
|
22
|
-
(cd "${work}/fleet" && node "$kayra" ui --no-open --port 0 > "${work}/ui.out" 2>&1) &
|
|
22
|
+
(cd "${work}/fleet" && exec node "$kayra" serve --ui --api --no-open --port 0 > "${work}/ui.out" 2>&1) &
|
|
23
23
|
ui=$!
|
|
24
24
|
for _ in $(seq 1 50); do grep -q '/login?token=' "${work}/ui.out" && break; sleep 0.2; done
|
|
25
|
-
url="$(grep -o 'http://127.0.0.1:[0-9]*/login?token=[A-Za-z0-9_-]*' "${work}/ui.out")" || { cat "${work}/ui.out" >&2; echo "✗ kayra
|
|
25
|
+
url="$(grep -o 'http://127.0.0.1:[0-9]*/login?token=[A-Za-z0-9_-]*' "${work}/ui.out")" || { cat "${work}/ui.out" >&2; echo "✗ kayra serve did not start" >&2; exit 1; }
|
|
26
26
|
page="$(curl -sf -L -c "${work}/jar" -b "${work}/jar" "$url")"
|
|
27
|
-
grep -q '<script type="module"' <<< "$page" || { echo "✗ kayra
|
|
28
|
-
|
|
27
|
+
grep -q '<script type="module"' <<< "$page" || { echo "✗ kayra serve serves no built browser side" >&2; exit 1; }
|
|
28
|
+
for asset in "${work}/fleet/node_modules/@yunuseon/kayra/cli/dist/browser/assets/"*; do
|
|
29
|
+
curl -sf -b "${work}/jar" "${url%%/login*}/assets/$(basename "$asset")" > "${work}/asset"
|
|
30
|
+
cmp -s "$asset" "${work}/asset" || { echo "✗ kayra serve did not serve $(basename "$asset")" >&2; exit 1; }
|
|
31
|
+
done
|
|
32
|
+
echo "✓ the packed kayra ${version} runs, and kayra serve serves its browser side"
|
|
33
|
+
|
|
34
|
+
discovery="${work}/fleet/.cache/serve.json"
|
|
35
|
+
[ -f "$discovery" ] || { echo "✗ kayra serve wrote no API discovery file" >&2; exit 1; }
|
|
36
|
+
api_url="$(node -p "JSON.parse(require('node:fs').readFileSync(process.argv[1], 'utf8')).url" "$discovery")"
|
|
37
|
+
token="$(node -p "JSON.parse(require('node:fs').readFileSync(process.argv[1], 'utf8')).token" "$discovery")"
|
|
38
|
+
operations="$(curl -sf -H "Authorization: Bearer ${token}" "${api_url}/operations")"
|
|
39
|
+
node -e 'const operations=JSON.parse(process.argv[1]); if (!operations.some(op=>op.name==="ssh") || !operations.some(op=>op.name==="deploy")) process.exit(1)' "$operations"
|
|
40
|
+
kill "$ui"
|
|
41
|
+
wait "$ui"
|
|
42
|
+
[ ! -f "$discovery" ] || { echo "✗ kayra serve left its API discovery file" >&2; exit 1; }
|
|
43
|
+
echo "✓ the packed API exposes the catalog and removes its discovery file on shutdown"
|
|
@@ -6,6 +6,13 @@
|
|
|
6
6
|
# Usage: bash scripts/local/setup-hosts.sh <domain> <ip> [subdomain…]
|
|
7
7
|
set -euo pipefail
|
|
8
8
|
|
|
9
|
+
if [ "$EUID" -ne 0 ]; then
|
|
10
|
+
if [ "${KAYRA_SUDO_STDIN:-}" = 1 ]; then
|
|
11
|
+
exec sudo -S -p '' -- bash "$0" "$@"
|
|
12
|
+
fi
|
|
13
|
+
exec sudo -- bash "$0" "$@"
|
|
14
|
+
fi
|
|
15
|
+
|
|
9
16
|
KAYRA_ROOT="$(cd "$(dirname "$0")/../.." && pwd)"
|
|
10
17
|
DOMAIN="${1:?Usage: setup-hosts.sh <domain> <ip>}"
|
|
11
18
|
IP="${2:?Usage: setup-hosts.sh <domain> <ip> [subdomain…]}"
|
|
@@ -15,7 +22,7 @@ echo "==> Updating /etc/hosts for ${DOMAIN} -> ${IP}..."
|
|
|
15
22
|
|
|
16
23
|
if grep -q "${DOMAIN}" /etc/hosts 2>/dev/null; then
|
|
17
24
|
echo " Removing old entries..."
|
|
18
|
-
|
|
25
|
+
sed -i.bak "/${DOMAIN}/d" /etc/hosts
|
|
19
26
|
fi
|
|
20
27
|
|
|
21
28
|
HOSTS=""
|
|
@@ -23,7 +30,7 @@ for sub in $(awk '!/^[[:space:]]*(#|$)/ {print $1}' "${KAYRA_ROOT}"/hosts.conf)
|
|
|
23
30
|
HOSTS="${HOSTS} ${sub}.${DOMAIN}"
|
|
24
31
|
done
|
|
25
32
|
|
|
26
|
-
echo "${IP} ${DOMAIN}${HOSTS}" |
|
|
33
|
+
echo "${IP} ${DOMAIN}${HOSTS}" | tee -a /etc/hosts > /dev/null
|
|
27
34
|
|
|
28
35
|
echo ""
|
|
29
36
|
echo "Done — /etc/hosts updated:"
|