@sixb/deploy-ssh 0.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (66) hide show
  1. package/LICENSE +21 -0
  2. package/README.md +74 -0
  3. package/dist/access.d.ts +11 -0
  4. package/dist/access.d.ts.map +1 -0
  5. package/dist/check.d.ts +28 -0
  6. package/dist/check.d.ts.map +1 -0
  7. package/dist/ci.d.ts +27 -0
  8. package/dist/ci.d.ts.map +1 -0
  9. package/dist/index.d.ts +3 -0
  10. package/dist/index.d.ts.map +1 -0
  11. package/dist/index.js +1415 -0
  12. package/dist/index.js.map +24 -0
  13. package/dist/keys.d.ts +13 -0
  14. package/dist/keys.d.ts.map +1 -0
  15. package/dist/layout.d.ts +38 -0
  16. package/dist/layout.d.ts.map +1 -0
  17. package/dist/operations.d.ts +10 -0
  18. package/dist/operations.d.ts.map +1 -0
  19. package/dist/render.d.ts +15 -0
  20. package/dist/render.d.ts.map +1 -0
  21. package/dist/scripts.d.ts +37 -0
  22. package/dist/scripts.d.ts.map +1 -0
  23. package/dist/server/cli.d.ts +3 -0
  24. package/dist/server/cli.d.ts.map +1 -0
  25. package/dist/server/logs.d.ts +25 -0
  26. package/dist/server/logs.d.ts.map +1 -0
  27. package/dist/server/manifest.d.ts +30 -0
  28. package/dist/server/manifest.d.ts.map +1 -0
  29. package/dist/server/state.d.ts +32 -0
  30. package/dist/server/state.d.ts.map +1 -0
  31. package/dist/server/supervisor.d.ts +16 -0
  32. package/dist/server/supervisor.d.ts.map +1 -0
  33. package/dist/setup.d.ts +25 -0
  34. package/dist/setup.d.ts.map +1 -0
  35. package/dist/shell.d.ts +5 -0
  36. package/dist/shell.d.ts.map +1 -0
  37. package/dist/ssh-config.d.ts +12 -0
  38. package/dist/ssh-config.d.ts.map +1 -0
  39. package/dist/ssh-target.d.ts +39 -0
  40. package/dist/ssh-target.d.ts.map +1 -0
  41. package/dist/transport.d.ts +45 -0
  42. package/dist/transport.d.ts.map +1 -0
  43. package/dist/tsconfig.json +5 -0
  44. package/dist/update-page.d.ts +6 -0
  45. package/dist/update-page.d.ts.map +1 -0
  46. package/package.json +61 -0
  47. package/src/access.ts +107 -0
  48. package/src/check.ts +215 -0
  49. package/src/ci.ts +88 -0
  50. package/src/index.ts +2 -0
  51. package/src/keys.ts +57 -0
  52. package/src/layout.ts +69 -0
  53. package/src/operations.ts +158 -0
  54. package/src/render.ts +129 -0
  55. package/src/scripts.ts +305 -0
  56. package/src/server/cli.ts +157 -0
  57. package/src/server/logs.ts +180 -0
  58. package/src/server/manifest.ts +112 -0
  59. package/src/server/state.ts +81 -0
  60. package/src/server/supervisor.ts +542 -0
  61. package/src/setup.ts +166 -0
  62. package/src/shell.ts +10 -0
  63. package/src/ssh-config.ts +35 -0
  64. package/src/ssh-target.ts +347 -0
  65. package/src/transport.ts +211 -0
  66. package/src/update-page.ts +44 -0
package/src/scripts.ts ADDED
@@ -0,0 +1,305 @@
1
+ import type { DeployCommand, DeployRecord, DeployRelease } from "@sixb/core/deploy"
2
+ import type { DeploymentLayout } from "./layout"
3
+ import { renderCaddySnippet, renderCtl, renderProcessManifest, renderUnit } from "./render"
4
+ import { shellQuote, writeFileCommand } from "./shell"
5
+
6
+ /** A line the deploy script prints around each step; the target turns it into progress. */
7
+ export const STEP_MARKER = "__SIXB_STEP__"
8
+
9
+ /** The server's home directory, and a fresh directory to upload the source into. */
10
+ export function renderPrepareScript(name: string, commit: string): string {
11
+ return [
12
+ `root="$HOME"/${shellQuote(name)}`,
13
+ 'mkdir -p "$root/deploy/incoming"',
14
+ `incoming="$(mktemp -d "$root/deploy/incoming/${commit.slice(0, 12)}.XXXXXX")"`,
15
+ // `mktemp` makes it 0700, and GNU tar passes the mode of the upload's `.` on to `code/`, which
16
+ // would lock out the admin, who reads the code through the deploy user's group.
17
+ 'chmod 755 "$incoming"',
18
+ `printf '%s\\n%s\\n' "$HOME" "$incoming"`,
19
+ ].join("\n")
20
+ }
21
+
22
+ /** Extracts one uploaded archive from standard input. */
23
+ export function renderReceiveScript(incoming: string, path: string): string {
24
+ const directory = path === "." ? incoming : `${incoming}/${path}`
25
+ return `mkdir -p ${shellQuote(directory)} && tar -x -f - -C ${shellQuote(directory)}`
26
+ }
27
+
28
+ export interface DeployScriptInput {
29
+ readonly release: DeployRelease
30
+ readonly layout: DeploymentLayout
31
+ readonly incoming: string
32
+ readonly record: DeployRecord
33
+ }
34
+
35
+ export interface DeployScript {
36
+ readonly labels: readonly string[]
37
+ readonly script: string
38
+ }
39
+
40
+ interface Step {
41
+ readonly label: string
42
+ readonly body: string
43
+ }
44
+
45
+ /**
46
+ * Everything the server does once the source is uploaded, as one script that holds the
47
+ * deployment's lock throughout. It runs with standard input closed: bash reads the whole `main`
48
+ * function before running any of it, so no command can swallow the rest of the script.
49
+ */
50
+ export function renderDeployScript(input: DeployScriptInput): DeployScript {
51
+ const { release, layout } = input
52
+ const api = release.services.find((service) => service.http?.readinessPath)
53
+ const steps: Step[] = [
54
+ { label: "Apply files", body: renderApplyFiles(layout, input.incoming) },
55
+ { label: `Install Bun ${release.bunVersion}`, body: renderInstallBun(release.bunVersion) },
56
+ { label: "Install dependencies", body: renderInstallDependencies() },
57
+ ...release.steps.build.map((command) => runStep(command)),
58
+ // Everything that can be rejected happens while the previous release still serves: services
59
+ // are down only from here to "Start services".
60
+ { label: "Write services", body: renderWriteServices(release, layout) },
61
+ { label: "Update routes", body: renderUpdateRoutes(release, layout) },
62
+ { label: "Stop services", body: renderStopServices() },
63
+ ...release.steps.beforeStart.map((command) => runStep(command)),
64
+ { label: "Start services", body: renderStartServices() },
65
+ {
66
+ label: "Check services",
67
+ body: renderCheckServices(
68
+ api?.http ? `http://${api.http.host}:${api.http.port}${api.http.readinessPath}` : null
69
+ ),
70
+ },
71
+ {
72
+ label: "Record release",
73
+ body: writeFileCommand(layout.releaseRecord, `${JSON.stringify(input.record)}\n`),
74
+ },
75
+ ]
76
+
77
+ const script = [
78
+ "set -euo pipefail",
79
+ "main() {",
80
+ renderEnvironment(release, layout),
81
+ renderTakeLock(layout),
82
+ ...steps.flatMap((step, index) => [
83
+ `printf '\\n%s\\t%s\\t%s\\n' ${STEP_MARKER} ${index} start`,
84
+ step.body,
85
+ `printf '%s\\t%s\\t%s\\n' ${STEP_MARKER} ${index} done`,
86
+ ]),
87
+ "}",
88
+ "main </dev/null",
89
+ "",
90
+ ].join("\n")
91
+
92
+ return { labels: steps.map((step) => step.label), script }
93
+ }
94
+
95
+ function renderEnvironment(release: DeployRelease, layout: DeploymentLayout): string {
96
+ const variables: Record<string, string> = {
97
+ ROOT: layout.root,
98
+ CODE: layout.code,
99
+ PROJECT: layout.project,
100
+ DEPLOY: layout.deployDir,
101
+ BUN: layout.bun,
102
+ BUN_DIR: layout.bunDir,
103
+ BUN_VERSION: release.bunVersion,
104
+ CTL: layout.ctl,
105
+ UNIT: layout.unitName,
106
+ UNIT_PATH: layout.unit,
107
+ CADDY_DIR: layout.caddyDir,
108
+ CADDY_SNIPPET: layout.caddySnippet,
109
+ }
110
+ return [
111
+ ...Object.entries(variables).map(([name, value]) => `${name}=${shellQuote(value)}`),
112
+ // The steps run with the release's environment; the project's `.env` fills in the rest.
113
+ ...Object.entries(release.env).map(([name, value]) => `export ${name}=${shellQuote(value)}`),
114
+ 'export PATH="$BUN_DIR:$PATH"',
115
+ // `systemctl --user` finds the user's systemd through this; SSH sessions usually set it.
116
+ 'printenv XDG_RUNTIME_DIR > /dev/null || export XDG_RUNTIME_DIR="/run/user/$(id -u)"',
117
+ ].join("\n")
118
+ }
119
+
120
+ function renderTakeLock(layout: DeploymentLayout): string {
121
+ return [
122
+ `exec 9>${shellQuote(layout.lock)}`,
123
+ "if ! flock -n 9; then",
124
+ ' echo "Waiting for another deploy of this project to finish…"',
125
+ "fi",
126
+ "if ! flock -w 600 9; then",
127
+ ' echo "Another deploy of this project has held its lock for 10 minutes." >&2',
128
+ " exit 1",
129
+ "fi",
130
+ ].join("\n")
131
+ }
132
+
133
+ /**
134
+ * Makes `code/` match the commit the way `git reset --hard` would: committed files replace what is
135
+ * there, files the previous deploy brought and this one does not are removed, and everything else
136
+ * — `.env`, `.sixb/`, `node_modules/` — stays. A committed `.env` is never copied over the
137
+ * server's.
138
+ */
139
+ export function renderApplyFiles(layout: DeploymentLayout, incoming: string): string {
140
+ return [
141
+ `incoming=${shellQuote(incoming)}`,
142
+ `files=${shellQuote(layout.fileList)}`,
143
+ `code=${shellQuote(layout.code)}`,
144
+ 'listed="$incoming.files"',
145
+ '(cd "$incoming" && find . \\( -type f -o -type l \\) ! -name .env | LC_ALL=C sort) > "$listed"',
146
+ 'mkdir -p "$code"',
147
+ 'if [ -f "$files" ]; then',
148
+ ' LC_ALL=C comm -23 "$files" "$listed" | while IFS= read -r path; do',
149
+ ' rm -f "$code/$path"',
150
+ ' dir="$(dirname "$code/$path")"',
151
+ ' while [ "$dir" != "$code" ] && [ "$dir" != "$code/." ] && rmdir "$dir" 2>/dev/null; do',
152
+ ' dir="$(dirname "$dir")"',
153
+ " done",
154
+ " done",
155
+ "fi",
156
+ `(cd "$incoming" && tar -c -f - --exclude=.env .) | (cd "$code" && tar -x -f -)`,
157
+ 'mv "$listed" "$files"',
158
+ 'rm -rf "$incoming"',
159
+ // Uploads a deploy abandoned before it took the lock.
160
+ 'find "$(dirname "$incoming")" -mindepth 1 -maxdepth 1 -mmin +1440 -exec rm -rf {} + 2>/dev/null || true',
161
+ ].join("\n")
162
+ }
163
+
164
+ /** Downloads the pinned Bun once per user and version, checking it against the release's sums. */
165
+ function renderInstallBun(version: string): string {
166
+ const release = `https://github.com/oven-sh/bun/releases/download/bun-v${version}`
167
+ return [
168
+ 'if [ ! -x "$BUN" ]; then',
169
+ ' case "$(uname -m)" in',
170
+ " x86_64 | amd64) arch=x64 ;;",
171
+ " aarch64 | arm64) arch=aarch64 ;;",
172
+ ' *) echo "Bun has no Linux build for $(uname -m)." >&2; exit 1 ;;',
173
+ " esac",
174
+ // Bun's default x64 build needs AVX2; older CPUs take the baseline build.
175
+ ' if [ "$arch" = x64 ] && ! grep -q avx2 /proc/cpuinfo; then arch=x64-baseline; fi',
176
+ ' asset="bun-linux-$arch"',
177
+ ' download="$(mktemp -d)"',
178
+ ` curl -fsSL --retry 3 -o "$download/$asset.zip" ${shellQuote(release)}"/$asset.zip"`,
179
+ ` curl -fsSL --retry 3 -o "$download/SHASUMS256.txt" ${shellQuote(release)}/SHASUMS256.txt`,
180
+ ' (cd "$download" && grep " $asset.zip\\$" SHASUMS256.txt | sha256sum -c --quiet -)',
181
+ ' unzip -q "$download/$asset.zip" -d "$download"',
182
+ ' mkdir -p "$BUN_DIR"',
183
+ ' mv "$download/$asset/bun" "$BUN"',
184
+ ' rm -rf "$download"',
185
+ "fi",
186
+ '"$BUN" --version',
187
+ ].join("\n")
188
+ }
189
+
190
+ function renderInstallDependencies(): string {
191
+ return [
192
+ 'cd "$PROJECT"',
193
+ '"$BUN" install --frozen-lockfile',
194
+ "for bin in sixb sixb-deploy-ssh; do",
195
+ ' if [ ! -e "$PROJECT/node_modules/.bin/$bin" ]; then',
196
+ ' package="@sixb/cli"; [ "$bin" = sixb ] || package="@sixb/deploy-ssh"',
197
+ ' echo "$package is not installed. Add it to the project dependencies." >&2',
198
+ " exit 1",
199
+ " fi",
200
+ "done",
201
+ ].join("\n")
202
+ }
203
+
204
+ function runStep(command: DeployCommand): Step {
205
+ const program = command.program === "sixb" ? '"$PROJECT/node_modules/.bin/sixb"' : ""
206
+ return {
207
+ label: [command.program, ...command.args].join(" "),
208
+ body: [
209
+ 'cd "$PROJECT"',
210
+ `"$BUN" ${program} ${command.args.map(shellQuote).join(" ")}`.replace(/ {2,}/g, " "),
211
+ ].join("\n"),
212
+ }
213
+ }
214
+
215
+ function renderStopServices(): string {
216
+ return [
217
+ 'if systemctl --user is-active --quiet "$UNIT"; then',
218
+ ' systemctl --user stop "$UNIT"',
219
+ "else",
220
+ " echo 'Services were not running.'",
221
+ "fi",
222
+ ].join("\n")
223
+ }
224
+
225
+ function renderWriteServices(release: DeployRelease, layout: DeploymentLayout): string {
226
+ return [
227
+ writeFileCommand(
228
+ layout.manifest,
229
+ `${JSON.stringify(renderProcessManifest(release, layout), null, 2)}\n`
230
+ ),
231
+ writeFileCommand(layout.ctl, renderCtl(layout)),
232
+ 'chmod 755 "$CTL"',
233
+ 'mkdir -p "$(dirname "$UNIT_PATH")"',
234
+ writeFileCommand(`${layout.unit}.new`, renderUnit(release, layout)),
235
+ 'if cmp -s "$UNIT_PATH.new" "$UNIT_PATH"; then',
236
+ ' rm -f "$UNIT_PATH.new"',
237
+ "else",
238
+ ' mv "$UNIT_PATH.new" "$UNIT_PATH"',
239
+ " systemctl --user daemon-reload",
240
+ ' systemctl --user enable "$UNIT"',
241
+ "fi",
242
+ ].join("\n")
243
+ }
244
+
245
+ /**
246
+ * Installs the deployment's Caddy routes when they changed. Caddy has to accept the whole
247
+ * configuration before it reloads; otherwise the previous routes come back and the deploy stops.
248
+ */
249
+ function renderUpdateRoutes(release: DeployRelease, layout: DeploymentLayout): string {
250
+ return [
251
+ `new=${shellQuote(`${layout.deployDir}/Caddyfile`)}`,
252
+ writeFileCommand(`${layout.deployDir}/Caddyfile`, renderCaddySnippet(release)),
253
+ 'if cmp -s "$new" "$CADDY_SNIPPET"; then',
254
+ " echo 'Routes unchanged.'",
255
+ "else",
256
+ ' if [ ! -w "$CADDY_DIR" ]; then',
257
+ ' echo "$CADDY_DIR is missing or not writable, so this server is not set up for sixb deploy." >&2',
258
+ " exit 1",
259
+ " fi",
260
+ ' previous="$DEPLOY/Caddyfile.previous"',
261
+ ' rm -f "$previous"',
262
+ ' if [ -f "$CADDY_SNIPPET" ]; then cp "$CADDY_SNIPPET" "$previous"; fi',
263
+ ' cp "$new" "$CADDY_SNIPPET"',
264
+ ' if ! caddy validate --config /etc/caddy/Caddyfile --adapter caddyfile > "$DEPLOY/caddy-validate.log" 2>&1; then',
265
+ ' if [ -f "$previous" ]; then cp "$previous" "$CADDY_SNIPPET"; else rm -f "$CADDY_SNIPPET"; fi',
266
+ ' tail -n 20 "$DEPLOY/caddy-validate.log" >&2',
267
+ " echo 'Caddy rejected the routes; the previous ones are back in place.' >&2",
268
+ " exit 1",
269
+ " fi",
270
+ " sudo -n /usr/bin/systemctl reload caddy",
271
+ "fi",
272
+ ].join("\n")
273
+ }
274
+
275
+ function renderStartServices(): string {
276
+ return ['systemctl --user restart "$UNIT"', 'systemctl --user is-active --quiet "$UNIT"'].join(
277
+ "\n"
278
+ )
279
+ }
280
+
281
+ /**
282
+ * Done means healthy, not just started: the API answers its readiness route, and no process has
283
+ * exited in the seconds after it did.
284
+ */
285
+ export function renderCheckServices(readinessUrl: string | null): string {
286
+ const waitForApi = readinessUrl
287
+ ? [
288
+ `ready_url=${shellQuote(readinessUrl)}`,
289
+ "ready=0",
290
+ "for attempt in $(seq 1 90); do",
291
+ ` if "$BUN" -e 'const r = await fetch(process.argv[1]).catch(() => null); process.exit(r?.ok ? 0 : 1)' "$ready_url" 2>/dev/null; then`,
292
+ " ready=1",
293
+ " break",
294
+ " fi",
295
+ " sleep 1",
296
+ "done",
297
+ 'if [ "$ready" != 1 ]; then',
298
+ ' echo "The API did not answer $ready_url within 90 seconds. Its latest log lines:" >&2',
299
+ ' "$CTL" logs api --tail 30 >&2 || true',
300
+ " exit 1",
301
+ "fi",
302
+ ]
303
+ : []
304
+ return [...waitForApi, "sleep 5", '"$CTL" status --check'].join("\n")
305
+ }
@@ -0,0 +1,157 @@
1
+ #!/usr/bin/env bun
2
+ import { readFile } from "node:fs/promises"
3
+ import type { DeployRecord, DeployStatus } from "@sixb/core/deploy"
4
+ import { followLogFiles, type LogLine, readRecentLogLines, renderProcessLogLine } from "./logs"
5
+ import { loadProcessManifest, type ProcessManifest, statePath } from "./manifest"
6
+ import { isMissing, isPidRunning, readProcessState } from "./state"
7
+ import { runSupervisor, type SupervisorCommand, sendSupervisorCommand } from "./supervisor"
8
+
9
+ /**
10
+ * The server half of `@sixb/deploy-ssh`. Each deployment runs it through its `deploy/ctl`
11
+ * script, which pins the Bun version and manifest. `sixb deploy` reaches it over SSH.
12
+ *
13
+ * supervise run the deployment's processes (the systemd unit's command)
14
+ * status [--json] [--check] --check exits 1 unless every process is running
15
+ * logs [service] [--tail n] [--follow]
16
+ * restart|start|stop [service]
17
+ */
18
+ const argv = process.argv.slice(2)
19
+
20
+ try {
21
+ await main()
22
+ } catch (error) {
23
+ console.error(error instanceof Error ? error.message : String(error))
24
+ process.exit(1)
25
+ }
26
+
27
+ async function main(): Promise<void> {
28
+ const manifestPath = option("--manifest")
29
+ if (!manifestPath) throw new Error("[SshTarget] --manifest is required.")
30
+ const [command, service] = positionals()
31
+
32
+ switch (command) {
33
+ case "supervise":
34
+ await runSupervisor(manifestPath)
35
+ // Its signal listeners would keep Bun running after every process has stopped.
36
+ return process.exit(0)
37
+ case "status":
38
+ return status(await loadProcessManifest(manifestPath))
39
+ case "logs":
40
+ return logs(await loadProcessManifest(manifestPath), service)
41
+ case "restart":
42
+ case "start":
43
+ case "stop":
44
+ console.log(
45
+ await sendSupervisorCommand(
46
+ await loadProcessManifest(manifestPath),
47
+ command satisfies SupervisorCommand,
48
+ service
49
+ )
50
+ )
51
+ return
52
+ default:
53
+ throw new Error(`[SshTarget] Unknown command '${command ?? ""}'.`)
54
+ }
55
+ }
56
+
57
+ async function status(manifest: ProcessManifest): Promise<void> {
58
+ const state = await readProcessState(statePath(manifest))
59
+ const running = state !== null && isPidRunning(state.supervisorPid)
60
+ const report: DeployStatus = {
61
+ release: await readRelease(manifest.releasePath),
62
+ running,
63
+ processes: running
64
+ ? state.instances.map((instance) => ({
65
+ service: instance.service,
66
+ instance: instance.instance,
67
+ status: instance.status,
68
+ restarts: instance.restarts,
69
+ ...(instance.pid === undefined ? {} : { pid: instance.pid }),
70
+ ...(instance.startedAt === undefined ? {} : { startedAt: instance.startedAt }),
71
+ ...(instance.cpuPercent === undefined ? {} : { cpuPercent: instance.cpuPercent }),
72
+ ...(instance.memoryBytes === undefined ? {} : { memoryBytes: instance.memoryBytes }),
73
+ ...(instance.lastError === undefined ? {} : { lastError: instance.lastError }),
74
+ }))
75
+ : [],
76
+ }
77
+
78
+ if (flag("--check")) {
79
+ // Right after a deploy, any restart means a process already exited once.
80
+ const down = report.processes.filter(
81
+ (process) => process.status !== "running" || process.restarts > 0
82
+ )
83
+ if (!running) console.error("The supervisor is not running.")
84
+ for (const process of down) {
85
+ const label =
86
+ process.instance === 0 ? process.service : `${process.service}#${process.instance}`
87
+ const state = process.status === "running" ? `restarted ${process.restarts}x` : process.status
88
+ console.error(`${label} ${state}${process.lastError ? `: ${process.lastError}` : ""}`)
89
+ }
90
+ if (!running || down.length > 0) process.exit(1)
91
+ return
92
+ }
93
+ console.log(JSON.stringify(report))
94
+ }
95
+
96
+ async function logs(manifest: ProcessManifest, service: string | undefined): Promise<void> {
97
+ const targets = manifest.services
98
+ .filter((candidate) => !service || candidate.name === service)
99
+ .flatMap((candidate) =>
100
+ Array.from({ length: candidate.instances }, (_, instance) => ({
101
+ path: `${manifest.logDir}/${candidate.processName}-${instance}.log`,
102
+ label: instance === 0 ? candidate.name : `${candidate.name}#${instance}`,
103
+ }))
104
+ )
105
+ if (targets.length === 0) {
106
+ throw new Error(
107
+ `[SshTarget] No service named '${service}'. ` +
108
+ `Services: ${manifest.services.map((candidate) => candidate.name).join(", ")}.`
109
+ )
110
+ }
111
+
112
+ const labels = new Map(targets.map((target) => [target.path, target.label]))
113
+ const labelWidth = Math.max(...targets.map((target) => target.label.length))
114
+ const print = (item: LogLine) => {
115
+ const line = renderProcessLogLine(item, { label: labels.get(item.path) ?? "", labelWidth })
116
+ if (line !== null) console.log(line)
117
+ }
118
+
119
+ const paths = targets.map((target) => target.path)
120
+ for (const item of await readRecentLogLines(paths, Number(option("--tail") ?? 100))) print(item)
121
+ if (!flag("--follow")) return
122
+
123
+ const controller = new AbortController()
124
+ // SIGHUP arrives when the SSH connection that asked for the logs goes away.
125
+ for (const signal of ["SIGINT", "SIGTERM", "SIGHUP"] as const) {
126
+ process.once(signal, () => controller.abort())
127
+ }
128
+ await followLogFiles(paths, print, { signal: controller.signal })
129
+ }
130
+
131
+ async function readRelease(path: string): Promise<DeployRecord | null> {
132
+ try {
133
+ return JSON.parse(await readFile(path, "utf8")) as DeployRecord
134
+ } catch (error) {
135
+ if (isMissing(error)) return null
136
+ throw error
137
+ }
138
+ }
139
+
140
+ function option(name: string): string | undefined {
141
+ const index = argv.indexOf(name)
142
+ return index === -1 ? undefined : argv[index + 1]
143
+ }
144
+
145
+ function flag(name: string): boolean {
146
+ return argv.includes(name)
147
+ }
148
+
149
+ function positionals(): string[] {
150
+ const values: string[] = []
151
+ for (let index = 0; index < argv.length; index++) {
152
+ const argument = argv[index] ?? ""
153
+ if (argument === "--manifest" || argument === "--tail") index++
154
+ else if (!argument.startsWith("--")) values.push(argument)
155
+ }
156
+ return values
157
+ }
@@ -0,0 +1,180 @@
1
+ import { createWriteStream, type WriteStream } from "node:fs"
2
+ import { mkdir, open, rename, stat } from "node:fs/promises"
3
+ import { dirname, resolve } from "node:path"
4
+ import type { ProcessManifest } from "./manifest"
5
+ import { isMissing } from "./state"
6
+
7
+ /** A log past this size moves to `<name>.log.1`, replacing the previous one. */
8
+ export const MAX_LOG_BYTES = 20 * 1024 * 1024
9
+
10
+ /** How much of a log `readRecentLogLines` reads from its end. */
11
+ const RECENT_BYTES = 256 * 1024
12
+
13
+ export interface LogLine {
14
+ readonly path: string
15
+ readonly line: string
16
+ }
17
+
18
+ export function processLogPath(
19
+ manifest: Pick<ProcessManifest, "logDir">,
20
+ processName: string,
21
+ instance: number
22
+ ): string {
23
+ return resolve(manifest.logDir, `${processName}-${instance}.log`)
24
+ }
25
+
26
+ export async function openProcessLog(path: string): Promise<WriteStream> {
27
+ await mkdir(dirname(path), { recursive: true })
28
+ return createWriteStream(path, { flags: "a" })
29
+ }
30
+
31
+ /** Moves a log aside once it passes {@link MAX_LOG_BYTES}. Returns whether it did. */
32
+ export async function rotateLogIfLarge(path: string): Promise<boolean> {
33
+ if ((await fileSize(path)) <= MAX_LOG_BYTES) return false
34
+ await rename(path, `${path}.1`)
35
+ return true
36
+ }
37
+
38
+ /** The last `tail` lines across the given logs, in time order. */
39
+ export async function readRecentLogLines(
40
+ paths: readonly string[],
41
+ tail: number
42
+ ): Promise<LogLine[]> {
43
+ const lines: LogLine[] = []
44
+ for (const path of paths) {
45
+ const recent = (await readEnd(path, RECENT_BYTES)).split(/\r?\n/).filter(Boolean).slice(-tail)
46
+ lines.push(...recent.map((line) => ({ path, line })))
47
+ }
48
+ return sortLogLines(lines).slice(-tail)
49
+ }
50
+
51
+ export async function followLogFiles(
52
+ paths: readonly string[],
53
+ onLine: (line: LogLine) => void,
54
+ options: { readonly intervalMs?: number; readonly signal?: AbortSignal } = {}
55
+ ): Promise<void> {
56
+ const positions = new Map<string, number>()
57
+ for (const path of paths) positions.set(path, await fileSize(path))
58
+
59
+ while (!options.signal?.aborted) {
60
+ await new Promise<void>((resolveWait) => setTimeout(resolveWait, options.intervalMs ?? 500))
61
+
62
+ for (const path of paths) {
63
+ const previous = positions.get(path) ?? 0
64
+ const current = await fileSize(path)
65
+ // A smaller file was rotated: read the new one from its start.
66
+ const from = current < previous ? 0 : previous
67
+ positions.set(path, current)
68
+ if (current === from) continue
69
+
70
+ const chunk = await readRange(path, from, current)
71
+ for (const line of chunk.split(/\r?\n/).filter(Boolean)) onLine({ path, line })
72
+ }
73
+ }
74
+ }
75
+
76
+ /**
77
+ * Terminal control sequences: services draw their startup view with cursor movement, which reads
78
+ * as noise once it is in a log.
79
+ */
80
+ const TERMINAL_CONTROL = /\x1B(?:\[[0-?]*[ -/]*[@-~]|\][^\x07\x1B]*(?:\x07|\x1B\\)|[@-Z\\-_])/g
81
+
82
+ export function stripTerminalControl(line: string): string {
83
+ return line.replace(TERMINAL_CONTROL, "")
84
+ }
85
+
86
+ /** `hh:mm:ss service stderr message`, or `null` for an empty line. */
87
+ export function renderProcessLogLine(
88
+ item: LogLine,
89
+ options: { readonly label: string; readonly labelWidth?: number }
90
+ ): string | null {
91
+ const parsed = parseStoredLogLine(item.line)
92
+ const message = formatLogMessage(stripTerminalControl(parsed.message))
93
+ if (!message.trim()) return null
94
+
95
+ const time = parsed.timestamp ? formatLogTime(parsed.timestamp) : " ".repeat(8)
96
+ const label = options.label.padEnd(options.labelWidth ?? options.label.length)
97
+ const stream = parsed.stream === "stderr" ? " stderr" : ""
98
+ return `${time} ${label}${stream} ${message}`
99
+ }
100
+
101
+ function sortLogLines(lines: readonly LogLine[]): LogLine[] {
102
+ return lines
103
+ .map((line, index) => ({ line, index, time: logTimestampMs(line.line) }))
104
+ .sort((a, b) => a.time - b.time || a.index - b.index)
105
+ .map((item) => item.line)
106
+ }
107
+
108
+ function parseStoredLogLine(line: string): {
109
+ readonly timestamp?: Date
110
+ readonly stream?: "stdout" | "stderr"
111
+ readonly message: string
112
+ } {
113
+ const match = line.match(/^\[([^\]]+)](?: \[(stdout|stderr)])?\s?(.*)$/)
114
+ if (!match) return { message: line }
115
+ const timestamp = new Date(match[1] ?? "")
116
+ return {
117
+ ...(Number.isFinite(timestamp.getTime()) ? { timestamp } : {}),
118
+ ...(match[2] === "stdout" || match[2] === "stderr" ? { stream: match[2] } : {}),
119
+ message: match[3] ?? "",
120
+ }
121
+ }
122
+
123
+ function formatLogMessage(message: string): string {
124
+ const start = message.match(/^start\s+\S+#\d+\s+pid=(\S+)$/)
125
+ if (start) return `started pid=${start[1]}`
126
+
127
+ const exit = message.match(/^exit code=(\S+)\s+signal=(\S+)$/)
128
+ if (exit) {
129
+ const code = exit[1] === "null" ? "" : ` code=${exit[1]}`
130
+ const signal = exit[2] === "null" ? "" : ` signal=${exit[2]}`
131
+ return `exited${code}${signal}`
132
+ }
133
+
134
+ return message
135
+ }
136
+
137
+ function formatLogTime(value: Date): string {
138
+ return [value.getHours(), value.getMinutes(), value.getSeconds()]
139
+ .map((part) => String(part).padStart(2, "0"))
140
+ .join(":")
141
+ }
142
+
143
+ function logTimestampMs(line: string): number {
144
+ const time = Date.parse(line.match(/^\[([^\]]+)]/)?.[1] ?? "")
145
+ return Number.isFinite(time) ? time : Number.MAX_SAFE_INTEGER
146
+ }
147
+
148
+ async function readEnd(path: string, bytes: number): Promise<string> {
149
+ const size = await fileSize(path)
150
+ const text = await readRange(path, Math.max(0, size - bytes), size)
151
+ // Drop a line cut in half by starting mid-file.
152
+ return size > bytes ? text.slice(text.indexOf("\n") + 1) : text
153
+ }
154
+
155
+ async function readRange(path: string, start: number, end: number): Promise<string> {
156
+ if (end <= start) return ""
157
+ let handle: Awaited<ReturnType<typeof open>>
158
+ try {
159
+ handle = await open(path, "r")
160
+ } catch (error) {
161
+ if (isMissing(error)) return ""
162
+ throw error
163
+ }
164
+ try {
165
+ const buffer = Buffer.alloc(end - start)
166
+ const { bytesRead } = await handle.read(buffer, 0, buffer.length, start)
167
+ return buffer.subarray(0, bytesRead).toString("utf8")
168
+ } finally {
169
+ await handle.close()
170
+ }
171
+ }
172
+
173
+ async function fileSize(path: string): Promise<number> {
174
+ try {
175
+ return (await stat(path)).size
176
+ } catch (error) {
177
+ if (isMissing(error)) return 0
178
+ throw error
179
+ }
180
+ }