@yunuseon/kayra 0.21.1 → 0.22.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/ansible/playbooks/edge.yml +7 -2
- package/ansible/playbooks/platform.yml +3 -0
- package/ansible/roles/backup-check/files/backup-check.service +1 -1
- package/ansible/roles/backup-check/tasks/main.yml +18 -4
- package/ansible/roles/backup-check/templates/backup-check.sh.j2 +25 -42
- package/ansible/roles/monitor/tasks/main.yml +58 -0
- package/ansible/roles/monitor/templates/config.yaml.j2 +33 -0
- package/ansible/roles/monitor/templates/docker-compose.yml.j2 +17 -0
- package/ansible/roles/monitor/templates/env.j2 +4 -0
- package/ansible/roles/ntfy/files/docker-compose.yml +25 -0
- package/ansible/roles/ntfy/handlers/main.yml +6 -0
- package/ansible/roles/ntfy/tasks/main.yml +37 -0
- package/ansible/roles/ntfy/templates/server.yml.j2 +23 -0
- package/ansible/roles/platform/defaults/main.yml +5 -3
- package/ansible/roles/platform/tasks/main.yml +16 -8
- package/cli/dist/core/fleet/settings.js +16 -8
- package/cli/dist/core/recipes/host.js +2 -1
- package/cli/dist/core/recipes/steps.js +7 -0
- package/cli/dist/deployer/build/builder.js +17 -1
- package/cli/dist/deployer/deployer.js +18 -4
- package/cli/dist/deployer/forges/notify.js +8 -8
- package/cli/dist/deployer/forges/ntfy.js +35 -0
- package/cli/dist/deployer/health.js +42 -0
- package/cli/dist/deployer/settings.js +11 -17
- package/compose/docker-compose.ci.yml +7 -1
- package/package.json +1 -1
- package/schema/defaults.json +102 -19
- package/schema/vars-edge.json +121 -9
- package/schema/vars-platform.json +55 -10
- package/scripts/local/monitor-endpoints.sh +98 -0
- package/cli/dist/deployer/forges/issues.js +0 -72
|
@@ -0,0 +1,98 @@
|
|
|
1
|
+
#!/usr/bin/env bash
|
|
2
|
+
# =============================================================================
|
|
3
|
+
# monitor-endpoints.sh <server> — what the server's monitor (Gatus, the
|
|
4
|
+
# monitor role) checks, from the same sources as verify.sh: for each server in
|
|
5
|
+
# its vars.yml's monitor.watch,
|
|
6
|
+
# platform: auth.<domain> answers; the platform's pages (hosts.conf) send a
|
|
7
|
+
# browser to the login; every app route answers, not 5xx;
|
|
8
|
+
# deploy.<domain>/health is 200 (monitor_health_tokens.<server>)
|
|
9
|
+
# edge: every edge_routes name answers, not 5xx
|
|
10
|
+
# nas: nothing of its own: its stacks are monitor.extra
|
|
11
|
+
# plus monitor.extra as given. Every certificate must have 10 days left. Each
|
|
12
|
+
# name of a watched server is pinned to its inventory's address, as verify
|
|
13
|
+
# does, so home names resolve from wherever the monitor runs.
|
|
14
|
+
#
|
|
15
|
+
# Prints one JSON object, for ansible-playbook -e:
|
|
16
|
+
# {"monitor_endpoints": [{name, group, url, conditions, redirects, health}],
|
|
17
|
+
# "monitor_hosts": {"<host>": "<address>"}}
|
|
18
|
+
# Written for macOS's bash 3.2 too.
|
|
19
|
+
# =============================================================================
|
|
20
|
+
set -euo pipefail
|
|
21
|
+
|
|
22
|
+
KAYRA_ROOT="$(cd "$(dirname "$0")/../.." && pwd)"
|
|
23
|
+
FLEET_ROOT="${FLEET_ROOT:?the directory of the fleet (servers/), set by kayra}"
|
|
24
|
+
name="${1:?Usage: monitor-endpoints.sh <server>}"
|
|
25
|
+
vars="${FLEET_ROOT}/servers/${name}/vars.yml"
|
|
26
|
+
[ -f "$vars" ] || vars=/dev/null
|
|
27
|
+
|
|
28
|
+
cert='[CERTIFICATE_EXPIRATION] > 240h'
|
|
29
|
+
|
|
30
|
+
# One endpoint as JSON: name group url redirects health condition...
|
|
31
|
+
endpoint() {
|
|
32
|
+
local endpoint_name="$1" group="$2" url="$3" redirects="$4" health="$5"
|
|
33
|
+
shift 5
|
|
34
|
+
jq -cn --arg name "$endpoint_name" --arg group "$group" --arg url "$url" \
|
|
35
|
+
--argjson redirects "$redirects" --argjson health "$health" '$ARGS.positional as $c |
|
|
36
|
+
{name: $name, group: $group, url: $url, conditions: $c, redirects: $redirects, health: $health}' \
|
|
37
|
+
--args "$@"
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
address_of() {
|
|
41
|
+
local inventory="${FLEET_ROOT}/servers/$1/inventory.ini"
|
|
42
|
+
[ -f "$inventory" ] || { echo "monitor: ${1} has no inventory.ini to take its address from: provision it first" >&2; exit 1; }
|
|
43
|
+
awk '{for (i = 1; i <= NF; i++) if (index($i, "ansible_host=") == 1) {print substr($i, 14); exit}}' "$inventory"
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
watched() {
|
|
47
|
+
local server="$1" dir="${FLEET_ROOT}/servers/$1" kind domain
|
|
48
|
+
kind="$(yq -r '.kind' "${dir}/server.yml")"
|
|
49
|
+
domain="$(yq -r '.domain' "${dir}/server.yml")"
|
|
50
|
+
case "$kind" in
|
|
51
|
+
platform)
|
|
52
|
+
endpoint "auth.${domain}" "$server" "https://auth.${domain}/" false false '[STATUS] == 200' "$cert"
|
|
53
|
+
awk '!/^[[:space:]]*(#|$)/ {print $1}' "${KAYRA_ROOT}/hosts.conf" | while IFS= read -r sub; do
|
|
54
|
+
[ "$sub" = auth ] || endpoint "${sub}.${domain}" "$server" "https://${sub}.${domain}/" false false '[STATUS] == 302' "$cert"
|
|
55
|
+
done
|
|
56
|
+
endpoint "deploy.${domain}/health" "$server" "https://deploy.${domain}/health" false true '[STATUS] == 200'
|
|
57
|
+
bash "${KAYRA_ROOT}/scripts/local/app-manifests.sh" --routes "$server" | while read -r sub path; do
|
|
58
|
+
[ -z "$sub" ] || endpoint "${sub}.${domain}${path%/}" "$server" "https://${sub}.${domain}${path}" true false '[STATUS] < 500' "$cert"
|
|
59
|
+
done
|
|
60
|
+
;;
|
|
61
|
+
edge)
|
|
62
|
+
yq -r '.edge_routes // [] | .[].subdomains[]' "${dir}/vars.yml" | while IFS= read -r sub; do
|
|
63
|
+
endpoint "${sub}.${domain}" "$server" "https://${sub}.${domain}/" true false '[STATUS] < 500' "$cert"
|
|
64
|
+
done
|
|
65
|
+
;;
|
|
66
|
+
esac
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
# Command substitutions swallow a failure (bash 3.2 has no inherit_errexit): check first.
|
|
70
|
+
watch="$(yq -r '.monitor.watch // [] | .[]' "$vars")"
|
|
71
|
+
pinned="$(yq -r '.monitor.extra // [] | .[] | select(.server != null) | .server' "$vars")"
|
|
72
|
+
for server in $watch $pinned; do
|
|
73
|
+
[ -f "${FLEET_ROOT}/servers/${server}/server.yml" ] || { echo "monitor: ${name} watches ${server}, which is no server of this fleet" >&2; exit 1; }
|
|
74
|
+
[ -n "$(address_of "$server")" ] || { echo "monitor: ${server}'s inventory.ini names no ansible_host" >&2; exit 1; }
|
|
75
|
+
done
|
|
76
|
+
|
|
77
|
+
endpoints="$(
|
|
78
|
+
for server in $watch; do watched "$server"; done
|
|
79
|
+
yq -o json -I0 '.monitor.extra // [] | .[]' "$vars" | while IFS= read -r extra; do
|
|
80
|
+
jq -c '{name, group: (.group // "extra"), url, conditions: (.conditions // ["[STATUS] == 200"]), redirects: true, health: false}' <<< "$extra"
|
|
81
|
+
done
|
|
82
|
+
)"
|
|
83
|
+
|
|
84
|
+
# Every name a watched server answers for, pinned to its address; extras pinned by their `server`.
|
|
85
|
+
hosts="$(
|
|
86
|
+
for server in $watch; do
|
|
87
|
+
address="$(address_of "$server")"
|
|
88
|
+
jq -r --arg group "$server" 'select(.group == $group) | .url' <<< "$endpoints" | sed -E 's#^https?://([^/:]+).*#\1#' | sort -u \
|
|
89
|
+
| while IFS= read -r host; do jq -cn --arg host "$host" --arg address "$address" '{($host): $address}'; done
|
|
90
|
+
done
|
|
91
|
+
yq -o json -I0 '.monitor.extra // [] | .[] | select(.server != null)' "$vars" | while IFS= read -r extra; do
|
|
92
|
+
address="$(address_of "$(jq -r .server <<< "$extra")")"
|
|
93
|
+
jq -cn --arg host "$(jq -r .url <<< "$extra" | sed -E 's#^https?://([^/:]+).*#\1#')" --arg address "$address" '{($host): $address}'
|
|
94
|
+
done
|
|
95
|
+
)"
|
|
96
|
+
|
|
97
|
+
jq -cn --argjson endpoints "$(jq -s . <<< "$endpoints")" --argjson hosts "$(jq -s 'add // {}' <<< "$hosts")" \
|
|
98
|
+
'{monitor_endpoints: $endpoints, monitor_hosts: $hosts}'
|
|
@@ -1,72 +0,0 @@
|
|
|
1
|
-
import { createSign } from "node:crypto";
|
|
2
|
-
import { err, ok } from "../../lib/result.js";
|
|
3
|
-
import { field } from "../app.js";
|
|
4
|
-
import { typed } from "../../lib/typed.js";
|
|
5
|
-
const base64url = (data) => Buffer.from(data).toString("base64url");
|
|
6
|
-
/** The app's own token: a JWT signed with its private key, valid nine minutes. */
|
|
7
|
-
export const appJwt = (appId, privateKey, nowSeconds) => {
|
|
8
|
-
const header = base64url(JSON.stringify({ alg: "RS256", typ: "JWT" }));
|
|
9
|
-
const payload = base64url(JSON.stringify({ iat: nowSeconds - 60, exp: nowSeconds + 540, iss: appId }));
|
|
10
|
-
const signature = createSign("RSA-SHA256").update(`${header}.${payload}`).sign(privateKey);
|
|
11
|
-
return `${header}.${payload}.${base64url(signature)}`;
|
|
12
|
-
};
|
|
13
|
-
const api = "https://api.github.com";
|
|
14
|
-
const headers = (token) => typed({
|
|
15
|
-
Authorization: `Bearer ${token}`,
|
|
16
|
-
Accept: "application/vnd.github+json",
|
|
17
|
-
"Content-Type": "application/json",
|
|
18
|
-
});
|
|
19
|
-
export const githubIssues = (repository, appId, world) => {
|
|
20
|
-
const call = async (token, method, path, body) => {
|
|
21
|
-
const response = await world
|
|
22
|
-
.fetch(`${api}${path}`, { method, headers: headers(token), ...(body === undefined ? {} : { body: JSON.stringify(body) }) })
|
|
23
|
-
.catch((error) => ({ status: 0, json: String(error) }));
|
|
24
|
-
return response.status >= 200 && response.status < 300 ? ok(response.json) : err(`GitHub ${method} ${path}: ${String(response.status)}`);
|
|
25
|
-
};
|
|
26
|
-
/** The installation's token for the repository, through the app's JWT. */
|
|
27
|
-
const token = async () => {
|
|
28
|
-
const key = world.privateKey();
|
|
29
|
-
if (!key.ok)
|
|
30
|
-
return key;
|
|
31
|
-
const jwt = appJwt(appId, key.value, Math.floor(world.now() / 1000));
|
|
32
|
-
const installation = await call(jwt, "GET", `/repos/${repository}/installation`);
|
|
33
|
-
const id = installation.ok && typeof installation.value === "object" && installation.value !== null ? field(installation.value, "id") : undefined;
|
|
34
|
-
if (typeof id !== "number")
|
|
35
|
-
return err(installation.ok ? "GitHub: the app is not installed on the repository" : installation.error);
|
|
36
|
-
const access = await call(jwt, "POST", `/app/installations/${String(id)}/access_tokens`);
|
|
37
|
-
const value = access.ok && typeof access.value === "object" && access.value !== null ? field(access.value, "token") : undefined;
|
|
38
|
-
return typeof value === "string" ? ok(value) : err(access.ok ? "GitHub: no installation token" : access.error);
|
|
39
|
-
};
|
|
40
|
-
const raise = async (alert) => {
|
|
41
|
-
if (world.openIssue(alert.key) !== undefined)
|
|
42
|
-
return ok(undefined);
|
|
43
|
-
const minted = await token();
|
|
44
|
-
if (!minted.ok)
|
|
45
|
-
return minted;
|
|
46
|
-
const created = await call(minted.value, "POST", `/repos/${repository}/issues`, { title: alert.title, body: alert.body });
|
|
47
|
-
if (!created.ok)
|
|
48
|
-
return created;
|
|
49
|
-
const number = typeof created.value === "object" && created.value !== null ? field(created.value, "number") : undefined;
|
|
50
|
-
if (typeof number !== "number")
|
|
51
|
-
return err("GitHub: the new issue has no number");
|
|
52
|
-
world.remember(alert.key, number);
|
|
53
|
-
return ok(undefined);
|
|
54
|
-
};
|
|
55
|
-
const resolve = async (key, note) => {
|
|
56
|
-
const issue = world.openIssue(key);
|
|
57
|
-
if (issue === undefined)
|
|
58
|
-
return ok(undefined);
|
|
59
|
-
const minted = await token();
|
|
60
|
-
if (!minted.ok)
|
|
61
|
-
return minted;
|
|
62
|
-
const commented = await call(minted.value, "POST", `/repos/${repository}/issues/${String(issue)}/comments`, { body: note });
|
|
63
|
-
if (!commented.ok)
|
|
64
|
-
return commented;
|
|
65
|
-
const closed = await call(minted.value, "PATCH", `/repos/${repository}/issues/${String(issue)}`, { state: "closed", state_reason: "completed" });
|
|
66
|
-
if (!closed.ok)
|
|
67
|
-
return closed;
|
|
68
|
-
world.remember(key, undefined);
|
|
69
|
-
return ok(undefined);
|
|
70
|
-
};
|
|
71
|
-
return typed({ raise, resolve });
|
|
72
|
-
};
|