@sixb/deploy-ssh 0.2.0 → 0.2.6
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/README.md +7 -5
- package/dist/check.d.ts.map +1 -1
- package/dist/index.js +592 -121
- package/dist/index.js.map +12 -11
- package/dist/layout.d.ts +21 -5
- package/dist/layout.d.ts.map +1 -1
- package/dist/operations.d.ts +10 -2
- package/dist/operations.d.ts.map +1 -1
- package/dist/render.d.ts +10 -4
- package/dist/render.d.ts.map +1 -1
- package/dist/scripts.d.ts +20 -10
- package/dist/scripts.d.ts.map +1 -1
- package/dist/server/manifest.d.ts +11 -0
- package/dist/server/manifest.d.ts.map +1 -1
- package/dist/server/run.d.ts +23 -0
- package/dist/server/run.d.ts.map +1 -0
- package/dist/server/state.d.ts +6 -0
- package/dist/server/state.d.ts.map +1 -1
- package/dist/server/supervisor.d.ts +11 -3
- package/dist/server/supervisor.d.ts.map +1 -1
- package/dist/ssh-target.d.ts +8 -1
- package/dist/ssh-target.d.ts.map +1 -1
- package/dist/transport.d.ts.map +1 -1
- package/package.json +3 -3
- package/src/check.ts +32 -6
- package/src/layout.ts +40 -5
- package/src/operations.ts +56 -1
- package/src/render.ts +89 -10
- package/src/scripts.ts +494 -114
- package/src/server/cli.ts +42 -8
- package/src/server/manifest.ts +49 -8
- package/src/server/run.ts +246 -0
- package/src/server/state.ts +8 -2
- package/src/server/supervisor.ts +329 -64
- package/src/ssh-target.ts +84 -12
- package/src/transport.ts +5 -2
package/src/operations.ts
CHANGED
|
@@ -1,12 +1,15 @@
|
|
|
1
|
+
import { randomUUID } from "node:crypto"
|
|
1
2
|
import type {
|
|
2
3
|
DeployContext,
|
|
3
4
|
DeployControlAction,
|
|
4
5
|
DeployLogsOptions,
|
|
5
6
|
DeployRelease,
|
|
7
|
+
DeployScript,
|
|
6
8
|
DeploySource,
|
|
7
9
|
DeployStatus,
|
|
8
10
|
} from "@sixb/core/deploy"
|
|
9
11
|
import { deploymentLayout } from "./layout"
|
|
12
|
+
import type { SlotPorts } from "./render"
|
|
10
13
|
import {
|
|
11
14
|
renderDeployScript,
|
|
12
15
|
renderPrepareScript,
|
|
@@ -25,7 +28,8 @@ export async function deployRelease(
|
|
|
25
28
|
user: string,
|
|
26
29
|
release: DeployRelease,
|
|
27
30
|
source: DeploySource,
|
|
28
|
-
context: DeployContext
|
|
31
|
+
context: DeployContext,
|
|
32
|
+
slotPorts?: SlotPorts
|
|
29
33
|
): Promise<void> {
|
|
30
34
|
const prepared = await capture(shell, renderPrepareScript(release.name, source.commit))
|
|
31
35
|
const [home, incoming] = prepared
|
|
@@ -44,6 +48,7 @@ export async function deployRelease(
|
|
|
44
48
|
release,
|
|
45
49
|
layout,
|
|
46
50
|
incoming,
|
|
51
|
+
...(slotPorts ? { slotPorts } : {}),
|
|
47
52
|
record: {
|
|
48
53
|
commit: source.commit,
|
|
49
54
|
ref: source.ref,
|
|
@@ -130,6 +135,56 @@ export async function controlServices(
|
|
|
130
135
|
await shell.run(ctlScript(name, [action, ...(service ? [service] : [])]))
|
|
131
136
|
}
|
|
132
137
|
|
|
138
|
+
/**
|
|
139
|
+
* Uploads the script, then runs it through `ctl run` in a terminal on the server: Ctrl-C reaches
|
|
140
|
+
* the script there, and so does a dropped connection, as a hangup. Holding the deploy lock shared
|
|
141
|
+
* for the whole run keeps a deploy from replacing the code it imports or migrating its storage;
|
|
142
|
+
* runs share it with each other. Resolves with the script's exit code.
|
|
143
|
+
*/
|
|
144
|
+
export async function runDeployScript(
|
|
145
|
+
shell: RemoteShell,
|
|
146
|
+
name: string,
|
|
147
|
+
script: DeployScript
|
|
148
|
+
): Promise<number> {
|
|
149
|
+
const root = `"$HOME"/${shellQuote(name)}`
|
|
150
|
+
const staged = `${root}/deploy/runs/${randomUUID()}`
|
|
151
|
+
await shell.run(
|
|
152
|
+
[
|
|
153
|
+
`if [ ! -x ${root}/deploy/ctl ]; then`,
|
|
154
|
+
` echo "Nothing named ${name} is deployed on this server yet." >&2; exit 1`,
|
|
155
|
+
"fi",
|
|
156
|
+
"umask 077",
|
|
157
|
+
`mkdir -p ${root}/deploy/runs`,
|
|
158
|
+
// An upload whose run never started is left behind; a day later it goes.
|
|
159
|
+
`find ${root}/deploy/runs -type f -mmin +1440 -delete 2> /dev/null || true`,
|
|
160
|
+
`cat > ${staged}`,
|
|
161
|
+
].join("\n"),
|
|
162
|
+
{ stdin: new Blob([Uint8Array.from(script.source)]).stream() }
|
|
163
|
+
)
|
|
164
|
+
|
|
165
|
+
const run = [
|
|
166
|
+
`cd ${root}`,
|
|
167
|
+
`exec 9> ${root}/deploy/lock`,
|
|
168
|
+
"if ! flock -s -n 9; then",
|
|
169
|
+
` echo "Waiting for a deploy of ${name} to finish…" >&2`,
|
|
170
|
+
"fi",
|
|
171
|
+
"if ! flock -s -w 600 9; then",
|
|
172
|
+
` echo "A deploy of ${name} has held its lock for 10 minutes." >&2`,
|
|
173
|
+
` rm -f ${staged}`,
|
|
174
|
+
" exit 1",
|
|
175
|
+
"fi",
|
|
176
|
+
`exec ${root}/deploy/ctl run ${staged} ${[script.path, ...script.args].map(shellQuote).join(" ")}`,
|
|
177
|
+
].join("\n")
|
|
178
|
+
try {
|
|
179
|
+
await shell.run(run, { terminal: true })
|
|
180
|
+
return 0
|
|
181
|
+
} catch (error) {
|
|
182
|
+
// 255 is OpenSSH's own failure; any other code is the script's.
|
|
183
|
+
if (error instanceof RemoteScriptError && error.exitCode !== 255) return error.exitCode
|
|
184
|
+
throw error
|
|
185
|
+
}
|
|
186
|
+
}
|
|
187
|
+
|
|
133
188
|
/** Runs the deployment's `ctl` with arguments; fails plainly when nothing is deployed yet. */
|
|
134
189
|
function ctlScript(
|
|
135
190
|
name: string,
|
package/src/render.ts
CHANGED
|
@@ -1,33 +1,79 @@
|
|
|
1
|
-
import type { DeployRelease } from "@sixb/core/deploy"
|
|
2
|
-
import type { DeploymentLayout } from "./layout"
|
|
1
|
+
import type { DeployHttpServiceName, DeployRelease, DeployReleaseService } from "@sixb/core/deploy"
|
|
2
|
+
import type { DeploymentLayout, DeploySlot } from "./layout"
|
|
3
3
|
import type { ProcessManifest } from "./server/manifest"
|
|
4
|
+
import { INTERRUPT_LEAD_MS, retiringKillTimeoutMs } from "./server/supervisor"
|
|
4
5
|
import { shellQuote } from "./shell"
|
|
5
6
|
import { renderUpdatePage } from "./update-page"
|
|
6
7
|
|
|
7
8
|
const GENERATED = "Generated by sixb deploy. Changes are replaced on the next deploy."
|
|
8
9
|
|
|
10
|
+
export type SlotPorts = Readonly<
|
|
11
|
+
Record<DeploySlot, Readonly<Record<DeployHttpServiceName, number>>>
|
|
12
|
+
>
|
|
13
|
+
|
|
14
|
+
/** Rewrites a release's HTTP services to listen on the given slot's loopback ports. */
|
|
15
|
+
export function releaseForSlot(
|
|
16
|
+
release: DeployRelease,
|
|
17
|
+
ports: Readonly<Record<DeployHttpServiceName, number>>
|
|
18
|
+
): DeployRelease {
|
|
19
|
+
return {
|
|
20
|
+
...release,
|
|
21
|
+
services: release.services.map((service) => {
|
|
22
|
+
if (!service.http || (service.name !== "api" && service.name !== "app")) {
|
|
23
|
+
return service
|
|
24
|
+
}
|
|
25
|
+
const port = ports[service.name]
|
|
26
|
+
return {
|
|
27
|
+
...service,
|
|
28
|
+
command: {
|
|
29
|
+
...service.command,
|
|
30
|
+
args: replacePortArg(service.command.args, port),
|
|
31
|
+
},
|
|
32
|
+
http: {
|
|
33
|
+
...service.http,
|
|
34
|
+
port,
|
|
35
|
+
},
|
|
36
|
+
}
|
|
37
|
+
}),
|
|
38
|
+
}
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
function replacePortArg(args: readonly string[], port: number): string[] {
|
|
42
|
+
const next = [...args]
|
|
43
|
+
const index = next.indexOf("--port")
|
|
44
|
+
if (index !== -1 && index + 1 < next.length) {
|
|
45
|
+
next[index + 1] = String(port)
|
|
46
|
+
}
|
|
47
|
+
return next
|
|
48
|
+
}
|
|
49
|
+
|
|
9
50
|
/** The supervisor's view of the release: absolute commands, full environments. */
|
|
10
51
|
export function renderProcessManifest(
|
|
11
52
|
release: DeployRelease,
|
|
12
|
-
layout: DeploymentLayout
|
|
53
|
+
layout: DeploymentLayout,
|
|
54
|
+
options: { readonly projectDir?: string; readonly slot?: DeploySlot } = {}
|
|
13
55
|
): ProcessManifest {
|
|
14
|
-
const
|
|
56
|
+
const projectDir = options.projectDir ?? layout.project
|
|
57
|
+
const sixb = `${projectDir}/node_modules/.bin/sixb`
|
|
15
58
|
return {
|
|
16
59
|
version: 1,
|
|
17
60
|
name: release.name,
|
|
18
|
-
|
|
61
|
+
...(options.slot ? { slot: options.slot } : {}),
|
|
62
|
+
cwd: projectDir,
|
|
19
63
|
runDir: layout.runDir,
|
|
20
64
|
logDir: layout.logDir,
|
|
21
65
|
releasePath: layout.releaseRecord,
|
|
66
|
+
env: release.env,
|
|
22
67
|
services: release.services.map((service) => ({
|
|
23
68
|
name: service.name,
|
|
69
|
+
kind: service.kind,
|
|
24
70
|
processName: `${release.name}-${service.name}`,
|
|
25
71
|
command: layout.bun,
|
|
26
72
|
args:
|
|
27
73
|
service.command.program === "sixb"
|
|
28
74
|
? [sixb, ...service.command.args]
|
|
29
75
|
: [...service.command.args],
|
|
30
|
-
env: service
|
|
76
|
+
env: serviceEnvWithDrain(service),
|
|
31
77
|
instances: service.instances,
|
|
32
78
|
killTimeoutMs: service.process.killTimeoutMs,
|
|
33
79
|
restartDelayMs: service.process.restartDelayMs,
|
|
@@ -36,6 +82,25 @@ export function renderProcessManifest(
|
|
|
36
82
|
}
|
|
37
83
|
}
|
|
38
84
|
|
|
85
|
+
/**
|
|
86
|
+
* A retiring `api` or `workers` drains until the supervisor's SIGINT, so it finishes its work on
|
|
87
|
+
* its own rather than being cut short. The window follows the service's own kill timeout.
|
|
88
|
+
*/
|
|
89
|
+
function serviceEnvWithDrain(service: DeployReleaseService): Record<string, string> {
|
|
90
|
+
if (
|
|
91
|
+
(service.name === "api" || service.name === "workers") &&
|
|
92
|
+
service.env.SIXB_DRAIN_TIMEOUT_MS === undefined
|
|
93
|
+
) {
|
|
94
|
+
const drainTimeoutMs = retiringKillTimeoutMs(service.process.killTimeoutMs) - INTERRUPT_LEAD_MS
|
|
95
|
+
|
|
96
|
+
return {
|
|
97
|
+
SIXB_DRAIN_TIMEOUT_MS: String(drainTimeoutMs),
|
|
98
|
+
...service.env,
|
|
99
|
+
}
|
|
100
|
+
}
|
|
101
|
+
return { ...service.env }
|
|
102
|
+
}
|
|
103
|
+
|
|
39
104
|
/** Pins the server half of this package to the project's Bun and manifest. */
|
|
40
105
|
export function renderCtl(layout: DeploymentLayout): string {
|
|
41
106
|
return [
|
|
@@ -77,7 +142,7 @@ export function renderUnit(release: DeployRelease, layout: DeploymentLayout): st
|
|
|
77
142
|
].join("\n")
|
|
78
143
|
}
|
|
79
144
|
|
|
80
|
-
/** One site per HTTP service, forwarded to the
|
|
145
|
+
/** One site per HTTP service, forwarded to the active slot's loopback port. */
|
|
81
146
|
export function renderCaddySnippet(release: DeployRelease): string {
|
|
82
147
|
const sites = release.services.flatMap((service) => {
|
|
83
148
|
if (!service.http) return []
|
|
@@ -86,7 +151,10 @@ export function renderCaddySnippet(release: DeployRelease): string {
|
|
|
86
151
|
[
|
|
87
152
|
`${service.http.domain} {`,
|
|
88
153
|
...(service.name === "api" ? compressJson() : []),
|
|
89
|
-
`\treverse_proxy ${service.http.host}:${service.http.port}`,
|
|
154
|
+
`\treverse_proxy ${service.http.host}:${service.http.port} {`,
|
|
155
|
+
"\t\tlb_try_duration 5s",
|
|
156
|
+
...(service.name === "api" ? keepWebSocketsThroughReload(service) : []),
|
|
157
|
+
"\t}",
|
|
90
158
|
...(pages ? updatePageOnProxyErrors() : []),
|
|
91
159
|
"}",
|
|
92
160
|
].join("\n"),
|
|
@@ -95,6 +163,17 @@ export function renderCaddySnippet(release: DeployRelease): string {
|
|
|
95
163
|
return [`# ${GENERATED}`, "", sites.join("\n\n"), ""].join("\n")
|
|
96
164
|
}
|
|
97
165
|
|
|
166
|
+
/**
|
|
167
|
+
* A reload closes every WebSocket Caddy proxies unless it is told to wait, and each deploy reloads
|
|
168
|
+
* Caddy before the old API retires. Kept open, a browser's socket stays on the retiring API, which
|
|
169
|
+
* closes it with 1012 so the page reconnects without showing a drop. The wait covers the API's
|
|
170
|
+
* whole retiring window; the API closes its sockets as soon as it starts draining.
|
|
171
|
+
*/
|
|
172
|
+
function keepWebSocketsThroughReload(service: DeployReleaseService): string[] {
|
|
173
|
+
const seconds = retiringKillTimeoutMs(service.process.killTimeoutMs) / 1000
|
|
174
|
+
return [`\t\tstream_close_delay ${seconds}s`]
|
|
175
|
+
}
|
|
176
|
+
|
|
98
177
|
/**
|
|
99
178
|
* The API never compresses its responses, and an object query page is easily a megabyte of
|
|
100
179
|
* JSON that compresses about twentyfold. Only complete JSON answers are encoded: the
|
|
@@ -116,8 +195,8 @@ function compressJson(): string[] {
|
|
|
116
195
|
}
|
|
117
196
|
|
|
118
197
|
/**
|
|
119
|
-
* While the app is down — during
|
|
120
|
-
*
|
|
198
|
+
* While the app is down — during an outage — a browser asking for a page gets the update page
|
|
199
|
+
* instead of Caddy's bare error. Other requests keep the error.
|
|
121
200
|
*/
|
|
122
201
|
function updatePageOnProxyErrors(): string[] {
|
|
123
202
|
return [
|