@sixb/deploy-ssh 0.1.0

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 (66) hide show
  1. package/LICENSE +21 -0
  2. package/README.md +74 -0
  3. package/dist/access.d.ts +11 -0
  4. package/dist/access.d.ts.map +1 -0
  5. package/dist/check.d.ts +28 -0
  6. package/dist/check.d.ts.map +1 -0
  7. package/dist/ci.d.ts +27 -0
  8. package/dist/ci.d.ts.map +1 -0
  9. package/dist/index.d.ts +3 -0
  10. package/dist/index.d.ts.map +1 -0
  11. package/dist/index.js +1415 -0
  12. package/dist/index.js.map +24 -0
  13. package/dist/keys.d.ts +13 -0
  14. package/dist/keys.d.ts.map +1 -0
  15. package/dist/layout.d.ts +38 -0
  16. package/dist/layout.d.ts.map +1 -0
  17. package/dist/operations.d.ts +10 -0
  18. package/dist/operations.d.ts.map +1 -0
  19. package/dist/render.d.ts +15 -0
  20. package/dist/render.d.ts.map +1 -0
  21. package/dist/scripts.d.ts +37 -0
  22. package/dist/scripts.d.ts.map +1 -0
  23. package/dist/server/cli.d.ts +3 -0
  24. package/dist/server/cli.d.ts.map +1 -0
  25. package/dist/server/logs.d.ts +25 -0
  26. package/dist/server/logs.d.ts.map +1 -0
  27. package/dist/server/manifest.d.ts +30 -0
  28. package/dist/server/manifest.d.ts.map +1 -0
  29. package/dist/server/state.d.ts +32 -0
  30. package/dist/server/state.d.ts.map +1 -0
  31. package/dist/server/supervisor.d.ts +16 -0
  32. package/dist/server/supervisor.d.ts.map +1 -0
  33. package/dist/setup.d.ts +25 -0
  34. package/dist/setup.d.ts.map +1 -0
  35. package/dist/shell.d.ts +5 -0
  36. package/dist/shell.d.ts.map +1 -0
  37. package/dist/ssh-config.d.ts +12 -0
  38. package/dist/ssh-config.d.ts.map +1 -0
  39. package/dist/ssh-target.d.ts +39 -0
  40. package/dist/ssh-target.d.ts.map +1 -0
  41. package/dist/transport.d.ts +45 -0
  42. package/dist/transport.d.ts.map +1 -0
  43. package/dist/tsconfig.json +5 -0
  44. package/dist/update-page.d.ts +6 -0
  45. package/dist/update-page.d.ts.map +1 -0
  46. package/package.json +61 -0
  47. package/src/access.ts +107 -0
  48. package/src/check.ts +215 -0
  49. package/src/ci.ts +88 -0
  50. package/src/index.ts +2 -0
  51. package/src/keys.ts +57 -0
  52. package/src/layout.ts +69 -0
  53. package/src/operations.ts +158 -0
  54. package/src/render.ts +129 -0
  55. package/src/scripts.ts +305 -0
  56. package/src/server/cli.ts +157 -0
  57. package/src/server/logs.ts +180 -0
  58. package/src/server/manifest.ts +112 -0
  59. package/src/server/state.ts +81 -0
  60. package/src/server/supervisor.ts +542 -0
  61. package/src/setup.ts +166 -0
  62. package/src/shell.ts +10 -0
  63. package/src/ssh-config.ts +35 -0
  64. package/src/ssh-target.ts +347 -0
  65. package/src/transport.ts +211 -0
  66. package/src/update-page.ts +44 -0
@@ -0,0 +1,112 @@
1
+ import { readFile } from "node:fs/promises"
2
+ import { resolve } from "node:path"
3
+
4
+ /** What the supervisor runs for one deployment, written by `sixb deploy` on each deploy. */
5
+ export interface ProcessManifest {
6
+ readonly version: 1
7
+ readonly name: string
8
+ /** The project directory every process starts in. */
9
+ readonly cwd: string
10
+ /** Supervisor state, control requests, and logs live here. */
11
+ readonly runDir: string
12
+ readonly logDir: string
13
+ /** Written by the deploy; read back by `status`. */
14
+ readonly releasePath: string
15
+ readonly services: readonly ProcessServiceManifest[]
16
+ }
17
+
18
+ export interface ProcessServiceManifest {
19
+ readonly name: string
20
+ readonly processName: string
21
+ readonly command: string
22
+ readonly args: readonly string[]
23
+ readonly env: Readonly<Record<string, string>>
24
+ readonly instances: number
25
+ readonly killTimeoutMs: number
26
+ readonly restartDelayMs: number
27
+ readonly maxMemory?: string
28
+ }
29
+
30
+ export async function loadProcessManifest(path: string): Promise<ProcessManifest> {
31
+ return parseProcessManifest(JSON.parse(await readFile(path, "utf8")), path)
32
+ }
33
+
34
+ export function statePath(manifest: ProcessManifest): string {
35
+ return resolve(manifest.runDir, "state.json")
36
+ }
37
+
38
+ export function controlDir(manifest: ProcessManifest): string {
39
+ return resolve(manifest.runDir, "control")
40
+ }
41
+
42
+ export function responseDir(manifest: ProcessManifest): string {
43
+ return resolve(manifest.runDir, "responses")
44
+ }
45
+
46
+ export function parseProcessManifest(value: unknown, source = "process manifest"): ProcessManifest {
47
+ const manifest = record(value, source)
48
+ if (manifest.version !== 1) throw new Error(`[SshTarget] ${source}.version must be 1`)
49
+ if (!Array.isArray(manifest.services)) {
50
+ throw new Error(`[SshTarget] ${source}.services must be an array`)
51
+ }
52
+
53
+ return {
54
+ version: 1,
55
+ name: string(manifest.name, `${source}.name`),
56
+ cwd: string(manifest.cwd, `${source}.cwd`),
57
+ runDir: string(manifest.runDir, `${source}.runDir`),
58
+ logDir: string(manifest.logDir, `${source}.logDir`),
59
+ releasePath: string(manifest.releasePath, `${source}.releasePath`),
60
+ services: manifest.services.map((service, index) =>
61
+ parseService(service, `${source}.services[${index}]`)
62
+ ),
63
+ }
64
+ }
65
+
66
+ function parseService(value: unknown, source: string): ProcessServiceManifest {
67
+ const service = record(value, source)
68
+ if (!Array.isArray(service.args) || service.args.some((arg) => typeof arg !== "string")) {
69
+ throw new Error(`[SshTarget] ${source}.args must be an array of strings`)
70
+ }
71
+ const env = record(service.env, `${source}.env`)
72
+ for (const [key, variable] of Object.entries(env)) {
73
+ if (typeof variable !== "string") {
74
+ throw new Error(`[SshTarget] ${source}.env.${key} must be a string`)
75
+ }
76
+ }
77
+
78
+ return {
79
+ name: string(service.name, `${source}.name`),
80
+ processName: string(service.processName, `${source}.processName`),
81
+ command: string(service.command, `${source}.command`),
82
+ args: service.args,
83
+ env: env as Record<string, string>,
84
+ instances: positiveInteger(service.instances, `${source}.instances`),
85
+ killTimeoutMs: positiveInteger(service.killTimeoutMs, `${source}.killTimeoutMs`),
86
+ restartDelayMs: positiveInteger(service.restartDelayMs, `${source}.restartDelayMs`),
87
+ ...(service.maxMemory === undefined
88
+ ? {}
89
+ : { maxMemory: string(service.maxMemory, `${source}.maxMemory`) }),
90
+ }
91
+ }
92
+
93
+ function record(value: unknown, source: string): Record<string, unknown> {
94
+ if (typeof value !== "object" || value === null || Array.isArray(value)) {
95
+ throw new Error(`[SshTarget] ${source} must be an object`)
96
+ }
97
+ return value as Record<string, unknown>
98
+ }
99
+
100
+ function string(value: unknown, source: string): string {
101
+ if (typeof value !== "string" || value.trim() === "") {
102
+ throw new Error(`[SshTarget] ${source} must be a non-empty string`)
103
+ }
104
+ return value
105
+ }
106
+
107
+ function positiveInteger(value: unknown, source: string): number {
108
+ if (!Number.isInteger(value) || (value as number) < 1) {
109
+ throw new Error(`[SshTarget] ${source} must be a positive integer`)
110
+ }
111
+ return value as number
112
+ }
@@ -0,0 +1,81 @@
1
+ import { readFile, rename, writeFile } from "node:fs/promises"
2
+
3
+ export type ProcessInstanceStatus = "starting" | "running" | "stopping" | "stopped" | "exited"
4
+
5
+ /** What the supervisor last knew about every process, rewritten on each change. */
6
+ export interface ProcessState {
7
+ readonly version: 1
8
+ readonly name: string
9
+ readonly supervisorPid: number
10
+ readonly updatedAt: string
11
+ readonly instances: readonly ProcessInstanceState[]
12
+ }
13
+
14
+ export interface ProcessInstanceState {
15
+ readonly service: string
16
+ readonly processName: string
17
+ readonly instance: number
18
+ readonly pid?: number
19
+ readonly status: ProcessInstanceStatus
20
+ readonly startedAt?: string
21
+ readonly stoppedAt?: string
22
+ readonly restarts: number
23
+ readonly cpuPercent?: number
24
+ readonly memoryBytes?: number
25
+ readonly lastExitCode?: number | null
26
+ readonly lastSignal?: string | null
27
+ readonly lastError?: string
28
+ readonly logPath: string
29
+ }
30
+
31
+ const STATUSES: readonly ProcessInstanceStatus[] = [
32
+ "starting",
33
+ "running",
34
+ "stopping",
35
+ "stopped",
36
+ "exited",
37
+ ]
38
+
39
+ export async function readProcessState(path: string): Promise<ProcessState | null> {
40
+ let raw: string
41
+ try {
42
+ raw = await readFile(path, "utf8")
43
+ } catch (error) {
44
+ if (isMissing(error)) return null
45
+ throw error
46
+ }
47
+ return parseProcessState(JSON.parse(raw), path)
48
+ }
49
+
50
+ /** Writes through a rename, so a reader never sees half a file. */
51
+ export async function writeProcessState(path: string, state: ProcessState): Promise<void> {
52
+ const temporary = `${path}.${process.pid}.tmp`
53
+ await writeFile(temporary, `${JSON.stringify(state, null, 2)}\n`, "utf8")
54
+ await rename(temporary, path)
55
+ }
56
+
57
+ export function parseProcessState(value: unknown, source = "process state"): ProcessState {
58
+ const state = value as Partial<ProcessState> | null
59
+ if (!state || state.version !== 1 || !Array.isArray(state.instances)) {
60
+ throw new Error(`[SshTarget] ${source} is not a supervisor state file`)
61
+ }
62
+ for (const instance of state.instances) {
63
+ if (!STATUSES.includes(instance.status)) {
64
+ throw new Error(`[SshTarget] ${source} has an instance with status '${instance.status}'`)
65
+ }
66
+ }
67
+ return state as ProcessState
68
+ }
69
+
70
+ export function isPidRunning(pid: number): boolean {
71
+ try {
72
+ process.kill(pid, 0)
73
+ return true
74
+ } catch {
75
+ return false
76
+ }
77
+ }
78
+
79
+ export function isMissing(error: unknown): boolean {
80
+ return error instanceof Error && "code" in error && error.code === "ENOENT"
81
+ }