@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/scripts.ts
DELETED
|
@@ -1,703 +0,0 @@
|
|
|
1
|
-
import type {
|
|
2
|
-
DeployCommand,
|
|
3
|
-
DeployHttpServiceName,
|
|
4
|
-
DeployRecord,
|
|
5
|
-
DeployRelease,
|
|
6
|
-
} from "@sixb/core/deploy"
|
|
7
|
-
import { type DeploymentLayout, type ReleaseLayout, releaseLayout } from "./layout"
|
|
8
|
-
import {
|
|
9
|
-
releaseForSlot,
|
|
10
|
-
renderCaddySnippet,
|
|
11
|
-
renderCtl,
|
|
12
|
-
renderProcessManifest,
|
|
13
|
-
renderUnit,
|
|
14
|
-
type SlotPorts,
|
|
15
|
-
} from "./render"
|
|
16
|
-
import { shellQuote, writeFileCommand } from "./shell"
|
|
17
|
-
|
|
18
|
-
/** A line the deploy script prints around each step; the target turns it into progress. */
|
|
19
|
-
export const STEP_MARKER = "__SIXB_STEP__"
|
|
20
|
-
|
|
21
|
-
/** The server's home directory, and a fresh directory to upload the source into. */
|
|
22
|
-
export function renderPrepareScript(name: string, commit: string): string {
|
|
23
|
-
return [
|
|
24
|
-
`root="$HOME"/${shellQuote(name)}`,
|
|
25
|
-
'mkdir -p "$root/deploy/incoming"',
|
|
26
|
-
`incoming="$(mktemp -d "$root/deploy/incoming/${commit.slice(0, 12)}.XXXXXX")"`,
|
|
27
|
-
// `mktemp` makes it 0700, and GNU tar passes the mode of the upload's `.` on to the release,
|
|
28
|
-
// which would lock out the admin, who reads the code through the deploy user's group.
|
|
29
|
-
'chmod 755 "$incoming"',
|
|
30
|
-
`printf '%s\\n%s\\n' "$HOME" "$incoming"`,
|
|
31
|
-
].join("\n")
|
|
32
|
-
}
|
|
33
|
-
|
|
34
|
-
/** Extracts one uploaded archive from standard input. */
|
|
35
|
-
export function renderReceiveScript(incoming: string, path: string): string {
|
|
36
|
-
const directory = path === "." ? incoming : `${incoming}/${path}`
|
|
37
|
-
return `mkdir -p ${shellQuote(directory)} && tar -x -f - -C ${shellQuote(directory)}`
|
|
38
|
-
}
|
|
39
|
-
|
|
40
|
-
export interface DeployScriptInput {
|
|
41
|
-
readonly release: DeployRelease
|
|
42
|
-
readonly layout: DeploymentLayout
|
|
43
|
-
readonly incoming: string
|
|
44
|
-
readonly record: DeployRecord
|
|
45
|
-
readonly releaseId?: string
|
|
46
|
-
readonly slotPorts?: SlotPorts
|
|
47
|
-
}
|
|
48
|
-
|
|
49
|
-
export interface DeployScript {
|
|
50
|
-
readonly labels: readonly string[]
|
|
51
|
-
readonly script: string
|
|
52
|
-
}
|
|
53
|
-
|
|
54
|
-
interface Step {
|
|
55
|
-
readonly label: string
|
|
56
|
-
readonly body: string
|
|
57
|
-
}
|
|
58
|
-
|
|
59
|
-
export function defaultSlotPorts(release: DeployRelease): SlotPorts {
|
|
60
|
-
const portOf = (name: DeployHttpServiceName, fallback: number): number =>
|
|
61
|
-
release.services.find((service) => service.name === name)?.http?.port ?? fallback
|
|
62
|
-
const blueApp = portOf("app", 3001)
|
|
63
|
-
const blueApi = portOf("api", 3002)
|
|
64
|
-
return {
|
|
65
|
-
blue: { app: blueApp, api: blueApi },
|
|
66
|
-
green: {
|
|
67
|
-
app: blueApp + 10 <= 65535 ? blueApp + 10 : blueApp - 10,
|
|
68
|
-
api: blueApi + 10 <= 65535 ? blueApi + 10 : blueApi - 10,
|
|
69
|
-
},
|
|
70
|
-
}
|
|
71
|
-
}
|
|
72
|
-
|
|
73
|
-
export function deriveReleaseId(record: DeployRecord): string {
|
|
74
|
-
const stamp = record.deployedAt.replace(/[-:.]/g, "").slice(0, 15)
|
|
75
|
-
const sha = record.commit.slice(0, 12).replace(/[^a-zA-Z0-9_-]/g, "")
|
|
76
|
-
return `${stamp}Z-${sha || "release"}`
|
|
77
|
-
}
|
|
78
|
-
|
|
79
|
-
/**
|
|
80
|
-
* Everything the server does once the source is uploaded, as one script that holds the
|
|
81
|
-
* deployment's lock throughout. Every build, migration, provider check, and readiness check runs
|
|
82
|
-
* against the new release directory before routes flip or the previous release retires.
|
|
83
|
-
*/
|
|
84
|
-
export function renderDeployScript(input: DeployScriptInput): DeployScript {
|
|
85
|
-
const { release, layout } = input
|
|
86
|
-
const releaseId = input.releaseId ?? deriveReleaseId(input.record)
|
|
87
|
-
const targetRelease = releaseLayout(layout, releaseId)
|
|
88
|
-
const slots = input.slotPorts ?? defaultSlotPorts(release)
|
|
89
|
-
const blueRelease = releaseForSlot(release, slots.blue)
|
|
90
|
-
const greenRelease = releaseForSlot(release, slots.green)
|
|
91
|
-
|
|
92
|
-
const readinessUrl = (candidate: DeployRelease): string | null => {
|
|
93
|
-
const api = candidate.services.find((service) => service.http?.readinessPath)
|
|
94
|
-
return api?.http ? `http://${api.http.host}:${api.http.port}${api.http.readinessPath}` : null
|
|
95
|
-
}
|
|
96
|
-
|
|
97
|
-
const steps: Step[] = [
|
|
98
|
-
{
|
|
99
|
-
label: "Prepare release",
|
|
100
|
-
body: renderPrepareRelease(layout, targetRelease, input.incoming),
|
|
101
|
-
},
|
|
102
|
-
{ label: `Install Bun ${release.bunVersion}`, body: renderInstallBun(release.bunVersion) },
|
|
103
|
-
{ label: "Install dependencies", body: renderInstallDependencies() },
|
|
104
|
-
...(release.steps.env ? [envStep(release.steps.env, layout)] : []),
|
|
105
|
-
...release.steps.build.map((command, index, all) =>
|
|
106
|
-
index === all.length - 1 ? withCarriedAssets(runStep(command)) : runStep(command)
|
|
107
|
-
),
|
|
108
|
-
...release.steps.beforeStart.flatMap((command) =>
|
|
109
|
-
isStorageMigration(command) ? [stopForSchemaChange(), runStep(command)] : [runStep(command)]
|
|
110
|
-
),
|
|
111
|
-
{
|
|
112
|
-
label: "Start new release",
|
|
113
|
-
body: renderStartCandidate({
|
|
114
|
-
layout,
|
|
115
|
-
targetRelease,
|
|
116
|
-
blueRelease,
|
|
117
|
-
greenRelease,
|
|
118
|
-
slots,
|
|
119
|
-
}),
|
|
120
|
-
},
|
|
121
|
-
{
|
|
122
|
-
label: "Check services",
|
|
123
|
-
body: renderCheckServices({
|
|
124
|
-
blueReadyUrl: readinessUrl(blueRelease),
|
|
125
|
-
greenReadyUrl: readinessUrl(greenRelease),
|
|
126
|
-
}),
|
|
127
|
-
},
|
|
128
|
-
{
|
|
129
|
-
label: "Update routes",
|
|
130
|
-
body: renderUpdateRoutes({
|
|
131
|
-
layout,
|
|
132
|
-
blueCaddy: renderCaddySnippet(blueRelease),
|
|
133
|
-
greenCaddy: renderCaddySnippet(greenRelease),
|
|
134
|
-
}),
|
|
135
|
-
},
|
|
136
|
-
{
|
|
137
|
-
label: "Activate release",
|
|
138
|
-
body: renderActivateRelease({ release: blueRelease, layout, slots }),
|
|
139
|
-
},
|
|
140
|
-
]
|
|
141
|
-
|
|
142
|
-
const script = [
|
|
143
|
-
"set -euo pipefail",
|
|
144
|
-
"main() {",
|
|
145
|
-
renderEnvironment(release, layout, targetRelease),
|
|
146
|
-
renderTakeLock(layout),
|
|
147
|
-
renderUseRelease(layout, input.record),
|
|
148
|
-
renderRestoreRoutes(),
|
|
149
|
-
renderCandidateTrap(),
|
|
150
|
-
...steps.flatMap((step, index) => [
|
|
151
|
-
`printf '\\n%s\\t%s\\t%s\\n' ${STEP_MARKER} ${index} start`,
|
|
152
|
-
step.body,
|
|
153
|
-
`printf '%s\\t%s\\t%s\\n' ${STEP_MARKER} ${index} done`,
|
|
154
|
-
]),
|
|
155
|
-
"}",
|
|
156
|
-
"main </dev/null",
|
|
157
|
-
"",
|
|
158
|
-
].join("\n")
|
|
159
|
-
|
|
160
|
-
return { labels: steps.map((step) => step.label), script }
|
|
161
|
-
}
|
|
162
|
-
|
|
163
|
-
function renderEnvironment(
|
|
164
|
-
release: DeployRelease,
|
|
165
|
-
layout: DeploymentLayout,
|
|
166
|
-
targetRelease: ReleaseLayout
|
|
167
|
-
): string {
|
|
168
|
-
const variables: Record<string, string> = {
|
|
169
|
-
ROOT: layout.root,
|
|
170
|
-
RELEASES_DIR: layout.releasesDir,
|
|
171
|
-
CURRENT: layout.currentLink,
|
|
172
|
-
CODE: layout.code,
|
|
173
|
-
PROJECT: layout.project,
|
|
174
|
-
SHARED_DIR: layout.sharedDir,
|
|
175
|
-
SHARED_ENV: layout.sharedEnv,
|
|
176
|
-
RELEASE_ID: targetRelease.id,
|
|
177
|
-
RELEASE_ROOT: targetRelease.root,
|
|
178
|
-
RELEASE_PROJECT: targetRelease.project,
|
|
179
|
-
DEPLOY: layout.deployDir,
|
|
180
|
-
MANIFEST: layout.manifest,
|
|
181
|
-
CANDIDATE_MANIFEST: layout.candidateManifest,
|
|
182
|
-
STATE_FILE: `${layout.runDir}/state.json`,
|
|
183
|
-
BUN: layout.bun,
|
|
184
|
-
BUN_DIR: layout.bunDir,
|
|
185
|
-
BUN_VERSION: release.bunVersion,
|
|
186
|
-
CTL: layout.ctl,
|
|
187
|
-
UNIT: layout.unitName,
|
|
188
|
-
UNIT_PATH: layout.unit,
|
|
189
|
-
CADDY_DIR: layout.caddyDir,
|
|
190
|
-
CADDY_SNIPPET: layout.caddySnippet,
|
|
191
|
-
PREVIOUS_ROUTES: `${layout.deployDir}/Caddyfile.previous`,
|
|
192
|
-
}
|
|
193
|
-
return [
|
|
194
|
-
...Object.entries(variables).map(([name, value]) => `${name}=${shellQuote(value)}`),
|
|
195
|
-
// The steps run with the release's environment; the project's `.env` fills in the rest.
|
|
196
|
-
...Object.entries(release.env).map(([name, value]) => `export ${name}=${shellQuote(value)}`),
|
|
197
|
-
'export PATH="$BUN_DIR:$PATH"',
|
|
198
|
-
// `systemctl --user` finds the user's systemd through this; SSH sessions usually set it.
|
|
199
|
-
'printenv XDG_RUNTIME_DIR > /dev/null || export XDG_RUNTIME_DIR="/run/user/$(id -u)"',
|
|
200
|
-
'deploy_mode="none"',
|
|
201
|
-
'target_slot="blue"',
|
|
202
|
-
'prev_release=""',
|
|
203
|
-
].join("\n")
|
|
204
|
-
}
|
|
205
|
-
|
|
206
|
-
function renderTakeLock(layout: DeploymentLayout): string {
|
|
207
|
-
return [
|
|
208
|
-
`exec 9>${shellQuote(layout.lock)}`,
|
|
209
|
-
"if ! flock -n 9; then",
|
|
210
|
-
' echo "Waiting for another deploy or script run of this project to finish…"',
|
|
211
|
-
"fi",
|
|
212
|
-
"if ! flock -w 600 9; then",
|
|
213
|
-
' echo "Another deploy or script run of this project has held its lock for 10 minutes." >&2',
|
|
214
|
-
" exit 1",
|
|
215
|
-
"fi",
|
|
216
|
-
].join("\n")
|
|
217
|
-
}
|
|
218
|
-
|
|
219
|
-
/**
|
|
220
|
-
* Points `current` at the new release and records it in `release.json` at the same moment, so
|
|
221
|
-
* `sixb deploy status` names the commit that serves from then on, even when a later check fails.
|
|
222
|
-
* Callers set `deploy_mode` right after it, with nothing in between that can fail.
|
|
223
|
-
*/
|
|
224
|
-
function renderUseRelease(layout: DeploymentLayout, record: DeployRecord): string {
|
|
225
|
-
return [
|
|
226
|
-
"use_release() {",
|
|
227
|
-
' ln -sfn "releases/$RELEASE_ID" "$CURRENT"',
|
|
228
|
-
' ln -sfn "current" "$CODE"',
|
|
229
|
-
` ${writeFileCommand(layout.releaseRecord, `${JSON.stringify(record)}\n`)}`,
|
|
230
|
-
"}",
|
|
231
|
-
].join("\n")
|
|
232
|
-
}
|
|
233
|
-
|
|
234
|
-
/** Puts back the routes "Update routes" replaced: the live release's, or none on a first deploy. */
|
|
235
|
-
function renderRestoreRoutes(): string {
|
|
236
|
-
return [
|
|
237
|
-
"restore_routes() {",
|
|
238
|
-
' if [ -f "$PREVIOUS_ROUTES" ]; then cp "$PREVIOUS_ROUTES" "$CADDY_SNIPPET"; else rm -f "$CADDY_SNIPPET"; fi',
|
|
239
|
-
"}",
|
|
240
|
-
].join("\n")
|
|
241
|
-
}
|
|
242
|
-
|
|
243
|
-
/**
|
|
244
|
-
* A failure before the switch discards the candidate, and the live release keeps serving. When
|
|
245
|
-
* Caddy already routes to the candidate, its routes go back to the live release first. After the
|
|
246
|
-
* switch there is nothing to discard: the new release serves, and the operator is told so.
|
|
247
|
-
*/
|
|
248
|
-
function renderCandidateTrap(): string {
|
|
249
|
-
return [
|
|
250
|
-
"cleanup_candidate() {",
|
|
251
|
-
" status=$?",
|
|
252
|
-
' if [ "$status" -eq 0 ]; then return; fi',
|
|
253
|
-
"",
|
|
254
|
-
' case "$deploy_mode" in',
|
|
255
|
-
" staged | routed)",
|
|
256
|
-
' if [ "$deploy_mode" = "routed" ]; then',
|
|
257
|
-
" restore_routes || true",
|
|
258
|
-
" if sudo -n /usr/bin/systemctl reload caddy > /dev/null 2>&1; then",
|
|
259
|
-
" echo 'The routes are back on the live release.' >&2",
|
|
260
|
-
" else",
|
|
261
|
-
" echo 'Caddy did not reload the previous routes: run sudo systemctl reload caddy on the server.' >&2",
|
|
262
|
-
" fi",
|
|
263
|
-
" fi",
|
|
264
|
-
' "$BUN" "$RELEASE_PROJECT/node_modules/.bin/sixb-deploy-ssh" discard --manifest "$MANIFEST" > /dev/null 2>&1 || true',
|
|
265
|
-
' rm -f "$CANDIDATE_MANIFEST" "$DEPLOY/manifest.blue.json" "$DEPLOY/manifest.green.json"',
|
|
266
|
-
" ;;",
|
|
267
|
-
" stopped)",
|
|
268
|
-
" echo 'The services were stopped for the storage migration and stay stopped. Fix what failed and deploy again.' >&2",
|
|
269
|
-
" ;;",
|
|
270
|
-
" activated | cold)",
|
|
271
|
-
" echo 'The new release is in place, but it did not pass its checks.' >&2",
|
|
272
|
-
" echo 'sixb deploy status and sixb deploy logs show what failed. To go back, deploy the previous commit with --ref.' >&2",
|
|
273
|
-
" ;;",
|
|
274
|
-
" esac",
|
|
275
|
-
"}",
|
|
276
|
-
"trap cleanup_candidate EXIT",
|
|
277
|
-
].join("\n")
|
|
278
|
-
}
|
|
279
|
-
|
|
280
|
-
/**
|
|
281
|
-
* Unpacks the upload into an immutable `releases/<id>` directory, migrating an in-place `code/`
|
|
282
|
-
* directory on upgrade. The release's `.env` links to the server's `shared/.env`, so an edit to
|
|
283
|
-
* that one file reaches the services on their next restart, and a committed `.env` never
|
|
284
|
-
* replaces it.
|
|
285
|
-
*/
|
|
286
|
-
export function renderPrepareRelease(
|
|
287
|
-
layout: DeploymentLayout,
|
|
288
|
-
targetRelease: ReleaseLayout,
|
|
289
|
-
incoming: string
|
|
290
|
-
): string {
|
|
291
|
-
return [
|
|
292
|
-
`incoming=${shellQuote(incoming)}`,
|
|
293
|
-
`releases_dir=${shellQuote(layout.releasesDir)}`,
|
|
294
|
-
`current=${shellQuote(layout.currentLink)}`,
|
|
295
|
-
`code=${shellQuote(layout.code)}`,
|
|
296
|
-
`project=${shellQuote(layout.project)}`,
|
|
297
|
-
`shared_dir=${shellQuote(layout.sharedDir)}`,
|
|
298
|
-
`shared_env=${shellQuote(layout.sharedEnv)}`,
|
|
299
|
-
`release_root=${shellQuote(targetRelease.root)}`,
|
|
300
|
-
`release_project=${shellQuote(targetRelease.project)}`,
|
|
301
|
-
'mkdir -p "$releases_dir" "$shared_dir"',
|
|
302
|
-
// Migrate a pre-blue-green deployment whose `code/` is still a plain directory.
|
|
303
|
-
'if [ -d "$code" ] && [ ! -L "$code" ]; then',
|
|
304
|
-
' if [ -f "$project/.env" ] && [ ! -f "$shared_env" ]; then',
|
|
305
|
-
' cp -pf "$project/.env" "$shared_env" && chmod 600 "$shared_env"',
|
|
306
|
-
" fi",
|
|
307
|
-
' rm -rf "$releases_dir/legacy"',
|
|
308
|
-
' mv "$code" "$releases_dir/legacy"',
|
|
309
|
-
' ln -sfn "releases/legacy" "$current"',
|
|
310
|
-
' ln -sfn "current" "$code"',
|
|
311
|
-
"fi",
|
|
312
|
-
// A release deployed before `.env` became a link holds a copy: keep the edits made to it.
|
|
313
|
-
'if [ -f "$project/.env" ] && { [ ! -f "$shared_env" ] || [ "$project/.env" -nt "$shared_env" ]; }; then',
|
|
314
|
-
' cp -pf "$project/.env" "$shared_env" && chmod 600 "$shared_env"',
|
|
315
|
-
"fi",
|
|
316
|
-
'if [ -L "$current" ]; then prev_release="$(basename "$(readlink "$current")")"; fi',
|
|
317
|
-
'rm -rf "$release_root"',
|
|
318
|
-
'mkdir -p "$release_root"',
|
|
319
|
-
`(cd "$incoming" && tar -c -f - --exclude=.env .) | (cd "$release_root" && tar -x -f -)`,
|
|
320
|
-
'chmod 755 "$release_root"',
|
|
321
|
-
'mkdir -p "$release_project"',
|
|
322
|
-
'if [ -f "$shared_env" ]; then ln -sfn "$shared_env" "$release_project/.env"; fi',
|
|
323
|
-
'rm -rf "$incoming"',
|
|
324
|
-
// Uploads a deploy abandoned before it took the lock.
|
|
325
|
-
'find "$(dirname "$incoming")" -mindepth 1 -maxdepth 1 -mmin +1440 -exec rm -rf {} + 2>/dev/null || true',
|
|
326
|
-
].join("\n")
|
|
327
|
-
}
|
|
328
|
-
|
|
329
|
-
/** Downloads the pinned Bun once per user and version, checking it against the release's sums. */
|
|
330
|
-
function renderInstallBun(version: string): string {
|
|
331
|
-
const release = `https://github.com/oven-sh/bun/releases/download/bun-v${version}`
|
|
332
|
-
return [
|
|
333
|
-
'if [ ! -x "$BUN" ]; then',
|
|
334
|
-
' case "$(uname -m)" in',
|
|
335
|
-
" x86_64 | amd64) arch=x64 ;;",
|
|
336
|
-
" aarch64 | arm64) arch=aarch64 ;;",
|
|
337
|
-
' *) echo "Bun has no Linux build for $(uname -m)." >&2; exit 1 ;;',
|
|
338
|
-
" esac",
|
|
339
|
-
// Bun's default x64 build needs AVX2; older CPUs take the baseline build.
|
|
340
|
-
' if [ "$arch" = x64 ] && ! grep -q avx2 /proc/cpuinfo; then arch=x64-baseline; fi',
|
|
341
|
-
' asset="bun-linux-$arch"',
|
|
342
|
-
' download="$(mktemp -d)"',
|
|
343
|
-
` curl -fsSL --retry 3 -o "$download/$asset.zip" ${shellQuote(release)}"/$asset.zip"`,
|
|
344
|
-
` curl -fsSL --retry 3 -o "$download/SHASUMS256.txt" ${shellQuote(release)}/SHASUMS256.txt`,
|
|
345
|
-
' (cd "$download" && grep " $asset.zip\\$" SHASUMS256.txt | sha256sum -c --quiet -)',
|
|
346
|
-
' unzip -q "$download/$asset.zip" -d "$download"',
|
|
347
|
-
' mkdir -p "$BUN_DIR"',
|
|
348
|
-
' mv "$download/$asset/bun" "$BUN"',
|
|
349
|
-
' rm -rf "$download"',
|
|
350
|
-
"fi",
|
|
351
|
-
// Dependency lifecycle scripts call `node`; with none installed, Bun runs them.
|
|
352
|
-
'command -v node > /dev/null || ln -s "$BUN" "$BUN_DIR/node"',
|
|
353
|
-
'"$BUN" --version',
|
|
354
|
-
].join("\n")
|
|
355
|
-
}
|
|
356
|
-
|
|
357
|
-
function renderInstallDependencies(): string {
|
|
358
|
-
return [
|
|
359
|
-
'cd "$RELEASE_PROJECT"',
|
|
360
|
-
'"$BUN" install --frozen-lockfile --backend=hardlink',
|
|
361
|
-
"for bin in sixb sixb-deploy-ssh; do",
|
|
362
|
-
' if [ ! -e "$RELEASE_PROJECT/node_modules/.bin/$bin" ]; then',
|
|
363
|
-
' package="@sixb/cli"; [ "$bin" = sixb ] || package="@sixb/deploy-ssh"',
|
|
364
|
-
' echo "$package is not installed. Add it to the project dependencies." >&2',
|
|
365
|
-
" exit 1",
|
|
366
|
-
" fi",
|
|
367
|
-
"done",
|
|
368
|
-
].join("\n")
|
|
369
|
-
}
|
|
370
|
-
|
|
371
|
-
function isStorageMigration(command: DeployCommand): boolean {
|
|
372
|
-
return command.program === "sixb" && command.args[0] === "db" && command.args[1] === "migrate"
|
|
373
|
-
}
|
|
374
|
-
|
|
375
|
-
/**
|
|
376
|
-
* The live release cannot run against a schema it does not know, so a release with storage
|
|
377
|
-
* migrations pending stops the services before migrating. "Start new release" then finds nothing
|
|
378
|
-
* running and starts it cold. `sixb db migrate --check` reads the schema without changing it, and
|
|
379
|
-
* exits 3 when migrations are pending. A release whose `sixb` predates `--check` rejects the
|
|
380
|
-
* option with 2: nothing says the schema stays, so the services stop too.
|
|
381
|
-
*/
|
|
382
|
-
function stopForSchemaChange(): Step {
|
|
383
|
-
return {
|
|
384
|
-
label: "Check storage migrations",
|
|
385
|
-
body: [
|
|
386
|
-
'cd "$RELEASE_PROJECT"',
|
|
387
|
-
"migrations=0",
|
|
388
|
-
'"$BUN" "$RELEASE_PROJECT/node_modules/.bin/sixb" db migrate --check || migrations=$?',
|
|
389
|
-
"",
|
|
390
|
-
'case "$migrations" in',
|
|
391
|
-
" 0) ;;",
|
|
392
|
-
" 2 | 3)",
|
|
393
|
-
' if [ "$migrations" = 3 ]; then',
|
|
394
|
-
" echo 'This release changes the storage schema, so the services stop while it migrates.'",
|
|
395
|
-
" else",
|
|
396
|
-
" echo 'This release cannot tell whether its schema changes, so the services stop while it migrates.'",
|
|
397
|
-
" fi",
|
|
398
|
-
' if systemctl --user is-active --quiet "$UNIT"; then systemctl --user stop "$UNIT"; fi',
|
|
399
|
-
' deploy_mode="stopped"',
|
|
400
|
-
" ;;",
|
|
401
|
-
' *) exit "$migrations" ;;',
|
|
402
|
-
"esac",
|
|
403
|
-
].join("\n"),
|
|
404
|
-
}
|
|
405
|
-
}
|
|
406
|
-
|
|
407
|
-
function runStep(command: DeployCommand): Step {
|
|
408
|
-
const program = command.program === "sixb" ? '"$RELEASE_PROJECT/node_modules/.bin/sixb"' : ""
|
|
409
|
-
return {
|
|
410
|
-
label: [command.program, ...command.args].join(" "),
|
|
411
|
-
body: [
|
|
412
|
-
'cd "$RELEASE_PROJECT"',
|
|
413
|
-
`"$BUN" ${program} ${command.args.map(shellQuote).join(" ")}`.replace(/ {2,}/g, " "),
|
|
414
|
-
].join("\n"),
|
|
415
|
-
}
|
|
416
|
-
}
|
|
417
|
-
|
|
418
|
-
/**
|
|
419
|
-
* Pulls `.env` with the token only this step reads. The pull replaces the release's link with a
|
|
420
|
-
* file, which becomes the server's `shared/.env` before the link comes back.
|
|
421
|
-
*/
|
|
422
|
-
function envStep(command: DeployCommand, layout: DeploymentLayout): Step {
|
|
423
|
-
const step = runStep(command)
|
|
424
|
-
return {
|
|
425
|
-
...step,
|
|
426
|
-
body: [
|
|
427
|
-
`${step.body} --token-file ${shellQuote(layout.envToken)}`,
|
|
428
|
-
'if [ -f "$RELEASE_PROJECT/.env" ] && [ ! -L "$RELEASE_PROJECT/.env" ]; then',
|
|
429
|
-
' mv -f "$RELEASE_PROJECT/.env" "$SHARED_ENV" && chmod 600 "$SHARED_ENV"',
|
|
430
|
-
' ln -sfn "$SHARED_ENV" "$RELEASE_PROJECT/.env"',
|
|
431
|
-
"fi",
|
|
432
|
-
].join("\n"),
|
|
433
|
-
}
|
|
434
|
-
}
|
|
435
|
-
|
|
436
|
-
/**
|
|
437
|
-
* Carries one generation of immutable content-hashed assets from the previous `current` build into
|
|
438
|
-
* the new `.sixb/dist/app` directory so open browser tabs never 404 on lazy chunks after cutover.
|
|
439
|
-
*/
|
|
440
|
-
function withCarriedAssets(step: Step): Step {
|
|
441
|
-
return {
|
|
442
|
-
...step,
|
|
443
|
-
body: [
|
|
444
|
-
step.body,
|
|
445
|
-
'new_app="$RELEASE_PROJECT/.sixb/dist/app"',
|
|
446
|
-
'old_app="$PROJECT/.sixb/dist/app"',
|
|
447
|
-
'own_list="$RELEASE_PROJECT/.sixb/dist/.own-assets"',
|
|
448
|
-
'old_own="$PROJECT/.sixb/dist/.own-assets"',
|
|
449
|
-
'if [ -d "$new_app" ]; then',
|
|
450
|
-
' (cd "$new_app" && find . -type f | LC_ALL=C sort) > "$own_list"',
|
|
451
|
-
' if [ -d "$old_app" ]; then',
|
|
452
|
-
' source_list="$old_own"',
|
|
453
|
-
' if [ ! -f "$source_list" ]; then',
|
|
454
|
-
' source_list="$(mktemp)"',
|
|
455
|
-
' (cd "$old_app" && find . -type f | LC_ALL=C sort) > "$source_list"',
|
|
456
|
-
' temp_source_list="$source_list"',
|
|
457
|
-
" else",
|
|
458
|
-
' temp_source_list=""',
|
|
459
|
-
" fi",
|
|
460
|
-
" while IFS= read -r rel; do",
|
|
461
|
-
' base="$(basename "$rel")"',
|
|
462
|
-
' case "$base" in',
|
|
463
|
-
" chunk-* | asset-* | app-*.* | shared-app-*.* | auth-*.*)",
|
|
464
|
-
' if [ ! -e "$new_app/$rel" ] && [ -f "$old_app/$rel" ]; then',
|
|
465
|
-
' mkdir -p "$(dirname "$new_app/$rel")"',
|
|
466
|
-
' ln "$old_app/$rel" "$new_app/$rel" 2>/dev/null || cp -p "$old_app/$rel" "$new_app/$rel"',
|
|
467
|
-
" fi",
|
|
468
|
-
" ;;",
|
|
469
|
-
" esac",
|
|
470
|
-
' done < "$source_list"',
|
|
471
|
-
' if [ -n "$temp_source_list" ]; then rm -f "$temp_source_list"; fi',
|
|
472
|
-
" fi",
|
|
473
|
-
"fi",
|
|
474
|
-
].join("\n"),
|
|
475
|
-
}
|
|
476
|
-
}
|
|
477
|
-
|
|
478
|
-
function renderStartCandidate(input: {
|
|
479
|
-
readonly layout: DeploymentLayout
|
|
480
|
-
readonly targetRelease: ReleaseLayout
|
|
481
|
-
readonly blueRelease: DeployRelease
|
|
482
|
-
readonly greenRelease: DeployRelease
|
|
483
|
-
readonly slots: SlotPorts
|
|
484
|
-
}): string {
|
|
485
|
-
const { layout, targetRelease, blueRelease, greenRelease, slots } = input
|
|
486
|
-
const blueManifest = renderProcessManifest(blueRelease, layout, {
|
|
487
|
-
projectDir: targetRelease.project,
|
|
488
|
-
slot: "blue",
|
|
489
|
-
})
|
|
490
|
-
const greenManifest = renderProcessManifest(greenRelease, layout, {
|
|
491
|
-
projectDir: targetRelease.project,
|
|
492
|
-
slot: "green",
|
|
493
|
-
})
|
|
494
|
-
const installServiceFiles = renderInstallServiceFiles(blueRelease, layout, slots)
|
|
495
|
-
|
|
496
|
-
return [
|
|
497
|
-
writeFileCommand(
|
|
498
|
-
`${layout.deployDir}/manifest.blue.json`,
|
|
499
|
-
`${JSON.stringify(blueManifest, null, 2)}\n`
|
|
500
|
-
),
|
|
501
|
-
writeFileCommand(
|
|
502
|
-
`${layout.deployDir}/manifest.green.json`,
|
|
503
|
-
`${JSON.stringify(greenManifest, null, 2)}\n`
|
|
504
|
-
),
|
|
505
|
-
// Blue-green needs a running supervisor that knows `stage`. Anything else starts cold: the
|
|
506
|
-
// first deploy, a stopped deployment, or a release whose migrations stopped the services.
|
|
507
|
-
"start_cold=1",
|
|
508
|
-
'if [ -f "$MANIFEST" ] && systemctl --user is-active --quiet "$UNIT"; then start_cold=0; fi',
|
|
509
|
-
"",
|
|
510
|
-
// Temporary, for the first deploy after upgrading to blue-green deploys (#1174): the
|
|
511
|
-
// supervisor still running predates `stage` and never answers it. Its manifest is the only
|
|
512
|
-
// one without a `slot`, so it is restarted cold once. Remove this once no server runs a
|
|
513
|
-
// supervisor from before #1174.
|
|
514
|
-
'if [ "$start_cold" = 0 ] && ! grep -qs \'"slot":\' "$MANIFEST"; then',
|
|
515
|
-
" echo 'The running supervisor predates blue-green deploys, so this deploy restarts it once.'",
|
|
516
|
-
" start_cold=1",
|
|
517
|
-
"fi",
|
|
518
|
-
"",
|
|
519
|
-
'if [ "$start_cold" = 0 ]; then',
|
|
520
|
-
' if grep -qs \'"slot": "green"\' "$MANIFEST"; then target_slot="blue"; else target_slot="green"; fi',
|
|
521
|
-
' cp "$DEPLOY/manifest.$target_slot.json" "$CANDIDATE_MANIFEST"',
|
|
522
|
-
' "$BUN" "$RELEASE_PROJECT/node_modules/.bin/sixb-deploy-ssh" stage "$CANDIDATE_MANIFEST" --manifest "$MANIFEST"',
|
|
523
|
-
' deploy_mode="staged"',
|
|
524
|
-
" started=$SECONDS",
|
|
525
|
-
"else",
|
|
526
|
-
' target_slot="blue"',
|
|
527
|
-
" use_release",
|
|
528
|
-
' deploy_mode="cold"',
|
|
529
|
-
...installServiceFiles.map((line) => ` ${line}`),
|
|
530
|
-
' mv "$DEPLOY/manifest.blue.json" "$MANIFEST"',
|
|
531
|
-
' rm -f "$DEPLOY/manifest.green.json" "$CANDIDATE_MANIFEST"',
|
|
532
|
-
' systemctl --user restart "$UNIT"',
|
|
533
|
-
' systemctl --user is-active --quiet "$UNIT"',
|
|
534
|
-
" started=$SECONDS",
|
|
535
|
-
"fi",
|
|
536
|
-
].join("\n")
|
|
537
|
-
}
|
|
538
|
-
|
|
539
|
-
/**
|
|
540
|
-
* Done means healthy, not just started: the API answers its readiness route, and no process has
|
|
541
|
-
* exited in its first seconds. Those seconds count from "Start new release", so the time the API
|
|
542
|
-
* takes to boot is part of them rather than added to them.
|
|
543
|
-
*/
|
|
544
|
-
export function renderCheckServices(input: {
|
|
545
|
-
readonly blueReadyUrl: string | null
|
|
546
|
-
readonly greenReadyUrl?: string | null
|
|
547
|
-
}): string {
|
|
548
|
-
const blueUrl = input.blueReadyUrl
|
|
549
|
-
const greenUrl = input.greenReadyUrl ?? input.blueReadyUrl
|
|
550
|
-
const waitForApi =
|
|
551
|
-
blueUrl || greenUrl
|
|
552
|
-
? [
|
|
553
|
-
`ready_url=${shellQuote(blueUrl ?? "")}`,
|
|
554
|
-
'if [ "$target_slot" = "green" ]; then',
|
|
555
|
-
` ready_url=${shellQuote(greenUrl ?? "")}`,
|
|
556
|
-
"fi",
|
|
557
|
-
'if [ -n "$ready_url" ]; then',
|
|
558
|
-
" ready=0",
|
|
559
|
-
" for attempt in $(seq 1 90); do",
|
|
560
|
-
` if "$BUN" -e 'const r = await fetch(process.argv[1]).catch(() => null); process.exit(r?.ok ? 0 : 1)' "$ready_url" 2>/dev/null; then`,
|
|
561
|
-
" ready=1",
|
|
562
|
-
" break",
|
|
563
|
-
" fi",
|
|
564
|
-
" sleep 1",
|
|
565
|
-
" done",
|
|
566
|
-
' if [ "$ready" != 1 ]; then',
|
|
567
|
-
' echo "The API did not answer $ready_url within 90 seconds. Its latest log lines:" >&2',
|
|
568
|
-
' "$CTL" logs api --tail 30 >&2 || true',
|
|
569
|
-
" exit 1",
|
|
570
|
-
" fi",
|
|
571
|
-
"fi",
|
|
572
|
-
]
|
|
573
|
-
: []
|
|
574
|
-
return [
|
|
575
|
-
...waitForApi,
|
|
576
|
-
"quiet=$((5 - (SECONDS - started)))",
|
|
577
|
-
'if [ "$quiet" -gt 0 ]; then sleep "$quiet"; fi',
|
|
578
|
-
'if [ "$deploy_mode" = "staged" ]; then',
|
|
579
|
-
' "$BUN" "$RELEASE_PROJECT/node_modules/.bin/sixb-deploy-ssh" status --check --candidate --manifest "$MANIFEST"',
|
|
580
|
-
"else",
|
|
581
|
-
' "$CTL" status --check',
|
|
582
|
-
"fi",
|
|
583
|
-
].join("\n")
|
|
584
|
-
}
|
|
585
|
-
|
|
586
|
-
/**
|
|
587
|
-
* Installs the deployment's Caddy routes for the verified slot. Caddy has to accept the whole
|
|
588
|
-
* configuration before it reloads; otherwise the previous routes come back and the deploy stops.
|
|
589
|
-
*/
|
|
590
|
-
function renderUpdateRoutes(input: {
|
|
591
|
-
readonly layout: DeploymentLayout
|
|
592
|
-
readonly blueCaddy: string
|
|
593
|
-
readonly greenCaddy: string
|
|
594
|
-
}): string {
|
|
595
|
-
const { layout, blueCaddy, greenCaddy } = input
|
|
596
|
-
return [
|
|
597
|
-
writeFileCommand(`${layout.deployDir}/Caddyfile.blue`, blueCaddy),
|
|
598
|
-
writeFileCommand(`${layout.deployDir}/Caddyfile.green`, greenCaddy),
|
|
599
|
-
'new="$DEPLOY/Caddyfile.$target_slot"',
|
|
600
|
-
'cp "$new" "$DEPLOY/Caddyfile"',
|
|
601
|
-
'rm -f "$DEPLOY/Caddyfile.blue" "$DEPLOY/Caddyfile.green"',
|
|
602
|
-
'if cmp -s "$DEPLOY/Caddyfile" "$CADDY_SNIPPET"; then',
|
|
603
|
-
" echo 'Routes unchanged.'",
|
|
604
|
-
"else",
|
|
605
|
-
' if [ ! -w "$CADDY_DIR" ]; then',
|
|
606
|
-
' echo "$CADDY_DIR is missing or not writable, so this server is not set up for sixb deploy." >&2',
|
|
607
|
-
" exit 1",
|
|
608
|
-
" fi",
|
|
609
|
-
' rm -f "$PREVIOUS_ROUTES"',
|
|
610
|
-
' if [ -f "$CADDY_SNIPPET" ]; then cp "$CADDY_SNIPPET" "$PREVIOUS_ROUTES"; fi',
|
|
611
|
-
' cp "$DEPLOY/Caddyfile" "$CADDY_SNIPPET"',
|
|
612
|
-
"",
|
|
613
|
-
' if ! caddy validate --config /etc/caddy/Caddyfile --adapter caddyfile > "$DEPLOY/caddy-validate.log" 2>&1; then',
|
|
614
|
-
" restore_routes",
|
|
615
|
-
' tail -n 20 "$DEPLOY/caddy-validate.log" >&2',
|
|
616
|
-
" echo 'Caddy rejected the routes; the previous ones are back in place.' >&2",
|
|
617
|
-
" exit 1",
|
|
618
|
-
" fi",
|
|
619
|
-
"",
|
|
620
|
-
// Caddy keeps the routes it had when a reload fails: the file goes back to match them.
|
|
621
|
-
" if ! sudo -n /usr/bin/systemctl reload caddy; then",
|
|
622
|
-
" restore_routes",
|
|
623
|
-
" echo 'Caddy did not reload; the previous routes are back in place.' >&2",
|
|
624
|
-
" exit 1",
|
|
625
|
-
" fi",
|
|
626
|
-
"",
|
|
627
|
-
// From here a failure has to bring traffic back to the live release before discarding.
|
|
628
|
-
' if [ "$deploy_mode" = "staged" ]; then deploy_mode="routed"; fi',
|
|
629
|
-
"fi",
|
|
630
|
-
].join("\n")
|
|
631
|
-
}
|
|
632
|
-
|
|
633
|
-
function renderInstallServiceFiles(
|
|
634
|
-
release: DeployRelease,
|
|
635
|
-
layout: DeploymentLayout,
|
|
636
|
-
slots: SlotPorts
|
|
637
|
-
): string[] {
|
|
638
|
-
const reservedPorts = [
|
|
639
|
-
...new Set(
|
|
640
|
-
release.services.flatMap((service) =>
|
|
641
|
-
service.http && (service.name === "api" || service.name === "app")
|
|
642
|
-
? [slots.blue[service.name], slots.green[service.name]]
|
|
643
|
-
: []
|
|
644
|
-
)
|
|
645
|
-
),
|
|
646
|
-
]
|
|
647
|
-
.map(String)
|
|
648
|
-
.join("\n")
|
|
649
|
-
|
|
650
|
-
return [
|
|
651
|
-
writeFileCommand(`${layout.deployDir}/ports.txt`, reservedPorts ? `${reservedPorts}\n` : ""),
|
|
652
|
-
writeFileCommand(layout.ctl, renderCtl(layout)),
|
|
653
|
-
'chmod 755 "$CTL"',
|
|
654
|
-
'mkdir -p "$(dirname "$UNIT_PATH")"',
|
|
655
|
-
writeFileCommand(`${layout.unit}.new`, renderUnit(release, layout)),
|
|
656
|
-
'if cmp -s "$UNIT_PATH.new" "$UNIT_PATH"; then',
|
|
657
|
-
' rm -f "$UNIT_PATH.new"',
|
|
658
|
-
"else",
|
|
659
|
-
' mv "$UNIT_PATH.new" "$UNIT_PATH"',
|
|
660
|
-
" systemctl --user daemon-reload",
|
|
661
|
-
' systemctl --user enable "$UNIT"',
|
|
662
|
-
"fi",
|
|
663
|
-
]
|
|
664
|
-
}
|
|
665
|
-
|
|
666
|
-
function renderActivateRelease(input: {
|
|
667
|
-
readonly release: DeployRelease
|
|
668
|
-
readonly layout: DeploymentLayout
|
|
669
|
-
readonly slots: SlotPorts
|
|
670
|
-
}): string {
|
|
671
|
-
const hasExclusiveServices = input.release.services.some(
|
|
672
|
-
(service) => service.kind === "singleton" || service.kind === "script"
|
|
673
|
-
)
|
|
674
|
-
const installServiceFiles = renderInstallServiceFiles(input.release, input.layout, input.slots)
|
|
675
|
-
return [
|
|
676
|
-
'if [ "$deploy_mode" = "staged" ] || [ "$deploy_mode" = "routed" ]; then',
|
|
677
|
-
...installServiceFiles.map((line) => ` ${line}`),
|
|
678
|
-
" use_release",
|
|
679
|
-
' deploy_mode="activated"',
|
|
680
|
-
' mv "$CANDIDATE_MANIFEST" "$MANIFEST"',
|
|
681
|
-
' rm -f "$DEPLOY/manifest.blue.json" "$DEPLOY/manifest.green.json"',
|
|
682
|
-
' "$CTL" activate',
|
|
683
|
-
...(hasExclusiveServices ? [" sleep 2"] : []),
|
|
684
|
-
' "$CTL" status --check',
|
|
685
|
-
"fi",
|
|
686
|
-
renderPruneReleases(),
|
|
687
|
-
].join("\n")
|
|
688
|
-
}
|
|
689
|
-
|
|
690
|
-
/** Keeps the active release, the previous release, and any release still draining in `state.json`. */
|
|
691
|
-
export function renderPruneReleases(): string {
|
|
692
|
-
return [
|
|
693
|
-
'if [ -d "$RELEASES_DIR" ]; then',
|
|
694
|
-
' for candidate_dir in "$RELEASES_DIR"/*; do',
|
|
695
|
-
' [ -d "$candidate_dir" ] || continue',
|
|
696
|
-
' name="$(basename "$candidate_dir")"',
|
|
697
|
-
' if [ "$name" = "$RELEASE_ID" ] || [ "$name" = "$prev_release" ]; then continue; fi',
|
|
698
|
-
' if [ -f "$STATE_FILE" ] && grep -qsF "$candidate_dir" "$STATE_FILE"; then continue; fi',
|
|
699
|
-
' rm -rf "$candidate_dir"',
|
|
700
|
-
" done",
|
|
701
|
-
"fi",
|
|
702
|
-
].join("\n")
|
|
703
|
-
}
|