@sixb/deploy-ssh 0.2.6 → 0.2.7

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (64) hide show
  1. package/README.md +51 -44
  2. package/dist/check/index.d.ts +24 -0
  3. package/dist/check/index.d.ts.map +1 -0
  4. package/dist/check/judge.d.ts +23 -0
  5. package/dist/check/judge.d.ts.map +1 -0
  6. package/dist/index.js +599 -1527
  7. package/dist/index.js.map +10 -16
  8. package/dist/layout.d.ts +87 -30
  9. package/dist/layout.d.ts.map +1 -1
  10. package/dist/operations.d.ts +5 -3
  11. package/dist/operations.d.ts.map +1 -1
  12. package/dist/plan.d.ts +25 -0
  13. package/dist/plan.d.ts.map +1 -0
  14. package/dist/render.d.ts +15 -11
  15. package/dist/render.d.ts.map +1 -1
  16. package/dist/server/manifest.d.ts +10 -4
  17. package/dist/server/manifest.d.ts.map +1 -1
  18. package/dist/server/release-host.d.ts +4 -0
  19. package/dist/server/release-host.d.ts.map +1 -0
  20. package/dist/server/release.d.ts +46 -0
  21. package/dist/server/release.d.ts.map +1 -0
  22. package/dist/server/state.d.ts +0 -4
  23. package/dist/server/state.d.ts.map +1 -1
  24. package/dist/server/supervisor.d.ts +8 -8
  25. package/dist/server/supervisor.d.ts.map +1 -1
  26. package/dist/ssh-target.d.ts +3 -24
  27. package/dist/ssh-target.d.ts.map +1 -1
  28. package/dist/transport.d.ts.map +1 -1
  29. package/package.json +3 -3
  30. package/src/bootstrap.sh +93 -0
  31. package/src/check/index.ts +117 -0
  32. package/src/check/judge.ts +299 -0
  33. package/src/check/probe.sh +99 -0
  34. package/src/layout.ts +109 -61
  35. package/src/operations.ts +105 -65
  36. package/src/plan.ts +59 -0
  37. package/src/render.ts +54 -35
  38. package/src/server/cli.ts +16 -20
  39. package/src/server/manifest.ts +13 -6
  40. package/src/server/release-host.ts +81 -0
  41. package/src/server/release.ts +738 -0
  42. package/src/server/state.ts +1 -5
  43. package/src/server/supervisor.ts +45 -238
  44. package/src/sh.d.ts +4 -0
  45. package/src/ssh-target.ts +43 -285
  46. package/src/transport.ts +28 -12
  47. package/dist/access.d.ts +0 -11
  48. package/dist/access.d.ts.map +0 -1
  49. package/dist/check.d.ts +0 -31
  50. package/dist/check.d.ts.map +0 -1
  51. package/dist/ci.d.ts +0 -27
  52. package/dist/ci.d.ts.map +0 -1
  53. package/dist/keys.d.ts +0 -13
  54. package/dist/keys.d.ts.map +0 -1
  55. package/dist/scripts.d.ts +0 -48
  56. package/dist/scripts.d.ts.map +0 -1
  57. package/dist/setup.d.ts +0 -32
  58. package/dist/setup.d.ts.map +0 -1
  59. package/src/access.ts +0 -107
  60. package/src/check.ts +0 -257
  61. package/src/ci.ts +0 -88
  62. package/src/keys.ts +0 -57
  63. package/src/scripts.ts +0 -703
  64. package/src/setup.ts +0 -203
@@ -0,0 +1,117 @@
1
+ /// <reference path="../sh.d.ts" />
2
+ import { resolve4, resolve6 } from "node:dns/promises"
3
+ import { isIP } from "node:net"
4
+ import type { DeployCheck, DeployRelease } from "@sixb/core/deploy"
5
+ import { shellQuote } from "../shell"
6
+ import type { RemoteShell } from "../transport"
7
+ import { type JudgeInput, judge, parseFacts } from "./judge"
8
+ import probe from "./probe.sh" with { type: "text" }
9
+
10
+ export { HOST_REQUIREMENTS } from "./judge"
11
+
12
+ /** Runs the read-only probe as the deploy account, and judges what it reports. */
13
+ export async function checkHost(
14
+ shell: RemoteShell,
15
+ input: JudgeInput & { readonly projectPath: string }
16
+ ): Promise<DeployCheck[]> {
17
+ const lines: string[] = []
18
+ await shell.run(renderProbe(input), {
19
+ onLine: (line, stream) => {
20
+ if (stream === "stdout") lines.push(line)
21
+ },
22
+ })
23
+ return judge(parseFacts(lines), input)
24
+ }
25
+
26
+ /** The probe, with what it needs to know set above it. */
27
+ export function renderProbe(input: {
28
+ readonly name: string
29
+ readonly projectPath: string
30
+ readonly ports: readonly number[]
31
+ readonly chromium: boolean
32
+ }): string {
33
+ return [
34
+ `SIXB_NAME=${shellQuote(input.name)}`,
35
+ `SIXB_PROJECT=${shellQuote(input.projectPath)}`,
36
+ `SIXB_PORTS=${shellQuote(input.ports.join(" "))}`,
37
+ `SIXB_CHROMIUM=${input.chromium ? 1 : 0}`,
38
+ probe,
39
+ ].join("\n")
40
+ }
41
+
42
+ /**
43
+ * Where each HTTP domain resolves, against the server's address. Reached over a tailnet or a
44
+ * private network, the server's public address is unknown here, so the check only says where
45
+ * each domain points.
46
+ */
47
+ export async function checkDns(release: DeployRelease, hostname: string): Promise<DeployCheck[]> {
48
+ const server = isIP(hostname) ? [hostname] : await lookup(hostname)
49
+ const comparable = server.length > 0 && server.every((address) => !isPrivateAddress(address))
50
+ return Promise.all(
51
+ release.services.flatMap((service) => {
52
+ const domain = service.http?.domain
53
+ if (!domain) return []
54
+ return [
55
+ (async (): Promise<DeployCheck> => {
56
+ const found = await lookup(domain)
57
+ const id = `dns.${service.name}`
58
+ const label = `${service.name} DNS`
59
+ const detail = `${domain} → ${found.join(", ")}`
60
+ if (found.length === 0) {
61
+ return {
62
+ id,
63
+ label,
64
+ status: "warning",
65
+ detail: `${domain} does not resolve`,
66
+ remedy: `Point ${domain} at the server. Until then, Caddy cannot get it a certificate.`,
67
+ }
68
+ }
69
+ if (!comparable || found.some((address) => server.includes(address))) {
70
+ return { id, label, status: "ok", detail }
71
+ }
72
+ const target = server[0] ?? hostname
73
+ return {
74
+ id,
75
+ label,
76
+ status: "warning",
77
+ detail,
78
+ remedy: `Point ${domain} at ${target} with an ${isIP(target) === 6 ? "AAAA" : "A"} record. Until then, Caddy cannot get it a certificate.`,
79
+ }
80
+ })(),
81
+ ]
82
+ })
83
+ )
84
+ }
85
+
86
+ /** Private, carrier-grade NAT (Tailscale's 100.64.0.0/10) and unique-local addresses. */
87
+ export function isPrivateAddress(address: string): boolean {
88
+ if (isIP(address) === 6) return /^f[cd]/i.test(address) || address === "::1"
89
+ const [a = 0, b = 0] = address.split(".").map(Number)
90
+ return (
91
+ a === 10 ||
92
+ a === 127 ||
93
+ (a === 172 && b >= 16 && b <= 31) ||
94
+ (a === 192 && b === 168) ||
95
+ (a === 100 && b >= 64 && b <= 127)
96
+ )
97
+ }
98
+
99
+ async function lookup(hostname: string): Promise<string[]> {
100
+ const [v4, v6] = await Promise.all([settle(resolve4(hostname)), settle(resolve6(hostname))])
101
+ return [...v4, ...v6]
102
+ }
103
+
104
+ /** A lookup's answers, or none once it fails or takes over four seconds. */
105
+ async function settle(query: Promise<string[]>): Promise<string[]> {
106
+ let timer: ReturnType<typeof setTimeout> | undefined
107
+ try {
108
+ return await Promise.race([
109
+ query.catch(() => []),
110
+ new Promise<string[]>((resolveTimeout) => {
111
+ timer = setTimeout(() => resolveTimeout([]), 4_000)
112
+ }),
113
+ ])
114
+ } finally {
115
+ clearTimeout(timer)
116
+ }
117
+ }
@@ -0,0 +1,299 @@
1
+ import type { DeployCheck } from "@sixb/core/deploy"
2
+
3
+ /** Where every remedy points: the list of what a server provides. */
4
+ export const HOST_REQUIREMENTS = "https://docs.sixb.ai/deployment/host-requirements"
5
+ const ENV_TOKEN_DOCS = "https://docs.sixb.ai/deployment/environment-variables#deploy"
6
+
7
+ /** What the probe reported, by key. */
8
+ export type HostFacts = ReadonlyMap<string, string>
9
+
10
+ export interface JudgeInput {
11
+ /** The deployment's `name`: its folder in the account's home. */
12
+ readonly name: string
13
+ /** The loopback ports the deployment's slots listen on. */
14
+ readonly ports: readonly number[]
15
+ /**
16
+ * Where the server's settings come from: the project's source, read with the env token or with
17
+ * the server's own identity, or the `.env` kept on the server.
18
+ */
19
+ readonly settings: "env-token" | "identity" | "env-file"
20
+ /** The project prints with Chromium, so the server needs Chromium's libraries. */
21
+ readonly chromium: boolean
22
+ }
23
+
24
+ /** The Debian package each tool the deploy uses comes in. */
25
+ const TOOL_PACKAGES: Readonly<Record<string, string>> = {
26
+ curl: "curl",
27
+ unzip: "unzip",
28
+ tar: "tar",
29
+ flock: "util-linux",
30
+ setsid: "util-linux",
31
+ sha256sum: "coreutils",
32
+ stat: "coreutils",
33
+ ss: "iproute2",
34
+ }
35
+
36
+ /** Caddy from its own apt repository, as its documentation installs it. */
37
+ const INSTALL_CADDY = [
38
+ "curl -fsSL https://dl.cloudsmith.io/public/caddy/stable/gpg.key | gpg --dearmor --yes -o /usr/share/keyrings/caddy-stable-archive-keyring.gpg",
39
+ "curl -fsSL https://dl.cloudsmith.io/public/caddy/stable/debian.deb.txt > /etc/apt/sources.list.d/caddy-stable.list",
40
+ "apt-get update && apt-get install -y caddy",
41
+ ]
42
+
43
+ const MIN_CADDY = [2, 7] as const
44
+ const MIN_FREE_KB = 2 * 1024 * 1024
45
+ const ARCHITECTURES = new Set(["x86_64", "amd64", "aarch64", "arm64"])
46
+
47
+ /** Reads the probe's `fact<TAB>key<TAB>value` lines. */
48
+ export function parseFacts(lines: readonly string[]): HostFacts {
49
+ const facts = new Map<string, string>()
50
+ for (const line of lines) {
51
+ const [marker, key, ...value] = line.split("\t")
52
+ if (marker === "fact" && key) facts.set(key, value.join("\t").trim())
53
+ }
54
+ return facts
55
+ }
56
+
57
+ /** What the facts mean for a deploy, one check per host requirement. */
58
+ export function judge(facts: HostFacts, input: JudgeInput): DeployCheck[] {
59
+ // The fixes name the account and its home as the server reports them, and are pasted into a root
60
+ // shell: anything that would need quoting gets no commands, only the remedy.
61
+ const user = pasteable(facts.get("account.name"))
62
+ const home = pasteable(facts.get("account.home"))
63
+ const root = home && pasteable(input.name) ? `${home}/${input.name}` : null
64
+ return [
65
+ server(facts),
66
+ account(facts),
67
+ deployments(facts),
68
+ linger(facts, user),
69
+ caddy(facts, user),
70
+ settings(facts, input.settings, root),
71
+ ports(facts, input.ports),
72
+ ...(input.chromium ? [chromium(facts)] : []),
73
+ disk(facts),
74
+ ]
75
+ }
76
+
77
+ function server(facts: HostFacts): DeployCheck {
78
+ const name = facts.get("os.name") || "an unknown system"
79
+ const family = `${facts.get("os.id") ?? ""} ${facts.get("os.like") ?? ""}`
80
+ const arch = facts.get("os.arch") ?? ""
81
+ const missing = facts.get("os.missing") ?? ""
82
+ const problems = [
83
+ /\b(debian|ubuntu)\b/.test(family) ? "" : `${name} is not Ubuntu or Debian`,
84
+ facts.get("os.systemd") === "yes" ? "" : "systemd is missing",
85
+ ARCHITECTURES.has(arch) ? "" : `Bun has no build for ${arch || "this CPU"}`,
86
+ missing ? `missing ${missing.split(" ").join(", ")}` : "",
87
+ ].filter(Boolean)
88
+ const packages = [...new Set(missing.split(" ").flatMap((tool) => TOOL_PACKAGES[tool] ?? []))]
89
+ return problems.length === 0
90
+ ? ok("server", "Server", `${name}, ${arch}`)
91
+ : fail("server", "Server", problems.join("; "), {
92
+ commands: packages.length > 0 ? [`apt-get install -y ${packages.join(" ")}`] : [],
93
+ })
94
+ }
95
+
96
+ /** `root` is refused in `sixb.deploy.ts`, and again here, where SSH config could still map to it. */
97
+ function account(facts: HostFacts): DeployCheck {
98
+ const name = facts.get("account.name") || "an unknown account"
99
+ return facts.get("account.uid") === "0"
100
+ ? fail("account", "Account", `${name} is root: deploy as an account without root access`)
101
+ : ok("account", "Account", name)
102
+ }
103
+
104
+ function deployments(facts: HostFacts): DeployCheck {
105
+ const others = (facts.get("deployments.others") ?? "").split(" ").filter(Boolean)
106
+ return others.length === 0
107
+ ? ok("deployments", "Deployments", "none other on this server")
108
+ : fail(
109
+ "deployments",
110
+ "Deployments",
111
+ `this server already runs ${others.join(", ")}: a server runs one deployment`
112
+ )
113
+ }
114
+
115
+ function linger(facts: HostFacts, user: string | null): DeployCheck {
116
+ return facts.get("linger") === "yes"
117
+ ? ok("linger", "Services at boot", "lingering is on")
118
+ : fail("linger", "Services at boot", "lingering is off: the services would stop at logout", {
119
+ commands: user ? [`loginctl enable-linger ${user}`] : [],
120
+ })
121
+ }
122
+
123
+ function caddy(facts: HostFacts, user: string | null): DeployCheck {
124
+ const version = facts.get("caddy.version") ?? ""
125
+ const routes = facts.get("caddy.routes") ?? "the routes folder"
126
+ const problems = [
127
+ !version
128
+ ? "Caddy is not installed"
129
+ : atLeast(version, MIN_CADDY)
130
+ ? ""
131
+ : `Caddy ${version} is older than ${MIN_CADDY.join(".")}`,
132
+ facts.get("caddy.caddyfile-imports") === "yes"
133
+ ? ""
134
+ : "/etc/caddy/Caddyfile does not import /etc/caddy/sixb.d/*.caddy",
135
+ facts.get("caddy.routes-imported") === "yes" ? "" : `nothing imports ${routes}`,
136
+ facts.get("caddy.routes-writable") === "yes" ? "" : `${routes} is missing or not writable`,
137
+ facts.get("caddy.reload") === "yes" ? "" : "sudo systemctl reload caddy is not allowed",
138
+ ].filter(Boolean)
139
+ if (problems.length === 0) return ok("caddy", "Caddy", `${version}, routes in ${routes}`)
140
+
141
+ const folder = `/etc/caddy/sixb.d/${user}`
142
+ const configure = !user
143
+ ? []
144
+ : [
145
+ ...(facts.get("caddy.caddyfile-imports") === "yes"
146
+ ? []
147
+ : [
148
+ // Safe to paste twice: a second import of the same files is an error Caddy won't reload.
149
+ "grep -qxF 'import /etc/caddy/sixb.d/*.caddy' /etc/caddy/Caddyfile || echo 'import /etc/caddy/sixb.d/*.caddy' >> /etc/caddy/Caddyfile",
150
+ ]),
151
+ ...(facts.get("caddy.routes-imported") === "yes"
152
+ ? []
153
+ : [
154
+ "install -d -m 0755 /etc/caddy/sixb.d",
155
+ `echo 'import ${folder}/*.caddy' > ${folder}.caddy`,
156
+ ]),
157
+ ...(facts.get("caddy.routes-writable") === "yes"
158
+ ? []
159
+ : [`install -d -m 0755 -o ${user} -g ${user} ${folder}`]),
160
+ ...(facts.get("caddy.reload") === "yes"
161
+ ? []
162
+ : [
163
+ `echo '${user} ALL=(root) NOPASSWD: /usr/bin/systemctl reload caddy' > /etc/sudoers.d/${user}`,
164
+ `chmod 440 /etc/sudoers.d/${user}`,
165
+ ]),
166
+ ]
167
+ const install = !version
168
+ ? INSTALL_CADDY
169
+ : atLeast(version, MIN_CADDY)
170
+ ? []
171
+ : ["apt-get update && apt-get install -y --only-upgrade caddy"]
172
+ return fail("caddy", "Caddy", problems.join("; "), {
173
+ commands: [
174
+ ...install,
175
+ ...configure,
176
+ ...(configure.length > 0 ? ["systemctl reload caddy"] : []),
177
+ ],
178
+ })
179
+ }
180
+
181
+ function settings(
182
+ facts: HostFacts,
183
+ source: JudgeInput["settings"],
184
+ root: string | null
185
+ ): DeployCheck {
186
+ const folder = root ?? "~/<name>"
187
+ if (source === "identity") {
188
+ return ok("settings", "Settings", "the source reads with the server's own identity")
189
+ }
190
+ if (source === "env-token") {
191
+ const token = fileFact(facts.get("file.env-token"))
192
+ if (!token || token.size === 0) {
193
+ return fail("settings", "Settings", "no env token: the deploy can't pull .env", {
194
+ remedy: `Put a read-only token for the project's prod values at ${folder}/env-token, mode 600. See ${ENV_TOKEN_DOCS}.`,
195
+ })
196
+ }
197
+ return privateMode(token.mode)
198
+ ? ok("settings", "Settings", "env token in place")
199
+ : fail("settings", "Settings", `the env token is readable by others (mode ${token.mode})`, {
200
+ commands: root ? [`chmod 600 ${root}/env-token`] : [],
201
+ })
202
+ }
203
+ // A deployment from before releases keeps its settings in its code folder.
204
+ const shared = fileFact(facts.get("file.shared-env"))
205
+ const env = shared ?? fileFact(facts.get("file.legacy-env"))
206
+ if (!env) {
207
+ return fail("settings", "Settings", "no .env on the server", {
208
+ remedy: `Create ${folder}/shared/.env, mode 600, with the project's settings.`,
209
+ })
210
+ }
211
+ return privateMode(env.mode)
212
+ ? ok("settings", "Settings", ".env in place")
213
+ : fail("settings", "Settings", `.env is readable by others (mode ${env.mode})`, {
214
+ commands: shared && root ? [`chmod 600 ${root}/shared/.env`] : [],
215
+ })
216
+ }
217
+
218
+ function ports(facts: HostFacts, wanted: readonly number[]): DeployCheck {
219
+ const taken = wanted.filter((port) => facts.get(`port.${port}`) === "other")
220
+ return taken.length === 0
221
+ ? ok("ports", "Ports", wanted.join(", "))
222
+ : fail(
223
+ "ports",
224
+ "Ports",
225
+ `${taken.join(", ")} ${taken.length === 1 ? "is" : "are"} held by another program: a server runs one deployment`
226
+ )
227
+ }
228
+
229
+ function chromium(facts: HostFacts): DeployCheck {
230
+ const missing = facts.get("chromium.missing") ?? ""
231
+ const checked = facts.get("chromium.checked") ?? "system libraries"
232
+ return missing
233
+ ? fail("chromium", "Chromium", `missing ${missing.split(" ").join(", ")} (${checked})`, {
234
+ remedy: "Install them as root, such as with `npx playwright install-deps chromium`.",
235
+ })
236
+ : ok("chromium", "Chromium", "its libraries are installed")
237
+ }
238
+
239
+ function disk(facts: HostFacts): DeployCheck {
240
+ const freeKb = Number(facts.get("disk.free-kb") ?? 0)
241
+ return freeKb >= MIN_FREE_KB
242
+ ? ok("disk", "Disk space", `${Math.floor(freeKb / 1048576)} GB free`)
243
+ : {
244
+ id: "disk",
245
+ label: "Disk space",
246
+ status: "warning",
247
+ detail: `${Math.floor(freeKb / 1024)} MB free`,
248
+ remedy: "A release needs about 1 GB while it builds.",
249
+ }
250
+ }
251
+
252
+ function ok(id: string, label: string, detail: string): DeployCheck {
253
+ return { id, label, status: "ok", detail }
254
+ }
255
+
256
+ /** A failed requirement, with what fixes it: commands to run as root, when there are some. */
257
+ function fail(
258
+ id: string,
259
+ label: string,
260
+ detail: string,
261
+ fix: { readonly remedy?: string; readonly commands?: readonly string[] } = {}
262
+ ): DeployCheck {
263
+ const commands = fix.commands ?? []
264
+ return {
265
+ id,
266
+ label,
267
+ status: "fail",
268
+ detail,
269
+ remedy: fix.remedy ?? `See ${HOST_REQUIREMENTS}.`,
270
+ ...(commands.length > 0 ? { commands } : {}),
271
+ }
272
+ }
273
+
274
+ /** A name or path that pastes into a shell as it is, or nothing when it would need quoting. */
275
+ function pasteable(value: string | undefined): string | null {
276
+ return value && /^[A-Za-z0-9._/-]+$/.test(value) ? value : null
277
+ }
278
+
279
+ /** Readable by its owner alone: no group or other bits, as 600 or 400. */
280
+ function privateMode(mode: string): boolean {
281
+ const bits = Number.parseInt(mode, 8)
282
+ return Number.isInteger(bits) && (bits & 0o077) === 0
283
+ }
284
+
285
+ /** `mode size` from the probe, or nothing when the file is missing. */
286
+ function fileFact(value: string | undefined): { mode: string; size: number } | null {
287
+ if (!value || value === "missing") return null
288
+ const [mode = "", size = "0"] = value.split(" ")
289
+ return { mode, size: Number(size) }
290
+ }
291
+
292
+ function atLeast(version: string, minimum: readonly number[]): boolean {
293
+ const parts = version.split(".").map((part) => Number.parseInt(part, 10) || 0)
294
+ for (const [index, wanted] of minimum.entries()) {
295
+ const part = parts[index] ?? 0
296
+ if (part !== wanted) return part > wanted
297
+ }
298
+ return true
299
+ }
@@ -0,0 +1,99 @@
1
+ # Reports what a deploy needs to know about the server, as the deploy account, changing nothing.
2
+ # Prints one `fact<TAB>key<TAB>value` line per fact; check/judge.ts decides what each means.
3
+ # The caller sets SIXB_NAME (the deployment), SIXB_PROJECT (the project's path in its repository),
4
+ # SIXB_PORTS (the slots' loopback ports) and SIXB_CHROMIUM (1 when the project prints with
5
+ # Chromium) above this script.
6
+
7
+ fact() { printf 'fact\t%s\t%s\n' "$1" "$(printf '%s' "$2" | tr '\t\n' ' ')"; }
8
+ yes_no() { if "$@" > /dev/null 2>&1; then echo yes; else echo no; fi; }
9
+
10
+ # Server
11
+ # In a subshell: os-release sets NAME, ID and others over this script's own variables.
12
+ if [ -r /etc/os-release ]; then
13
+ (
14
+ # shellcheck disable=SC1091
15
+ . /etc/os-release
16
+ fact os.id "${ID:-}"
17
+ fact os.like "${ID_LIKE:-}"
18
+ fact os.name "${PRETTY_NAME:-}"
19
+ )
20
+ fi
21
+ fact os.systemd "$(yes_no command -v systemctl)"
22
+ fact os.arch "$(uname -m)"
23
+ missing=""
24
+ for tool in curl unzip tar flock setsid sha256sum ss; do
25
+ command -v "$tool" > /dev/null || missing="$missing $tool"
26
+ done
27
+ # The deploy reads modes with GNU stat's `-c`.
28
+ stat -c %a "$HOME" > /dev/null 2>&1 || missing="$missing stat"
29
+ fact os.missing "${missing# }"
30
+ fact account.name "$(id -un)"
31
+ fact account.home "$HOME"
32
+ fact account.uid "$(id -u)"
33
+ fact linger "$(yes_no test -e "/var/lib/systemd/linger/$USER")"
34
+
35
+ # Caddy
36
+ fact caddy.version "$(caddy version 2> /dev/null | cut -d' ' -f1 | sed 's/^v//')"
37
+ fact caddy.reload "$(yes_no sudo -n -l /usr/bin/systemctl reload caddy)"
38
+ routes="/etc/caddy/sixb.d/$USER"
39
+ fact caddy.routes "$routes"
40
+ fact caddy.routes-writable "$(yes_no test -d "$routes" -a -w "$routes")"
41
+ fact caddy.routes-imported "$(yes_no grep -qsF "import $routes/*.caddy" "/etc/caddy/sixb.d/$USER.caddy")"
42
+ fact caddy.caddyfile-imports "$(yes_no grep -qsF 'import /etc/caddy/sixb.d/*.caddy' /etc/caddy/Caddyfile)"
43
+
44
+ # Deployments on this server, by their systemd units: a server runs one
45
+ others="$(
46
+ for unit in "$HOME"/.config/systemd/user/sixb-*.service; do
47
+ [ -e "$unit" ] || continue
48
+ deployment="${unit##*/sixb-}"
49
+ deployment="${deployment%.service}"
50
+ deployment="${deployment%@}"
51
+ [ "$deployment" = "$SIXB_NAME" ] || echo "$deployment"
52
+ done | sort -u | tr '\n' ' '
53
+ )"
54
+ fact deployments.others "${others% }"
55
+
56
+ # Settings: the token a deploy pulls .env with, or the .env kept on the server
57
+ file_fact() {
58
+ if [ -f "$2" ]; then fact "$1" "$(stat -c '%a %s' "$2")"; else fact "$1" missing; fi
59
+ }
60
+ root="$HOME/$SIXB_NAME"
61
+ file_fact file.env-token "$root/env-token"
62
+ file_fact file.shared-env "$root/shared/.env"
63
+ # A deployment from before releases kept its .env in its code folder, which the first deploy moves.
64
+ if [ ! -L "$root/code" ]; then
65
+ file_fact file.legacy-env "$root/code/$SIXB_PROJECT/.env"
66
+ fi
67
+
68
+ # Ports: free, held by this account, or held by something else
69
+ uid="$(id -u)"
70
+ for port in $SIXB_PORTS; do
71
+ listening="$(ss -ltnHe "sport = :$port" 2> /dev/null)"
72
+ if [ -z "$listening" ]; then
73
+ fact "port.$port" free
74
+ elif printf '%s' "$listening" | grep -q "uid:$uid\\b"; then
75
+ fact "port.$port" own
76
+ else
77
+ fact "port.$port" other
78
+ fi
79
+ done
80
+
81
+ fact disk.free-kb "$(df -Pk "$HOME" | awk 'NR == 2 { print $4 + 0 }')"
82
+
83
+ # Chromium: the libraries the downloaded browser can't find, or, before the first download, the
84
+ # few that a server without Chromium's dependencies lacks
85
+ if [ "${SIXB_CHROMIUM:-0}" = 1 ]; then
86
+ browser="$(find "$HOME/.cache/ms-playwright" -path '*/chrome-headless-shell-linux64/chrome-headless-shell' -type f 2> /dev/null | sort -V | tail -n 1)"
87
+ if [ -n "$browser" ]; then
88
+ fact chromium.checked "$browser"
89
+ fact chromium.missing "$(ldd "$browser" 2> /dev/null | awk '/not found/ { print $1 }' | tr '\n' ' ')"
90
+ else
91
+ fact chromium.checked "system libraries"
92
+ ldconfig="$(command -v ldconfig || echo /sbin/ldconfig)"
93
+ missing=""
94
+ for lib in libnss3.so libatk-1.0.so.0 libatk-bridge-2.0.so.0 libgbm.so.1 libxkbcommon.so.0 libasound.so.2; do
95
+ "$ldconfig" -p 2> /dev/null | grep -qF "$lib" || missing="$missing $lib"
96
+ done
97
+ fact chromium.missing "${missing# }"
98
+ fi
99
+ fi