@yunuseon/kayra 0.18.0 → 0.19.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/agent/skills/kayra-rollout/SKILL.md +3 -2
- package/ansible/playbooks/edge.yml +5 -0
- package/ansible/playbooks/nas.yml +9 -1
- package/ansible/playbooks/platform.yml +8 -2
- package/ansible/roles/app/tasks/main.yml +11 -3
- package/ansible/roles/kayra-version/defaults/main.yml +5 -0
- package/ansible/roles/kayra-version/tasks/main.yml +18 -0
- package/ansible/roles/platform/tasks/main.yml +6 -4
- package/cli/dist/browser/assets/index-C4HCL022.js +7 -0
- package/cli/dist/browser/index.html +1 -1
- package/cli/dist/clients/cli/status.js +28 -7
- package/cli/dist/clients/wire/schemas.js +2 -1
- package/cli/dist/clients/wire/status.js +7 -1
- package/cli/dist/core/adapters/network.js +20 -1
- package/cli/dist/core/fleet/brand.js +5 -0
- package/cli/dist/core/fleet/parse.js +10 -1
- package/cli/dist/core/fleet/settings.js +18 -3
- package/cli/dist/core/operations/dispatch.js +3 -3
- package/cli/dist/core/recipes/doctor.js +3 -3
- package/cli/dist/core/recipes/host.js +8 -3
- package/cli/dist/core/status/look.js +33 -13
- package/cli/dist/core/status/model.js +2 -0
- package/cli/dist/deployer/brand.js +5 -0
- package/cli/dist/deployer/build/builder.js +5 -0
- package/cli/dist/deployer/build/bundle.js +5 -0
- package/cli/dist/deployer/build/compose.js +21 -8
- package/cli/dist/deployer/build/routes.js +128 -0
- package/cli/dist/deployer/deployer.js +10 -4
- package/cli/dist/deployer/runs/run.js +14 -7
- package/package.json +1 -1
- package/schema/app.json +64 -8
- package/scripts/local/app-check.sh +12 -9
- package/scripts/local/app-manifests.sh +11 -6
- package/scripts/local/verify.sh +22 -11
- package/scripts/platform/compose-policy.sh +37 -40
- package/cli/dist/browser/assets/index-hcyM6f6T.js +0 -7
- package/compose/traefik/dynamic/ratelimit.yml +0 -21
|
@@ -14,7 +14,7 @@ export const provision = (engine, server) => ok(partOf("provision", [
|
|
|
14
14
|
]
|
|
15
15
|
: []),
|
|
16
16
|
]));
|
|
17
|
-
/** The playbook for the server's kind, as setup runs it; plan adds check mode. */
|
|
17
|
+
/** The playbook for the server's kind, as setup runs it; plan adds check mode. It records which kayra ran it (the kayra-version role). */
|
|
18
18
|
const hostPlaybook = (engine, server, extra = []) => vaulted(run(`Host layer · ${server.kind}.yml${extra.includes("--check") ? ", check mode" : ""}`, [
|
|
19
19
|
"ansible-playbook",
|
|
20
20
|
"-i",
|
|
@@ -28,6 +28,8 @@ const hostPlaybook = (engine, server, extra = []) => vaulted(run(`Host layer ·
|
|
|
28
28
|
`domain=${server.domain}`,
|
|
29
29
|
"-e",
|
|
30
30
|
`target=${server.target}`,
|
|
31
|
+
"-e",
|
|
32
|
+
`kayra_version=${engine.version}`,
|
|
31
33
|
...extra,
|
|
32
34
|
], ansibleEnv(engine, {})), server);
|
|
33
35
|
/** The host layer: the playbook for the server's kind. */
|
|
@@ -70,9 +72,12 @@ export const hosts = (engine, server) => ok(partOf("hosts", server.target === "m
|
|
|
70
72
|
]), [{ kind: "sudo" }])
|
|
71
73
|
]
|
|
72
74
|
: [{ kind: "note", text: `hosts: ${server.target} uses real DNS, nothing to do.` }]));
|
|
73
|
-
/**
|
|
75
|
+
/**
|
|
76
|
+
* Whether a platform server works as its users see it (scripts/local/verify.sh),
|
|
77
|
+
* and runs the kayra this is; other kinds have nothing to check yet.
|
|
78
|
+
*/
|
|
74
79
|
const verifyStep = (engine, server) => typed({
|
|
75
|
-
...run("Verify", ["bash", script(engine, "verify.sh"), server.name], fleetEnv(engine)),
|
|
80
|
+
...run("Verify", ["bash", script(engine, "verify.sh"), server.name], { ...fleetEnv(engine), KAYRA_VERSION: engine.version }),
|
|
76
81
|
part: "verify",
|
|
77
82
|
});
|
|
78
83
|
export const verify = (engine, server) => server.kind === "platform"
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { host, sshDefaultPort, within } from "../fleet/brand.js";
|
|
1
|
+
import { host, kayraVersion, sshDefaultPort, within } from "../fleet/brand.js";
|
|
2
2
|
import { isMachine } from "./model.js";
|
|
3
3
|
import { typed } from "../../lib/typed.js";
|
|
4
4
|
/** The inventory names the address: hand-written at home, written by provision elsewhere. */
|
|
@@ -22,34 +22,54 @@ export const sshPort = (server) => server.inventory?.port ?? sshDefaultPort;
|
|
|
22
22
|
export const authUrl = (server) => `https://auth.${server.domain}/`;
|
|
23
23
|
const pending = { kind: "pending" };
|
|
24
24
|
const skipped = { kind: "skipped" };
|
|
25
|
+
const notAsked = { kind: "skipped" };
|
|
26
|
+
/** What the server's version file holds, as a version: none where setup never recorded one. */
|
|
27
|
+
const running = (read) => {
|
|
28
|
+
if (!read.ok)
|
|
29
|
+
return typed({ kind: "unreadable", reason: read.error });
|
|
30
|
+
const text = read.value.trim();
|
|
31
|
+
if (text === "")
|
|
32
|
+
return typed({ kind: "unrecorded" });
|
|
33
|
+
const version = kayraVersion(text);
|
|
34
|
+
return version.ok ? typed({ kind: "recorded", version: version.value }) : typed({ kind: "unreadable", reason: version.error });
|
|
35
|
+
};
|
|
36
|
+
/** Which kayra set the server up, once SSH answered: its version file, over SSH as the inventory says. */
|
|
37
|
+
const recorded = (server, network, ssh) => ssh.kind === "open" && server.inventory !== undefined && server.versionFile !== undefined
|
|
38
|
+
? network.read(server.inventory, server.versionFile).then(running)
|
|
39
|
+
: Promise.resolve(notAsked);
|
|
25
40
|
const at = (index, change) => (looks) => typed(looks.map((look, each) => (each === index ? { ...look, ...change } : look)));
|
|
41
|
+
/** What the network answers: SSH, then the version file once SSH is open; on a platform server, Authelia's health. */
|
|
42
|
+
const probes = (server, index, machine, network) => {
|
|
43
|
+
const ssh = machine.then((found) => found.kind === "found" && found.address !== undefined ? network.connect(found.address, sshPort(server)) : skipped);
|
|
44
|
+
return typed([
|
|
45
|
+
ssh.then((answer) => at(index, { ssh: answer })),
|
|
46
|
+
ssh.then((answer) => recorded(server, network, answer)).then((answer) => at(index, { kayra: answer })),
|
|
47
|
+
...(server.kind === "platform" ? [network.get(`${authUrl(server)}api/health`).then((answer) => at(index, { https: answer }))] : []),
|
|
48
|
+
]);
|
|
49
|
+
};
|
|
26
50
|
/**
|
|
27
51
|
* Every server as far as the fleet's files tell, and the answers still to
|
|
28
52
|
* come, each an update to it: OpenTofu's state where no inventory names the
|
|
29
|
-
* address, then a TCP connect to SSH
|
|
30
|
-
*
|
|
31
|
-
* (--offline), only
|
|
53
|
+
* address, then a TCP connect to SSH and, once it is open, the version file
|
|
54
|
+
* its setup wrote, over SSH; on platform servers HTTPS on auth.<domain>
|
|
55
|
+
* (Authelia's health), all at once. Without a network (--offline), only
|
|
56
|
+
* OpenTofu.
|
|
32
57
|
*/
|
|
33
|
-
export const look = (fleet, tofu, network) => fleet.servers.reduce((so, server, index) => {
|
|
58
|
+
export const look = (fleet, pin, tofu, network) => fleet.servers.reduce((so, server, index) => {
|
|
34
59
|
const known = listed(server);
|
|
35
60
|
const machine = known === undefined ? provisioned(server, tofu) : Promise.resolve(known);
|
|
36
61
|
const platform = server.kind === "platform";
|
|
37
62
|
const updates = [
|
|
38
63
|
...(known === undefined ? [machine.then((answer) => at(index, { machine: answer }))] : []),
|
|
39
|
-
...(network === undefined
|
|
40
|
-
? []
|
|
41
|
-
: [
|
|
42
|
-
machine
|
|
43
|
-
.then((found) => found.kind === "found" && found.address !== undefined ? network.connect(found.address, sshPort(server)) : skipped)
|
|
44
|
-
.then((answer) => at(index, { ssh: answer })),
|
|
45
|
-
...(platform ? [network.get(`${authUrl(server)}api/health`).then((answer) => at(index, { https: answer }))] : []),
|
|
46
|
-
]),
|
|
64
|
+
...(network === undefined ? [] : probes(server, index, machine, network)),
|
|
47
65
|
];
|
|
48
66
|
const first = {
|
|
49
67
|
server,
|
|
50
68
|
machine: known ?? pending,
|
|
51
69
|
ssh: network === undefined ? skipped : pending,
|
|
52
70
|
https: platform ? (network === undefined ? skipped : pending) : undefined,
|
|
71
|
+
kayra: network === undefined ? notAsked : pending,
|
|
72
|
+
pin,
|
|
53
73
|
};
|
|
54
74
|
return { initial: [...so.initial, first], updates: [...so.updates, ...updates] };
|
|
55
75
|
}, { initial: [], updates: [] });
|
|
@@ -1,5 +1,7 @@
|
|
|
1
1
|
/** Every state a probe's cell can be in, as the API names them. */
|
|
2
2
|
export const reachStates = ["pending", "open", "answered", "refused", "no-answer", "unreachable", "unknown-host", "bad-certificate", "failed", "skipped"];
|
|
3
|
+
/** Every state the running kayra's cell can be in. */
|
|
4
|
+
export const runningStates = ["pending", "recorded", "unrecorded", "unreadable", "skipped"];
|
|
3
5
|
/** Every state a machine's cell can be in. */
|
|
4
6
|
export const machineStates = ["pending", "found", "not-provisioned", "no-machine", "unreadable"];
|
|
5
7
|
/** A machine kayra's modules create, by its resource type; a data source is none. */
|
|
@@ -3,6 +3,11 @@ export const appName = (value) => /^[a-z][a-z0-9-]*$/.test(value) ? ok(value) :
|
|
|
3
3
|
export const sha = (value) => /^[0-9a-f]{40}$/.test(value) ? ok(value) : err(`'${value}' is not a full commit id`);
|
|
4
4
|
export const runId = (value) => /^\d{8}T\d{6}Z-[0-9a-f]{6}$/.test(value) ? ok(value) : err(`'${value}' is not a run id`);
|
|
5
5
|
export const newRunId = (now, random) => `${now.toISOString().replace(/[-:]/g, "").replace(/\.\d+Z$/, "Z")}-${random}`;
|
|
6
|
+
export const subdomain = (value) => /^[a-z0-9]([a-z0-9-]*[a-z0-9])?$/.test(value) ? ok(value) : err(`'${value}' is not a subdomain`);
|
|
7
|
+
export const serviceName = (value) => /^[a-zA-Z0-9][a-zA-Z0-9_.-]*$/.test(value) ? ok(value) : err(`'${value}' is not a compose service name`);
|
|
8
|
+
export const port = (value) => Number.isInteger(value) && value >= 1 && value <= 65535 ? ok(value) : err(`${String(value)} is not a port`);
|
|
9
|
+
export const routePath = (value) => /^\/[A-Za-z0-9._~%/-]*$/.test(value) ? ok(value) : err(`'${value}' is not a path (/, then letters, digits and ._~%/-)`);
|
|
10
|
+
export const period = (value) => /^[1-9][0-9]*(ms|s|m|h)$/.test(value) ? ok(value) : err(`'${value}' is not a period such as 10s, 10m or 1h`);
|
|
6
11
|
export const appOwner = (app) => app;
|
|
7
12
|
export const jobOwner = (job) => `_${job}`;
|
|
8
13
|
export const owner = (value) => /^(_gc|_renovate|[a-z][a-z0-9-]*)$/.test(value) ? ok(value) : err(`'${value}' has no runs`);
|
|
@@ -103,10 +103,15 @@ export const prepare = async (app, commit, s, line) => {
|
|
|
103
103
|
const missing = files.find((file) => !existsSync(join(root, file)));
|
|
104
104
|
if (missing !== undefined)
|
|
105
105
|
return err(`app.yml: compose file ${missing} does not exist at ${commit}`);
|
|
106
|
+
const routes = await run(["yq", "-o=json", ".routes // []", join(root, "app.yml")], { capture: true }, line);
|
|
107
|
+
if (!routes.ok)
|
|
108
|
+
return routes;
|
|
109
|
+
const routed = JSON.parse(routes.value);
|
|
106
110
|
return ok({
|
|
107
111
|
sha: commit,
|
|
108
112
|
images: artifacts.map(([, variable, ref]) => ({ variable, ref })),
|
|
109
113
|
compose: files.map((file) => ({ path: file, content: readFileSync(join(root, file), "utf8") })),
|
|
114
|
+
routes: routed,
|
|
110
115
|
});
|
|
111
116
|
};
|
|
112
117
|
const hook = async (s, name, request, response) => {
|
|
@@ -2,6 +2,7 @@ import { err, ok } from "../../lib/result.js";
|
|
|
2
2
|
import { field } from "../app.js";
|
|
3
3
|
import { sha } from "../brand.js";
|
|
4
4
|
import { typed } from "../../lib/typed.js";
|
|
5
|
+
import { parseRoutes } from "./routes.js";
|
|
5
6
|
const relative = (path) => path !== "" && !path.startsWith("/") && !path.split("/").some((segment) => segment === ".." || segment === "");
|
|
6
7
|
export const parseBundle = (app, expected, json) => {
|
|
7
8
|
if (typeof json !== "object" || json === null)
|
|
@@ -40,10 +41,14 @@ export const parseBundle = (app, expected, json) => {
|
|
|
40
41
|
const problem = [...parsedImages, ...parsedCompose].find((item) => typeof item === "string");
|
|
41
42
|
if (problem !== undefined)
|
|
42
43
|
return err(`bundle: ${problem}`);
|
|
44
|
+
const routes = parseRoutes(field(json, "routes"));
|
|
45
|
+
if (!routes.ok)
|
|
46
|
+
return err(`bundle: ${routes.error}`);
|
|
43
47
|
return ok({
|
|
44
48
|
sha: minted.value,
|
|
45
49
|
images: parsedImages.filter((item) => typeof item !== "string"),
|
|
46
50
|
compose: parsedCompose.filter((item) => typeof item !== "string"),
|
|
51
|
+
routes: routes.value,
|
|
47
52
|
});
|
|
48
53
|
};
|
|
49
54
|
/** images.env, as compose reads it next to the app's own .env. */
|
|
@@ -1,4 +1,13 @@
|
|
|
1
|
+
import { wireRoutes } from "./routes.js";
|
|
1
2
|
import { typed } from "../../lib/typed.js";
|
|
3
|
+
/**
|
|
4
|
+
* The run's directory: the app's compose files under app/, at their paths in
|
|
5
|
+
* the app; the platform's files beside it, where no app file can stand in for
|
|
6
|
+
* them.
|
|
7
|
+
*/
|
|
8
|
+
export const appFiles = "app";
|
|
9
|
+
export const imagesFile = "images.env";
|
|
10
|
+
export const routesFile = "routes.yml";
|
|
2
11
|
/**
|
|
3
12
|
* The app's compose project, run in the run's directory: its .env from the
|
|
4
13
|
* platform (operator config), the image refs from this run, the compose files
|
|
@@ -8,20 +17,24 @@ import { typed } from "../../lib/typed.js";
|
|
|
8
17
|
const project = (app, appsDir, bundle) => {
|
|
9
18
|
const dir = `${appsDir}/${app.name}`;
|
|
10
19
|
return typed([
|
|
11
|
-
"compose", "-p", app.name, "--project-directory", dir, "--env-file", `${dir}/.env`, "--env-file",
|
|
12
|
-
...bundle.compose.flatMap((file) => ["-f", file.path]),
|
|
20
|
+
"compose", "-p", app.name, "--project-directory", dir, "--env-file", `${dir}/.env`, "--env-file", imagesFile,
|
|
21
|
+
...bundle.compose.flatMap((file) => ["-f", `${appFiles}/${file.path}`]),
|
|
13
22
|
]);
|
|
14
23
|
};
|
|
15
24
|
/**
|
|
16
|
-
* What admission checks. --no-env-resolution:
|
|
17
|
-
* env_file here, from any path (another app's
|
|
18
|
-
* into the config; unresolved, the allow-list
|
|
25
|
+
* What admission checks: the app's files alone. --no-env-resolution:
|
|
26
|
+
* otherwise compose reads every env_file here, from any path (another app's
|
|
27
|
+
* secrets included), and bakes it into the config; unresolved, the allow-list
|
|
28
|
+
* sees env_file and refuses it.
|
|
19
29
|
*/
|
|
20
30
|
export const config = (app, appsDir, bundle) => typed([
|
|
21
31
|
"docker", ...project(app, appsDir, bundle), "config", "--no-env-resolution", "--format", "json",
|
|
22
32
|
]);
|
|
23
|
-
export const policy = (app, script) => typed(["bash", script, app.name, ...app.hosts]);
|
|
24
|
-
/**
|
|
33
|
+
export const policy = (app, script, domain, bundle) => typed(["bash", script, app.name, domain, JSON.stringify(wireRoutes(bundle.routes)), ...app.hosts]);
|
|
34
|
+
/**
|
|
35
|
+
* The app's files, then the platform's routes. --remove-orphans: a service the
|
|
36
|
+
* app dropped from its compose files must not keep running.
|
|
37
|
+
*/
|
|
25
38
|
export const up = (app, appsDir, bundle) => typed([
|
|
26
|
-
"docker", ...project(app, appsDir, bundle), "up", "-d", "--wait", "--wait-timeout", "180", "--remove-orphans",
|
|
39
|
+
"docker", ...project(app, appsDir, bundle), "-f", routesFile, "up", "-d", "--wait", "--wait-timeout", "180", "--remove-orphans",
|
|
27
40
|
]);
|
|
@@ -0,0 +1,128 @@
|
|
|
1
|
+
import { all, attempt, err, flatMap, ok } from "../../lib/result.js";
|
|
2
|
+
import { typed } from "../../lib/typed.js";
|
|
3
|
+
import { field } from "../app.js";
|
|
4
|
+
import { period, port, routePath, serviceName, subdomain } from "../brand.js";
|
|
5
|
+
const unknownKey = (value, known) => Object.keys(value).find((key) => !known.includes(key));
|
|
6
|
+
const count = (value, name) => typeof value === "number" && Number.isInteger(value) && value > 0 ? ok(value) : err(`${name} ${String(value)} is not a positive whole number`);
|
|
7
|
+
const text = (value, name) => (typeof value === "string" ? ok(value) : err(`${name} is missing or not text`));
|
|
8
|
+
const rateLimit = (value) => {
|
|
9
|
+
if (value === undefined)
|
|
10
|
+
return ok(undefined);
|
|
11
|
+
if (typeof value !== "object" || value === null)
|
|
12
|
+
return err("rate_limit is not a mapping");
|
|
13
|
+
const extra = unknownKey(value, ["average", "period", "burst"]);
|
|
14
|
+
if (extra !== undefined)
|
|
15
|
+
return err(`rate_limit.${extra} is not a rate limit field`);
|
|
16
|
+
const average = count(field(value, "average"), "rate_limit.average");
|
|
17
|
+
const burst = count(field(value, "burst"), "rate_limit.burst");
|
|
18
|
+
const per = flatMap(text(field(value, "period"), "rate_limit.period"), period);
|
|
19
|
+
if (!average.ok)
|
|
20
|
+
return average;
|
|
21
|
+
if (!burst.ok)
|
|
22
|
+
return burst;
|
|
23
|
+
if (!per.ok)
|
|
24
|
+
return per;
|
|
25
|
+
return ok({ average: average.value, period: per.value, burst: burst.value });
|
|
26
|
+
};
|
|
27
|
+
const route = (value, index) => {
|
|
28
|
+
const where = (problem) => `route ${String(index)}: ${problem}`;
|
|
29
|
+
if (typeof value !== "object" || value === null)
|
|
30
|
+
return err(where("not a mapping"));
|
|
31
|
+
const extra = unknownKey(value, ["host", "path", "service", "port", "rate_limit"]);
|
|
32
|
+
if (extra !== undefined)
|
|
33
|
+
return err(where(`${extra} is not a route field`));
|
|
34
|
+
const host = flatMap(text(field(value, "host"), "host"), subdomain);
|
|
35
|
+
const service = flatMap(text(field(value, "service"), "service"), serviceName);
|
|
36
|
+
const rawPort = field(value, "port");
|
|
37
|
+
const listens = typeof rawPort === "number" ? port(rawPort) : err(`port ${String(rawPort)} is not a port`);
|
|
38
|
+
const rawPath = field(value, "path");
|
|
39
|
+
const path = rawPath === undefined ? ok(undefined) : flatMap(text(rawPath, "path"), routePath);
|
|
40
|
+
const limit = rateLimit(field(value, "rate_limit"));
|
|
41
|
+
if (!host.ok)
|
|
42
|
+
return err(where(host.error));
|
|
43
|
+
if (!service.ok)
|
|
44
|
+
return err(where(service.error));
|
|
45
|
+
if (!listens.ok)
|
|
46
|
+
return err(where(listens.error));
|
|
47
|
+
if (!path.ok)
|
|
48
|
+
return err(where(path.error));
|
|
49
|
+
if (!limit.ok)
|
|
50
|
+
return err(where(limit.error));
|
|
51
|
+
return ok({ host: host.value, path: path.value, service: service.value, port: listens.value, rateLimit: limit.value });
|
|
52
|
+
};
|
|
53
|
+
/** app.yml's routes as the builder read them at the commit: untrusted, so every field is checked. */
|
|
54
|
+
export const parseRoutes = (json) => Array.isArray(json) ? all(json.map((item, index) => route(item, index))) : err("routes is not a list");
|
|
55
|
+
/** Routes as app.yml writes them, for the compose policy. */
|
|
56
|
+
export const wireRoutes = (routes) => Object.freeze(routes.map((route) => typed({
|
|
57
|
+
host: route.host,
|
|
58
|
+
...(route.path === undefined ? {} : { path: route.path }),
|
|
59
|
+
service: route.service,
|
|
60
|
+
port: route.port,
|
|
61
|
+
...(route.rateLimit === undefined ? {} : { rate_limit: route.rateLimit }),
|
|
62
|
+
})));
|
|
63
|
+
/** One line for the run's log: `www → web:3000, api/signup → api:3000 (rate limited)`. */
|
|
64
|
+
export const describeRoutes = (routes) => routes.length === 0
|
|
65
|
+
? "none"
|
|
66
|
+
: routes
|
|
67
|
+
.map((route) => `${route.host}${route.path ?? ""} → ${route.service}:${String(route.port)}${route.rateLimit === undefined ? "" : " (rate limited)"}`)
|
|
68
|
+
.join(", ");
|
|
69
|
+
/** A router per route: unique on the server through the app's prefix, readable in Traefik's dashboard. */
|
|
70
|
+
const routerName = (app, index, route) => `${app}-${String(index)}-${route.host}`;
|
|
71
|
+
/** A Traefik service per service and port, so one container can serve two ports. */
|
|
72
|
+
const backendName = (app, route) => `${app}-${route.service}-${String(route.port)}`;
|
|
73
|
+
const rule = (route, domain) => [`Host(\`${route.host}.${domain}\`)`, ...(route.path === undefined ? [] : [`PathPrefix(\`${route.path}\`)`])].join(" && ");
|
|
74
|
+
/** A route's labels. Security headers need none: the websecure entrypoint gives them to every router. */
|
|
75
|
+
const routeLabels = (app, domain, route, index) => {
|
|
76
|
+
const router = `traefik.http.routers.${routerName(app, index, route)}`;
|
|
77
|
+
const limit = `${routerName(app, index, route)}-limit`;
|
|
78
|
+
const backend = backendName(app, route);
|
|
79
|
+
return typed([
|
|
80
|
+
[`${router}.rule`, rule(route, domain)],
|
|
81
|
+
[`${router}.entrypoints`, "websecure"],
|
|
82
|
+
[`${router}.tls.certresolver`, "le"],
|
|
83
|
+
[`${router}.service`, backend],
|
|
84
|
+
[`traefik.http.services.${backend}.loadbalancer.server.port`, String(route.port)],
|
|
85
|
+
...(route.rateLimit === undefined
|
|
86
|
+
? []
|
|
87
|
+
: [
|
|
88
|
+
[`${router}.middlewares`, limit],
|
|
89
|
+
[`traefik.http.middlewares.${limit}.ratelimit.average`, String(route.rateLimit.average)],
|
|
90
|
+
[`traefik.http.middlewares.${limit}.ratelimit.period`, route.rateLimit.period],
|
|
91
|
+
[`traefik.http.middlewares.${limit}.ratelimit.burst`, String(route.rateLimit.burst)],
|
|
92
|
+
]),
|
|
93
|
+
]);
|
|
94
|
+
};
|
|
95
|
+
/** The networks the admitted config gives a service; Compose spells out the implicit default. */
|
|
96
|
+
const networksOf = (config, service) => {
|
|
97
|
+
const services = typeof config === "object" && config !== null ? field(config, "services") : undefined;
|
|
98
|
+
const definition = typeof services === "object" && services !== null ? field(services, service) : undefined;
|
|
99
|
+
if (typeof definition !== "object" || definition === null)
|
|
100
|
+
return err(`route to ${service}: no such service in the compose files`);
|
|
101
|
+
const networks = field(definition, "networks");
|
|
102
|
+
return ok(typeof networks === "object" && networks !== null ? { ...networks } : { default: null });
|
|
103
|
+
};
|
|
104
|
+
/**
|
|
105
|
+
* The platform's part of an app's compose project, from its admitted config
|
|
106
|
+
* and its routes: each routed service joins platform-public, beside the
|
|
107
|
+
* networks it has (restated, or the override would replace them), and gets the
|
|
108
|
+
* Traefik labels its routes become. Apps write no Traefik label themselves:
|
|
109
|
+
* the policy refuses them.
|
|
110
|
+
*/
|
|
111
|
+
export const routesOverride = (app, domain, routes, admitted) => flatMap(attempt(() => ok(JSON.parse(admitted)), (why) => `the admitted config is not JSON: ${why}`), (config) => {
|
|
112
|
+
const routed = [...new Set(routes.map((route) => route.service))];
|
|
113
|
+
const services = all(routed.map((service) => flatMap(networksOf(config, service), (networks) => ok([
|
|
114
|
+
service,
|
|
115
|
+
{
|
|
116
|
+
networks: { ...networks, "platform-public": null },
|
|
117
|
+
labels: Object.fromEntries([
|
|
118
|
+
["traefik.enable", "true"],
|
|
119
|
+
["traefik.docker.network", "platform-public"],
|
|
120
|
+
...routes.flatMap((route, index) => (route.service === service ? routeLabels(app, domain, route, index) : [])),
|
|
121
|
+
]),
|
|
122
|
+
},
|
|
123
|
+
]))));
|
|
124
|
+
return flatMap(services, (entries) => ok(`${JSON.stringify({
|
|
125
|
+
services: Object.fromEntries(entries),
|
|
126
|
+
...(entries.length === 0 ? {} : { networks: { "platform-public": { name: "platform-public", external: true } } }),
|
|
127
|
+
}, null, 2)}\n`));
|
|
128
|
+
});
|
|
@@ -9,6 +9,7 @@ import { all, err, ok } from "../lib/result.js";
|
|
|
9
9
|
import { field, registered } from "./app.js";
|
|
10
10
|
import { appName, appOwner, newRunId, sha } from "./brand.js";
|
|
11
11
|
import { matches, parseCron } from "./cron.js";
|
|
12
|
+
import { appFiles, imagesFile, routesFile } from "./build/compose.js";
|
|
12
13
|
import { forge, forgeRepo } from "./forges/forge.js";
|
|
13
14
|
import { json, send } from "./http/http.js";
|
|
14
15
|
import { spawnLines } from "./process.js";
|
|
@@ -148,13 +149,18 @@ const effects = (s, who, id, log) => typed({
|
|
|
148
149
|
secrets: appSecrets,
|
|
149
150
|
workdir: (bundle, images) => {
|
|
150
151
|
const dir = mkdtempSync(join(tmpdir(), `${id}-`));
|
|
151
|
-
writeFileSync(join(dir,
|
|
152
|
+
writeFileSync(join(dir, imagesFile), images);
|
|
152
153
|
bundle.compose.forEach((file) => {
|
|
153
|
-
|
|
154
|
-
|
|
154
|
+
const path = join(dir, appFiles, file.path);
|
|
155
|
+
mkdirSync(dirname(path), { recursive: true });
|
|
156
|
+
writeFileSync(path, file.content);
|
|
155
157
|
});
|
|
156
158
|
return Promise.resolve(dir);
|
|
157
159
|
},
|
|
160
|
+
writeRoutes: (cwd, content) => {
|
|
161
|
+
writeFileSync(join(cwd, routesFile), content);
|
|
162
|
+
return Promise.resolve();
|
|
163
|
+
},
|
|
158
164
|
record: (app, commit, images) => {
|
|
159
165
|
const dir = join(s.platformDir, "releases", app.name);
|
|
160
166
|
mkdirSync(dir, { recursive: true });
|
|
@@ -179,7 +185,7 @@ const work = async (s, request, fx) => {
|
|
|
179
185
|
fx.log(app.error);
|
|
180
186
|
return typed({ ok: false });
|
|
181
187
|
}
|
|
182
|
-
return deploy(app.value, request.work.ref, { appsDir: s.appsDir, policyScript: s.policyScript }, fx);
|
|
188
|
+
return deploy(app.value, request.work.ref, { appsDir: s.appsDir, policyScript: s.policyScript, domain: s.domain }, fx);
|
|
183
189
|
}
|
|
184
190
|
const argv = request.work.job === "gc" ? ["bash", s.gcScript] : renovate(s.env, request.work.dryRun, s.renovateImage);
|
|
185
191
|
const env = request.work.job === "renovate" ? { RENOVATE_TOKEN: token("RENOVATE_TOKEN") } : {};
|
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
import { err, ok } from "../../lib/result.js";
|
|
2
2
|
import { imagesEnv, parseBundle } from "../build/bundle.js";
|
|
3
3
|
import { config, policy, up } from "../build/compose.js";
|
|
4
|
+
import { describeRoutes, routesOverride } from "../build/routes.js";
|
|
4
5
|
import { typed } from "../../lib/typed.js";
|
|
5
6
|
const step = async (fx, label, argv, options) => {
|
|
6
7
|
fx.log(`── ${label}`);
|
|
@@ -9,21 +10,27 @@ const step = async (fx, label, argv, options) => {
|
|
|
9
10
|
};
|
|
10
11
|
/**
|
|
11
12
|
* Admission, then up: the compose config of the commit being deployed must
|
|
12
|
-
* pass the policy before anything starts, and what it checked is what runs
|
|
13
|
+
* pass the policy before anything starts, and what it checked is what runs,
|
|
14
|
+
* with the routes the platform writes from it.
|
|
13
15
|
*/
|
|
14
|
-
const deployBundle = async (app, bundle,
|
|
16
|
+
const deployBundle = async (app, bundle, site, fx) => {
|
|
15
17
|
const images = imagesEnv(bundle);
|
|
16
18
|
const secrets = fx.secrets(app);
|
|
17
19
|
if (!secrets.ok)
|
|
18
20
|
return secrets;
|
|
19
21
|
const cwd = await fx.workdir(bundle, images);
|
|
20
|
-
const resolved = await step(fx, "Admission: compose config", config(app,
|
|
22
|
+
const resolved = await step(fx, "Admission: compose config", config(app, site.appsDir, bundle), { cwd, env: secrets.value, capture: true });
|
|
21
23
|
if (!resolved.ok)
|
|
22
24
|
return resolved;
|
|
23
|
-
const admitted = await step(fx, "Admission: policy", policy(app,
|
|
25
|
+
const admitted = await step(fx, "Admission: policy", policy(app, site.policyScript, site.domain, bundle), { cwd, env: {}, stdin: resolved.value });
|
|
24
26
|
if (!admitted.ok)
|
|
25
27
|
return err("refused by the compose policy");
|
|
26
|
-
const
|
|
28
|
+
const routes = routesOverride(app.name, site.domain, bundle.routes, resolved.value);
|
|
29
|
+
if (!routes.ok)
|
|
30
|
+
return routes;
|
|
31
|
+
fx.log(`── Routes: ${describeRoutes(bundle.routes)}`);
|
|
32
|
+
await fx.writeRoutes(cwd, routes.value);
|
|
33
|
+
const started = await step(fx, "Up", up(app, site.appsDir, bundle), { cwd, env: secrets.value });
|
|
27
34
|
if (!started.ok)
|
|
28
35
|
return started;
|
|
29
36
|
await fx.record(app, bundle.sha, images);
|
|
@@ -34,7 +41,7 @@ const deployBundle = async (app, bundle, paths, fx) => {
|
|
|
34
41
|
* then admitted and started (kayra/deploy). Each phase posts its status on the
|
|
35
42
|
* commit; a failed build never reaches deploy.
|
|
36
43
|
*/
|
|
37
|
-
export const deploy = async (app, ref,
|
|
44
|
+
export const deploy = async (app, ref, site, fx) => {
|
|
38
45
|
fx.log(`Deploying ${app.name} at ${ref}`);
|
|
39
46
|
const commit = await fx.resolve(app, ref);
|
|
40
47
|
if (!commit.ok) {
|
|
@@ -53,7 +60,7 @@ export const deploy = async (app, ref, paths, fx) => {
|
|
|
53
60
|
}
|
|
54
61
|
await fx.status(app, sha, "kayra/build", "success", "Images built");
|
|
55
62
|
await fx.status(app, sha, "kayra/deploy", "pending", "Deploying");
|
|
56
|
-
const deployed = await deployBundle(app, bundle.value,
|
|
63
|
+
const deployed = await deployBundle(app, bundle.value, site, fx);
|
|
57
64
|
if (!deployed.ok) {
|
|
58
65
|
fx.log(deployed.error);
|
|
59
66
|
await fx.status(app, sha, "kayra/deploy", "failure", deployed.error);
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@yunuseon/kayra",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.19.0",
|
|
4
4
|
"description": "A self-hosted app platform and the CLI that runs it: Traefik, Authelia, a registry, rootless BuildKit and a small deployer per server, set up with OpenTofu and Ansible.",
|
|
5
5
|
"license": "MIT",
|
|
6
6
|
"author": "Yunus Öztürk",
|
package/schema/app.json
CHANGED
|
@@ -12,15 +12,72 @@
|
|
|
12
12
|
"items": {
|
|
13
13
|
"type": "string"
|
|
14
14
|
},
|
|
15
|
-
"description": "Compose files, relative to the app root, in order."
|
|
15
|
+
"description": "Compose files, relative to the app root, in order. No Traefik label, no platform network: routes say how the app is reached."
|
|
16
16
|
},
|
|
17
|
-
"
|
|
17
|
+
"routes": {
|
|
18
|
+
"description": "How the app is reached: each route a host (and path) to a service's port. Read at the commit being deployed; a new host needs kayra apps.",
|
|
18
19
|
"type": "array",
|
|
19
20
|
"items": {
|
|
20
|
-
"type": "
|
|
21
|
-
"
|
|
22
|
-
|
|
23
|
-
|
|
21
|
+
"type": "object",
|
|
22
|
+
"properties": {
|
|
23
|
+
"host": {
|
|
24
|
+
"description": "Its hostname, <host>.<domain>. The app's hosts are its routes' hosts.",
|
|
25
|
+
"type": "string",
|
|
26
|
+
"pattern": "^[a-z0-9]([a-z0-9-]*[a-z0-9])?$"
|
|
27
|
+
},
|
|
28
|
+
"path": {
|
|
29
|
+
"description": "A path prefix: the route answers below it, and beats its host's route without one.",
|
|
30
|
+
"type": "string",
|
|
31
|
+
"pattern": "^\\/[A-Za-z0-9._~%/-]*$"
|
|
32
|
+
},
|
|
33
|
+
"service": {
|
|
34
|
+
"description": "The compose service that answers it.",
|
|
35
|
+
"type": "string",
|
|
36
|
+
"pattern": "^[a-zA-Z0-9][a-zA-Z0-9_.-]*$"
|
|
37
|
+
},
|
|
38
|
+
"port": {
|
|
39
|
+
"type": "integer",
|
|
40
|
+
"minimum": 1,
|
|
41
|
+
"maximum": 65535,
|
|
42
|
+
"description": "The port that service listens on, inside its container."
|
|
43
|
+
},
|
|
44
|
+
"rate_limit": {
|
|
45
|
+
"description": "A rate limit on this route, per client address (Traefik's rateLimit).",
|
|
46
|
+
"type": "object",
|
|
47
|
+
"properties": {
|
|
48
|
+
"average": {
|
|
49
|
+
"type": "integer",
|
|
50
|
+
"exclusiveMinimum": 0,
|
|
51
|
+
"maximum": 9007199254740991,
|
|
52
|
+
"description": "Requests a client may send per period, on average."
|
|
53
|
+
},
|
|
54
|
+
"period": {
|
|
55
|
+
"description": "The period, such as 10s, 10m or 1h.",
|
|
56
|
+
"type": "string",
|
|
57
|
+
"pattern": "^[1-9][0-9]*(ms|s|m|h)$"
|
|
58
|
+
},
|
|
59
|
+
"burst": {
|
|
60
|
+
"type": "integer",
|
|
61
|
+
"exclusiveMinimum": 0,
|
|
62
|
+
"maximum": 9007199254740991,
|
|
63
|
+
"description": "Requests a client may send at once beyond the average."
|
|
64
|
+
}
|
|
65
|
+
},
|
|
66
|
+
"required": [
|
|
67
|
+
"average",
|
|
68
|
+
"period",
|
|
69
|
+
"burst"
|
|
70
|
+
],
|
|
71
|
+
"additionalProperties": false
|
|
72
|
+
}
|
|
73
|
+
},
|
|
74
|
+
"required": [
|
|
75
|
+
"host",
|
|
76
|
+
"service",
|
|
77
|
+
"port"
|
|
78
|
+
],
|
|
79
|
+
"additionalProperties": false
|
|
80
|
+
}
|
|
24
81
|
},
|
|
25
82
|
"secrets": {
|
|
26
83
|
"description": "Secret keys; their values come from the fleet's apps/<app>.secrets.yml.",
|
|
@@ -39,8 +96,7 @@
|
|
|
39
96
|
},
|
|
40
97
|
"required": [
|
|
41
98
|
"name",
|
|
42
|
-
"compose"
|
|
43
|
-
"subdomains"
|
|
99
|
+
"compose"
|
|
44
100
|
],
|
|
45
101
|
"additionalProperties": false,
|
|
46
102
|
"title": "app.yml"
|
|
@@ -4,13 +4,14 @@
|
|
|
4
4
|
# checked in the app's own repository, before a push: run by `kayra app
|
|
5
5
|
# check`, which has checked app.yml against its schema first.
|
|
6
6
|
#
|
|
7
|
-
# - app.yml's compose files exist, inside the app; its
|
|
8
|
-
# the platform's (hosts.conf)
|
|
7
|
+
# - app.yml's compose files exist, inside the app; its routes' hosts are
|
|
8
|
+
# not the platform's (hosts.conf)
|
|
9
9
|
# - every docker/image.json: its image in the app's namespace, a commit
|
|
10
10
|
# touching its inputs (resolve-images.sh --plan, as the builder runs it)
|
|
11
11
|
# - the compose config those files resolve to passes the compose policy
|
|
12
|
-
# (compose-policy.sh, as the deployer runs it
|
|
13
|
-
#
|
|
12
|
+
# (compose-policy.sh, as the deployer runs it: no Traefik label, no
|
|
13
|
+
# platform network, each route to one of its services), with placeholder
|
|
14
|
+
# values for the images, config and secrets, and hosts under example.test
|
|
14
15
|
#
|
|
15
16
|
# Needs git, jq, yq and the docker CLI with Compose (no daemon: `compose
|
|
16
17
|
# config` only reads files). Written for macOS's bash 3.2 too: no mapfile.
|
|
@@ -28,7 +29,8 @@ pass() { echo "✓ $*"; }
|
|
|
28
29
|
|
|
29
30
|
name="$(yq -r '.name' "$manifest")"
|
|
30
31
|
files="$(yq -r '.compose[]' "$manifest")"
|
|
31
|
-
|
|
32
|
+
routes="$(yq -o=json -I0 '.routes // []' "$manifest")"
|
|
33
|
+
subdomains="$(yq -r '.routes // [] | map(.host) | unique | .[]' "$manifest")"
|
|
32
34
|
reserved="$(awk '!/^[[:space:]]*(#|$)/ {print $1}' "${KAYRA_ROOT}/hosts.conf")"
|
|
33
35
|
|
|
34
36
|
# ── app.yml ─────────────────────────────────────────────────────────────────
|
|
@@ -39,9 +41,10 @@ while IFS= read -r file; do
|
|
|
39
41
|
esac
|
|
40
42
|
done <<< "$files"
|
|
41
43
|
while IFS= read -r sub; do
|
|
42
|
-
if grep -qxF -- "$sub" <<< "$reserved"; then fail "
|
|
44
|
+
if [ -n "$sub" ] && grep -qxF -- "$sub" <<< "$reserved"; then fail "route host ${sub} is the platform's"; fi
|
|
43
45
|
done <<< "$subdomains"
|
|
44
|
-
|
|
46
|
+
described="$(yq -r '.routes // [] | map(.host + (.path // "") + " → " + .service + ":" + (.port | tostring)) | join(", ")' "$manifest")"
|
|
47
|
+
[ "$failed" = 0 ] && pass "app.yml: compose $(paste -sd' ' - <<< "$files") · routes ${described:-none}"
|
|
45
48
|
|
|
46
49
|
# ── Images ──────────────────────────────────────────────────────────────────
|
|
47
50
|
if plan="$(bash "${KAYRA_ROOT}/scripts/platform/resolve-images.sh" "$name" "$root" --plan)"; then
|
|
@@ -71,8 +74,8 @@ while IFS= read -r file; do set -- "$@" -f "${root}/${file}"; done <<< "$files"
|
|
|
71
74
|
|| { fail "compose: docker compose config failed (above)"; exit 1; }
|
|
72
75
|
|
|
73
76
|
set --
|
|
74
|
-
while IFS= read -r sub; do set -- "$@" "${sub}.${domain}"; done <<< "$subdomains"
|
|
75
|
-
if violations="$(bash "${KAYRA_ROOT}/scripts/platform/compose-policy.sh" "$name" "$@" < "${work}/config.json")"; then
|
|
77
|
+
while IFS= read -r sub; do [ -z "$sub" ] || set -- "$@" "${sub}.${domain}"; done <<< "$subdomains"
|
|
78
|
+
if violations="$(bash "${KAYRA_ROOT}/scripts/platform/compose-policy.sh" "$name" "$domain" "$routes" "$@" < "${work}/config.json")"; then
|
|
76
79
|
pass "compose policy: admitted"
|
|
77
80
|
else
|
|
78
81
|
while IFS= read -r line; do fail "compose policy: ${line}"; done <<< "$violations"
|
|
@@ -4,14 +4,15 @@
|
|
|
4
4
|
# app role reads). A missing clone is
|
|
5
5
|
# fetched with the operator's own git credentials (kayra doctor checks them).
|
|
6
6
|
#
|
|
7
|
-
# Usage: FLEET_ROOT=<fleet> bash scripts/local/app-manifests.sh [--subdomains] <server-name>
|
|
8
|
-
# --subdomains print the apps'
|
|
7
|
+
# Usage: FLEET_ROOT=<fleet> bash scripts/local/app-manifests.sh [--subdomains|--routes] <server-name>
|
|
8
|
+
# --subdomains print the apps' hosts instead, their routes' (for certs and /etc/hosts)
|
|
9
|
+
# --routes print each route as "<subdomain> <path>", / where it has none (for verify)
|
|
9
10
|
set -euo pipefail
|
|
10
11
|
|
|
11
12
|
FLEET_ROOT="${FLEET_ROOT:?the directory of the fleet (servers/), set by kayra}"
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
name="${1:?Usage: app-manifests.sh [--subdomains] <server-name>}"
|
|
13
|
+
mode=manifests
|
|
14
|
+
case "${1:-}" in --subdomains) mode=subdomains; shift ;; --routes) mode=routes; shift ;; esac
|
|
15
|
+
name="${1:?Usage: app-manifests.sh [--subdomains|--routes] <server-name>}"
|
|
15
16
|
descriptor="${FLEET_ROOT}/servers/${name}/server.yml"
|
|
16
17
|
|
|
17
18
|
yq -o json -I0 '.apps // [] | .[]' "$descriptor" | while read -r app; do
|
|
@@ -22,5 +23,9 @@ yq -o json -I0 '.apps // [] | .[]' "$descriptor" | while read -r app; do
|
|
|
22
23
|
git clone -q --depth 1 --branch "$(jq -r '.ref // "main"' <<< "$app")" "$repo" "$clone"
|
|
23
24
|
fi
|
|
24
25
|
manifest="${clone}/${path}/app.yml"
|
|
25
|
-
|
|
26
|
+
case "$mode" in
|
|
27
|
+
subdomains) yq '.routes // [] | map(.host) | unique | .[]' "$manifest" ;;
|
|
28
|
+
routes) yq '.routes // [] | map(.host + " " + (.path // "/")) | unique | .[]' "$manifest" ;;
|
|
29
|
+
*) echo "$manifest" ;;
|
|
30
|
+
esac
|
|
26
31
|
done
|