@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.
- package/LICENSE +21 -0
- package/README.md +74 -0
- package/dist/access.d.ts +11 -0
- package/dist/access.d.ts.map +1 -0
- package/dist/check.d.ts +28 -0
- package/dist/check.d.ts.map +1 -0
- package/dist/ci.d.ts +27 -0
- package/dist/ci.d.ts.map +1 -0
- package/dist/index.d.ts +3 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +1415 -0
- package/dist/index.js.map +24 -0
- package/dist/keys.d.ts +13 -0
- package/dist/keys.d.ts.map +1 -0
- package/dist/layout.d.ts +38 -0
- package/dist/layout.d.ts.map +1 -0
- package/dist/operations.d.ts +10 -0
- package/dist/operations.d.ts.map +1 -0
- package/dist/render.d.ts +15 -0
- package/dist/render.d.ts.map +1 -0
- package/dist/scripts.d.ts +37 -0
- package/dist/scripts.d.ts.map +1 -0
- package/dist/server/cli.d.ts +3 -0
- package/dist/server/cli.d.ts.map +1 -0
- package/dist/server/logs.d.ts +25 -0
- package/dist/server/logs.d.ts.map +1 -0
- package/dist/server/manifest.d.ts +30 -0
- package/dist/server/manifest.d.ts.map +1 -0
- package/dist/server/state.d.ts +32 -0
- package/dist/server/state.d.ts.map +1 -0
- package/dist/server/supervisor.d.ts +16 -0
- package/dist/server/supervisor.d.ts.map +1 -0
- package/dist/setup.d.ts +25 -0
- package/dist/setup.d.ts.map +1 -0
- package/dist/shell.d.ts +5 -0
- package/dist/shell.d.ts.map +1 -0
- package/dist/ssh-config.d.ts +12 -0
- package/dist/ssh-config.d.ts.map +1 -0
- package/dist/ssh-target.d.ts +39 -0
- package/dist/ssh-target.d.ts.map +1 -0
- package/dist/transport.d.ts +45 -0
- package/dist/transport.d.ts.map +1 -0
- package/dist/tsconfig.json +5 -0
- package/dist/update-page.d.ts +6 -0
- package/dist/update-page.d.ts.map +1 -0
- package/package.json +61 -0
- package/src/access.ts +107 -0
- package/src/check.ts +215 -0
- package/src/ci.ts +88 -0
- package/src/index.ts +2 -0
- package/src/keys.ts +57 -0
- package/src/layout.ts +69 -0
- package/src/operations.ts +158 -0
- package/src/render.ts +129 -0
- package/src/scripts.ts +305 -0
- package/src/server/cli.ts +157 -0
- package/src/server/logs.ts +180 -0
- package/src/server/manifest.ts +112 -0
- package/src/server/state.ts +81 -0
- package/src/server/supervisor.ts +542 -0
- package/src/setup.ts +166 -0
- package/src/shell.ts +10 -0
- package/src/ssh-config.ts +35 -0
- package/src/ssh-target.ts +347 -0
- package/src/transport.ts +211 -0
- package/src/update-page.ts +44 -0
package/src/transport.ts
ADDED
|
@@ -0,0 +1,211 @@
|
|
|
1
|
+
import { mkdtemp, rm } from "node:fs/promises"
|
|
2
|
+
import { tmpdir } from "node:os"
|
|
3
|
+
import { join } from "node:path"
|
|
4
|
+
import { shellQuote } from "./shell"
|
|
5
|
+
|
|
6
|
+
export interface RunOptions {
|
|
7
|
+
/** Streamed to the script's standard input. The script itself then travels as an argument. */
|
|
8
|
+
readonly stdin?: ReadableStream<Uint8Array>
|
|
9
|
+
/**
|
|
10
|
+
* Every line the script prints, and which stream it came from. Read results from `stdout`
|
|
11
|
+
* only: a login script or a failed redirection can write to `stderr` first.
|
|
12
|
+
*/
|
|
13
|
+
readonly onLine?: (line: string, stream: "stdout" | "stderr") => void
|
|
14
|
+
/** Attach the terminal, so the remote command stops when you press Ctrl-C. */
|
|
15
|
+
readonly terminal?: boolean
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
/** Runs bash scripts on a deployment's server. */
|
|
19
|
+
export interface RemoteShell {
|
|
20
|
+
/** Rejects with the script's last output lines when it exits non-zero. */
|
|
21
|
+
run(script: string, options?: RunOptions): Promise<void>
|
|
22
|
+
close(): Promise<void>
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
export class RemoteScriptError extends Error {
|
|
26
|
+
readonly exitCode: number
|
|
27
|
+
|
|
28
|
+
constructor(message: string, exitCode: number) {
|
|
29
|
+
super(message)
|
|
30
|
+
this.name = "RemoteScriptError"
|
|
31
|
+
this.exitCode = exitCode
|
|
32
|
+
}
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
const OUTPUT_TAIL = 20
|
|
36
|
+
|
|
37
|
+
/**
|
|
38
|
+
* One SSH connection, shared by every script a command runs through OpenSSH's connection
|
|
39
|
+
* multiplexing. Never prompts: a password, passphrase, or unknown host key fails with a message
|
|
40
|
+
* instead of hanging a deploy.
|
|
41
|
+
*/
|
|
42
|
+
export class SshShell implements RemoteShell {
|
|
43
|
+
private constructor(
|
|
44
|
+
private readonly destination: string,
|
|
45
|
+
private readonly controlDir: string
|
|
46
|
+
) {}
|
|
47
|
+
|
|
48
|
+
static async open(destination: string): Promise<SshShell> {
|
|
49
|
+
// Kept short: a socket path longer than about 100 characters cannot be bound.
|
|
50
|
+
const controlDir = await mkdtemp(join(tmpdir(), "sixb-ssh-"))
|
|
51
|
+
const shell = new SshShell(destination, controlDir)
|
|
52
|
+
try {
|
|
53
|
+
await runProcess(["ssh", ...shell.options(), "-M", "-N", "-f", destination], {})
|
|
54
|
+
} catch (error) {
|
|
55
|
+
await rm(controlDir, { recursive: true, force: true })
|
|
56
|
+
throw sshError(destination, error)
|
|
57
|
+
}
|
|
58
|
+
return shell
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
async run(script: string, options: RunOptions = {}): Promise<void> {
|
|
62
|
+
const inline = options.stdin !== undefined || options.terminal === true
|
|
63
|
+
const command = inline ? ["bash", "-c", shellQuote(script)] : ["bash", "-s"]
|
|
64
|
+
try {
|
|
65
|
+
await runProcess(
|
|
66
|
+
[
|
|
67
|
+
"ssh",
|
|
68
|
+
...this.options(),
|
|
69
|
+
...(options.terminal ? ["-tt"] : []),
|
|
70
|
+
this.destination,
|
|
71
|
+
command.join(" "),
|
|
72
|
+
],
|
|
73
|
+
{
|
|
74
|
+
...options,
|
|
75
|
+
...(inline ? {} : { stdin: new Blob([script]).stream() }),
|
|
76
|
+
}
|
|
77
|
+
)
|
|
78
|
+
} catch (error) {
|
|
79
|
+
throw sshError(this.destination, error)
|
|
80
|
+
}
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
async close(): Promise<void> {
|
|
84
|
+
await runProcess(["ssh", ...this.options(), "-O", "exit", this.destination], {}).catch(() => {})
|
|
85
|
+
await rm(this.controlDir, { recursive: true, force: true })
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
private options(): string[] {
|
|
89
|
+
return [
|
|
90
|
+
"-o",
|
|
91
|
+
"BatchMode=yes",
|
|
92
|
+
"-o",
|
|
93
|
+
"ConnectTimeout=15",
|
|
94
|
+
// Notices a dead connection during a long build instead of waiting forever.
|
|
95
|
+
"-o",
|
|
96
|
+
"ServerAliveInterval=15",
|
|
97
|
+
"-o",
|
|
98
|
+
"ControlMaster=auto",
|
|
99
|
+
"-o",
|
|
100
|
+
"ControlPersist=60",
|
|
101
|
+
"-o",
|
|
102
|
+
"LogLevel=ERROR",
|
|
103
|
+
"-S",
|
|
104
|
+
join(this.controlDir, "control"),
|
|
105
|
+
]
|
|
106
|
+
}
|
|
107
|
+
}
|
|
108
|
+
|
|
109
|
+
/** Runs the scripts on this machine instead, with its own home. For tests. */
|
|
110
|
+
export class LocalShell implements RemoteShell {
|
|
111
|
+
constructor(private readonly env: Readonly<Record<string, string>> = {}) {}
|
|
112
|
+
|
|
113
|
+
async run(script: string, options: RunOptions = {}): Promise<void> {
|
|
114
|
+
const inline = options.stdin !== undefined
|
|
115
|
+
await runProcess(inline ? ["bash", "-c", script] : ["bash", "-s"], {
|
|
116
|
+
...options,
|
|
117
|
+
...(inline ? {} : { stdin: new Blob([script]).stream() }),
|
|
118
|
+
env: this.env,
|
|
119
|
+
})
|
|
120
|
+
}
|
|
121
|
+
|
|
122
|
+
async close(): Promise<void> {}
|
|
123
|
+
}
|
|
124
|
+
|
|
125
|
+
async function runProcess(
|
|
126
|
+
command: readonly string[],
|
|
127
|
+
options: RunOptions & { readonly env?: Readonly<Record<string, string>> }
|
|
128
|
+
): Promise<void> {
|
|
129
|
+
if (options.terminal) {
|
|
130
|
+
const child = Bun.spawn([...command], { stdio: ["inherit", "inherit", "inherit"] })
|
|
131
|
+
const exitCode = await child.exited
|
|
132
|
+
if (exitCode !== 0) throw new RemoteScriptError(`exited with code ${exitCode}`, exitCode)
|
|
133
|
+
return
|
|
134
|
+
}
|
|
135
|
+
|
|
136
|
+
const child = Bun.spawn([...command], {
|
|
137
|
+
stdin: options.stdin ? "pipe" : "ignore",
|
|
138
|
+
stdout: "pipe",
|
|
139
|
+
stderr: "pipe",
|
|
140
|
+
env: { ...process.env, ...options.env },
|
|
141
|
+
})
|
|
142
|
+
const tail: string[] = []
|
|
143
|
+
const onLine = (stream: "stdout" | "stderr") => (line: string) => {
|
|
144
|
+
tail.push(line)
|
|
145
|
+
if (tail.length > OUTPUT_TAIL) tail.shift()
|
|
146
|
+
options.onLine?.(line, stream)
|
|
147
|
+
}
|
|
148
|
+
|
|
149
|
+
const [exitCode] = await Promise.all([
|
|
150
|
+
child.exited,
|
|
151
|
+
options.stdin && child.stdin ? pump(options.stdin, child.stdin) : undefined,
|
|
152
|
+
readLines(child.stdout, onLine("stdout")),
|
|
153
|
+
readLines(child.stderr, onLine("stderr")),
|
|
154
|
+
])
|
|
155
|
+
if (exitCode !== 0) {
|
|
156
|
+
throw new RemoteScriptError(
|
|
157
|
+
tail.length > 0 ? tail.join("\n") : `exited with code ${exitCode}`,
|
|
158
|
+
exitCode
|
|
159
|
+
)
|
|
160
|
+
}
|
|
161
|
+
}
|
|
162
|
+
|
|
163
|
+
async function pump(source: ReadableStream<Uint8Array>, sink: Bun.FileSink): Promise<void> {
|
|
164
|
+
const reader = source.getReader()
|
|
165
|
+
try {
|
|
166
|
+
for (let chunk = await reader.read(); !chunk.done; chunk = await reader.read()) {
|
|
167
|
+
sink.write(chunk.value)
|
|
168
|
+
await sink.flush()
|
|
169
|
+
}
|
|
170
|
+
} finally {
|
|
171
|
+
await sink.end()
|
|
172
|
+
}
|
|
173
|
+
}
|
|
174
|
+
|
|
175
|
+
async function readLines(
|
|
176
|
+
stream: ReadableStream<Uint8Array>,
|
|
177
|
+
onLine: (line: string) => void
|
|
178
|
+
): Promise<void> {
|
|
179
|
+
const reader = stream.getReader()
|
|
180
|
+
const decoder = new TextDecoder()
|
|
181
|
+
let buffer = ""
|
|
182
|
+
for (let chunk = await reader.read(); !chunk.done; chunk = await reader.read()) {
|
|
183
|
+
buffer += decoder.decode(chunk.value, { stream: true })
|
|
184
|
+
const lines = buffer.split(/\r?\n/)
|
|
185
|
+
buffer = lines.pop() ?? ""
|
|
186
|
+
for (const line of lines) onLine(line)
|
|
187
|
+
}
|
|
188
|
+
buffer += decoder.decode()
|
|
189
|
+
if (buffer) onLine(buffer)
|
|
190
|
+
}
|
|
191
|
+
|
|
192
|
+
/** OpenSSH's own failures, which exit 255, reworded as what to do about them. */
|
|
193
|
+
export function sshError(destination: string, error: unknown): Error {
|
|
194
|
+
if (!(error instanceof RemoteScriptError) || error.exitCode !== 255) {
|
|
195
|
+
return error instanceof Error ? error : new Error(String(error))
|
|
196
|
+
}
|
|
197
|
+
const output = error.message
|
|
198
|
+
const host = destination.split("@").at(-1) ?? destination
|
|
199
|
+
const reason = /REMOTE HOST IDENTIFICATION HAS CHANGED/.test(output)
|
|
200
|
+
? `${host} answered with a host key other than the one on record. If the server was rebuilt, remove the old key with \`ssh-keygen -R ${host}\` and connect once to accept the new one; for CI, run \`sixb deploy ci\` again. Otherwise, something else is answering at that address: do not deploy to it.`
|
|
201
|
+
: /Host key verification failed/.test(output)
|
|
202
|
+
? `The host key for ${host} is not trusted yet. Connect once with \`ssh ${destination}\`, check the fingerprint, and accept it.`
|
|
203
|
+
: /Permission denied/.test(output)
|
|
204
|
+
? `The server refused your SSH key for ${destination}. Load it into your SSH agent, or ask someone with access to authorize it.`
|
|
205
|
+
: /Could not resolve hostname/.test(output)
|
|
206
|
+
? `${host} does not resolve. Check the target's host.`
|
|
207
|
+
: /Connection refused|timed out|No route to host/.test(output)
|
|
208
|
+
? `Could not reach ${host} over SSH.`
|
|
209
|
+
: `SSH to ${destination} failed.`
|
|
210
|
+
return new RemoteScriptError(`[SshTarget] ${reason}\n${output}`, error.exitCode)
|
|
211
|
+
}
|
|
@@ -0,0 +1,44 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Shown by Caddy for a page request while the app or Atlas cannot answer — during a deploy, or
|
|
3
|
+
* an outage. It needs no assets or JavaScript and reloads itself every 10 seconds.
|
|
4
|
+
*/
|
|
5
|
+
export function renderUpdatePage(): string {
|
|
6
|
+
return `<!doctype html>
|
|
7
|
+
<html lang="en">
|
|
8
|
+
<head>
|
|
9
|
+
<meta charset="utf-8">
|
|
10
|
+
<meta name="viewport" content="width=device-width, initial-scale=1">
|
|
11
|
+
<meta http-equiv="refresh" content="10">
|
|
12
|
+
<title>Updates in progress</title>
|
|
13
|
+
<style>
|
|
14
|
+
:root { color-scheme: light dark; font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", sans-serif; background: #fff; color: #111; --muted: #666; --line: #e8e8e8; --surface: #fafafa; --track: #d6d6d6; }
|
|
15
|
+
* { box-sizing: border-box; }
|
|
16
|
+
body { margin: 0; min-height: 100vh; min-height: 100svh; display: grid; place-items: center; padding: 48px 28px; -webkit-font-smoothing: antialiased; }
|
|
17
|
+
main { width: 100%; max-width: 480px; }
|
|
18
|
+
.status { display: inline-flex; align-items: center; gap: 9px; margin-bottom: 28px; padding: 8px 11px; border: 1px solid var(--line); border-radius: 6px; background: var(--surface); font-family: ui-monospace, SFMono-Regular, Consolas, monospace; font-size: 10px; font-weight: 500; line-height: 1.4; letter-spacing: 0.1em; text-transform: uppercase; }
|
|
19
|
+
.loader { width: 12px; height: 12px; flex: none; border: 1.5px solid var(--track); border-top-color: currentColor; border-radius: 50%; animation: spin 1s linear infinite; }
|
|
20
|
+
h1 { margin: 0 0 16px; font-size: clamp(28px, 5vw, 36px); font-weight: 500; letter-spacing: -0.045em; line-height: 1.2; text-wrap: balance; }
|
|
21
|
+
.description { margin: 0; max-width: 380px; color: var(--muted); font-size: 15px; line-height: 1.8; text-wrap: pretty; }
|
|
22
|
+
.footer { display: flex; align-items: baseline; justify-content: space-between; flex-wrap: wrap; gap: 8px 20px; margin-top: 36px; padding-top: 20px; border-top: 1px solid var(--line); color: var(--muted); }
|
|
23
|
+
.footer p { margin: 0; font-size: 12px; line-height: 1.6; }
|
|
24
|
+
.footer .interval { font-family: ui-monospace, SFMono-Regular, Consolas, monospace; font-size: 10px; letter-spacing: 0.02em; }
|
|
25
|
+
@keyframes spin { to { transform: rotate(360deg); } }
|
|
26
|
+
@media (prefers-reduced-motion: reduce) { .loader { animation: none; } }
|
|
27
|
+
@media (prefers-color-scheme: dark) {
|
|
28
|
+
:root { background: #0a0a0a; color: #f5f5f5; --muted: #999; --line: #262626; --surface: #111; --track: #444; }
|
|
29
|
+
}
|
|
30
|
+
</style>
|
|
31
|
+
</head>
|
|
32
|
+
<body>
|
|
33
|
+
<main>
|
|
34
|
+
<div class="status"><span class="loader" aria-hidden="true"></span>Site update</div>
|
|
35
|
+
<h1>Updates in progress</h1>
|
|
36
|
+
<p class="description">We’re making a few updates. The site should be back soon. Thanks for your patience.</p>
|
|
37
|
+
<div class="footer">
|
|
38
|
+
<p>This page will reconnect automatically.</p>
|
|
39
|
+
<p class="interval">Retrying every 10s</p>
|
|
40
|
+
</div>
|
|
41
|
+
</main>
|
|
42
|
+
</body>
|
|
43
|
+
</html>`
|
|
44
|
+
}
|