@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/dist/index.js CHANGED
@@ -139,7 +139,10 @@ import { posix } from "path";
139
139
  var ENV_TOKEN_FILE = "env-token";
140
140
  function deploymentLayout(input) {
141
141
  const root = posix.join(input.home, input.name);
142
+ const releasesDir = posix.join(root, "releases");
143
+ const currentLink = posix.join(root, "current");
142
144
  const code = posix.join(root, "code");
145
+ const sharedDir = posix.join(root, "shared");
143
146
  const deployDir = posix.join(root, "deploy");
144
147
  const runDir = posix.join(root, "run");
145
148
  const bunDir = posix.join(input.home, ".sixb", "bun", input.bunVersion);
@@ -147,14 +150,20 @@ function deploymentLayout(input) {
147
150
  const caddyDir = posix.join("/etc/caddy/sixb.d", input.user);
148
151
  return {
149
152
  root,
153
+ releasesDir,
154
+ currentLink,
150
155
  code,
156
+ sharedDir,
157
+ sharedEnv: posix.join(sharedDir, ".env"),
151
158
  envToken: posix.join(root, ENV_TOKEN_FILE),
152
159
  project: posix.join(code, input.projectPath),
160
+ projectPath: input.projectPath,
153
161
  deployDir,
154
162
  incomingDir: posix.join(deployDir, "incoming"),
155
163
  lock: posix.join(deployDir, "lock"),
156
164
  fileList: posix.join(deployDir, "files.txt"),
157
165
  manifest: posix.join(deployDir, "processes.json"),
166
+ candidateManifest: posix.join(deployDir, "candidate.json"),
158
167
  releaseRecord: posix.join(deployDir, "release.json"),
159
168
  ctl: posix.join(deployDir, "ctl"),
160
169
  runDir,
@@ -167,6 +176,14 @@ function deploymentLayout(input) {
167
176
  caddySnippet: posix.join(caddyDir, `${input.name}.caddy`)
168
177
  };
169
178
  }
179
+ function releaseLayout(layout, releaseId) {
180
+ const root = posix.join(layout.releasesDir, releaseId);
181
+ return {
182
+ id: releaseId,
183
+ root,
184
+ project: posix.join(root, layout.projectPath)
185
+ };
186
+ }
170
187
 
171
188
  // src/check.ts
172
189
  var CHECK_MARKER = "__SIXB_CHECK__";
@@ -174,11 +191,14 @@ var TOOLS = ["git", "curl", "unzip", "tar", "flock", "sha256sum", "caddy"];
174
191
  var ENV_TOKEN_CHECK = "project.env-token";
175
192
  function renderCheckScript(input) {
176
193
  const project = posix2.join(input.name, "code", input.projectPath);
194
+ const sharedEnv = posix2.join(input.name, "shared", ".env");
177
195
  const envToken = posix2.join(input.name, ENV_TOKEN_FILE);
178
196
  return [
179
197
  `emit() { printf '${CHECK_MARKER}\\t%s\\t%s\\t%s\\n' "$1" "$2" "$(printf '%s' "$3" | tr '\\t\\n' ' ')"; }`,
180
198
  `project="$HOME"/${shellQuote(project)}`,
199
+ `shared_env="$HOME"/${shellQuote(sharedEnv)}`,
181
200
  `manifest="$HOME"/${shellQuote(input.name)}/deploy/processes.json`,
201
+ `ports_file="$HOME"/${shellQuote(input.name)}/deploy/ports.txt`,
182
202
  'routes_dir="/etc/caddy/sixb.d/$USER"',
183
203
  `snippet="$routes_dir"/${shellQuote(`${input.name}.caddy`)}`,
184
204
  "",
@@ -193,6 +213,12 @@ function renderCheckScript(input) {
193
213
  'if [ -n "$missing" ]; then emit server.tools fixable "missing:$missing"',
194
214
  'else emit server.tools ok "Caddy $(caddy version 2>/dev/null | cut -d" " -f1), git, curl, unzip"; fi',
195
215
  "",
216
+ "if command -v caddy > /dev/null; then",
217
+ ' caddy_version="$(caddy version 2>/dev/null | cut -d" " -f1 | sed "s/^v//")"',
218
+ ' if [ "$(printf "%s\\n" 2.7.0 "$caddy_version" | sort -V | head -n 1)" = 2.7.0 ]; then emit server.caddy ok "$caddy_version"',
219
+ ' else emit server.caddy manual "$caddy_version is older than 2.7"; fi',
220
+ "fi",
221
+ "",
196
222
  'if [ -e "/var/lib/systemd/linger/$USER" ]; then emit server.linger ok "on for $USER"',
197
223
  'else emit server.linger fixable "off for $USER: services would stop when you log out"; fi',
198
224
  "",
@@ -221,20 +247,24 @@ function renderCheckScript(input) {
221
247
  `elif [ "$(stat -c %a "$token")" != 600 ]; then emit ${ENV_TOKEN_CHECK} fixable "$token is readable by others (mode $(stat -c %a "$token"))"`,
222
248
  `else emit ${ENV_TOKEN_CHECK} ok "$token"; fi`
223
249
  ] : [
224
- 'if [ ! -f "$project/.env" ]; then emit project.env manual "$project/.env does not exist"',
225
- 'elif [ "$(stat -c %a "$project/.env")" != 600 ]; then emit project.env fixable "$project/.env is readable by others (mode $(stat -c %a "$project/.env"))"',
226
- 'else emit project.env ok "$project/.env"; fi'
250
+ 'env_file="$project/.env"',
251
+ 'if [ -f "$shared_env" ]; then env_file="$shared_env"; fi',
252
+ 'if [ ! -f "$env_file" ]; then emit project.env manual "$env_file does not exist"',
253
+ 'elif [ "$(stat -c %a "$env_file")" != 600 ]; then emit project.env fixable "$env_file is readable by others (mode $(stat -c %a "$env_file"))"',
254
+ 'else emit project.env ok "$env_file"; fi'
227
255
  ],
228
256
  "",
229
257
  ...input.ports.flatMap(({ service, port }) => [
230
258
  `if ss -ltnH "sport = :${port}" 2> /dev/null | grep -q .; then`,
231
- ` if grep -qs '"--port",' "$manifest" && grep -A1 -s '"--port",' "$manifest" | grep -q '"${port}"'; then`,
259
+ ` if grep -qx '${port}' "$ports_file" 2> /dev/null || { grep -qs '"--port",' "$manifest" && grep -A1 -s '"--port",' "$manifest" | grep -q '"${port}"'; }; then`,
232
260
  ` emit project.port.${service} ok "${port}, used by this deployment"`,
233
261
  " else",
234
262
  ` emit project.port.${service} manual "${port} is in use by another program"`,
235
263
  " fi",
236
264
  `elif grep -ls '127.0.0.1:${port}\\b' /etc/caddy/sixb.d/*/*.caddy 2> /dev/null | grep -qv "^$snippet$"; then`,
237
265
  ` emit project.port.${service} manual "${port} is routed to another deployment"`,
266
+ `elif grep -lxs '${port}' "$HOME"/*/deploy/ports.txt 2> /dev/null | grep -qv "^$ports_file$"; then`,
267
+ ` emit project.port.${service} manual "${port} is reserved by another deployment"`,
238
268
  "else",
239
269
  ` emit project.port.${service} ok "${port}"`,
240
270
  "fi"
@@ -294,6 +324,7 @@ var LABELS = {
294
324
  "server.os": "Server",
295
325
  "server.tools": "Server tools",
296
326
  "server.linger": "Services at boot",
327
+ "server.caddy": "Caddy version",
297
328
  "server.reload": "Caddy reload",
298
329
  "server.routes": "Caddy routes",
299
330
  "server.admin-api": "Caddy admin API",
@@ -302,7 +333,7 @@ var LABELS = {
302
333
  [ENV_TOKEN_CHECK]: "Source token"
303
334
  };
304
335
  function describe(id, status, detail, context) {
305
- const label = LABELS[id] ?? (id.startsWith("project.port.") ? `${id.slice(13)} port` : id);
336
+ const label = LABELS[id] ?? (id.startsWith("project.port.") ? `${id.slice(13).replace(/-/g, " ")} port` : id);
306
337
  const check = { id, label, status, detail };
307
338
  if (status === "ok")
308
339
  return check;
@@ -318,7 +349,16 @@ function describe(id, status, detail, context) {
318
349
  };
319
350
  }
320
351
  if (id.startsWith("project.port.")) {
321
- return { ...check, remedy: "Give this service another port in the target's `ports`." };
352
+ return {
353
+ ...check,
354
+ remedy: "Give this service another port in the target's `ports` or `standbyPorts`."
355
+ };
356
+ }
357
+ if (id === "server.caddy") {
358
+ return {
359
+ ...check,
360
+ remedy: "Upgrade Caddy from its own apt repository: https://caddyserver.com/docs/install#debian-ubuntu-raspbian"
361
+ };
322
362
  }
323
363
  if (id === "server.admin-api") {
324
364
  return {
@@ -412,6 +452,21 @@ function renderCiInstall(input) {
412
452
  // src/operations.ts
413
453
  import { randomUUID } from "crypto";
414
454
 
455
+ // src/server/logs.ts
456
+ var MAX_LOG_BYTES = 20 * 1024 * 1024;
457
+ var RECENT_BYTES = 256 * 1024;
458
+ var TERMINAL_CONTROL = /\x1B(?:\[[0-?]*[ -/]*[@-~]|\][^\x07\x1B]*(?:\x07|\x1B\\)|[@-Z\\-_])/g;
459
+ function stripTerminalControl(line) {
460
+ return line.replace(TERMINAL_CONTROL, "");
461
+ }
462
+
463
+ // src/server/supervisor.ts
464
+ var RETIRING_KILL_TIMEOUT_MS = 300000;
465
+ var INTERRUPT_LEAD_MS = 1e4;
466
+ function retiringKillTimeoutMs(killTimeoutMs) {
467
+ return Math.max(killTimeoutMs, RETIRING_KILL_TIMEOUT_MS);
468
+ }
469
+
415
470
  // src/update-page.ts
416
471
  function renderUpdatePage() {
417
472
  return `<!doctype html>
@@ -456,22 +511,55 @@ function renderUpdatePage() {
456
511
 
457
512
  // src/render.ts
458
513
  var GENERATED = "Generated by sixb deploy. Changes are replaced on the next deploy.";
459
- function renderProcessManifest(release, layout) {
460
- const sixb = `${layout.project}/node_modules/.bin/sixb`;
514
+ function releaseForSlot(release, ports) {
515
+ return {
516
+ ...release,
517
+ services: release.services.map((service) => {
518
+ if (!service.http || service.name !== "api" && service.name !== "app") {
519
+ return service;
520
+ }
521
+ const port = ports[service.name];
522
+ return {
523
+ ...service,
524
+ command: {
525
+ ...service.command,
526
+ args: replacePortArg(service.command.args, port)
527
+ },
528
+ http: {
529
+ ...service.http,
530
+ port
531
+ }
532
+ };
533
+ })
534
+ };
535
+ }
536
+ function replacePortArg(args, port) {
537
+ const next = [...args];
538
+ const index = next.indexOf("--port");
539
+ if (index !== -1 && index + 1 < next.length) {
540
+ next[index + 1] = String(port);
541
+ }
542
+ return next;
543
+ }
544
+ function renderProcessManifest(release, layout, options = {}) {
545
+ const projectDir = options.projectDir ?? layout.project;
546
+ const sixb = `${projectDir}/node_modules/.bin/sixb`;
461
547
  return {
462
548
  version: 1,
463
549
  name: release.name,
464
- cwd: layout.project,
550
+ ...options.slot ? { slot: options.slot } : {},
551
+ cwd: projectDir,
465
552
  runDir: layout.runDir,
466
553
  logDir: layout.logDir,
467
554
  releasePath: layout.releaseRecord,
468
555
  env: release.env,
469
556
  services: release.services.map((service) => ({
470
557
  name: service.name,
558
+ kind: service.kind,
471
559
  processName: `${release.name}-${service.name}`,
472
560
  command: layout.bun,
473
561
  args: service.command.program === "sixb" ? [sixb, ...service.command.args] : [...service.command.args],
474
- env: service.env,
562
+ env: serviceEnvWithDrain(service),
475
563
  instances: service.instances,
476
564
  killTimeoutMs: service.process.killTimeoutMs,
477
565
  restartDelayMs: service.process.restartDelayMs,
@@ -479,6 +567,16 @@ function renderProcessManifest(release, layout) {
479
567
  }))
480
568
  };
481
569
  }
570
+ function serviceEnvWithDrain(service) {
571
+ if ((service.name === "api" || service.name === "workers") && service.env.SIXB_DRAIN_TIMEOUT_MS === undefined) {
572
+ const drainTimeoutMs = retiringKillTimeoutMs(service.process.killTimeoutMs) - INTERRUPT_LEAD_MS;
573
+ return {
574
+ SIXB_DRAIN_TIMEOUT_MS: String(drainTimeoutMs),
575
+ ...service.env
576
+ };
577
+ }
578
+ return { ...service.env };
579
+ }
482
580
  function renderCtl(layout) {
483
581
  return [
484
582
  "#!/bin/sh",
@@ -520,7 +618,10 @@ function renderCaddySnippet(release) {
520
618
  [
521
619
  `${service.http.domain} {`,
522
620
  ...service.name === "api" ? compressJson() : [],
523
- ` reverse_proxy ${service.http.host}:${service.http.port}`,
621
+ ` reverse_proxy ${service.http.host}:${service.http.port} {`,
622
+ "\t\tlb_try_duration 5s",
623
+ ...service.name === "api" ? keepWebSocketsThroughReload(service) : [],
624
+ "\t}",
524
625
  ...pages ? updatePageOnProxyErrors() : [],
525
626
  "}"
526
627
  ].join(`
@@ -532,6 +633,10 @@ function renderCaddySnippet(release) {
532
633
  `), ""].join(`
533
634
  `);
534
635
  }
636
+ function keepWebSocketsThroughReload(service) {
637
+ const seconds = retiringKillTimeoutMs(service.process.killTimeoutMs) / 1000;
638
+ return [` stream_close_delay ${seconds}s`];
639
+ }
535
640
  function compressJson() {
536
641
  return [
537
642
  "\tencode {",
@@ -588,35 +693,82 @@ function renderReceiveScript(incoming, path) {
588
693
  const directory = path === "." ? incoming : `${incoming}/${path}`;
589
694
  return `mkdir -p ${shellQuote(directory)} && tar -x -f - -C ${shellQuote(directory)}`;
590
695
  }
696
+ function defaultSlotPorts(release) {
697
+ const portOf = (name, fallback) => release.services.find((service) => service.name === name)?.http?.port ?? fallback;
698
+ const blueApp = portOf("app", 3001);
699
+ const blueApi = portOf("api", 3002);
700
+ return {
701
+ blue: { app: blueApp, api: blueApi },
702
+ green: {
703
+ app: blueApp + 10 <= 65535 ? blueApp + 10 : blueApp - 10,
704
+ api: blueApi + 10 <= 65535 ? blueApi + 10 : blueApi - 10
705
+ }
706
+ };
707
+ }
708
+ function deriveReleaseId(record) {
709
+ const stamp = record.deployedAt.replace(/[-:.]/g, "").slice(0, 15);
710
+ const sha = record.commit.slice(0, 12).replace(/[^a-zA-Z0-9_-]/g, "");
711
+ return `${stamp}Z-${sha || "release"}`;
712
+ }
591
713
  function renderDeployScript(input) {
592
714
  const { release, layout } = input;
593
- const api = release.services.find((service) => service.http?.readinessPath);
715
+ const releaseId = input.releaseId ?? deriveReleaseId(input.record);
716
+ const targetRelease = releaseLayout(layout, releaseId);
717
+ const slots = input.slotPorts ?? defaultSlotPorts(release);
718
+ const blueRelease = releaseForSlot(release, slots.blue);
719
+ const greenRelease = releaseForSlot(release, slots.green);
720
+ const readinessUrl = (candidate) => {
721
+ const api = candidate.services.find((service) => service.http?.readinessPath);
722
+ return api?.http ? `http://${api.http.host}:${api.http.port}${api.http.readinessPath}` : null;
723
+ };
594
724
  const steps = [
595
- { label: "Apply files", body: renderApplyFiles(layout, input.incoming) },
725
+ {
726
+ label: "Prepare release",
727
+ body: renderPrepareRelease(layout, targetRelease, input.incoming)
728
+ },
596
729
  { label: `Install Bun ${release.bunVersion}`, body: renderInstallBun(release.bunVersion) },
597
730
  { label: "Install dependencies", body: renderInstallDependencies() },
598
731
  ...release.steps.env ? [envStep(release.steps.env, layout)] : [],
599
- ...release.steps.build.map((command) => runStep(command)),
600
- { label: "Write services", body: renderWriteServices(release, layout) },
601
- { label: "Update routes", body: renderUpdateRoutes(release, layout) },
602
- { label: "Stop services", body: renderStopServices() },
603
- ...release.steps.beforeStart.map((command) => runStep(command)),
604
- { label: "Start services", body: renderStartServices() },
732
+ ...release.steps.build.map((command, index, all) => index === all.length - 1 ? withCarriedAssets(runStep(command)) : runStep(command)),
733
+ ...release.steps.beforeStart.flatMap((command) => isStorageMigration(command) ? [stopForSchemaChange(), runStep(command)] : [runStep(command)]),
734
+ {
735
+ label: "Start new release",
736
+ body: renderStartCandidate({
737
+ layout,
738
+ targetRelease,
739
+ blueRelease,
740
+ greenRelease,
741
+ slots
742
+ })
743
+ },
605
744
  {
606
745
  label: "Check services",
607
- body: renderCheckServices(api?.http ? `http://${api.http.host}:${api.http.port}${api.http.readinessPath}` : null)
746
+ body: renderCheckServices({
747
+ blueReadyUrl: readinessUrl(blueRelease),
748
+ greenReadyUrl: readinessUrl(greenRelease)
749
+ })
608
750
  },
609
751
  {
610
- label: "Record release",
611
- body: writeFileCommand(layout.releaseRecord, `${JSON.stringify(input.record)}
612
- `)
752
+ label: "Update routes",
753
+ body: renderUpdateRoutes({
754
+ layout,
755
+ blueCaddy: renderCaddySnippet(blueRelease),
756
+ greenCaddy: renderCaddySnippet(greenRelease)
757
+ })
758
+ },
759
+ {
760
+ label: "Activate release",
761
+ body: renderActivateRelease({ release: blueRelease, layout, slots })
613
762
  }
614
763
  ];
615
764
  const script = [
616
765
  "set -euo pipefail",
617
766
  "main() {",
618
- renderEnvironment(release, layout),
767
+ renderEnvironment(release, layout, targetRelease),
619
768
  renderTakeLock(layout),
769
+ renderUseRelease(layout, input.record),
770
+ renderRestoreRoutes(),
771
+ renderCandidateTrap(),
620
772
  ...steps.flatMap((step, index) => [
621
773
  `printf '\\n%s\\t%s\\t%s\\n' ${STEP_MARKER} ${index} start`,
622
774
  step.body,
@@ -629,12 +781,22 @@ function renderDeployScript(input) {
629
781
  `);
630
782
  return { labels: steps.map((step) => step.label), script };
631
783
  }
632
- function renderEnvironment(release, layout) {
784
+ function renderEnvironment(release, layout, targetRelease) {
633
785
  const variables = {
634
786
  ROOT: layout.root,
787
+ RELEASES_DIR: layout.releasesDir,
788
+ CURRENT: layout.currentLink,
635
789
  CODE: layout.code,
636
790
  PROJECT: layout.project,
791
+ SHARED_DIR: layout.sharedDir,
792
+ SHARED_ENV: layout.sharedEnv,
793
+ RELEASE_ID: targetRelease.id,
794
+ RELEASE_ROOT: targetRelease.root,
795
+ RELEASE_PROJECT: targetRelease.project,
637
796
  DEPLOY: layout.deployDir,
797
+ MANIFEST: layout.manifest,
798
+ CANDIDATE_MANIFEST: layout.candidateManifest,
799
+ STATE_FILE: `${layout.runDir}/state.json`,
638
800
  BUN: layout.bun,
639
801
  BUN_DIR: layout.bunDir,
640
802
  BUN_VERSION: release.bunVersion,
@@ -642,13 +804,17 @@ function renderEnvironment(release, layout) {
642
804
  UNIT: layout.unitName,
643
805
  UNIT_PATH: layout.unit,
644
806
  CADDY_DIR: layout.caddyDir,
645
- CADDY_SNIPPET: layout.caddySnippet
807
+ CADDY_SNIPPET: layout.caddySnippet,
808
+ PREVIOUS_ROUTES: `${layout.deployDir}/Caddyfile.previous`
646
809
  };
647
810
  return [
648
811
  ...Object.entries(variables).map(([name, value]) => `${name}=${shellQuote(value)}`),
649
812
  ...Object.entries(release.env).map(([name, value]) => `export ${name}=${shellQuote(value)}`),
650
813
  'export PATH="$BUN_DIR:$PATH"',
651
- 'printenv XDG_RUNTIME_DIR > /dev/null || export XDG_RUNTIME_DIR="/run/user/$(id -u)"'
814
+ 'printenv XDG_RUNTIME_DIR > /dev/null || export XDG_RUNTIME_DIR="/run/user/$(id -u)"',
815
+ 'deploy_mode="none"',
816
+ 'target_slot="blue"',
817
+ 'prev_release=""'
652
818
  ].join(`
653
819
  `);
654
820
  }
@@ -665,25 +831,88 @@ function renderTakeLock(layout) {
665
831
  ].join(`
666
832
  `);
667
833
  }
668
- function renderApplyFiles(layout, incoming) {
834
+ function renderUseRelease(layout, record) {
835
+ return [
836
+ "use_release() {",
837
+ ' ln -sfn "releases/$RELEASE_ID" "$CURRENT"',
838
+ ' ln -sfn "current" "$CODE"',
839
+ ` ${writeFileCommand(layout.releaseRecord, `${JSON.stringify(record)}
840
+ `)}`,
841
+ "}"
842
+ ].join(`
843
+ `);
844
+ }
845
+ function renderRestoreRoutes() {
846
+ return [
847
+ "restore_routes() {",
848
+ ' if [ -f "$PREVIOUS_ROUTES" ]; then cp "$PREVIOUS_ROUTES" "$CADDY_SNIPPET"; else rm -f "$CADDY_SNIPPET"; fi',
849
+ "}"
850
+ ].join(`
851
+ `);
852
+ }
853
+ function renderCandidateTrap() {
854
+ return [
855
+ "cleanup_candidate() {",
856
+ " status=$?",
857
+ ' if [ "$status" -eq 0 ]; then return; fi',
858
+ "",
859
+ ' case "$deploy_mode" in',
860
+ " staged | routed)",
861
+ ' if [ "$deploy_mode" = "routed" ]; then',
862
+ " restore_routes || true",
863
+ " if sudo -n /usr/bin/systemctl reload caddy > /dev/null 2>&1; then",
864
+ " echo 'The routes are back on the live release.' >&2",
865
+ " else",
866
+ " echo 'Caddy did not reload the previous routes: run sudo systemctl reload caddy on the server.' >&2",
867
+ " fi",
868
+ " fi",
869
+ ' "$BUN" "$RELEASE_PROJECT/node_modules/.bin/sixb-deploy-ssh" discard --manifest "$MANIFEST" > /dev/null 2>&1 || true',
870
+ ' rm -f "$CANDIDATE_MANIFEST" "$DEPLOY/manifest.blue.json" "$DEPLOY/manifest.green.json"',
871
+ " ;;",
872
+ " stopped)",
873
+ " echo 'The services were stopped for the storage migration and stay stopped. Fix what failed and deploy again.' >&2",
874
+ " ;;",
875
+ " activated | cold)",
876
+ " echo 'The new release is in place, but it did not pass its checks.' >&2",
877
+ " echo 'sixb deploy status and sixb deploy logs show what failed. To go back, deploy the previous commit with --ref.' >&2",
878
+ " ;;",
879
+ " esac",
880
+ "}",
881
+ "trap cleanup_candidate EXIT"
882
+ ].join(`
883
+ `);
884
+ }
885
+ function renderPrepareRelease(layout, targetRelease, incoming) {
669
886
  return [
670
887
  `incoming=${shellQuote(incoming)}`,
671
- `files=${shellQuote(layout.fileList)}`,
888
+ `releases_dir=${shellQuote(layout.releasesDir)}`,
889
+ `current=${shellQuote(layout.currentLink)}`,
672
890
  `code=${shellQuote(layout.code)}`,
673
- 'listed="$incoming.files"',
674
- '(cd "$incoming" && find . \\( -type f -o -type l \\) ! -name .env | LC_ALL=C sort) > "$listed"',
675
- 'mkdir -p "$code"',
676
- 'if [ -f "$files" ]; then',
677
- ' LC_ALL=C comm -23 "$files" "$listed" | while IFS= read -r path; do',
678
- ' rm -f "$code/$path"',
679
- ' dir="$(dirname "$code/$path")"',
680
- ' while [ "$dir" != "$code" ] && [ "$dir" != "$code/." ] && rmdir "$dir" 2>/dev/null; do',
681
- ' dir="$(dirname "$dir")"',
682
- " done",
683
- " done",
891
+ `project=${shellQuote(layout.project)}`,
892
+ `shared_dir=${shellQuote(layout.sharedDir)}`,
893
+ `shared_env=${shellQuote(layout.sharedEnv)}`,
894
+ `release_root=${shellQuote(targetRelease.root)}`,
895
+ `release_project=${shellQuote(targetRelease.project)}`,
896
+ 'mkdir -p "$releases_dir" "$shared_dir"',
897
+ 'if [ -d "$code" ] && [ ! -L "$code" ]; then',
898
+ ' if [ -f "$project/.env" ] && [ ! -f "$shared_env" ]; then',
899
+ ' cp -pf "$project/.env" "$shared_env" && chmod 600 "$shared_env"',
900
+ " fi",
901
+ ' rm -rf "$releases_dir/legacy"',
902
+ ' mv "$code" "$releases_dir/legacy"',
903
+ ' ln -sfn "releases/legacy" "$current"',
904
+ ' ln -sfn "current" "$code"',
905
+ "fi",
906
+ 'if [ -f "$project/.env" ] && { [ ! -f "$shared_env" ] || [ "$project/.env" -nt "$shared_env" ]; }; then',
907
+ ' cp -pf "$project/.env" "$shared_env" && chmod 600 "$shared_env"',
684
908
  "fi",
685
- `(cd "$incoming" && tar -c -f - --exclude=.env .) | (cd "$code" && tar -x -f -)`,
686
- 'mv "$listed" "$files"',
909
+ 'if [ -L "$current" ]; then prev_release="$(basename "$(readlink "$current")")"; fi',
910
+ 'rm -rf "$release_root"',
911
+ 'mkdir -p "$release_root"',
912
+ `(cd "$incoming" && tar -c -f - --exclude=.env .) | (cd "$release_root" && tar -x -f -)`,
913
+ 'chmod 755 "$release_root"',
914
+ 'mkdir -p "$release_project"',
915
+ 'if [ -f "$shared_env" ]; then ln -sfn "$shared_env" "$release_project/.env"; fi',
687
916
  'rm -rf "$incoming"',
688
917
  'find "$(dirname "$incoming")" -mindepth 1 -maxdepth 1 -mmin +1440 -exec rm -rf {} + 2>/dev/null || true'
689
918
  ].join(`
@@ -716,10 +945,10 @@ function renderInstallBun(version) {
716
945
  }
717
946
  function renderInstallDependencies() {
718
947
  return [
719
- 'cd "$PROJECT"',
720
- '"$BUN" install --frozen-lockfile',
948
+ 'cd "$RELEASE_PROJECT"',
949
+ '"$BUN" install --frozen-lockfile --backend=hardlink',
721
950
  "for bin in sixb sixb-deploy-ssh; do",
722
- ' if [ ! -e "$PROJECT/node_modules/.bin/$bin" ]; then',
951
+ ' if [ ! -e "$RELEASE_PROJECT/node_modules/.bin/$bin" ]; then',
723
952
  ' package="@sixb/cli"; [ "$bin" = sixb ] || package="@sixb/deploy-ssh"',
724
953
  ' echo "$package is not installed. Add it to the project dependencies." >&2',
725
954
  " exit 1",
@@ -728,12 +957,40 @@ function renderInstallDependencies() {
728
957
  ].join(`
729
958
  `);
730
959
  }
960
+ function isStorageMigration(command) {
961
+ return command.program === "sixb" && command.args[0] === "db" && command.args[1] === "migrate";
962
+ }
963
+ function stopForSchemaChange() {
964
+ return {
965
+ label: "Check storage migrations",
966
+ body: [
967
+ 'cd "$RELEASE_PROJECT"',
968
+ "migrations=0",
969
+ '"$BUN" "$RELEASE_PROJECT/node_modules/.bin/sixb" db migrate --check || migrations=$?',
970
+ "",
971
+ 'case "$migrations" in',
972
+ " 0) ;;",
973
+ " 2 | 3)",
974
+ ' if [ "$migrations" = 3 ]; then',
975
+ " echo 'This release changes the storage schema, so the services stop while it migrates.'",
976
+ " else",
977
+ " echo 'This release cannot tell whether its schema changes, so the services stop while it migrates.'",
978
+ " fi",
979
+ ' if systemctl --user is-active --quiet "$UNIT"; then systemctl --user stop "$UNIT"; fi',
980
+ ' deploy_mode="stopped"',
981
+ " ;;",
982
+ ' *) exit "$migrations" ;;',
983
+ "esac"
984
+ ].join(`
985
+ `)
986
+ };
987
+ }
731
988
  function runStep(command) {
732
- const program = command.program === "sixb" ? '"$PROJECT/node_modules/.bin/sixb"' : "";
989
+ const program = command.program === "sixb" ? '"$RELEASE_PROJECT/node_modules/.bin/sixb"' : "";
733
990
  return {
734
991
  label: [command.program, ...command.args].join(" "),
735
992
  body: [
736
- 'cd "$PROJECT"',
993
+ 'cd "$RELEASE_PROJECT"',
737
994
  `"$BUN" ${program} ${command.args.map(shellQuote).join(" ")}`.replace(/ {2,}/g, " ")
738
995
  ].join(`
739
996
  `)
@@ -741,104 +998,227 @@ function runStep(command) {
741
998
  }
742
999
  function envStep(command, layout) {
743
1000
  const step = runStep(command);
744
- return { ...step, body: `${step.body} --token-file ${shellQuote(layout.envToken)}` };
1001
+ return {
1002
+ ...step,
1003
+ body: [
1004
+ `${step.body} --token-file ${shellQuote(layout.envToken)}`,
1005
+ 'if [ -f "$RELEASE_PROJECT/.env" ] && [ ! -L "$RELEASE_PROJECT/.env" ]; then',
1006
+ ' mv -f "$RELEASE_PROJECT/.env" "$SHARED_ENV" && chmod 600 "$SHARED_ENV"',
1007
+ ' ln -sfn "$SHARED_ENV" "$RELEASE_PROJECT/.env"',
1008
+ "fi"
1009
+ ].join(`
1010
+ `)
1011
+ };
745
1012
  }
746
- function renderStopServices() {
1013
+ function withCarriedAssets(step) {
1014
+ return {
1015
+ ...step,
1016
+ body: [
1017
+ step.body,
1018
+ 'new_app="$RELEASE_PROJECT/.sixb/dist/app"',
1019
+ 'old_app="$PROJECT/.sixb/dist/app"',
1020
+ 'own_list="$RELEASE_PROJECT/.sixb/dist/.own-assets"',
1021
+ 'old_own="$PROJECT/.sixb/dist/.own-assets"',
1022
+ 'if [ -d "$new_app" ]; then',
1023
+ ' (cd "$new_app" && find . -type f | LC_ALL=C sort) > "$own_list"',
1024
+ ' if [ -d "$old_app" ]; then',
1025
+ ' source_list="$old_own"',
1026
+ ' if [ ! -f "$source_list" ]; then',
1027
+ ' source_list="$(mktemp)"',
1028
+ ' (cd "$old_app" && find . -type f | LC_ALL=C sort) > "$source_list"',
1029
+ ' temp_source_list="$source_list"',
1030
+ " else",
1031
+ ' temp_source_list=""',
1032
+ " fi",
1033
+ " while IFS= read -r rel; do",
1034
+ ' base="$(basename "$rel")"',
1035
+ ' case "$base" in',
1036
+ " chunk-* | asset-* | app-*.* | shared-app-*.* | auth-*.*)",
1037
+ ' if [ ! -e "$new_app/$rel" ] && [ -f "$old_app/$rel" ]; then',
1038
+ ' mkdir -p "$(dirname "$new_app/$rel")"',
1039
+ ' ln "$old_app/$rel" "$new_app/$rel" 2>/dev/null || cp -p "$old_app/$rel" "$new_app/$rel"',
1040
+ " fi",
1041
+ " ;;",
1042
+ " esac",
1043
+ ' done < "$source_list"',
1044
+ ' if [ -n "$temp_source_list" ]; then rm -f "$temp_source_list"; fi',
1045
+ " fi",
1046
+ "fi"
1047
+ ].join(`
1048
+ `)
1049
+ };
1050
+ }
1051
+ function renderStartCandidate(input) {
1052
+ const { layout, targetRelease, blueRelease, greenRelease, slots } = input;
1053
+ const blueManifest = renderProcessManifest(blueRelease, layout, {
1054
+ projectDir: targetRelease.project,
1055
+ slot: "blue"
1056
+ });
1057
+ const greenManifest = renderProcessManifest(greenRelease, layout, {
1058
+ projectDir: targetRelease.project,
1059
+ slot: "green"
1060
+ });
1061
+ const installServiceFiles = renderInstallServiceFiles(blueRelease, layout, slots);
747
1062
  return [
748
- 'if systemctl --user is-active --quiet "$UNIT"; then',
749
- ' systemctl --user stop "$UNIT"',
1063
+ writeFileCommand(`${layout.deployDir}/manifest.blue.json`, `${JSON.stringify(blueManifest, null, 2)}
1064
+ `),
1065
+ writeFileCommand(`${layout.deployDir}/manifest.green.json`, `${JSON.stringify(greenManifest, null, 2)}
1066
+ `),
1067
+ "start_cold=1",
1068
+ 'if [ -f "$MANIFEST" ] && systemctl --user is-active --quiet "$UNIT"; then start_cold=0; fi',
1069
+ "",
1070
+ `if [ "$start_cold" = 0 ] && ! grep -qs '"slot":' "$MANIFEST"; then`,
1071
+ " echo 'The running supervisor predates blue-green deploys, so this deploy restarts it once.'",
1072
+ " start_cold=1",
1073
+ "fi",
1074
+ "",
1075
+ 'if [ "$start_cold" = 0 ]; then',
1076
+ ` if grep -qs '"slot": "green"' "$MANIFEST"; then target_slot="blue"; else target_slot="green"; fi`,
1077
+ ' cp "$DEPLOY/manifest.$target_slot.json" "$CANDIDATE_MANIFEST"',
1078
+ ' "$BUN" "$RELEASE_PROJECT/node_modules/.bin/sixb-deploy-ssh" stage "$CANDIDATE_MANIFEST" --manifest "$MANIFEST"',
1079
+ ' deploy_mode="staged"',
1080
+ " started=$SECONDS",
750
1081
  "else",
751
- " echo 'Services were not running.'",
1082
+ ' target_slot="blue"',
1083
+ " use_release",
1084
+ ' deploy_mode="cold"',
1085
+ ...installServiceFiles.map((line) => ` ${line}`),
1086
+ ' mv "$DEPLOY/manifest.blue.json" "$MANIFEST"',
1087
+ ' rm -f "$DEPLOY/manifest.green.json" "$CANDIDATE_MANIFEST"',
1088
+ ' systemctl --user restart "$UNIT"',
1089
+ ' systemctl --user is-active --quiet "$UNIT"',
1090
+ " started=$SECONDS",
752
1091
  "fi"
753
1092
  ].join(`
754
1093
  `);
755
1094
  }
756
- function renderWriteServices(release, layout) {
1095
+ function renderCheckServices(input) {
1096
+ const blueUrl = input.blueReadyUrl;
1097
+ const greenUrl = input.greenReadyUrl ?? input.blueReadyUrl;
1098
+ const waitForApi = blueUrl || greenUrl ? [
1099
+ `ready_url=${shellQuote(blueUrl ?? "")}`,
1100
+ 'if [ "$target_slot" = "green" ]; then',
1101
+ ` ready_url=${shellQuote(greenUrl ?? "")}`,
1102
+ "fi",
1103
+ 'if [ -n "$ready_url" ]; then',
1104
+ " ready=0",
1105
+ " for attempt in $(seq 1 90); do",
1106
+ ` if "$BUN" -e 'const r = await fetch(process.argv[1]).catch(() => null); process.exit(r?.ok ? 0 : 1)' "$ready_url" 2>/dev/null; then`,
1107
+ " ready=1",
1108
+ " break",
1109
+ " fi",
1110
+ " sleep 1",
1111
+ " done",
1112
+ ' if [ "$ready" != 1 ]; then',
1113
+ ' echo "The API did not answer $ready_url within 90 seconds. Its latest log lines:" >&2',
1114
+ ' "$CTL" logs api --tail 30 >&2 || true',
1115
+ " exit 1",
1116
+ " fi",
1117
+ "fi"
1118
+ ] : [];
757
1119
  return [
758
- writeFileCommand(layout.manifest, `${JSON.stringify(renderProcessManifest(release, layout), null, 2)}
759
- `),
760
- writeFileCommand(layout.ctl, renderCtl(layout)),
761
- 'chmod 755 "$CTL"',
762
- 'mkdir -p "$(dirname "$UNIT_PATH")"',
763
- writeFileCommand(`${layout.unit}.new`, renderUnit(release, layout)),
764
- 'if cmp -s "$UNIT_PATH.new" "$UNIT_PATH"; then',
765
- ' rm -f "$UNIT_PATH.new"',
1120
+ ...waitForApi,
1121
+ "quiet=$((5 - (SECONDS - started)))",
1122
+ 'if [ "$quiet" -gt 0 ]; then sleep "$quiet"; fi',
1123
+ 'if [ "$deploy_mode" = "staged" ]; then',
1124
+ ' "$BUN" "$RELEASE_PROJECT/node_modules/.bin/sixb-deploy-ssh" status --check --candidate --manifest "$MANIFEST"',
766
1125
  "else",
767
- ' mv "$UNIT_PATH.new" "$UNIT_PATH"',
768
- " systemctl --user daemon-reload",
769
- ' systemctl --user enable "$UNIT"',
1126
+ ' "$CTL" status --check',
770
1127
  "fi"
771
1128
  ].join(`
772
1129
  `);
773
1130
  }
774
- function renderUpdateRoutes(release, layout) {
1131
+ function renderUpdateRoutes(input) {
1132
+ const { layout, blueCaddy, greenCaddy } = input;
775
1133
  return [
776
- `new=${shellQuote(`${layout.deployDir}/Caddyfile`)}`,
777
- writeFileCommand(`${layout.deployDir}/Caddyfile`, renderCaddySnippet(release)),
778
- 'if cmp -s "$new" "$CADDY_SNIPPET"; then',
1134
+ writeFileCommand(`${layout.deployDir}/Caddyfile.blue`, blueCaddy),
1135
+ writeFileCommand(`${layout.deployDir}/Caddyfile.green`, greenCaddy),
1136
+ 'new="$DEPLOY/Caddyfile.$target_slot"',
1137
+ 'cp "$new" "$DEPLOY/Caddyfile"',
1138
+ 'rm -f "$DEPLOY/Caddyfile.blue" "$DEPLOY/Caddyfile.green"',
1139
+ 'if cmp -s "$DEPLOY/Caddyfile" "$CADDY_SNIPPET"; then',
779
1140
  " echo 'Routes unchanged.'",
780
1141
  "else",
781
1142
  ' if [ ! -w "$CADDY_DIR" ]; then',
782
1143
  ' echo "$CADDY_DIR is missing or not writable, so this server is not set up for sixb deploy." >&2',
783
1144
  " exit 1",
784
1145
  " fi",
785
- ' previous="$DEPLOY/Caddyfile.previous"',
786
- ' rm -f "$previous"',
787
- ' if [ -f "$CADDY_SNIPPET" ]; then cp "$CADDY_SNIPPET" "$previous"; fi',
788
- ' cp "$new" "$CADDY_SNIPPET"',
1146
+ ' rm -f "$PREVIOUS_ROUTES"',
1147
+ ' if [ -f "$CADDY_SNIPPET" ]; then cp "$CADDY_SNIPPET" "$PREVIOUS_ROUTES"; fi',
1148
+ ' cp "$DEPLOY/Caddyfile" "$CADDY_SNIPPET"',
1149
+ "",
789
1150
  ' if ! caddy validate --config /etc/caddy/Caddyfile --adapter caddyfile > "$DEPLOY/caddy-validate.log" 2>&1; then',
790
- ' if [ -f "$previous" ]; then cp "$previous" "$CADDY_SNIPPET"; else rm -f "$CADDY_SNIPPET"; fi',
1151
+ " restore_routes",
791
1152
  ' tail -n 20 "$DEPLOY/caddy-validate.log" >&2',
792
1153
  " echo 'Caddy rejected the routes; the previous ones are back in place.' >&2",
793
1154
  " exit 1",
794
1155
  " fi",
795
- " sudo -n /usr/bin/systemctl reload caddy",
1156
+ "",
1157
+ " if ! sudo -n /usr/bin/systemctl reload caddy; then",
1158
+ " restore_routes",
1159
+ " echo 'Caddy did not reload; the previous routes are back in place.' >&2",
1160
+ " exit 1",
1161
+ " fi",
1162
+ "",
1163
+ ' if [ "$deploy_mode" = "staged" ]; then deploy_mode="routed"; fi',
796
1164
  "fi"
797
1165
  ].join(`
798
1166
  `);
799
1167
  }
800
- function renderStartServices() {
1168
+ function renderInstallServiceFiles(release, layout, slots) {
1169
+ const reservedPorts = [
1170
+ ...new Set(release.services.flatMap((service) => service.http && (service.name === "api" || service.name === "app") ? [slots.blue[service.name], slots.green[service.name]] : []))
1171
+ ].map(String).join(`
1172
+ `);
1173
+ return [
1174
+ writeFileCommand(`${layout.deployDir}/ports.txt`, reservedPorts ? `${reservedPorts}
1175
+ ` : ""),
1176
+ writeFileCommand(layout.ctl, renderCtl(layout)),
1177
+ 'chmod 755 "$CTL"',
1178
+ 'mkdir -p "$(dirname "$UNIT_PATH")"',
1179
+ writeFileCommand(`${layout.unit}.new`, renderUnit(release, layout)),
1180
+ 'if cmp -s "$UNIT_PATH.new" "$UNIT_PATH"; then',
1181
+ ' rm -f "$UNIT_PATH.new"',
1182
+ "else",
1183
+ ' mv "$UNIT_PATH.new" "$UNIT_PATH"',
1184
+ " systemctl --user daemon-reload",
1185
+ ' systemctl --user enable "$UNIT"',
1186
+ "fi"
1187
+ ];
1188
+ }
1189
+ function renderActivateRelease(input) {
1190
+ const hasExclusiveServices = input.release.services.some((service) => service.kind === "singleton" || service.kind === "script");
1191
+ const installServiceFiles = renderInstallServiceFiles(input.release, input.layout, input.slots);
801
1192
  return [
802
- 'systemctl --user restart "$UNIT"',
803
- 'systemctl --user is-active --quiet "$UNIT"',
804
- "started=$SECONDS"
1193
+ 'if [ "$deploy_mode" = "staged" ] || [ "$deploy_mode" = "routed" ]; then',
1194
+ ...installServiceFiles.map((line) => ` ${line}`),
1195
+ " use_release",
1196
+ ' deploy_mode="activated"',
1197
+ ' mv "$CANDIDATE_MANIFEST" "$MANIFEST"',
1198
+ ' rm -f "$DEPLOY/manifest.blue.json" "$DEPLOY/manifest.green.json"',
1199
+ ' "$CTL" activate',
1200
+ ...hasExclusiveServices ? [" sleep 2"] : [],
1201
+ ' "$CTL" status --check',
1202
+ "fi",
1203
+ renderPruneReleases()
805
1204
  ].join(`
806
1205
  `);
807
1206
  }
808
- function renderCheckServices(readinessUrl) {
809
- const waitForApi = readinessUrl ? [
810
- `ready_url=${shellQuote(readinessUrl)}`,
811
- "ready=0",
812
- "for attempt in $(seq 1 90); do",
813
- ` if "$BUN" -e 'const r = await fetch(process.argv[1]).catch(() => null); process.exit(r?.ok ? 0 : 1)' "$ready_url" 2>/dev/null; then`,
814
- " ready=1",
815
- " break",
816
- " fi",
817
- " sleep 1",
818
- "done",
819
- 'if [ "$ready" != 1 ]; then',
820
- ' echo "The API did not answer $ready_url within 90 seconds. Its latest log lines:" >&2',
821
- ' "$CTL" logs api --tail 30 >&2 || true',
822
- " exit 1",
823
- "fi"
824
- ] : [];
1207
+ function renderPruneReleases() {
825
1208
  return [
826
- ...waitForApi,
827
- "quiet=$((5 - (SECONDS - started)))",
828
- 'if [ "$quiet" -gt 0 ]; then sleep "$quiet"; fi',
829
- '"$CTL" status --check'
1209
+ 'if [ -d "$RELEASES_DIR" ]; then',
1210
+ ' for candidate_dir in "$RELEASES_DIR"/*; do',
1211
+ ' [ -d "$candidate_dir" ] || continue',
1212
+ ' name="$(basename "$candidate_dir")"',
1213
+ ' if [ "$name" = "$RELEASE_ID" ] || [ "$name" = "$prev_release" ]; then continue; fi',
1214
+ ' if [ -f "$STATE_FILE" ] && grep -qsF "$candidate_dir" "$STATE_FILE"; then continue; fi',
1215
+ ' rm -rf "$candidate_dir"',
1216
+ " done",
1217
+ "fi"
830
1218
  ].join(`
831
1219
  `);
832
1220
  }
833
1221
 
834
- // src/server/logs.ts
835
- var MAX_LOG_BYTES = 20 * 1024 * 1024;
836
- var RECENT_BYTES = 256 * 1024;
837
- var TERMINAL_CONTROL = /\x1B(?:\[[0-?]*[ -/]*[@-~]|\][^\x07\x1B]*(?:\x07|\x1B\\)|[@-Z\\-_])/g;
838
- function stripTerminalControl(line) {
839
- return line.replace(TERMINAL_CONTROL, "");
840
- }
841
-
842
1222
  // src/transport.ts
843
1223
  import { mkdtemp as mkdtemp2, rm as rm2 } from "fs/promises";
844
1224
  import { tmpdir as tmpdir2 } from "os";
@@ -986,7 +1366,7 @@ ${output}`, error.exitCode);
986
1366
 
987
1367
  // src/operations.ts
988
1368
  var NOT_DEPLOYED = "__SIXB_NOT_DEPLOYED__";
989
- async function deployRelease(shell, user, release, source, context) {
1369
+ async function deployRelease(shell, user, release, source, context, slotPorts) {
990
1370
  const prepared = await capture(shell, renderPrepareScript(release.name, source.commit));
991
1371
  const [home, incoming] = prepared;
992
1372
  if (!home || !incoming) {
@@ -1003,6 +1383,7 @@ async function deployRelease(shell, user, release, source, context) {
1003
1383
  release,
1004
1384
  layout,
1005
1385
  incoming,
1386
+ ...slotPorts ? { slotPorts } : {},
1006
1387
  record: {
1007
1388
  commit: source.commit,
1008
1389
  ref: source.ref,
@@ -1308,7 +1689,7 @@ async function resolveSshConfig(destination) {
1308
1689
  }
1309
1690
 
1310
1691
  // src/ssh-target.ts
1311
- var OPTIONS = ["host", "user", "ports"];
1692
+ var OPTIONS = ["host", "user", "ports", "standbyPorts"];
1312
1693
  var DEFAULT_USER = "sixb";
1313
1694
  var DEFAULT_PORTS = {
1314
1695
  app: 3001,
@@ -1323,6 +1704,7 @@ class SshTarget {
1323
1704
  user;
1324
1705
  location;
1325
1706
  #ports;
1707
+ #standbyPorts;
1326
1708
  constructor(options) {
1327
1709
  if (typeof options !== "object" || options === null) {
1328
1710
  throw new Error('[SshTarget] Pass options, such as `new SshTarget({ host: "203.0.113.10" })`.');
@@ -1335,13 +1717,27 @@ class SshTarget {
1335
1717
  this.host = validateHost(options.host);
1336
1718
  this.user = validateUser(options.user ?? DEFAULT_USER);
1337
1719
  this.location = `${this.user}@${this.host}`;
1338
- this.#ports = { ...DEFAULT_PORTS, ...validatePorts(options.ports) };
1720
+ const primary = { ...DEFAULT_PORTS, ...validatePorts(options.ports, "ports") };
1721
+ const standbyDefaults = {
1722
+ app: defaultStandbyPort(primary.app),
1723
+ api: defaultStandbyPort(primary.api)
1724
+ };
1725
+ const standby = { ...standbyDefaults, ...validatePorts(options.standbyPorts, "standbyPorts") };
1726
+ assertDistinctPorts(primary, standby);
1727
+ this.#ports = primary;
1728
+ this.#standbyPorts = standby;
1339
1729
  }
1340
1730
  listenAddress(service) {
1341
1731
  return { host: LISTEN_HOST, port: this.#ports[service] };
1342
1732
  }
1733
+ standbyListenAddress(service) {
1734
+ return { host: LISTEN_HOST, port: this.#standbyPorts[service] };
1735
+ }
1343
1736
  deploy(release, source, context) {
1344
- return this.#connected((shell) => deployRelease(shell, this.user, release, source, context));
1737
+ return this.#connected((shell) => deployRelease(shell, this.user, release, source, context, {
1738
+ blue: this.#ports,
1739
+ green: this.#standbyPorts
1740
+ }));
1345
1741
  }
1346
1742
  status(context) {
1347
1743
  return this.#connected((shell) => readStatus(shell, context.name));
@@ -1366,7 +1762,14 @@ class SshTarget {
1366
1762
  ...await runChecks(shell, {
1367
1763
  name: context.name,
1368
1764
  projectPath: context.projectPath,
1369
- ports: release.services.flatMap((service) => service.http ? [{ service: service.name, port: service.http.port }] : []),
1765
+ ports: release.services.flatMap((service) => {
1766
+ if (!service.http || service.name !== "api" && service.name !== "app")
1767
+ return [];
1768
+ return [
1769
+ { service: service.name, port: this.#ports[service.name] },
1770
+ { service: `${service.name}-standby`, port: this.#standbyPorts[service.name] }
1771
+ ];
1772
+ }),
1370
1773
  envFromSource: release.steps.env !== undefined,
1371
1774
  location: this.location
1372
1775
  })
@@ -1392,8 +1795,11 @@ class SshTarget {
1392
1795
  }
1393
1796
  context.write(`Preparing ${context.name} as ${this.user}.`);
1394
1797
  await this.#connected((shell) => shell.run([
1798
+ `root="$HOME"/${shellQuote(context.name)}`,
1395
1799
  `project="$HOME"/${shellQuote(`${context.name}/code/${context.projectPath}`)}`,
1396
- 'mkdir -p "$project"',
1800
+ 'mkdir -p "$root/shared"',
1801
+ 'if [ ! -L "$root/code" ]; then mkdir -p "$project"; fi',
1802
+ 'if [ -f "$root/shared/.env" ]; then chmod 600 "$root/shared/.env"; fi',
1397
1803
  'if [ -f "$project/.env" ]; then chmod 600 "$project/.env"; fi',
1398
1804
  `token="$HOME"/${shellQuote(`${context.name}/${ENV_TOKEN_FILE}`)}`,
1399
1805
  'if [ -f "$token" ]; then chmod 600 "$token"; fi'
@@ -1448,6 +1854,25 @@ class SshTarget {
1448
1854
  }
1449
1855
  }
1450
1856
  }
1857
+ function defaultStandbyPort(port) {
1858
+ return port + 10 <= 65535 ? port + 10 : port - 10;
1859
+ }
1860
+ function assertDistinctPorts(primary, standby) {
1861
+ const entries = [
1862
+ ["ports.app", primary.app],
1863
+ ["ports.api", primary.api],
1864
+ ["standbyPorts.app", standby.app],
1865
+ ["standbyPorts.api", standby.api]
1866
+ ];
1867
+ const seen = new Map;
1868
+ for (const [label, port] of entries) {
1869
+ const previous = seen.get(port);
1870
+ if (previous) {
1871
+ throw new Error(`[SshTarget] ${label} (${port}) conflicts with ${previous}. Give each HTTP service and slot its own port.`);
1872
+ }
1873
+ seen.set(port, label);
1874
+ }
1875
+ }
1451
1876
  function validateHost(value) {
1452
1877
  if (typeof value !== "string" || value.trim() === "") {
1453
1878
  throw new Error("[SshTarget] host must be the server's hostname or IP address.");
@@ -1473,19 +1898,19 @@ function validateUser(value) {
1473
1898
  }
1474
1899
  return value;
1475
1900
  }
1476
- function validatePorts(value) {
1901
+ function validatePorts(value, field) {
1477
1902
  if (value === undefined)
1478
1903
  return {};
1479
1904
  if (typeof value !== "object" || value === null || Array.isArray(value)) {
1480
- throw new Error("[SshTarget] ports must be an object, such as `{ api: 3012 }`.");
1905
+ throw new Error(`[SshTarget] ${field} must be an object, such as \`{ api: 3012 }\`.`);
1481
1906
  }
1482
1907
  const ports = {};
1483
1908
  for (const [service, port] of Object.entries(value)) {
1484
1909
  if (!Object.hasOwn(DEFAULT_PORTS, service)) {
1485
- throw new Error(`[SshTarget] ports.${service} is not an HTTP service. Available: api, app.`);
1910
+ throw new Error(`[SshTarget] ${field}.${service} is not an HTTP service. Available: api, app.`);
1486
1911
  }
1487
1912
  if (!Number.isInteger(port) || port < 1024 || port > 65535) {
1488
- throw new Error(`[SshTarget] ports.${service} must be a port from 1024 to 65535.`);
1913
+ throw new Error(`[SshTarget] ${field}.${service} must be a port from 1024 to 65535.`);
1489
1914
  }
1490
1915
  ports[service] = port;
1491
1916
  }
@@ -1528,4 +1953,4 @@ export {
1528
1953
  SshTarget
1529
1954
  };
1530
1955
 
1531
- //# debugId=B4A600CC24E47FE664756E2164756E21
1956
+ //# debugId=F0A8F6E476574A6F64756E2164756E21