@sixb/deploy-ssh 0.2.5 → 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/src/ssh-target.ts CHANGED
@@ -49,9 +49,14 @@ export interface SshTargetOptions {
49
49
  * projects share a server, so that each has its own. Defaults: app 3001, api 3002.
50
50
  */
51
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>>>
52
57
  }
53
58
 
54
- const OPTIONS = ["host", "user", "ports"] as const
59
+ const OPTIONS = ["host", "user", "ports", "standbyPorts"] as const
55
60
 
56
61
  const DEFAULT_USER = "sixb"
57
62
 
@@ -73,6 +78,7 @@ export class SshTarget implements DeployTarget {
73
78
  readonly user: string
74
79
  readonly location: string
75
80
  readonly #ports: Readonly<Record<DeployHttpServiceName, number>>
81
+ readonly #standbyPorts: Readonly<Record<DeployHttpServiceName, number>>
76
82
 
77
83
  constructor(options: SshTargetOptions) {
78
84
  if (typeof options !== "object" || options === null) {
@@ -89,15 +95,32 @@ export class SshTarget implements DeployTarget {
89
95
  this.host = validateHost(options.host)
90
96
  this.user = validateUser(options.user ?? DEFAULT_USER)
91
97
  this.location = `${this.user}@${this.host}`
92
- this.#ports = { ...DEFAULT_PORTS, ...validatePorts(options.ports) }
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
93
107
  }
94
108
 
95
109
  listenAddress(service: DeployHttpServiceName): DeployListenAddress {
96
110
  return { host: LISTEN_HOST, port: this.#ports[service] }
97
111
  }
98
112
 
113
+ standbyListenAddress(service: DeployHttpServiceName): DeployListenAddress {
114
+ return { host: LISTEN_HOST, port: this.#standbyPorts[service] }
115
+ }
116
+
99
117
  deploy(release: DeployRelease, source: DeploySource, context: DeployContext): Promise<void> {
100
- return this.#connected((shell) => deployRelease(shell, this.user, release, source, context))
118
+ return this.#connected((shell) =>
119
+ deployRelease(shell, this.user, release, source, context, {
120
+ blue: this.#ports,
121
+ green: this.#standbyPorts,
122
+ })
123
+ )
101
124
  }
102
125
 
103
126
  status(context: DeployOperationContext): Promise<DeployStatus> {
@@ -132,9 +155,13 @@ export class SshTarget implements DeployTarget {
132
155
  ...(await runChecks(shell, {
133
156
  name: context.name,
134
157
  projectPath: context.projectPath,
135
- ports: release.services.flatMap((service) =>
136
- service.http ? [{ service: service.name, port: service.http.port }] : []
137
- ),
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
+ }),
138
165
  envFromSource: release.steps.env !== undefined,
139
166
  location: this.location,
140
167
  })),
@@ -181,8 +208,11 @@ export class SshTarget implements DeployTarget {
181
208
  await this.#connected((shell) =>
182
209
  shell.run(
183
210
  [
211
+ `root="$HOME"/${shellQuote(context.name)}`,
184
212
  `project="$HOME"/${shellQuote(`${context.name}/code/${context.projectPath}`)}`,
185
- 'mkdir -p "$project"',
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',
186
216
  'if [ -f "$project/.env" ]; then chmod 600 "$project/.env"; fi',
187
217
  `token="$HOME"/${shellQuote(`${context.name}/${ENV_TOKEN_FILE}`)}`,
188
218
  'if [ -f "$token" ]; then chmod 600 "$token"; fi',
@@ -263,6 +293,32 @@ export class SshTarget implements DeployTarget {
263
293
  }
264
294
  }
265
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
+
266
322
  function validateHost(value: unknown): string {
267
323
  if (typeof value !== "string" || value.trim() === "") {
268
324
  throw new Error("[SshTarget] host must be the server's hostname or IP address.")
@@ -302,20 +358,25 @@ function validateUser(value: unknown): string {
302
358
  return value
303
359
  }
304
360
 
305
- function validatePorts(value: unknown): Partial<Record<DeployHttpServiceName, number>> {
361
+ function validatePorts(
362
+ value: unknown,
363
+ field: "ports" | "standbyPorts"
364
+ ): Partial<Record<DeployHttpServiceName, number>> {
306
365
  if (value === undefined) return {}
307
366
  if (typeof value !== "object" || value === null || Array.isArray(value)) {
308
- throw new Error("[SshTarget] ports must be an object, such as `{ api: 3012 }`.")
367
+ throw new Error(`[SshTarget] ${field} must be an object, such as \`{ api: 3012 }\`.`)
309
368
  }
310
369
 
311
370
  const ports: Partial<Record<DeployHttpServiceName, number>> = {}
312
371
  for (const [service, port] of Object.entries(value)) {
313
372
  if (!Object.hasOwn(DEFAULT_PORTS, service)) {
314
- throw new Error(`[SshTarget] ports.${service} is not an HTTP service. Available: api, app.`)
373
+ throw new Error(
374
+ `[SshTarget] ${field}.${service} is not an HTTP service. Available: api, app.`
375
+ )
315
376
  }
316
377
  // A deploy user has no privileges, and ports below 1024 need them.
317
378
  if (!Number.isInteger(port) || port < 1024 || port > 65_535) {
318
- throw new Error(`[SshTarget] ports.${service} must be a port from 1024 to 65535.`)
379
+ throw new Error(`[SshTarget] ${field}.${service} must be a port from 1024 to 65535.`)
319
380
  }
320
381
  ports[service as DeployHttpServiceName] = port
321
382
  }