@yunuseon/kayra 0.17.1 → 0.18.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 +2 -0
- package/agent/skills/kayra-release/SKILL.md +6 -1
- package/ansible/roles/platform/tasks/main.yml +10 -2
- package/cli/dist/clients/api/api.js +6 -4
- package/cli/dist/clients/api/auth.js +1 -1
- package/cli/dist/clients/api/commands.js +1 -49
- package/cli/dist/clients/api/directories.js +1 -1
- package/cli/dist/clients/api/discovery.js +1 -1
- package/cli/dist/clients/api/{contract.js → openapi.js} +3 -52
- package/cli/dist/clients/api/protocol.js +1 -66
- package/cli/dist/clients/api/runs.js +1 -1
- package/cli/dist/clients/api/start.js +3 -3
- package/cli/dist/clients/cli/explain.js +1 -1
- package/cli/dist/clients/cli/framed.js +1 -1
- package/cli/dist/clients/cli/invocation.js +1 -1
- package/cli/dist/clients/cli/parser.js +4 -0
- package/cli/dist/clients/cli/questions.js +1 -1
- package/cli/dist/clients/cli/status.js +1 -1
- package/cli/dist/clients/cli/terminal.js +1 -1
- package/cli/dist/clients/cli/text.js +1 -1
- package/cli/dist/clients/cli/validate.js +1 -1
- package/cli/dist/clients/cli/values.js +1 -1
- package/cli/dist/clients/cli/views.js +1 -1
- package/cli/dist/clients/mcp/run.js +69 -0
- package/cli/dist/clients/mcp/server.js +130 -0
- package/cli/dist/clients/mcp/tools.js +101 -0
- package/cli/dist/clients/wire/contract.js +53 -0
- package/cli/dist/clients/{api → wire}/queries.js +1 -1
- package/cli/dist/clients/wire/schemas.js +67 -0
- package/cli/dist/clients/wire/words.js +48 -0
- package/cli/dist/core/adapters/fs.js +1 -1
- package/cli/dist/core/adapters/network.js +1 -1
- package/cli/dist/core/adapters/opentofu.js +1 -1
- package/cli/dist/core/fleet/brand.js +23 -14
- package/cli/dist/core/fleet/docs.js +2 -2
- package/cli/dist/core/fleet/load.js +3 -2
- package/cli/dist/core/fleet/parse.js +59 -91
- package/cli/dist/core/fleet/settings.js +38 -13
- package/cli/dist/core/fleet/shape.js +5 -20
- package/cli/dist/core/interpreter/events.js +1 -1
- package/cli/dist/core/interpreter/loop.js +1 -1
- package/cli/dist/core/interpreter/needs.js +1 -1
- package/cli/dist/core/interpreter/process.js +2 -2
- package/cli/dist/core/interpreter/progress.js +1 -1
- package/cli/dist/core/interpreter/pty.js +1 -1
- package/cli/dist/core/operations/dispatch.js +2 -2
- package/cli/dist/core/plan/model.js +1 -1
- package/cli/dist/core/recipes/app-check.js +5 -8
- package/cli/dist/core/recipes/apps.js +2 -2
- package/cli/dist/core/recipes/backup.js +1 -1
- package/cli/dist/core/recipes/certs.js +1 -1
- package/cli/dist/core/recipes/doctor.js +1 -1
- package/cli/dist/core/recipes/host.js +2 -2
- package/cli/dist/core/recipes/ssh.js +2 -2
- package/cli/dist/core/recipes/steps.js +2 -2
- package/cli/dist/core/status/look.js +1 -1
- package/cli/dist/core/text/spans.js +1 -1
- package/cli/dist/core/validation/facts.js +3 -2
- package/cli/dist/core/validation/findings.js +3 -3
- package/cli/dist/deployer/app.js +2 -2
- package/cli/dist/deployer/brand.js +1 -1
- package/cli/dist/deployer/build/builder.js +2 -2
- package/cli/dist/deployer/build/bundle.js +2 -2
- package/cli/dist/deployer/build/compose.js +1 -1
- package/cli/dist/deployer/cron.js +1 -1
- package/cli/dist/deployer/ctl/answers.js +1 -1
- package/cli/dist/deployer/deployer.js +7 -11
- package/cli/dist/deployer/forges/forge.js +1 -1
- package/cli/dist/deployer/forges/forgejo.js +1 -1
- package/cli/dist/deployer/forges/github.js +1 -1
- package/cli/dist/deployer/forges/gitlab.js +1 -1
- package/cli/dist/deployer/forges/issues.js +2 -2
- package/cli/dist/deployer/forges/notify.js +2 -2
- package/cli/dist/deployer/runs/queue.js +8 -1
- package/cli/dist/deployer/runs/records.js +1 -1
- package/cli/dist/deployer/runs/run.js +2 -2
- package/cli/dist/deployer/runs/store.js +1 -1
- package/cli/dist/lib/brand.js +2 -0
- package/cli/dist/lib/closest.js +17 -0
- package/cli/dist/lib/schema.js +7 -0
- package/cli/dist/lib/yaml.js +21 -0
- package/cli/dist/main.js +12 -4
- package/package.json +3 -1
- package/schema/app.json +4 -2
- package/schema/server.json +10 -5
- package/scripts/agent/guard.sh +28 -6
- /package/cli/dist/clients/{api → wire}/contract.gen.js +0 -0
- /package/cli/dist/clients/{api → wire}/status.js +0 -0
- /package/cli/dist/clients/{api → wire}/validate.js +0 -0
- /package/cli/dist/{result.js → lib/result.js} +0 -0
- /package/cli/dist/{typed.js → lib/typed.js} +0 -0
|
@@ -0,0 +1,101 @@
|
|
|
1
|
+
import { z } from "zod";
|
|
2
|
+
import { operation, operationNames } from "../../core/operations/catalog.js";
|
|
3
|
+
import { querySchemas, schemas } from "../wire/schemas.js";
|
|
4
|
+
import { typed } from "../../lib/typed.js";
|
|
5
|
+
/**
|
|
6
|
+
* How MCP reaches each operation. The compiler refuses a catalog operation
|
|
7
|
+
* this table leaves out; a tool's name is the operation's, `.` spelled `_`
|
|
8
|
+
* (Claude's API takes no dots in a tool name).
|
|
9
|
+
*/
|
|
10
|
+
export const reaches = {
|
|
11
|
+
up: { tool: "up" },
|
|
12
|
+
provision: { tool: "provision" },
|
|
13
|
+
deploy: { tool: "deploy" },
|
|
14
|
+
setup: { tool: "setup" },
|
|
15
|
+
plan: { tool: "plan" },
|
|
16
|
+
verify: { tool: "verify" },
|
|
17
|
+
apps: { tool: "apps" },
|
|
18
|
+
hosts: { tool: "hosts" },
|
|
19
|
+
ssh: { refused: "an interactive shell, which an agent cannot hold: ssh_exec runs a command" },
|
|
20
|
+
"ssh.exec": { tool: "ssh_exec" },
|
|
21
|
+
down: { tool: "down" },
|
|
22
|
+
adopt: { tool: "adopt" },
|
|
23
|
+
"backup.test": { tool: "backup_test" },
|
|
24
|
+
"app.check": { tool: "app_check" },
|
|
25
|
+
"app.purge": { tool: "app_purge" },
|
|
26
|
+
"app.runs": { tool: "app_runs" },
|
|
27
|
+
"app.log": { tool: "app_log" },
|
|
28
|
+
"app.deploy": { tool: "app_deploy" },
|
|
29
|
+
"app.releases": { tool: "app_releases" },
|
|
30
|
+
"app.rollback": { tool: "app_rollback" },
|
|
31
|
+
job: { tool: "job" },
|
|
32
|
+
status: { tool: "status" },
|
|
33
|
+
validate: { tool: "validate" },
|
|
34
|
+
doctor: { tool: "doctor" },
|
|
35
|
+
certs: { tool: "certs" },
|
|
36
|
+
version: { tool: "version" },
|
|
37
|
+
};
|
|
38
|
+
/** The operations offered as tools, by their tool's name. */
|
|
39
|
+
export const offered = Object.freeze(operationNames.flatMap((name) => {
|
|
40
|
+
const offer = reaches[name];
|
|
41
|
+
return "tool" in offer ? [[offer.tool, name]] : [];
|
|
42
|
+
}));
|
|
43
|
+
export const operationOf = (tool) => offered.find(([name]) => name === tool)?.[1];
|
|
44
|
+
/** What a plan's tool takes besides the operation's input. */
|
|
45
|
+
export const runOptions = z.strictObject({
|
|
46
|
+
dryRun: z.boolean().default(false).describe("List the plan's steps and run nothing"),
|
|
47
|
+
verbose: z.boolean().default(false).describe("Ansible's own output, as with -v"),
|
|
48
|
+
});
|
|
49
|
+
const effectSaid = (each) => {
|
|
50
|
+
if (each.effect === "reads")
|
|
51
|
+
return "Changes nothing.";
|
|
52
|
+
if (each.effect === "destroys")
|
|
53
|
+
return "Irreversible: the operator approves every call, and types the server's name before it runs.";
|
|
54
|
+
return each.scope === "fleet" || each.scope === "repo"
|
|
55
|
+
? "Changes files on this machine."
|
|
56
|
+
: "Changes the server: on one that is not multipass, the operator approves it first.";
|
|
57
|
+
};
|
|
58
|
+
const kindSaid = (each) => each.kind === "plan" ? (each.bare === true ? " Its output is its answer." : " Runs a plan of steps; dryRun lists them and runs nothing.") : "";
|
|
59
|
+
const isSchema = (value) => typeof value === "object" && value !== null && !Array.isArray(value);
|
|
60
|
+
/** A JSON Schema without its dialect: Claude Code reads it as draft 2020-12 either way. */
|
|
61
|
+
// eslint-disable-next-line functional/prefer-immutable-types -- a Zod schema: immutable, a class the rule cannot see into
|
|
62
|
+
const plain = (schema, io) => typed(Object.fromEntries(Object.entries(z.toJSONSchema(schema, { io })).filter(([key]) => key !== "$schema")));
|
|
63
|
+
const optionsSchema = plain(runOptions, "input");
|
|
64
|
+
/** A tool's schemas as MCP has them: an object at the root, its properties plain JSON. */
|
|
65
|
+
const objectSchema = z.looseObject({ type: z.literal("object"), properties: z.record(z.string(), z.json()).optional() });
|
|
66
|
+
/** The input's JSON Schema, with the fleet's servers as the only choices (the command line's choice()), and a plan's options. */
|
|
67
|
+
const inputSchema = (each, servers) => {
|
|
68
|
+
const base = plain(each.input, "input");
|
|
69
|
+
const properties = {
|
|
70
|
+
...(isSchema(base["properties"]) ? base["properties"] : {}),
|
|
71
|
+
...(each.kind === "plan" && isSchema(optionsSchema["properties"]) ? optionsSchema["properties"] : {}),
|
|
72
|
+
};
|
|
73
|
+
const server = properties["server"];
|
|
74
|
+
return objectSchema.parse({ ...base, properties: isSchema(server) && servers.length > 0 ? { ...properties, server: { ...server, enum: servers } } : properties });
|
|
75
|
+
};
|
|
76
|
+
const isQueryName = (name) => Object.hasOwn(querySchemas, name);
|
|
77
|
+
/** An answer's schema, where it is an object: MCP's structured content is one. */
|
|
78
|
+
const outputSchema = (name) => {
|
|
79
|
+
const schema = name === "status" ? schemas.Status : isQueryName(name) ? querySchemas[name] : undefined;
|
|
80
|
+
if (schema === undefined)
|
|
81
|
+
return undefined;
|
|
82
|
+
const json = objectSchema.safeParse(plain(schema, "output"));
|
|
83
|
+
return json.success ? json.data : undefined;
|
|
84
|
+
};
|
|
85
|
+
/** Every tool, as tools/list answers. */
|
|
86
|
+
export const tools = (fleet) => {
|
|
87
|
+
const servers = fleet.servers.map((each) => each.name);
|
|
88
|
+
return Object.freeze(offered.map(([tool, name]) => {
|
|
89
|
+
const each = operation(name);
|
|
90
|
+
const output = outputSchema(name);
|
|
91
|
+
return {
|
|
92
|
+
name: tool,
|
|
93
|
+
title: each.summary,
|
|
94
|
+
description: `${each.summary} ${effectSaid(each)}${kindSaid(each)}`,
|
|
95
|
+
inputSchema: inputSchema(each, servers),
|
|
96
|
+
...(output === undefined ? {} : { outputSchema: output }),
|
|
97
|
+
annotations: { readOnlyHint: each.effect === "reads", destructiveHint: each.effect === "destroys", openWorldHint: each.scope !== "repo" },
|
|
98
|
+
...(each.effect === "destroys" ? { _meta: { "anthropic/requiresUserInteraction": true } } : {}),
|
|
99
|
+
};
|
|
100
|
+
}));
|
|
101
|
+
};
|
|
@@ -0,0 +1,53 @@
|
|
|
1
|
+
import { z } from "zod";
|
|
2
|
+
import { operations, operationNames } from "../../core/operations/catalog.js";
|
|
3
|
+
import { options, schemas } from "./schemas.js";
|
|
4
|
+
import { typed } from "../../lib/typed.js";
|
|
5
|
+
export const catalog = () => Object.freeze(operationNames.map((name) => {
|
|
6
|
+
const { summary, scope, effect, kind, input } = operations[name];
|
|
7
|
+
return { name, summary, scope, effect, kind, input: z.toJSONSchema(input, { io: "input" }) };
|
|
8
|
+
}));
|
|
9
|
+
const isObject = (value) => typeof value === "object" && value !== null;
|
|
10
|
+
const object = (value) => isObject(value) ? value : typed({});
|
|
11
|
+
const list = (value) => typed(Array.isArray(value) ? value : []);
|
|
12
|
+
const literal = (value) => JSON.stringify(value);
|
|
13
|
+
// Recursive: TypeScript cannot infer what it returns.
|
|
14
|
+
const typescript = (value) => {
|
|
15
|
+
const schema = object(value);
|
|
16
|
+
if ("const" in schema)
|
|
17
|
+
return literal(schema["const"]);
|
|
18
|
+
if (Array.isArray(schema["enum"]))
|
|
19
|
+
return schema["enum"].map(literal).join(" | ");
|
|
20
|
+
if (Array.isArray(schema["anyOf"]))
|
|
21
|
+
return schema["anyOf"].map(typescript).join(" | ");
|
|
22
|
+
if (Array.isArray(schema["oneOf"]))
|
|
23
|
+
return schema["oneOf"].map(typescript).join(" | ");
|
|
24
|
+
if (Array.isArray(schema["type"]))
|
|
25
|
+
return schema["type"].map((type) => typescript({ ...schema, type })).join(" | ");
|
|
26
|
+
switch (schema["type"]) {
|
|
27
|
+
case "null": return "null";
|
|
28
|
+
case "string": return "string";
|
|
29
|
+
case "integer":
|
|
30
|
+
case "number": return "number";
|
|
31
|
+
case "boolean": return "boolean";
|
|
32
|
+
case "array": return `readonly (${typescript(schema["items"])})[]`;
|
|
33
|
+
case "object": {
|
|
34
|
+
const properties = Object.entries(object(schema["properties"]));
|
|
35
|
+
const required = list(schema["required"]);
|
|
36
|
+
const fields = properties.map(([key, property]) => `readonly ${literal(key)}${required.includes(key) ? "" : "?"}: ${typescript(property)}`).join("; ");
|
|
37
|
+
if (properties.length === 0 && schema["additionalProperties"] === false)
|
|
38
|
+
return "Readonly<Record<string, never>>";
|
|
39
|
+
return properties.length === 0 ? `Readonly<Record<string, ${typescript(schema["additionalProperties"])}>>` : `{ ${fields} }`;
|
|
40
|
+
}
|
|
41
|
+
default: return "unknown";
|
|
42
|
+
}
|
|
43
|
+
};
|
|
44
|
+
export const contractTypes = () => [
|
|
45
|
+
"// Generated by npm run contract. The pinned kayra version defines this /api contract.",
|
|
46
|
+
...Object.entries(schemas).map(([name, schema]) => `export type ${name} = ${typescript(z.toJSONSchema(schema))};`),
|
|
47
|
+
`export type OperationInputs = { ${operationNames.map((name) => `readonly ${literal(name)}: ${typescript(z.toJSONSchema(operations[name].input, { io: "input" }))}`).join("; ")} };`,
|
|
48
|
+
'export type OperationName = keyof OperationInputs;',
|
|
49
|
+
'export type InputOf<N extends OperationName> = OperationInputs[N];',
|
|
50
|
+
'export type OperationCall = { readonly [N in OperationName]: { readonly name: N; readonly input: InputOf<N> } }[OperationName];',
|
|
51
|
+
`export type RunOptions = ${typescript(z.toJSONSchema(options, { io: "input" }))};`,
|
|
52
|
+
"",
|
|
53
|
+
].join("\n");
|
|
@@ -2,7 +2,7 @@ import { z } from "zod";
|
|
|
2
2
|
import { query } from "../../core/operations/dispatch.js";
|
|
3
3
|
import { validateJson } from "./validate.js";
|
|
4
4
|
import { parsed, releasesSchema, runsSchema } from "../../core/ctl/answers.js";
|
|
5
|
-
import { err, map, ok } from "../../result.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)
|
|
@@ -0,0 +1,67 @@
|
|
|
1
|
+
import { z } from "zod";
|
|
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);
|
|
12
|
+
const span = z.union([z.string(), z.object({ tone: tone.exactOptional(), text: z.string(), href: z.string().exactOptional() })]);
|
|
13
|
+
const line = z.array(span);
|
|
14
|
+
const text = z.union([z.string(), line]);
|
|
15
|
+
const part = z.enum(parts);
|
|
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() });
|
|
17
|
+
const invocation = z.object({ command: z.string(), server: z.string().exactOptional(), about: z.array(z.string()) });
|
|
18
|
+
const stop = z.enum(stopReasons);
|
|
19
|
+
const state = z.enum(["running", "passed", "failed", "interrupted", "declined"]);
|
|
20
|
+
const summary = z.object({ id: z.string(), command: z.string(), server: z.string().exactOptional(), started: z.string(), state, session: z.boolean() });
|
|
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
|
+
const event = z.discriminatedUnion("kind", [
|
|
23
|
+
z.object({ kind: z.literal("plan"), invocation, steps: z.array(listed) }),
|
|
24
|
+
z.object({ kind: z.literal("started"), index: z.number() }),
|
|
25
|
+
z.object({ kind: z.literal("done"), index: z.number(), failed: z.boolean(), stopped: z.boolean(), why: z.string().exactOptional(), ms: z.number() }),
|
|
26
|
+
ended,
|
|
27
|
+
z.object({ kind: z.literal("output"), stream: z.enum(["stdout", "stderr"]), chunk: z.string() }),
|
|
28
|
+
z.object({ kind: z.literal("said"), stream: z.enum(["out", "err"]), text }),
|
|
29
|
+
z.object({ kind: z.literal("ask"), prompt: text, secret: z.boolean(), expect: z.string().exactOptional() }),
|
|
30
|
+
z.object({ kind: z.literal("answered") }),
|
|
31
|
+
]);
|
|
32
|
+
const reach = z.enum(reachStates);
|
|
33
|
+
const verdict = z.enum(verdicts);
|
|
34
|
+
const serverCommand = z.enum(["up", "provision", "deploy", "setup", "plan", "verify", "apps", "down", "adopt", "backup.test"]);
|
|
35
|
+
const serverFacts = z.object({
|
|
36
|
+
name: z.string(), kind: z.enum(serverKinds), target: z.enum(serverTargets), domain: z.string(), apps: z.array(z.string()),
|
|
37
|
+
commands: z.array(z.union([serverCommand, z.literal("job")])),
|
|
38
|
+
});
|
|
39
|
+
export const options = z.strictObject({
|
|
40
|
+
verbose: z.boolean().default(false),
|
|
41
|
+
dryRun: z.boolean().default(false),
|
|
42
|
+
columns: z.number().int().min(1).max(1000).default(120),
|
|
43
|
+
rows: z.number().int().min(1).max(1000).default(32),
|
|
44
|
+
});
|
|
45
|
+
export const schemas = {
|
|
46
|
+
Tone: tone, Span: span, Line: line, Text: text, Part: part, Listed: listed, Invocation: invocation, Stop: stop, RunState: state,
|
|
47
|
+
RunEvent: event, RunSummary: summary,
|
|
48
|
+
Run: summary.extend({ events: z.array(event) }),
|
|
49
|
+
Preview: z.object({ invocation, steps: z.array(listed), changes: z.boolean() }),
|
|
50
|
+
ServerCommand: serverCommand,
|
|
51
|
+
ServerFacts: serverFacts,
|
|
52
|
+
FleetFacts: z.object({ fleet: z.string(), version: z.string(), servers: z.array(serverFacts) }),
|
|
53
|
+
DirectoryListing: z.object({ path: z.string(), parent: z.string(), directories: z.array(z.object({ name: z.string(), path: z.string() })) }),
|
|
54
|
+
ReachState: reach,
|
|
55
|
+
Status: z.object({ servers: z.array(z.object({
|
|
56
|
+
name: z.string(), kind: z.string(), target: z.string(), domain: z.string(), apps: z.array(z.string()),
|
|
57
|
+
machine: z.object({ state: z.enum(machineStates), address: z.string().nullable(), from: z.enum(["inventory", "opentofu"]).nullable(), reason: z.string().nullable() }),
|
|
58
|
+
ssh: z.object({ port: z.number(), state: reach, ms: z.number().nullable(), error: z.string().nullable() }),
|
|
59
|
+
https: z.object({ url: z.string(), state: reach, status: z.number().nullable(), ms: z.number().nullable(), error: z.string().nullable() }).nullable(),
|
|
60
|
+
})) }),
|
|
61
|
+
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() })) })) }),
|
|
62
|
+
AppRuns: runsSchema, AppReleases: releasesSchema,
|
|
63
|
+
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()) }),
|
|
64
|
+
};
|
|
65
|
+
export const querySchemas = {
|
|
66
|
+
validate: schemas.Validation, version: z.string(), "app.runs": schemas.AppRuns, "app.releases": schemas.AppReleases, "app.log": z.string(),
|
|
67
|
+
};
|
|
@@ -0,0 +1,48 @@
|
|
|
1
|
+
/** What follows the server and the app on the command line, by operation. */
|
|
2
|
+
const extra = (call) => {
|
|
3
|
+
switch (call.name) {
|
|
4
|
+
case "ssh.exec":
|
|
5
|
+
return ["--", ...call.input.command];
|
|
6
|
+
case "job":
|
|
7
|
+
return [call.input.job, ...(call.input.noPrs ? ["--no-prs"] : [])];
|
|
8
|
+
case "app.rollback":
|
|
9
|
+
return [call.input.sha];
|
|
10
|
+
case "app.deploy":
|
|
11
|
+
return call.input.ref === undefined ? [] : [call.input.ref];
|
|
12
|
+
case "app.log":
|
|
13
|
+
return call.input.run === undefined ? [] : [call.input.run];
|
|
14
|
+
case "app.check":
|
|
15
|
+
return [call.input.dir];
|
|
16
|
+
case "certs":
|
|
17
|
+
return [call.input.domain, ...call.input.subdomains];
|
|
18
|
+
case "up":
|
|
19
|
+
case "provision":
|
|
20
|
+
case "deploy":
|
|
21
|
+
case "setup":
|
|
22
|
+
case "plan":
|
|
23
|
+
case "verify":
|
|
24
|
+
case "apps":
|
|
25
|
+
case "hosts":
|
|
26
|
+
case "ssh":
|
|
27
|
+
case "down":
|
|
28
|
+
case "adopt":
|
|
29
|
+
case "backup.test":
|
|
30
|
+
case "app.purge":
|
|
31
|
+
case "app.runs":
|
|
32
|
+
case "app.releases":
|
|
33
|
+
case "status":
|
|
34
|
+
case "validate":
|
|
35
|
+
case "doctor":
|
|
36
|
+
case "version":
|
|
37
|
+
return [];
|
|
38
|
+
}
|
|
39
|
+
};
|
|
40
|
+
/** How the command line spells an operation where its words are not its name's. */
|
|
41
|
+
const spelled = { "ssh.exec": "ssh", "app.releases": "app rollback", "app.log": "app logs" };
|
|
42
|
+
/** The command's words, as typed on the command line. */
|
|
43
|
+
export const words = (call) => {
|
|
44
|
+
const { input } = call;
|
|
45
|
+
const scope = ["server" in input ? input.server : undefined, "app" in input ? input.app : undefined];
|
|
46
|
+
const name = spelled[call.name] ?? call.name.replaceAll(".", " ");
|
|
47
|
+
return ["kayra", name, ...scope, ...extra(call)].filter((word) => word !== undefined).join(" ");
|
|
48
|
+
};
|
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import { existsSync, readdirSync, readFileSync } from "node:fs";
|
|
2
|
-
import { typed } from "../../typed.js";
|
|
2
|
+
import { typed } from "../../lib/typed.js";
|
|
3
3
|
/** A file's text; undefined where there is none. */
|
|
4
4
|
export const textAt = (path) => (existsSync(path) ? readFileSync(path, "utf8") : undefined);
|
|
5
5
|
/** The folders in a directory, by name; none where it is missing. */
|
|
@@ -2,7 +2,7 @@ import { Agent, request } from "node:https";
|
|
|
2
2
|
import { connect } from "node:net";
|
|
3
3
|
import { getCACertificates } from "node:tls";
|
|
4
4
|
import { milliseconds } from "../fleet/brand.js";
|
|
5
|
-
import { typed } from "../../typed.js";
|
|
5
|
+
import { typed } from "../../lib/typed.js";
|
|
6
6
|
/** Why a connection failed, by Node's error code. */
|
|
7
7
|
export const failure = (code) => {
|
|
8
8
|
if (code === "ECONNREFUSED")
|
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import { capture } from "../interpreter/process.js";
|
|
2
|
-
import { err, ok } from "../../result.js";
|
|
2
|
+
import { err, ok } from "../../lib/result.js";
|
|
3
3
|
/** Why tofu could not read the state, in a few words: the first line of its error. */
|
|
4
4
|
export const unreadable = (stderr) => {
|
|
5
5
|
if (stderr.includes("tofu init"))
|
|
@@ -1,16 +1,28 @@
|
|
|
1
1
|
import { homedir } from "node:os";
|
|
2
2
|
import { isAbsolute, join, normalize } from "node:path";
|
|
3
|
-
import {
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
export const
|
|
11
|
-
export const
|
|
12
|
-
|
|
13
|
-
|
|
3
|
+
import { z } from "zod";
|
|
4
|
+
import { branded } from "../../lib/brand.js";
|
|
5
|
+
import { err, ok } from "../../lib/result.js";
|
|
6
|
+
import { minting } from "../../lib/schema.js";
|
|
7
|
+
/** A string matching `regex`: brand it, and it is the schema the fleet's files are parsed with and the constructor everything else mints with. */
|
|
8
|
+
// eslint-disable-next-line functional/prefer-immutable-types -- a RegExp, and a Zod schema: immutable once built, but classes the rule cannot see into
|
|
9
|
+
const pattern = (regex, what) => z.string().regex(regex, { error: (issue) => `'${String(issue.input)}' is not ${what}` });
|
|
10
|
+
export const serverNameSchema = pattern(/^[a-z0-9][a-z0-9-]*$/, "a server name (lowercase letters, digits, dashes)").transform(branded());
|
|
11
|
+
export const appNameSchema = pattern(/^[a-z][a-z0-9-]*$/, "an app name (lowercase letters, digits, dashes)").transform(branded());
|
|
12
|
+
export const subdomainSchema = pattern(/^[a-z0-9]([a-z0-9-]*[a-z0-9])?$/, "a subdomain").transform(branded());
|
|
13
|
+
export const domainSchema = pattern(/^([a-z0-9]([a-z0-9-]*[a-z0-9])?\.)+[a-z][a-z0-9-]*$/, "a domain").transform(branded());
|
|
14
|
+
/** A path inside a repository, never above it. */
|
|
15
|
+
export const repoPathSchema = z
|
|
16
|
+
.string()
|
|
17
|
+
.refine((value) => !`/${value}/`.includes("/../"), { error: "path must stay inside its repo" })
|
|
18
|
+
.transform(branded());
|
|
19
|
+
/** Where an app's repository is cloned from: which schemes, the server's target says. */
|
|
20
|
+
export const repoUrlSchema = z.string().transform(branded());
|
|
21
|
+
export const serverName = minting(serverNameSchema);
|
|
22
|
+
export const appName = minting(appNameSchema);
|
|
23
|
+
export const subdomain = minting(subdomainSchema);
|
|
24
|
+
export const domain = minting(domainSchema);
|
|
25
|
+
export const repoPath = minting(repoPathSchema);
|
|
14
26
|
/** `~/…` is the operator's home, as Ansible reads it in vault_password_file. */
|
|
15
27
|
export const absolutePath = (value) => {
|
|
16
28
|
const expanded = value === "~" || value.startsWith("~/") ? join(homedir(), value.slice(1)) : value;
|
|
@@ -36,9 +48,6 @@ export const keyFile = word("a key file", (value) => value);
|
|
|
36
48
|
/** Where ssh listens unless the inventory says otherwise. */
|
|
37
49
|
export const sshDefaultPort = "22";
|
|
38
50
|
export const port = (value) => /^\d+$/.test(value) && Number(value) >= 1 && Number(value) <= 65535 ? ok(value) : err(`'${value}' is not a port`);
|
|
39
|
-
/** HTTPS, or also plain HTTP where `schemes` allow it (a multipass server, a test machine by definition). */
|
|
40
|
-
export const repoUrl = (value, schemes) => schemes.some((scheme) => value.startsWith(scheme)) ? ok(value) : err(`'${value}' is not a ${schemes.join(" or ")} URL`);
|
|
41
|
-
export const repoPath = (value) => `/${value}/`.includes("/../") ? err(`'${value}' leaves its repository`) : ok(value);
|
|
42
51
|
export const remotePath = (value) => value.startsWith("/") ? ok(value) : err(`'${value}' is not an absolute path`);
|
|
43
52
|
export const milliseconds = (value) => Math.max(0, value);
|
|
44
53
|
export const exitCode = (value) => value;
|
|
@@ -1,9 +1,9 @@
|
|
|
1
1
|
import { z } from "zod";
|
|
2
2
|
import { appManifestSchema, appValuesSchema, defaultsSchema, serverKinds, serverSchema, settings, varsSchema, } from "./settings.js";
|
|
3
|
-
import { typed } from "../../typed.js";
|
|
3
|
+
import { typed } from "../../lib/typed.js";
|
|
4
4
|
const version = "https://json-schema.org/draft/2020-12/schema";
|
|
5
5
|
const jsonSchema = (title, schema) => typed({
|
|
6
|
-
...z.toJSONSchema(schema, { target: "draft-2020-12", unrepresentable: "any" }),
|
|
6
|
+
...z.toJSONSchema(schema, { target: "draft-2020-12", unrepresentable: "any", io: "input" }),
|
|
7
7
|
$schema: version,
|
|
8
8
|
title,
|
|
9
9
|
});
|
|
@@ -2,8 +2,9 @@ import { existsSync } from "node:fs";
|
|
|
2
2
|
import { join, sep } from "node:path";
|
|
3
3
|
import { filesIn, foldersIn, textAt } from "../adapters/fs.js";
|
|
4
4
|
import { fleetRoot, serverDir, serverName, within } from "./brand.js";
|
|
5
|
-
import {
|
|
6
|
-
import {
|
|
5
|
+
import { parseServer } from "./parse.js";
|
|
6
|
+
import { fields } from "../../lib/yaml.js";
|
|
7
|
+
import { all, attempt, err, flatMap, map, ok } from "../../lib/result.js";
|
|
7
8
|
import { defaultsSchema } from "./settings.js";
|
|
8
9
|
import { checkSettings, knownSettings } from "./shape.js";
|
|
9
10
|
/** The fleet is the nearest directory, from `from` upwards, with a servers/defaults.yml. */
|
|
@@ -1,66 +1,49 @@
|
|
|
1
|
-
import {
|
|
2
|
-
import {
|
|
3
|
-
import {
|
|
4
|
-
import {
|
|
5
|
-
import {
|
|
6
|
-
import {
|
|
7
|
-
import { typed } from "../../typed.js";
|
|
8
|
-
export const mapping = (value) => typeof value === "object" && value !== null && !Array.isArray(value) ? typed(Object.fromEntries(Object.entries(value))) : undefined;
|
|
9
|
-
/** YAML text as a value; a syntax error is a value too, not an exception. */
|
|
10
|
-
export const yamlOf = (text) => {
|
|
11
|
-
const document = parseDocument(text);
|
|
12
|
-
const problem = document.errors.at(0);
|
|
13
|
-
return problem === undefined ? ok(document.toJS()) : err(`is not valid YAML: ${problem.message.split("\n")[0] ?? ""}`);
|
|
14
|
-
};
|
|
15
|
-
/** YAML text as a mapping; empty for an empty file. */
|
|
16
|
-
export const fields = (text) => {
|
|
17
|
-
const parsed = yamlOf(text);
|
|
18
|
-
if (!parsed.ok)
|
|
19
|
-
return parsed;
|
|
20
|
-
const value = parsed.value;
|
|
21
|
-
if (value === null || value === undefined)
|
|
22
|
-
return ok({});
|
|
23
|
-
const result = mapping(value);
|
|
24
|
-
return result === undefined ? err("is not a YAML mapping") : ok(result);
|
|
25
|
-
};
|
|
1
|
+
import { absolutePath, host, sshDefaultPort, keyFile, port, remotePath, sshUser, vaultPasswordFile } from "./brand.js";
|
|
2
|
+
import { err, map, ok } from "../../lib/result.js";
|
|
3
|
+
import { serverSchema, varsSchema } from "./settings.js";
|
|
4
|
+
import { checkSettings, knownSettings } from "./shape.js";
|
|
5
|
+
import { typed } from "../../lib/typed.js";
|
|
6
|
+
import { fields, mapping } from "../../lib/yaml.js";
|
|
26
7
|
export const isVaulted = (text) => text.startsWith("$ANSIBLE_VAULT");
|
|
27
|
-
const oneOf = (allowed, value, field) => {
|
|
28
|
-
const match = allowed.find((candidate) => candidate === value);
|
|
29
|
-
return match === undefined
|
|
30
|
-
? err(`${field} must be ${allowed.join(", ")}, not '${String(value)}'`)
|
|
31
|
-
: ok(match);
|
|
32
|
-
};
|
|
33
8
|
const vault = (descriptor) => {
|
|
34
|
-
const file = descriptor
|
|
35
|
-
if (descriptor
|
|
9
|
+
const file = descriptor.vault_password_file;
|
|
10
|
+
if (!descriptor.vault)
|
|
36
11
|
return ok({ kind: "none" });
|
|
37
12
|
if (file === undefined)
|
|
38
13
|
return ok({ kind: "ask" });
|
|
39
|
-
return
|
|
40
|
-
|
|
41
|
-
|
|
14
|
+
return map(absolutePath(file), (path) => typed({ kind: "file", file: vaultPasswordFile(path) }));
|
|
15
|
+
};
|
|
16
|
+
/** What Zod's expected types are called in a fleet's YAML. */
|
|
17
|
+
const expected = { string: "a string", boolean: "true or false", array: "a list", object: "a mapping" };
|
|
18
|
+
/** What the file says at a path Zod reports: Zod's issues leave the input out. */
|
|
19
|
+
const written = (value, path) => {
|
|
20
|
+
// Recursive: a path one key at a time.
|
|
21
|
+
const [key, ...rest] = path;
|
|
22
|
+
if (key === undefined)
|
|
23
|
+
return value;
|
|
24
|
+
const next = Array.isArray(value) && typeof key === "number" ? value[key] : mapping(value)?.[String(key)];
|
|
25
|
+
return written(next, rest);
|
|
42
26
|
};
|
|
43
27
|
/**
|
|
44
|
-
*
|
|
45
|
-
*
|
|
46
|
-
*
|
|
28
|
+
* The first thing wrong with server.yml, said as the operator wrote it: an
|
|
29
|
+
* app by its name, a missing key as required. The schema's own messages are
|
|
30
|
+
* whole sentences already.
|
|
47
31
|
*/
|
|
48
|
-
const
|
|
49
|
-
const
|
|
50
|
-
if (
|
|
51
|
-
return
|
|
52
|
-
const
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
const
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
return
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
return ok({ name: name.value, repo: repo.value, path: path.value, entry });
|
|
32
|
+
const serverProblem = (error, descriptor) => {
|
|
33
|
+
const issue = error.issues.at(0);
|
|
34
|
+
if (issue === undefined)
|
|
35
|
+
return "server.yml is invalid";
|
|
36
|
+
const [first, index] = issue.path;
|
|
37
|
+
const name = index === undefined ? undefined : written(descriptor, ["apps", index, "name"]);
|
|
38
|
+
const app = first === "apps" && typeof index === "number" ? `app ${typeof name === "string" ? name : `#${String(index + 1)}`}: ` : "";
|
|
39
|
+
const key = String(issue.path.at(-1) ?? "");
|
|
40
|
+
if (issue.code === "unrecognized_keys") {
|
|
41
|
+
const where = issue.path.length === 0 ? "" : ` (in ${issue.path.join(".")})`;
|
|
42
|
+
return `server.yml: '${issue.keys.at(0) ?? ""}' is not a key it knows${where}`;
|
|
43
|
+
}
|
|
44
|
+
if (issue.code === "invalid_type")
|
|
45
|
+
return `${app}${key} ${written(descriptor, issue.path) === undefined ? "is required" : `must be ${expected[issue.expected] ?? issue.expected}`}`;
|
|
46
|
+
return `${app}${issue.message}`;
|
|
64
47
|
};
|
|
65
48
|
const inventoryField = (inventory, key) => new RegExp(`${key}=([^ \\n]*)`).exec(inventory)?.[1] ?? "";
|
|
66
49
|
/** A field the inventory may leave out: undefined where it does. */
|
|
@@ -101,33 +84,16 @@ export const parseServer = (files) => {
|
|
|
101
84
|
const descriptor = fields(files.descriptor);
|
|
102
85
|
if (!descriptor.ok)
|
|
103
86
|
return descriptor;
|
|
104
|
-
const
|
|
105
|
-
if (!
|
|
106
|
-
return
|
|
107
|
-
const
|
|
108
|
-
|
|
109
|
-
return target;
|
|
110
|
-
const declared = descriptor.value["domain"];
|
|
111
|
-
const serverDomain = typeof declared === "string" ? domain(declared) : err("domain is required");
|
|
112
|
-
if (!serverDomain.ok)
|
|
113
|
-
return serverDomain;
|
|
114
|
-
const serverVault = vault(descriptor.value);
|
|
87
|
+
const parsed = serverSchema.safeParse(descriptor.value);
|
|
88
|
+
if (!parsed.success)
|
|
89
|
+
return err(serverProblem(parsed.error, descriptor.value));
|
|
90
|
+
const { kind, target, domain, apps } = parsed.data;
|
|
91
|
+
const serverVault = vault(parsed.data);
|
|
115
92
|
if (!serverVault.ok)
|
|
116
93
|
return serverVault;
|
|
117
|
-
|
|
118
|
-
if (!Array.isArray(declaredApps))
|
|
119
|
-
return err("apps must be a list");
|
|
120
|
-
if (kind.value !== "platform" && declaredApps.length > 0)
|
|
121
|
-
return err("apps run on platform servers only");
|
|
122
|
-
const apps = all(declaredApps.map(app(target.value)));
|
|
123
|
-
if (!apps.ok)
|
|
124
|
-
return apps;
|
|
125
|
-
const shape = checkShape("server.yml", descriptor.value, serverSchema);
|
|
126
|
-
if (!shape.ok)
|
|
127
|
-
return shape;
|
|
128
|
-
if (target.value === "home" && files.inventory === undefined)
|
|
94
|
+
if (target === "home" && files.inventory === undefined)
|
|
129
95
|
return err("target home needs a hand-written inventory.ini next to it");
|
|
130
|
-
if (target
|
|
96
|
+
if (target !== "home" && !files.hasTerraform)
|
|
131
97
|
return err("no terraform/ next to it");
|
|
132
98
|
if (files.vars === undefined)
|
|
133
99
|
return err("no vars.yml next to it");
|
|
@@ -135,17 +101,17 @@ export const parseServer = (files) => {
|
|
|
135
101
|
return err("no secrets.yml next to it");
|
|
136
102
|
if (isVaulted(files.vars))
|
|
137
103
|
return err("vars.yml is vault-encrypted; it holds settings only — secrets go in secrets.yml");
|
|
138
|
-
if (target
|
|
139
|
-
return err(`target ${target
|
|
140
|
-
if (target
|
|
141
|
-
return err(`target ${target
|
|
142
|
-
const appValues = appValuesProblem(files.appValues, target
|
|
104
|
+
if (target !== "multipass" && serverVault.value.kind === "none")
|
|
105
|
+
return err(`target ${target} requires vault: true`);
|
|
106
|
+
if (target !== "multipass" && !isVaulted(files.secrets))
|
|
107
|
+
return err(`target ${target} requires a vault-encrypted secrets.yml`);
|
|
108
|
+
const appValues = appValuesProblem(files.appValues, target !== "multipass");
|
|
143
109
|
if (appValues !== undefined)
|
|
144
110
|
return err(appValues);
|
|
145
111
|
const vars = fields(files.vars);
|
|
146
112
|
if (!vars.ok)
|
|
147
113
|
return err(`vars.yml ${vars.error}`);
|
|
148
|
-
const varsChecked = checkSettings("vars.yml", vars.value, varsSchema(kind
|
|
114
|
+
const varsChecked = checkSettings("vars.yml", vars.value, varsSchema(kind), knownSettings(kind));
|
|
149
115
|
if (!varsChecked.ok)
|
|
150
116
|
return varsChecked;
|
|
151
117
|
const backup = mapping(vars.value["restic_backup"])?.["dir"];
|
|
@@ -155,21 +121,23 @@ export const parseServer = (files) => {
|
|
|
155
121
|
const inventory = files.inventory === undefined ? ok(undefined) : inventoryOf(files.inventory);
|
|
156
122
|
if (!inventory.ok)
|
|
157
123
|
return inventory;
|
|
124
|
+
const declared = descriptor.value["apps"];
|
|
125
|
+
const entries = Array.isArray(declared) ? declared : [];
|
|
158
126
|
return ok({
|
|
159
127
|
name: files.name,
|
|
160
128
|
dir: files.dir,
|
|
161
|
-
kind
|
|
162
|
-
target
|
|
163
|
-
domain
|
|
129
|
+
kind,
|
|
130
|
+
target,
|
|
131
|
+
domain,
|
|
164
132
|
vault: serverVault.value,
|
|
165
133
|
hasVaultedSecrets: isVaulted(files.secrets),
|
|
166
|
-
apps: apps.
|
|
134
|
+
apps: apps.map((app, index) => typed({ name: app.name, repo: app.repo, path: app.path, entry: mapping(entries[index]) ?? {} })),
|
|
167
135
|
hasTerraform: files.hasTerraform,
|
|
168
|
-
buildsVm: target
|
|
136
|
+
buildsVm: target === "home" && vars.value["vm"] !== undefined,
|
|
169
137
|
usesAcmeDns: typeof vars.value["acme_dns"] === "string" && vars.value["acme_dns"] !== "",
|
|
170
138
|
inventory: inventory.value,
|
|
171
139
|
backupDir: backupDir.value,
|
|
172
|
-
stackHosts: stackHosts(files.stacks.flatMap((stack) => Object.freeze(stack.compose ?? [])),
|
|
140
|
+
stackHosts: stackHosts(files.stacks.flatMap((stack) => Object.freeze(stack.compose ?? [])), domain),
|
|
173
141
|
vars: vars.value,
|
|
174
142
|
files: { vars: files.vars, secrets: files.secrets, stacks: files.stacks.map((stack) => stack.name), appValues: files.appValues },
|
|
175
143
|
});
|