@sixb/deploy-ssh 0.2.6 → 0.2.7
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/README.md +51 -44
- package/dist/check/index.d.ts +24 -0
- package/dist/check/index.d.ts.map +1 -0
- package/dist/check/judge.d.ts +23 -0
- package/dist/check/judge.d.ts.map +1 -0
- package/dist/index.js +599 -1527
- package/dist/index.js.map +10 -16
- package/dist/layout.d.ts +87 -30
- package/dist/layout.d.ts.map +1 -1
- package/dist/operations.d.ts +5 -3
- package/dist/operations.d.ts.map +1 -1
- package/dist/plan.d.ts +25 -0
- package/dist/plan.d.ts.map +1 -0
- package/dist/render.d.ts +15 -11
- package/dist/render.d.ts.map +1 -1
- package/dist/server/manifest.d.ts +10 -4
- package/dist/server/manifest.d.ts.map +1 -1
- package/dist/server/release-host.d.ts +4 -0
- package/dist/server/release-host.d.ts.map +1 -0
- package/dist/server/release.d.ts +46 -0
- package/dist/server/release.d.ts.map +1 -0
- package/dist/server/state.d.ts +0 -4
- package/dist/server/state.d.ts.map +1 -1
- package/dist/server/supervisor.d.ts +8 -8
- package/dist/server/supervisor.d.ts.map +1 -1
- package/dist/ssh-target.d.ts +3 -24
- package/dist/ssh-target.d.ts.map +1 -1
- package/dist/transport.d.ts.map +1 -1
- package/package.json +3 -3
- package/src/bootstrap.sh +93 -0
- package/src/check/index.ts +117 -0
- package/src/check/judge.ts +299 -0
- package/src/check/probe.sh +99 -0
- package/src/layout.ts +109 -61
- package/src/operations.ts +105 -65
- package/src/plan.ts +59 -0
- package/src/render.ts +54 -35
- package/src/server/cli.ts +16 -20
- package/src/server/manifest.ts +13 -6
- package/src/server/release-host.ts +81 -0
- package/src/server/release.ts +738 -0
- package/src/server/state.ts +1 -5
- package/src/server/supervisor.ts +45 -238
- package/src/sh.d.ts +4 -0
- package/src/ssh-target.ts +43 -285
- package/src/transport.ts +28 -12
- package/dist/access.d.ts +0 -11
- package/dist/access.d.ts.map +0 -1
- package/dist/check.d.ts +0 -31
- package/dist/check.d.ts.map +0 -1
- package/dist/ci.d.ts +0 -27
- package/dist/ci.d.ts.map +0 -1
- package/dist/keys.d.ts +0 -13
- package/dist/keys.d.ts.map +0 -1
- package/dist/scripts.d.ts +0 -48
- package/dist/scripts.d.ts.map +0 -1
- package/dist/setup.d.ts +0 -32
- package/dist/setup.d.ts.map +0 -1
- package/src/access.ts +0 -107
- package/src/check.ts +0 -257
- package/src/ci.ts +0 -88
- package/src/keys.ts +0 -57
- package/src/scripts.ts +0 -703
- package/src/setup.ts +0 -203
package/src/setup.ts
DELETED
|
@@ -1,203 +0,0 @@
|
|
|
1
|
-
import { access } from "node:fs/promises"
|
|
2
|
-
import { ENV_TOKEN_FILE } from "./layout"
|
|
3
|
-
import { shellQuote } from "./shell"
|
|
4
|
-
import { RemoteScriptError, type RemoteShell } from "./transport"
|
|
5
|
-
|
|
6
|
-
/** The first line of the Caddyfile the Debian and Ubuntu packages install. */
|
|
7
|
-
const PACKAGED_CADDYFILE = "The Caddyfile is an easy way to configure your Caddy web server."
|
|
8
|
-
|
|
9
|
-
/**
|
|
10
|
-
* Everything that needs root, run once per server and once per deploy user, safe to run again.
|
|
11
|
-
* Afterwards the deploy user needs no sudo but reloading Caddy.
|
|
12
|
-
*
|
|
13
|
-
* Caddy's configuration is taken over only when it is still the one the package installed. A
|
|
14
|
-
* server that already serves other sites keeps its configuration and gains one import line (the
|
|
15
|
-
* old file is kept beside it), and its admin API stays where that configuration puts it.
|
|
16
|
-
*/
|
|
17
|
-
export function renderAdminScript(input: {
|
|
18
|
-
readonly user: string
|
|
19
|
-
readonly publicKey: string
|
|
20
|
-
readonly admin: string
|
|
21
|
-
}): string {
|
|
22
|
-
const user = shellQuote(input.user)
|
|
23
|
-
return [
|
|
24
|
-
"set -euo pipefail",
|
|
25
|
-
`deploy_user=${user}`,
|
|
26
|
-
`public_key=${shellQuote(input.publicKey)}`,
|
|
27
|
-
`admin_login=${shellQuote(input.admin)}`,
|
|
28
|
-
'step() { printf "\\n== %s\\n" "$1"; }',
|
|
29
|
-
// Shows a command's output only when it fails, so a first setup is not pages of apt.
|
|
30
|
-
'quiet() { log="$(mktemp)"; if "$@" > "$log" 2>&1; then rm -f "$log"; else status=$?; tail -n 20 "$log" >&2; rm -f "$log"; return "$status"; fi; }',
|
|
31
|
-
"",
|
|
32
|
-
'step "Server"',
|
|
33
|
-
"if ! command -v apt-get > /dev/null || ! command -v systemctl > /dev/null; then",
|
|
34
|
-
' echo "sixb deploy needs an Ubuntu or Debian server with systemd." >&2',
|
|
35
|
-
" exit 1",
|
|
36
|
-
"fi",
|
|
37
|
-
'. /etc/os-release && echo "$PRETTY_NAME"',
|
|
38
|
-
"",
|
|
39
|
-
'step "Packages"',
|
|
40
|
-
"export DEBIAN_FRONTEND=noninteractive",
|
|
41
|
-
'missing=""',
|
|
42
|
-
"for package in git curl unzip ca-certificates gnupg; do",
|
|
43
|
-
// `ii` is installed; `dpkg -s` also succeeds for a removed package whose config it keeps.
|
|
44
|
-
' dpkg -l "$package" 2> /dev/null | grep -q "^ii" || missing="$missing $package"',
|
|
45
|
-
"done",
|
|
46
|
-
'if [ -n "$missing" ]; then',
|
|
47
|
-
' echo "Installing$missing"',
|
|
48
|
-
" quiet apt-get update",
|
|
49
|
-
" quiet apt-get install -y $missing",
|
|
50
|
-
"fi",
|
|
51
|
-
"if ! command -v caddy > /dev/null; then",
|
|
52
|
-
' echo "Installing caddy"',
|
|
53
|
-
" curl -fsSL https://dl.cloudsmith.io/public/caddy/stable/gpg.key |",
|
|
54
|
-
" gpg --dearmor --yes -o /usr/share/keyrings/caddy-stable-archive-keyring.gpg",
|
|
55
|
-
" curl -fsSL https://dl.cloudsmith.io/public/caddy/stable/debian.deb.txt > /etc/apt/sources.list.d/caddy-stable.list",
|
|
56
|
-
" quiet apt-get update",
|
|
57
|
-
" quiet apt-get install -y caddy",
|
|
58
|
-
"fi",
|
|
59
|
-
"echo \"git, curl, unzip, caddy $(caddy version | cut -d' ' -f1)\"",
|
|
60
|
-
"",
|
|
61
|
-
'step "Deploy user $deploy_user"',
|
|
62
|
-
'id "$deploy_user" > /dev/null 2>&1 || useradd --create-home --shell /bin/bash --user-group "$deploy_user"',
|
|
63
|
-
'home="$(getent passwd "$deploy_user" | cut -d: -f6)"',
|
|
64
|
-
'chmod 750 "$home"',
|
|
65
|
-
'install -d -m 0700 -o "$deploy_user" -g "$deploy_user" "$home/.ssh"',
|
|
66
|
-
'touch "$home/.ssh/authorized_keys"',
|
|
67
|
-
'grep -qxF "$public_key" "$home/.ssh/authorized_keys" || echo "$public_key" >> "$home/.ssh/authorized_keys"',
|
|
68
|
-
'chown "$deploy_user:$deploy_user" "$home/.ssh/authorized_keys"',
|
|
69
|
-
'chmod 0600 "$home/.ssh/authorized_keys"',
|
|
70
|
-
'loginctl enable-linger "$deploy_user"',
|
|
71
|
-
'grep -qs XDG_RUNTIME_DIR "$home/.profile" ||',
|
|
72
|
-
' echo \'export XDG_RUNTIME_DIR="/run/user/$(id -u)"\' >> "$home/.profile"',
|
|
73
|
-
'chown "$deploy_user:$deploy_user" "$home/.profile"',
|
|
74
|
-
// The admin reads every project under this user without sudo; `.env` stays 0600.
|
|
75
|
-
'if [ "$admin_login" != root ]; then usermod -aG "$deploy_user" "$admin_login"; fi',
|
|
76
|
-
'echo "$home, key-only, services start at boot"',
|
|
77
|
-
"",
|
|
78
|
-
'step "Caddy reload"',
|
|
79
|
-
"groupadd -f sixb-deploy",
|
|
80
|
-
'usermod -aG sixb-deploy "$deploy_user"',
|
|
81
|
-
'sudoers="$(mktemp)"',
|
|
82
|
-
"echo '%sixb-deploy ALL=(root) NOPASSWD: /usr/bin/systemctl reload caddy' > \"$sudoers\"",
|
|
83
|
-
'visudo -cf "$sudoers" > /dev/null',
|
|
84
|
-
'install -m 0440 "$sudoers" /etc/sudoers.d/sixb',
|
|
85
|
-
'rm -f "$sudoers"',
|
|
86
|
-
'echo "$deploy_user may run sudo systemctl reload caddy, and nothing else"',
|
|
87
|
-
"",
|
|
88
|
-
'step "Caddy routes"',
|
|
89
|
-
"caddyfile=/etc/caddy/Caddyfile",
|
|
90
|
-
"backup=/etc/caddy/Caddyfile.before-sixb-deploy",
|
|
91
|
-
"install -d -m 0755 /etc/caddy/sixb.d",
|
|
92
|
-
'install -d -m 0755 -o "$deploy_user" -g "$deploy_user" "/etc/caddy/sixb.d/$deploy_user"',
|
|
93
|
-
// Caddy allows one `*` per import pattern, so each deploy user's folder has an index file.
|
|
94
|
-
'printf \'import /etc/caddy/sixb.d/%s/*.caddy\\n\' "$deploy_user" > "/etc/caddy/sixb.d/$deploy_user.caddy"',
|
|
95
|
-
"restart_caddy=0",
|
|
96
|
-
`if [ ! -s "$caddyfile" ] || grep -qF ${shellQuote(PACKAGED_CADDYFILE)} "$caddyfile"; then`,
|
|
97
|
-
' if [ -f "$caddyfile" ]; then cp "$caddyfile" "$backup"; fi',
|
|
98
|
-
" cat > \"$caddyfile\" <<'__SIXB_CADDYFILE__'",
|
|
99
|
-
"# Managed by sixb deploy. Each deploy user's routes live in /etc/caddy/sixb.d/<user>/.",
|
|
100
|
-
"{",
|
|
101
|
-
"\t# Only root and Caddy may change the running configuration.",
|
|
102
|
-
"\tadmin unix//var/lib/caddy/admin.sock",
|
|
103
|
-
"}",
|
|
104
|
-
"",
|
|
105
|
-
"import /etc/caddy/sixb.d/*.caddy",
|
|
106
|
-
"__SIXB_CADDYFILE__",
|
|
107
|
-
// A restart, not a reload: the admin address itself moves.
|
|
108
|
-
" restart_caddy=1",
|
|
109
|
-
"elif ! grep -qF 'import /etc/caddy/sixb.d/*.caddy' \"$caddyfile\"; then",
|
|
110
|
-
' cp "$caddyfile" "$backup"',
|
|
111
|
-
" printf '\\n# Routes written by sixb deploy, one folder per deploy user.\\nimport /etc/caddy/sixb.d/*.caddy\\n' >> \"$caddyfile\"",
|
|
112
|
-
' echo "Kept this server\'s Caddyfile and added one import; the previous one is $backup."',
|
|
113
|
-
"fi",
|
|
114
|
-
'if ! caddy validate --config "$caddyfile" --adapter caddyfile > /tmp/sixb-caddy-validate.log 2>&1; then',
|
|
115
|
-
" tail -n 5 /tmp/sixb-caddy-validate.log >&2",
|
|
116
|
-
' if [ -f "$backup" ]; then cp "$backup" "$caddyfile"; fi',
|
|
117
|
-
' echo "Caddy rejected the configuration; the previous one is back in place." >&2',
|
|
118
|
-
" exit 1",
|
|
119
|
-
"fi",
|
|
120
|
-
'if [ "$restart_caddy" = 1 ]; then systemctl restart caddy; else systemctl reload caddy; fi',
|
|
121
|
-
'echo "/etc/caddy/sixb.d/$deploy_user"',
|
|
122
|
-
"",
|
|
123
|
-
'step "Checks"',
|
|
124
|
-
// Reported, not changed: editing sshd or the firewall is how people lock themselves out.
|
|
125
|
-
"sshd -T 2> /dev/null | grep -qx 'passwordauthentication yes' &&",
|
|
126
|
-
' echo "Warning: SSH accepts passwords. Set PasswordAuthentication no in /etc/ssh/sshd_config." || true',
|
|
127
|
-
"sshd -T 2> /dev/null | grep -qx 'permitrootlogin yes' &&",
|
|
128
|
-
' echo "Warning: root can log in with a password. Set PermitRootLogin prohibit-password." || true',
|
|
129
|
-
"if command -v ufw > /dev/null && ufw status 2> /dev/null | grep -q '^Status: active'; then",
|
|
130
|
-
" for port in 80 443; do",
|
|
131
|
-
' ufw status | grep -qE "^$port(/tcp)?\\s+ALLOW" ||',
|
|
132
|
-
' echo "Warning: the firewall blocks port $port, which Caddy needs. Run: sudo ufw allow $port/tcp"',
|
|
133
|
-
" done",
|
|
134
|
-
"fi",
|
|
135
|
-
'echo "Done."',
|
|
136
|
-
].join("\n")
|
|
137
|
-
}
|
|
138
|
-
|
|
139
|
-
/**
|
|
140
|
-
* The public key to authorize for deploys: the one given, else the first key SSH would offer to
|
|
141
|
-
* the server that has a `.pub` beside it, else the first key in the SSH agent.
|
|
142
|
-
*/
|
|
143
|
-
export async function pickPublicKey(input: {
|
|
144
|
-
readonly key?: string
|
|
145
|
-
readonly identityFiles: readonly string[]
|
|
146
|
-
}): Promise<{ readonly text: string; readonly source: string }> {
|
|
147
|
-
if (input.key) {
|
|
148
|
-
const path = input.key.endsWith(".pub") ? input.key : `${input.key}.pub`
|
|
149
|
-
return { text: (await Bun.file(path).text()).trim(), source: path }
|
|
150
|
-
}
|
|
151
|
-
for (const identity of input.identityFiles) {
|
|
152
|
-
const path = `${identity}.pub`
|
|
153
|
-
if (
|
|
154
|
-
await access(path).then(
|
|
155
|
-
() => true,
|
|
156
|
-
() => false
|
|
157
|
-
)
|
|
158
|
-
) {
|
|
159
|
-
return { text: (await Bun.file(path).text()).trim(), source: path }
|
|
160
|
-
}
|
|
161
|
-
}
|
|
162
|
-
const agent = Bun.spawn(["ssh-add", "-L"], { stdout: "pipe", stderr: "ignore" })
|
|
163
|
-
const first = (await new Response(agent.stdout).text()).split("\n").find((line) => line.trim())
|
|
164
|
-
if ((await agent.exited) === 0 && first) return { text: first.trim(), source: "your SSH agent" }
|
|
165
|
-
throw new Error(
|
|
166
|
-
"[SshTarget] No public key to authorize. Pass one with `--key ~/.ssh/id_ed25519.pub`."
|
|
167
|
-
)
|
|
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
|
-
}
|