@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/ssh-target.ts
CHANGED
|
@@ -13,6 +13,7 @@ import type {
|
|
|
13
13
|
DeployLogsOptions,
|
|
14
14
|
DeployOperationContext,
|
|
15
15
|
DeployRelease,
|
|
16
|
+
DeployScript,
|
|
16
17
|
DeploySetupContext,
|
|
17
18
|
DeploySource,
|
|
18
19
|
DeployStatus,
|
|
@@ -22,7 +23,13 @@ import { addKey, listKeys, removeKeys, removeKeysWhere } from "./access"
|
|
|
22
23
|
import { checkDns, ENV_TOKEN_CHECK, runChecks } from "./check"
|
|
23
24
|
import { generateCiKey, knownHostsLines, readHostKeys, renderCiInstall } from "./ci"
|
|
24
25
|
import { ENV_TOKEN_FILE } from "./layout"
|
|
25
|
-
import {
|
|
26
|
+
import {
|
|
27
|
+
controlServices,
|
|
28
|
+
deployRelease,
|
|
29
|
+
readStatus,
|
|
30
|
+
runDeployScript,
|
|
31
|
+
streamLogs,
|
|
32
|
+
} from "./operations"
|
|
26
33
|
import { ensureEnvToken, pickPublicKey, renderAdminScript } from "./setup"
|
|
27
34
|
import { shellQuote } from "./shell"
|
|
28
35
|
import { resolveSshConfig } from "./ssh-config"
|
|
@@ -42,9 +49,14 @@ export interface SshTargetOptions {
|
|
|
42
49
|
* projects share a server, so that each has its own. Defaults: app 3001, api 3002.
|
|
43
50
|
*/
|
|
44
51
|
readonly ports?: Readonly<Partial<Record<DeployHttpServiceName, number>>>
|
|
52
|
+
/**
|
|
53
|
+
* Second local port pair for blue-green cutovers while the active slot keeps serving. Defaults
|
|
54
|
+
* to `ports` plus 10 (app 3011, api 3012).
|
|
55
|
+
*/
|
|
56
|
+
readonly standbyPorts?: Readonly<Partial<Record<DeployHttpServiceName, number>>>
|
|
45
57
|
}
|
|
46
58
|
|
|
47
|
-
const OPTIONS = ["host", "user", "ports"] as const
|
|
59
|
+
const OPTIONS = ["host", "user", "ports", "standbyPorts"] as const
|
|
48
60
|
|
|
49
61
|
const DEFAULT_USER = "sixb"
|
|
50
62
|
|
|
@@ -66,6 +78,7 @@ export class SshTarget implements DeployTarget {
|
|
|
66
78
|
readonly user: string
|
|
67
79
|
readonly location: string
|
|
68
80
|
readonly #ports: Readonly<Record<DeployHttpServiceName, number>>
|
|
81
|
+
readonly #standbyPorts: Readonly<Record<DeployHttpServiceName, number>>
|
|
69
82
|
|
|
70
83
|
constructor(options: SshTargetOptions) {
|
|
71
84
|
if (typeof options !== "object" || options === null) {
|
|
@@ -82,15 +95,32 @@ export class SshTarget implements DeployTarget {
|
|
|
82
95
|
this.host = validateHost(options.host)
|
|
83
96
|
this.user = validateUser(options.user ?? DEFAULT_USER)
|
|
84
97
|
this.location = `${this.user}@${this.host}`
|
|
85
|
-
|
|
98
|
+
const primary = { ...DEFAULT_PORTS, ...validatePorts(options.ports, "ports") }
|
|
99
|
+
const standbyDefaults: Record<DeployHttpServiceName, number> = {
|
|
100
|
+
app: defaultStandbyPort(primary.app),
|
|
101
|
+
api: defaultStandbyPort(primary.api),
|
|
102
|
+
}
|
|
103
|
+
const standby = { ...standbyDefaults, ...validatePorts(options.standbyPorts, "standbyPorts") }
|
|
104
|
+
assertDistinctPorts(primary, standby)
|
|
105
|
+
this.#ports = primary
|
|
106
|
+
this.#standbyPorts = standby
|
|
86
107
|
}
|
|
87
108
|
|
|
88
109
|
listenAddress(service: DeployHttpServiceName): DeployListenAddress {
|
|
89
110
|
return { host: LISTEN_HOST, port: this.#ports[service] }
|
|
90
111
|
}
|
|
91
112
|
|
|
113
|
+
standbyListenAddress(service: DeployHttpServiceName): DeployListenAddress {
|
|
114
|
+
return { host: LISTEN_HOST, port: this.#standbyPorts[service] }
|
|
115
|
+
}
|
|
116
|
+
|
|
92
117
|
deploy(release: DeployRelease, source: DeploySource, context: DeployContext): Promise<void> {
|
|
93
|
-
return this.#connected((shell) =>
|
|
118
|
+
return this.#connected((shell) =>
|
|
119
|
+
deployRelease(shell, this.user, release, source, context, {
|
|
120
|
+
blue: this.#ports,
|
|
121
|
+
green: this.#standbyPorts,
|
|
122
|
+
})
|
|
123
|
+
)
|
|
94
124
|
}
|
|
95
125
|
|
|
96
126
|
status(context: DeployOperationContext): Promise<DeployStatus> {
|
|
@@ -111,6 +141,10 @@ export class SshTarget implements DeployTarget {
|
|
|
111
141
|
return this.#connected((shell) => controlServices(shell, context.name, action, service))
|
|
112
142
|
}
|
|
113
143
|
|
|
144
|
+
run(script: DeployScript, context: DeployOperationContext): Promise<number> {
|
|
145
|
+
return this.#connected((shell) => runDeployScript(shell, context.name, script))
|
|
146
|
+
}
|
|
147
|
+
|
|
114
148
|
async check(release: DeployRelease, context: DeployCheckContext): Promise<DeployCheck[]> {
|
|
115
149
|
const { hostname } = await resolveSshConfig(this.location)
|
|
116
150
|
const dns = checkDns(release, hostname)
|
|
@@ -121,9 +155,13 @@ export class SshTarget implements DeployTarget {
|
|
|
121
155
|
...(await runChecks(shell, {
|
|
122
156
|
name: context.name,
|
|
123
157
|
projectPath: context.projectPath,
|
|
124
|
-
ports: release.services.flatMap((service) =>
|
|
125
|
-
service.http
|
|
126
|
-
|
|
158
|
+
ports: release.services.flatMap((service) => {
|
|
159
|
+
if (!service.http || (service.name !== "api" && service.name !== "app")) return []
|
|
160
|
+
return [
|
|
161
|
+
{ service: service.name, port: this.#ports[service.name] },
|
|
162
|
+
{ service: `${service.name}-standby`, port: this.#standbyPorts[service.name] },
|
|
163
|
+
]
|
|
164
|
+
}),
|
|
127
165
|
envFromSource: release.steps.env !== undefined,
|
|
128
166
|
location: this.location,
|
|
129
167
|
})),
|
|
@@ -170,8 +208,11 @@ export class SshTarget implements DeployTarget {
|
|
|
170
208
|
await this.#connected((shell) =>
|
|
171
209
|
shell.run(
|
|
172
210
|
[
|
|
211
|
+
`root="$HOME"/${shellQuote(context.name)}`,
|
|
173
212
|
`project="$HOME"/${shellQuote(`${context.name}/code/${context.projectPath}`)}`,
|
|
174
|
-
'mkdir -p "$
|
|
213
|
+
'mkdir -p "$root/shared"',
|
|
214
|
+
'if [ ! -L "$root/code" ]; then mkdir -p "$project"; fi',
|
|
215
|
+
'if [ -f "$root/shared/.env" ]; then chmod 600 "$root/shared/.env"; fi',
|
|
175
216
|
'if [ -f "$project/.env" ]; then chmod 600 "$project/.env"; fi',
|
|
176
217
|
`token="$HOME"/${shellQuote(`${context.name}/${ENV_TOKEN_FILE}`)}`,
|
|
177
218
|
'if [ -f "$token" ]; then chmod 600 "$token"; fi',
|
|
@@ -252,6 +293,32 @@ export class SshTarget implements DeployTarget {
|
|
|
252
293
|
}
|
|
253
294
|
}
|
|
254
295
|
|
|
296
|
+
function defaultStandbyPort(port: number): number {
|
|
297
|
+
return port + 10 <= 65_535 ? port + 10 : port - 10
|
|
298
|
+
}
|
|
299
|
+
|
|
300
|
+
function assertDistinctPorts(
|
|
301
|
+
primary: Readonly<Record<DeployHttpServiceName, number>>,
|
|
302
|
+
standby: Readonly<Record<DeployHttpServiceName, number>>
|
|
303
|
+
): void {
|
|
304
|
+
const entries: readonly [string, number][] = [
|
|
305
|
+
["ports.app", primary.app],
|
|
306
|
+
["ports.api", primary.api],
|
|
307
|
+
["standbyPorts.app", standby.app],
|
|
308
|
+
["standbyPorts.api", standby.api],
|
|
309
|
+
]
|
|
310
|
+
const seen = new Map<number, string>()
|
|
311
|
+
for (const [label, port] of entries) {
|
|
312
|
+
const previous = seen.get(port)
|
|
313
|
+
if (previous) {
|
|
314
|
+
throw new Error(
|
|
315
|
+
`[SshTarget] ${label} (${port}) conflicts with ${previous}. Give each HTTP service and slot its own port.`
|
|
316
|
+
)
|
|
317
|
+
}
|
|
318
|
+
seen.set(port, label)
|
|
319
|
+
}
|
|
320
|
+
}
|
|
321
|
+
|
|
255
322
|
function validateHost(value: unknown): string {
|
|
256
323
|
if (typeof value !== "string" || value.trim() === "") {
|
|
257
324
|
throw new Error("[SshTarget] host must be the server's hostname or IP address.")
|
|
@@ -291,20 +358,25 @@ function validateUser(value: unknown): string {
|
|
|
291
358
|
return value
|
|
292
359
|
}
|
|
293
360
|
|
|
294
|
-
function validatePorts(
|
|
361
|
+
function validatePorts(
|
|
362
|
+
value: unknown,
|
|
363
|
+
field: "ports" | "standbyPorts"
|
|
364
|
+
): Partial<Record<DeployHttpServiceName, number>> {
|
|
295
365
|
if (value === undefined) return {}
|
|
296
366
|
if (typeof value !== "object" || value === null || Array.isArray(value)) {
|
|
297
|
-
throw new Error(
|
|
367
|
+
throw new Error(`[SshTarget] ${field} must be an object, such as \`{ api: 3012 }\`.`)
|
|
298
368
|
}
|
|
299
369
|
|
|
300
370
|
const ports: Partial<Record<DeployHttpServiceName, number>> = {}
|
|
301
371
|
for (const [service, port] of Object.entries(value)) {
|
|
302
372
|
if (!Object.hasOwn(DEFAULT_PORTS, service)) {
|
|
303
|
-
throw new Error(
|
|
373
|
+
throw new Error(
|
|
374
|
+
`[SshTarget] ${field}.${service} is not an HTTP service. Available: api, app.`
|
|
375
|
+
)
|
|
304
376
|
}
|
|
305
377
|
// A deploy user has no privileges, and ports below 1024 need them.
|
|
306
378
|
if (!Number.isInteger(port) || port < 1024 || port > 65_535) {
|
|
307
|
-
throw new Error(`[SshTarget]
|
|
379
|
+
throw new Error(`[SshTarget] ${field}.${service} must be a port from 1024 to 65535.`)
|
|
308
380
|
}
|
|
309
381
|
ports[service as DeployHttpServiceName] = port
|
|
310
382
|
}
|
package/src/transport.ts
CHANGED
|
@@ -111,7 +111,7 @@ export class LocalShell implements RemoteShell {
|
|
|
111
111
|
constructor(private readonly env: Readonly<Record<string, string>> = {}) {}
|
|
112
112
|
|
|
113
113
|
async run(script: string, options: RunOptions = {}): Promise<void> {
|
|
114
|
-
const inline = options.stdin !== undefined
|
|
114
|
+
const inline = options.stdin !== undefined || options.terminal === true
|
|
115
115
|
await runProcess(inline ? ["bash", "-c", script] : ["bash", "-s"], {
|
|
116
116
|
...options,
|
|
117
117
|
...(inline ? {} : { stdin: new Blob([script]).stream() }),
|
|
@@ -127,7 +127,10 @@ async function runProcess(
|
|
|
127
127
|
options: RunOptions & { readonly env?: Readonly<Record<string, string>> }
|
|
128
128
|
): Promise<void> {
|
|
129
129
|
if (options.terminal) {
|
|
130
|
-
const child = Bun.spawn([...command], {
|
|
130
|
+
const child = Bun.spawn([...command], {
|
|
131
|
+
stdio: ["inherit", "inherit", "inherit"],
|
|
132
|
+
env: { ...process.env, ...options.env },
|
|
133
|
+
})
|
|
131
134
|
const exitCode = await child.exited
|
|
132
135
|
if (exitCode !== 0) throw new RemoteScriptError(`exited with code ${exitCode}`, exitCode)
|
|
133
136
|
return
|