@yunuseon/kayra 0.17.2 → 0.19.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.
Files changed (55) hide show
  1. package/README.md +2 -0
  2. package/agent/skills/kayra-rollout/SKILL.md +3 -2
  3. package/ansible/playbooks/edge.yml +5 -0
  4. package/ansible/playbooks/nas.yml +9 -1
  5. package/ansible/playbooks/platform.yml +8 -2
  6. package/ansible/roles/app/tasks/main.yml +11 -3
  7. package/ansible/roles/kayra-version/defaults/main.yml +5 -0
  8. package/ansible/roles/kayra-version/tasks/main.yml +18 -0
  9. package/ansible/roles/platform/tasks/main.yml +6 -4
  10. package/cli/dist/browser/assets/index-C4HCL022.js +7 -0
  11. package/cli/dist/browser/index.html +1 -1
  12. package/cli/dist/clients/api/api.js +6 -4
  13. package/cli/dist/clients/api/commands.js +0 -48
  14. package/cli/dist/clients/api/{contract.js → openapi.js} +2 -51
  15. package/cli/dist/clients/api/protocol.js +1 -66
  16. package/cli/dist/clients/api/start.js +1 -1
  17. package/cli/dist/clients/cli/parser.js +4 -0
  18. package/cli/dist/clients/cli/status.js +28 -7
  19. package/cli/dist/clients/mcp/run.js +69 -0
  20. package/cli/dist/clients/mcp/server.js +130 -0
  21. package/cli/dist/clients/mcp/tools.js +101 -0
  22. package/cli/dist/clients/wire/contract.js +53 -0
  23. package/cli/dist/clients/wire/schemas.js +68 -0
  24. package/cli/dist/clients/{api → wire}/status.js +7 -1
  25. package/cli/dist/clients/wire/words.js +48 -0
  26. package/cli/dist/core/adapters/network.js +20 -1
  27. package/cli/dist/core/fleet/brand.js +5 -0
  28. package/cli/dist/core/fleet/parse.js +10 -1
  29. package/cli/dist/core/fleet/settings.js +18 -3
  30. package/cli/dist/core/operations/dispatch.js +3 -3
  31. package/cli/dist/core/recipes/doctor.js +3 -3
  32. package/cli/dist/core/recipes/host.js +8 -3
  33. package/cli/dist/core/status/look.js +33 -13
  34. package/cli/dist/core/status/model.js +2 -0
  35. package/cli/dist/deployer/brand.js +5 -0
  36. package/cli/dist/deployer/build/builder.js +5 -0
  37. package/cli/dist/deployer/build/bundle.js +5 -0
  38. package/cli/dist/deployer/build/compose.js +21 -8
  39. package/cli/dist/deployer/build/routes.js +128 -0
  40. package/cli/dist/deployer/deployer.js +15 -13
  41. package/cli/dist/deployer/runs/queue.js +7 -0
  42. package/cli/dist/deployer/runs/run.js +14 -7
  43. package/cli/dist/main.js +11 -3
  44. package/package.json +3 -1
  45. package/schema/app.json +64 -8
  46. package/scripts/agent/guard.sh +28 -6
  47. package/scripts/local/app-check.sh +12 -9
  48. package/scripts/local/app-manifests.sh +11 -6
  49. package/scripts/local/verify.sh +22 -11
  50. package/scripts/platform/compose-policy.sh +37 -40
  51. package/cli/dist/browser/assets/index-hcyM6f6T.js +0 -7
  52. package/compose/traefik/dynamic/ratelimit.yml +0 -21
  53. /package/cli/dist/clients/{api → wire}/contract.gen.js +0 -0
  54. /package/cli/dist/clients/{api → wire}/queries.js +0 -0
  55. /package/cli/dist/clients/{api → wire}/validate.js +0 -0
@@ -1,6 +1,6 @@
1
1
  import { z } from "zod";
2
2
  import { typed } from "../../lib/typed.js";
3
- import { appNameSchema, domainSchema, repoPathSchema, repoUrlSchema, subdomainSchema } from "./brand.js";
3
+ import { appNameSchema, domainSchema, periodSchema, repoPathSchema, repoUrlSchema, routePathSchema, serviceNameSchema, subdomainSchema } from "./brand.js";
4
4
  import { serverTargets } from "./model.js";
5
5
  /** The server kinds, each with its playbook; settings say which kinds read them. */
6
6
  export const serverKinds = ["platform", "edge", "nas"];
@@ -157,11 +157,26 @@ export const serverSchema = z
157
157
  });
158
158
  });
159
159
  });
160
+ /** One of an app's public routes: the platform writes its Traefik router, the app no label. */
161
+ const routeSchema = z.strictObject({
162
+ host: subdomainSchema.describe("Its hostname, <host>.<domain>. The app's hosts are its routes' hosts."),
163
+ path: routePathSchema.optional().describe("A path prefix: the route answers below it, and beats its host's route without one."),
164
+ service: serviceNameSchema.describe("The compose service that answers it."),
165
+ port: z.number().int().min(1).max(65535).describe("The port that service listens on, inside its container."),
166
+ rate_limit: z
167
+ .strictObject({
168
+ average: z.number().int().positive().describe("Requests a client may send per period, on average."),
169
+ period: periodSchema.describe("The period, such as 10s, 10m or 1h."),
170
+ burst: z.number().int().positive().describe("Requests a client may send at once beyond the average."),
171
+ })
172
+ .optional()
173
+ .describe("A rate limit on this route, per client address (Traefik's rateLimit)."),
174
+ });
160
175
  /** app.yml, at the root of an app's repository: the app contract. */
161
176
  export const appManifestSchema = z.strictObject({
162
177
  name: appNameSchema.describe("The app's name, as in the server.yml entry."),
163
- compose: z.array(z.string()).describe("Compose files, relative to the app root, in order."),
164
- subdomains: z.array(subdomainSchema).describe("Hostnames the app claims: <sub>.<domain>."),
178
+ compose: z.array(z.string()).describe("Compose files, relative to the app root, in order. No Traefik label, no platform network: routes say how the app is reached."),
179
+ routes: z.array(routeSchema).optional().describe("How the app is reached: each route a host (and path) to a service's port. Read at the commit being deployed; a new host needs kayra apps."),
165
180
  secrets: z.array(z.string()).optional().describe("Secret keys; their values come from the fleet's apps/<app>.secrets.yml."),
166
181
  config: z.array(z.string()).optional().describe("Config keys; their values come from the fleet's apps/<app>.yml."),
167
182
  });
@@ -19,7 +19,7 @@ import { resolve as substitute, spawned } from "../interpreter/process.js";
19
19
  /** The context for a fleet, with the files read from disk: what every client runs the core with. */
20
20
  export const contextFor = (parts) => {
21
21
  const { kayra, ...rest } = parts;
22
- return typed({ ...rest, engine: { kayra, fleet: parts.fleet.root }, read: textAt });
22
+ return typed({ ...rest, engine: { kayra, fleet: parts.fleet.root, version: parts.version }, read: textAt });
23
23
  };
24
24
  /** Every operation with its input: what a client sends, parsed in one go. */
25
25
  const invocations = z.discriminatedUnion("name", [
@@ -94,7 +94,7 @@ const plans = {
94
94
  "app.releases": onDeployer((app) => ({ kind: "rollback", app, more: undefined })),
95
95
  "app.rollback": onDeployer((app, input) => ({ kind: "rollback", app, more: optional(input["sha"]) })),
96
96
  job: (ctx, input) => flatMap(onServer(ctx, input.server), (target) => deployer(target, { kind: "job", job: input.job, noPrs: input.noPrs })),
97
- doctor: (ctx) => ok(doctor(ctx.fleet, ctx.engine.kayra)),
97
+ doctor: (ctx) => ok(doctor(ctx.fleet, ctx.engine)),
98
98
  certs: (ctx, input) => flatMap(domain(input.domain), (named) => flatMap(all(input.subdomains.map(subdomain)), (subdomains) => certsFor(ctx.engine, named, subdomains))),
99
99
  };
100
100
  export const planOf = (ctx, call) => plans[call.name](ctx, call.input);
@@ -129,7 +129,7 @@ const queries = {
129
129
  };
130
130
  export const query = (ctx, name, input) => queries[name](ctx, input);
131
131
  const streams = {
132
- status: (ctx, input) => look(ctx.fleet, ctx.tofu, input.offline ? undefined : ctx.network),
132
+ status: (ctx, input) => look(ctx.fleet, ctx.version, ctx.tofu, input.offline ? undefined : ctx.network),
133
133
  };
134
134
  /** A stream's first state and the updates still to land. */
135
135
  export const stream = (ctx, name, input) => streams[name](ctx, input);
@@ -54,11 +54,11 @@ const repoChecks = (fleet) => Object.freeze([...new Set(fleet.servers.flatMap((s
54
54
  hint: "gh auth login && gh auth setup-git (or git credentials of your own for that host)",
55
55
  onFailure: "continue",
56
56
  })));
57
- const reachChecks = (fleet, kayra) => Object.freeze(fleet.servers.filter((server) => server.target === "home").map((server) => homeReach({ kayra, fleet: fleet.root }, server, "continue")));
57
+ const reachChecks = (fleet, engine) => Object.freeze(fleet.servers.filter((server) => server.target === "home").map((server) => homeReach(engine, server, "continue")));
58
58
  /** Is this Mac ready to run every command on every server? Each check reports; none stops the others. */
59
- export const doctor = (fleet, kayra) => typed([
59
+ export const doctor = (fleet, engine) => typed([
60
60
  ...toolChecks(fleet),
61
61
  ...vaultChecks(fleet),
62
62
  ...repoChecks(fleet),
63
- ...reachChecks(fleet, kayra),
63
+ ...reachChecks(fleet, engine),
64
64
  ]);
@@ -14,7 +14,7 @@ export const provision = (engine, server) => ok(partOf("provision", [
14
14
  ]
15
15
  : []),
16
16
  ]));
17
- /** The playbook for the server's kind, as setup runs it; plan adds check mode. */
17
+ /** The playbook for the server's kind, as setup runs it; plan adds check mode. It records which kayra ran it (the kayra-version role). */
18
18
  const hostPlaybook = (engine, server, extra = []) => vaulted(run(`Host layer · ${server.kind}.yml${extra.includes("--check") ? ", check mode" : ""}`, [
19
19
  "ansible-playbook",
20
20
  "-i",
@@ -28,6 +28,8 @@ const hostPlaybook = (engine, server, extra = []) => vaulted(run(`Host layer ·
28
28
  `domain=${server.domain}`,
29
29
  "-e",
30
30
  `target=${server.target}`,
31
+ "-e",
32
+ `kayra_version=${engine.version}`,
31
33
  ...extra,
32
34
  ], ansibleEnv(engine, {})), server);
33
35
  /** The host layer: the playbook for the server's kind. */
@@ -70,9 +72,12 @@ export const hosts = (engine, server) => ok(partOf("hosts", server.target === "m
70
72
  ]), [{ kind: "sudo" }])
71
73
  ]
72
74
  : [{ kind: "note", text: `hosts: ${server.target} uses real DNS, nothing to do.` }]));
73
- /** Whether a platform server works as its users see it (scripts/local/verify.sh); other kinds have nothing to check yet. */
75
+ /**
76
+ * Whether a platform server works as its users see it (scripts/local/verify.sh),
77
+ * and runs the kayra this is; other kinds have nothing to check yet.
78
+ */
74
79
  const verifyStep = (engine, server) => typed({
75
- ...run("Verify", ["bash", script(engine, "verify.sh"), server.name], fleetEnv(engine)),
80
+ ...run("Verify", ["bash", script(engine, "verify.sh"), server.name], { ...fleetEnv(engine), KAYRA_VERSION: engine.version }),
76
81
  part: "verify",
77
82
  });
78
83
  export const verify = (engine, server) => server.kind === "platform"
@@ -1,4 +1,4 @@
1
- import { host, sshDefaultPort, within } from "../fleet/brand.js";
1
+ import { host, kayraVersion, sshDefaultPort, within } from "../fleet/brand.js";
2
2
  import { isMachine } from "./model.js";
3
3
  import { typed } from "../../lib/typed.js";
4
4
  /** The inventory names the address: hand-written at home, written by provision elsewhere. */
@@ -22,34 +22,54 @@ export const sshPort = (server) => server.inventory?.port ?? sshDefaultPort;
22
22
  export const authUrl = (server) => `https://auth.${server.domain}/`;
23
23
  const pending = { kind: "pending" };
24
24
  const skipped = { kind: "skipped" };
25
+ const notAsked = { kind: "skipped" };
26
+ /** What the server's version file holds, as a version: none where setup never recorded one. */
27
+ const running = (read) => {
28
+ if (!read.ok)
29
+ return typed({ kind: "unreadable", reason: read.error });
30
+ const text = read.value.trim();
31
+ if (text === "")
32
+ return typed({ kind: "unrecorded" });
33
+ const version = kayraVersion(text);
34
+ return version.ok ? typed({ kind: "recorded", version: version.value }) : typed({ kind: "unreadable", reason: version.error });
35
+ };
36
+ /** Which kayra set the server up, once SSH answered: its version file, over SSH as the inventory says. */
37
+ const recorded = (server, network, ssh) => ssh.kind === "open" && server.inventory !== undefined && server.versionFile !== undefined
38
+ ? network.read(server.inventory, server.versionFile).then(running)
39
+ : Promise.resolve(notAsked);
25
40
  const at = (index, change) => (looks) => typed(looks.map((look, each) => (each === index ? { ...look, ...change } : look)));
41
+ /** What the network answers: SSH, then the version file once SSH is open; on a platform server, Authelia's health. */
42
+ const probes = (server, index, machine, network) => {
43
+ const ssh = machine.then((found) => found.kind === "found" && found.address !== undefined ? network.connect(found.address, sshPort(server)) : skipped);
44
+ return typed([
45
+ ssh.then((answer) => at(index, { ssh: answer })),
46
+ ssh.then((answer) => recorded(server, network, answer)).then((answer) => at(index, { kayra: answer })),
47
+ ...(server.kind === "platform" ? [network.get(`${authUrl(server)}api/health`).then((answer) => at(index, { https: answer }))] : []),
48
+ ]);
49
+ };
26
50
  /**
27
51
  * Every server as far as the fleet's files tell, and the answers still to
28
52
  * come, each an update to it: OpenTofu's state where no inventory names the
29
- * address, then a TCP connect to SSH, and on platform servers HTTPS on
30
- * auth.<domain> (Authelia's health), all at once. Without a network
31
- * (--offline), only OpenTofu.
53
+ * address, then a TCP connect to SSH and, once it is open, the version file
54
+ * its setup wrote, over SSH; on platform servers HTTPS on auth.<domain>
55
+ * (Authelia's health), all at once. Without a network (--offline), only
56
+ * OpenTofu.
32
57
  */
33
- export const look = (fleet, tofu, network) => fleet.servers.reduce((so, server, index) => {
58
+ export const look = (fleet, pin, tofu, network) => fleet.servers.reduce((so, server, index) => {
34
59
  const known = listed(server);
35
60
  const machine = known === undefined ? provisioned(server, tofu) : Promise.resolve(known);
36
61
  const platform = server.kind === "platform";
37
62
  const updates = [
38
63
  ...(known === undefined ? [machine.then((answer) => at(index, { machine: answer }))] : []),
39
- ...(network === undefined
40
- ? []
41
- : [
42
- machine
43
- .then((found) => found.kind === "found" && found.address !== undefined ? network.connect(found.address, sshPort(server)) : skipped)
44
- .then((answer) => at(index, { ssh: answer })),
45
- ...(platform ? [network.get(`${authUrl(server)}api/health`).then((answer) => at(index, { https: answer }))] : []),
46
- ]),
64
+ ...(network === undefined ? [] : probes(server, index, machine, network)),
47
65
  ];
48
66
  const first = {
49
67
  server,
50
68
  machine: known ?? pending,
51
69
  ssh: network === undefined ? skipped : pending,
52
70
  https: platform ? (network === undefined ? skipped : pending) : undefined,
71
+ kayra: network === undefined ? notAsked : pending,
72
+ pin,
53
73
  };
54
74
  return { initial: [...so.initial, first], updates: [...so.updates, ...updates] };
55
75
  }, { initial: [], updates: [] });
@@ -1,5 +1,7 @@
1
1
  /** Every state a probe's cell can be in, as the API names them. */
2
2
  export const reachStates = ["pending", "open", "answered", "refused", "no-answer", "unreachable", "unknown-host", "bad-certificate", "failed", "skipped"];
3
+ /** Every state the running kayra's cell can be in. */
4
+ export const runningStates = ["pending", "recorded", "unrecorded", "unreadable", "skipped"];
3
5
  /** Every state a machine's cell can be in. */
4
6
  export const machineStates = ["pending", "found", "not-provisioned", "no-machine", "unreadable"];
5
7
  /** A machine kayra's modules create, by its resource type; a data source is none. */
@@ -3,6 +3,11 @@ export const appName = (value) => /^[a-z][a-z0-9-]*$/.test(value) ? ok(value) :
3
3
  export const sha = (value) => /^[0-9a-f]{40}$/.test(value) ? ok(value) : err(`'${value}' is not a full commit id`);
4
4
  export const runId = (value) => /^\d{8}T\d{6}Z-[0-9a-f]{6}$/.test(value) ? ok(value) : err(`'${value}' is not a run id`);
5
5
  export const newRunId = (now, random) => `${now.toISOString().replace(/[-:]/g, "").replace(/\.\d+Z$/, "Z")}-${random}`;
6
+ export const subdomain = (value) => /^[a-z0-9]([a-z0-9-]*[a-z0-9])?$/.test(value) ? ok(value) : err(`'${value}' is not a subdomain`);
7
+ export const serviceName = (value) => /^[a-zA-Z0-9][a-zA-Z0-9_.-]*$/.test(value) ? ok(value) : err(`'${value}' is not a compose service name`);
8
+ export const port = (value) => Number.isInteger(value) && value >= 1 && value <= 65535 ? ok(value) : err(`${String(value)} is not a port`);
9
+ export const routePath = (value) => /^\/[A-Za-z0-9._~%/-]*$/.test(value) ? ok(value) : err(`'${value}' is not a path (/, then letters, digits and ._~%/-)`);
10
+ export const period = (value) => /^[1-9][0-9]*(ms|s|m|h)$/.test(value) ? ok(value) : err(`'${value}' is not a period such as 10s, 10m or 1h`);
6
11
  export const appOwner = (app) => app;
7
12
  export const jobOwner = (job) => `_${job}`;
8
13
  export const owner = (value) => /^(_gc|_renovate|[a-z][a-z0-9-]*)$/.test(value) ? ok(value) : err(`'${value}' has no runs`);
@@ -103,10 +103,15 @@ export const prepare = async (app, commit, s, line) => {
103
103
  const missing = files.find((file) => !existsSync(join(root, file)));
104
104
  if (missing !== undefined)
105
105
  return err(`app.yml: compose file ${missing} does not exist at ${commit}`);
106
+ const routes = await run(["yq", "-o=json", ".routes // []", join(root, "app.yml")], { capture: true }, line);
107
+ if (!routes.ok)
108
+ return routes;
109
+ const routed = JSON.parse(routes.value);
106
110
  return ok({
107
111
  sha: commit,
108
112
  images: artifacts.map(([, variable, ref]) => ({ variable, ref })),
109
113
  compose: files.map((file) => ({ path: file, content: readFileSync(join(root, file), "utf8") })),
114
+ routes: routed,
110
115
  });
111
116
  };
112
117
  const hook = async (s, name, request, response) => {
@@ -2,6 +2,7 @@ import { err, ok } from "../../lib/result.js";
2
2
  import { field } from "../app.js";
3
3
  import { sha } from "../brand.js";
4
4
  import { typed } from "../../lib/typed.js";
5
+ import { parseRoutes } from "./routes.js";
5
6
  const relative = (path) => path !== "" && !path.startsWith("/") && !path.split("/").some((segment) => segment === ".." || segment === "");
6
7
  export const parseBundle = (app, expected, json) => {
7
8
  if (typeof json !== "object" || json === null)
@@ -40,10 +41,14 @@ export const parseBundle = (app, expected, json) => {
40
41
  const problem = [...parsedImages, ...parsedCompose].find((item) => typeof item === "string");
41
42
  if (problem !== undefined)
42
43
  return err(`bundle: ${problem}`);
44
+ const routes = parseRoutes(field(json, "routes"));
45
+ if (!routes.ok)
46
+ return err(`bundle: ${routes.error}`);
43
47
  return ok({
44
48
  sha: minted.value,
45
49
  images: parsedImages.filter((item) => typeof item !== "string"),
46
50
  compose: parsedCompose.filter((item) => typeof item !== "string"),
51
+ routes: routes.value,
47
52
  });
48
53
  };
49
54
  /** images.env, as compose reads it next to the app's own .env. */
@@ -1,4 +1,13 @@
1
+ import { wireRoutes } from "./routes.js";
1
2
  import { typed } from "../../lib/typed.js";
3
+ /**
4
+ * The run's directory: the app's compose files under app/, at their paths in
5
+ * the app; the platform's files beside it, where no app file can stand in for
6
+ * them.
7
+ */
8
+ export const appFiles = "app";
9
+ export const imagesFile = "images.env";
10
+ export const routesFile = "routes.yml";
2
11
  /**
3
12
  * The app's compose project, run in the run's directory: its .env from the
4
13
  * platform (operator config), the image refs from this run, the compose files
@@ -8,20 +17,24 @@ import { typed } from "../../lib/typed.js";
8
17
  const project = (app, appsDir, bundle) => {
9
18
  const dir = `${appsDir}/${app.name}`;
10
19
  return typed([
11
- "compose", "-p", app.name, "--project-directory", dir, "--env-file", `${dir}/.env`, "--env-file", "images.env",
12
- ...bundle.compose.flatMap((file) => ["-f", file.path]),
20
+ "compose", "-p", app.name, "--project-directory", dir, "--env-file", `${dir}/.env`, "--env-file", imagesFile,
21
+ ...bundle.compose.flatMap((file) => ["-f", `${appFiles}/${file.path}`]),
13
22
  ]);
14
23
  };
15
24
  /**
16
- * What admission checks. --no-env-resolution: otherwise compose reads every
17
- * env_file here, from any path (another app's secrets included), and bakes it
18
- * into the config; unresolved, the allow-list sees env_file and refuses it.
25
+ * What admission checks: the app's files alone. --no-env-resolution:
26
+ * otherwise compose reads every env_file here, from any path (another app's
27
+ * secrets included), and bakes it into the config; unresolved, the allow-list
28
+ * sees env_file and refuses it.
19
29
  */
20
30
  export const config = (app, appsDir, bundle) => typed([
21
31
  "docker", ...project(app, appsDir, bundle), "config", "--no-env-resolution", "--format", "json",
22
32
  ]);
23
- export const policy = (app, script) => typed(["bash", script, app.name, ...app.hosts]);
24
- /** --remove-orphans: a service the app dropped from its compose files must not keep running. */
33
+ export const policy = (app, script, domain, bundle) => typed(["bash", script, app.name, domain, JSON.stringify(wireRoutes(bundle.routes)), ...app.hosts]);
34
+ /**
35
+ * The app's files, then the platform's routes. --remove-orphans: a service the
36
+ * app dropped from its compose files must not keep running.
37
+ */
25
38
  export const up = (app, appsDir, bundle) => typed([
26
- "docker", ...project(app, appsDir, bundle), "up", "-d", "--wait", "--wait-timeout", "180", "--remove-orphans",
39
+ "docker", ...project(app, appsDir, bundle), "-f", routesFile, "up", "-d", "--wait", "--wait-timeout", "180", "--remove-orphans",
27
40
  ]);
@@ -0,0 +1,128 @@
1
+ import { all, attempt, err, flatMap, ok } from "../../lib/result.js";
2
+ import { typed } from "../../lib/typed.js";
3
+ import { field } from "../app.js";
4
+ import { period, port, routePath, serviceName, subdomain } from "../brand.js";
5
+ const unknownKey = (value, known) => Object.keys(value).find((key) => !known.includes(key));
6
+ const count = (value, name) => typeof value === "number" && Number.isInteger(value) && value > 0 ? ok(value) : err(`${name} ${String(value)} is not a positive whole number`);
7
+ const text = (value, name) => (typeof value === "string" ? ok(value) : err(`${name} is missing or not text`));
8
+ const rateLimit = (value) => {
9
+ if (value === undefined)
10
+ return ok(undefined);
11
+ if (typeof value !== "object" || value === null)
12
+ return err("rate_limit is not a mapping");
13
+ const extra = unknownKey(value, ["average", "period", "burst"]);
14
+ if (extra !== undefined)
15
+ return err(`rate_limit.${extra} is not a rate limit field`);
16
+ const average = count(field(value, "average"), "rate_limit.average");
17
+ const burst = count(field(value, "burst"), "rate_limit.burst");
18
+ const per = flatMap(text(field(value, "period"), "rate_limit.period"), period);
19
+ if (!average.ok)
20
+ return average;
21
+ if (!burst.ok)
22
+ return burst;
23
+ if (!per.ok)
24
+ return per;
25
+ return ok({ average: average.value, period: per.value, burst: burst.value });
26
+ };
27
+ const route = (value, index) => {
28
+ const where = (problem) => `route ${String(index)}: ${problem}`;
29
+ if (typeof value !== "object" || value === null)
30
+ return err(where("not a mapping"));
31
+ const extra = unknownKey(value, ["host", "path", "service", "port", "rate_limit"]);
32
+ if (extra !== undefined)
33
+ return err(where(`${extra} is not a route field`));
34
+ const host = flatMap(text(field(value, "host"), "host"), subdomain);
35
+ const service = flatMap(text(field(value, "service"), "service"), serviceName);
36
+ const rawPort = field(value, "port");
37
+ const listens = typeof rawPort === "number" ? port(rawPort) : err(`port ${String(rawPort)} is not a port`);
38
+ const rawPath = field(value, "path");
39
+ const path = rawPath === undefined ? ok(undefined) : flatMap(text(rawPath, "path"), routePath);
40
+ const limit = rateLimit(field(value, "rate_limit"));
41
+ if (!host.ok)
42
+ return err(where(host.error));
43
+ if (!service.ok)
44
+ return err(where(service.error));
45
+ if (!listens.ok)
46
+ return err(where(listens.error));
47
+ if (!path.ok)
48
+ return err(where(path.error));
49
+ if (!limit.ok)
50
+ return err(where(limit.error));
51
+ return ok({ host: host.value, path: path.value, service: service.value, port: listens.value, rateLimit: limit.value });
52
+ };
53
+ /** app.yml's routes as the builder read them at the commit: untrusted, so every field is checked. */
54
+ export const parseRoutes = (json) => Array.isArray(json) ? all(json.map((item, index) => route(item, index))) : err("routes is not a list");
55
+ /** Routes as app.yml writes them, for the compose policy. */
56
+ export const wireRoutes = (routes) => Object.freeze(routes.map((route) => typed({
57
+ host: route.host,
58
+ ...(route.path === undefined ? {} : { path: route.path }),
59
+ service: route.service,
60
+ port: route.port,
61
+ ...(route.rateLimit === undefined ? {} : { rate_limit: route.rateLimit }),
62
+ })));
63
+ /** One line for the run's log: `www → web:3000, api/signup → api:3000 (rate limited)`. */
64
+ export const describeRoutes = (routes) => routes.length === 0
65
+ ? "none"
66
+ : routes
67
+ .map((route) => `${route.host}${route.path ?? ""} → ${route.service}:${String(route.port)}${route.rateLimit === undefined ? "" : " (rate limited)"}`)
68
+ .join(", ");
69
+ /** A router per route: unique on the server through the app's prefix, readable in Traefik's dashboard. */
70
+ const routerName = (app, index, route) => `${app}-${String(index)}-${route.host}`;
71
+ /** A Traefik service per service and port, so one container can serve two ports. */
72
+ const backendName = (app, route) => `${app}-${route.service}-${String(route.port)}`;
73
+ const rule = (route, domain) => [`Host(\`${route.host}.${domain}\`)`, ...(route.path === undefined ? [] : [`PathPrefix(\`${route.path}\`)`])].join(" && ");
74
+ /** A route's labels. Security headers need none: the websecure entrypoint gives them to every router. */
75
+ const routeLabels = (app, domain, route, index) => {
76
+ const router = `traefik.http.routers.${routerName(app, index, route)}`;
77
+ const limit = `${routerName(app, index, route)}-limit`;
78
+ const backend = backendName(app, route);
79
+ return typed([
80
+ [`${router}.rule`, rule(route, domain)],
81
+ [`${router}.entrypoints`, "websecure"],
82
+ [`${router}.tls.certresolver`, "le"],
83
+ [`${router}.service`, backend],
84
+ [`traefik.http.services.${backend}.loadbalancer.server.port`, String(route.port)],
85
+ ...(route.rateLimit === undefined
86
+ ? []
87
+ : [
88
+ [`${router}.middlewares`, limit],
89
+ [`traefik.http.middlewares.${limit}.ratelimit.average`, String(route.rateLimit.average)],
90
+ [`traefik.http.middlewares.${limit}.ratelimit.period`, route.rateLimit.period],
91
+ [`traefik.http.middlewares.${limit}.ratelimit.burst`, String(route.rateLimit.burst)],
92
+ ]),
93
+ ]);
94
+ };
95
+ /** The networks the admitted config gives a service; Compose spells out the implicit default. */
96
+ const networksOf = (config, service) => {
97
+ const services = typeof config === "object" && config !== null ? field(config, "services") : undefined;
98
+ const definition = typeof services === "object" && services !== null ? field(services, service) : undefined;
99
+ if (typeof definition !== "object" || definition === null)
100
+ return err(`route to ${service}: no such service in the compose files`);
101
+ const networks = field(definition, "networks");
102
+ return ok(typeof networks === "object" && networks !== null ? { ...networks } : { default: null });
103
+ };
104
+ /**
105
+ * The platform's part of an app's compose project, from its admitted config
106
+ * and its routes: each routed service joins platform-public, beside the
107
+ * networks it has (restated, or the override would replace them), and gets the
108
+ * Traefik labels its routes become. Apps write no Traefik label themselves:
109
+ * the policy refuses them.
110
+ */
111
+ export const routesOverride = (app, domain, routes, admitted) => flatMap(attempt(() => ok(JSON.parse(admitted)), (why) => `the admitted config is not JSON: ${why}`), (config) => {
112
+ const routed = [...new Set(routes.map((route) => route.service))];
113
+ const services = all(routed.map((service) => flatMap(networksOf(config, service), (networks) => ok([
114
+ service,
115
+ {
116
+ networks: { ...networks, "platform-public": null },
117
+ labels: Object.fromEntries([
118
+ ["traefik.enable", "true"],
119
+ ["traefik.docker.network", "platform-public"],
120
+ ...routes.flatMap((route, index) => (route.service === service ? routeLabels(app, domain, route, index) : [])),
121
+ ]),
122
+ },
123
+ ]))));
124
+ return flatMap(services, (entries) => ok(`${JSON.stringify({
125
+ services: Object.fromEntries(entries),
126
+ ...(entries.length === 0 ? {} : { networks: { "platform-public": { name: "platform-public", external: true } } }),
127
+ }, null, 2)}\n`));
128
+ });
@@ -7,12 +7,13 @@ import { createInterface } from "node:readline";
7
7
  import { Readable } from "node:stream";
8
8
  import { all, err, ok } from "../lib/result.js";
9
9
  import { field, registered } from "./app.js";
10
- import { appName, appOwner, jobOwner, newRunId, sha } from "./brand.js";
10
+ import { appName, appOwner, newRunId, sha } from "./brand.js";
11
11
  import { matches, parseCron } from "./cron.js";
12
+ import { appFiles, imagesFile, routesFile } from "./build/compose.js";
12
13
  import { forge, forgeRepo } from "./forges/forge.js";
13
14
  import { json, send } from "./http/http.js";
14
15
  import { spawnLines } from "./process.js";
15
- import { empty, enqueue, finish } from "./runs/queue.js";
16
+ import { describe, empty, enqueue, finish, jobRequest } from "./runs/queue.js";
16
17
  import { expired, moved, releaseName, releases } from "./runs/records.js";
17
18
  import { githubIssues } from "./forges/issues.js";
18
19
  import { decide, parseNotify, silent } from "./forges/notify.js";
@@ -148,13 +149,18 @@ const effects = (s, who, id, log) => typed({
148
149
  secrets: appSecrets,
149
150
  workdir: (bundle, images) => {
150
151
  const dir = mkdtempSync(join(tmpdir(), `${id}-`));
151
- writeFileSync(join(dir, "images.env"), images);
152
+ writeFileSync(join(dir, imagesFile), images);
152
153
  bundle.compose.forEach((file) => {
153
- mkdirSync(dirname(join(dir, file.path)), { recursive: true });
154
- writeFileSync(join(dir, file.path), file.content);
154
+ const path = join(dir, appFiles, file.path);
155
+ mkdirSync(dirname(path), { recursive: true });
156
+ writeFileSync(path, file.content);
155
157
  });
156
158
  return Promise.resolve(dir);
157
159
  },
160
+ writeRoutes: (cwd, content) => {
161
+ writeFileSync(join(cwd, routesFile), content);
162
+ return Promise.resolve();
163
+ },
158
164
  record: (app, commit, images) => {
159
165
  const dir = join(s.platformDir, "releases", app.name);
160
166
  mkdirSync(dir, { recursive: true });
@@ -172,9 +178,6 @@ const effects = (s, who, id, log) => typed({
172
178
  log(`(commit status ${context} not posted: ${posted.error})`);
173
179
  },
174
180
  });
175
- const describe = (request) => request.work.kind === "deploy"
176
- ? typed({ what: `deploy ${request.work.ref}`, why: request.work.why })
177
- : typed({ what: `${request.work.job}${request.work.dryRun ? " (dry run)" : ""}`, why: "schedule" });
178
181
  const work = async (s, request, fx) => {
179
182
  if (request.work.kind === "deploy") {
180
183
  const app = registered(s.platformDir, request.work.app);
@@ -182,7 +185,7 @@ const work = async (s, request, fx) => {
182
185
  fx.log(app.error);
183
186
  return typed({ ok: false });
184
187
  }
185
- return deploy(app.value, request.work.ref, { appsDir: s.appsDir, policyScript: s.policyScript }, fx);
188
+ return deploy(app.value, request.work.ref, { appsDir: s.appsDir, policyScript: s.policyScript, domain: s.domain }, fx);
186
189
  }
187
190
  const argv = request.work.job === "gc" ? ["bash", s.gcScript] : renovate(s.env, request.work.dryRun, s.renovateImage);
188
191
  const env = request.work.job === "renovate" ? { RENOVATE_TOKEN: token("RENOVATE_TOKEN") } : {};
@@ -238,7 +241,6 @@ const deployRequest = (app, ref, why) => typed({
238
241
  owner: appOwner(app.name),
239
242
  work: { kind: "deploy", app: app.name, ref, why },
240
243
  });
241
- const jobRequest = (job, dryRun) => typed({ owner: jobOwner(job), work: { kind: "job", job, dryRun } });
242
244
  /** The webhook's fallback: an app whose ref moved to a commit no run has tried yet and no release runs (one migrated from Jenkins has releases but no runs). */
243
245
  const poll = async (s) => {
244
246
  await Promise.all(registeredApps(s).map(async (app) => {
@@ -254,9 +256,9 @@ const tick = (s, at) => {
254
256
  if (matches(s.poll, at))
255
257
  void poll(s);
256
258
  if (matches(s.gc, at))
257
- swap(s, (queue) => enqueue(queue, jobRequest("gc", false)));
259
+ swap(s, (queue) => enqueue(queue, jobRequest("gc", false, "schedule")));
258
260
  if (s.renovate !== undefined && matches(s.renovate, at))
259
- swap(s, (queue) => enqueue(queue, jobRequest("renovate", false)));
261
+ swap(s, (queue) => enqueue(queue, jobRequest("renovate", false, "schedule")));
260
262
  };
261
263
  const schedule = (s) => {
262
264
  const wait = 60_000 - (Date.now() % 60_000) + 500;
@@ -301,7 +303,7 @@ const control = async (s, request, response) => {
301
303
  send(response, 409, "this server has no renovate_repositories\n");
302
304
  return;
303
305
  }
304
- accept(s, response, jobRequest(job, field(input, "dryRun") === true));
306
+ accept(s, response, jobRequest(job, field(input, "dryRun") === true, "manual"));
305
307
  return;
306
308
  }
307
309
  send(response, 404, "not found\n");
@@ -1,4 +1,11 @@
1
+ import { jobOwner } from "../brand.js";
1
2
  import { typed } from "../../lib/typed.js";
3
+ export const jobRequest = (job, dryRun, why) => typed({ owner: jobOwner(job), work: { kind: "job", job, dryRun, why } });
4
+ /** A run's meta.json `what` and `why`. */
5
+ export const describe = (request) => Object.freeze({
6
+ what: request.work.kind === "deploy" ? `deploy ${request.work.ref}` : `${request.work.job}${request.work.dryRun ? " (dry run)" : ""}`,
7
+ why: request.work.why,
8
+ });
2
9
  export const empty = (limit) => typed({ running: [], waiting: [], limit });
3
10
  const exclusive = (request) => request.work.kind === "job" && request.work.job === "gc";
4
11
  /** Starts what may start, in order; a waiting exclusive request holds back everything behind it. */
@@ -1,6 +1,7 @@
1
1
  import { err, ok } from "../../lib/result.js";
2
2
  import { imagesEnv, parseBundle } from "../build/bundle.js";
3
3
  import { config, policy, up } from "../build/compose.js";
4
+ import { describeRoutes, routesOverride } from "../build/routes.js";
4
5
  import { typed } from "../../lib/typed.js";
5
6
  const step = async (fx, label, argv, options) => {
6
7
  fx.log(`── ${label}`);
@@ -9,21 +10,27 @@ const step = async (fx, label, argv, options) => {
9
10
  };
10
11
  /**
11
12
  * Admission, then up: the compose config of the commit being deployed must
12
- * pass the policy before anything starts, and what it checked is what runs.
13
+ * pass the policy before anything starts, and what it checked is what runs,
14
+ * with the routes the platform writes from it.
13
15
  */
14
- const deployBundle = async (app, bundle, paths, fx) => {
16
+ const deployBundle = async (app, bundle, site, fx) => {
15
17
  const images = imagesEnv(bundle);
16
18
  const secrets = fx.secrets(app);
17
19
  if (!secrets.ok)
18
20
  return secrets;
19
21
  const cwd = await fx.workdir(bundle, images);
20
- const resolved = await step(fx, "Admission: compose config", config(app, paths.appsDir, bundle), { cwd, env: secrets.value, capture: true });
22
+ const resolved = await step(fx, "Admission: compose config", config(app, site.appsDir, bundle), { cwd, env: secrets.value, capture: true });
21
23
  if (!resolved.ok)
22
24
  return resolved;
23
- const admitted = await step(fx, "Admission: policy", policy(app, paths.policyScript), { cwd, env: {}, stdin: resolved.value });
25
+ const admitted = await step(fx, "Admission: policy", policy(app, site.policyScript, site.domain, bundle), { cwd, env: {}, stdin: resolved.value });
24
26
  if (!admitted.ok)
25
27
  return err("refused by the compose policy");
26
- const started = await step(fx, "Up", up(app, paths.appsDir, bundle), { cwd, env: secrets.value });
28
+ const routes = routesOverride(app.name, site.domain, bundle.routes, resolved.value);
29
+ if (!routes.ok)
30
+ return routes;
31
+ fx.log(`── Routes: ${describeRoutes(bundle.routes)}`);
32
+ await fx.writeRoutes(cwd, routes.value);
33
+ const started = await step(fx, "Up", up(app, site.appsDir, bundle), { cwd, env: secrets.value });
27
34
  if (!started.ok)
28
35
  return started;
29
36
  await fx.record(app, bundle.sha, images);
@@ -34,7 +41,7 @@ const deployBundle = async (app, bundle, paths, fx) => {
34
41
  * then admitted and started (kayra/deploy). Each phase posts its status on the
35
42
  * commit; a failed build never reaches deploy.
36
43
  */
37
- export const deploy = async (app, ref, paths, fx) => {
44
+ export const deploy = async (app, ref, site, fx) => {
38
45
  fx.log(`Deploying ${app.name} at ${ref}`);
39
46
  const commit = await fx.resolve(app, ref);
40
47
  if (!commit.ok) {
@@ -53,7 +60,7 @@ export const deploy = async (app, ref, paths, fx) => {
53
60
  }
54
61
  await fx.status(app, sha, "kayra/build", "success", "Images built");
55
62
  await fx.status(app, sha, "kayra/deploy", "pending", "Deploying");
56
- const deployed = await deployBundle(app, bundle.value, paths, fx);
63
+ const deployed = await deployBundle(app, bundle.value, site, fx);
57
64
  if (!deployed.ok) {
58
65
  fx.log(deployed.error);
59
66
  await fx.status(app, sha, "kayra/deploy", "failure", deployed.error);
package/cli/dist/main.js CHANGED
@@ -9,10 +9,10 @@ import { fromCommand } from "./clients/cli/invocation.js";
9
9
  import { parser, runOptions } from "./clients/cli/parser.js";
10
10
  import { verbose } from "./core/recipes/steps.js";
11
11
  import { opentofu } from "./core/adapters/opentofu.js";
12
- import { statusJson } from "./clients/api/status.js";
12
+ import { statusJson } from "./clients/wire/status.js";
13
13
  import { statusView } from "./clients/cli/status.js";
14
14
  import { overall } from "./core/validation/findings.js";
15
- import { validateJson } from "./clients/api/validate.js";
15
+ import { validateJson } from "./clients/wire/validate.js";
16
16
  import { validateView } from "./clients/cli/validate.js";
17
17
  import { interpret } from "./core/interpreter/loop.js";
18
18
  import { findFleetRoot, loadFleet } from "./core/fleet/load.js";
@@ -20,6 +20,7 @@ import { operation as describe } from "./core/operations/catalog.js";
20
20
  import { contextFor, hasPlan, planOf, query, serverOf, stream } from "./core/operations/dispatch.js";
21
21
  import { systemBrowser } from "./clients/ui/opener.js";
22
22
  import { serve } from "./serve.js";
23
+ import { serveMcp } from "./clients/mcp/server.js";
23
24
  import { network } from "./core/adapters/network.js";
24
25
  import { render } from "./core/plan/describe.js";
25
26
  import { err, flatMap, map, ok } from "./lib/result.js";
@@ -90,7 +91,14 @@ const short = shortener([
90
91
  [homedir(), "~"],
91
92
  ]);
92
93
  const context = contextFor({ kayra, fleet: fleet, version, here: here.value, network: network(2_000), tofu: opentofu });
93
- if (args.command.name === "serve") {
94
+ if (args.command.name === "mcp") {
95
+ // stdout is the protocol: nothing else may write there, and every step's output is piped.
96
+ if (!found.ok)
97
+ fail(found.error);
98
+ else
99
+ serveMcp({ context, short }, (message) => { process.stderr.write(`kayra mcp: ${message}\n`); });
100
+ }
101
+ else if (args.command.name === "serve") {
94
102
  if (!found.ok)
95
103
  fail(found.error);
96
104
  // The browser side: web/dist in a checkout, cli/dist/browser in the package.