@sixb/deploy-ssh 0.1.4 → 0.2.5

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.
@@ -0,0 +1,246 @@
1
+ import { createHash } from "node:crypto"
2
+ import { mkdir, readdir, readFile, realpath, rm, rmdir, stat, writeFile } from "node:fs/promises"
3
+ import { constants } from "node:os"
4
+ import { dirname, join, relative, resolve } from "node:path"
5
+ import type { ProcessManifest } from "./manifest"
6
+ import { isPidRunning } from "./state"
7
+
8
+ /**
9
+ * `ctl run <staged> <path> [args...]`: puts an uploaded script at `path` in the deployed project and
10
+ * runs `sixb run` on it there, so its imports resolve against the deployed code, with the
11
+ * environment the deployment's services start from. The caller holds the deploy lock shared for
12
+ * the whole run, so no deploy replaces that code or migrates its storage underneath. Resolves with
13
+ * the exit code to end with.
14
+ *
15
+ * absent write it, and delete it afterwards
16
+ * identical run the deployed file in place
17
+ * different refuse: the deployed file is someone's committed work
18
+ *
19
+ * One run per path at a time: a second run of the same new file would find the first one's copy,
20
+ * and the first one's cleanup would delete it underneath the second. Each run marks its path, and
21
+ * whether it wrote the file, so a later run removes what a killed one left in the deployed code.
22
+ */
23
+ export async function runScript(input: {
24
+ readonly manifest: ProcessManifest
25
+ readonly staged: string
26
+ readonly path: string
27
+ readonly args: readonly string[]
28
+ }): Promise<number> {
29
+ const { manifest, staged, path } = input
30
+ try {
31
+ assertProjectPath(path)
32
+ if (!manifest.env) {
33
+ throw new Error(
34
+ `[SshTarget] ${manifest.name} was deployed before sixb run could run scripts on it. Deploy it again.`
35
+ )
36
+ }
37
+ const project = manifest.cwd
38
+ const marks = join(manifest.runDir, "scripts")
39
+ await removeAbandoned(marks, project)
40
+ const mark = await claimPath(marks, path)
41
+ try {
42
+ const target = resolve(project, path)
43
+ const created = await placeScript(project, target, path, await readFile(staged))
44
+ if (created) await mark.wrote(await stat(target))
45
+ try {
46
+ return await runSixb(project, path, input.args, manifest.env)
47
+ } finally {
48
+ if (created) await removeCreated(target, created)
49
+ }
50
+ } finally {
51
+ await mark.release()
52
+ }
53
+ } finally {
54
+ await rm(staged, { force: true })
55
+ }
56
+ }
57
+
58
+ /** A path relative to the project with forward slashes, which `sixb run` records as given. */
59
+ function assertProjectPath(path: string): void {
60
+ if (
61
+ path === "" ||
62
+ path.startsWith("/") ||
63
+ path.includes("\\") ||
64
+ path.includes("\0") ||
65
+ path.split("/").some((segment) => segment === "" || segment === "." || segment === "..")
66
+ ) {
67
+ throw new Error(`[SshTarget] '${path}' is not a path inside the project.`)
68
+ }
69
+ }
70
+
71
+ /** What a run records about its path while it runs. */
72
+ interface Mark {
73
+ readonly pid: number
74
+ readonly path: string
75
+ /** The file the run wrote, so it is the run's to remove while it is still that file. */
76
+ readonly wrote?: { readonly ino: number; readonly mtimeMs: number }
77
+ }
78
+
79
+ /**
80
+ * Marks `path` as running under this process's id until `release`. A mark whose process is gone
81
+ * was left by a run that was killed: `removeAbandoned` clears it first.
82
+ */
83
+ async function claimPath(
84
+ marks: string,
85
+ path: string
86
+ ): Promise<{ wrote(file: Mark["wrote"]): Promise<void>; release(): Promise<void> }> {
87
+ const file = join(marks, `${createHash("sha256").update(path).digest("hex")}.json`)
88
+ const record = (wrote?: Mark["wrote"]) =>
89
+ JSON.stringify({ pid: process.pid, path, ...(wrote ? { wrote } : {}) } satisfies Mark)
90
+ try {
91
+ await writeFile(file, record(), { flag: "wx" })
92
+ } catch (error) {
93
+ if ((error as NodeJS.ErrnoException).code !== "EEXIST") throw error
94
+ throw new Error(`[SshTarget] ${path} is already running on the deployment.`)
95
+ }
96
+ return {
97
+ wrote: (written) =>
98
+ writeFile(file, record(written && { ino: written.ino, mtimeMs: written.mtimeMs })),
99
+ release: () => rm(file, { force: true }),
100
+ }
101
+ }
102
+
103
+ /**
104
+ * Clears the marks of runs whose process is gone, removing each file such a run wrote unless
105
+ * something has replaced it since, such as a deploy of the same path.
106
+ */
107
+ async function removeAbandoned(marks: string, project: string): Promise<void> {
108
+ await mkdir(marks, { recursive: true })
109
+ for (const name of await readdir(marks)) {
110
+ const file = join(marks, name)
111
+ const mark = await readMark(file)
112
+ if (mark && isPidRunning(mark.pid)) continue
113
+ if (mark?.wrote) {
114
+ try {
115
+ assertProjectPath(mark.path)
116
+ const target = resolve(project, mark.path)
117
+ const current = await stat(target)
118
+ if (current.ino === mark.wrote.ino && current.mtimeMs === mark.wrote.mtimeMs) {
119
+ await rm(target, { force: true })
120
+ }
121
+ } catch {
122
+ // Gone already, or a mark this code did not write: nothing to remove.
123
+ }
124
+ }
125
+ await rm(file, { force: true })
126
+ }
127
+ }
128
+
129
+ async function readMark(file: string): Promise<Mark | null> {
130
+ try {
131
+ const mark = JSON.parse(await readFile(file, "utf8")) as Partial<Mark>
132
+ if (typeof mark.pid !== "number" || typeof mark.path !== "string") return null
133
+ const wrote = mark.wrote
134
+ return typeof wrote?.ino === "number" && typeof wrote.mtimeMs === "number"
135
+ ? { pid: mark.pid, path: mark.path, wrote: { ino: wrote.ino, mtimeMs: wrote.mtimeMs } }
136
+ : { pid: mark.pid, path: mark.path }
137
+ } catch {
138
+ return null
139
+ }
140
+ }
141
+
142
+ /** Where the script was written, for cleanup: the first directory it had to make. */
143
+ interface Created {
144
+ readonly firstDirectory: string | null
145
+ }
146
+
147
+ async function placeScript(
148
+ project: string,
149
+ target: string,
150
+ path: string,
151
+ source: Buffer
152
+ ): Promise<Created | null> {
153
+ // Checked before anything is made: a symlinked directory in the deployed code must not carry
154
+ // the script, or the directories made for it, out of the project.
155
+ const existing = await nearestExisting(dirname(target))
156
+ if (relative(await realpath(project), await realpath(existing)).startsWith("..")) {
157
+ throw new Error(`[SshTarget] '${path}' leads outside the deployed project.`)
158
+ }
159
+ const firstDirectory = existing === dirname(target) ? null : firstMissing(target, existing)
160
+ await mkdir(dirname(target), { recursive: true })
161
+
162
+ try {
163
+ await writeFile(target, source, { flag: "wx", mode: 0o600 })
164
+ return { firstDirectory }
165
+ } catch (error) {
166
+ if ((error as NodeJS.ErrnoException).code !== "EEXIST") throw error
167
+ }
168
+
169
+ const deployed = await readFile(target)
170
+ if (!deployed.equals(source)) {
171
+ throw new Error(
172
+ `[SshTarget] The deployed ${path} differs from yours. Rename your copy, or deploy it first.`
173
+ )
174
+ }
175
+ return null
176
+ }
177
+
178
+ async function nearestExisting(directory: string): Promise<string> {
179
+ let current = directory
180
+ while (!(await stat(current).catch(() => null)) && dirname(current) !== current) {
181
+ current = dirname(current)
182
+ }
183
+ return current
184
+ }
185
+
186
+ /** The directory just below `existing` on the way to `target`. */
187
+ function firstMissing(target: string, existing: string): string {
188
+ let current = dirname(target)
189
+ while (dirname(current) !== existing) current = dirname(current)
190
+ return current
191
+ }
192
+
193
+ async function removeCreated(target: string, created: Created): Promise<void> {
194
+ await rm(target, { force: true })
195
+ if (!created.firstDirectory) return
196
+ // Only directories left empty: whatever the script wrote next to itself stays.
197
+ for (let current = dirname(target); ; current = dirname(current)) {
198
+ const removed = await rmdir(current).then(
199
+ () => true,
200
+ () => false
201
+ )
202
+ if (!removed || current === created.firstDirectory) return
203
+ }
204
+ }
205
+
206
+ /**
207
+ * The project's own `sixb run`, on this Bun, with the environment the release's services start
208
+ * from; the project's `.env` fills in the rest, as it does for them. It shares the terminal, so
209
+ * Ctrl-C reaches it from there. A dropped connection hangs up only the session leader, which is
210
+ * this process, so the hangup is passed on, as is a SIGTERM sent here.
211
+ */
212
+ async function runSixb(
213
+ project: string,
214
+ path: string,
215
+ args: readonly string[],
216
+ env: Readonly<Record<string, string>>
217
+ ): Promise<number> {
218
+ const child = Bun.spawn(
219
+ [process.execPath, join(project, "node_modules", ".bin", "sixb"), "run", path, ...args],
220
+ {
221
+ cwd: project,
222
+ stdio: ["inherit", "inherit", "inherit"],
223
+ env: {
224
+ ...process.env,
225
+ ...env,
226
+ PATH: `${dirname(process.execPath)}:${process.env.PATH ?? ""}`,
227
+ },
228
+ }
229
+ )
230
+ const ignore = () => {}
231
+ const hangUp = () => child.kill("SIGHUP")
232
+ const terminate = () => child.kill("SIGTERM")
233
+ process.on("SIGINT", ignore)
234
+ process.on("SIGHUP", hangUp)
235
+ process.on("SIGTERM", terminate)
236
+ try {
237
+ const exitCode = await child.exited
238
+ const signal = child.signalCode
239
+ if (signal === null) return exitCode
240
+ return 128 + (typeof signal === "number" ? signal : (constants.signals[signal] ?? 0))
241
+ } finally {
242
+ process.off("SIGINT", ignore)
243
+ process.off("SIGHUP", hangUp)
244
+ process.off("SIGTERM", terminate)
245
+ }
246
+ }
package/src/setup.ts CHANGED
@@ -1,5 +1,7 @@
1
1
  import { access } from "node:fs/promises"
2
+ import { ENV_TOKEN_FILE } from "./layout"
2
3
  import { shellQuote } from "./shell"
4
+ import { RemoteScriptError, type RemoteShell } from "./transport"
3
5
 
4
6
  /** The first line of the Caddyfile the Debian and Ubuntu packages install. */
5
7
  const PACKAGED_CADDYFILE = "The Caddyfile is an easy way to configure your Caddy web server."
@@ -164,3 +166,38 @@ export async function pickPublicKey(input: {
164
166
  "[SshTarget] No public key to authorize. Pass one with `--key ~/.ssh/id_ed25519.pub`."
165
167
  )
166
168
  }
169
+
170
+ /**
171
+ * Creates the deploy's token when the server doesn't have one. This asks the server rather than
172
+ * going by the checks setup started with: on a fresh server those stop at SSH, before they reach
173
+ * the token. Returns whether it installed one.
174
+ */
175
+ export async function ensureEnvToken(
176
+ shell: RemoteShell,
177
+ name: string,
178
+ createToken: () => Promise<string>
179
+ ): Promise<boolean> {
180
+ const path = `"$HOME"/${shellQuote(`${name}/${ENV_TOKEN_FILE}`)}`
181
+
182
+ const installed = await shell.run(`[ -s ${path} ]`).then(
183
+ () => true,
184
+ (error: unknown) => {
185
+ if (error instanceof RemoteScriptError && error.exitCode === 1) return false
186
+ throw error
187
+ }
188
+ )
189
+ if (installed) return false
190
+
191
+ // On standard input, because a script is an argument any process on the server can read.
192
+ await shell.run(
193
+ [
194
+ "set -e",
195
+ "umask 077",
196
+ `mkdir -p "$HOME"/${shellQuote(name)}`,
197
+ `cat > ${path}.new`,
198
+ `mv ${path}.new ${path}`,
199
+ ].join("\n"),
200
+ { stdin: new Blob([`${await createToken()}\n`]).stream() }
201
+ )
202
+ return true
203
+ }
package/src/ssh-target.ts CHANGED
@@ -13,16 +13,24 @@ import type {
13
13
  DeployLogsOptions,
14
14
  DeployOperationContext,
15
15
  DeployRelease,
16
+ DeployScript,
16
17
  DeploySetupContext,
17
18
  DeploySource,
18
19
  DeployStatus,
19
20
  DeployTarget,
20
21
  } from "@sixb/core/deploy"
21
22
  import { addKey, listKeys, removeKeys, removeKeysWhere } from "./access"
22
- import { checkDns, runChecks } from "./check"
23
+ import { checkDns, ENV_TOKEN_CHECK, runChecks } from "./check"
23
24
  import { generateCiKey, knownHostsLines, readHostKeys, renderCiInstall } from "./ci"
24
- import { controlServices, deployRelease, readStatus, streamLogs } from "./operations"
25
- import { pickPublicKey, renderAdminScript } from "./setup"
25
+ import { ENV_TOKEN_FILE } from "./layout"
26
+ import {
27
+ controlServices,
28
+ deployRelease,
29
+ readStatus,
30
+ runDeployScript,
31
+ streamLogs,
32
+ } from "./operations"
33
+ import { ensureEnvToken, pickPublicKey, renderAdminScript } from "./setup"
26
34
  import { shellQuote } from "./shell"
27
35
  import { resolveSshConfig } from "./ssh-config"
28
36
  import { RemoteScriptError, type RemoteShell, SshShell } from "./transport"
@@ -110,6 +118,10 @@ export class SshTarget implements DeployTarget {
110
118
  return this.#connected((shell) => controlServices(shell, context.name, action, service))
111
119
  }
112
120
 
121
+ run(script: DeployScript, context: DeployOperationContext): Promise<number> {
122
+ return this.#connected((shell) => runDeployScript(shell, context.name, script))
123
+ }
124
+
113
125
  async check(release: DeployRelease, context: DeployCheckContext): Promise<DeployCheck[]> {
114
126
  const { hostname } = await resolveSshConfig(this.location)
115
127
  const dns = checkDns(release, hostname)
@@ -123,6 +135,7 @@ export class SshTarget implements DeployTarget {
123
135
  ports: release.services.flatMap((service) =>
124
136
  service.http ? [{ service: service.name, port: service.http.port }] : []
125
137
  ),
138
+ envFromSource: release.steps.env !== undefined,
126
139
  location: this.location,
127
140
  })),
128
141
  ])
@@ -140,7 +153,9 @@ export class SshTarget implements DeployTarget {
140
153
  const before = await this.check(release, context)
141
154
  const serverFixes = before.filter(
142
155
  (check) =>
143
- (check.status === "fixable" && check.id !== "project.env") ||
156
+ (check.status === "fixable" &&
157
+ check.id !== "project.env" &&
158
+ check.id !== ENV_TOKEN_CHECK) ||
144
159
  (check.id === "ssh" && check.status !== "ok")
145
160
  )
146
161
  const unreachable = before.find((check) => check.id === "ssh" && check.status === "manual")
@@ -169,9 +184,19 @@ export class SshTarget implements DeployTarget {
169
184
  `project="$HOME"/${shellQuote(`${context.name}/code/${context.projectPath}`)}`,
170
185
  'mkdir -p "$project"',
171
186
  'if [ -f "$project/.env" ]; then chmod 600 "$project/.env"; fi',
187
+ `token="$HOME"/${shellQuote(`${context.name}/${ENV_TOKEN_FILE}`)}`,
188
+ 'if [ -f "$token" ]; then chmod 600 "$token"; fi',
172
189
  ].join("\n")
173
190
  )
174
191
  )
192
+
193
+ const { createEnvToken } = context
194
+ if (release.steps.env && createEnvToken) {
195
+ const installed = await this.#connected((shell) =>
196
+ ensureEnvToken(shell, context.name, createEnvToken)
197
+ )
198
+ if (installed) context.write(`Installed a read-only token for ${context.name}.`)
199
+ }
175
200
  }
176
201
 
177
202
  readonly access: DeployAccess = {
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], { stdio: ["inherit", "inherit", "inherit"] })
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