@yunuseon/kayra 0.20.1 → 0.21.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 (50) hide show
  1. package/README.md +2 -2
  2. package/agent/skills/kayra-rollout/SKILL.md +1 -1
  3. package/ansible/.ansible-lint +2 -2
  4. package/ansible/playbooks/app.yml +1 -1
  5. package/ansible/playbooks/edge.yml +1 -1
  6. package/ansible/playbooks/nas.yml +1 -1
  7. package/ansible/playbooks/platform.yml +2 -2
  8. package/ansible/playbooks/restore-test.yml +24 -0
  9. package/ansible/roles/app/tasks/main.yml +20 -28
  10. package/ansible/roles/forgejo-config/tasks/main.yml +1 -1
  11. package/ansible/roles/platform/defaults/main.yml +1 -1
  12. package/ansible/roles/platform/tasks/main.yml +1 -1
  13. package/cli/dist/app-hook.js +6 -13
  14. package/cli/dist/app-launch.js +1 -1
  15. package/cli/dist/app-values.js +1 -1
  16. package/cli/dist/clients/mcp/run.js +1 -1
  17. package/cli/dist/clients/mcp/server.js +1 -1
  18. package/cli/dist/core/adapters/sops.js +19 -0
  19. package/cli/dist/core/app-values/store.js +46 -57
  20. package/cli/dist/core/fleet/brand.js +2 -2
  21. package/cli/dist/core/fleet/docs.js +1 -1
  22. package/cli/dist/core/fleet/parse.js +19 -29
  23. package/cli/dist/core/fleet/secrets.js +59 -0
  24. package/cli/dist/core/fleet/settings.js +5 -6
  25. package/cli/dist/core/interpreter/loop.js +8 -3
  26. package/cli/dist/core/interpreter/needs.js +54 -26
  27. package/cli/dist/core/interpreter/process.js +2 -2
  28. package/cli/dist/core/operations/catalog.js +1 -1
  29. package/cli/dist/core/operations/dispatch.js +1 -1
  30. package/cli/dist/core/plan/describe.js +2 -2
  31. package/cli/dist/core/recipes/app-hook.js +2 -2
  32. package/cli/dist/core/recipes/app-values.js +1 -2
  33. package/cli/dist/core/recipes/apps.js +5 -6
  34. package/cli/dist/core/recipes/backup.js +2 -15
  35. package/cli/dist/core/recipes/doctor.js +8 -11
  36. package/cli/dist/core/recipes/host.js +11 -11
  37. package/cli/dist/core/recipes/launch.js +3 -3
  38. package/cli/dist/core/recipes/steps.js +7 -20
  39. package/cli/dist/core/validation/facts.js +37 -27
  40. package/cli/dist/core/validation/findings.js +23 -4
  41. package/compose/authelia/configuration.yml +1 -1
  42. package/package.json +1 -1
  43. package/schema/app-values.json +1 -1
  44. package/schema/app.json +1 -1
  45. package/schema/server.json +2 -7
  46. package/scripts/local/authelia-dev-init.sh +2 -2
  47. package/scripts/server/app-restore +39 -0
  48. package/terraform/modules/platform-dns/tests/fleet/servers/nested/server.yml +0 -1
  49. package/terraform/modules/platform-dns/tests/fleet/servers/shop/server.yml +0 -1
  50. package/terraform/modules/platform-dns/tests/fleet/servers/uncloned/server.yml +0 -1
@@ -0,0 +1,59 @@
1
+ import { err, ok } from "../../lib/result.js";
2
+ import { typed } from "../../lib/typed.js";
3
+ import { fields, mapping } from "../../lib/yaml.js";
4
+ /**
5
+ * A server's secrets.yml: SOPS encrypts each value and leaves the keys, so
6
+ * what it holds can be read without its key; only the values need one.
7
+ * Beside the host's secrets it holds each app's (`app_secrets.<app>`) and
8
+ * OpenTofu's variables (`terraform`).
9
+ */
10
+ export const isEncrypted = (text) => /^sops:[ \t]*$/m.test(text);
11
+ const without = (values, keys) => typed(Object.fromEntries(Object.entries(values).filter(([key]) => !keys.includes(key))));
12
+ const encryptedValue = /^ENC\[AES256_GCM,data:[^,]*,iv:[^,]*,tag:[^,]*,type:(\w+)\]$/;
13
+ /** A value of the type SOPS recorded for it: enough for a schema to check, nothing of the secret. */
14
+ const standIn = (type) => {
15
+ switch (type) {
16
+ case "int":
17
+ return 0;
18
+ case "float":
19
+ return 0.5;
20
+ case "bool":
21
+ return true;
22
+ default:
23
+ return "(encrypted)";
24
+ }
25
+ };
26
+ const shaped = (value) => {
27
+ // Recursive: a tree of mappings and lists, its leaves replaced.
28
+ if (Array.isArray(value))
29
+ return Object.freeze(value.map(shaped));
30
+ const fields = mapping(value);
31
+ if (fields !== undefined)
32
+ return Object.freeze(Object.fromEntries(Object.entries(fields).map(([key, each]) => [key, shaped(each)])));
33
+ if (typeof value !== "string")
34
+ return value;
35
+ const type = encryptedValue.exec(value)?.[1];
36
+ return type === undefined ? value : standIn(type);
37
+ };
38
+ /**
39
+ * secrets.yml without its key: every key, each value a stand-in of its type.
40
+ * A plaintext file (a multipass server's test values) as it is.
41
+ */
42
+ export const secretsShape = (text) => {
43
+ const parsed = fields(text);
44
+ if (!parsed.ok)
45
+ return parsed;
46
+ return ok(typed(mapping(shaped(without(parsed.value, ["sops"]))) ?? {}));
47
+ };
48
+ /** One app's secrets, from secrets.yml's app_secrets; none where it has none. */
49
+ export const appSecretsIn = (secrets, app) => mapping(mapping(secrets["app_secrets"])?.[app]) ?? {};
50
+ /** The host's own secrets: what is neither an app's nor OpenTofu's. */
51
+ export const hostSecretsIn = (secrets) => without(secrets, ["app_secrets", "terraform"]);
52
+ /** OpenTofu's variables from secrets.yml's terraform mapping, as its environment takes them. */
53
+ export const terraformVariables = (secrets) => {
54
+ const variables = mapping(secrets["terraform"]) ?? {};
55
+ const unsupported = Object.entries(variables).find(([, value]) => !["string", "number", "boolean"].includes(typeof value));
56
+ if (unsupported !== undefined)
57
+ return err(`secrets.yml: terraform.${unsupported[0]} must be a string, number or boolean`);
58
+ return ok(Object.freeze(Object.fromEntries(Object.entries(variables).map(([key, value]) => [`TF_VAR_${key}`, String(value)]))));
59
+ };
@@ -114,7 +114,9 @@ const shape = (chosen) => Object.fromEntries(chosen.map((each) => [each.name, ea
114
114
  export const varsSchema = (kind) => z.strictObject(shape(settingsFor(kind, false)));
115
115
  /** defaults.yml: settings shared by every server, so any kind's. */
116
116
  export const defaultsSchema = z.strictObject(shape(settings.filter((each) => !each.secret)));
117
- /** secrets.yml: the known secrets typed; other keys may be helpers that vars.yml references. */
117
+ /** apps/<app>.yml, and an app's mapping in secrets.yml's app_secrets: flat key → value. */
118
+ export const appValuesSchema = z.record(z.string(), z.union([z.string(), z.number(), z.boolean()]));
119
+ /** secrets.yml's own (the host's) secrets: the known ones typed; other keys may be helpers that vars.yml references. */
118
120
  // eslint-disable-next-line functional/prefer-immutable-types -- a Zod schema: immutable once built, but a class the rule cannot see into
119
121
  export const secretsSchema = (kind) => z.looseObject(shape(settingsFor(kind, true)));
120
122
  /**
@@ -140,8 +142,7 @@ export const serverSchema = z
140
142
  .enum(serverTargets, { error: (issue) => `target must be ${serverTargets.join(", ")}, not '${String(issue.input)}'` })
141
143
  .describe("Where it runs: hetzner, multipass (a local VM) or home (a machine nothing creates)."),
142
144
  domain: domainSchema.describe("The server's domain; platform subdomains and the apps' hang off it."),
143
- vault: z.boolean().default(false).describe("secrets.yml is vault-encrypted (required outside multipass)."),
144
- vault_password_file: z.string().optional().describe("The vault password file; without it, Ansible asks."),
145
+ age_key_file: z.string().optional().describe("The age identity that opens secrets.yml (SOPS); without it, SOPS's own default (SOPS_AGE_KEY_FILE, ~/.config/sops/age/keys.txt)."),
145
146
  apps: z
146
147
  .array(appEntrySchema)
147
148
  .default([])
@@ -182,8 +183,6 @@ export const appManifestSchema = z.strictObject({
182
183
  name: appNameSchema.describe("The app's name, as in the server.yml entry."),
183
184
  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."),
184
185
  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."),
185
- secrets: z.array(z.string()).optional().describe("Secret keys; their values come from the fleet's apps/<app>.secrets.yml."),
186
+ secrets: z.array(z.string()).optional().describe("Secret keys; their values come from the fleet's secrets.yml, under app_secrets.<app>."),
186
187
  config: z.array(z.string()).optional().describe("Config keys; their values come from the fleet's apps/<app>.yml."),
187
188
  });
188
- /** apps/<app>.yml and apps/<app>.secrets.yml: flat key → value. */
189
- export const appValuesSchema = z.record(z.string(), z.union([z.string(), z.number(), z.boolean()]));
@@ -24,7 +24,7 @@ const check = async (step, plumbing) => {
24
24
  return typed(exit.code === 0 ? { ...goOn, out: [[fine("✓"), " ", step.label]] } : { failed: true, stopped: false, out: [[bad("✗"), " ", step.label], ...said], err: [] });
25
25
  };
26
26
  const runStep = async (step, io, met) => {
27
- const argv = await resolve(step.argv, io);
27
+ const argv = await resolve(step.argv, io, met.env);
28
28
  if (!argv.ok)
29
29
  return typed({ failed: true, stopped: true, why: "a query failed", out: [], err: [detail(argv.error)] });
30
30
  const { stdout } = step;
@@ -77,11 +77,16 @@ export const interpret = async (plan, io, progress = silent, interrupted = () =>
77
77
  if (interrupted() || isStopped(io.signal))
78
78
  return { ...so, stop: { index, step, why: "interrupted" } };
79
79
  // What a step needs is asked before its frame opens: a prompt, then the step.
80
- const met = (step.kind === "run" || step.kind === "session") && step.needs !== undefined ? await meet(step.needs, so.asked, io, made) : { met: { env: {} }, asked: so.asked };
81
- if (met === undefined) {
80
+ const meeting = (step.kind === "run" || step.kind === "session") && step.needs !== undefined ? await meet(step.needs, so.asked, io, made) : { kind: "met", meeting: { met: { env: {} }, given: so.asked } };
81
+ if (meeting.kind === "declined") {
82
82
  io.err([" ", muted("Nothing given: stopped before it ran.")]);
83
83
  return { ...so, stop: { index, step, why: isStopped(io.signal) ? "interrupted" : "declined" } };
84
84
  }
85
+ if (meeting.kind === "failed") {
86
+ io.err([" ", muted(meeting.why)]);
87
+ return { failures: so.failures + 1, asked: so.asked, stop: { index, step, why: isStopped(io.signal) ? "interrupted" : "failed" } };
88
+ }
89
+ const met = { met: meeting.meeting.met, asked: meeting.meeting.given };
85
90
  const done = await progress.step(step, position(plan, index), () => perform(step, io, met.met));
86
91
  // Ctrl-C reached the step's program too: how it ended says nothing about the server.
87
92
  if (interrupted() || isStopped(io.signal))
@@ -1,28 +1,28 @@
1
- import { chmodSync, mkdtempSync, rmSync, writeFileSync } from "node:fs";
1
+ import { mkdtempSync, rmSync, writeFileSync } from "node:fs";
2
2
  import { tmpdir } from "node:os";
3
3
  import { join } from "node:path";
4
+ import { textAt } from "../adapters/fs.js";
5
+ import { sops } from "../adapters/sops.js";
6
+ import { terraformVariables } from "../fleet/secrets.js";
4
7
  import { active, muted, strong } from "../text/spans.js";
5
8
  import { ask } from "./io.js";
6
9
  import { spawned } from "./process.js";
10
+ import { err, flatMap, map, mapError, ok } from "../../lib/result.js";
7
11
  import { typed } from "../../lib/typed.js";
8
- const keyOf = (need) => (need.kind === "vault" ? `vault:${need.server}` : "sudo");
9
- const questionOf = (need) => typed(need.kind === "vault" ? { kind: "secret", text: [" ", active("?"), " ", strong(`${need.server}'s vault password`), muted(" (asked once, kept for this run): ")] } : { kind: "secret", text: [" ", active("?"), " ", strong("sudo's password"), muted(" (for this step only): ")] });
12
+ import { fields } from "../../lib/yaml.js";
13
+ const sudoQuestion = typed({ kind: "secret", text: [" ", active("?"), " ", strong("sudo's password"), muted(" (for this step only): ")] });
10
14
  /** Whether sudo would run without a password: a cached one, or none needed. */
11
15
  const sudoReady = async (plumbing) => (await spawned(["sudo", "-n", "true"], { stdout: "discard", stderr: "capture", stdin: { kind: "none" } }, {}, plumbing)).code === 0;
12
16
  export const scratch = () => {
13
17
  // eslint-disable-next-line functional/prefer-immutable-types -- made on first need, removed when the run ends
14
18
  const made = {};
15
- const dir = () => {
16
- // eslint-disable-next-line functional/immutable-data -- see above
17
- made.dir ??= mkdtempSync(join(tmpdir(), "kayra-"));
18
- return made.dir;
19
- };
20
19
  return typed({
21
- vaultScript: () => {
22
- const script = join(dir(), "vault-password");
23
- writeFileSync(script, '#!/bin/sh\nprintf \'%s\\n\' "$KAYRA_VAULT_PASSWORD"\n', { mode: 0o700 });
24
- chmodSync(script, 0o700);
25
- return script;
20
+ write: (name, text) => {
21
+ // eslint-disable-next-line functional/immutable-data -- see above
22
+ made.dir ??= mkdtempSync(join(tmpdir(), "kayra-"));
23
+ const file = join(made.dir, name);
24
+ writeFileSync(file, text, { mode: 0o600 });
25
+ return file;
26
26
  },
27
27
  clean: () => {
28
28
  if (made.dir !== undefined)
@@ -30,18 +30,46 @@ export const scratch = () => {
30
30
  },
31
31
  });
32
32
  };
33
- /** Asks what a step needs and has not been given yet in this run; undefined if the operator gave nothing. */
34
- export const meet = async (needs, asked, io, made) => needs.reduce(async (previous, need) => {
35
- const so = await previous;
36
- if (so === undefined)
37
- return undefined;
38
- if (need.kind === "sudo" && (await sudoReady(io)))
39
- return so;
40
- const key = keyOf(need);
41
- const given = so.asked[key] ?? (await ask(io, questionOf(need)));
33
+ /** Where this run has secrets.yml in plaintext: opened once (one Touch ID, where the key asks for it), then reused. */
34
+ const opened = async (need, given, made, plumbing) => {
35
+ const cached = given[`secrets:${need.server}`];
36
+ if (cached !== undefined)
37
+ return ok(cached);
38
+ if (!need.encrypted)
39
+ return ok(need.file);
40
+ return map(await sops.decrypt(need.file, need.key, plumbing), (text) => made.write(`${need.server}.secrets.yml`, text));
41
+ };
42
+ const secretsEnv = (need, file) => {
43
+ if (need.for === "ansible")
44
+ return ok({ KAYRA_SECRETS: file });
45
+ const text = textAt(file);
46
+ return text === undefined ? err(`cannot read ${file}`) : flatMap(mapError(fields(text), (why) => `${file} ${why}`), terraformVariables);
47
+ };
48
+ const meetOne = async (so, need, io, made) => {
49
+ if (need.kind === "secrets") {
50
+ const file = await opened(need, so.given, made, io);
51
+ if (!file.ok)
52
+ return file;
53
+ const env = secretsEnv(need, file.value);
54
+ if (!env.ok)
55
+ return env;
56
+ return ok({ given: { ...so.given, [`secrets:${need.server}`]: file.value }, met: { ...so.met, env: { ...so.met.env, ...env.value } } });
57
+ }
58
+ if (await sudoReady(io))
59
+ return ok(so);
60
+ const given = await ask(io, sudoQuestion);
42
61
  // An empty password is none: Enter alone declines.
43
62
  if (given === undefined || given === "")
44
- return undefined;
45
- const now = need.kind === "vault" ? { ...so.asked, [key]: given } : so.asked;
46
- return typed(need.kind === "vault" ? { asked: now, met: { ...so.met, env: { ...so.met.env, ANSIBLE_VAULT_PASSWORD_FILE: made.vaultScript(), KAYRA_VAULT_PASSWORD: given } } } : { asked: now, met: { env: { ...so.met.env, KAYRA_SUDO_STDIN: "1" }, stdin: { kind: "given", text: `${given}\n` } } });
47
- }, Promise.resolve({ met: { env: {} }, asked }));
63
+ return ok(undefined);
64
+ return ok({ given: so.given, met: { env: { ...so.met.env, KAYRA_SUDO_STDIN: "1" }, stdin: { kind: "given", text: `${given}\n` } } });
65
+ };
66
+ /** Meets what a step needs and this run has not met yet. */
67
+ export const meet = async (needs, given, io, made) => {
68
+ const ended = await needs.reduce(async (previous, need) => {
69
+ const so = await previous;
70
+ return !so.ok || so.value === undefined ? so : meetOne(so.value, need, io, made);
71
+ }, Promise.resolve(ok({ met: { env: {} }, given })));
72
+ if (!ended.ok)
73
+ return typed({ kind: "failed", why: ended.error });
74
+ return ended.value === undefined ? typed({ kind: "declined" }) : typed({ kind: "met", meeting: ended.value });
75
+ };
@@ -90,11 +90,11 @@ export const spawned = async (argv, streams, env = {}, plumbing = terminal) => {
90
90
  export const capture = (argv, stderr = "show", env = {}, plumbing = terminal) => spawned(argv, { stdout: "capture", stderr }, env, plumbing);
91
91
  export const answer = (probe, plumbing) => spawned(probe.kind === "on-path" ? ["sh", "-c", 'command -v "$1"', "sh", probe.program] : probe.argv, { stdout: "discard", stderr: "capture", stdin: { kind: "none" } }, probe.kind === "exits-zero" ? probe.env : {}, plumbing);
92
92
  /** Each query's answer in place of it, as `$(…)` would: one word, or split into many. */
93
- export const resolve = async (argv, plumbing) => {
93
+ export const resolve = async (argv, plumbing, env = {}) => {
94
94
  const words = await Promise.all(argv.map(async (value) => {
95
95
  if (typeof value === "string")
96
96
  return ok([value]);
97
- const exit = await capture(value.argv, "show", value.env, plumbing);
97
+ const exit = await capture(value.argv, "show", { ...env, ...value.env }, plumbing);
98
98
  if (exit.code !== 0)
99
99
  return err(`$(${value.argv.join(" ")}) failed: exit ${String(exit.code)}`);
100
100
  const output = exit.stdout.replace(/\n+$/, "");
@@ -107,7 +107,7 @@ export const operations = {
107
107
  input: z.strictObject({ server, app, dir, key: valueKeySchema, value: z.string().min(1).refine((value) => !value.includes("\0"), "NUL is not an environment value") }),
108
108
  },
109
109
  "app.secret.import": {
110
- summary: "Import a declared secret from a local file into the fleet vault; never print its value.",
110
+ summary: "Import a declared secret from a local file into the server's secrets.yml; never print its value.",
111
111
  scope: "app", effect: "changes", kind: "plan", local: true,
112
112
  input: z.strictObject({ server, app, dir, key: valueKeySchema, file: z.string().min(1).describe("Local file containing the secret, not YAML; one trailing newline is removed") }),
113
113
  },
@@ -144,7 +144,7 @@ const queries = {
144
144
  return await inspectValues(ctx.fleet.root, target.value[0], target.value[1], dir.value);
145
145
  }
146
146
  catch {
147
- return err("app values could not be inspected; check file access and vault configuration");
147
+ return err("app values could not be inspected; check file access and secrets.yml");
148
148
  }
149
149
  },
150
150
  "app.runs": (ctx, input) => deployerAnswer(ctx, "app.runs", input),
@@ -27,10 +27,10 @@ export const titleOf = (step) => {
27
27
  return step.text;
28
28
  }
29
29
  };
30
- /** What --dry-run says a step will ask for: kayra asks it when the step runs. */
30
+ /** What --dry-run says a step will need: kayra meets it when the step runs. */
31
31
  const asks = (needs = []) => needs.length === 0
32
32
  ? ""
33
- : ` # asks for ${needs.map((need) => (need.kind === "vault" ? `${need.server}'s vault password` : "sudo's password, unless cached")).join(" and ")}`;
33
+ : ` # needs ${needs.map((need) => (need.kind === "secrets" ? `${need.server}'s secrets.yml${need.for === "opentofu" ? " (terraform, as TF_VAR_*)" : ""}` : "sudo's password, unless cached")).join(" and ")}`;
34
34
  /** The shell command a step runs, or undefined if it runs none. */
35
35
  export const commandOf = (step) => {
36
36
  switch (step.kind) {
@@ -1,7 +1,7 @@
1
1
  import { within } from "../fleet/brand.js";
2
2
  import { run } from "../plan/model.js";
3
3
  import { err, flatMap, ok } from "../../lib/result.js";
4
- import { vaulted } from "./steps.js";
4
+ import { withSecrets } from "./steps.js";
5
5
  /** Whether a server takes push webhooks: its vars.yml's `webhooks` (default true), and a forge able to reach it. */
6
6
  export const takesWebhooks = (server) => {
7
7
  if (server.kind !== "platform")
@@ -21,6 +21,6 @@ export const appHook = (engine, root, server, app) => flatMap(takesWebhooks(serv
21
21
  if (!server.apps.some((each) => each.name === app))
22
22
  return err(`${app} is not in ${server.name}'s server.yml`);
23
23
  return ok([
24
- vaulted(run(`Hook ${app}'s repository to ${server.name}`, [process.execPath, within(engine.kayra, "bin", "kayra-app-hook.js"), JSON.stringify({ root, server: server.name, app })]), server),
24
+ withSecrets(run(`Hook ${app}'s repository to ${server.name}`, [process.execPath, within(engine.kayra, "bin", "kayra-app-hook.js"), JSON.stringify({ root, server: server.name, app })]), server),
25
25
  ]);
26
26
  });
@@ -2,7 +2,6 @@ import { resolve } from "node:path";
2
2
  import { editSchema, requirements } from "../app-values/model.js";
3
3
  import { within } from "../fleet/brand.js";
4
4
  import { run } from "../plan/model.js";
5
- import { vaulted } from "./steps.js";
6
5
  import { err, ok } from "../../lib/result.js";
7
6
  export const appValues = (engine, server, app, edit, here, read, dir) => {
8
7
  if (!editSchema.safeParse(edit).success)
@@ -17,5 +16,5 @@ export const appValues = (engine, server, app, edit, here, read, dir) => {
17
16
  const step = run(`Prepare ${app}'s ${edit.kind === "config" ? "configuration" : "secret"} ${edit.key} in the fleet`, [
18
17
  process.execPath, within(engine.kayra, "bin", "kayra-app-values.js"), JSON.stringify(input),
19
18
  ]);
20
- return ok([edit.kind === "config" ? step : vaulted(step, server)]);
19
+ return ok([step]);
21
20
  };
@@ -1,21 +1,20 @@
1
1
  import { partOf, run } from "../plan/model.js";
2
2
  import { err, map, ok } from "../../lib/result.js";
3
- import { ansibleEnv, ansiblePath, collections, fromMain, inventory, vault, vaulted } from "./steps.js";
3
+ import { ansibleEnv, ansiblePath, collections, fromMain, inventory, withSecrets } from "./steps.js";
4
4
  import { sshTo } from "./ssh.js";
5
5
  import { typed } from "../../lib/typed.js";
6
6
  /** Register every app in server.yml, then remove the rest (their data kept). */
7
7
  export const apps = (engine, server) => {
8
- const { env } = vault(server);
9
- const playbook = (title, name, ...extra) => vaulted(run(title, ["ansible-playbook", "-i", inventory(server), ansiblePath(engine, "playbooks", name), ...extra], ansibleEnv(engine, {})), server);
8
+ const playbook = (title, name, ...extra) => run(title, ["ansible-playbook", "-i", inventory(server), ansiblePath(engine, "playbooks", name), ...extra], ansibleEnv(engine, {}));
10
9
  return ok(partOf("apps", [
11
10
  fromMain(engine, server),
12
11
  ...(server.kind === "platform"
13
12
  ? [
14
13
  ...server.apps.flatMap((app) => Object.freeze([
15
- collections(engine, env),
16
- playbook(`App ${app.name}`, "app.yml", "-e", `server=${server.name}`, "-e", `server_dir=${server.dir}`, "-e", `domain=${server.domain}`, "-e", `{"app": ${JSON.stringify(app.entry)}}`),
14
+ collections(engine, {}),
15
+ withSecrets(playbook(`App ${app.name}`, "app.yml", "-e", `server=${server.name}`, "-e", `server_dir=${server.dir}`, "-e", `domain=${server.domain}`, "-e", `{"app": ${JSON.stringify(app.entry)}}`), server),
17
16
  ])),
18
- collections(engine, env),
17
+ collections(engine, {}),
19
18
  playbook("Remove apps not in server.yml", "apps-prune.yml", "-e", `{"keep": ${JSON.stringify(server.apps.map((app) => app.name))}}`),
20
19
  ]
21
20
  : []),
@@ -1,7 +1,6 @@
1
- import { within } from "../fleet/brand.js";
2
1
  import { run } from "../plan/model.js";
3
2
  import { err, ok } from "../../lib/result.js";
4
- import { ansibleEnv, inventory, reachable, vaulted } from "./steps.js";
3
+ import { ansibleEnv, ansiblePath, inventory, reachable, withSecrets } from "./steps.js";
5
4
  import { sshArgv } from "./ssh.js";
6
5
  /**
7
6
  * The restic-backup role's restore test: the latest backup, restored to a scratch dir and checked.
@@ -15,19 +14,7 @@ export const backupTest = (engine, server) => {
15
14
  return err(`no inventory for '${server.name}' — provision it first.`);
16
15
  return ok([
17
16
  ...reachable(engine, server),
18
- vaulted(run("Start the restore test", [
19
- "ansible",
20
- "servers",
21
- "-i",
22
- inventory(server),
23
- "--become",
24
- "-e",
25
- `@${within(server.dir, "secrets.yml")}`,
26
- "-m",
27
- "ansible.builtin.command",
28
- "-a",
29
- `docker compose -f ${server.backupDir}/docker-compose.yml exec -d -T backup /etc/backup/restore-test.sh`,
30
- ], ansibleEnv(engine, {})), server),
17
+ withSecrets(run("Start the restore test", ["ansible-playbook", "-i", inventory(server), ansiblePath(engine, "playbooks", "restore-test.yml"), "-e", `server_dir=${server.dir}`], ansibleEnv(engine, {})), server),
31
18
  { kind: "note", text: "Following the restore test. Ctrl-C stops following, not the test." },
32
19
  run("Follow the restore test", sshArgv(server.inventory, ["-o", "StrictHostKeyChecking=accept-new"], ["sh", `${server.backupDir}/follow-restore-test.sh`])),
33
20
  ]);
@@ -1,4 +1,3 @@
1
- import { within } from "../fleet/brand.js";
2
1
  import { homeReach, onPath } from "./steps.js";
3
2
  import { typed } from "../../lib/typed.js";
4
3
  const everyServer = () => true;
@@ -9,7 +8,8 @@ const tools = [
9
8
  { program: "jq", install: "brew install jq", neededBy: everyServer },
10
9
  { program: "ansible-playbook", install: "brew install ansible", neededBy: everyServer },
11
10
  { program: "ansible-galaxy", install: "brew install ansible", neededBy: everyServer },
12
- { program: "ansible-vault", install: "brew install ansible", neededBy: (server) => server.vault.kind !== "none" },
11
+ { program: "sops", install: "brew install sops", neededBy: (server) => server.secrets.encrypted },
12
+ { program: "age-plugin-se", install: "brew install age-plugin-se", neededBy: (server) => server.files.secrets.includes("recipient: age1se1") },
13
13
  { program: "tofu", install: "brew install opentofu", neededBy: (server) => server.hasTerraform },
14
14
  {
15
15
  program: "mkcert",
@@ -31,17 +31,14 @@ const toolChecks = (fleet) => Object.freeze(tools.flatMap((tool) => {
31
31
  onPath(`${tool.program} (${users.length === fleet.servers.length ? "every server" : users.join(", ")})`, tool.program, tool.install, "continue"),
32
32
  ]);
33
33
  }));
34
- // ansible-vault opens whole-file vaults only; values vaulted inline are opened by setup.
35
- const vaultChecks = (fleet) => Object.freeze(fleet.servers.flatMap((server) => server.vault.kind === "file" && server.hasVaultedSecrets
34
+ // The key is only looked for, never used: opening secrets.yml may ask for Touch ID.
35
+ const keyChecks = (fleet) => Object.freeze(fleet.servers.flatMap((server) => server.secrets.encrypted && server.secrets.key !== undefined
36
36
  ? Object.freeze([
37
37
  typed({
38
38
  kind: "check",
39
- label: `vault password opens ${server.name}'s secrets.yml`,
40
- probe: {
41
- kind: "exits-zero",
42
- argv: ["ansible-vault", "view", "--vault-password-file", server.vault.file, within(server.dir, "secrets.yml")],
43
- },
44
- hint: `the password file is ${server.vault.file}`,
39
+ label: `${server.name}'s age_key_file is here`,
40
+ probe: { kind: "exits-zero", argv: ["test", "-f", server.secrets.key] },
41
+ hint: `age_key_file in its server.yml is ${server.secrets.key}`,
45
42
  onFailure: "continue",
46
43
  }),
47
44
  ])
@@ -58,7 +55,7 @@ const reachChecks = (fleet, engine) => Object.freeze(fleet.servers.filter((serve
58
55
  /** Is this Mac ready to run every command on every server? Each check reports; none stops the others. */
59
56
  export const doctor = (fleet, engine) => typed([
60
57
  ...toolChecks(fleet),
61
- ...vaultChecks(fleet),
58
+ ...keyChecks(fleet),
62
59
  ...repoChecks(fleet),
63
60
  ...reachChecks(fleet, engine),
64
61
  ]);
@@ -1,6 +1,6 @@
1
1
  import { partOf, run } from "../plan/model.js";
2
2
  import { err, map, ok } from "../../lib/result.js";
3
- import { ansibleEnv, ansiblePath, appSubdomains, certs, collections, fleetEnv, fromMain, inventory, latestState, needing, reachable, script, sequence, terraform, vault, vaulted } from "./steps.js";
3
+ import { ansibleEnv, ansiblePath, appSubdomains, certs, collections, fleetEnv, fromMain, inventory, latestState, needing, reachable, script, sequence, terraform, withSecrets, withVariables } from "./steps.js";
4
4
  import { apps } from "./apps.js";
5
5
  import { typed } from "../../lib/typed.js";
6
6
  export const provision = (engine, server) => ok(partOf("provision", [
@@ -9,13 +9,13 @@ export const provision = (engine, server) => ok(partOf("provision", [
9
9
  ...(server.hasTerraform
10
10
  ? [
11
11
  latestState(engine, server),
12
- run("OpenTofu init", ["tofu", terraform(server), "init", "-input=false"]),
13
- run("OpenTofu apply", ["tofu", terraform(server), "apply", "-auto-approve"]),
12
+ withVariables(run("OpenTofu init", ["tofu", terraform(server), "init", "-input=false"]), server),
13
+ withVariables(run("OpenTofu apply", ["tofu", terraform(server), "apply", "-auto-approve"]), server),
14
14
  ]
15
15
  : []),
16
16
  ]));
17
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
- const hostPlaybook = (engine, server, extra = []) => vaulted(run(`Host layer · ${server.kind}.yml${extra.includes("--check") ? ", check mode" : ""}`, [
18
+ const hostPlaybook = (engine, server, extra = []) => withSecrets(run(`Host layer · ${server.kind}.yml${extra.includes("--check") ? ", check mode" : ""}`, [
19
19
  "ansible-playbook",
20
20
  "-i",
21
21
  inventory(server),
@@ -40,7 +40,7 @@ export const setup = (engine, server) => ok(partOf("setup", [
40
40
  ? [certs(engine, server.domain, [appSubdomains(engine, server), ...server.stackHosts])]
41
41
  : []),
42
42
  ...reachable(engine, server),
43
- collections(engine, vault(server).env),
43
+ collections(engine, {}),
44
44
  hostPlaybook(engine, server),
45
45
  ]));
46
46
  /**
@@ -53,23 +53,23 @@ export const preview = (engine, server) => ok([
53
53
  ...(server.hasTerraform
54
54
  ? [
55
55
  latestState(engine, server),
56
- run("OpenTofu init", ["tofu", terraform(server), "init", "-input=false"]),
57
- run("OpenTofu plan", ["tofu", terraform(server), "plan", "-input=false", "-lock=false"]),
56
+ withVariables(run("OpenTofu init", ["tofu", terraform(server), "init", "-input=false"]), server),
57
+ withVariables(run("OpenTofu plan", ["tofu", terraform(server), "plan", "-input=false", "-lock=false"]), server),
58
58
  ]
59
59
  : []),
60
- collections(engine, vault(server).env),
60
+ collections(engine, {}),
61
61
  hostPlaybook(engine, server, ["--check", "--diff"]),
62
62
  { kind: "note", text: `kayra plan changed nothing on ${server.name}: the changed tasks above are what kayra setup would do.` },
63
63
  ]);
64
64
  export const hosts = (engine, server) => ok(partOf("hosts", server.target === "multipass"
65
65
  ? [
66
- needing(run("/etc/hosts", [
66
+ withVariables(needing(run("/etc/hosts", [
67
67
  "bash",
68
68
  script(engine, "setup-hosts.sh"),
69
69
  server.domain,
70
70
  { kind: "substitution", argv: ["tofu", terraform(server), "output", "-raw", "server_ip"], words: "one" },
71
71
  appSubdomains(engine, server),
72
- ]), [{ kind: "sudo" }])
72
+ ]), [{ kind: "sudo" }]), server)
73
73
  ]
74
74
  : [{ kind: "note", text: `hosts: ${server.target} uses real DNS, nothing to do.` }]));
75
75
  /**
@@ -98,7 +98,7 @@ export const down = (engine, server) => server.hasTerraform
98
98
  },
99
99
  ]
100
100
  : []),
101
- run("OpenTofu destroy", ["tofu", terraform(server), "destroy", "-auto-approve"]),
101
+ withVariables(run("OpenTofu destroy", ["tofu", terraform(server), "destroy", "-auto-approve"]), server),
102
102
  ])
103
103
  : err(`${server.name} has no terraform/: nothing here creates it, so nothing destroys it`);
104
104
  /** Once, for a server we did not create: the deploy user, added as root. */
@@ -5,7 +5,6 @@ import { err, flatMap, map, ok } from "../../lib/result.js";
5
5
  import { apps, deployer } from "./apps.js";
6
6
  import { appHook, takesWebhooks } from "./app-hook.js";
7
7
  import { forgeRepo } from "../../lib/forge-repo.js";
8
- import { vaulted } from "./steps.js";
9
8
  import { typed } from "../../lib/typed.js";
10
9
  /** The server.yml entry the launch registers, or the one already there; a different one given is refused, never overwritten. */
11
10
  const entryOf = (server, launch) => {
@@ -40,11 +39,12 @@ export const launch = (engine, root, server, wanted) => {
40
39
  const app = typed({ name: parsed.data.name, repo: parsed.data.repo, path: parsed.data.path, entry: raw });
41
40
  const registered = server.apps.some((each) => each.name === app.name);
42
41
  const withApp = registered ? server : { ...server, apps: [...server.apps, app] };
43
- const prepare = vaulted(run(`Register ${app.name} and check its values`, [
42
+ // Which secrets are set shows without the key: SOPS leaves secrets.yml's keys readable.
43
+ const prepare = run(`Register ${app.name} and check its values`, [
44
44
  process.execPath,
45
45
  within(engine.kayra, "bin", "kayra-app-launch.js"),
46
46
  JSON.stringify({ root, server: server.name, entry: raw, ...(wanted.dir === undefined ? {} : { dir: wanted.dir }) }),
47
- ]), server);
47
+ ]);
48
48
  const hook = takesWebhooks(server).ok && forgeRepo(app.repo) !== undefined ? appHook(engine, root, withApp, app.name) : ok([]);
49
49
  return flatMap(apps(engine, withApp), (applied) => flatMap(deployer(server, { kind: "deploy", app: app.name, more: undefined }), (deploy) => map(hook, (hooked) => typed([prepare, ...applied, ...deploy, ...hooked]))));
50
50
  });
@@ -8,26 +8,13 @@ export const script = ({ kayra }, name) => within(kayra, "scripts", "local", nam
8
8
  export const ansiblePath = ({ kayra }, ...segments) => within(kayra, "ansible", ...segments);
9
9
  export const inventory = (server) => within(server.dir, "inventory.ini");
10
10
  export const terraform = (server) => `-chdir=${within(server.dir, "terraform")}`;
11
- /**
12
- * How Ansible gets the vault password: from the server's password file, or
13
- * from kayra, which asks the operator once per run (Need "vault").
14
- */
15
- export const vault = (server) => {
16
- switch (server.vault.kind) {
17
- case "file":
18
- return typed({ env: { ANSIBLE_VAULT_PASSWORD_FILE: server.vault.file }, needs: [] });
19
- case "ask":
20
- return typed({ env: {}, needs: [{ kind: "vault", server: server.name }] });
21
- case "none":
22
- return typed({ env: {}, needs: [] });
23
- }
24
- };
25
- export const needing = (step, needs) => typed({ ...step, needs });
26
- /** The vault's environment, and its need where kayra must ask for the password. */
27
- export const vaulted = (step, server) => {
28
- const { env, needs } = vault(server);
29
- return typed({ ...step, env: { ...env, ...step.env }, ...(needs.length === 0 ? {} : { needs }) });
30
- };
11
+ /** secrets.yml opened for a step: for Ansible as a file (KAYRA_SECRETS), for OpenTofu as its variables. */
12
+ export const secretsFor = (server, use) => typed({ kind: "secrets", server: server.name, file: server.secrets.file, key: server.secrets.key, encrypted: server.secrets.encrypted, for: use });
13
+ export const needing = (step, needs) => typed({ ...step, needs: [...(step.needs ?? []), ...needs] });
14
+ /** A step that reads the server's secrets.yml: Ansible's playbooks load it from KAYRA_SECRETS. */
15
+ export const withSecrets = (step, server) => needing(step, [secretsFor(server, "ansible")]);
16
+ /** An OpenTofu step: its variables from secrets.yml's terraform mapping (the state's passphrase among them). */
17
+ export const withVariables = (step, server) => needing(step, [secretsFor(server, "opentofu")]);
31
18
  export const ansibleEnv = (engine, env) => typed({ ...env, ANSIBLE_CONFIG: ansiblePath(engine, "ansible.cfg") });
32
19
  /** --verbose: every playbook prints Ansible's own output, with -v, instead of kayra's summary of it (ansible.cfg's stdout_callback). */
33
20
  export const verbose = (plan) => typed(plan.map((step) => step.kind === "run" && step.argv[0] === "ansible-playbook"