@sixb/deploy-ssh 0.2.0 → 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 {
@@ -409,6 +449,24 @@ function renderCiInstall(input) {
409
449
  ];
410
450
  }
411
451
 
452
+ // src/operations.ts
453
+ import { randomUUID } from "crypto";
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
+
412
470
  // src/update-page.ts
413
471
  function renderUpdatePage() {
414
472
  return `<!doctype html>
@@ -453,21 +511,55 @@ function renderUpdatePage() {
453
511
 
454
512
  // src/render.ts
455
513
  var GENERATED = "Generated by sixb deploy. Changes are replaced on the next deploy.";
456
- function renderProcessManifest(release, layout) {
457
- 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`;
458
547
  return {
459
548
  version: 1,
460
549
  name: release.name,
461
- cwd: layout.project,
550
+ ...options.slot ? { slot: options.slot } : {},
551
+ cwd: projectDir,
462
552
  runDir: layout.runDir,
463
553
  logDir: layout.logDir,
464
554
  releasePath: layout.releaseRecord,
555
+ env: release.env,
465
556
  services: release.services.map((service) => ({
466
557
  name: service.name,
558
+ kind: service.kind,
467
559
  processName: `${release.name}-${service.name}`,
468
560
  command: layout.bun,
469
561
  args: service.command.program === "sixb" ? [sixb, ...service.command.args] : [...service.command.args],
470
- env: service.env,
562
+ env: serviceEnvWithDrain(service),
471
563
  instances: service.instances,
472
564
  killTimeoutMs: service.process.killTimeoutMs,
473
565
  restartDelayMs: service.process.restartDelayMs,
@@ -475,6 +567,16 @@ function renderProcessManifest(release, layout) {
475
567
  }))
476
568
  };
477
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
+ }
478
580
  function renderCtl(layout) {
479
581
  return [
480
582
  "#!/bin/sh",
@@ -516,7 +618,10 @@ function renderCaddySnippet(release) {
516
618
  [
517
619
  `${service.http.domain} {`,
518
620
  ...service.name === "api" ? compressJson() : [],
519
- ` 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}",
520
625
  ...pages ? updatePageOnProxyErrors() : [],
521
626
  "}"
522
627
  ].join(`
@@ -528,6 +633,10 @@ function renderCaddySnippet(release) {
528
633
  `), ""].join(`
529
634
  `);
530
635
  }
636
+ function keepWebSocketsThroughReload(service) {
637
+ const seconds = retiringKillTimeoutMs(service.process.killTimeoutMs) / 1000;
638
+ return [` stream_close_delay ${seconds}s`];
639
+ }
531
640
  function compressJson() {
532
641
  return [
533
642
  "\tencode {",
@@ -584,35 +693,82 @@ function renderReceiveScript(incoming, path) {
584
693
  const directory = path === "." ? incoming : `${incoming}/${path}`;
585
694
  return `mkdir -p ${shellQuote(directory)} && tar -x -f - -C ${shellQuote(directory)}`;
586
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
+ }
587
713
  function renderDeployScript(input) {
588
714
  const { release, layout } = input;
589
- 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
+ };
590
724
  const steps = [
591
- { label: "Apply files", body: renderApplyFiles(layout, input.incoming) },
725
+ {
726
+ label: "Prepare release",
727
+ body: renderPrepareRelease(layout, targetRelease, input.incoming)
728
+ },
592
729
  { label: `Install Bun ${release.bunVersion}`, body: renderInstallBun(release.bunVersion) },
593
730
  { label: "Install dependencies", body: renderInstallDependencies() },
594
731
  ...release.steps.env ? [envStep(release.steps.env, layout)] : [],
595
- ...release.steps.build.map((command) => runStep(command)),
596
- { label: "Write services", body: renderWriteServices(release, layout) },
597
- { label: "Update routes", body: renderUpdateRoutes(release, layout) },
598
- { label: "Stop services", body: renderStopServices() },
599
- ...release.steps.beforeStart.map((command) => runStep(command)),
600
- { 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
+ },
601
744
  {
602
745
  label: "Check services",
603
- 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
+ })
604
750
  },
605
751
  {
606
- label: "Record release",
607
- body: writeFileCommand(layout.releaseRecord, `${JSON.stringify(input.record)}
608
- `)
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 })
609
762
  }
610
763
  ];
611
764
  const script = [
612
765
  "set -euo pipefail",
613
766
  "main() {",
614
- renderEnvironment(release, layout),
767
+ renderEnvironment(release, layout, targetRelease),
615
768
  renderTakeLock(layout),
769
+ renderUseRelease(layout, input.record),
770
+ renderRestoreRoutes(),
771
+ renderCandidateTrap(),
616
772
  ...steps.flatMap((step, index) => [
617
773
  `printf '\\n%s\\t%s\\t%s\\n' ${STEP_MARKER} ${index} start`,
618
774
  step.body,
@@ -625,12 +781,22 @@ function renderDeployScript(input) {
625
781
  `);
626
782
  return { labels: steps.map((step) => step.label), script };
627
783
  }
628
- function renderEnvironment(release, layout) {
784
+ function renderEnvironment(release, layout, targetRelease) {
629
785
  const variables = {
630
786
  ROOT: layout.root,
787
+ RELEASES_DIR: layout.releasesDir,
788
+ CURRENT: layout.currentLink,
631
789
  CODE: layout.code,
632
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,
633
796
  DEPLOY: layout.deployDir,
797
+ MANIFEST: layout.manifest,
798
+ CANDIDATE_MANIFEST: layout.candidateManifest,
799
+ STATE_FILE: `${layout.runDir}/state.json`,
634
800
  BUN: layout.bun,
635
801
  BUN_DIR: layout.bunDir,
636
802
  BUN_VERSION: release.bunVersion,
@@ -638,13 +804,17 @@ function renderEnvironment(release, layout) {
638
804
  UNIT: layout.unitName,
639
805
  UNIT_PATH: layout.unit,
640
806
  CADDY_DIR: layout.caddyDir,
641
- CADDY_SNIPPET: layout.caddySnippet
807
+ CADDY_SNIPPET: layout.caddySnippet,
808
+ PREVIOUS_ROUTES: `${layout.deployDir}/Caddyfile.previous`
642
809
  };
643
810
  return [
644
811
  ...Object.entries(variables).map(([name, value]) => `${name}=${shellQuote(value)}`),
645
812
  ...Object.entries(release.env).map(([name, value]) => `export ${name}=${shellQuote(value)}`),
646
813
  'export PATH="$BUN_DIR:$PATH"',
647
- '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=""'
648
818
  ].join(`
649
819
  `);
650
820
  }
@@ -652,34 +822,97 @@ function renderTakeLock(layout) {
652
822
  return [
653
823
  `exec 9>${shellQuote(layout.lock)}`,
654
824
  "if ! flock -n 9; then",
655
- ' echo "Waiting for another deploy of this project to finish\u2026"',
825
+ ' echo "Waiting for another deploy or script run of this project to finish\u2026"',
656
826
  "fi",
657
827
  "if ! flock -w 600 9; then",
658
- ' echo "Another deploy of this project has held its lock for 10 minutes." >&2',
828
+ ' echo "Another deploy or script run of this project has held its lock for 10 minutes." >&2',
659
829
  " exit 1",
660
830
  "fi"
661
831
  ].join(`
662
832
  `);
663
833
  }
664
- 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) {
665
886
  return [
666
887
  `incoming=${shellQuote(incoming)}`,
667
- `files=${shellQuote(layout.fileList)}`,
888
+ `releases_dir=${shellQuote(layout.releasesDir)}`,
889
+ `current=${shellQuote(layout.currentLink)}`,
668
890
  `code=${shellQuote(layout.code)}`,
669
- 'listed="$incoming.files"',
670
- '(cd "$incoming" && find . \\( -type f -o -type l \\) ! -name .env | LC_ALL=C sort) > "$listed"',
671
- 'mkdir -p "$code"',
672
- 'if [ -f "$files" ]; then',
673
- ' LC_ALL=C comm -23 "$files" "$listed" | while IFS= read -r path; do',
674
- ' rm -f "$code/$path"',
675
- ' dir="$(dirname "$code/$path")"',
676
- ' while [ "$dir" != "$code" ] && [ "$dir" != "$code/." ] && rmdir "$dir" 2>/dev/null; do',
677
- ' dir="$(dirname "$dir")"',
678
- " done",
679
- " 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"',
680
908
  "fi",
681
- `(cd "$incoming" && tar -c -f - --exclude=.env .) | (cd "$code" && tar -x -f -)`,
682
- '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',
683
916
  'rm -rf "$incoming"',
684
917
  'find "$(dirname "$incoming")" -mindepth 1 -maxdepth 1 -mmin +1440 -exec rm -rf {} + 2>/dev/null || true'
685
918
  ].join(`
@@ -712,10 +945,10 @@ function renderInstallBun(version) {
712
945
  }
713
946
  function renderInstallDependencies() {
714
947
  return [
715
- 'cd "$PROJECT"',
716
- '"$BUN" install --frozen-lockfile',
948
+ 'cd "$RELEASE_PROJECT"',
949
+ '"$BUN" install --frozen-lockfile --backend=hardlink',
717
950
  "for bin in sixb sixb-deploy-ssh; do",
718
- ' if [ ! -e "$PROJECT/node_modules/.bin/$bin" ]; then',
951
+ ' if [ ! -e "$RELEASE_PROJECT/node_modules/.bin/$bin" ]; then',
719
952
  ' package="@sixb/cli"; [ "$bin" = sixb ] || package="@sixb/deploy-ssh"',
720
953
  ' echo "$package is not installed. Add it to the project dependencies." >&2',
721
954
  " exit 1",
@@ -724,12 +957,40 @@ function renderInstallDependencies() {
724
957
  ].join(`
725
958
  `);
726
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
+ }
727
988
  function runStep(command) {
728
- const program = command.program === "sixb" ? '"$PROJECT/node_modules/.bin/sixb"' : "";
989
+ const program = command.program === "sixb" ? '"$RELEASE_PROJECT/node_modules/.bin/sixb"' : "";
729
990
  return {
730
991
  label: [command.program, ...command.args].join(" "),
731
992
  body: [
732
- 'cd "$PROJECT"',
993
+ 'cd "$RELEASE_PROJECT"',
733
994
  `"$BUN" ${program} ${command.args.map(shellQuote).join(" ")}`.replace(/ {2,}/g, " ")
734
995
  ].join(`
735
996
  `)
@@ -737,104 +998,227 @@ function runStep(command) {
737
998
  }
738
999
  function envStep(command, layout) {
739
1000
  const step = runStep(command);
740
- 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
+ };
1012
+ }
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
+ };
741
1050
  }
742
- function renderStopServices() {
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);
743
1062
  return [
744
- 'if systemctl --user is-active --quiet "$UNIT"; then',
745
- ' 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",
746
1081
  "else",
747
- " 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",
748
1091
  "fi"
749
1092
  ].join(`
750
1093
  `);
751
1094
  }
752
- 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
+ ] : [];
753
1119
  return [
754
- writeFileCommand(layout.manifest, `${JSON.stringify(renderProcessManifest(release, layout), null, 2)}
755
- `),
756
- writeFileCommand(layout.ctl, renderCtl(layout)),
757
- 'chmod 755 "$CTL"',
758
- 'mkdir -p "$(dirname "$UNIT_PATH")"',
759
- writeFileCommand(`${layout.unit}.new`, renderUnit(release, layout)),
760
- 'if cmp -s "$UNIT_PATH.new" "$UNIT_PATH"; then',
761
- ' 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"',
762
1125
  "else",
763
- ' mv "$UNIT_PATH.new" "$UNIT_PATH"',
764
- " systemctl --user daemon-reload",
765
- ' systemctl --user enable "$UNIT"',
1126
+ ' "$CTL" status --check',
766
1127
  "fi"
767
1128
  ].join(`
768
1129
  `);
769
1130
  }
770
- function renderUpdateRoutes(release, layout) {
1131
+ function renderUpdateRoutes(input) {
1132
+ const { layout, blueCaddy, greenCaddy } = input;
771
1133
  return [
772
- `new=${shellQuote(`${layout.deployDir}/Caddyfile`)}`,
773
- writeFileCommand(`${layout.deployDir}/Caddyfile`, renderCaddySnippet(release)),
774
- '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',
775
1140
  " echo 'Routes unchanged.'",
776
1141
  "else",
777
1142
  ' if [ ! -w "$CADDY_DIR" ]; then',
778
1143
  ' echo "$CADDY_DIR is missing or not writable, so this server is not set up for sixb deploy." >&2',
779
1144
  " exit 1",
780
1145
  " fi",
781
- ' previous="$DEPLOY/Caddyfile.previous"',
782
- ' rm -f "$previous"',
783
- ' if [ -f "$CADDY_SNIPPET" ]; then cp "$CADDY_SNIPPET" "$previous"; fi',
784
- ' 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
+ "",
785
1150
  ' if ! caddy validate --config /etc/caddy/Caddyfile --adapter caddyfile > "$DEPLOY/caddy-validate.log" 2>&1; then',
786
- ' if [ -f "$previous" ]; then cp "$previous" "$CADDY_SNIPPET"; else rm -f "$CADDY_SNIPPET"; fi',
1151
+ " restore_routes",
787
1152
  ' tail -n 20 "$DEPLOY/caddy-validate.log" >&2',
788
1153
  " echo 'Caddy rejected the routes; the previous ones are back in place.' >&2",
789
1154
  " exit 1",
790
1155
  " fi",
791
- " 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',
792
1164
  "fi"
793
1165
  ].join(`
794
1166
  `);
795
1167
  }
796
- 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
+ `);
797
1173
  return [
798
- 'systemctl --user restart "$UNIT"',
799
- 'systemctl --user is-active --quiet "$UNIT"',
800
- "started=$SECONDS"
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);
1192
+ return [
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()
801
1204
  ].join(`
802
1205
  `);
803
1206
  }
804
- function renderCheckServices(readinessUrl) {
805
- const waitForApi = readinessUrl ? [
806
- `ready_url=${shellQuote(readinessUrl)}`,
807
- "ready=0",
808
- "for attempt in $(seq 1 90); do",
809
- ` if "$BUN" -e 'const r = await fetch(process.argv[1]).catch(() => null); process.exit(r?.ok ? 0 : 1)' "$ready_url" 2>/dev/null; then`,
810
- " ready=1",
811
- " break",
812
- " fi",
813
- " sleep 1",
814
- "done",
815
- 'if [ "$ready" != 1 ]; then',
816
- ' echo "The API did not answer $ready_url within 90 seconds. Its latest log lines:" >&2',
817
- ' "$CTL" logs api --tail 30 >&2 || true',
818
- " exit 1",
819
- "fi"
820
- ] : [];
1207
+ function renderPruneReleases() {
821
1208
  return [
822
- ...waitForApi,
823
- "quiet=$((5 - (SECONDS - started)))",
824
- 'if [ "$quiet" -gt 0 ]; then sleep "$quiet"; fi',
825
- '"$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"
826
1218
  ].join(`
827
1219
  `);
828
1220
  }
829
1221
 
830
- // src/server/logs.ts
831
- var MAX_LOG_BYTES = 20 * 1024 * 1024;
832
- var RECENT_BYTES = 256 * 1024;
833
- var TERMINAL_CONTROL = /\x1B(?:\[[0-?]*[ -/]*[@-~]|\][^\x07\x1B]*(?:\x07|\x1B\\)|[@-Z\\-_])/g;
834
- function stripTerminalControl(line) {
835
- return line.replace(TERMINAL_CONTROL, "");
836
- }
837
-
838
1222
  // src/transport.ts
839
1223
  import { mkdtemp as mkdtemp2, rm as rm2 } from "fs/promises";
840
1224
  import { tmpdir as tmpdir2 } from "os";
@@ -910,7 +1294,10 @@ class SshShell {
910
1294
  }
911
1295
  async function runProcess(command, options) {
912
1296
  if (options.terminal) {
913
- const child = Bun.spawn([...command], { stdio: ["inherit", "inherit", "inherit"] });
1297
+ const child = Bun.spawn([...command], {
1298
+ stdio: ["inherit", "inherit", "inherit"],
1299
+ env: { ...process.env, ...options.env }
1300
+ });
914
1301
  const exitCode = await child.exited;
915
1302
  if (exitCode !== 0)
916
1303
  throw new RemoteScriptError(`exited with code ${exitCode}`, exitCode);
@@ -979,7 +1366,7 @@ ${output}`, error.exitCode);
979
1366
 
980
1367
  // src/operations.ts
981
1368
  var NOT_DEPLOYED = "__SIXB_NOT_DEPLOYED__";
982
- async function deployRelease(shell, user, release, source, context) {
1369
+ async function deployRelease(shell, user, release, source, context, slotPorts) {
983
1370
  const prepared = await capture(shell, renderPrepareScript(release.name, source.commit));
984
1371
  const [home, incoming] = prepared;
985
1372
  if (!home || !incoming) {
@@ -996,6 +1383,7 @@ async function deployRelease(shell, user, release, source, context) {
996
1383
  release,
997
1384
  layout,
998
1385
  incoming,
1386
+ ...slotPorts ? { slotPorts } : {},
999
1387
  record: {
1000
1388
  commit: source.commit,
1001
1389
  ref: source.ref,
@@ -1061,6 +1449,42 @@ async function streamLogs(shell, name, options) {
1061
1449
  async function controlServices(shell, name, action, service) {
1062
1450
  await shell.run(ctlScript(name, [action, ...service ? [service] : []]));
1063
1451
  }
1452
+ async function runDeployScript(shell, name, script) {
1453
+ const root = `"$HOME"/${shellQuote(name)}`;
1454
+ const staged = `${root}/deploy/runs/${randomUUID()}`;
1455
+ await shell.run([
1456
+ `if [ ! -x ${root}/deploy/ctl ]; then`,
1457
+ ` echo "Nothing named ${name} is deployed on this server yet." >&2; exit 1`,
1458
+ "fi",
1459
+ "umask 077",
1460
+ `mkdir -p ${root}/deploy/runs`,
1461
+ `find ${root}/deploy/runs -type f -mmin +1440 -delete 2> /dev/null || true`,
1462
+ `cat > ${staged}`
1463
+ ].join(`
1464
+ `), { stdin: new Blob([Uint8Array.from(script.source)]).stream() });
1465
+ const run = [
1466
+ `cd ${root}`,
1467
+ `exec 9> ${root}/deploy/lock`,
1468
+ "if ! flock -s -n 9; then",
1469
+ ` echo "Waiting for a deploy of ${name} to finish\u2026" >&2`,
1470
+ "fi",
1471
+ "if ! flock -s -w 600 9; then",
1472
+ ` echo "A deploy of ${name} has held its lock for 10 minutes." >&2`,
1473
+ ` rm -f ${staged}`,
1474
+ " exit 1",
1475
+ "fi",
1476
+ `exec ${root}/deploy/ctl run ${staged} ${[script.path, ...script.args].map(shellQuote).join(" ")}`
1477
+ ].join(`
1478
+ `);
1479
+ try {
1480
+ await shell.run(run, { terminal: true });
1481
+ return 0;
1482
+ } catch (error) {
1483
+ if (error instanceof RemoteScriptError && error.exitCode !== 255)
1484
+ return error.exitCode;
1485
+ throw error;
1486
+ }
1487
+ }
1064
1488
  function ctlScript(name, args, options = {}) {
1065
1489
  return [
1066
1490
  `ctl="$HOME"/${shellQuote(name)}/deploy/ctl`,
@@ -1265,7 +1689,7 @@ async function resolveSshConfig(destination) {
1265
1689
  }
1266
1690
 
1267
1691
  // src/ssh-target.ts
1268
- var OPTIONS = ["host", "user", "ports"];
1692
+ var OPTIONS = ["host", "user", "ports", "standbyPorts"];
1269
1693
  var DEFAULT_USER = "sixb";
1270
1694
  var DEFAULT_PORTS = {
1271
1695
  app: 3001,
@@ -1280,6 +1704,7 @@ class SshTarget {
1280
1704
  user;
1281
1705
  location;
1282
1706
  #ports;
1707
+ #standbyPorts;
1283
1708
  constructor(options) {
1284
1709
  if (typeof options !== "object" || options === null) {
1285
1710
  throw new Error('[SshTarget] Pass options, such as `new SshTarget({ host: "203.0.113.10" })`.');
@@ -1292,13 +1717,27 @@ class SshTarget {
1292
1717
  this.host = validateHost(options.host);
1293
1718
  this.user = validateUser(options.user ?? DEFAULT_USER);
1294
1719
  this.location = `${this.user}@${this.host}`;
1295
- 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;
1296
1729
  }
1297
1730
  listenAddress(service) {
1298
1731
  return { host: LISTEN_HOST, port: this.#ports[service] };
1299
1732
  }
1733
+ standbyListenAddress(service) {
1734
+ return { host: LISTEN_HOST, port: this.#standbyPorts[service] };
1735
+ }
1300
1736
  deploy(release, source, context) {
1301
- 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
+ }));
1302
1741
  }
1303
1742
  status(context) {
1304
1743
  return this.#connected((shell) => readStatus(shell, context.name));
@@ -1310,6 +1749,9 @@ class SshTarget {
1310
1749
  control(action, service, context) {
1311
1750
  return this.#connected((shell) => controlServices(shell, context.name, action, service));
1312
1751
  }
1752
+ run(script, context) {
1753
+ return this.#connected((shell) => runDeployScript(shell, context.name, script));
1754
+ }
1313
1755
  async check(release, context) {
1314
1756
  const { hostname } = await resolveSshConfig(this.location);
1315
1757
  const dns = checkDns(release, hostname);
@@ -1320,7 +1762,14 @@ class SshTarget {
1320
1762
  ...await runChecks(shell, {
1321
1763
  name: context.name,
1322
1764
  projectPath: context.projectPath,
1323
- 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
+ }),
1324
1773
  envFromSource: release.steps.env !== undefined,
1325
1774
  location: this.location
1326
1775
  })
@@ -1346,8 +1795,11 @@ class SshTarget {
1346
1795
  }
1347
1796
  context.write(`Preparing ${context.name} as ${this.user}.`);
1348
1797
  await this.#connected((shell) => shell.run([
1798
+ `root="$HOME"/${shellQuote(context.name)}`,
1349
1799
  `project="$HOME"/${shellQuote(`${context.name}/code/${context.projectPath}`)}`,
1350
- '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',
1351
1803
  'if [ -f "$project/.env" ]; then chmod 600 "$project/.env"; fi',
1352
1804
  `token="$HOME"/${shellQuote(`${context.name}/${ENV_TOKEN_FILE}`)}`,
1353
1805
  'if [ -f "$token" ]; then chmod 600 "$token"; fi'
@@ -1402,6 +1854,25 @@ class SshTarget {
1402
1854
  }
1403
1855
  }
1404
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
+ }
1405
1876
  function validateHost(value) {
1406
1877
  if (typeof value !== "string" || value.trim() === "") {
1407
1878
  throw new Error("[SshTarget] host must be the server's hostname or IP address.");
@@ -1427,19 +1898,19 @@ function validateUser(value) {
1427
1898
  }
1428
1899
  return value;
1429
1900
  }
1430
- function validatePorts(value) {
1901
+ function validatePorts(value, field) {
1431
1902
  if (value === undefined)
1432
1903
  return {};
1433
1904
  if (typeof value !== "object" || value === null || Array.isArray(value)) {
1434
- 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 }\`.`);
1435
1906
  }
1436
1907
  const ports = {};
1437
1908
  for (const [service, port] of Object.entries(value)) {
1438
1909
  if (!Object.hasOwn(DEFAULT_PORTS, service)) {
1439
- 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.`);
1440
1911
  }
1441
1912
  if (!Number.isInteger(port) || port < 1024 || port > 65535) {
1442
- 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.`);
1443
1914
  }
1444
1915
  ports[service] = port;
1445
1916
  }
@@ -1482,4 +1953,4 @@ export {
1482
1953
  SshTarget
1483
1954
  };
1484
1955
 
1485
- //# debugId=1A9D7F7D9BA68E6064756E2164756E21
1956
+ //# debugId=F0A8F6E476574A6F64756E2164756E21