@yunuseon/kayra 0.13.1 → 0.15.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/ansible/ansible.cfg +6 -3
- package/ansible/callback_plugins/kayra.py +299 -0
- package/cli/dist/cli.js +156 -30
- package/cli/dist/commands/app-check.js +1 -1
- package/cli/dist/commands/server.js +68 -49
- package/cli/dist/commands/status.js +181 -30
- package/cli/dist/commands/validate.js +92 -31
- package/cli/dist/deployer/answers.js +21 -0
- package/cli/dist/deployer/ctl.js +20 -6
- package/cli/dist/gather.js +9 -5
- package/cli/dist/interpret.js +71 -50
- package/cli/dist/main.js +87 -25
- package/cli/dist/net.js +52 -0
- package/cli/dist/plan.js +35 -4
- package/cli/dist/ui/explain.js +37 -0
- package/cli/dist/ui/line.js +39 -0
- package/cli/dist/ui/progress.js +100 -0
- package/cli/dist/ui/terminal.js +93 -0
- package/cli/dist/ui/text.js +71 -0
- package/cli/dist/ui/views.js +127 -0
- package/cli/dist/validate.js +8 -3
- package/package.json +1 -1
- package/scripts/local/verify.sh +2 -2
|
@@ -0,0 +1,127 @@
|
|
|
1
|
+
import { z } from "zod";
|
|
2
|
+
import { accent, active, bad, muted, ok, strong, warn } from "./line.js";
|
|
3
|
+
import { ago, duration, table } from "./text.js";
|
|
4
|
+
const deployed = z.object({ sha: z.string(), time: z.iso.datetime() });
|
|
5
|
+
const runsAnswer = z.object({
|
|
6
|
+
app: z.string(),
|
|
7
|
+
live: deployed.exactOptional(),
|
|
8
|
+
runs: z.array(z.object({
|
|
9
|
+
id: z.string(),
|
|
10
|
+
state: z.enum(["running", "success", "failure"]),
|
|
11
|
+
why: z.string(),
|
|
12
|
+
what: z.string(),
|
|
13
|
+
sha: z.string().exactOptional(),
|
|
14
|
+
started: z.iso.datetime(),
|
|
15
|
+
finished: z.iso.datetime().exactOptional(),
|
|
16
|
+
})),
|
|
17
|
+
});
|
|
18
|
+
const releasesAnswer = z.object({
|
|
19
|
+
app: z.string(),
|
|
20
|
+
releases: z.array(deployed.extend({ live: z.boolean() })),
|
|
21
|
+
});
|
|
22
|
+
/** The answer as the schema has it, or nothing: then it is not the answer this kayra expects. */
|
|
23
|
+
const parsed = (schema, answer) => {
|
|
24
|
+
try {
|
|
25
|
+
const json = JSON.parse(answer);
|
|
26
|
+
const result = schema.safeParse(json);
|
|
27
|
+
return result.success ? result.data : undefined;
|
|
28
|
+
}
|
|
29
|
+
catch {
|
|
30
|
+
return undefined;
|
|
31
|
+
}
|
|
32
|
+
};
|
|
33
|
+
/** Lines as printing them one by one gives the text back: newline-ended, as a ctl writes. */
|
|
34
|
+
const linesOf = (text) => (text === "" ? [] : text.replace(/\n$/, "").split("\n"));
|
|
35
|
+
const short = (sha) => sha.slice(0, 7);
|
|
36
|
+
const since = (time, now) => ago(now - Date.parse(time));
|
|
37
|
+
const heading = (view, ...more) => [strong(view.app), muted(` on ${view.server}`), ...more];
|
|
38
|
+
const live = (release, now) => release === undefined
|
|
39
|
+
? [muted(" · not deployed yet")]
|
|
40
|
+
: [muted(" · live "), accent(short(release.sha)), muted(` since ${since(release.time, now)}`)];
|
|
41
|
+
const hint = (command, what) => [" ", active("→"), " ", strong(command), muted(` ${what}`)];
|
|
42
|
+
const states = {
|
|
43
|
+
success: [ok("✓ success")],
|
|
44
|
+
failure: [bad("✗ failure")],
|
|
45
|
+
running: [active("◆ running")],
|
|
46
|
+
};
|
|
47
|
+
const took = (run, now) => run.finished === undefined
|
|
48
|
+
? [active(`${duration(now - Date.parse(run.started))}…`)]
|
|
49
|
+
: [duration(Date.parse(run.finished) - Date.parse(run.started))];
|
|
50
|
+
const commit = (run, release) => {
|
|
51
|
+
if (run.sha === undefined)
|
|
52
|
+
return [muted("—")];
|
|
53
|
+
return run.sha === release?.sha ? [accent(short(run.sha))] : [short(run.sha)];
|
|
54
|
+
};
|
|
55
|
+
/** `kayra app runs`: what runs now, then a table of the newest runs. */
|
|
56
|
+
export const runsView = (view, answer, now) => {
|
|
57
|
+
const rows = table([
|
|
58
|
+
["STATE", "COMMIT", "WHY", "WHEN", "TOOK", "RUN"].map((title) => [muted(title)]),
|
|
59
|
+
...answer.runs.map((run) => [states[run.state], commit(run, answer.live), [run.why], [since(run.started, now)], took(run, now), [muted(run.id)]]),
|
|
60
|
+
]);
|
|
61
|
+
return [
|
|
62
|
+
heading(view, ...live(answer.live, now)),
|
|
63
|
+
[],
|
|
64
|
+
...(answer.runs.length === 0
|
|
65
|
+
? [[" ", muted("no runs yet")]]
|
|
66
|
+
: [...rows.map((row) => [" ", ...row]), [], hint(`kayra app logs ${view.server} ${view.app} [RUN]`, "shows a run's log, the newest by default")]),
|
|
67
|
+
];
|
|
68
|
+
};
|
|
69
|
+
/** `kayra app rollback` without a commit: every release, the live one marked, and how to go back to one. */
|
|
70
|
+
export const releasesView = (view, answer, now) => {
|
|
71
|
+
const earlier = answer.releases.find((release) => !release.live);
|
|
72
|
+
const rows = table([
|
|
73
|
+
[[muted(" COMMIT")], [muted("DEPLOYED")]],
|
|
74
|
+
...answer.releases.map((release) => [
|
|
75
|
+
release.live ? [ok("●"), " ", accent(short(release.sha))] : [" ", short(release.sha)],
|
|
76
|
+
[since(release.time, now)],
|
|
77
|
+
release.live ? [ok("live")] : [],
|
|
78
|
+
]),
|
|
79
|
+
]);
|
|
80
|
+
const count = answer.releases.length;
|
|
81
|
+
return [
|
|
82
|
+
heading(view, muted(` · ${String(count)} release${count === 1 ? "" : "s"}`)),
|
|
83
|
+
[],
|
|
84
|
+
...rows.map((row) => [" ", ...row]),
|
|
85
|
+
[],
|
|
86
|
+
earlier === undefined
|
|
87
|
+
? [" ", muted("nothing earlier to roll back to")]
|
|
88
|
+
: hint(`kayra app rollback ${view.server} ${view.app} ${short(earlier.sha)}`, "deploys that one again"),
|
|
89
|
+
];
|
|
90
|
+
};
|
|
91
|
+
const errors = /\b(error|fatal|failed|failure|panic|refused|denied|unhealthy)\b/i;
|
|
92
|
+
const warnings = /\b(warn|warning|deprecated)\b/i;
|
|
93
|
+
/** The deployer's time, before each line it logs. */
|
|
94
|
+
const stamped = /^(\d{2}:\d{2}:\d{2} )(.*)$/s;
|
|
95
|
+
const logLine = (line) => {
|
|
96
|
+
const [, time = "", text = line] = stamped.exec(line) ?? [];
|
|
97
|
+
const tone = errors.test(text) ? bad : warnings.test(text) ? warn : text.startsWith("── ") ? strong : text.startsWith("Deployed ") ? ok : undefined;
|
|
98
|
+
return [...(time === "" ? [] : [muted(time)]), tone === undefined ? text : tone(text)];
|
|
99
|
+
};
|
|
100
|
+
/** `kayra app logs`: the log as it is, its errors and warnings marked; uncoloured, the same bytes. */
|
|
101
|
+
export const logView = (log) => linesOf(log).map(logLine);
|
|
102
|
+
/** A deployer's answer as its view draws it, or as it came if it is not what the view reads (an older ctl). */
|
|
103
|
+
export const draw = (view, answer, now) => {
|
|
104
|
+
switch (view.view) {
|
|
105
|
+
case "runs": {
|
|
106
|
+
const runs = parsed(runsAnswer, answer);
|
|
107
|
+
return runs === undefined ? linesOf(answer) : runsView(view, runs, now);
|
|
108
|
+
}
|
|
109
|
+
case "releases": {
|
|
110
|
+
const releases = parsed(releasesAnswer, answer);
|
|
111
|
+
return releases === undefined ? linesOf(answer) : releasesView(view, releases, now);
|
|
112
|
+
}
|
|
113
|
+
case "log":
|
|
114
|
+
return logView(answer);
|
|
115
|
+
}
|
|
116
|
+
};
|
|
117
|
+
/** What --dry-run says about a drawn answer. */
|
|
118
|
+
export const drawnAs = (view) => {
|
|
119
|
+
switch (view.view) {
|
|
120
|
+
case "runs":
|
|
121
|
+
return "its answer drawn as a table of runs";
|
|
122
|
+
case "releases":
|
|
123
|
+
return "its answer drawn as the releases, the live one marked";
|
|
124
|
+
case "log":
|
|
125
|
+
return "the log as it is, errors and warnings marked on a terminal";
|
|
126
|
+
}
|
|
127
|
+
};
|
package/cli/dist/validate.js
CHANGED
|
@@ -8,12 +8,17 @@ const distance = (a, b) => indices(b.length)
|
|
|
8
8
|
Math.min((row[i + 1] ?? 0) + 1, (next[i] ?? 0) + 1, (row[i] ?? 0) + (a.charAt(i) === b.charAt(j) ? 0 : 1)),
|
|
9
9
|
], [j + 1]), indices(a.length + 1))
|
|
10
10
|
.at(-1) ?? 0;
|
|
11
|
-
|
|
12
|
-
|
|
11
|
+
/** The known name a key is most likely a typo of, if one is close enough. */
|
|
12
|
+
export const closest = (key, known) => {
|
|
13
|
+
const nearest = known
|
|
13
14
|
.map((name) => ({ name, cost: distance(key, name) }))
|
|
14
15
|
.toSorted((x, y) => x.cost - y.cost)
|
|
15
16
|
.at(0);
|
|
16
|
-
return
|
|
17
|
+
return nearest !== undefined && nearest.cost <= Math.max(2, Math.floor(key.length / 4)) ? nearest.name : undefined;
|
|
18
|
+
};
|
|
19
|
+
const suggestion = (key, known) => {
|
|
20
|
+
const name = closest(key, known);
|
|
21
|
+
return name === undefined ? "" : ` — did you mean '${name}'?`;
|
|
17
22
|
};
|
|
18
23
|
/** The first problem Zod found, said for the file it is in. */
|
|
19
24
|
const firstIssue = (file, error, known, unknownKey) => {
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@yunuseon/kayra",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.15.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",
|
package/scripts/local/verify.sh
CHANGED
|
@@ -28,8 +28,8 @@ dir="${FLEET_ROOT}/servers/${name}"
|
|
|
28
28
|
wait="${VERIFY_WAIT:-150}"
|
|
29
29
|
failed=0
|
|
30
30
|
|
|
31
|
-
fail() { echo "✗ $*"; failed=1; }
|
|
32
|
-
pass() { echo "✓ $*"; }
|
|
31
|
+
fail() { echo " ✗ $*"; failed=1; }
|
|
32
|
+
pass() { echo " ✓ $*"; }
|
|
33
33
|
|
|
34
34
|
domain="$(yq -r '.domain' "${dir}/server.yml")"
|
|
35
35
|
inventory="${dir}/inventory.ini"
|