@sixb/deploy-ssh 0.2.5 → 0.2.6
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 +7 -5
- package/dist/check.d.ts.map +1 -1
- package/dist/index.js +543 -118
- package/dist/index.js.map +11 -10
- package/dist/layout.d.ts +21 -5
- package/dist/layout.d.ts.map +1 -1
- package/dist/operations.d.ts +2 -1
- package/dist/operations.d.ts.map +1 -1
- package/dist/render.d.ts +10 -4
- package/dist/render.d.ts.map +1 -1
- package/dist/scripts.d.ts +20 -10
- package/dist/scripts.d.ts.map +1 -1
- package/dist/server/manifest.d.ts +6 -0
- package/dist/server/manifest.d.ts.map +1 -1
- package/dist/server/state.d.ts +6 -0
- package/dist/server/state.d.ts.map +1 -1
- package/dist/server/supervisor.d.ts +11 -3
- package/dist/server/supervisor.d.ts.map +1 -1
- package/dist/ssh-target.d.ts +6 -0
- package/dist/ssh-target.d.ts.map +1 -1
- package/package.json +3 -3
- package/src/check.ts +32 -6
- package/src/layout.ts +40 -5
- package/src/operations.ts +4 -1
- package/src/render.ts +88 -10
- package/src/scripts.ts +492 -112
- package/src/server/cli.ts +30 -9
- package/src/server/manifest.ts +32 -1
- package/src/server/state.ts +8 -2
- package/src/server/supervisor.ts +329 -64
- package/src/ssh-target.ts +72 -11
package/src/scripts.ts
CHANGED
|
@@ -1,6 +1,18 @@
|
|
|
1
|
-
import type {
|
|
2
|
-
|
|
3
|
-
|
|
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"
|
|
4
16
|
import { shellQuote, writeFileCommand } from "./shell"
|
|
5
17
|
|
|
6
18
|
/** A line the deploy script prints around each step; the target turns it into progress. */
|
|
@@ -12,8 +24,8 @@ export function renderPrepareScript(name: string, commit: string): string {
|
|
|
12
24
|
`root="$HOME"/${shellQuote(name)}`,
|
|
13
25
|
'mkdir -p "$root/deploy/incoming"',
|
|
14
26
|
`incoming="$(mktemp -d "$root/deploy/incoming/${commit.slice(0, 12)}.XXXXXX")"`,
|
|
15
|
-
// `mktemp` makes it 0700, and GNU tar passes the mode of the upload's `.` on to
|
|
16
|
-
// would lock out the admin, who reads the code through the deploy user's group.
|
|
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.
|
|
17
29
|
'chmod 755 "$incoming"',
|
|
18
30
|
`printf '%s\\n%s\\n' "$HOME" "$incoming"`,
|
|
19
31
|
].join("\n")
|
|
@@ -30,6 +42,8 @@ export interface DeployScriptInput {
|
|
|
30
42
|
readonly layout: DeploymentLayout
|
|
31
43
|
readonly incoming: string
|
|
32
44
|
readonly record: DeployRecord
|
|
45
|
+
readonly releaseId?: string
|
|
46
|
+
readonly slotPorts?: SlotPorts
|
|
33
47
|
}
|
|
34
48
|
|
|
35
49
|
export interface DeployScript {
|
|
@@ -42,44 +56,97 @@ interface Step {
|
|
|
42
56
|
readonly body: string
|
|
43
57
|
}
|
|
44
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
|
+
|
|
45
79
|
/**
|
|
46
80
|
* Everything the server does once the source is uploaded, as one script that holds the
|
|
47
|
-
* deployment's lock throughout.
|
|
48
|
-
*
|
|
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.
|
|
49
83
|
*/
|
|
50
84
|
export function renderDeployScript(input: DeployScriptInput): DeployScript {
|
|
51
85
|
const { release, layout } = input
|
|
52
|
-
const
|
|
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
|
+
|
|
53
97
|
const steps: Step[] = [
|
|
54
|
-
{
|
|
98
|
+
{
|
|
99
|
+
label: "Prepare release",
|
|
100
|
+
body: renderPrepareRelease(layout, targetRelease, input.incoming),
|
|
101
|
+
},
|
|
55
102
|
{ label: `Install Bun ${release.bunVersion}`, body: renderInstallBun(release.bunVersion) },
|
|
56
103
|
{ label: "Install dependencies", body: renderInstallDependencies() },
|
|
57
104
|
...(release.steps.env ? [envStep(release.steps.env, layout)] : []),
|
|
58
|
-
...release.steps.build.map((command) =>
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
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
|
+
},
|
|
66
121
|
{
|
|
67
122
|
label: "Check services",
|
|
68
|
-
body: renderCheckServices(
|
|
69
|
-
|
|
70
|
-
|
|
123
|
+
body: renderCheckServices({
|
|
124
|
+
blueReadyUrl: readinessUrl(blueRelease),
|
|
125
|
+
greenReadyUrl: readinessUrl(greenRelease),
|
|
126
|
+
}),
|
|
71
127
|
},
|
|
72
128
|
{
|
|
73
|
-
label: "
|
|
74
|
-
body:
|
|
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 }),
|
|
75
139
|
},
|
|
76
140
|
]
|
|
77
141
|
|
|
78
142
|
const script = [
|
|
79
143
|
"set -euo pipefail",
|
|
80
144
|
"main() {",
|
|
81
|
-
renderEnvironment(release, layout),
|
|
145
|
+
renderEnvironment(release, layout, targetRelease),
|
|
82
146
|
renderTakeLock(layout),
|
|
147
|
+
renderUseRelease(layout, input.record),
|
|
148
|
+
renderRestoreRoutes(),
|
|
149
|
+
renderCandidateTrap(),
|
|
83
150
|
...steps.flatMap((step, index) => [
|
|
84
151
|
`printf '\\n%s\\t%s\\t%s\\n' ${STEP_MARKER} ${index} start`,
|
|
85
152
|
step.body,
|
|
@@ -93,12 +160,26 @@ export function renderDeployScript(input: DeployScriptInput): DeployScript {
|
|
|
93
160
|
return { labels: steps.map((step) => step.label), script }
|
|
94
161
|
}
|
|
95
162
|
|
|
96
|
-
function renderEnvironment(
|
|
163
|
+
function renderEnvironment(
|
|
164
|
+
release: DeployRelease,
|
|
165
|
+
layout: DeploymentLayout,
|
|
166
|
+
targetRelease: ReleaseLayout
|
|
167
|
+
): string {
|
|
97
168
|
const variables: Record<string, string> = {
|
|
98
169
|
ROOT: layout.root,
|
|
170
|
+
RELEASES_DIR: layout.releasesDir,
|
|
171
|
+
CURRENT: layout.currentLink,
|
|
99
172
|
CODE: layout.code,
|
|
100
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,
|
|
101
179
|
DEPLOY: layout.deployDir,
|
|
180
|
+
MANIFEST: layout.manifest,
|
|
181
|
+
CANDIDATE_MANIFEST: layout.candidateManifest,
|
|
182
|
+
STATE_FILE: `${layout.runDir}/state.json`,
|
|
102
183
|
BUN: layout.bun,
|
|
103
184
|
BUN_DIR: layout.bunDir,
|
|
104
185
|
BUN_VERSION: release.bunVersion,
|
|
@@ -107,6 +188,7 @@ function renderEnvironment(release: DeployRelease, layout: DeploymentLayout): st
|
|
|
107
188
|
UNIT_PATH: layout.unit,
|
|
108
189
|
CADDY_DIR: layout.caddyDir,
|
|
109
190
|
CADDY_SNIPPET: layout.caddySnippet,
|
|
191
|
+
PREVIOUS_ROUTES: `${layout.deployDir}/Caddyfile.previous`,
|
|
110
192
|
}
|
|
111
193
|
return [
|
|
112
194
|
...Object.entries(variables).map(([name, value]) => `${name}=${shellQuote(value)}`),
|
|
@@ -115,6 +197,9 @@ function renderEnvironment(release: DeployRelease, layout: DeploymentLayout): st
|
|
|
115
197
|
'export PATH="$BUN_DIR:$PATH"',
|
|
116
198
|
// `systemctl --user` finds the user's systemd through this; SSH sessions usually set it.
|
|
117
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=""',
|
|
118
203
|
].join("\n")
|
|
119
204
|
}
|
|
120
205
|
|
|
@@ -132,30 +217,109 @@ function renderTakeLock(layout: DeploymentLayout): string {
|
|
|
132
217
|
}
|
|
133
218
|
|
|
134
219
|
/**
|
|
135
|
-
*
|
|
136
|
-
*
|
|
137
|
-
*
|
|
138
|
-
|
|
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.
|
|
139
247
|
*/
|
|
140
|
-
|
|
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 {
|
|
141
291
|
return [
|
|
142
292
|
`incoming=${shellQuote(incoming)}`,
|
|
143
|
-
`
|
|
293
|
+
`releases_dir=${shellQuote(layout.releasesDir)}`,
|
|
294
|
+
`current=${shellQuote(layout.currentLink)}`,
|
|
144
295
|
`code=${shellQuote(layout.code)}`,
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
'
|
|
151
|
-
|
|
152
|
-
'
|
|
153
|
-
'
|
|
154
|
-
|
|
155
|
-
"
|
|
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"',
|
|
156
311
|
"fi",
|
|
157
|
-
|
|
158
|
-
'
|
|
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',
|
|
159
323
|
'rm -rf "$incoming"',
|
|
160
324
|
// Uploads a deploy abandoned before it took the lock.
|
|
161
325
|
'find "$(dirname "$incoming")" -mindepth 1 -maxdepth 1 -mmin +1440 -exec rm -rf {} + 2>/dev/null || true',
|
|
@@ -192,10 +356,10 @@ function renderInstallBun(version: string): string {
|
|
|
192
356
|
|
|
193
357
|
function renderInstallDependencies(): string {
|
|
194
358
|
return [
|
|
195
|
-
'cd "$
|
|
196
|
-
'"$BUN" install --frozen-lockfile',
|
|
359
|
+
'cd "$RELEASE_PROJECT"',
|
|
360
|
+
'"$BUN" install --frozen-lockfile --backend=hardlink',
|
|
197
361
|
"for bin in sixb sixb-deploy-ssh; do",
|
|
198
|
-
' if [ ! -e "$
|
|
362
|
+
' if [ ! -e "$RELEASE_PROJECT/node_modules/.bin/$bin" ]; then',
|
|
199
363
|
' package="@sixb/cli"; [ "$bin" = sixb ] || package="@sixb/deploy-ssh"',
|
|
200
364
|
' echo "$package is not installed. Add it to the project dependencies." >&2',
|
|
201
365
|
" exit 1",
|
|
@@ -204,120 +368,336 @@ function renderInstallDependencies(): string {
|
|
|
204
368
|
].join("\n")
|
|
205
369
|
}
|
|
206
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
|
+
|
|
207
407
|
function runStep(command: DeployCommand): Step {
|
|
208
|
-
const program = command.program === "sixb" ? '"$
|
|
408
|
+
const program = command.program === "sixb" ? '"$RELEASE_PROJECT/node_modules/.bin/sixb"' : ""
|
|
209
409
|
return {
|
|
210
410
|
label: [command.program, ...command.args].join(" "),
|
|
211
411
|
body: [
|
|
212
|
-
'cd "$
|
|
412
|
+
'cd "$RELEASE_PROJECT"',
|
|
213
413
|
`"$BUN" ${program} ${command.args.map(shellQuote).join(" ")}`.replace(/ {2,}/g, " "),
|
|
214
414
|
].join("\n"),
|
|
215
415
|
}
|
|
216
416
|
}
|
|
217
417
|
|
|
218
|
-
/**
|
|
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
|
+
*/
|
|
219
422
|
function envStep(command: DeployCommand, layout: DeploymentLayout): Step {
|
|
220
423
|
const step = runStep(command)
|
|
221
|
-
return {
|
|
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
|
+
}
|
|
222
476
|
}
|
|
223
477
|
|
|
224
|
-
function
|
|
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
|
+
|
|
225
496
|
return [
|
|
226
|
-
|
|
227
|
-
|
|
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",
|
|
228
525
|
"else",
|
|
229
|
-
"
|
|
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",
|
|
230
535
|
"fi",
|
|
231
536
|
].join("\n")
|
|
232
537
|
}
|
|
233
538
|
|
|
234
|
-
|
|
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
|
+
: []
|
|
235
574
|
return [
|
|
236
|
-
|
|
237
|
-
|
|
238
|
-
|
|
239
|
-
|
|
240
|
-
|
|
241
|
-
'chmod 755 "$CTL"',
|
|
242
|
-
'mkdir -p "$(dirname "$UNIT_PATH")"',
|
|
243
|
-
writeFileCommand(`${layout.unit}.new`, renderUnit(release, layout)),
|
|
244
|
-
'if cmp -s "$UNIT_PATH.new" "$UNIT_PATH"; then',
|
|
245
|
-
' rm -f "$UNIT_PATH.new"',
|
|
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"',
|
|
246
580
|
"else",
|
|
247
|
-
'
|
|
248
|
-
" systemctl --user daemon-reload",
|
|
249
|
-
' systemctl --user enable "$UNIT"',
|
|
581
|
+
' "$CTL" status --check',
|
|
250
582
|
"fi",
|
|
251
583
|
].join("\n")
|
|
252
584
|
}
|
|
253
585
|
|
|
254
586
|
/**
|
|
255
|
-
* Installs the deployment's Caddy routes
|
|
587
|
+
* Installs the deployment's Caddy routes for the verified slot. Caddy has to accept the whole
|
|
256
588
|
* configuration before it reloads; otherwise the previous routes come back and the deploy stops.
|
|
257
589
|
*/
|
|
258
|
-
function renderUpdateRoutes(
|
|
590
|
+
function renderUpdateRoutes(input: {
|
|
591
|
+
readonly layout: DeploymentLayout
|
|
592
|
+
readonly blueCaddy: string
|
|
593
|
+
readonly greenCaddy: string
|
|
594
|
+
}): string {
|
|
595
|
+
const { layout, blueCaddy, greenCaddy } = input
|
|
259
596
|
return [
|
|
260
|
-
|
|
261
|
-
writeFileCommand(`${layout.deployDir}/Caddyfile`,
|
|
262
|
-
'
|
|
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',
|
|
263
603
|
" echo 'Routes unchanged.'",
|
|
264
604
|
"else",
|
|
265
605
|
' if [ ! -w "$CADDY_DIR" ]; then',
|
|
266
606
|
' echo "$CADDY_DIR is missing or not writable, so this server is not set up for sixb deploy." >&2',
|
|
267
607
|
" exit 1",
|
|
268
608
|
" fi",
|
|
269
|
-
'
|
|
270
|
-
'
|
|
271
|
-
'
|
|
272
|
-
|
|
609
|
+
' rm -f "$PREVIOUS_ROUTES"',
|
|
610
|
+
' if [ -f "$CADDY_SNIPPET" ]; then cp "$CADDY_SNIPPET" "$PREVIOUS_ROUTES"; fi',
|
|
611
|
+
' cp "$DEPLOY/Caddyfile" "$CADDY_SNIPPET"',
|
|
612
|
+
"",
|
|
273
613
|
' if ! caddy validate --config /etc/caddy/Caddyfile --adapter caddyfile > "$DEPLOY/caddy-validate.log" 2>&1; then',
|
|
274
|
-
|
|
614
|
+
" restore_routes",
|
|
275
615
|
' tail -n 20 "$DEPLOY/caddy-validate.log" >&2',
|
|
276
616
|
" echo 'Caddy rejected the routes; the previous ones are back in place.' >&2",
|
|
277
617
|
" exit 1",
|
|
278
618
|
" fi",
|
|
279
|
-
"
|
|
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',
|
|
280
629
|
"fi",
|
|
281
630
|
].join("\n")
|
|
282
631
|
}
|
|
283
632
|
|
|
284
|
-
function
|
|
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)
|
|
285
675
|
return [
|
|
286
|
-
'
|
|
287
|
-
|
|
288
|
-
|
|
289
|
-
"
|
|
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(),
|
|
290
687
|
].join("\n")
|
|
291
688
|
}
|
|
292
689
|
|
|
293
|
-
/**
|
|
294
|
-
|
|
295
|
-
* exited in its first seconds. Those seconds count from "Start services", so the time the API
|
|
296
|
-
* takes to boot is part of them rather than added to them.
|
|
297
|
-
*/
|
|
298
|
-
export function renderCheckServices(readinessUrl: string | null): string {
|
|
299
|
-
const waitForApi = readinessUrl
|
|
300
|
-
? [
|
|
301
|
-
`ready_url=${shellQuote(readinessUrl)}`,
|
|
302
|
-
"ready=0",
|
|
303
|
-
"for attempt in $(seq 1 90); do",
|
|
304
|
-
` if "$BUN" -e 'const r = await fetch(process.argv[1]).catch(() => null); process.exit(r?.ok ? 0 : 1)' "$ready_url" 2>/dev/null; then`,
|
|
305
|
-
" ready=1",
|
|
306
|
-
" break",
|
|
307
|
-
" fi",
|
|
308
|
-
" sleep 1",
|
|
309
|
-
"done",
|
|
310
|
-
'if [ "$ready" != 1 ]; then',
|
|
311
|
-
' echo "The API did not answer $ready_url within 90 seconds. Its latest log lines:" >&2',
|
|
312
|
-
' "$CTL" logs api --tail 30 >&2 || true',
|
|
313
|
-
" exit 1",
|
|
314
|
-
"fi",
|
|
315
|
-
]
|
|
316
|
-
: []
|
|
690
|
+
/** Keeps the active release, the previous release, and any release still draining in `state.json`. */
|
|
691
|
+
export function renderPruneReleases(): string {
|
|
317
692
|
return [
|
|
318
|
-
|
|
319
|
-
|
|
320
|
-
'
|
|
321
|
-
'"$
|
|
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",
|
|
322
702
|
].join("\n")
|
|
323
703
|
}
|