@yunuseon/kayra 0.19.0 → 0.20.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/agent/skills/kayra-release/SKILL.md +1 -4
- package/agent/skills/kayra-rollout/SKILL.md +1 -4
- package/ansible/roles/app/tasks/main.yml +24 -0
- package/ansible/roles/platform/defaults/main.yml +3 -0
- package/ansible/roles/platform/tasks/main.yml +6 -86
- package/ansible/roles/platform/templates/platform.env.j2 +2 -0
- package/bin/kayra-app-hook.js +6 -0
- package/bin/kayra-app-init.js +6 -0
- package/bin/kayra-app-launch.js +6 -0
- package/bin/kayra-app-values.js +6 -0
- package/cli/dist/app-hook.js +75 -0
- package/cli/dist/app-init.js +65 -0
- package/cli/dist/app-launch.js +89 -0
- package/cli/dist/app-values.js +37 -0
- package/cli/dist/browser/assets/ibm-plex-sans-cyrillic-400-normal-BTotfTJu.woff +0 -0
- package/cli/dist/browser/assets/ibm-plex-sans-cyrillic-400-normal-DZqxrq2p.woff2 +0 -0
- package/cli/dist/browser/assets/ibm-plex-sans-cyrillic-600-normal-71GNu3SW.woff2 +0 -0
- package/cli/dist/browser/assets/ibm-plex-sans-cyrillic-600-normal-BGq0mW3O.woff +0 -0
- package/cli/dist/browser/assets/ibm-plex-sans-cyrillic-ext-400-normal-Dsrv2Tcn.woff +0 -0
- package/cli/dist/browser/assets/ibm-plex-sans-cyrillic-ext-400-normal-g30qAdWV.woff2 +0 -0
- package/cli/dist/browser/assets/ibm-plex-sans-cyrillic-ext-600-normal-Bz0x94Yp.woff +0 -0
- package/cli/dist/browser/assets/ibm-plex-sans-cyrillic-ext-600-normal-DUMzJB7m.woff2 +0 -0
- package/cli/dist/browser/assets/ibm-plex-sans-greek-400-normal-D9ESIMu3.woff +0 -0
- package/cli/dist/browser/assets/ibm-plex-sans-greek-400-normal-_efipK4i.woff2 +0 -0
- package/cli/dist/browser/assets/ibm-plex-sans-greek-600-normal-D-CqTdkO.woff +0 -0
- package/cli/dist/browser/assets/ibm-plex-sans-greek-600-normal-DzTrcv_p.woff2 +0 -0
- package/cli/dist/browser/assets/ibm-plex-sans-latin-400-normal-CDDApCn2.woff2 +0 -0
- package/cli/dist/browser/assets/ibm-plex-sans-latin-400-normal-CYLoc0-x.woff +0 -0
- package/cli/dist/browser/assets/ibm-plex-sans-latin-600-normal-Cu4Hd6ag.woff +0 -0
- package/cli/dist/browser/assets/ibm-plex-sans-latin-600-normal-CuJfVYMP.woff2 +0 -0
- package/cli/dist/browser/assets/ibm-plex-sans-latin-ext-400-normal-C5H60-Va.woff2 +0 -0
- package/cli/dist/browser/assets/ibm-plex-sans-latin-ext-400-normal-RBey6euL.woff +0 -0
- package/cli/dist/browser/assets/ibm-plex-sans-latin-ext-600-normal-DIrixKbi.woff +0 -0
- package/cli/dist/browser/assets/ibm-plex-sans-latin-ext-600-normal-DOrvGEcy.woff2 +0 -0
- package/cli/dist/browser/assets/ibm-plex-sans-vietnamese-400-normal-DG4YqDda.woff2 +0 -0
- package/cli/dist/browser/assets/ibm-plex-sans-vietnamese-400-normal-fK1oJ5dG.woff +0 -0
- package/cli/dist/browser/assets/ibm-plex-sans-vietnamese-600-normal-DgdngZtN.woff +0 -0
- package/cli/dist/browser/assets/ibm-plex-sans-vietnamese-600-normal-DpPYBSTl.woff2 +0 -0
- package/cli/dist/browser/assets/index-DzIVPXyL.css +1 -0
- package/cli/dist/browser/assets/index-SP8bCDrl.js +7 -0
- package/cli/dist/browser/assets/jetbrains-mono-cyrillic-400-normal-BEIGL1Tu.woff2 +0 -0
- package/cli/dist/browser/assets/jetbrains-mono-cyrillic-400-normal-ugxPyKxw.woff +0 -0
- package/cli/dist/browser/assets/jetbrains-mono-cyrillic-500-normal-DJqRU3vO.woff +0 -0
- package/cli/dist/browser/assets/jetbrains-mono-cyrillic-500-normal-DmUKJPL_.woff2 +0 -0
- package/cli/dist/browser/assets/jetbrains-mono-cyrillic-600-normal-8K4wrrwR.woff +0 -0
- package/cli/dist/browser/assets/jetbrains-mono-cyrillic-600-normal-EVf6-Yzo.woff2 +0 -0
- package/cli/dist/browser/assets/jetbrains-mono-cyrillic-700-normal-BWTpRfYl.woff2 +0 -0
- package/cli/dist/browser/assets/jetbrains-mono-cyrillic-700-normal-CEoEElIJ.woff +0 -0
- package/cli/dist/browser/assets/jetbrains-mono-greek-400-normal-B9oWc5Lo.woff +0 -0
- package/cli/dist/browser/assets/jetbrains-mono-greek-400-normal-C190GLew.woff2 +0 -0
- package/cli/dist/browser/assets/jetbrains-mono-greek-500-normal-D7SFKleX.woff +0 -0
- package/cli/dist/browser/assets/jetbrains-mono-greek-500-normal-JpySY46c.woff2 +0 -0
- package/cli/dist/browser/assets/jetbrains-mono-greek-600-normal-H7WoG9Et.woff2 +0 -0
- package/cli/dist/browser/assets/jetbrains-mono-greek-600-normal-mc2nkWzM.woff +0 -0
- package/cli/dist/browser/assets/jetbrains-mono-greek-700-normal-C6CZE3T8.woff2 +0 -0
- package/cli/dist/browser/assets/jetbrains-mono-greek-700-normal-DEigVDxa.woff +0 -0
- package/cli/dist/browser/assets/jetbrains-mono-latin-400-normal-6-qcROiO.woff +0 -0
- package/cli/dist/browser/assets/jetbrains-mono-latin-400-normal-V6pRDFza.woff2 +0 -0
- package/cli/dist/browser/assets/jetbrains-mono-latin-500-normal-BWZEU5yA.woff2 +0 -0
- package/cli/dist/browser/assets/jetbrains-mono-latin-500-normal-CJOVTJB7.woff +0 -0
- package/cli/dist/browser/assets/jetbrains-mono-latin-600-normal-BfsvjouI.woff +0 -0
- package/cli/dist/browser/assets/jetbrains-mono-latin-600-normal-C8RAYTDA.woff2 +0 -0
- package/cli/dist/browser/assets/jetbrains-mono-latin-700-normal-BYuf6tUa.woff2 +0 -0
- package/cli/dist/browser/assets/jetbrains-mono-latin-700-normal-D3wTyLJW.woff +0 -0
- package/cli/dist/browser/assets/jetbrains-mono-latin-ext-400-normal-Bc8Ftmh3.woff2 +0 -0
- package/cli/dist/browser/assets/jetbrains-mono-latin-ext-400-normal-fXTG6kC5.woff +0 -0
- package/cli/dist/browser/assets/jetbrains-mono-latin-ext-500-normal-Cut-4mMH.woff2 +0 -0
- package/cli/dist/browser/assets/jetbrains-mono-latin-ext-500-normal-ckzbgY84.woff +0 -0
- package/cli/dist/browser/assets/jetbrains-mono-latin-ext-600-normal-BfB_LPfz.woff2 +0 -0
- package/cli/dist/browser/assets/jetbrains-mono-latin-ext-600-normal-DObL3zCW.woff +0 -0
- package/cli/dist/browser/assets/jetbrains-mono-latin-ext-700-normal-CZipNAKV.woff2 +0 -0
- package/cli/dist/browser/assets/jetbrains-mono-latin-ext-700-normal-CxPITLHs.woff +0 -0
- package/cli/dist/browser/assets/jetbrains-mono-vietnamese-400-normal-CqNFfHCs.woff +0 -0
- package/cli/dist/browser/assets/jetbrains-mono-vietnamese-500-normal-DNRqzVM1.woff +0 -0
- package/cli/dist/browser/assets/jetbrains-mono-vietnamese-600-normal-OWROknRo.woff +0 -0
- package/cli/dist/browser/assets/jetbrains-mono-vietnamese-700-normal-BDLVIk2r.woff +0 -0
- package/cli/dist/browser/index.html +10 -3
- package/cli/dist/clients/api/start.js +1 -1
- package/cli/dist/clients/cli/invocation.js +25 -0
- package/cli/dist/clients/cli/parser.js +24 -7
- package/cli/dist/clients/cli/text.js +3 -3
- package/cli/dist/clients/cli/values.js +0 -1
- package/cli/dist/clients/cli/views.js +9 -2
- package/cli/dist/clients/mcp/tools.js +8 -1
- package/cli/dist/clients/ui/assets.js +1 -0
- package/cli/dist/clients/wire/contract.js +2 -2
- package/cli/dist/clients/wire/queries.js +1 -0
- package/cli/dist/clients/wire/schemas.js +4 -1
- package/cli/dist/clients/wire/words.js +14 -0
- package/cli/dist/core/adapters/opentofu.js +1 -1
- package/cli/dist/core/app-hook/hooks.js +90 -0
- package/cli/dist/core/app-hook/token.js +49 -0
- package/cli/dist/core/app-init/plan.js +208 -0
- package/cli/dist/core/app-values/model.js +45 -0
- package/cli/dist/core/app-values/store.js +124 -0
- package/cli/dist/core/ctl/answers.js +14 -9
- package/cli/dist/core/fleet/brand.js +10 -12
- package/cli/dist/core/fleet/settings.js +14 -9
- package/cli/dist/core/operations/catalog.js +46 -1
- package/cli/dist/core/operations/dispatch.js +37 -6
- package/cli/dist/core/recipes/app-check.js +14 -1
- package/cli/dist/core/recipes/app-hook.js +26 -0
- package/cli/dist/core/recipes/app-values.js +21 -0
- package/cli/dist/core/recipes/doctor.js +2 -2
- package/cli/dist/core/recipes/launch.js +51 -0
- package/cli/dist/core/validation/facts.js +1 -3
- package/cli/dist/deployer/brand.js +8 -5
- package/cli/dist/deployer/build/builder.js +10 -4
- package/cli/dist/deployer/build/client.js +44 -0
- package/cli/dist/deployer/build/routes.js +27 -6
- package/cli/dist/deployer/ctl/answers.js +6 -2
- package/cli/dist/deployer/ctl/ctl.js +66 -9
- package/cli/dist/deployer/deployer.js +113 -196
- package/cli/dist/deployer/forges/forge.js +0 -12
- package/cli/dist/deployer/forges/notify.js +3 -3
- package/cli/dist/deployer/http/http.js +0 -1
- package/cli/dist/deployer/http/page.js +4 -2
- package/cli/dist/deployer/http/webhook.js +0 -1
- package/cli/dist/deployer/runs/effects.js +70 -0
- package/cli/dist/deployer/runs/queue.js +5 -4
- package/cli/dist/deployer/runs/records.js +22 -3
- package/cli/dist/deployer/runs/restore.js +6 -0
- package/cli/dist/deployer/runs/run.js +51 -5
- package/cli/dist/deployer/runs/store.js +19 -3
- package/cli/dist/deployer/settings.js +69 -0
- package/cli/dist/lib/forge-repo.js +15 -0
- package/cli/dist/lib/names.js +13 -0
- package/cli/dist/main.js +46 -25
- package/compose/docker-compose.ci.yml +2 -0
- package/package.json +2 -2
- package/schema/app.json +5 -0
- package/schema/defaults.json +8 -0
- package/schema/vars-platform.json +8 -0
- package/scripts/local/verify.sh +9 -2
- package/terraform/modules/platform-dns/main.tf +66 -0
- package/terraform/modules/platform-dns/outputs.tf +9 -0
- package/terraform/modules/platform-dns/tests/fleet/.cache/apps/blog/site/app.yml +3 -0
- package/terraform/modules/platform-dns/tests/fleet/.cache/apps/shop/app.yml +6 -0
- package/terraform/modules/platform-dns/tests/fleet/servers/nested/server.yml +6 -0
- package/terraform/modules/platform-dns/tests/fleet/servers/shop/server.yml +7 -0
- package/terraform/modules/platform-dns/tests/fleet/servers/uncloned/server.yml +6 -0
- package/terraform/modules/platform-dns/tests/platform-dns.tftest.hcl +64 -0
- package/terraform/modules/platform-dns/variables.tf +25 -0
- package/cli/dist/browser/assets/index-C4HCL022.js +0 -7
- package/cli/dist/browser/assets/index-Ct0Eudk4.css +0 -1
- package/scripts/agent/guard.sh +0 -125
|
@@ -1,80 +1,33 @@
|
|
|
1
1
|
import { randomBytes } from "node:crypto";
|
|
2
|
-
import {
|
|
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 {
|
|
6
|
-
import {
|
|
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
|
|
11
|
-
import {
|
|
12
|
-
import {
|
|
13
|
-
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";
|
|
14
11
|
import { json, send } from "./http/http.js";
|
|
15
|
-
import { spawnLines } from "./process.js";
|
|
16
12
|
import { describe, empty, enqueue, finish, jobRequest } from "./runs/queue.js";
|
|
17
|
-
import {
|
|
18
|
-
import {
|
|
19
|
-
import {
|
|
13
|
+
import { ended, moved, releases } from "./runs/records.js";
|
|
14
|
+
import { decide } from "./forges/notify.js";
|
|
15
|
+
import { effects, runPage } from "./runs/effects.js";
|
|
20
16
|
import { deploy, renovate } from "./runs/run.js";
|
|
21
|
-
import {
|
|
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";
|
|
22
21
|
import { typed } from "../lib/typed.js";
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
const settings = (env) => {
|
|
33
|
-
const scripts = env["PLATFORM_SCRIPTS"] ?? "/opt/kayra/scripts/platform";
|
|
34
|
-
const strings = Object.fromEntries(Object.entries(env).flatMap(([key, value]) => typed(value === undefined ? [] : [[key, value]])));
|
|
35
|
-
return typed({
|
|
36
|
-
platformDir: env["PLATFORM_DIR"] ?? "/opt/platform",
|
|
37
|
-
appsDir: env["APPS_DIR"] ?? "/opt/apps",
|
|
38
|
-
builder: env["BUILDER_URL"] ?? "http://builder:8081",
|
|
39
|
-
domain: env["DOMAIN"] ?? "localhost",
|
|
40
|
-
policyScript: join(scripts, "compose-policy.sh"),
|
|
41
|
-
gcScript: join(scripts, "registry-gc.sh"),
|
|
42
|
-
renovateImage: env["RENOVATE_IMAGE"] ?? "",
|
|
43
|
-
renovate: env["PLATFORM_RENOVATE"] === "true" ? must(parseCron(env["RENOVATE_SCHEDULE"] ?? "0 4 * * 1")) : undefined,
|
|
44
|
-
gc: must(parseCron("0 3 * * *")),
|
|
45
|
-
poll: must(parseCron("17 * * * *")),
|
|
46
|
-
env: strings,
|
|
47
|
-
server: env["SERVER"] ?? env["DOMAIN"] ?? "this server",
|
|
48
|
-
notifier: notifierFor(must(parseNotify(env["NOTIFIER"])), env["PLATFORM_DIR"] ?? "/opt/platform"),
|
|
49
|
-
});
|
|
50
|
-
};
|
|
51
|
-
/** The configured notifier; its open alerts are kept in <platformDir>/alerts/<key>. */
|
|
52
|
-
const notifierFor = (config, platformDir) => {
|
|
53
|
-
if (config.kind === "none")
|
|
54
|
-
return silent;
|
|
55
|
-
const alerts = join(platformDir, "alerts");
|
|
56
|
-
const keyFile = "/run/deployer-secrets/NOTIFIER_GITHUB_APP_KEY";
|
|
57
|
-
return githubIssues(config.repository, config.appId, {
|
|
58
|
-
fetch: async (url, init) => {
|
|
59
|
-
const response = await fetch(url, init);
|
|
60
|
-
return { status: response.status, json: await response.json().catch(() => undefined) };
|
|
61
|
-
},
|
|
62
|
-
privateKey: () => (existsSync(keyFile) ? ok(readFileSync(keyFile, "utf8")) : err(`${keyFile} is missing: notifier_github_app_key in secrets.yml`)),
|
|
63
|
-
now: () => Date.now(),
|
|
64
|
-
openIssue: (key) => {
|
|
65
|
-
const path = join(alerts, key);
|
|
66
|
-
const number = existsSync(path) ? Number(readFileSync(path, "utf8")) : Number.NaN;
|
|
67
|
-
return Number.isInteger(number) ? number : undefined;
|
|
68
|
-
},
|
|
69
|
-
remember: (key, issue) => {
|
|
70
|
-
mkdirSync(alerts, { recursive: true });
|
|
71
|
-
if (issue === undefined)
|
|
72
|
-
rmSync(join(alerts, key), { force: true });
|
|
73
|
-
else
|
|
74
|
-
writeFileSync(join(alerts, key), String(issue));
|
|
75
|
-
},
|
|
76
|
-
});
|
|
77
|
-
};
|
|
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
|
+
*/
|
|
78
31
|
/** Raises or resolves what the finished run means; a notifier's failure is logged, never the run's. */
|
|
79
32
|
const notify = async (s, end, log) => {
|
|
80
33
|
const decision = decide(end);
|
|
@@ -86,7 +39,6 @@ const notify = async (s, end, log) => {
|
|
|
86
39
|
if (!done.ok)
|
|
87
40
|
log(`(alert not sent: ${done.error})`);
|
|
88
41
|
};
|
|
89
|
-
const runsDir = (s) => join(s.platformDir, "runs");
|
|
90
42
|
const keepRuns = 50;
|
|
91
43
|
const registeredApps = (s) => {
|
|
92
44
|
const dir = join(s.platformDir, "apps");
|
|
@@ -96,88 +48,6 @@ const registeredApps = (s) => {
|
|
|
96
48
|
return app?.ok === true ? Object.freeze([app.value]) : Object.freeze([]);
|
|
97
49
|
}));
|
|
98
50
|
};
|
|
99
|
-
const builderResolve = async (s, app, ref) => {
|
|
100
|
-
const response = await fetch(`${s.builder}/resolve`, { method: "POST", body: JSON.stringify({ app: app.name, ref }) }).catch(() => undefined);
|
|
101
|
-
if (response === undefined)
|
|
102
|
-
return err("the builder did not answer");
|
|
103
|
-
const answer = await response.json().catch(() => undefined);
|
|
104
|
-
const given = typeof answer === "object" && answer !== null ? field(answer, "sha") : undefined;
|
|
105
|
-
const problem = typeof answer === "object" && answer !== null ? field(answer, "error") : undefined;
|
|
106
|
-
return typeof given === "string" ? sha(given) : err(typeof problem === "string" ? problem : `resolve: HTTP ${String(response.status)}`);
|
|
107
|
-
};
|
|
108
|
-
/** The builder's NDJSON: log lines as they come, then the bundle or an error. */
|
|
109
|
-
const builderPrepare = async (s, app, commit, log) => {
|
|
110
|
-
const response = await fetch(`${s.builder}/prepare`, {
|
|
111
|
-
method: "POST",
|
|
112
|
-
body: JSON.stringify({ app: app.name, sha: commit }),
|
|
113
|
-
signal: AbortSignal.timeout(45 * 60_000),
|
|
114
|
-
}).catch(() => undefined);
|
|
115
|
-
if (response?.body == null || !response.ok)
|
|
116
|
-
return err("the builder did not prepare a bundle");
|
|
117
|
-
const lines = createInterface({ input: Readable.fromWeb(response.body) });
|
|
118
|
-
const outcome = await Array.fromAsync(lines, (line) => {
|
|
119
|
-
try {
|
|
120
|
-
const message = JSON.parse(line);
|
|
121
|
-
if (typeof message !== "object" || message === null)
|
|
122
|
-
return undefined;
|
|
123
|
-
const text = field(message, "log");
|
|
124
|
-
if (typeof text === "string")
|
|
125
|
-
log(text);
|
|
126
|
-
const bundle = field(message, "bundle");
|
|
127
|
-
const problem = field(message, "error");
|
|
128
|
-
return bundle !== undefined ? ok(bundle) : typeof problem === "string" ? err(problem) : undefined;
|
|
129
|
-
}
|
|
130
|
-
catch {
|
|
131
|
-
return undefined;
|
|
132
|
-
}
|
|
133
|
-
});
|
|
134
|
-
return outcome.findLast((item) => item !== undefined) ?? err("the builder stopped without a bundle");
|
|
135
|
-
};
|
|
136
|
-
const appSecrets = (app) => {
|
|
137
|
-
const read = all(app.secrets.map((key) => {
|
|
138
|
-
const path = join("/run/app-secrets", app.name, key);
|
|
139
|
-
return existsSync(path) ? ok([key, readFileSync(path, "utf8")]) : err(`${app.name}: secret ${key} is missing; run kayra apps`);
|
|
140
|
-
}));
|
|
141
|
-
return read.ok ? ok(Object.fromEntries(read.value)) : read;
|
|
142
|
-
};
|
|
143
|
-
const target = (s, who, id) => `https://deploy.${s.domain}/runs/${who}/${id}`;
|
|
144
|
-
const effects = (s, who, id, log) => typed({
|
|
145
|
-
log,
|
|
146
|
-
resolve: (app, ref) => builderResolve(s, app, ref),
|
|
147
|
-
prepare: (app, commit) => builderPrepare(s, app, commit, log),
|
|
148
|
-
exec: (argv, options) => spawnLines(argv, { ...options, timeoutMs: 20 * 60_000 }, log),
|
|
149
|
-
secrets: appSecrets,
|
|
150
|
-
workdir: (bundle, images) => {
|
|
151
|
-
const dir = mkdtempSync(join(tmpdir(), `${id}-`));
|
|
152
|
-
writeFileSync(join(dir, imagesFile), images);
|
|
153
|
-
bundle.compose.forEach((file) => {
|
|
154
|
-
const path = join(dir, appFiles, file.path);
|
|
155
|
-
mkdirSync(dirname(path), { recursive: true });
|
|
156
|
-
writeFileSync(path, file.content);
|
|
157
|
-
});
|
|
158
|
-
return Promise.resolve(dir);
|
|
159
|
-
},
|
|
160
|
-
writeRoutes: (cwd, content) => {
|
|
161
|
-
writeFileSync(join(cwd, routesFile), content);
|
|
162
|
-
return Promise.resolve();
|
|
163
|
-
},
|
|
164
|
-
record: (app, commit, images) => {
|
|
165
|
-
const dir = join(s.platformDir, "releases", app.name);
|
|
166
|
-
mkdirSync(dir, { recursive: true });
|
|
167
|
-
writeFileSync(join(dir, releaseName(new Date(), commit)), images);
|
|
168
|
-
return Promise.resolve();
|
|
169
|
-
},
|
|
170
|
-
status: async (app, commit, context, state, description) => {
|
|
171
|
-
const repo = forgeRepo(app.repo);
|
|
172
|
-
if (repo === undefined) {
|
|
173
|
-
log(`(no commit status: ${app.repo} is on no forge kayra knows)`);
|
|
174
|
-
return;
|
|
175
|
-
}
|
|
176
|
-
const posted = await forge(repo, token("GIT_TOKEN"), fetch).postStatus(commit, { context, state, description, target: target(s, who, id) });
|
|
177
|
-
if (!posted.ok)
|
|
178
|
-
log(`(commit status ${context} not posted: ${posted.error})`);
|
|
179
|
-
},
|
|
180
|
-
});
|
|
181
51
|
const work = async (s, request, fx) => {
|
|
182
52
|
if (request.work.kind === "deploy") {
|
|
183
53
|
const app = registered(s.platformDir, request.work.app);
|
|
@@ -195,70 +65,114 @@ const work = async (s, request, fx) => {
|
|
|
195
65
|
/** The daemon's one mutable cell: the queue, swapped whole on every change. */
|
|
196
66
|
// eslint-disable-next-line functional/prefer-immutable-types -- the daemon's state lives here, nowhere else
|
|
197
67
|
const cell = { queue: empty(2) };
|
|
198
|
-
|
|
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. */
|
|
199
70
|
const swap = (s, change) => {
|
|
200
71
|
const next = change(cell.queue);
|
|
201
72
|
// eslint-disable-next-line functional/immutable-data -- the daemon's state lives here, nowhere else
|
|
202
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
|
+
});
|
|
203
78
|
next.start.forEach((request) => {
|
|
204
79
|
void start(s, request);
|
|
205
80
|
});
|
|
206
81
|
return next.start;
|
|
207
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
|
+
};
|
|
208
100
|
const start = async (s, request) => {
|
|
209
|
-
const id =
|
|
210
|
-
const
|
|
211
|
-
|
|
212
|
-
|
|
213
|
-
const begun = { id, owner: request.owner, what, why, state: "running", started: new Date().toISOString() };
|
|
214
|
-
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);
|
|
215
105
|
const log = (line) => {
|
|
216
|
-
|
|
106
|
+
appendLog(runs, request.owner, id, line);
|
|
217
107
|
};
|
|
218
|
-
const
|
|
108
|
+
const scratch = mkdtempSync(join(tmpdir(), `${id}-`));
|
|
109
|
+
const outcome = await work(s, request, effects(s, request.owner, id, scratch, log)).catch((error) => {
|
|
219
110
|
log(`deployer: ${String(error)}`);
|
|
220
|
-
return typed({ ok: false });
|
|
111
|
+
return typed({ ok: false, error: "the deployer failed; see the log" });
|
|
221
112
|
});
|
|
222
|
-
|
|
113
|
+
writeMeta(runs, request.owner, {
|
|
223
114
|
...begun,
|
|
224
115
|
...(outcome.sha === undefined ? {} : { sha: outcome.sha }),
|
|
225
116
|
state: outcome.ok ? "success" : "failure",
|
|
226
117
|
finished: new Date().toISOString(),
|
|
227
|
-
|
|
228
|
-
|
|
229
|
-
|
|
230
|
-
expired(readdirSync(join(runsDir(s), request.owner)), keepRuns).forEach((old) => {
|
|
231
|
-
rmSync(join(runsDir(s), request.owner, old), { recursive: true, force: true });
|
|
232
|
-
});
|
|
233
|
-
readdirSync(tmpdir())
|
|
234
|
-
.filter((name) => name.startsWith(`${id}-`))
|
|
235
|
-
.forEach((name) => {
|
|
236
|
-
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 }),
|
|
237
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 });
|
|
238
125
|
swap(s, (queue) => finish(queue, request.owner));
|
|
239
126
|
};
|
|
240
127
|
const deployRequest = (app, ref, why) => typed({
|
|
128
|
+
id: newId(),
|
|
241
129
|
owner: appOwner(app.name),
|
|
242
130
|
work: { kind: "deploy", app: app.name, ref, why },
|
|
243
131
|
});
|
|
244
|
-
/** The webhook's fallback: an app whose ref moved to a commit no run has tried yet and no release 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). */
|
|
245
133
|
const poll = async (s) => {
|
|
246
134
|
await Promise.all(registeredApps(s).map(async (app) => {
|
|
247
|
-
const commit = await builderResolve(s, app, app.ref);
|
|
135
|
+
const commit = await builderResolve(s.builder, app, app.ref);
|
|
248
136
|
const lastRun = runsOf(runsDir(s), appOwner(app.name)).find((run) => run.sha !== undefined);
|
|
249
137
|
const releasesDir = join(s.platformDir, "releases", app.name);
|
|
250
138
|
const lastRelease = releases(existsSync(releasesDir) ? readdirSync(releasesDir) : [])[0];
|
|
251
139
|
if (commit.ok && moved(commit.value, lastRun?.sha, lastRelease?.sha))
|
|
252
|
-
|
|
140
|
+
ask(s, deployRequest(app, app.ref, "poll"));
|
|
253
141
|
}));
|
|
254
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;
|
|
255
169
|
const tick = (s, at) => {
|
|
256
170
|
if (matches(s.poll, at))
|
|
257
171
|
void poll(s);
|
|
258
172
|
if (matches(s.gc, at))
|
|
259
|
-
|
|
173
|
+
ask(s, jobRequest(newId(), "gc", false, "schedule"));
|
|
260
174
|
if (s.renovate !== undefined && matches(s.renovate, at))
|
|
261
|
-
|
|
175
|
+
ask(s, jobRequest(newId(), "renovate", false, "schedule"));
|
|
262
176
|
};
|
|
263
177
|
const schedule = (s) => {
|
|
264
178
|
const wait = 60_000 - (Date.now() % 60_000) + 500;
|
|
@@ -268,15 +182,29 @@ const schedule = (s) => {
|
|
|
268
182
|
}, wait);
|
|
269
183
|
};
|
|
270
184
|
const handle = (handler) => (request, response) => {
|
|
271
|
-
handler(request, response).catch((error) => {
|
|
185
|
+
Promise.resolve(handler(request, response)).catch((error) => {
|
|
272
186
|
console.error(error);
|
|
273
187
|
if (!response.headersSent)
|
|
274
188
|
send(response, 500, "internal error\n");
|
|
275
189
|
});
|
|
276
190
|
};
|
|
277
191
|
const accept = (s, response, request) => {
|
|
278
|
-
const started =
|
|
279
|
-
send(response, 202,
|
|
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"));
|
|
280
208
|
};
|
|
281
209
|
const control = async (s, request, response) => {
|
|
282
210
|
const path = new URL(request.url ?? "/", "http://deployer").pathname;
|
|
@@ -303,28 +231,17 @@ const control = async (s, request, response) => {
|
|
|
303
231
|
send(response, 409, "this server has no renovate_repositories\n");
|
|
304
232
|
return;
|
|
305
233
|
}
|
|
306
|
-
accept(s, response, jobRequest(job, field(input, "dryRun") === true, "manual"));
|
|
234
|
+
accept(s, response, jobRequest(newId(), job, field(input, "dryRun") === true, "manual"));
|
|
307
235
|
return;
|
|
308
236
|
}
|
|
309
237
|
send(response, 404, "not found\n");
|
|
310
238
|
};
|
|
311
239
|
export const serveDeployer = (env) => {
|
|
312
240
|
const s = settings(env);
|
|
313
|
-
|
|
314
|
-
|
|
315
|
-
const app = name === undefined || !appName(name).ok ? undefined : registered(s.platformDir, name);
|
|
316
|
-
if (request.method !== "POST" || app === undefined) {
|
|
317
|
-
send(response, 404, "not found\n");
|
|
318
|
-
return;
|
|
319
|
-
}
|
|
320
|
-
if (!app.ok) {
|
|
321
|
-
send(response, 404, `${app.error}\n`);
|
|
322
|
-
return;
|
|
323
|
-
}
|
|
324
|
-
accept(s, response, deployRequest(app.value, app.value.ref, "push"));
|
|
325
|
-
return Promise.resolve();
|
|
326
|
-
})).listen(8090);
|
|
241
|
+
abandon(s);
|
|
242
|
+
createServer(handle((request, response) => { push(s, request, response); })).listen(8090);
|
|
327
243
|
createServer(handle((request, response) => control(s, request, response))).listen(8091, "127.0.0.1");
|
|
328
244
|
schedule(s);
|
|
245
|
+
setTimeout(() => { void restore(s); }, restoreAfter);
|
|
329
246
|
console.log(`deployer: :8090 pushes, 127.0.0.1:8091 control; renovate ${s.renovate === undefined ? "off" : "on"}`);
|
|
330
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
|
|
32
|
-
*
|
|
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
|
|
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
|
|
2
|
+
/** The read-only runs pages at deploy.<domain>, behind Authelia. */
|
|
3
3
|
const escape = (text) => text.replaceAll("&", "&").replaceAll("<", "<").replaceAll(">", ">").replaceAll('"', """);
|
|
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
|
|
31
|
-
const waiting =
|
|
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. */
|
|
@@ -0,0 +1,6 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The apps to deploy again as they were: deployed once, yet no container of
|
|
3
|
+
* theirs running now — after a Docker update, a reboot, or anything else that
|
|
4
|
+
* stopped them without kayra.
|
|
5
|
+
*/
|
|
6
|
+
export const toRestore = (apps, running) => Object.freeze(apps.flatMap((each) => (each.live !== undefined && !running(each.app) ? Object.freeze([{ app: each.app, live: each.live }]) : Object.freeze([]))));
|