@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
package/src/layout.ts CHANGED
@@ -1,109 +1,157 @@
1
1
  import { posix } from "node:path"
2
+ import type { DeployHttpServiceName } from "@sixb/core/deploy"
2
3
 
3
- /**
4
- * Where a deployment lives on its server. Everything but the Bun binaries and the Caddy route
5
- * sits in one directory in the deploy user's home, so projects under one user never share files.
6
- *
7
- * ~/<name>/releases/<id> immutable release directories (code, node_modules, .sixb/dist)
8
- * ~/<name>/current symlink to the active release directory
9
- * ~/<name>/code symlink to `current` (migrated in place from single-folder deploys)
10
- * ~/<name>/shared/.env shared project `.env`, linked from each release
11
- * ~/<name>/env-token the read-only token a deploy pulls `.env` with, if it has a source
12
- * ~/<name>/deploy lock, uploads, manifests, release record, ctl
13
- * ~/<name>/run supervisor state, control requests, and logs
14
- * ~/.sixb/bun/<v> Bun, shared by the user's projects that pin the same version
15
- */
4
+ /** The read-only token a deploy pulls `.env` with, for a project with a source. */
16
5
  export const ENV_TOKEN_FILE = "env-token"
17
6
 
7
+ /**
8
+ * A deployment runs in one of two slots. A release starts in the idle one, beside the live one,
9
+ * and takes traffic once it is healthy.
10
+ */
18
11
  export type DeploySlot = "blue" | "green"
19
12
 
13
+ export const SLOTS: readonly DeploySlot[] = ["blue", "green"]
14
+
15
+ /** The loopback ports each slot's HTTP services listen on. Caddy forwards to the live slot's. */
16
+ export const SLOT_PORTS = {
17
+ blue: { app: 3001, api: 3002 },
18
+ green: { app: 3011, api: 3012 },
19
+ } as const satisfies Record<DeploySlot, Record<DeployHttpServiceName, number>>
20
+
21
+ export function otherSlot(slot: DeploySlot): DeploySlot {
22
+ return slot === "blue" ? "green" : "blue"
23
+ }
24
+
25
+ /**
26
+ * Where a deployment lives on its server: one folder in the deploy account's home, plus Bun, the
27
+ * slot service and the Caddy routes.
28
+ *
29
+ * ~/<name>/releases/<id> immutable releases: code, node_modules, .sixb/dist, .env
30
+ * ~/<name>/current the live release
31
+ * ~/<name>/code `current`, for scripts and older tools that look here
32
+ * ~/<name>/shared/.env the `.env` kept on the server, for a project without a source
33
+ * ~/<name>/env-token the token a deploy pulls `.env` with, for a project with one
34
+ * ~/<name>/deploy/ lock, uploads, release record, live slot, slot launchers, ctl
35
+ * ~/<name>/run/<slot>/ each slot's supervisor state, control requests and logs
36
+ * ~/.sixb/bun/<version>/ Bun, shared by releases on the same version
37
+ */
20
38
  export interface DeploymentLayout {
39
+ readonly name: string
40
+ readonly home: string
21
41
  readonly root: string
22
42
  readonly releasesDir: string
23
43
  readonly currentLink: string
24
44
  readonly code: string
25
- readonly sharedDir: string
26
45
  readonly sharedEnv: string
27
46
  readonly envToken: string
28
- readonly project: string
29
- readonly projectPath: string
30
47
  readonly deployDir: string
31
48
  readonly incomingDir: string
49
+ readonly runsDir: string
32
50
  readonly lock: string
33
- readonly fileList: string
34
- readonly manifest: string
35
- readonly candidateManifest: string
36
51
  readonly releaseRecord: string
37
- /** Runs the server half of this package with the project's Bun and manifest. */
52
+ /** Names the live slot. */
53
+ readonly liveSlotFile: string
54
+ /** What `status`, `logs` and the other operations run: the live release's server half. */
38
55
  readonly ctl: string
39
- readonly runDir: string
40
- readonly logDir: string
41
- readonly bunDir: string
42
- readonly bun: string
43
- readonly unitName: string
44
- readonly unit: string
56
+ readonly unitDir: string
57
+ /** The slot service's template, instantiated as `sixb-<name>@blue` and `sixb-<name>@green`. */
58
+ readonly unitTemplate: string
45
59
  readonly caddyDir: string
46
60
  readonly caddySnippet: string
47
61
  }
48
62
 
49
- export interface ReleaseLayout {
50
- readonly id: string
51
- readonly root: string
52
- readonly project: string
63
+ export interface SlotLayout {
64
+ readonly slot: DeploySlot
65
+ readonly unit: string
66
+ readonly manifest: string
67
+ /** What the slot's service runs: its release's supervisor, with its Bun. */
68
+ readonly launcher: string
69
+ readonly runDir: string
70
+ readonly logDir: string
53
71
  }
54
72
 
73
+ /** A deployment from before slots: one service, one manifest. Adopted by its first slot deploy. */
74
+ export interface LegacyLayout {
75
+ readonly unit: string
76
+ readonly unitPath: string
77
+ readonly manifest: string
78
+ }
79
+
80
+ /** Where a deployment lives: under the home of the account it runs as, whichever that is. */
55
81
  export function deploymentLayout(input: {
56
82
  readonly home: string
57
83
  readonly user: string
58
84
  readonly name: string
59
- readonly projectPath: string
60
- readonly bunVersion: string
61
- }): DeploymentLayout {
85
+ }) {
62
86
  const root = posix.join(input.home, input.name)
63
- const releasesDir = posix.join(root, "releases")
64
- const currentLink = posix.join(root, "current")
65
- const code = posix.join(root, "code")
66
- const sharedDir = posix.join(root, "shared")
67
87
  const deployDir = posix.join(root, "deploy")
68
- const runDir = posix.join(root, "run")
69
- const bunDir = posix.join(input.home, ".sixb", "bun", input.bunVersion)
70
- const unitName = `sixb-${input.name}.service`
88
+ const unitDir = posix.join(input.home, ".config", "systemd", "user")
89
+ // Each account writes routes only in its own folder, which the server's Caddyfile imports.
71
90
  const caddyDir = posix.join("/etc/caddy/sixb.d", input.user)
72
-
73
91
  return {
92
+ name: input.name,
93
+ home: input.home,
74
94
  root,
75
- releasesDir,
76
- currentLink,
77
- code,
78
- sharedDir,
79
- sharedEnv: posix.join(sharedDir, ".env"),
95
+ releasesDir: posix.join(root, "releases"),
96
+ currentLink: posix.join(root, "current"),
97
+ code: posix.join(root, "code"),
98
+ sharedEnv: posix.join(root, "shared", ".env"),
80
99
  envToken: posix.join(root, ENV_TOKEN_FILE),
81
- project: posix.join(code, input.projectPath),
82
- projectPath: input.projectPath,
83
100
  deployDir,
84
101
  incomingDir: posix.join(deployDir, "incoming"),
102
+ runsDir: posix.join(deployDir, "runs"),
85
103
  lock: posix.join(deployDir, "lock"),
86
- fileList: posix.join(deployDir, "files.txt"),
87
- manifest: posix.join(deployDir, "processes.json"),
88
- candidateManifest: posix.join(deployDir, "candidate.json"),
89
104
  releaseRecord: posix.join(deployDir, "release.json"),
105
+ liveSlotFile: posix.join(deployDir, "live-slot"),
90
106
  ctl: posix.join(deployDir, "ctl"),
91
- runDir,
92
- logDir: posix.join(runDir, "logs"),
93
- bunDir,
94
- bun: posix.join(bunDir, "bun"),
95
- unitName,
96
- unit: posix.join(input.home, ".config", "systemd", "user", unitName),
107
+ unitDir,
108
+ unitTemplate: posix.join(unitDir, `sixb-${input.name}@.service`),
97
109
  caddyDir,
98
110
  caddySnippet: posix.join(caddyDir, `${input.name}.caddy`),
111
+ } satisfies DeploymentLayout
112
+ }
113
+
114
+ export function slotLayout(layout: DeploymentLayout, slot: DeploySlot): SlotLayout {
115
+ const runDir = posix.join(layout.root, "run", slot)
116
+ return {
117
+ slot,
118
+ unit: `sixb-${layout.name}@${slot}.service`,
119
+ manifest: posix.join(layout.deployDir, `${slot}.json`),
120
+ launcher: posix.join(layout.deployDir, slot),
121
+ runDir,
122
+ logDir: posix.join(runDir, "logs"),
99
123
  }
100
124
  }
101
125
 
102
- export function releaseLayout(layout: DeploymentLayout, releaseId: string): ReleaseLayout {
103
- const root = posix.join(layout.releasesDir, releaseId)
126
+ export function legacyLayout(layout: DeploymentLayout): LegacyLayout {
127
+ const unit = `sixb-${layout.name}.service`
104
128
  return {
105
- id: releaseId,
129
+ unit,
130
+ unitPath: posix.join(layout.unitDir, unit),
131
+ manifest: posix.join(layout.deployDir, "processes.json"),
132
+ }
133
+ }
134
+
135
+ export interface ReleaseLayout {
136
+ readonly id: string
137
+ readonly root: string
138
+ /** The project inside the release: its root, or a folder of a monorepo. */
139
+ readonly project: string
140
+ readonly bunDir: string
141
+ readonly bun: string
142
+ }
143
+
144
+ export function releaseLayout(
145
+ layout: DeploymentLayout,
146
+ input: { readonly id: string; readonly projectPath: string; readonly bunVersion: string }
147
+ ): ReleaseLayout {
148
+ const root = posix.join(layout.releasesDir, input.id)
149
+ const bunDir = posix.join(layout.home, ".sixb", "bun", input.bunVersion)
150
+ return {
151
+ id: input.id,
106
152
  root,
107
- project: posix.join(root, layout.projectPath),
153
+ project: posix.join(root, input.projectPath),
154
+ bunDir,
155
+ bun: posix.join(bunDir, "bun"),
108
156
  }
109
157
  }
package/src/operations.ts CHANGED
@@ -1,3 +1,4 @@
1
+ /// <reference path="./sh.d.ts" />
1
2
  import { randomUUID } from "node:crypto"
2
3
  import type {
3
4
  DeployContext,
@@ -8,96 +9,135 @@ import type {
8
9
  DeploySource,
9
10
  DeployStatus,
10
11
  } from "@sixb/core/deploy"
11
- import { deploymentLayout } from "./layout"
12
- import type { SlotPorts } from "./render"
13
- import {
14
- renderDeployScript,
15
- renderPrepareScript,
16
- renderReceiveScript,
17
- STEP_MARKER,
18
- } from "./scripts"
12
+ import bootstrap from "./bootstrap.sh" with { type: "text" }
13
+ import { deriveReleaseId, parseEvent, type ReleasePlan, releaseStepLabels } from "./plan"
19
14
  import { stripTerminalControl } from "./server/logs"
20
15
  import { shellQuote } from "./shell"
21
16
  import { RemoteScriptError, type RemoteShell } from "./transport"
22
17
 
23
18
  const NOT_DEPLOYED = "__SIXB_NOT_DEPLOYED__"
24
19
 
25
- /** Uploads the source, then runs the release on the server as one locked script. */
20
+ /**
21
+ * Uploads the source and the plan, starts the release on the server detached from this
22
+ * connection, and follows its log until it ends. A dropped connection leaves the deploy running.
23
+ */
26
24
  export async function deployRelease(
27
25
  shell: RemoteShell,
28
- user: string,
29
26
  release: DeployRelease,
30
27
  source: DeploySource,
31
- context: DeployContext,
32
- slotPorts?: SlotPorts
28
+ context: DeployContext
33
29
  ): Promise<void> {
34
- const prepared = await capture(shell, renderPrepareScript(release.name, source.commit))
35
- const [home, incoming] = prepared
36
- if (!home || !incoming) {
37
- throw new Error("[SshTarget] The server did not report its home directory.")
30
+ const record = {
31
+ commit: source.commit,
32
+ ref: source.ref,
33
+ deployedAt: new Date().toISOString(),
34
+ deployedBy: context.deployedBy,
38
35
  }
36
+ const releaseId = deriveReleaseId(record)
37
+ const [home, incoming] = await capture(
38
+ shell,
39
+ [
40
+ `root="$HOME"/${shellQuote(release.name)}`,
41
+ 'mkdir -p "$root/deploy/incoming" "$root/deploy/runs"',
42
+ // Run logs hold all the build and migration output: for the deploy account alone.
43
+ 'chmod 700 "$root/deploy/runs"',
44
+ `incoming="$(mktemp -d "$root/deploy/incoming/${releaseId}.XXXXXX")"`,
45
+ // `mktemp` makes it 0700, and tar passes the mode of the upload's `.` on to the release.
46
+ 'chmod 755 "$incoming"',
47
+ `printf '%s\n%s\n' "$HOME" "$incoming"`,
48
+ ].join("\n")
49
+ )
50
+ if (!home || !incoming)
51
+ throw new Error("[SshTarget] The server did not report its home directory.")
39
52
 
40
- const layout = deploymentLayout({
41
- home,
42
- user,
43
- name: release.name,
44
- projectPath: source.projectPath,
45
- bunVersion: release.bunVersion,
46
- })
47
- const { labels, script } = renderDeployScript({
48
- release,
49
- layout,
50
- incoming,
51
- ...(slotPorts ? { slotPorts } : {}),
52
- record: {
53
- commit: source.commit,
54
- ref: source.ref,
55
- deployedAt: new Date().toISOString(),
56
- deployedBy: context.deployedBy,
57
- },
58
- })
59
- context.report({ type: "steps", labels: ["Upload source", ...labels] })
60
-
53
+ const labels = releaseStepLabels(release)
54
+ context.report({ type: "steps", labels })
61
55
  context.report({ type: "step", index: 0, status: "running" })
62
56
  for (const archive of source.archives) {
63
- await shell.run(renderReceiveScript(incoming, archive.path), { stdin: archive.open() })
57
+ const directory = archive.path === "." ? incoming : `${incoming}/${archive.path}`
58
+ await shell.run(
59
+ `mkdir -p ${shellQuote(directory)} && tar -x -f - -C ${shellQuote(directory)}`,
60
+ { stdin: archive.open() }
61
+ )
64
62
  }
63
+ const runs = `${home}/${release.name}/deploy/runs`
64
+ const plan: ReleasePlan = {
65
+ version: 1,
66
+ release,
67
+ record,
68
+ releaseId,
69
+ projectPath: source.projectPath,
70
+ }
71
+ const files = { plan: `${runs}/${releaseId}.json`, script: `${runs}/${releaseId}.sh` }
72
+ await upload(shell, files.plan, JSON.stringify(plan))
73
+ await upload(shell, files.script, bootstrap)
65
74
  context.report({ type: "step", index: 0, status: "done" })
66
75
 
67
- await runReportingSteps(shell, script, context)
76
+ const log = `${runs}/${releaseId}.log`
77
+ const args = [
78
+ release.name,
79
+ releaseId,
80
+ source.projectPath,
81
+ release.bunVersion,
82
+ incoming,
83
+ files.plan,
84
+ ]
85
+ const [pid] = await capture(
86
+ shell,
87
+ `setsid nohup bash ${[files.script, ...args].map(shellQuote).join(" ")} > ${shellQuote(log)} 2>&1 < /dev/null & echo $!`
88
+ )
89
+ await follow(shell, { pid: pid ?? "", log, labels, context })
68
90
  }
69
91
 
70
- /** Runs the deploy script, turning its step markers into progress and the rest into output. */
71
- async function runReportingSteps(
92
+ /** Reads the release's log as it grows, until the release ends, and reports its progress. */
93
+ async function follow(
72
94
  shell: RemoteShell,
73
- script: string,
74
- context: DeployContext
95
+ input: {
96
+ readonly pid: string
97
+ readonly log: string
98
+ readonly labels: readonly string[]
99
+ readonly context: DeployContext
100
+ }
75
101
  ): Promise<void> {
76
- const run = shell.run(script, {
77
- onLine: (line) => {
78
- const [marker, index, status] = line.split("\t")
79
- if (marker === STEP_MARKER && (status === "start" || status === "done")) {
80
- const step = Number(index) + 1
81
- context.report({
82
- type: "step",
83
- index: step,
84
- status: status === "start" ? "running" : "done",
85
- })
86
- } else {
87
- const output = stripTerminalControl(line)
88
- if (output.trim()) context.report({ type: "output", line: output })
89
- }
90
- },
91
- })
102
+ const { context, labels } = input
103
+ let exit: number | null = null
104
+ let failure: string | null = null
92
105
  try {
93
- await run
106
+ await shell.run(
107
+ `tail -n +1 -F --pid=${shellQuote(input.pid)} ${shellQuote(input.log)} 2> /dev/null`,
108
+ {
109
+ onLine: (line, stream) => {
110
+ if (stream !== "stdout") return
111
+ const event = parseEvent(line)
112
+ if (event && "exit" in event) {
113
+ exit = event.exit
114
+ } else if (event) {
115
+ const index = labels.indexOf(event.step)
116
+ if (index === -1) context.report({ type: "output", line: event.step })
117
+ else context.report({ type: "step", index, status: event.status })
118
+ } else {
119
+ const output = stripTerminalControl(line)
120
+ if (output.startsWith("[SshTarget] ")) failure = output
121
+ if (output.trim()) context.report({ type: "output", line: output })
122
+ }
123
+ },
124
+ }
125
+ )
94
126
  } catch (error) {
95
- // The step's output already reached `report`; repeating it here would print it twice.
96
- if (error instanceof RemoteScriptError && error.exitCode !== 255) {
97
- throw new Error(`[SshTarget] The step exited with code ${error.exitCode}.`)
98
- }
99
- throw error
127
+ throw new Error(
128
+ `[SshTarget] Lost the connection while following the deploy, which carries on on the server. \`sixb deploy status\` shows the release once it ends; its log is ${input.log}.`,
129
+ { cause: error }
130
+ )
100
131
  }
132
+ if (exit === 0) return
133
+ // The step's output already reached `report`; the error repeats only its conclusion.
134
+ throw new Error(failure ?? `[SshTarget] The deploy ended with code ${exit ?? "unknown"}.`)
135
+ }
136
+
137
+ async function upload(shell: RemoteShell, path: string, content: string): Promise<void> {
138
+ await shell.run(`umask 077 && cat > ${shellQuote(path)}`, {
139
+ stdin: new Blob([content]).stream(),
140
+ })
101
141
  }
102
142
 
103
143
  export async function readStatus(shell: RemoteShell, name: string): Promise<DeployStatus> {
package/src/plan.ts ADDED
@@ -0,0 +1,59 @@
1
+ import type { DeployCommand, DeployRecord, DeployRelease } from "@sixb/core/deploy"
2
+
3
+ /** What the laptop sends the server's runner: the release, and how it was asked for. */
4
+ export interface ReleasePlan {
5
+ readonly version: 1
6
+ readonly release: DeployRelease
7
+ readonly record: DeployRecord
8
+ readonly releaseId: string
9
+ readonly projectPath: string
10
+ }
11
+
12
+ /** Starts every progress line, so the laptop tells events from a command's output (RFC 7464). */
13
+ export const EVENT_PREFIX = "\u001e"
14
+
15
+ export type ReleaseEvent =
16
+ | { readonly step: string; readonly status: "running" | "done" }
17
+ | { readonly exit: number }
18
+
19
+ export function event(value: ReleaseEvent): string {
20
+ return `${EVENT_PREFIX}${JSON.stringify(value)}`
21
+ }
22
+
23
+ export function parseEvent(line: string): ReleaseEvent | null {
24
+ if (!line.startsWith(EVENT_PREFIX)) return null
25
+ try {
26
+ return JSON.parse(line.slice(EVENT_PREFIX.length)) as ReleaseEvent
27
+ } catch {
28
+ return null
29
+ }
30
+ }
31
+
32
+ /** The steps a deploy runs, in order: the upload, then what the server does. */
33
+ export function releaseStepLabels(release: DeployRelease): string[] {
34
+ return [
35
+ "Upload source",
36
+ "Prepare release",
37
+ `Install Bun ${release.bunVersion}`,
38
+ "Install dependencies",
39
+ ...(release.steps.env ? [commandLabel(release.steps.env)] : []),
40
+ ...release.steps.build.map(commandLabel),
41
+ ...release.steps.beforeStart.map(commandLabel),
42
+ "Start new release",
43
+ "Check services",
44
+ "Update routes",
45
+ "Move singletons",
46
+ "Retire previous release",
47
+ ]
48
+ }
49
+
50
+ export function commandLabel(command: DeployCommand): string {
51
+ return [command.program, ...command.args].join(" ")
52
+ }
53
+
54
+ /** A release folder's name: when, to the millisecond, then which commit. */
55
+ export function deriveReleaseId(record: DeployRecord): string {
56
+ const stamp = record.deployedAt.replace(/[-:.]/g, "").slice(0, 18)
57
+ const sha = record.commit.slice(0, 12).replace(/[^a-zA-Z0-9_-]/g, "")
58
+ return `${stamp}Z-${sha || "release"}`
59
+ }
package/src/render.ts CHANGED
@@ -1,16 +1,12 @@
1
1
  import type { DeployHttpServiceName, DeployRelease, DeployReleaseService } from "@sixb/core/deploy"
2
- import type { DeploymentLayout, DeploySlot } from "./layout"
2
+ import type { DeploymentLayout, ReleaseLayout, SlotLayout } from "./layout"
3
3
  import type { ProcessManifest } from "./server/manifest"
4
- import { INTERRUPT_LEAD_MS, retiringKillTimeoutMs } from "./server/supervisor"
4
+ import { drainKillTimeoutMs, INTERRUPT_LEAD_MS } from "./server/supervisor"
5
5
  import { shellQuote } from "./shell"
6
6
  import { renderUpdatePage } from "./update-page"
7
7
 
8
8
  const GENERATED = "Generated by sixb deploy. Changes are replaced on the next deploy."
9
9
 
10
- export type SlotPorts = Readonly<
11
- Record<DeploySlot, Readonly<Record<DeployHttpServiceName, number>>>
12
- >
13
-
14
10
  /** Rewrites a release's HTTP services to listen on the given slot's loopback ports. */
15
11
  export function releaseForSlot(
16
12
  release: DeployRelease,
@@ -47,28 +43,33 @@ function replacePortArg(args: readonly string[], port: number): string[] {
47
43
  return next
48
44
  }
49
45
 
50
- /** The supervisor's view of the release: absolute commands, full environments. */
46
+ /** The supervisor's view of a release in one slot: absolute commands, full environments. */
51
47
  export function renderProcessManifest(
52
48
  release: DeployRelease,
53
- layout: DeploymentLayout,
54
- options: { readonly projectDir?: string; readonly slot?: DeploySlot } = {}
49
+ input: {
50
+ readonly layout: DeploymentLayout
51
+ readonly slot: SlotLayout
52
+ readonly release: ReleaseLayout
53
+ readonly holdExclusive: boolean
54
+ }
55
55
  ): ProcessManifest {
56
- const projectDir = options.projectDir ?? layout.project
57
- const sixb = `${projectDir}/node_modules/.bin/sixb`
56
+ const project = input.release.project
57
+ const sixb = `${project}/node_modules/.bin/sixb`
58
58
  return {
59
59
  version: 1,
60
60
  name: release.name,
61
- ...(options.slot ? { slot: options.slot } : {}),
62
- cwd: projectDir,
63
- runDir: layout.runDir,
64
- logDir: layout.logDir,
65
- releasePath: layout.releaseRecord,
61
+ slot: input.slot.slot,
62
+ ...(input.holdExclusive ? { holdExclusive: true } : {}),
63
+ cwd: project,
64
+ runDir: input.slot.runDir,
65
+ logDir: input.slot.logDir,
66
+ releasePath: input.layout.releaseRecord,
66
67
  env: release.env,
67
68
  services: release.services.map((service) => ({
68
69
  name: service.name,
69
70
  kind: service.kind,
70
71
  processName: `${release.name}-${service.name}`,
71
- command: layout.bun,
72
+ command: input.release.bun,
72
73
  args:
73
74
  service.command.program === "sixb"
74
75
  ? [sixb, ...service.command.args]
@@ -91,7 +92,7 @@ function serviceEnvWithDrain(service: DeployReleaseService): Record<string, stri
91
92
  (service.name === "api" || service.name === "workers") &&
92
93
  service.env.SIXB_DRAIN_TIMEOUT_MS === undefined
93
94
  ) {
94
- const drainTimeoutMs = retiringKillTimeoutMs(service.process.killTimeoutMs) - INTERRUPT_LEAD_MS
95
+ const drainTimeoutMs = drainKillTimeoutMs(service.process.killTimeoutMs) - INTERRUPT_LEAD_MS
95
96
 
96
97
  return {
97
98
  SIXB_DRAIN_TIMEOUT_MS: String(drainTimeoutMs),
@@ -101,40 +102,58 @@ function serviceEnvWithDrain(service: DeployReleaseService): Record<string, stri
101
102
  return { ...service.env }
102
103
  }
103
104
 
104
- /** Pins the server half of this package to the project's Bun and manifest. */
105
- export function renderCtl(layout: DeploymentLayout): string {
105
+ /** What a slot's service runs: its release's supervisor, with that release's Bun. */
106
+ export function renderLauncher(release: ReleaseLayout, slot: SlotLayout): string {
107
+ return renderServerCommand(release, ["supervise"], slot.manifest)
108
+ }
109
+
110
+ /** What `status`, `logs` and the other operations run: the live release's server half. */
111
+ export function renderCtl(release: ReleaseLayout, slot: SlotLayout): string {
112
+ return renderServerCommand(release, [], slot.manifest, '"$@" ')
113
+ }
114
+
115
+ function renderServerCommand(
116
+ release: ReleaseLayout,
117
+ args: readonly string[],
118
+ manifest: string,
119
+ passThrough = ""
120
+ ): string {
121
+ const bin = `${release.project}/node_modules/.bin/sixb-deploy-ssh`
122
+ const command = [release.bun, bin, ...args].map(shellQuote).join(" ")
106
123
  return [
107
124
  "#!/bin/sh",
108
125
  `# ${GENERATED}`,
109
- `exec ${shellQuote(layout.bun)} ${shellQuote(`${layout.project}/node_modules/.bin/sixb-deploy-ssh`)} "$@" --manifest ${shellQuote(layout.manifest)}`,
126
+ // A process that runs `bun` or `node` (a dependency's script) finds the release's Bun.
127
+ `PATH=${shellQuote(release.bunDir)}:"$PATH"; export PATH`,
128
+ `exec ${command} ${passThrough}--manifest ${shellQuote(manifest)}`,
110
129
  "",
111
130
  ].join("\n")
112
131
  }
113
132
 
114
133
  /**
115
- * The deployment's `systemd --user` unit. Linger starts the user's systemd at boot, so the
116
- * unit comes back after a reboot without anyone logged in.
134
+ * The slot service's template: `sixb-<name>@blue` and `sixb-<name>@green` each run their slot's
135
+ * launcher. Lingering starts the live slot's at boot. Stopping one signals only the supervisor,
136
+ * which lets its HTTP services and workers drain before systemd's stop timeout.
117
137
  */
118
- export function renderUnit(release: DeployRelease, layout: DeploymentLayout): string {
119
- // Long enough for the supervisor to stop every process with its own kill timeout.
120
- const stopSeconds =
121
- Math.ceil(
122
- Math.max(...release.services.map((service) => service.process.killTimeoutMs)) / 1000
123
- ) + 10
138
+ export function renderSlotUnit(release: DeployRelease, layout: DeploymentLayout): string {
139
+ const killTimeoutMs = Math.max(
140
+ ...release.services.map((service) => drainKillTimeoutMs(service.process.killTimeoutMs))
141
+ )
124
142
  return [
125
143
  `# ${GENERATED}`,
126
144
  "[Unit]",
127
- `Description=Sixb deployment ${release.name}`,
145
+ `Description=Sixb deployment ${release.name}, %i slot`,
128
146
  "",
129
147
  "[Service]",
130
148
  "Type=simple",
131
- `WorkingDirectory=${systemdValue(layout.project)}`,
132
- `Environment=${systemdValue(`PATH=${layout.bunDir}:/usr/local/bin:/usr/bin:/bin`)}`,
133
- `ExecStart=${systemdValue(layout.ctl)} supervise`,
149
+ `WorkingDirectory=${systemdValue(layout.root)}`,
150
+ "Environment=PATH=/usr/local/bin:/usr/bin:/bin",
151
+ `ExecStart=${systemdValue(layout.deployDir)}/%i`,
134
152
  "Restart=always",
135
153
  "RestartSec=2",
154
+ "KillMode=mixed",
136
155
  "KillSignal=SIGTERM",
137
- `TimeoutStopSec=${stopSeconds}`,
156
+ `TimeoutStopSec=${Math.ceil(killTimeoutMs / 1000) + 15}`,
138
157
  "",
139
158
  "[Install]",
140
159
  "WantedBy=default.target",
@@ -170,7 +189,7 @@ export function renderCaddySnippet(release: DeployRelease): string {
170
189
  * whole retiring window; the API closes its sockets as soon as it starts draining.
171
190
  */
172
191
  function keepWebSocketsThroughReload(service: DeployReleaseService): string[] {
173
- const seconds = retiringKillTimeoutMs(service.process.killTimeoutMs) / 1000
192
+ const seconds = drainKillTimeoutMs(service.process.killTimeoutMs) / 1000
174
193
  return [`\t\tstream_close_delay ${seconds}s`]
175
194
  }
176
195