@sixb/deploy-ssh 0.2.0 → 0.2.5

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
@@ -409,6 +409,9 @@ function renderCiInstall(input) {
409
409
  ];
410
410
  }
411
411
 
412
+ // src/operations.ts
413
+ import { randomUUID } from "crypto";
414
+
412
415
  // src/update-page.ts
413
416
  function renderUpdatePage() {
414
417
  return `<!doctype html>
@@ -462,6 +465,7 @@ function renderProcessManifest(release, layout) {
462
465
  runDir: layout.runDir,
463
466
  logDir: layout.logDir,
464
467
  releasePath: layout.releaseRecord,
468
+ env: release.env,
465
469
  services: release.services.map((service) => ({
466
470
  name: service.name,
467
471
  processName: `${release.name}-${service.name}`,
@@ -652,10 +656,10 @@ function renderTakeLock(layout) {
652
656
  return [
653
657
  `exec 9>${shellQuote(layout.lock)}`,
654
658
  "if ! flock -n 9; then",
655
- ' echo "Waiting for another deploy of this project to finish\u2026"',
659
+ ' echo "Waiting for another deploy or script run of this project to finish\u2026"',
656
660
  "fi",
657
661
  "if ! flock -w 600 9; then",
658
- ' echo "Another deploy of this project has held its lock for 10 minutes." >&2',
662
+ ' echo "Another deploy or script run of this project has held its lock for 10 minutes." >&2',
659
663
  " exit 1",
660
664
  "fi"
661
665
  ].join(`
@@ -910,7 +914,10 @@ class SshShell {
910
914
  }
911
915
  async function runProcess(command, options) {
912
916
  if (options.terminal) {
913
- const child = Bun.spawn([...command], { stdio: ["inherit", "inherit", "inherit"] });
917
+ const child = Bun.spawn([...command], {
918
+ stdio: ["inherit", "inherit", "inherit"],
919
+ env: { ...process.env, ...options.env }
920
+ });
914
921
  const exitCode = await child.exited;
915
922
  if (exitCode !== 0)
916
923
  throw new RemoteScriptError(`exited with code ${exitCode}`, exitCode);
@@ -1061,6 +1068,42 @@ async function streamLogs(shell, name, options) {
1061
1068
  async function controlServices(shell, name, action, service) {
1062
1069
  await shell.run(ctlScript(name, [action, ...service ? [service] : []]));
1063
1070
  }
1071
+ async function runDeployScript(shell, name, script) {
1072
+ const root = `"$HOME"/${shellQuote(name)}`;
1073
+ const staged = `${root}/deploy/runs/${randomUUID()}`;
1074
+ await shell.run([
1075
+ `if [ ! -x ${root}/deploy/ctl ]; then`,
1076
+ ` echo "Nothing named ${name} is deployed on this server yet." >&2; exit 1`,
1077
+ "fi",
1078
+ "umask 077",
1079
+ `mkdir -p ${root}/deploy/runs`,
1080
+ `find ${root}/deploy/runs -type f -mmin +1440 -delete 2> /dev/null || true`,
1081
+ `cat > ${staged}`
1082
+ ].join(`
1083
+ `), { stdin: new Blob([Uint8Array.from(script.source)]).stream() });
1084
+ const run = [
1085
+ `cd ${root}`,
1086
+ `exec 9> ${root}/deploy/lock`,
1087
+ "if ! flock -s -n 9; then",
1088
+ ` echo "Waiting for a deploy of ${name} to finish\u2026" >&2`,
1089
+ "fi",
1090
+ "if ! flock -s -w 600 9; then",
1091
+ ` echo "A deploy of ${name} has held its lock for 10 minutes." >&2`,
1092
+ ` rm -f ${staged}`,
1093
+ " exit 1",
1094
+ "fi",
1095
+ `exec ${root}/deploy/ctl run ${staged} ${[script.path, ...script.args].map(shellQuote).join(" ")}`
1096
+ ].join(`
1097
+ `);
1098
+ try {
1099
+ await shell.run(run, { terminal: true });
1100
+ return 0;
1101
+ } catch (error) {
1102
+ if (error instanceof RemoteScriptError && error.exitCode !== 255)
1103
+ return error.exitCode;
1104
+ throw error;
1105
+ }
1106
+ }
1064
1107
  function ctlScript(name, args, options = {}) {
1065
1108
  return [
1066
1109
  `ctl="$HOME"/${shellQuote(name)}/deploy/ctl`,
@@ -1310,6 +1353,9 @@ class SshTarget {
1310
1353
  control(action, service, context) {
1311
1354
  return this.#connected((shell) => controlServices(shell, context.name, action, service));
1312
1355
  }
1356
+ run(script, context) {
1357
+ return this.#connected((shell) => runDeployScript(shell, context.name, script));
1358
+ }
1313
1359
  async check(release, context) {
1314
1360
  const { hostname } = await resolveSshConfig(this.location);
1315
1361
  const dns = checkDns(release, hostname);
@@ -1482,4 +1528,4 @@ export {
1482
1528
  SshTarget
1483
1529
  };
1484
1530
 
1485
- //# debugId=1A9D7F7D9BA68E6064756E2164756E21
1531
+ //# debugId=B4A600CC24E47FE664756E2164756E21