@intentius/chant-lexicon-pulumi 0.124.86 → 0.124.88

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/src/stack-ops.ts CHANGED
@@ -95,7 +95,7 @@ import type { GatedRequest, GatedRequestRules } from "@intentius/chant/lifecycle
95
95
  import type { ForgeTargetOptions } from "@intentius/chant/pr-forge";
96
96
  import { exportStateVersion, StateExportError, type StateVersionSource } from "@intentius/chant/state-export";
97
97
  import { runStateMigration, type MigrationCheck, type MigrationRootPlan, type StateMigrationEngine } from "@intentius/chant/state-migration";
98
- import { describeRun, forgeLiveRuns, releaseStateLock, type LiveRun, type LockInfo, type StateLockSource } from "@intentius/chant/state-unlock";
98
+ import { describeRun, forgeLiveRunsOf, releaseStateLock, type LiveRun, type LockInfo, type StateLockSource } from "@intentius/chant/state-unlock";
99
99
  import { parsePulumiUrn, pulumiNodeKey } from "@intentius/chant/pulumi-urn";
100
100
  import { pulumiStackBackend } from "./adopt";
101
101
  import { pulumiExportResources, pulumiPreviewDigest, pulumiSteps, PULUMI_UNCHANGED_OPS } from "./change-set";
@@ -148,6 +148,35 @@ export interface PulumiStackOpCommon {
148
148
  plunge?: PulumiRunner;
149
149
  env?: Record<string, string | undefined>;
150
150
  gated?: GatedRequestApi;
151
+ /**
152
+ * The command a waiting or refused request prints (#4911), as the product
153
+ * words it: a function of the gate (the request's subject) and the
154
+ * digest, which core's adapters take as `words.approveCommand`, or a
155
+ * template whose `{op}`, `{gate}`, `{digest}` and `{sign}` chant fills,
156
+ * `{sign}` being `--sign` when `rules.mode` is `sealed`. Default core's
157
+ * line.
158
+ */
159
+ approveCommand?: string | ((subject: string, digest: string) => string);
160
+ }
161
+
162
+ /** The approve line `common` asks for (#4911), as core's adapters take `words.approveCommand`; undefined for core's own. */
163
+ export function stackOpApproveCommand(common: Pick<PulumiStackOpCommon, "approveCommand" | "rules" | "op">): ((subject: string, digest: string) => string) | undefined {
164
+ const given = common.approveCommand;
165
+ if (given === undefined) return undefined;
166
+ if (typeof given === "function") return given;
167
+ const op = common.op ?? PULUMI_STACK_OPS_OP;
168
+ const sign = common.rules.mode === "sealed" ? "--sign" : "";
169
+ return (gate, digest) =>
170
+ given
171
+ .replace(/\{(op|gate|digest|sign)\}/g, (_, k: string) => ({ op, gate, digest, sign })[k as "op"])
172
+ .replace(/\s+/g, " ")
173
+ .trim();
174
+ }
175
+
176
+ /** The words core's walks take: the op, the kind, and the product's approve line when it gives one. */
177
+ function wordsOf(common: PulumiStackOpCommon, kind: PulumiStackOpKind) {
178
+ const approveCommand = stackOpApproveCommand(common);
179
+ return { op: common.op ?? PULUMI_STACK_OPS_OP, kind, ...(approveCommand ? { approveCommand } : {}) };
151
180
  }
152
181
 
153
182
  /** Which of core's checks stopped an operation that was approved (#4808): a state that moved between the plan and the write. */
@@ -353,7 +382,7 @@ export async function gatedStackExport(opts: PulumiStackExportOptions): Promise<
353
382
  source,
354
383
  out,
355
384
  ...(opts.version ? { version: opts.version } : {}),
356
- words: { op: opts.op ?? PULUMI_STACK_OPS_OP, kind: "stack-export" },
385
+ words: wordsOf(opts, "stack-export"),
357
386
  env,
358
387
  ...gatedSettings(opts),
359
388
  });
@@ -464,6 +493,36 @@ export async function pulumiStackLockIds(opts: { dir: string; stack: string; cwd
464
493
  return { checked: true, ...(at.backend ? { backend: at.backend } : {}), ids: diyStackLockIds(at.root, at.project, opts.stack) };
465
494
  }
466
495
 
496
+ /** Why {@link pulumiStackLockToCancel} names no lock (#4911). */
497
+ export type PulumiLockToCancelRefusal = "read-failed" | "not-listed" | "no-lock" | "many-locks";
498
+
499
+ /** The lock a cancel releases, or why none can be named without the caller's id. */
500
+ export type PulumiLockToCancel =
501
+ | { lockId: string; read: boolean }
502
+ | { refusal: PulumiLockToCancelRefusal; message: string; backend?: string; ids?: string[] };
503
+
504
+ /**
505
+ * The lock id a cancel asks to release (#4911): `given` when the caller
506
+ * names one, else the one lock {@link pulumiStackLockIds} finds on the
507
+ * stack's `file://` backend. No lock, two locks, a backend chant cannot
508
+ * list, or a read that failed answers a `refusal` with chant's message; a
509
+ * product words it from the code, the `backend` and the `ids`.
510
+ */
511
+ export async function pulumiStackLockToCancel(given: string | undefined, stack: { dir: string; stack: string; cwd?: string; project?: string; engine?: PulumiMemberEngine; env?: NodeJS.ProcessEnv }): Promise<PulumiLockToCancel> {
512
+ if (given) return { lockId: given, read: false };
513
+ let locks: PulumiStackLocks;
514
+ try {
515
+ locks = await pulumiStackLockIds(stack);
516
+ } catch (e) {
517
+ return { refusal: "read-failed", message: `a cancel names the lock it releases, and reading stack ${stack.stack}'s lock failed: ${(e as Error).message}` };
518
+ }
519
+ const backend = locks.backend ? { backend: locks.backend } : {};
520
+ if (!locks.checked) return { refusal: "not-listed", ...backend, message: `a cancel names the lock it releases here: chant reads a stack's lock from a file:// backend only, and stack ${stack.stack}'s backend is ${locks.backend ?? "none chant can find"}` };
521
+ if (locks.ids.length === 0) return { refusal: "no-lock", ...backend, ids: [], message: `stack ${stack.stack} holds no lock now, so there is nothing to cancel` };
522
+ if (locks.ids.length > 1) return { refusal: "many-locks", ...backend, ids: locks.ids, message: `stack ${stack.stack} holds ${locks.ids.length} locks (${locks.ids.join(", ")}); name the one to release` };
523
+ return { lockId: locks.ids[0]!, read: true };
524
+ }
525
+
467
526
  export interface PulumiStackCancelOptions extends PulumiStackOpCommon {
468
527
  dir: string;
469
528
  cwd?: string;
@@ -479,8 +538,10 @@ export interface PulumiStackCancelOptions extends PulumiStackOpCommon {
479
538
  * The repository's runs that have not finished (#4808), read before the
480
539
  * request and again just before the spend: while one that began before
481
540
  * the lock was taken is alive, nothing is released. A throw refuses the
482
- * release. Default core's `forgeLiveRuns` of the forge {@link forge}
483
- * names over the environment (`forgeTargetWith`).
541
+ * release. Default core's `forgeLiveRunsOf` of the forge {@link forge}
542
+ * names over the environment (`forgeTargetWith`); with no CI variables
543
+ * and no `forge.repo`, the stack directory's origin remote names the
544
+ * forge and the repository (#4920).
484
545
  */
485
546
  liveRuns?: () => Promise<LiveRun[]>;
486
547
  /** The forge whose runs are read when no `liveRuns` is given: `--forge`, `--repo` and the rest a product's flags give. */
@@ -495,15 +556,6 @@ export interface PulumiStackCancelResult {
495
556
  liveRuns: number;
496
557
  }
497
558
 
498
- /** The runs of the forge `forge` names over `env`, as core reads them (#4808). */
499
- function forgeRuns(env: Record<string, string | undefined>, forge: (ForgeTargetOptions & ForgeOverrides) | undefined): () => Promise<LiveRun[]> {
500
- return async () => {
501
- const { forgeTargetWith } = await import("@intentius/chant/forge-flags");
502
- const t = forgeTargetWith(env, forge ?? {});
503
- return forgeLiveRuns({ kind: t.kind, apiBase: t.apiBase, repo: t.repo, token: t.token, ...(t.tokenHeader ? { tokenHeader: t.tokenHeader } : {}) });
504
- };
505
- }
506
-
507
559
  /**
508
560
  * Release a stuck lock after an approval of its lock id, while no run that
509
561
  * may hold it is alive: core's `releaseStateLock` over the stack's lock.
@@ -560,8 +612,9 @@ export async function gatedStackCancel(opts: PulumiStackCancelOptions): Promise<
560
612
  subject: opts.stack,
561
613
  by: opts.by,
562
614
  lock,
563
- liveRuns: opts.liveRuns ?? forgeRuns(env, opts.forge),
564
- words: { op: opts.op ?? PULUMI_STACK_OPS_OP, kind: "stack-cancel" },
615
+ // #4911, #4920: core's reader of the forge's runs, the origin remote naming the forge on a laptop.
616
+ liveRuns: opts.liveRuns ?? forgeLiveRunsOf(env, opts.forge ?? {}, { cwd: dir }),
617
+ words: wordsOf(opts, "stack-cancel"),
565
618
  ...(opts.commit ? { commit: opts.commit } : {}),
566
619
  ...gatedSettings(opts),
567
620
  });
@@ -596,6 +649,25 @@ export interface PulumiMoveStack {
596
649
  dir: string;
597
650
  /** The stack, as `--source` or `--dest` takes it (`<org>/<project>/<stack>` across projects). */
598
651
  stack: string;
652
+ /**
653
+ * The stack's project (#4911). Given on both sides with a bare `stack`,
654
+ * the move names the destination as `--dest` takes it itself
655
+ * ({@link pulumiMoveDestStack}).
656
+ */
657
+ project?: string;
658
+ }
659
+
660
+ /**
661
+ * The destination as `pulumi state move --dest` takes it (#4911): the bare
662
+ * stack in the source's project, else `<org>/<project>/<stack>`. A DIY
663
+ * backend's organization is always `organization`; on Pulumi Cloud (no
664
+ * backend, or an http(s) one) it is `org` when given. A stack already
665
+ * qualified is kept.
666
+ */
667
+ export function pulumiMoveDestStack(source: { project: string }, dest: { project: string; stack: string; backend?: string }, opts: { org?: string } = {}): string {
668
+ if (dest.stack.includes("/") || dest.project === source.project) return dest.stack;
669
+ const cloud = !dest.backend || /^https?:\/\//.test(dest.backend);
670
+ return `${(cloud ? opts.org : undefined) ?? "organization"}/${dest.project}/${dest.stack}`;
599
671
  }
600
672
 
601
673
  export interface PulumiStackMoveOptions extends PulumiStackOpCommon {
@@ -607,6 +679,8 @@ export interface PulumiStackMoveOptions extends PulumiStackOpCommon {
607
679
  engine?: PulumiMemberEngine;
608
680
  runId?: string;
609
681
  commit?: string;
682
+ /** The Pulumi Cloud organization a destination in another project is named under ({@link pulumiMoveDestStack}). */
683
+ org?: string;
610
684
  }
611
685
 
612
686
  export interface PulumiStackMoveResult {
@@ -687,9 +761,15 @@ export async function gatedStackMove(opts: PulumiStackMoveOptions): Promise<Pulu
687
761
  if (engine !== destEngine) throw new Error(`${opts.source.member} runs on ${engine} and ${opts.dest.member} on ${destEngine}; a move needs both stacks on one engine`);
688
762
  const urns = [...new Set(opts.urns)].sort();
689
763
  const runners = { ...(opts.run ? { run: opts.run } : {}), ...(opts.plunge ? { plunge: opts.plunge } : {}), engine };
764
+ // #4911: with both projects, the destination's --dest name is the lexicon's to decide.
765
+ const destBackend = opts.source.project && opts.dest.project ? pulumiStackBackend(destDir, { env })?.url : undefined;
766
+ const destStack =
767
+ opts.source.project && opts.dest.project
768
+ ? pulumiMoveDestStack({ project: opts.source.project }, { project: opts.dest.project, stack: opts.dest.stack, ...(destBackend ? { backend: destBackend } : {}) }, opts.org ? { org: opts.org } : {})
769
+ : opts.dest.stack;
690
770
  const sides = [
691
771
  { ...opts.source, dir: sourceDir },
692
- { ...opts.dest, dir: destDir },
772
+ { ...opts.dest, stack: destStack, dir: destDir },
693
773
  ];
694
774
  const side = (member: string) => sides.find((s) => s.member === member)!;
695
775
 
@@ -706,7 +786,7 @@ export async function gatedStackMove(opts: PulumiStackMoveOptions): Promise<Pulu
706
786
  const left = moveLeftovers(urns, before.source, before.dest);
707
787
  if (left.length > 0) throw new Error(`refusing to move: the previews change more than the moved resources, so the stacks would not preview no change after it: ${left.join("; ")}`);
708
788
  if (engine === "plunge") {
709
- const args = plungeMoveArgs({ source: opts.source.stack, dest: opts.dest.stack, urns });
789
+ const args = plungeMoveArgs({ source: opts.source.stack, dest: destStack, urns });
710
790
  let view: ReturnType<typeof moveView>;
711
791
  try {
712
792
  view = moveView((await runAllowedPlunge(opts.plunge ?? defaultPlungeRunner, args, sourceDir)).stdout);
@@ -720,7 +800,7 @@ export async function gatedStackMove(opts: PulumiStackMoveOptions): Promise<Pulu
720
800
  }
721
801
  }
722
802
  const digestOf = async (member: string): Promise<string> => sha256(await exportNow({ ...side(member), ...runners }));
723
- const move = { source: opts.source.stack, dest: opts.dest.stack, urns };
803
+ const move = { source: opts.source.stack, dest: destStack, urns };
724
804
  const proof: MigrationCheck = { changes: [], summary: `the previews change only the ${urns.length} moved resource(s)` };
725
805
  const roots: MigrationRootPlan[] = [];
726
806
  for (const s of sides) {
@@ -771,17 +851,17 @@ export async function gatedStackMove(opts: PulumiStackMoveOptions): Promise<Pulu
771
851
  },
772
852
  };
773
853
 
774
- const subject = `move:${opts.source.stack}:${opts.dest.stack}`;
775
- const fields = { source: opts.source.stack, dest: opts.dest.stack, engine, urns: urns.join(",") };
854
+ const subject = `move:${opts.source.stack}:${destStack}`;
855
+ const fields = { source: opts.source.stack, dest: destStack, engine, urns: urns.join(",") };
776
856
  const r = await runStateMigration({
777
857
  name: subject,
778
858
  by: opts.by,
779
859
  roots,
780
860
  engine: migration,
781
- words: { op: opts.op ?? PULUMI_STACK_OPS_OP, kind: "stack-move" },
861
+ words: wordsOf(opts, "stack-move"),
782
862
  fields,
783
863
  detail: (res) => ({ urns: urns.length, engine, afterNoChange: res.status === "applied" }),
784
- description: `move of ${urns.length} resource(s) from ${opts.source.stack} to ${opts.dest.stack}`,
864
+ description: `move of ${urns.length} resource(s) from ${opts.source.stack} to ${destStack}`,
785
865
  ...(opts.runId ? { runId: opts.runId } : {}),
786
866
  ...(opts.commit ? { commit: opts.commit } : {}),
787
867
  ...gatedSettings(opts),
@@ -981,11 +1061,13 @@ export async function gatedStackAction(opts: PulumiStackActionOptions): Promise<
981
1061
  }
982
1062
  }) as GatedOpAct<{ result: PulumiStackActionResult; error?: string }>,
983
1063
  );
1064
+ const approve = stackOpApproveCommand(opts);
1065
+ const command = (fallback: string): string => (approve ? approve(subject, r.digest) : fallback);
984
1066
  switch (r.status) {
985
1067
  case "waiting":
986
- return { status: "waiting", digest: r.digest, request: r.request, command: r.command };
1068
+ return { status: "waiting", digest: r.digest, request: r.request, command: command(r.command) };
987
1069
  case "refused":
988
- return { status: "refused", digest: r.digest, request: r.request, by: r.by, reason: r.reason, command: r.command };
1070
+ return { status: "refused", digest: r.digest, request: r.request, by: r.by, reason: r.reason, command: command(r.command) };
989
1071
  case "spent":
990
1072
  return { status: "spent", digest: r.digest, request: r.request };
991
1073
  case "held":