@yunuseon/kayra 0.18.0 → 0.20.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 (164) hide show
  1. package/agent/skills/kayra-release/SKILL.md +1 -4
  2. package/agent/skills/kayra-rollout/SKILL.md +4 -6
  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 +33 -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/defaults/main.yml +3 -0
  10. package/ansible/roles/platform/tasks/main.yml +6 -84
  11. package/ansible/roles/platform/templates/platform.env.j2 +2 -0
  12. package/bin/kayra-app-hook.js +6 -0
  13. package/bin/kayra-app-init.js +6 -0
  14. package/bin/kayra-app-launch.js +6 -0
  15. package/bin/kayra-app-values.js +6 -0
  16. package/cli/dist/app-hook.js +75 -0
  17. package/cli/dist/app-init.js +65 -0
  18. package/cli/dist/app-launch.js +89 -0
  19. package/cli/dist/app-values.js +37 -0
  20. package/cli/dist/browser/assets/ibm-plex-sans-cyrillic-400-normal-BTotfTJu.woff +0 -0
  21. package/cli/dist/browser/assets/ibm-plex-sans-cyrillic-400-normal-DZqxrq2p.woff2 +0 -0
  22. package/cli/dist/browser/assets/ibm-plex-sans-cyrillic-600-normal-71GNu3SW.woff2 +0 -0
  23. package/cli/dist/browser/assets/ibm-plex-sans-cyrillic-600-normal-BGq0mW3O.woff +0 -0
  24. package/cli/dist/browser/assets/ibm-plex-sans-cyrillic-ext-400-normal-Dsrv2Tcn.woff +0 -0
  25. package/cli/dist/browser/assets/ibm-plex-sans-cyrillic-ext-400-normal-g30qAdWV.woff2 +0 -0
  26. package/cli/dist/browser/assets/ibm-plex-sans-cyrillic-ext-600-normal-Bz0x94Yp.woff +0 -0
  27. package/cli/dist/browser/assets/ibm-plex-sans-cyrillic-ext-600-normal-DUMzJB7m.woff2 +0 -0
  28. package/cli/dist/browser/assets/ibm-plex-sans-greek-400-normal-D9ESIMu3.woff +0 -0
  29. package/cli/dist/browser/assets/ibm-plex-sans-greek-400-normal-_efipK4i.woff2 +0 -0
  30. package/cli/dist/browser/assets/ibm-plex-sans-greek-600-normal-D-CqTdkO.woff +0 -0
  31. package/cli/dist/browser/assets/ibm-plex-sans-greek-600-normal-DzTrcv_p.woff2 +0 -0
  32. package/cli/dist/browser/assets/ibm-plex-sans-latin-400-normal-CDDApCn2.woff2 +0 -0
  33. package/cli/dist/browser/assets/ibm-plex-sans-latin-400-normal-CYLoc0-x.woff +0 -0
  34. package/cli/dist/browser/assets/ibm-plex-sans-latin-600-normal-Cu4Hd6ag.woff +0 -0
  35. package/cli/dist/browser/assets/ibm-plex-sans-latin-600-normal-CuJfVYMP.woff2 +0 -0
  36. package/cli/dist/browser/assets/ibm-plex-sans-latin-ext-400-normal-C5H60-Va.woff2 +0 -0
  37. package/cli/dist/browser/assets/ibm-plex-sans-latin-ext-400-normal-RBey6euL.woff +0 -0
  38. package/cli/dist/browser/assets/ibm-plex-sans-latin-ext-600-normal-DIrixKbi.woff +0 -0
  39. package/cli/dist/browser/assets/ibm-plex-sans-latin-ext-600-normal-DOrvGEcy.woff2 +0 -0
  40. package/cli/dist/browser/assets/ibm-plex-sans-vietnamese-400-normal-DG4YqDda.woff2 +0 -0
  41. package/cli/dist/browser/assets/ibm-plex-sans-vietnamese-400-normal-fK1oJ5dG.woff +0 -0
  42. package/cli/dist/browser/assets/ibm-plex-sans-vietnamese-600-normal-DgdngZtN.woff +0 -0
  43. package/cli/dist/browser/assets/ibm-plex-sans-vietnamese-600-normal-DpPYBSTl.woff2 +0 -0
  44. package/cli/dist/browser/assets/index-DzIVPXyL.css +1 -0
  45. package/cli/dist/browser/assets/index-SP8bCDrl.js +7 -0
  46. package/cli/dist/browser/assets/jetbrains-mono-cyrillic-400-normal-BEIGL1Tu.woff2 +0 -0
  47. package/cli/dist/browser/assets/jetbrains-mono-cyrillic-400-normal-ugxPyKxw.woff +0 -0
  48. package/cli/dist/browser/assets/jetbrains-mono-cyrillic-500-normal-DJqRU3vO.woff +0 -0
  49. package/cli/dist/browser/assets/jetbrains-mono-cyrillic-500-normal-DmUKJPL_.woff2 +0 -0
  50. package/cli/dist/browser/assets/jetbrains-mono-cyrillic-600-normal-8K4wrrwR.woff +0 -0
  51. package/cli/dist/browser/assets/jetbrains-mono-cyrillic-600-normal-EVf6-Yzo.woff2 +0 -0
  52. package/cli/dist/browser/assets/jetbrains-mono-cyrillic-700-normal-BWTpRfYl.woff2 +0 -0
  53. package/cli/dist/browser/assets/jetbrains-mono-cyrillic-700-normal-CEoEElIJ.woff +0 -0
  54. package/cli/dist/browser/assets/jetbrains-mono-greek-400-normal-B9oWc5Lo.woff +0 -0
  55. package/cli/dist/browser/assets/jetbrains-mono-greek-400-normal-C190GLew.woff2 +0 -0
  56. package/cli/dist/browser/assets/jetbrains-mono-greek-500-normal-D7SFKleX.woff +0 -0
  57. package/cli/dist/browser/assets/jetbrains-mono-greek-500-normal-JpySY46c.woff2 +0 -0
  58. package/cli/dist/browser/assets/jetbrains-mono-greek-600-normal-H7WoG9Et.woff2 +0 -0
  59. package/cli/dist/browser/assets/jetbrains-mono-greek-600-normal-mc2nkWzM.woff +0 -0
  60. package/cli/dist/browser/assets/jetbrains-mono-greek-700-normal-C6CZE3T8.woff2 +0 -0
  61. package/cli/dist/browser/assets/jetbrains-mono-greek-700-normal-DEigVDxa.woff +0 -0
  62. package/cli/dist/browser/assets/jetbrains-mono-latin-400-normal-6-qcROiO.woff +0 -0
  63. package/cli/dist/browser/assets/jetbrains-mono-latin-400-normal-V6pRDFza.woff2 +0 -0
  64. package/cli/dist/browser/assets/jetbrains-mono-latin-500-normal-BWZEU5yA.woff2 +0 -0
  65. package/cli/dist/browser/assets/jetbrains-mono-latin-500-normal-CJOVTJB7.woff +0 -0
  66. package/cli/dist/browser/assets/jetbrains-mono-latin-600-normal-BfsvjouI.woff +0 -0
  67. package/cli/dist/browser/assets/jetbrains-mono-latin-600-normal-C8RAYTDA.woff2 +0 -0
  68. package/cli/dist/browser/assets/jetbrains-mono-latin-700-normal-BYuf6tUa.woff2 +0 -0
  69. package/cli/dist/browser/assets/jetbrains-mono-latin-700-normal-D3wTyLJW.woff +0 -0
  70. package/cli/dist/browser/assets/jetbrains-mono-latin-ext-400-normal-Bc8Ftmh3.woff2 +0 -0
  71. package/cli/dist/browser/assets/jetbrains-mono-latin-ext-400-normal-fXTG6kC5.woff +0 -0
  72. package/cli/dist/browser/assets/jetbrains-mono-latin-ext-500-normal-Cut-4mMH.woff2 +0 -0
  73. package/cli/dist/browser/assets/jetbrains-mono-latin-ext-500-normal-ckzbgY84.woff +0 -0
  74. package/cli/dist/browser/assets/jetbrains-mono-latin-ext-600-normal-BfB_LPfz.woff2 +0 -0
  75. package/cli/dist/browser/assets/jetbrains-mono-latin-ext-600-normal-DObL3zCW.woff +0 -0
  76. package/cli/dist/browser/assets/jetbrains-mono-latin-ext-700-normal-CZipNAKV.woff2 +0 -0
  77. package/cli/dist/browser/assets/jetbrains-mono-latin-ext-700-normal-CxPITLHs.woff +0 -0
  78. package/cli/dist/browser/assets/jetbrains-mono-vietnamese-400-normal-CqNFfHCs.woff +0 -0
  79. package/cli/dist/browser/assets/jetbrains-mono-vietnamese-500-normal-DNRqzVM1.woff +0 -0
  80. package/cli/dist/browser/assets/jetbrains-mono-vietnamese-600-normal-OWROknRo.woff +0 -0
  81. package/cli/dist/browser/assets/jetbrains-mono-vietnamese-700-normal-BDLVIk2r.woff +0 -0
  82. package/cli/dist/browser/index.html +10 -3
  83. package/cli/dist/clients/api/start.js +1 -1
  84. package/cli/dist/clients/cli/invocation.js +25 -0
  85. package/cli/dist/clients/cli/parser.js +24 -7
  86. package/cli/dist/clients/cli/status.js +28 -7
  87. package/cli/dist/clients/cli/text.js +3 -3
  88. package/cli/dist/clients/cli/values.js +0 -1
  89. package/cli/dist/clients/cli/views.js +9 -2
  90. package/cli/dist/clients/mcp/tools.js +8 -1
  91. package/cli/dist/clients/ui/assets.js +1 -0
  92. package/cli/dist/clients/wire/contract.js +2 -2
  93. package/cli/dist/clients/wire/queries.js +1 -0
  94. package/cli/dist/clients/wire/schemas.js +6 -2
  95. package/cli/dist/clients/wire/status.js +7 -1
  96. package/cli/dist/clients/wire/words.js +14 -0
  97. package/cli/dist/core/adapters/network.js +20 -1
  98. package/cli/dist/core/adapters/opentofu.js +1 -1
  99. package/cli/dist/core/app-hook/hooks.js +90 -0
  100. package/cli/dist/core/app-hook/token.js +49 -0
  101. package/cli/dist/core/app-init/plan.js +208 -0
  102. package/cli/dist/core/app-values/model.js +45 -0
  103. package/cli/dist/core/app-values/store.js +124 -0
  104. package/cli/dist/core/ctl/answers.js +14 -9
  105. package/cli/dist/core/fleet/brand.js +10 -7
  106. package/cli/dist/core/fleet/parse.js +10 -1
  107. package/cli/dist/core/fleet/settings.js +32 -12
  108. package/cli/dist/core/operations/catalog.js +46 -1
  109. package/cli/dist/core/operations/dispatch.js +40 -9
  110. package/cli/dist/core/recipes/app-check.js +14 -1
  111. package/cli/dist/core/recipes/app-hook.js +26 -0
  112. package/cli/dist/core/recipes/app-values.js +21 -0
  113. package/cli/dist/core/recipes/doctor.js +5 -5
  114. package/cli/dist/core/recipes/host.js +8 -3
  115. package/cli/dist/core/recipes/launch.js +51 -0
  116. package/cli/dist/core/status/look.js +33 -13
  117. package/cli/dist/core/status/model.js +2 -0
  118. package/cli/dist/core/validation/facts.js +1 -3
  119. package/cli/dist/deployer/brand.js +9 -1
  120. package/cli/dist/deployer/build/builder.js +15 -4
  121. package/cli/dist/deployer/build/bundle.js +5 -0
  122. package/cli/dist/deployer/build/client.js +44 -0
  123. package/cli/dist/deployer/build/compose.js +21 -8
  124. package/cli/dist/deployer/build/routes.js +149 -0
  125. package/cli/dist/deployer/ctl/answers.js +6 -2
  126. package/cli/dist/deployer/ctl/ctl.js +66 -9
  127. package/cli/dist/deployer/deployer.js +114 -191
  128. package/cli/dist/deployer/forges/forge.js +0 -12
  129. package/cli/dist/deployer/forges/notify.js +3 -3
  130. package/cli/dist/deployer/http/http.js +0 -1
  131. package/cli/dist/deployer/http/page.js +4 -2
  132. package/cli/dist/deployer/http/webhook.js +0 -1
  133. package/cli/dist/deployer/runs/effects.js +70 -0
  134. package/cli/dist/deployer/runs/queue.js +5 -4
  135. package/cli/dist/deployer/runs/records.js +22 -3
  136. package/cli/dist/deployer/runs/restore.js +6 -0
  137. package/cli/dist/deployer/runs/run.js +64 -11
  138. package/cli/dist/deployer/runs/store.js +19 -3
  139. package/cli/dist/deployer/settings.js +69 -0
  140. package/cli/dist/lib/forge-repo.js +15 -0
  141. package/cli/dist/lib/names.js +13 -0
  142. package/cli/dist/main.js +46 -25
  143. package/compose/docker-compose.ci.yml +2 -0
  144. package/package.json +2 -2
  145. package/schema/app.json +69 -8
  146. package/schema/defaults.json +8 -0
  147. package/schema/vars-platform.json +8 -0
  148. package/scripts/local/app-check.sh +12 -9
  149. package/scripts/local/app-manifests.sh +11 -6
  150. package/scripts/local/verify.sh +30 -12
  151. package/scripts/platform/compose-policy.sh +37 -40
  152. package/terraform/modules/platform-dns/main.tf +66 -0
  153. package/terraform/modules/platform-dns/outputs.tf +9 -0
  154. package/terraform/modules/platform-dns/tests/fleet/.cache/apps/blog/site/app.yml +3 -0
  155. package/terraform/modules/platform-dns/tests/fleet/.cache/apps/shop/app.yml +6 -0
  156. package/terraform/modules/platform-dns/tests/fleet/servers/nested/server.yml +6 -0
  157. package/terraform/modules/platform-dns/tests/fleet/servers/shop/server.yml +7 -0
  158. package/terraform/modules/platform-dns/tests/fleet/servers/uncloned/server.yml +6 -0
  159. package/terraform/modules/platform-dns/tests/platform-dns.tftest.hcl +64 -0
  160. package/terraform/modules/platform-dns/variables.tf +25 -0
  161. package/cli/dist/browser/assets/index-Ct0Eudk4.css +0 -1
  162. package/cli/dist/browser/assets/index-hcyM6f6T.js +0 -7
  163. package/compose/traefik/dynamic/ratelimit.yml +0 -21
  164. package/scripts/agent/guard.sh +0 -125
@@ -1,79 +1,33 @@
1
1
  import { randomBytes } from "node:crypto";
2
- import { appendFileSync, existsSync, mkdirSync, mkdtempSync, readdirSync, readFileSync, rmSync, writeFileSync } from "node:fs";
2
+ import { existsSync, mkdtempSync, readdirSync, rmSync } from "node:fs";
3
3
  import { createServer } from "node:http";
4
4
  import { tmpdir } from "node:os";
5
- import { dirname, join } from "node:path";
6
- import { createInterface } from "node:readline";
7
- import { Readable } from "node:stream";
8
- import { all, err, ok } from "../lib/result.js";
5
+ import { join } from "node:path";
6
+ import { ok } from "../lib/result.js";
9
7
  import { field, registered } from "./app.js";
10
- import { appName, appOwner, newRunId, sha } from "./brand.js";
11
- import { matches, parseCron } from "./cron.js";
12
- import { forge, forgeRepo } from "./forges/forge.js";
8
+ import { appName, appOwner, newRunId } from "./brand.js";
9
+ import { builderResolve } from "./build/client.js";
10
+ import { matches } from "./cron.js";
13
11
  import { json, send } from "./http/http.js";
14
- import { spawnLines } from "./process.js";
15
12
  import { describe, empty, enqueue, finish, jobRequest } from "./runs/queue.js";
16
- import { expired, moved, releaseName, releases } from "./runs/records.js";
17
- import { githubIssues } from "./forges/issues.js";
18
- import { decide, parseNotify, silent } from "./forges/notify.js";
13
+ import { ended, moved, releases } from "./runs/records.js";
14
+ import { decide } from "./forges/notify.js";
15
+ import { effects, runPage } from "./runs/effects.js";
19
16
  import { deploy, renovate } from "./runs/run.js";
20
- import { runsOf } from "./runs/store.js";
17
+ import { toRestore } from "./runs/restore.js";
18
+ import { spawnLines } from "./process.js";
19
+ import { appendLog, meta, owners, prune, runsOf, writeMeta } from "./runs/store.js";
20
+ import { runsDir, settings, token } from "./settings.js";
21
21
  import { typed } from "../lib/typed.js";
22
- const token = (name) => {
23
- const path = `/run/platform-secrets/${name}`;
24
- return existsSync(path) ? readFileSync(path, "utf8").trim() : "";
25
- };
26
- const must = (result) => {
27
- if (!result.ok)
28
- throw new Error(result.error);
29
- return result.value;
30
- };
31
- const settings = (env) => {
32
- const scripts = env["PLATFORM_SCRIPTS"] ?? "/opt/kayra/scripts/platform";
33
- const strings = Object.fromEntries(Object.entries(env).flatMap(([key, value]) => typed(value === undefined ? [] : [[key, value]])));
34
- return typed({
35
- platformDir: env["PLATFORM_DIR"] ?? "/opt/platform",
36
- appsDir: env["APPS_DIR"] ?? "/opt/apps",
37
- builder: env["BUILDER_URL"] ?? "http://builder:8081",
38
- domain: env["DOMAIN"] ?? "localhost",
39
- policyScript: join(scripts, "compose-policy.sh"),
40
- gcScript: join(scripts, "registry-gc.sh"),
41
- renovateImage: env["RENOVATE_IMAGE"] ?? "",
42
- renovate: env["PLATFORM_RENOVATE"] === "true" ? must(parseCron(env["RENOVATE_SCHEDULE"] ?? "0 4 * * 1")) : undefined,
43
- gc: must(parseCron("0 3 * * *")),
44
- poll: must(parseCron("17 * * * *")),
45
- env: strings,
46
- server: env["SERVER"] ?? env["DOMAIN"] ?? "this server",
47
- notifier: notifierFor(must(parseNotify(env["NOTIFIER"])), env["PLATFORM_DIR"] ?? "/opt/platform"),
48
- });
49
- };
50
- /** The configured notifier; its open alerts are kept in <platformDir>/alerts/<key>. */
51
- const notifierFor = (config, platformDir) => {
52
- if (config.kind === "none")
53
- return silent;
54
- const alerts = join(platformDir, "alerts");
55
- const keyFile = "/run/deployer-secrets/NOTIFIER_GITHUB_APP_KEY";
56
- return githubIssues(config.repository, config.appId, {
57
- fetch: async (url, init) => {
58
- const response = await fetch(url, init);
59
- return { status: response.status, json: await response.json().catch(() => undefined) };
60
- },
61
- privateKey: () => (existsSync(keyFile) ? ok(readFileSync(keyFile, "utf8")) : err(`${keyFile} is missing: notifier_github_app_key in secrets.yml`)),
62
- now: () => Date.now(),
63
- openIssue: (key) => {
64
- const path = join(alerts, key);
65
- const number = existsSync(path) ? Number(readFileSync(path, "utf8")) : Number.NaN;
66
- return Number.isInteger(number) ? number : undefined;
67
- },
68
- remember: (key, issue) => {
69
- mkdirSync(alerts, { recursive: true });
70
- if (issue === undefined)
71
- rmSync(join(alerts, key), { force: true });
72
- else
73
- writeFileSync(join(alerts, key), String(issue));
74
- },
75
- });
76
- };
22
+ /**
23
+ * The deployer: the half that holds the docker socket and the app secrets.
24
+ * Never routed by Traefik. It queues runs, asks the builder for bundles,
25
+ * admits and starts them, records releases, posts commit statuses, and runs
26
+ * the platform's scheduled jobs (registry GC, Renovate, the hourly poll).
27
+ *
28
+ * :8090 platform-internal: POST /push/<app> (the builder, after a webhook)
29
+ * :8091 inside the container only: POST /deploy, POST /job (kayra app …, kayra job …)
30
+ */
77
31
  /** Raises or resolves what the finished run means; a notifier's failure is logged, never the run's. */
78
32
  const notify = async (s, end, log) => {
79
33
  const decision = decide(end);
@@ -85,7 +39,6 @@ const notify = async (s, end, log) => {
85
39
  if (!done.ok)
86
40
  log(`(alert not sent: ${done.error})`);
87
41
  };
88
- const runsDir = (s) => join(s.platformDir, "runs");
89
42
  const keepRuns = 50;
90
43
  const registeredApps = (s) => {
91
44
  const dir = join(s.platformDir, "apps");
@@ -95,83 +48,6 @@ const registeredApps = (s) => {
95
48
  return app?.ok === true ? Object.freeze([app.value]) : Object.freeze([]);
96
49
  }));
97
50
  };
98
- const builderResolve = async (s, app, ref) => {
99
- const response = await fetch(`${s.builder}/resolve`, { method: "POST", body: JSON.stringify({ app: app.name, ref }) }).catch(() => undefined);
100
- if (response === undefined)
101
- return err("the builder did not answer");
102
- const answer = await response.json().catch(() => undefined);
103
- const given = typeof answer === "object" && answer !== null ? field(answer, "sha") : undefined;
104
- const problem = typeof answer === "object" && answer !== null ? field(answer, "error") : undefined;
105
- return typeof given === "string" ? sha(given) : err(typeof problem === "string" ? problem : `resolve: HTTP ${String(response.status)}`);
106
- };
107
- /** The builder's NDJSON: log lines as they come, then the bundle or an error. */
108
- const builderPrepare = async (s, app, commit, log) => {
109
- const response = await fetch(`${s.builder}/prepare`, {
110
- method: "POST",
111
- body: JSON.stringify({ app: app.name, sha: commit }),
112
- signal: AbortSignal.timeout(45 * 60_000),
113
- }).catch(() => undefined);
114
- if (response?.body == null || !response.ok)
115
- return err("the builder did not prepare a bundle");
116
- const lines = createInterface({ input: Readable.fromWeb(response.body) });
117
- const outcome = await Array.fromAsync(lines, (line) => {
118
- try {
119
- const message = JSON.parse(line);
120
- if (typeof message !== "object" || message === null)
121
- return undefined;
122
- const text = field(message, "log");
123
- if (typeof text === "string")
124
- log(text);
125
- const bundle = field(message, "bundle");
126
- const problem = field(message, "error");
127
- return bundle !== undefined ? ok(bundle) : typeof problem === "string" ? err(problem) : undefined;
128
- }
129
- catch {
130
- return undefined;
131
- }
132
- });
133
- return outcome.findLast((item) => item !== undefined) ?? err("the builder stopped without a bundle");
134
- };
135
- const appSecrets = (app) => {
136
- const read = all(app.secrets.map((key) => {
137
- const path = join("/run/app-secrets", app.name, key);
138
- return existsSync(path) ? ok([key, readFileSync(path, "utf8")]) : err(`${app.name}: secret ${key} is missing; run kayra apps`);
139
- }));
140
- return read.ok ? ok(Object.fromEntries(read.value)) : read;
141
- };
142
- const target = (s, who, id) => `https://deploy.${s.domain}/runs/${who}/${id}`;
143
- const effects = (s, who, id, log) => typed({
144
- log,
145
- resolve: (app, ref) => builderResolve(s, app, ref),
146
- prepare: (app, commit) => builderPrepare(s, app, commit, log),
147
- exec: (argv, options) => spawnLines(argv, { ...options, timeoutMs: 20 * 60_000 }, log),
148
- secrets: appSecrets,
149
- workdir: (bundle, images) => {
150
- const dir = mkdtempSync(join(tmpdir(), `${id}-`));
151
- writeFileSync(join(dir, "images.env"), images);
152
- bundle.compose.forEach((file) => {
153
- mkdirSync(dirname(join(dir, file.path)), { recursive: true });
154
- writeFileSync(join(dir, file.path), file.content);
155
- });
156
- return Promise.resolve(dir);
157
- },
158
- record: (app, commit, images) => {
159
- const dir = join(s.platformDir, "releases", app.name);
160
- mkdirSync(dir, { recursive: true });
161
- writeFileSync(join(dir, releaseName(new Date(), commit)), images);
162
- return Promise.resolve();
163
- },
164
- status: async (app, commit, context, state, description) => {
165
- const repo = forgeRepo(app.repo);
166
- if (repo === undefined) {
167
- log(`(no commit status: ${app.repo} is on no forge kayra knows)`);
168
- return;
169
- }
170
- const posted = await forge(repo, token("GIT_TOKEN"), fetch).postStatus(commit, { context, state, description, target: target(s, who, id) });
171
- if (!posted.ok)
172
- log(`(commit status ${context} not posted: ${posted.error})`);
173
- },
174
- });
175
51
  const work = async (s, request, fx) => {
176
52
  if (request.work.kind === "deploy") {
177
53
  const app = registered(s.platformDir, request.work.app);
@@ -179,7 +55,7 @@ const work = async (s, request, fx) => {
179
55
  fx.log(app.error);
180
56
  return typed({ ok: false });
181
57
  }
182
- return deploy(app.value, request.work.ref, { appsDir: s.appsDir, policyScript: s.policyScript }, fx);
58
+ return deploy(app.value, request.work.ref, { appsDir: s.appsDir, policyScript: s.policyScript, domain: s.domain }, fx);
183
59
  }
184
60
  const argv = request.work.job === "gc" ? ["bash", s.gcScript] : renovate(s.env, request.work.dryRun, s.renovateImage);
185
61
  const env = request.work.job === "renovate" ? { RENOVATE_TOKEN: token("RENOVATE_TOKEN") } : {};
@@ -189,70 +65,114 @@ const work = async (s, request, fx) => {
189
65
  /** The daemon's one mutable cell: the queue, swapped whole on every change. */
190
66
  // eslint-disable-next-line functional/prefer-immutable-types -- the daemon's state lives here, nowhere else
191
67
  const cell = { queue: empty(2) };
192
- /** Applies a change to the queue and starts what it lets start; returns those. */
68
+ const asked = (request, state) => typed({ id: request.id, owner: request.owner, ...describe(request), state, started: new Date().toISOString() });
69
+ /** Applies a change to the queue, starts what it lets start, and ends what it replaced; returns what started. */
193
70
  const swap = (s, change) => {
194
71
  const next = change(cell.queue);
195
72
  // eslint-disable-next-line functional/immutable-data -- the daemon's state lives here, nowhere else
196
73
  cell.queue = next.queue;
74
+ (next.replaced ?? []).forEach((request) => {
75
+ const was = meta(runsDir(s), request.owner, request.id);
76
+ writeMeta(runsDir(s), request.owner, { ...(was ?? asked(request, "superseded")), state: "superseded", finished: new Date().toISOString() });
77
+ });
197
78
  next.start.forEach((request) => {
198
79
  void start(s, request);
199
80
  });
200
81
  return next.start;
201
82
  };
83
+ /** A request on record as queued before the queue sees it, so whoever asked can follow it by its id at once. */
84
+ const ask = (s, request) => {
85
+ writeMeta(runsDir(s), request.owner, asked(request, "queued"));
86
+ return swap(s, (queue) => enqueue(queue, request)).includes(request);
87
+ };
88
+ const newId = () => newRunId(new Date(), randomBytes(3).toString("hex"));
89
+ /** Runs a restart cut short: they never end otherwise, and whoever follows one would wait forever. */
90
+ const abandon = (s) => {
91
+ const runs = runsDir(s);
92
+ owners(runs).forEach((who) => {
93
+ runsOf(runs, who)
94
+ .filter((run) => !ended(run.state))
95
+ .forEach((run) => {
96
+ writeMeta(runs, who, { ...run, state: "failure", error: "the deployer restarted before this run ended", finished: new Date().toISOString() });
97
+ });
98
+ });
99
+ };
202
100
  const start = async (s, request) => {
203
- const id = newRunId(new Date(), randomBytes(3).toString("hex"));
204
- const dir = join(runsDir(s), request.owner, id);
205
- mkdirSync(dir, { recursive: true });
206
- const { what, why } = describe(request);
207
- const begun = { id, owner: request.owner, what, why, state: "running", started: new Date().toISOString() };
208
- writeFileSync(join(dir, "meta.json"), JSON.stringify(begun));
101
+ const id = request.id;
102
+ const runs = runsDir(s);
103
+ const begun = asked(request, "running");
104
+ writeMeta(runs, request.owner, begun);
209
105
  const log = (line) => {
210
- appendFileSync(join(dir, "log"), `${new Date().toISOString().slice(11, 19)} ${line}\n`);
106
+ appendLog(runs, request.owner, id, line);
211
107
  };
212
- const outcome = await work(s, request, effects(s, request.owner, id, log)).catch((error) => {
108
+ const scratch = mkdtempSync(join(tmpdir(), `${id}-`));
109
+ const outcome = await work(s, request, effects(s, request.owner, id, scratch, log)).catch((error) => {
213
110
  log(`deployer: ${String(error)}`);
214
- return typed({ ok: false });
111
+ return typed({ ok: false, error: "the deployer failed; see the log" });
215
112
  });
216
- const ended = {
113
+ writeMeta(runs, request.owner, {
217
114
  ...begun,
218
115
  ...(outcome.sha === undefined ? {} : { sha: outcome.sha }),
219
116
  state: outcome.ok ? "success" : "failure",
220
117
  finished: new Date().toISOString(),
221
- };
222
- writeFileSync(join(dir, "meta.json"), JSON.stringify(ended));
223
- await notify(s, { server: s.server, request, ok: outcome.ok, ...(outcome.sha === undefined ? {} : { sha: outcome.sha }), page: target(s, request.owner, id) }, log);
224
- expired(readdirSync(join(runsDir(s), request.owner)), keepRuns).forEach((old) => {
225
- rmSync(join(runsDir(s), request.owner, old), { recursive: true, force: true });
226
- });
227
- readdirSync(tmpdir())
228
- .filter((name) => name.startsWith(`${id}-`))
229
- .forEach((name) => {
230
- rmSync(join(tmpdir(), name), { recursive: true, force: true });
118
+ ...(outcome.error === undefined ? {} : { error: outcome.error }),
119
+ ...(outcome.urls === undefined ? {} : { urls: outcome.urls }),
120
+ ...(outcome.checks === undefined ? {} : { checks: outcome.checks }),
231
121
  });
122
+ await notify(s, { server: s.server, request, ok: outcome.ok, ...(outcome.sha === undefined ? {} : { sha: outcome.sha }), page: runPage(s, request.owner, id) }, log);
123
+ prune(runs, request.owner, keepRuns);
124
+ rmSync(scratch, { recursive: true, force: true });
232
125
  swap(s, (queue) => finish(queue, request.owner));
233
126
  };
234
127
  const deployRequest = (app, ref, why) => typed({
128
+ id: newId(),
235
129
  owner: appOwner(app.name),
236
130
  work: { kind: "deploy", app: app.name, ref, why },
237
131
  });
238
- /** 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). */
132
+ /** The webhook's fallback: an app whose ref moved to a commit no run has tried yet and no release runs (runs are pruned, releases are not). */
239
133
  const poll = async (s) => {
240
134
  await Promise.all(registeredApps(s).map(async (app) => {
241
- const commit = await builderResolve(s, app, app.ref);
135
+ const commit = await builderResolve(s.builder, app, app.ref);
242
136
  const lastRun = runsOf(runsDir(s), appOwner(app.name)).find((run) => run.sha !== undefined);
243
137
  const releasesDir = join(s.platformDir, "releases", app.name);
244
138
  const lastRelease = releases(existsSync(releasesDir) ? readdirSync(releasesDir) : [])[0];
245
139
  if (commit.ok && moved(commit.value, lastRun?.sha, lastRelease?.sha))
246
- swap(s, (queue) => enqueue(queue, deployRequest(app, app.ref, "poll")));
140
+ ask(s, deployRequest(app, app.ref, "poll"));
247
141
  }));
248
142
  };
143
+ /** Whether any container of an app's compose project runs (or is restarting). */
144
+ const running = async (app) => {
145
+ const exit = await spawnLines(["docker", "ps", "-q", "--filter", `label=com.docker.compose.project=${app}`, "--filter", "status=running", "--filter", "status=restarting"], { cwd: "/", env: {}, capture: true, timeoutMs: 30_000 }, () => undefined);
146
+ return exit.code !== 0 || exit.stdout.trim() !== "";
147
+ };
148
+ /**
149
+ * After the deployer starts (and Docker with it): each app that was deployed
150
+ * and runs no container now, deployed again at the release that ran. A Docker
151
+ * update or a reboot stops every container; an app deployed before the
152
+ * platform gave every service a restart policy would otherwise stay down.
153
+ */
154
+ const restore = async (s) => {
155
+ const apps = registeredApps(s);
156
+ const states = await Promise.all(apps.map(async (app) => [app.name, await running(app.name)]));
157
+ const up = new Map(states);
158
+ toRestore(apps.map((app) => {
159
+ const dir = join(s.platformDir, "releases", app.name);
160
+ return typed({ app: app.name, live: releases(existsSync(dir) ? readdirSync(dir) : [])[0]?.sha });
161
+ }), (app) => up.get(app) ?? true).forEach(({ app, live }) => {
162
+ const registration = apps.find((each) => each.name === app);
163
+ if (registration !== undefined)
164
+ ask(s, deployRequest(registration, live, "restore"));
165
+ });
166
+ };
167
+ /** Long enough for Docker to have started the containers with a restart policy itself. */
168
+ const restoreAfter = 30_000;
249
169
  const tick = (s, at) => {
250
170
  if (matches(s.poll, at))
251
171
  void poll(s);
252
172
  if (matches(s.gc, at))
253
- swap(s, (queue) => enqueue(queue, jobRequest("gc", false, "schedule")));
173
+ ask(s, jobRequest(newId(), "gc", false, "schedule"));
254
174
  if (s.renovate !== undefined && matches(s.renovate, at))
255
- swap(s, (queue) => enqueue(queue, jobRequest("renovate", false, "schedule")));
175
+ ask(s, jobRequest(newId(), "renovate", false, "schedule"));
256
176
  };
257
177
  const schedule = (s) => {
258
178
  const wait = 60_000 - (Date.now() % 60_000) + 500;
@@ -262,15 +182,29 @@ const schedule = (s) => {
262
182
  }, wait);
263
183
  };
264
184
  const handle = (handler) => (request, response) => {
265
- handler(request, response).catch((error) => {
185
+ Promise.resolve(handler(request, response)).catch((error) => {
266
186
  console.error(error);
267
187
  if (!response.headersSent)
268
188
  send(response, 500, "internal error\n");
269
189
  });
270
190
  };
271
191
  const accept = (s, response, request) => {
272
- const started = swap(s, (queue) => enqueue(queue, request)).includes(request);
273
- send(response, 202, started ? `${request.owner}: started\n` : `${request.owner}: queued, after what runs now\n`);
192
+ const started = ask(s, request);
193
+ send(response, 202, `${JSON.stringify({ id: request.id, owner: request.owner, state: started ? "running" : "queued" })}\n`);
194
+ };
195
+ /** POST /push/<app>, from the builder once a webhook proved itself: a deploy of the app's own ref. */
196
+ const push = (s, request, response) => {
197
+ const name = /^\/push\/([^/]+)$/.exec(new URL(request.url ?? "/", "http://deployer").pathname)?.[1];
198
+ const app = name === undefined || !appName(name).ok ? undefined : registered(s.platformDir, name);
199
+ if (request.method !== "POST" || app === undefined) {
200
+ send(response, 404, "not found\n");
201
+ return;
202
+ }
203
+ if (!app.ok) {
204
+ send(response, 404, `${app.error}\n`);
205
+ return;
206
+ }
207
+ accept(s, response, deployRequest(app.value, app.value.ref, "push"));
274
208
  };
275
209
  const control = async (s, request, response) => {
276
210
  const path = new URL(request.url ?? "/", "http://deployer").pathname;
@@ -297,28 +231,17 @@ const control = async (s, request, response) => {
297
231
  send(response, 409, "this server has no renovate_repositories\n");
298
232
  return;
299
233
  }
300
- accept(s, response, jobRequest(job, field(input, "dryRun") === true, "manual"));
234
+ accept(s, response, jobRequest(newId(), job, field(input, "dryRun") === true, "manual"));
301
235
  return;
302
236
  }
303
237
  send(response, 404, "not found\n");
304
238
  };
305
239
  export const serveDeployer = (env) => {
306
240
  const s = settings(env);
307
- createServer(handle(async (request, response) => {
308
- const name = /^\/push\/([^/]+)$/.exec(new URL(request.url ?? "/", "http://deployer").pathname)?.[1];
309
- const app = name === undefined || !appName(name).ok ? undefined : registered(s.platformDir, name);
310
- if (request.method !== "POST" || app === undefined) {
311
- send(response, 404, "not found\n");
312
- return;
313
- }
314
- if (!app.ok) {
315
- send(response, 404, `${app.error}\n`);
316
- return;
317
- }
318
- accept(s, response, deployRequest(app.value, app.value.ref, "push"));
319
- return Promise.resolve();
320
- })).listen(8090);
241
+ abandon(s);
242
+ createServer(handle((request, response) => { push(s, request, response); })).listen(8090);
321
243
  createServer(handle((request, response) => control(s, request, response))).listen(8091, "127.0.0.1");
322
244
  schedule(s);
245
+ setTimeout(() => { void restore(s); }, restoreAfter);
323
246
  console.log(`deployer: :8090 pushes, 127.0.0.1:8091 control; renovate ${s.renovate === undefined ? "off" : "on"}`);
324
247
  };
@@ -1,18 +1,6 @@
1
1
  import { forgejo } from "./forgejo.js";
2
2
  import { github } from "./github.js";
3
3
  import { gitlab } from "./gitlab.js";
4
- import { typed } from "../../lib/typed.js";
5
4
  const adapters = { github, forgejo, gitlab };
6
- const kinds = { "https://github.com": "github", "https://gitlab.com": "gitlab" };
7
- /** GitHub for https://github.com, GitLab for https://gitlab.com, a Forgejo for any other host; undefined for a URL none understands. */
8
- export const forgeRepo = (repo) => {
9
- const match = /^(https?:\/\/[^/]+)\/(.+)\/([^/]+?)(\.git)?\/?$/.exec(repo);
10
- if (match === null)
11
- return undefined;
12
- const [, origin, owner, name] = match;
13
- if (origin === undefined || owner === undefined || name === undefined)
14
- return undefined;
15
- return typed({ kind: kinds[origin] ?? "forgejo", origin, owner, name });
16
- };
17
5
  export const forge = (repo, token, fetch) => adapters[repo.kind].api(repo, token, fetch);
18
6
  export const signed = (repo, secret, body, headers) => adapters[repo.kind].signed(secret, body, headers);
@@ -28,8 +28,8 @@ export const parseNotify = (raw) => {
28
28
  }
29
29
  };
30
30
  /**
31
- * What a finished run means for alerts. A deploy of the app's own ref that
32
- * failed (a push or the poll) raises; a deploy someone started by hand
31
+ * What a finished run means for alerts. A deploy nobody watches that failed
32
+ * (a push, the poll, a restore) raises; a deploy someone started by hand
33
33
  * (kayra app deploy | rollback) only resolves, since whoever started it is
34
34
  * watching. A platform job raises when it fails. Any success resolves.
35
35
  */
@@ -40,7 +40,7 @@ export const decide = (end) => {
40
40
  if (end.ok)
41
41
  return typed({ kind: "resolve", key, note: `Recovered: ${end.page}` });
42
42
  if (request.work.kind === "deploy") {
43
- if (request.work.why !== "push" && request.work.why !== "poll")
43
+ if (request.work.why === "deploy" || request.work.why === "rollback")
44
44
  return typed({ kind: "none" });
45
45
  return typed({
46
46
  kind: "raise",
@@ -1,6 +1,5 @@
1
1
  /** The request body, refused past `limit` bytes. */
2
2
  export const body = async (request, limit = 1 << 20) => {
3
- // eslint-disable-next-line functional/prefer-immutable-types -- Node's Buffer: bytes, never changed here
4
3
  const chunks = await Array.fromAsync(request, (chunk) => chunk);
5
4
  const all = Buffer.concat(chunks);
6
5
  return all.length > limit ? undefined : all;
@@ -1,5 +1,5 @@
1
1
  import { log, meta, owners, runsOf } from "../runs/store.js";
2
- /** The read-only runs pages at deploy.<domain>, behind Authelia: what Jenkins' job pages were for. */
2
+ /** The read-only runs pages at deploy.<domain>, behind Authelia. */
3
3
  const escape = (text) => text.replaceAll("&", "&amp;").replaceAll("<", "&lt;").replaceAll(">", "&gt;").replaceAll('"', "&quot;");
4
4
  const shell = (title, content) => `<!doctype html>
5
5
  <html lang="en"><head><meta charset="utf-8"><meta name="viewport" content="width=device-width, initial-scale=1">
@@ -11,7 +11,7 @@ body { font: 15px/1.5 system-ui, sans-serif; color: var(--fg); background: var(-
11
11
  h1 { font-size: 1.3rem; } h2 { font-size: 1.05rem; margin-top: 2rem; }
12
12
  a { color: inherit; } table { border-collapse: collapse; width: 100%; }
13
13
  td { padding: .3rem .6rem .3rem 0; border-bottom: 1px solid var(--line); vertical-align: top; }
14
- .muted { color: var(--muted); } .success { color: var(--ok); } .failure { color: var(--bad); } .running { color: var(--run); }
14
+ .muted { color: var(--muted); } .success { color: var(--ok); } .failure { color: var(--bad); } .running { color: var(--run); } .queued, .superseded { color: var(--muted); }
15
15
  code, pre { font: 13px/1.45 ui-monospace, monospace; } pre { white-space: pre-wrap; overflow-wrap: anywhere; }
16
16
  </style></head><body>${content}</body></html>
17
17
  `;
@@ -28,5 +28,7 @@ export const runPage = (runs, who, id) => {
28
28
  return shell("Not found", `<p><a href="/">Runs</a> · no run ${escape(id)}</p>`);
29
29
  return shell(`${who} ${id}`, `<p><a href="/">Runs</a> · ${escape(who)}</p><h1 class="${found.state}">${escape(found.what)} — ${found.state}</h1>` +
30
30
  `<p class="muted">${found.why}, started ${escape(found.started)}${found.finished === undefined ? "" : `, finished ${escape(found.finished)}`}</p>` +
31
+ (found.error === undefined ? "" : `<p class="failure">${escape(found.error)}</p>`) +
32
+ (found.urls === undefined ? "" : `<p>${found.urls.map((url) => `<a href="${escape(url)}">${escape(url)}</a>`).join(" · ")}</p>`) +
31
33
  `<pre>${escape(log(runs, who, id) ?? "")}</pre>`);
32
34
  };
@@ -16,7 +16,6 @@ export const hmacMatches = (secret, body, given) => {
16
16
  export const secretMatches = (secret, given) => {
17
17
  if (secret === "" || given === undefined)
18
18
  return false;
19
- // eslint-disable-next-line functional/prefer-immutable-types -- Node's Buffer: bytes, never changed here
20
19
  const digest = (text) => createHash("sha256").update(text).digest();
21
20
  return timingSafeEqual(digest(secret), digest(given));
22
21
  };
@@ -0,0 +1,70 @@
1
+ import { existsSync, mkdirSync, readFileSync, writeFileSync } from "node:fs";
2
+ import { dirname, join } from "node:path";
3
+ import { all, err, ok } from "../../lib/result.js";
4
+ import { builderPrepare, builderResolve } from "../build/client.js";
5
+ import { appFiles, imagesFile, routesFile } from "../build/compose.js";
6
+ import { forge } from "../forges/forge.js";
7
+ import { forgeRepo } from "../../lib/forge-repo.js";
8
+ import { spawnLines } from "../process.js";
9
+ import { mounts, token } from "../settings.js";
10
+ import { releaseName } from "./records.js";
11
+ import { typed } from "../../lib/typed.js";
12
+ const appSecrets = (app) => {
13
+ const read = all(app.secrets.map((key) => {
14
+ const path = join(mounts.apps, app.name, key);
15
+ return existsSync(path) ? ok([key, readFileSync(path, "utf8")]) : err(`${app.name}: secret ${key} is missing; run kayra apps`);
16
+ }));
17
+ return read.ok ? ok(Object.fromEntries(read.value)) : read;
18
+ };
19
+ /**
20
+ * A GET through Traefik on platform-internal, so it needs neither the public
21
+ * DNS nor a way back in from outside. Its certificate is verify's to judge, not
22
+ * readiness's: on multipass it is mkcert's, which this container does not trust.
23
+ */
24
+ const probe = async (url) => {
25
+ const host = new URL(url).hostname;
26
+ const exit = await spawnLines(["curl", "-sk", "-o", "/dev/null", "-m", "10", "--connect-to", `${host}:443:traefik:443`, "-w", "%{http_code}", url], { cwd: "/", env: {}, capture: true, timeoutMs: 15_000 }, () => undefined);
27
+ const status = Number.parseInt(exit.stdout, 10);
28
+ return Number.isNaN(status) ? 0 : status;
29
+ };
30
+ /** A run's page, which its commit statuses link to. */
31
+ export const runPage = (s, who, id) => `https://deploy.${s.domain}/runs/${who}/${id}`;
32
+ /** What one run does to this server; its files go into `scratch`, which the run removes when it ends. */
33
+ export const effects = (s, who, id, scratch, log) => typed({
34
+ log,
35
+ resolve: (app, ref) => builderResolve(s.builder, app, ref),
36
+ prepare: (app, commit) => builderPrepare(s.builder, app, commit, log),
37
+ exec: (argv, options) => spawnLines(argv, { ...options, timeoutMs: 20 * 60_000 }, log),
38
+ secrets: appSecrets,
39
+ workdir: (bundle, images) => {
40
+ writeFileSync(join(scratch, imagesFile), images);
41
+ bundle.compose.forEach((file) => {
42
+ const path = join(scratch, appFiles, file.path);
43
+ mkdirSync(dirname(path), { recursive: true });
44
+ writeFileSync(path, file.content);
45
+ });
46
+ return Promise.resolve(scratch);
47
+ },
48
+ writeRoutes: (cwd, content) => {
49
+ writeFileSync(join(cwd, routesFile), content);
50
+ return Promise.resolve();
51
+ },
52
+ record: (app, commit, images) => {
53
+ const dir = join(s.platformDir, "releases", app.name);
54
+ mkdirSync(dir, { recursive: true });
55
+ writeFileSync(join(dir, releaseName(new Date(), commit)), images);
56
+ return Promise.resolve();
57
+ },
58
+ status: async (app, commit, context, state, description) => {
59
+ const repo = forgeRepo(app.repo);
60
+ if (repo === undefined) {
61
+ log(`(no commit status: ${app.repo} is on no forge kayra knows)`);
62
+ return;
63
+ }
64
+ const posted = await forge(repo, token("GIT_TOKEN"), fetch).postStatus(commit, { context, state, description, target: runPage(s, who, id) });
65
+ if (!posted.ok)
66
+ log(`(commit status ${context} not posted: ${posted.error})`);
67
+ },
68
+ probe,
69
+ pause: (ms) => new Promise((resolve) => setTimeout(resolve, ms)),
70
+ });
@@ -1,6 +1,6 @@
1
1
  import { jobOwner } from "../brand.js";
2
2
  import { typed } from "../../lib/typed.js";
3
- export const jobRequest = (job, dryRun, why) => typed({ owner: jobOwner(job), work: { kind: "job", job, dryRun, why } });
3
+ export const jobRequest = (id, job, dryRun, why) => typed({ id, owner: jobOwner(job), work: { kind: "job", job, dryRun, why } });
4
4
  /** A run's meta.json `what` and `why`. */
5
5
  export const describe = (request) => Object.freeze({
6
6
  what: request.work.kind === "deploy" ? `deploy ${request.work.ref}` : `${request.work.job}${request.work.dryRun ? " (dry run)" : ""}`,
@@ -26,11 +26,12 @@ const fill = (queue) => {
26
26
  start: pick.start,
27
27
  });
28
28
  };
29
+ /** Also says which waiting request the new one replaced: that run is superseded, never started. */
29
30
  export const enqueue = (queue, request) => {
30
- const waits = queue.waiting.some((waiting) => waiting.owner === request.owner);
31
- const waiting = waits
31
+ const replaced = queue.waiting.filter((waiting) => waiting.owner === request.owner);
32
+ const waiting = replaced.length > 0
32
33
  ? queue.waiting.map((waiting) => (waiting.owner === request.owner ? request : waiting))
33
34
  : [...queue.waiting, request];
34
- return fill({ ...queue, waiting });
35
+ return typed({ ...fill({ ...queue, waiting }), replaced });
35
36
  };
36
37
  export const finish = (queue, owner) => fill({ ...queue, running: queue.running.filter((running) => running.owner !== owner) });
@@ -10,15 +10,28 @@ export const releases = (names) => Object.freeze(names
10
10
  .flatMap((name) => {
11
11
  const match = /^(\d{8}T\d{6}Z)-([0-9a-f]{40})\.env$/.exec(name);
12
12
  const commit = sha(match?.[2] ?? "");
13
- return match?.[1] === undefined || !commit.ok ? Object.freeze([]) : Object.freeze([{ sha: commit.value, time: match[1] }]);
13
+ return match?.[1] === undefined || !commit.ok ? Object.freeze([]) : Object.freeze([typed({ sha: commit.value, time: match[1] })]);
14
14
  })
15
15
  .filter((release, i, all) => all.findIndex((other) => other.sha === release.sha) === i));
16
16
  /** The poll's question: has the ref moved to a commit neither the newest run tried nor the newest release runs? */
17
17
  export const moved = (resolved, lastRun, lastRelease) => resolved !== lastRun && resolved !== lastRelease;
18
- const whys = ["push", "poll", "deploy", "rollback", "schedule", "manual"];
19
- const states = ["running", "success", "failure"];
18
+ const whys = ["push", "poll", "deploy", "rollback", "restore", "schedule", "manual"];
19
+ const states = ["queued", "running", "success", "failure", "superseded"];
20
20
  const isWhy = (value) => whys.some((why) => why === value);
21
21
  const isState = (value) => states.some((state) => state === value);
22
+ /** A run that will change no more. */
23
+ export const ended = (state) => state === "success" || state === "failure" || state === "superseded";
24
+ const parseCheck = (json) => {
25
+ if (typeof json !== "object" || json === null)
26
+ return Object.freeze([]);
27
+ const url = field(json, "url");
28
+ const status = field(json, "status");
29
+ const passed = field(json, "ok");
30
+ return typeof url === "string" && typeof status === "number" && typeof passed === "boolean"
31
+ ? Object.freeze([typed({ url, status, ok: passed })])
32
+ : Object.freeze([]);
33
+ };
34
+ const texts = (json) => Array.isArray(json) && json.every((each) => typeof each === "string") ? Object.freeze(json.map(String)) : undefined;
22
35
  export const parseMeta = (json) => {
23
36
  if (typeof json !== "object" || json === null)
24
37
  return undefined;
@@ -30,6 +43,9 @@ export const parseMeta = (json) => {
30
43
  const state = field(json, "state");
31
44
  const started = field(json, "started");
32
45
  const finished = field(json, "finished");
46
+ const error = field(json, "error");
47
+ const urls = texts(field(json, "urls"));
48
+ const checks = field(json, "checks");
33
49
  if (!id.ok || typeof owner !== "string" || typeof what !== "string" || typeof started !== "string")
34
50
  return undefined;
35
51
  if (!isWhy(why) || !isState(state))
@@ -43,6 +59,9 @@ export const parseMeta = (json) => {
43
59
  started,
44
60
  ...(commit.ok ? { sha: commit.value } : {}),
45
61
  ...(typeof finished === "string" ? { finished } : {}),
62
+ ...(typeof error === "string" ? { error } : {}),
63
+ ...(urls === undefined ? {} : { urls }),
64
+ ...(Array.isArray(checks) ? { checks: Object.freeze(checks.flatMap(parseCheck)) } : {}),
46
65
  });
47
66
  };
48
67
  /** Run directories past the newest `keep`, oldest first: what retention deletes. */