@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/server/cli.ts CHANGED
@@ -12,10 +12,12 @@ import { runSupervisor, type SupervisorCommand, sendSupervisorCommand } from "./
12
12
  * script, which pins the Bun version and manifest. `sixb deploy` reaches it over SSH.
13
13
  *
14
14
  * supervise run the deployment's processes (the systemd unit's command)
15
- * status [--json] [--check] --check exits 1 unless every process is running
15
+ * status [--json] [--check] [--candidate]
16
16
  * logs [service] [--tail n] [--follow]
17
17
  * restart|start|stop [service]
18
- * run <staged> <path> [args...] run an uploaded script with `sixb run`; see ./run.ts
18
+ * stage <candidate-manifest>
19
+ * activate|discard
20
+ * run <staged> <path> [args...] run an uploaded script with `sixb run`; see ./run.ts
19
21
  */
20
22
  const argv = process.argv.slice(2)
21
23
 
@@ -40,7 +42,7 @@ async function main(): Promise<void> {
40
42
 
41
43
  const manifestPath = option("--manifest")
42
44
  if (!manifestPath) throw new Error("[SshTarget] --manifest is required.")
43
- const [command, service] = positionals()
45
+ const [command, arg] = positionals()
44
46
 
45
47
  switch (command) {
46
48
  case "supervise":
@@ -50,7 +52,7 @@ async function main(): Promise<void> {
50
52
  case "status":
51
53
  return status(await loadProcessManifest(manifestPath))
52
54
  case "logs":
53
- return logs(await loadProcessManifest(manifestPath), service)
55
+ return logs(await loadProcessManifest(manifestPath), arg)
54
56
  case "restart":
55
57
  case "start":
56
58
  case "stop":
@@ -58,10 +60,22 @@ async function main(): Promise<void> {
58
60
  await sendSupervisorCommand(
59
61
  await loadProcessManifest(manifestPath),
60
62
  command satisfies SupervisorCommand,
61
- service
63
+ { service: arg }
62
64
  )
63
65
  )
64
66
  return
67
+ case "stage":
68
+ if (!arg) throw new Error("[SshTarget] Usage: stage <candidate-manifest> --manifest <path>")
69
+ console.log(
70
+ await sendSupervisorCommand(await loadProcessManifest(manifestPath), "stage", {
71
+ manifestPath: arg,
72
+ })
73
+ )
74
+ return
75
+ case "activate":
76
+ case "discard":
77
+ console.log(await sendSupervisorCommand(await loadProcessManifest(manifestPath), command))
78
+ return
65
79
  default:
66
80
  throw new Error(`[SshTarget] Unknown command '${command ?? ""}'.`)
67
81
  }
@@ -70,11 +84,12 @@ async function main(): Promise<void> {
70
84
  async function status(manifest: ProcessManifest): Promise<void> {
71
85
  const state = await readProcessState(statePath(manifest))
72
86
  const running = state !== null && isPidRunning(state.supervisorPid)
87
+ const sourceInstances = flag("--candidate") ? (state?.staged ?? []) : (state?.instances ?? [])
73
88
  const report: DeployStatus = {
74
89
  release: await readRelease(manifest.releasePath),
75
90
  running,
76
91
  processes: running
77
- ? state.instances.map((instance) => ({
92
+ ? sourceInstances.map((instance) => ({
78
93
  service: instance.service,
79
94
  instance: instance.instance,
80
95
  status: instance.status,
@@ -94,13 +109,19 @@ async function status(manifest: ProcessManifest): Promise<void> {
94
109
  (process) => process.status !== "running" || process.restarts > 0
95
110
  )
96
111
  if (!running) console.error("The supervisor is not running.")
112
+ else if (flag("--candidate") && report.processes.length === 0) {
113
+ console.error("No candidate processes are staged.")
114
+ }
97
115
  for (const process of down) {
98
116
  const label =
99
117
  process.instance === 0 ? process.service : `${process.service}#${process.instance}`
100
- const state = process.status === "running" ? `restarted ${process.restarts}x` : process.status
101
- console.error(`${label} ${state}${process.lastError ? `: ${process.lastError}` : ""}`)
118
+ const stateText =
119
+ process.status === "running" ? `restarted ${process.restarts}x` : process.status
120
+ console.error(`${label} ${stateText}${process.lastError ? `: ${process.lastError}` : ""}`)
121
+ }
122
+ if (!running || down.length > 0 || (flag("--candidate") && report.processes.length === 0)) {
123
+ process.exit(1)
102
124
  }
103
- if (!running || down.length > 0) process.exit(1)
104
125
  return
105
126
  }
106
127
  console.log(JSON.stringify(report))
@@ -1,10 +1,15 @@
1
1
  import { readFile } from "node:fs/promises"
2
2
  import { resolve } from "node:path"
3
+ import type { DeploySlot } from "../layout"
4
+
5
+ export type ProcessServiceKind = "http" | "singleton" | "workers" | "script"
3
6
 
4
7
  /** What the supervisor runs for one deployment, written by `sixb deploy` on each deploy. */
5
8
  export interface ProcessManifest {
6
9
  readonly version: 1
7
10
  readonly name: string
11
+ /** Which port pair (`blue` or `green`) this release's HTTP services bind. */
12
+ readonly slot?: DeploySlot
8
13
  /** The project directory every process starts in. */
9
14
  readonly cwd: string
10
15
  /** Supervisor state, control requests, and logs live here. */
@@ -22,6 +27,7 @@ export interface ProcessManifest {
22
27
 
23
28
  export interface ProcessServiceManifest {
24
29
  readonly name: string
30
+ readonly kind?: ProcessServiceKind
25
31
  readonly processName: string
26
32
  readonly command: string
27
33
  readonly args: readonly string[]
@@ -32,6 +38,13 @@ export interface ProcessServiceManifest {
32
38
  readonly maxMemory?: string
33
39
  }
34
40
 
41
+ const SERVICE_KINDS: readonly ProcessServiceKind[] = ["http", "singleton", "workers", "script"]
42
+
43
+ export function isOverlapSafeService(service: ProcessServiceManifest): boolean {
44
+ if (service.kind) return service.kind === "http" || service.kind === "workers"
45
+ return service.name === "api" || service.name === "app" || service.name === "workers"
46
+ }
47
+
35
48
  export async function loadProcessManifest(path: string): Promise<ProcessManifest> {
36
49
  return parseProcessManifest(JSON.parse(await readFile(path, "utf8")), path)
37
50
  }
@@ -54,10 +67,12 @@ export function parseProcessManifest(value: unknown, source = "process manifest"
54
67
  if (!Array.isArray(manifest.services)) {
55
68
  throw new Error(`[SshTarget] ${source}.services must be an array`)
56
69
  }
70
+ const slot = parseSlot(manifest.slot, `${source}.slot`)
57
71
 
58
72
  return {
59
73
  version: 1,
60
74
  name: string(manifest.name, `${source}.name`),
75
+ ...(slot === undefined ? {} : { slot }),
61
76
  cwd: string(manifest.cwd, `${source}.cwd`),
62
77
  runDir: string(manifest.runDir, `${source}.runDir`),
63
78
  logDir: string(manifest.logDir, `${source}.logDir`),
@@ -75,9 +90,11 @@ function parseService(value: unknown, source: string): ProcessServiceManifest {
75
90
  throw new Error(`[SshTarget] ${source}.args must be an array of strings`)
76
91
  }
77
92
  const env = environment(service.env, `${source}.env`)
93
+ const kind = parseKind(service.kind, `${source}.kind`)
78
94
 
79
95
  return {
80
96
  name: string(service.name, `${source}.name`),
97
+ ...(kind === undefined ? {} : { kind }),
81
98
  processName: string(service.processName, `${source}.processName`),
82
99
  command: string(service.command, `${source}.command`),
83
100
  args: service.args,
@@ -91,6 +108,20 @@ function parseService(value: unknown, source: string): ProcessServiceManifest {
91
108
  }
92
109
  }
93
110
 
111
+ function parseSlot(value: unknown, source: string): DeploySlot | undefined {
112
+ if (value === undefined) return undefined
113
+ if (value === "blue" || value === "green") return value
114
+ throw new Error(`[SshTarget] ${source} must be 'blue' or 'green'`)
115
+ }
116
+
117
+ function parseKind(value: unknown, source: string): ProcessServiceKind | undefined {
118
+ if (value === undefined) return undefined
119
+ if (typeof value === "string" && (SERVICE_KINDS as readonly string[]).includes(value)) {
120
+ return value as ProcessServiceKind
121
+ }
122
+ throw new Error(`[SshTarget] ${source} must be one of: ${SERVICE_KINDS.join(", ")}`)
123
+ }
124
+
94
125
  function environment(value: unknown, source: string): Record<string, string> {
95
126
  const env = record(value, source)
96
127
  for (const [key, variable] of Object.entries(env)) {
@@ -114,7 +145,7 @@ function string(value: unknown, source: string): string {
114
145
  return value
115
146
  }
116
147
 
117
- function positiveInteger(value: unknown, source: string): number {
148
+ function positiveInteger(value: unknown, source: number | string): number {
118
149
  if (!Number.isInteger(value) || (value as number) < 1) {
119
150
  throw new Error(`[SshTarget] ${source} must be a positive integer`)
120
151
  }
@@ -1,4 +1,5 @@
1
1
  import { readFile, rename, writeFile } from "node:fs/promises"
2
+ import type { DeploySlot } from "../layout"
2
3
 
3
4
  export type ProcessInstanceStatus = "starting" | "running" | "stopping" | "stopped" | "exited"
4
5
 
@@ -8,7 +9,12 @@ export interface ProcessState {
8
9
  readonly name: string
9
10
  readonly supervisorPid: number
10
11
  readonly updatedAt: string
12
+ readonly slot?: DeploySlot
11
13
  readonly instances: readonly ProcessInstanceState[]
14
+ /** Overlap-safe instances of a candidate release waiting to cut over. */
15
+ readonly staged?: readonly ProcessInstanceState[]
16
+ /** Release `cwd`s still held by background-draining processes after a cutover. */
17
+ readonly retiringCwds?: readonly string[]
12
18
  }
13
19
 
14
20
  export interface ProcessInstanceState {
@@ -66,7 +72,7 @@ export function parseProcessState(value: unknown, source = "process state"): Pro
66
72
  if (!state || state.version !== 1 || !Array.isArray(state.instances)) {
67
73
  throw new Error(`[SshTarget] ${source} is not a supervisor state file`)
68
74
  }
69
- for (const instance of state.instances) {
75
+ for (const instance of [...state.instances, ...(state.staged ?? [])]) {
70
76
  if (!STATUSES.includes(instance.status)) {
71
77
  throw new Error(`[SshTarget] ${source} has an instance with status '${instance.status}'`)
72
78
  }
@@ -84,5 +90,5 @@ export function isPidRunning(pid: number): boolean {
84
90
  }
85
91
 
86
92
  export function isMissing(error: unknown): boolean {
87
- return error instanceof Error && "code" in error && error.code === "ENOENT"
93
+ return (error as NodeJS.ErrnoException | null)?.code === "ENOENT"
88
94
  }