@yunuseon/kayra 0.17.0 → 0.17.2
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/agent/skills/kayra-release/SKILL.md +6 -1
- package/ansible/roles/platform/tasks/main.yml +40 -1
- package/cli/dist/browser/assets/index-hcyM6f6T.js +7 -0
- package/cli/dist/browser/index.html +1 -1
- package/cli/dist/clients/api/api.js +138 -218
- package/cli/dist/clients/api/auth.js +8 -7
- package/cli/dist/clients/api/commands.js +50 -14
- package/cli/dist/clients/api/contract.js +8 -6
- package/cli/dist/clients/api/directories.js +1 -1
- package/cli/dist/clients/api/discovery.js +8 -3
- package/cli/dist/clients/api/live.js +40 -0
- package/cli/dist/clients/api/protocol.js +17 -10
- package/cli/dist/clients/api/queries.js +4 -4
- package/cli/dist/clients/api/runs.js +10 -9
- package/cli/dist/clients/api/start.js +97 -0
- package/cli/dist/clients/api/status.js +29 -0
- package/cli/dist/clients/api/validate.js +13 -0
- package/cli/dist/clients/cli/explain.js +12 -10
- package/cli/dist/clients/cli/framed.js +12 -9
- package/cli/dist/clients/cli/invocation.js +4 -3
- package/cli/dist/clients/cli/paint.js +1 -1
- package/cli/dist/clients/cli/parser.js +8 -8
- package/cli/dist/clients/cli/questions.js +11 -10
- package/cli/dist/clients/cli/status.js +75 -0
- package/cli/dist/clients/cli/terminal.js +4 -2
- package/cli/dist/{core/present → clients/cli}/text.js +15 -24
- package/cli/dist/clients/cli/validate.js +32 -0
- package/cli/dist/clients/cli/values.js +19 -0
- package/cli/dist/{core/present → clients/cli}/views.js +28 -60
- package/cli/dist/core/adapters/fs.js +18 -0
- package/cli/dist/core/{net.js → adapters/network.js} +20 -5
- package/cli/dist/core/adapters/opentofu.js +24 -0
- package/cli/dist/core/ctl/answers.js +32 -0
- package/cli/dist/core/fleet/brand.js +54 -0
- package/cli/dist/core/{schemas.js → fleet/docs.js} +5 -4
- package/cli/dist/core/fleet/load.js +45 -0
- package/cli/dist/core/fleet/model.js +1 -0
- package/cli/dist/core/fleet/parse.js +144 -0
- package/cli/dist/core/{settings.js → fleet/settings.js} +47 -15
- package/cli/dist/core/{validate.js → fleet/shape.js} +6 -20
- package/cli/dist/core/{present/progress.js → interpreter/events.js} +27 -31
- package/cli/dist/core/interpreter/io.js +18 -0
- package/cli/dist/core/interpreter/loop.js +101 -0
- package/cli/dist/core/interpreter/needs.js +47 -0
- package/cli/dist/core/interpreter/process.js +107 -0
- package/cli/dist/core/interpreter/progress.js +15 -0
- package/cli/dist/core/interpreter/pty.js +8 -0
- package/cli/dist/core/operations/catalog.js +35 -18
- package/cli/dist/core/operations/{run.js → dispatch.js} +50 -47
- package/cli/dist/core/{plan.js → plan/describe.js} +4 -14
- package/cli/dist/core/plan/model.js +14 -0
- package/cli/dist/core/recipes/app-check.js +27 -0
- package/cli/dist/core/recipes/apps.js +63 -0
- package/cli/dist/core/recipes/backup.js +34 -0
- package/cli/dist/core/recipes/certs.js +4 -0
- package/cli/dist/core/{commands → recipes}/doctor.js +20 -35
- package/cli/dist/core/recipes/host.js +104 -0
- package/cli/dist/core/recipes/ssh.js +20 -0
- package/cli/dist/core/recipes/steps.js +82 -0
- package/cli/dist/core/status/look.js +55 -0
- package/cli/dist/core/status/model.js +6 -0
- package/cli/dist/core/status/ports.js +1 -0
- package/cli/dist/core/text/plural.js +2 -0
- package/cli/dist/core/text/spans.js +22 -0
- package/cli/dist/core/text/time.js +12 -0
- package/cli/dist/core/validation/facts.js +50 -0
- package/cli/dist/core/{commands/validate.js → validation/findings.js} +31 -66
- package/cli/dist/deployer/app.js +3 -2
- package/cli/dist/deployer/brand.js +1 -1
- package/cli/dist/deployer/{builder.js → build/builder.js} +14 -13
- package/cli/dist/deployer/{bundle.js → build/bundle.js} +8 -7
- package/cli/dist/deployer/{compose.js → build/compose.js} +8 -7
- package/cli/dist/deployer/cron.js +3 -3
- package/cli/dist/deployer/{answers.js → ctl/answers.js} +5 -4
- package/cli/dist/deployer/{ctl.js → ctl/ctl.js} +3 -3
- package/cli/dist/deployer/deployer.js +24 -23
- package/cli/dist/deployer/{forge.js → forges/forge.js} +2 -1
- package/cli/dist/deployer/{forgejo.js → forges/forgejo.js} +2 -2
- package/cli/dist/deployer/{github.js → forges/github.js} +2 -2
- package/cli/dist/deployer/{gitlab.js → forges/gitlab.js} +2 -2
- package/cli/dist/deployer/{github-issues.js → forges/issues.js} +5 -4
- package/cli/dist/deployer/{notify.js → forges/notify.js} +9 -8
- package/cli/dist/deployer/{http.js → http/http.js} +1 -0
- package/cli/dist/deployer/{page.js → http/page.js} +1 -1
- package/cli/dist/deployer/{webhook.js → http/webhook.js} +2 -1
- package/cli/dist/deployer/main.js +2 -2
- package/cli/dist/deployer/{queue.js → runs/queue.js} +4 -3
- package/cli/dist/deployer/{records.js → runs/records.js} +9 -8
- package/cli/dist/deployer/{run.js → runs/run.js} +10 -9
- package/cli/dist/deployer/{store.js → runs/store.js} +9 -8
- package/cli/dist/lib/brand.js +2 -0
- package/cli/dist/lib/closest.js +17 -0
- package/cli/dist/lib/result.js +15 -0
- package/cli/dist/lib/schema.js +7 -0
- package/cli/dist/lib/typed.js +2 -0
- package/cli/dist/lib/yaml.js +21 -0
- package/cli/dist/main.js +89 -89
- package/cli/dist/serve.js +2 -2
- package/package.json +3 -2
- package/schema/app.json +4 -2
- package/schema/server.json +10 -5
- package/cli/dist/browser/assets/index-9a-SE2rU.js +0 -7
- package/cli/dist/core/brand.js +0 -39
- package/cli/dist/core/commands/app-check.js +0 -35
- package/cli/dist/core/commands/server.js +0 -298
- package/cli/dist/core/commands/status.js +0 -194
- package/cli/dist/core/fleet.js +0 -156
- package/cli/dist/core/gather.js +0 -61
- package/cli/dist/core/interpret.js +0 -276
- package/cli/dist/core/load.js +0 -64
- package/cli/dist/core/present/line.js +0 -17
- package/cli/dist/result.js +0 -5
- /package/cli/dist/core/{updates.js → status/updates.js} +0 -0
|
@@ -0,0 +1,40 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The runs of one listener, in one cell replaced whole: no launch shares
|
|
3
|
+
* locks, credentials or sessions with another.
|
|
4
|
+
*/
|
|
5
|
+
export const liveRuns = () => {
|
|
6
|
+
// eslint-disable-next-line functional/prefer-immutable-types -- the one state cell of this listener's runs
|
|
7
|
+
const cell = { runs: {} };
|
|
8
|
+
return {
|
|
9
|
+
get: (id) => cell.runs[id],
|
|
10
|
+
all: () => Object.values(cell.runs),
|
|
11
|
+
/** Installed before any asynchronous work: the run's lock. */
|
|
12
|
+
add: (id, live) => {
|
|
13
|
+
// eslint-disable-next-line functional/immutable-data -- replace the one state cell whole
|
|
14
|
+
cell.runs = { ...cell.runs, [id]: live };
|
|
15
|
+
},
|
|
16
|
+
swap: (id, change) => {
|
|
17
|
+
const current = cell.runs[id];
|
|
18
|
+
// eslint-disable-next-line functional/immutable-data -- replace the one state cell whole
|
|
19
|
+
if (current !== undefined)
|
|
20
|
+
cell.runs = { ...cell.runs, [id]: change(current) };
|
|
21
|
+
},
|
|
22
|
+
};
|
|
23
|
+
};
|
|
24
|
+
/** The requests in flight: shutdown waits for them as well as for the runs. */
|
|
25
|
+
export const inFlight = () => {
|
|
26
|
+
// eslint-disable-next-line functional/prefer-immutable-types -- the requests not yet answered
|
|
27
|
+
const cell = { pending: [] };
|
|
28
|
+
return {
|
|
29
|
+
track: (work) => {
|
|
30
|
+
const tracked = work.finally(() => {
|
|
31
|
+
// eslint-disable-next-line functional/immutable-data -- release it once its response completes
|
|
32
|
+
cell.pending = cell.pending.filter((pending) => pending !== tracked);
|
|
33
|
+
});
|
|
34
|
+
// eslint-disable-next-line functional/immutable-data -- one more request in flight
|
|
35
|
+
cell.pending = [...cell.pending, tracked];
|
|
36
|
+
return tracked;
|
|
37
|
+
},
|
|
38
|
+
settled: () => Promise.allSettled(cell.pending),
|
|
39
|
+
};
|
|
40
|
+
};
|
|
@@ -1,14 +1,21 @@
|
|
|
1
1
|
import { z } from "zod";
|
|
2
|
-
import { operationNames } from "../../core/operations/catalog.js";
|
|
3
|
-
import {
|
|
4
|
-
|
|
2
|
+
import { effects, kinds, operationNames, scopes } from "../../core/operations/catalog.js";
|
|
3
|
+
import { serverKinds } from "../../core/fleet/settings.js";
|
|
4
|
+
import { serverTargets } from "../../core/fleet/model.js";
|
|
5
|
+
import { parts } from "../../core/plan/model.js";
|
|
6
|
+
import { stopReasons } from "../../core/interpreter/progress.js";
|
|
7
|
+
import { machineStates, reachStates } from "../../core/status/model.js";
|
|
8
|
+
import { tones } from "../../core/text/spans.js";
|
|
9
|
+
import { verdicts } from "../../core/validation/findings.js";
|
|
10
|
+
import { releasesSchema, runsSchema } from "../../core/ctl/answers.js";
|
|
11
|
+
const tone = z.enum(tones);
|
|
5
12
|
const span = z.union([z.string(), z.object({ tone: tone.exactOptional(), text: z.string(), href: z.string().exactOptional() })]);
|
|
6
13
|
const line = z.array(span);
|
|
7
14
|
const text = z.union([z.string(), line]);
|
|
8
|
-
const part = z.enum(
|
|
15
|
+
const part = z.enum(parts);
|
|
9
16
|
const listed = z.object({ index: z.number().int(), kind: z.string(), title: z.string(), command: z.string().exactOptional(), part: part.exactOptional(), opens: part.exactOptional() });
|
|
10
17
|
const invocation = z.object({ command: z.string(), server: z.string().exactOptional(), about: z.array(z.string()) });
|
|
11
|
-
const stop = z.enum(
|
|
18
|
+
const stop = z.enum(stopReasons);
|
|
12
19
|
const state = z.enum(["running", "passed", "failed", "interrupted", "declined"]);
|
|
13
20
|
const summary = z.object({ id: z.string(), command: z.string(), server: z.string().exactOptional(), started: z.string(), state, session: z.boolean() });
|
|
14
21
|
const ended = z.object({ kind: z.literal("ended"), failures: z.number(), stop: z.object({ index: z.number(), why: stop }).exactOptional(), ms: z.number(), footer: z.array(line) });
|
|
@@ -22,11 +29,11 @@ const event = z.discriminatedUnion("kind", [
|
|
|
22
29
|
z.object({ kind: z.literal("ask"), prompt: text, secret: z.boolean(), expect: z.string().exactOptional() }),
|
|
23
30
|
z.object({ kind: z.literal("answered") }),
|
|
24
31
|
]);
|
|
25
|
-
const reach = z.enum(
|
|
26
|
-
const verdict = z.enum(
|
|
32
|
+
const reach = z.enum(reachStates);
|
|
33
|
+
const verdict = z.enum(verdicts);
|
|
27
34
|
const serverCommand = z.enum(["up", "provision", "deploy", "setup", "plan", "verify", "apps", "down", "adopt", "backup.test"]);
|
|
28
35
|
const serverFacts = z.object({
|
|
29
|
-
name: z.string(), kind: z.enum(
|
|
36
|
+
name: z.string(), kind: z.enum(serverKinds), target: z.enum(serverTargets), domain: z.string(), apps: z.array(z.string()),
|
|
30
37
|
commands: z.array(z.union([serverCommand, z.literal("job")])),
|
|
31
38
|
});
|
|
32
39
|
export const options = z.strictObject({
|
|
@@ -48,13 +55,13 @@ export const schemas = {
|
|
|
48
55
|
ReachState: reach,
|
|
49
56
|
Status: z.object({ servers: z.array(z.object({
|
|
50
57
|
name: z.string(), kind: z.string(), target: z.string(), domain: z.string(), apps: z.array(z.string()),
|
|
51
|
-
machine: z.object({ state: z.enum(
|
|
58
|
+
machine: z.object({ state: z.enum(machineStates), address: z.string().nullable(), from: z.enum(["inventory", "opentofu"]).nullable(), reason: z.string().nullable() }),
|
|
52
59
|
ssh: z.object({ port: z.number(), state: reach, ms: z.number().nullable(), error: z.string().nullable() }),
|
|
53
60
|
https: z.object({ url: z.string(), state: reach, status: z.number().nullable(), ms: z.number().nullable(), error: z.string().nullable() }).nullable(),
|
|
54
61
|
})) }),
|
|
55
62
|
Validation: z.object({ result: verdict, servers: z.array(z.object({ name: z.string(), kind: z.string(), target: z.string(), result: verdict, findings: z.array(z.object({ result: verdict, text: z.string(), hint: z.string().nullable() })) })) }),
|
|
56
63
|
AppRuns: runsSchema, AppReleases: releasesSchema,
|
|
57
|
-
OperationInfo: z.object({ name: z.enum(operationNames), summary: z.string(), scope: z.enum(
|
|
64
|
+
OperationInfo: z.object({ name: z.enum(operationNames), summary: z.string(), scope: z.enum(scopes), effect: z.enum(effects), kind: z.enum(kinds), input: z.record(z.string(), z.unknown()) }),
|
|
58
65
|
};
|
|
59
66
|
export const querySchemas = {
|
|
60
67
|
validate: schemas.Validation, version: z.string(), "app.runs": schemas.AppRuns, "app.releases": schemas.AppReleases, "app.log": z.string(),
|
|
@@ -1,8 +1,8 @@
|
|
|
1
1
|
import { z } from "zod";
|
|
2
|
-
import { query } from "../../core/operations/
|
|
3
|
-
import { validateJson } from "
|
|
4
|
-
import { parsed, releasesSchema, runsSchema } from "../../core/
|
|
5
|
-
import { err, map, ok } from "../../result.js";
|
|
2
|
+
import { query } from "../../core/operations/dispatch.js";
|
|
3
|
+
import { validateJson } from "./validate.js";
|
|
4
|
+
import { parsed, releasesSchema, runsSchema } from "../../core/ctl/answers.js";
|
|
5
|
+
import { err, map, ok } from "../../lib/result.js";
|
|
6
6
|
// eslint-disable-next-line functional/prefer-immutable-types -- Zod schemas are immutable; each method returns a new schema
|
|
7
7
|
const data = (schema, text) => {
|
|
8
8
|
if (!text.ok)
|
|
@@ -1,16 +1,17 @@
|
|
|
1
|
+
import { typed } from "../../lib/typed.js";
|
|
1
2
|
/** Sortable by time (an ISO 8601 instant), unique by its tail. */
|
|
2
3
|
export const runId = (iso, random) => `${iso.replace(/[-:]/g, "").replace(/\.\d+Z$/, "Z")}-${random}`;
|
|
3
4
|
export const stateOf = (events) => {
|
|
4
5
|
const ended = events.findLast((event) => event.kind === "ended");
|
|
5
6
|
if (ended?.kind !== "ended")
|
|
6
|
-
return "running";
|
|
7
|
+
return typed("running");
|
|
7
8
|
if (ended.stop?.why === "interrupted")
|
|
8
|
-
return "interrupted";
|
|
9
|
+
return typed("interrupted");
|
|
9
10
|
if (ended.stop?.why === "declined")
|
|
10
|
-
return "declined";
|
|
11
|
-
return ended.failures === 0 ? "passed" : "failed";
|
|
11
|
+
return typed("declined");
|
|
12
|
+
return typed(ended.failures === 0 ? "passed" : "failed");
|
|
12
13
|
};
|
|
13
|
-
export const summary = (run) => ({
|
|
14
|
+
export const summary = (run) => typed({
|
|
14
15
|
id: run.id,
|
|
15
16
|
command: run.command,
|
|
16
17
|
...(run.server === undefined ? {} : { server: run.server }),
|
|
@@ -28,24 +29,24 @@ export const readRecord = (text) => {
|
|
|
28
29
|
.filter((line) => line !== "")
|
|
29
30
|
.flatMap((line) => {
|
|
30
31
|
try {
|
|
31
|
-
return [JSON.parse(line)];
|
|
32
|
+
return typed([JSON.parse(line)]);
|
|
32
33
|
}
|
|
33
34
|
catch {
|
|
34
|
-
return [];
|
|
35
|
+
return typed([]);
|
|
35
36
|
}
|
|
36
37
|
});
|
|
37
38
|
if (!isObject(head) || typeof head["id"] !== "string" || typeof head["command"] !== "string" || typeof head["started"] !== "string")
|
|
38
39
|
return undefined;
|
|
39
40
|
// Written by this kayra, the lines are its own events.
|
|
40
41
|
const events = rest.filter((each) => isObject(each) && typeof each["kind"] === "string");
|
|
41
|
-
return {
|
|
42
|
+
return typed({
|
|
42
43
|
id: head["id"],
|
|
43
44
|
command: head["command"],
|
|
44
45
|
...(typeof head["server"] === "string" ? { server: head["server"] } : {}),
|
|
45
46
|
started: head["started"],
|
|
46
47
|
events,
|
|
47
48
|
session: head["session"] === true,
|
|
48
|
-
};
|
|
49
|
+
});
|
|
49
50
|
};
|
|
50
51
|
/** A server is busy while a run on it has not ended: one at a time, as on the command line. */
|
|
51
52
|
export const busy = (runs, server) => server !== undefined && runs.some((run) => run.server === server && stateOf(run.events) === "running");
|
|
@@ -0,0 +1,97 @@
|
|
|
1
|
+
import { randomBytes } from "node:crypto";
|
|
2
|
+
import { appendFileSync, existsSync, mkdirSync, readdirSync, readFileSync } from "node:fs";
|
|
3
|
+
import { join } from "node:path";
|
|
4
|
+
import { z } from "zod";
|
|
5
|
+
import { milliseconds } from "../../core/fleet/brand.js";
|
|
6
|
+
import { heading, listed, reporting } from "../../core/interpreter/events.js";
|
|
7
|
+
import { interpret } from "../../core/interpreter/loop.js";
|
|
8
|
+
import { operation } from "../../core/operations/catalog.js";
|
|
9
|
+
import { hasPlan, planOf, serverOf } from "../../core/operations/dispatch.js";
|
|
10
|
+
import { verbose } from "../../core/recipes/steps.js";
|
|
11
|
+
import { err, ok } from "../../lib/result.js";
|
|
12
|
+
import { typed } from "../../lib/typed.js";
|
|
13
|
+
import { words } from "./commands.js";
|
|
14
|
+
import { busy, readRecord, recordLine, runId } from "./runs.js";
|
|
15
|
+
/** Every run there is: the live ones, then those recorded, newest first. */
|
|
16
|
+
export const allRuns = (served) => {
|
|
17
|
+
const live = served.runs.all().map((each) => each.run);
|
|
18
|
+
const records = existsSync(served.dir)
|
|
19
|
+
? readdirSync(served.dir).filter((name) => name.endsWith(".jsonl")).flatMap((name) => {
|
|
20
|
+
const run = readRecord(readFileSync(join(served.dir, name), "utf8"));
|
|
21
|
+
return run === undefined || live.some((each) => each.id === run.id) ? [] : [run];
|
|
22
|
+
})
|
|
23
|
+
: [];
|
|
24
|
+
return Object.freeze([...live, ...records].toSorted((a, b) => b.id.localeCompare(a.id)));
|
|
25
|
+
};
|
|
26
|
+
const about = (served, call) => heading(words(call), serverOf(served.context, call), served.api.version);
|
|
27
|
+
const recipe = (served, call, settings) => {
|
|
28
|
+
if (!hasPlan(call))
|
|
29
|
+
return err(`${call.name} has no plan`);
|
|
30
|
+
const plan = planOf(served.context, call);
|
|
31
|
+
return plan.ok && settings.verbose ? ok(verbose(plan.value)) : plan;
|
|
32
|
+
};
|
|
33
|
+
/** The plan an operation would run, as the browser lists it. */
|
|
34
|
+
export const preview = (served, call, settings) => {
|
|
35
|
+
const plan = recipe(served, call, settings);
|
|
36
|
+
return plan.ok
|
|
37
|
+
? ok({ invocation: about(served, call), steps: listed(plan.value, served.api.short), changes: operation(call.name).effect !== "reads" })
|
|
38
|
+
: plan;
|
|
39
|
+
};
|
|
40
|
+
/**
|
|
41
|
+
* A plan or a session, started: recorded as it goes, its questions and input
|
|
42
|
+
* answered over HTTP. A change to a server that is not multipass needs its
|
|
43
|
+
* name in `confirm`; one run per server at a time.
|
|
44
|
+
*/
|
|
45
|
+
export const start = (served, call, settings, confirm) => {
|
|
46
|
+
const { runs } = served;
|
|
47
|
+
if (served.shutdown.signal.aborted)
|
|
48
|
+
return err(typed([503, "kayra is stopping"]));
|
|
49
|
+
const plan = recipe(served, call, settings);
|
|
50
|
+
if (!plan.ok)
|
|
51
|
+
return err(typed([400, plan.error]));
|
|
52
|
+
const server = serverOf(served.context, call);
|
|
53
|
+
if (busy(runs.all().map((live) => live.run), server?.name))
|
|
54
|
+
return err(typed([409, `a run on ${server?.name ?? ""} has not ended`]));
|
|
55
|
+
if (operation(call.name).effect !== "reads" && server !== undefined && server.target !== "multipass" && confirm !== server.name)
|
|
56
|
+
return err(typed([403, `this changes ${server.name}: confirm with its name`]));
|
|
57
|
+
const started = new Date().toISOString();
|
|
58
|
+
const id = runId(started, randomBytes(6).toString("hex"));
|
|
59
|
+
const session = operation(call.name).kind === "session";
|
|
60
|
+
const run = { id, command: words(call), started, events: [], session, ...(server === undefined ? {} : { server: server.name }) };
|
|
61
|
+
mkdirSync(served.dir, { recursive: true });
|
|
62
|
+
const file = join(served.dir, `${id}.jsonl`);
|
|
63
|
+
appendFileSync(file, recordLine({ id, command: run.command, started, session, ...(server === undefined ? {} : { server: server.name }) }));
|
|
64
|
+
const stop = new AbortController();
|
|
65
|
+
runs.add(id, { run, stop, listeners: [], answer: undefined, input: undefined, done: Promise.resolve() });
|
|
66
|
+
const emit = (event) => {
|
|
67
|
+
appendFileSync(file, recordLine(event));
|
|
68
|
+
runs.swap(id, (live) => ({ ...live, run: { ...live.run, events: [...live.run.events, event] } }));
|
|
69
|
+
runs.get(id)?.listeners.forEach((listener) => { listener(event); });
|
|
70
|
+
};
|
|
71
|
+
const io = {
|
|
72
|
+
out: (text) => { emit({ kind: "said", stream: "out", text }); },
|
|
73
|
+
err: (text) => { emit({ kind: "said", stream: "err", text }); },
|
|
74
|
+
ask: (question) => new Promise((resolve) => {
|
|
75
|
+
runs.swap(id, (live) => ({ ...live, answer: (text) => {
|
|
76
|
+
runs.swap(id, (after) => ({ ...after, answer: undefined }));
|
|
77
|
+
emit({ kind: "answered" });
|
|
78
|
+
resolve(text);
|
|
79
|
+
} }));
|
|
80
|
+
emit({ kind: "ask", prompt: question.text, secret: question.kind === "secret", ...(question.kind === "confirm" ? { expect: question.expect } : {}) });
|
|
81
|
+
}),
|
|
82
|
+
output: (chunk, stream) => { emit({ kind: "output", stream, chunk }); },
|
|
83
|
+
input: (deliver) => {
|
|
84
|
+
runs.swap(id, (live) => ({ ...live, input: deliver }));
|
|
85
|
+
return () => { runs.swap(id, (live) => ({ ...live, input: undefined })); };
|
|
86
|
+
},
|
|
87
|
+
size: { columns: settings.columns, rows: settings.rows },
|
|
88
|
+
signal: stop.signal,
|
|
89
|
+
};
|
|
90
|
+
const coloured = plan.value.map((step) => (step.kind === "run" ? { ...step, env: { ...step.env, ANSIBLE_FORCE_COLOR: "1" } } : step));
|
|
91
|
+
const done = interpret(coloured, io, reporting(emit, about(served, call), served.api.short)).catch(() => {
|
|
92
|
+
emit({ kind: "said", stream: "err", text: "The run could not finish; check the local kayra process." });
|
|
93
|
+
emit({ kind: "ended", failures: 1, ms: milliseconds(Date.now() - Date.parse(started)), footer: [] });
|
|
94
|
+
}).finally(() => { runs.swap(id, (live) => ({ ...live, answer: undefined, input: undefined })); });
|
|
95
|
+
runs.swap(id, (live) => ({ ...live, done }));
|
|
96
|
+
return ok(id);
|
|
97
|
+
};
|
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
import { authUrl, sshPort } from "../../core/status/look.js";
|
|
2
|
+
export const ms = (reach) => ("ms" in reach ? reach.ms : null);
|
|
3
|
+
const error = (reach) => ("code" in reach ? reach.code : null);
|
|
4
|
+
/** `kayra status --json`: the shape cli.md documents. */
|
|
5
|
+
export const statusJson = (looks) => ({
|
|
6
|
+
servers: looks.map(({ server, machine, ssh, https }) => ({
|
|
7
|
+
name: server.name,
|
|
8
|
+
kind: server.kind,
|
|
9
|
+
target: server.target,
|
|
10
|
+
domain: server.domain,
|
|
11
|
+
apps: server.apps.map((app) => app.name),
|
|
12
|
+
machine: {
|
|
13
|
+
state: machine.kind,
|
|
14
|
+
address: machine.kind === "found" ? (machine.address ?? null) : null,
|
|
15
|
+
from: machine.kind === "found" ? machine.from : null,
|
|
16
|
+
reason: machine.kind === "unreadable" ? machine.reason : null,
|
|
17
|
+
},
|
|
18
|
+
ssh: { port: Number(sshPort(server)), state: ssh.kind, ms: ms(ssh), error: error(ssh) },
|
|
19
|
+
https: https === undefined
|
|
20
|
+
? null
|
|
21
|
+
: {
|
|
22
|
+
url: `${authUrl(server)}api/health`,
|
|
23
|
+
state: https.kind,
|
|
24
|
+
status: https.kind === "answered" ? https.status : null,
|
|
25
|
+
ms: ms(https),
|
|
26
|
+
error: error(https),
|
|
27
|
+
},
|
|
28
|
+
})),
|
|
29
|
+
});
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
import { overall, tally } from "../../core/validation/findings.js";
|
|
2
|
+
/** `kayra validate --json`: the shape cli.md documents. */
|
|
3
|
+
export const validateJson = (reports) => ({
|
|
4
|
+
result: overall(reports.flatMap((report) => report.findings)),
|
|
5
|
+
...tally(reports),
|
|
6
|
+
servers: reports.map((report) => ({
|
|
7
|
+
name: report.server.name,
|
|
8
|
+
kind: report.server.kind,
|
|
9
|
+
target: report.server.target,
|
|
10
|
+
result: overall(report.findings),
|
|
11
|
+
findings: report.findings.map((each) => ({ result: each.result, text: each.text, hint: each.hint ?? null })),
|
|
12
|
+
})),
|
|
13
|
+
});
|
|
@@ -1,8 +1,10 @@
|
|
|
1
|
-
import { commandOf, kindOf, titleOf } from "../../core/plan.js";
|
|
2
|
-
import { accent, active, bad, muted, strong, warn } from "../../core/
|
|
3
|
-
import { position
|
|
4
|
-
import {
|
|
5
|
-
import {
|
|
1
|
+
import { commandOf, kindOf, titleOf } from "../../core/plan/describe.js";
|
|
2
|
+
import { accent, active, bad, muted, strong, warn } from "../../core/text/spans.js";
|
|
3
|
+
import { position } from "../../core/interpreter/progress.js";
|
|
4
|
+
import { steps as count } from "../../core/interpreter/events.js";
|
|
5
|
+
import { wrap } from "./text.js";
|
|
6
|
+
import { drawnAs } from "./views.js";
|
|
7
|
+
import { typed } from "../../lib/typed.js";
|
|
6
8
|
const kinds = { guard: warn, check: active, run: active, confirm: bad, note: muted };
|
|
7
9
|
const tag = (step) => {
|
|
8
10
|
const kind = kindOf(step);
|
|
@@ -19,19 +21,19 @@ export const explain = (invocation, plan, short, columns) => {
|
|
|
19
21
|
const steps = plan.flatMap((step, index) => {
|
|
20
22
|
const { opens } = position(plan, index);
|
|
21
23
|
const command = commandOf(step);
|
|
22
|
-
return [
|
|
24
|
+
return typed([
|
|
23
25
|
...(opens === undefined ? [] : index === 0 ? [[accent(opens)]] : [[], [accent(opens)]]),
|
|
24
26
|
[" ", muted(String(index + 1).padStart(digits)), " ", tag(step), " ", strong(titleOf(step))],
|
|
25
27
|
...(step.kind === "confirm" ? [[indent, muted(`asks for '${step.expect}' to be typed, or stops`)]] : []),
|
|
26
28
|
...(command === undefined
|
|
27
29
|
? []
|
|
28
|
-
: wrap(short(command).split(" "), Math.max(40, Math.min(columns, 120) - indent.length - 2)).map((line, at) => [indent, muted(at === 0 ? line : ` ${line}`)])),
|
|
30
|
+
: wrap(short(command).split(" "), Math.max(40, Math.min(columns, 120) - indent.length - 2)).map((line, at) => typed([indent, muted(at === 0 ? line : ` ${line}`)]))),
|
|
29
31
|
...(step.kind === "run" && typeof step.stdout === "object" ? [[indent, muted(`→ ${drawnAs(step.stdout)}`)]] : []),
|
|
30
|
-
];
|
|
32
|
+
]);
|
|
31
33
|
});
|
|
32
|
-
return [
|
|
34
|
+
return typed([
|
|
33
35
|
[strong(invocation.command), muted([...invocation.about, count(plan), "dry run: nothing runs"].map((each) => ` · ${each}`).join(""))],
|
|
34
36
|
[],
|
|
35
37
|
...steps,
|
|
36
|
-
];
|
|
38
|
+
]);
|
|
37
39
|
};
|
|
@@ -1,9 +1,12 @@
|
|
|
1
|
-
import { titleOf } from "../../core/plan.js";
|
|
2
|
-
import { accent, active, bad, muted, ok, strong } from "../../core/
|
|
3
|
-
import { footer, steps } from "../../core/
|
|
4
|
-
import { duration
|
|
1
|
+
import { titleOf } from "../../core/plan/describe.js";
|
|
2
|
+
import { accent, active, bad, muted, ok, strong } from "../../core/text/spans.js";
|
|
3
|
+
import { footer, steps } from "../../core/interpreter/events.js";
|
|
4
|
+
import { duration } from "../../core/text/time.js";
|
|
5
|
+
import { spread, truncate } from "./text.js";
|
|
5
6
|
import { paint } from "./paint.js";
|
|
6
7
|
import { escapes, spinner } from "./terminal.js";
|
|
8
|
+
import { milliseconds } from "../../core/fleet/brand.js";
|
|
9
|
+
import { typed } from "../../lib/typed.js";
|
|
7
10
|
/** A run of this long earns a notification when it ends: the operator has looked away. */
|
|
8
11
|
const worthNotifying = 30_000;
|
|
9
12
|
/** 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. */
|
|
@@ -16,14 +19,14 @@ export const framed = (screen, write, invocation) => {
|
|
|
16
19
|
if (when)
|
|
17
20
|
write(raw);
|
|
18
21
|
};
|
|
19
|
-
return {
|
|
22
|
+
return typed({
|
|
20
23
|
plan: async (plan, work) => {
|
|
21
24
|
const started = performance.now();
|
|
22
25
|
tell(escapes.pushTitle + escapes.title(invocation.command), screen.title);
|
|
23
26
|
say([strong(invocation.command), muted([...invocation.about, steps(plan)].map((each) => ` · ${each}`).join(""))]);
|
|
24
27
|
say("");
|
|
25
28
|
const ended = await work();
|
|
26
|
-
const ms = performance.now() - started;
|
|
29
|
+
const ms = milliseconds(performance.now() - started);
|
|
27
30
|
say("");
|
|
28
31
|
footer(invocation, plan, ended, ms).forEach(say);
|
|
29
32
|
tell(escapes.popTitle, screen.title);
|
|
@@ -48,7 +51,7 @@ export const framed = (screen, write, invocation) => {
|
|
|
48
51
|
if (shown)
|
|
49
52
|
say(spread([" ", active("◆"), " ", strong(title)], [muted(counter)], columns));
|
|
50
53
|
const stop = !shown && screen.live
|
|
51
|
-
? spinner(write, (frame, ms) => paint(spread(truncate([" ", active(frame), " ", title], columns - 8), [muted(duration(ms))], columns), screen.colors))
|
|
54
|
+
? spinner(write, (frame, ms) => paint(spread(truncate([" ", active(frame), " ", title], columns - 8), [muted(duration(milliseconds(ms)))], columns), screen.colors))
|
|
52
55
|
: () => undefined;
|
|
53
56
|
const started = performance.now();
|
|
54
57
|
const done = await work();
|
|
@@ -56,9 +59,9 @@ export const framed = (screen, write, invocation) => {
|
|
|
56
59
|
// A diagnosis reports itself on stdout: that line is doctor's output.
|
|
57
60
|
if (step.kind === "check" && step.onFailure === "continue")
|
|
58
61
|
return done;
|
|
59
|
-
const took = duration(performance.now() - started);
|
|
62
|
+
const took = duration(milliseconds(performance.now() - started));
|
|
60
63
|
say(spread([" ", done.failed ? bad("✗") : ok("✓"), " ", title], [muted(done.why === undefined ? took : `${done.why} · ${took}`)], columns));
|
|
61
64
|
return done;
|
|
62
65
|
},
|
|
63
|
-
};
|
|
66
|
+
});
|
|
64
67
|
};
|
|
@@ -1,9 +1,10 @@
|
|
|
1
|
+
import { typed } from "../../lib/typed.js";
|
|
1
2
|
/**
|
|
2
3
|
* The parsed command line as the operation it names. `app rollback` without a
|
|
3
4
|
* commit lists the releases (`app.releases`), with one it rolls back.
|
|
4
5
|
*/
|
|
5
6
|
export const fromCommand = (command) => {
|
|
6
|
-
const plainly = (invocation) => ({ invocation, json: false });
|
|
7
|
+
const plainly = (invocation) => typed({ invocation, json: false });
|
|
7
8
|
switch (command.name) {
|
|
8
9
|
case "up":
|
|
9
10
|
case "provision":
|
|
@@ -37,9 +38,9 @@ export const fromCommand = (command) => {
|
|
|
37
38
|
case "job":
|
|
38
39
|
return plainly({ name: "job", input: { server: command.server, job: command.job, noPrs: command.noPrs } });
|
|
39
40
|
case "status":
|
|
40
|
-
return { invocation: { name: "status", input: { offline: command.offline } }, json: command.json };
|
|
41
|
+
return typed({ invocation: { name: "status", input: { offline: command.offline } }, json: command.json });
|
|
41
42
|
case "validate":
|
|
42
|
-
return { invocation: { name: "validate", input: command.server === undefined ? {} : { server: command.server } }, json: command.json };
|
|
43
|
+
return typed({ invocation: { name: "validate", input: command.server === undefined ? {} : { server: command.server } }, json: command.json });
|
|
43
44
|
case "doctor":
|
|
44
45
|
return plainly({ name: "doctor", input: {} });
|
|
45
46
|
case "certs":
|
|
@@ -2,7 +2,7 @@ import { command, constant, group, map, merge, multiple, object, option, optiona
|
|
|
2
2
|
import { lineBreak, message, text, url } from "@optique/core/message";
|
|
3
3
|
import { argument } from "@optique/core/primitives";
|
|
4
4
|
import { choice, integer, string } from "@optique/core/valueparser";
|
|
5
|
-
import { appNameParser, domainParser, serverNameParser, subdomainParser } from "
|
|
5
|
+
import { appNameParser, domainParser, serverNameParser, subdomainParser } from "./values.js";
|
|
6
6
|
import { operation } from "../../core/operations/catalog.js";
|
|
7
7
|
/** A command's one line, as the catalog says it: the same words in every client. */
|
|
8
8
|
// eslint-disable-next-line functional/prefer-immutable-types -- Optique's Message is readonly only at the top
|
|
@@ -10,18 +10,18 @@ const summary = (name) => message `${text(operation(name).summary)}`;
|
|
|
10
10
|
/** Command lines, each with what it is for, aligned. Plain text: Optique folds an indent that stands alone before a command line. */
|
|
11
11
|
const examples = (indent, rows) => {
|
|
12
12
|
const width = Math.max(...rows.map(([line]) => line.length));
|
|
13
|
-
return rows.flatMap(([line, what], index) => [...(index === 0 ? [] : [lineBreak()]), text(`${indent}${line.padEnd(width)} ${what}`)]);
|
|
13
|
+
return Object.freeze(rows.flatMap(([line, what], index) => Object.freeze([...(index === 0 ? [] : [lineBreak()]), text(`${indent}${line.padEnd(width)} ${what}`)])));
|
|
14
14
|
};
|
|
15
15
|
const docs = message `Docs: ${url("https://yunuseon.github.io/kayra/")}`;
|
|
16
16
|
/** A command's examples; Optique shows a command's footer instead of the program's, so the docs follow. */
|
|
17
|
-
const footer = (...rows) => [
|
|
17
|
+
const footer = (...rows) => Object.freeze([
|
|
18
18
|
...message `Examples:`,
|
|
19
19
|
lineBreak(),
|
|
20
20
|
...examples(" ", rows),
|
|
21
21
|
lineBreak(),
|
|
22
22
|
lineBreak(),
|
|
23
23
|
...docs,
|
|
24
|
-
];
|
|
24
|
+
]);
|
|
25
25
|
const sections = ["On a server", "Apps on a server", "In an app's repository", "The whole fleet", "Options", "Help"];
|
|
26
26
|
/** Arguments (the untitled section) first, then the sections in the order above. */
|
|
27
27
|
const sectionOrder = (a, b) => sections.indexOf(a.title ?? "") - sections.indexOf(b.title ?? "");
|
|
@@ -32,7 +32,7 @@ const usageLine = [
|
|
|
32
32
|
{ type: "optional", terms: [{ type: "option", names: ["-v", "--verbose"] }] },
|
|
33
33
|
];
|
|
34
34
|
/** How kayra presents itself: help in sections, examples, the docs; errors without the full usage above them. */
|
|
35
|
-
export const runOptions = (version) => ({
|
|
35
|
+
export const runOptions = (version) => Object.freeze(({
|
|
36
36
|
programName: "kayra",
|
|
37
37
|
// `kayra help deploy` and `kayra version` work too; listed once, as options.
|
|
38
38
|
help: { option: { names: ["-h", "--help"], group: "Help" }, command: { hidden: "doc" } },
|
|
@@ -57,7 +57,7 @@ export const runOptions = (version) => ({
|
|
|
57
57
|
termWidth: "auto",
|
|
58
58
|
showChoices: true,
|
|
59
59
|
aboveError: "none",
|
|
60
|
-
});
|
|
60
|
+
}));
|
|
61
61
|
/** The arguments kayra takes; server names are the fleet's, so they complete and are checked while parsing. */
|
|
62
62
|
export const parser = (fleet) => {
|
|
63
63
|
const [first, ...rest] = fleet.servers.map((each) => each.name);
|
|
@@ -67,7 +67,7 @@ export const parser = (fleet) => {
|
|
|
67
67
|
const app = argument(appNameParser, { description: message `An app in the server's server.yml.` });
|
|
68
68
|
// eslint-disable-next-line functional/prefer-immutable-types -- Optique's Message is readonly only at the top
|
|
69
69
|
const onServer = (name, brief, more = {}) => command(name, object({ name: constant(name), server }), { brief, ...more });
|
|
70
|
-
return merge(group("Options", object({
|
|
70
|
+
return Object.freeze(merge(group("Options", object({
|
|
71
71
|
dryRun: option("--dry-run", { description: message `Print the plan instead of running it.` }),
|
|
72
72
|
verbose: option("-v", "--verbose", {
|
|
73
73
|
description: message `Ansible's own output (with -v) instead of kayra's summary.`,
|
|
@@ -176,5 +176,5 @@ export const parser = (fleet) => {
|
|
|
176
176
|
}), {
|
|
177
177
|
brief: summary("certs"),
|
|
178
178
|
footer: footer(["kayra certs shop.test www api", "into the fleet's .cache/certs"]),
|
|
179
|
-
})))), (chosen) => ({ command: chosen })));
|
|
179
|
+
})))), (chosen) => ({ command: chosen }))));
|
|
180
180
|
};
|
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
import { createInterface } from "node:readline/promises";
|
|
2
|
+
import { typed } from "../../lib/typed.js";
|
|
2
3
|
/** The command line's questions: secret typing stays unseen, piped answers keep their order. */
|
|
3
4
|
export const questions = (render) => {
|
|
4
5
|
// eslint-disable-next-line functional/prefer-immutable-types -- resources belong to this reader and close with the plan
|
|
@@ -14,7 +15,7 @@ export const questions = (render) => {
|
|
|
14
15
|
const next = await reading.lines.next();
|
|
15
16
|
return next.done === true ? "" : next.value;
|
|
16
17
|
};
|
|
17
|
-
const
|
|
18
|
+
const prompted = (prompt, secret) => {
|
|
18
19
|
const { stdin } = process;
|
|
19
20
|
if (!secret) {
|
|
20
21
|
const terminal = createInterface({ input: stdin, output: process.stderr });
|
|
@@ -24,10 +25,10 @@ export const questions = (render) => {
|
|
|
24
25
|
terminal.close();
|
|
25
26
|
};
|
|
26
27
|
// eslint-disable-next-line functional/immutable-data -- cancellation closes the outstanding question
|
|
27
|
-
reading.close = () => { done(
|
|
28
|
-
terminal.once("SIGINT", () => { done(
|
|
29
|
-
terminal.once("close", () => { resolve(
|
|
30
|
-
void terminal.question(prompt).then(done, () => { done(
|
|
28
|
+
reading.close = () => { done(undefined); };
|
|
29
|
+
terminal.once("SIGINT", () => { done(undefined); });
|
|
30
|
+
terminal.once("close", () => { resolve(undefined); });
|
|
31
|
+
void terminal.question(prompt).then(done, () => { done(undefined); });
|
|
31
32
|
});
|
|
32
33
|
}
|
|
33
34
|
const wasRaw = stdin.isRaw;
|
|
@@ -49,7 +50,7 @@ export const questions = (render) => {
|
|
|
49
50
|
process.stderr.write("\n");
|
|
50
51
|
resolve(answer);
|
|
51
52
|
};
|
|
52
|
-
const ended = () => { done(
|
|
53
|
+
const ended = () => { done(undefined); };
|
|
53
54
|
const key = (chunk) => {
|
|
54
55
|
Array.from(chunk).forEach((char) => {
|
|
55
56
|
if (state.ended)
|
|
@@ -57,7 +58,7 @@ export const questions = (render) => {
|
|
|
57
58
|
if (char === "\r" || char === "\n")
|
|
58
59
|
done(state.text);
|
|
59
60
|
else if (char === "\u0003" || char === "\u0004")
|
|
60
|
-
done(
|
|
61
|
+
done(undefined);
|
|
61
62
|
else if (char === "\u007f" || char === "\b") {
|
|
62
63
|
// eslint-disable-next-line functional/immutable-data -- remove one Unicode character
|
|
63
64
|
state.text = Array.from(state.text).slice(0, -1).join("");
|
|
@@ -75,14 +76,14 @@ export const questions = (render) => {
|
|
|
75
76
|
process.stderr.write(prompt);
|
|
76
77
|
});
|
|
77
78
|
};
|
|
78
|
-
return {
|
|
79
|
+
return typed({
|
|
79
80
|
ask: async (question) => {
|
|
80
81
|
const prompt = render(question.text);
|
|
81
82
|
if (process.stdin.isTTY)
|
|
82
|
-
return
|
|
83
|
+
return prompted(prompt, question.kind === "secret");
|
|
83
84
|
process.stderr.write(prompt);
|
|
84
85
|
return piped();
|
|
85
86
|
},
|
|
86
87
|
close: () => { reading.close?.(); },
|
|
87
|
-
};
|
|
88
|
+
});
|
|
88
89
|
};
|