@intentius/chant-lexicon-terraform 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.
Files changed (72) hide show
  1. package/dist/action-ops.d.ts.map +1 -1
  2. package/dist/action-ops.js +3 -1
  3. package/dist/chant-floors.d.ts +30 -0
  4. package/dist/chant-floors.d.ts.map +1 -0
  5. package/dist/chant-floors.js +29 -0
  6. package/dist/components/capability-plugin.d.ts +15 -1
  7. package/dist/components/capability-plugin.d.ts.map +1 -1
  8. package/dist/components/capability-plugin.js +13 -5
  9. package/dist/components/compose-group.d.ts +5 -1
  10. package/dist/components/compose-group.d.ts.map +1 -1
  11. package/dist/components/compose-group.js +7 -4
  12. package/dist/engine-timings.d.ts +249 -0
  13. package/dist/engine-timings.d.ts.map +1 -0
  14. package/dist/engine-timings.js +569 -0
  15. package/dist/integrity.json +2 -2
  16. package/dist/live-roots.d.ts +93 -0
  17. package/dist/live-roots.d.ts.map +1 -0
  18. package/dist/live-roots.js +206 -0
  19. package/dist/manifest.json +1 -1
  20. package/dist/migration-history.d.ts +105 -0
  21. package/dist/migration-history.d.ts.map +1 -0
  22. package/dist/migration-history.js +159 -0
  23. package/dist/module-catalog/index.d.ts +1 -0
  24. package/dist/module-catalog/index.d.ts.map +1 -1
  25. package/dist/module-catalog/index.js +3 -1
  26. package/dist/op/activities/live-detect.d.ts +15 -0
  27. package/dist/op/activities/live-detect.d.ts.map +1 -1
  28. package/dist/op/activities/live-detect.js +94 -0
  29. package/dist/op/activities/terraform.d.ts +1 -65
  30. package/dist/op/activities/terraform.d.ts.map +1 -1
  31. package/dist/op/activities/terraform.js +4 -54
  32. package/dist/op/command-hook.d.ts +88 -0
  33. package/dist/op/command-hook.d.ts.map +1 -0
  34. package/dist/op/command-hook.js +115 -0
  35. package/dist/registry/index.d.ts +1 -0
  36. package/dist/registry/index.d.ts.map +1 -1
  37. package/dist/registry/index.js +1 -0
  38. package/dist/state/exec.d.ts +23 -0
  39. package/dist/state/exec.d.ts.map +1 -1
  40. package/dist/state/exec.js +38 -0
  41. package/dist/state-ops.d.ts.map +1 -1
  42. package/dist/state-ops.js +3 -1
  43. package/dist/terragrunt/index.d.ts +3 -1
  44. package/dist/terragrunt/index.d.ts.map +1 -1
  45. package/dist/terragrunt/index.js +3 -1
  46. package/dist/terragrunt/place.d.ts +27 -0
  47. package/dist/terragrunt/place.d.ts.map +1 -0
  48. package/dist/terragrunt/place.js +52 -0
  49. package/package.json +2 -2
  50. package/src/action-ops.ts +3 -1
  51. package/src/chant-floors.ts +32 -0
  52. package/src/components/capability-plugin.ts +27 -6
  53. package/src/components/compose-group.test.ts +29 -0
  54. package/src/components/compose-group.ts +12 -5
  55. package/src/components/terraform-apply-command-hook.test.ts +43 -0
  56. package/src/engine-timings.test.ts +203 -0
  57. package/src/engine-timings.ts +718 -0
  58. package/src/live-roots.test.ts +80 -0
  59. package/src/live-roots.ts +244 -0
  60. package/src/migration-history.test.ts +83 -0
  61. package/src/migration-history.ts +199 -0
  62. package/src/module-catalog/index.ts +4 -1
  63. package/src/op/activities/live-detect.ts +87 -0
  64. package/src/op/activities/terraform.ts +12 -111
  65. package/src/op/command-hook.ts +182 -0
  66. package/src/registry/index.ts +1 -0
  67. package/src/state/engine-env.test.ts +18 -0
  68. package/src/state/exec.ts +38 -0
  69. package/src/state-ops.ts +3 -1
  70. package/src/terragrunt/index.ts +3 -1
  71. package/src/terragrunt/place.test.ts +42 -0
  72. package/src/terragrunt/place.ts +75 -0
@@ -189,3 +189,90 @@ export function detectLivePolicyVerbs(dir: string, binary = "choudoufu"): LivePo
189
189
 
190
190
  return undefined;
191
191
  }
192
+
193
+ /**
194
+ * Whether the root module directory `dir` keeps its resources under
195
+ * choudoufu's live markers (chant#4910): an `estate.chdf.hcl` sidecar, or a
196
+ * `live` block in a `terraform` block, whether or not it names an estate.
197
+ */
198
+ export function declaresLive(dir: string, binary = "choudoufu"): boolean {
199
+ if (existsSync(join(dir, LIVE_SIDECAR_FILENAME))) return true;
200
+ let names: string[];
201
+ try {
202
+ names = liveFiles(dir, binary);
203
+ } catch {
204
+ return false;
205
+ }
206
+ return names.some((name) => {
207
+ try {
208
+ return liveBlocksOf(name, stripLineComments(name, readFileSync(join(dir, name), "utf-8"))).length > 0;
209
+ } catch {
210
+ return false;
211
+ }
212
+ });
213
+ }
214
+
215
+ /**
216
+ * Each `data "terraform_estate_outputs"` block of the root module directory
217
+ * `dir` (chant#4910): its label and the estate whose recorded outputs it
218
+ * reads. A block that names no estate as a literal is left out.
219
+ */
220
+ export function estateOutputReads(dir: string, binary = "choudoufu"): Array<{ name: string; estate: string }> {
221
+ let names: string[];
222
+ try {
223
+ names = liveFiles(dir, binary);
224
+ } catch {
225
+ return [];
226
+ }
227
+ const out: Array<{ name: string; estate: string }> = [];
228
+ for (const name of names) {
229
+ let source: string;
230
+ try {
231
+ source = readFileSync(join(dir, name), "utf-8");
232
+ } catch {
233
+ continue;
234
+ }
235
+ if (isJsonConfigFile(name)) {
236
+ let doc: unknown;
237
+ try {
238
+ doc = JSON.parse(source);
239
+ } catch {
240
+ continue;
241
+ }
242
+ const data = [(doc as { data?: unknown } | null)?.data].flat();
243
+ for (const d of data) {
244
+ const reads = d && typeof d === "object" ? (d as Record<string, unknown>).terraform_estate_outputs : undefined;
245
+ for (const r of [reads].flat()) {
246
+ if (!r || typeof r !== "object") continue;
247
+ for (const [label, body] of Object.entries(r as Record<string, unknown>)) {
248
+ const estate = [body].flat().map((b) => nonEmpty((b as { estate?: unknown } | null)?.estate)).find((e) => e !== undefined);
249
+ if (estate) out.push({ name: label, estate });
250
+ }
251
+ }
252
+ }
253
+ continue;
254
+ }
255
+ const text = stripLineComments(name, source);
256
+ for (const m of text.matchAll(/\bdata\s+"terraform_estate_outputs"\s+"([^"]+)"\s*\{/g)) {
257
+ const body = bodyAt(text, m.index! + m[0].length - 1);
258
+ const estate = body === undefined ? undefined : extractEstateAttr(body);
259
+ if (estate) out.push({ name: m[1]!, estate });
260
+ }
261
+ }
262
+ return out;
263
+ }
264
+
265
+ /** An HCL file's text with its `#` and `//` line comments blanked; a JSON file's as it is. */
266
+ function stripLineComments(name: string, source: string): string {
267
+ return isJsonConfigFile(name) ? source : source.replace(/^\s*(#|\/\/).*$/gm, "");
268
+ }
269
+
270
+ /** The inner text of the `{ ... }` block whose opening brace is at `open`, or undefined when it never closes. */
271
+ function bodyAt(source: string, open: number): string | undefined {
272
+ let depth = 0;
273
+ for (let i = open; i < source.length; i++) {
274
+ if (source[i] === "{") depth++;
275
+ else if (source[i] === "}" && --depth === 0) return source.slice(open + 1, i);
276
+ }
277
+ return undefined;
278
+ }
@@ -39,7 +39,6 @@
39
39
  * {@link MIN_CHOUDOUFU_VERSION}.
40
40
  */
41
41
 
42
- import { AsyncLocalStorage } from "node:async_hooks";
43
42
  import { exec } from "node:child_process";
44
43
  import { existsSync, mkdirSync, mkdtempSync, readFileSync, rmSync, writeFileSync } from "node:fs";
45
44
  import { tmpdir } from "node:os";
@@ -53,6 +52,7 @@ import { choudoufuSetPlanParts, plannerForBinary, terraformChangeSetPart } from
53
52
  import type { ChangeSetPart } from "@intentius/chant/change-set";
54
53
  import type { TerraformConfig, TerraformRootConfig } from "../../config";
55
54
  import { detectLiveEstate } from "./live-detect";
55
+ import { commandWords, currentCommandScope, scopedCommand, type TerraformCommand } from "../command-hook";
56
56
  import { backendConfigValues } from "../../backend-config";
57
57
  import { lockTimeoutArgs } from "../../lock-timeout";
58
58
  import { coneArgs, readPlanCone, type PlanCone } from "../../cone";
@@ -1043,112 +1043,15 @@ interface ExecFailure {
1043
1043
  stderr?: string;
1044
1044
  }
1045
1045
 
1046
- /**
1047
- * One engine command as a product's {@link TerraformCommandHook} sees it
1048
- * before it runs (chant#4865).
1049
- */
1050
- export interface TerraformCommand {
1051
- /** The step's root the command runs for. */
1052
- root: string;
1053
- /** The upstream root whose state the command reads for `root`'s `reads`, when it runs for one. */
1054
- upstream?: string;
1055
- /** The step method that runs it: `plan`, `run` (the apply) or `outputs`. */
1056
- method: string;
1057
- /** The binary, as the command line names it. */
1058
- binary: string;
1059
- /** The subcommand, the first argument that is not a flag: `init`, `plan`, `show`, `apply`, `output`, `workspace`, `live-plan-set`. */
1060
- name: string;
1061
- /** The arguments after the binary, unquoted. */
1062
- args: string[];
1063
- /** The command line as the shell runs it. */
1064
- line: string;
1065
- /** The directory it runs in. */
1066
- cwd: string;
1067
- /** The variables chant sets for it over the job's environment, such as `TF_IN_AUTOMATION`, `TF_WORKSPACE` and the run hook's `processEnv`. */
1068
- env: Record<string, string>;
1069
- }
1070
-
1071
- /** How a {@link TerraformCommand} came out, for the hook's `after` (chant#4865). */
1072
- export interface TerraformCommandResult {
1073
- /** The exit code; null when the command did not exit on its own (a signal, an abort, a spawn failure). */
1074
- code: number | null;
1075
- /** How long it ran, in seconds. */
1076
- seconds: number;
1077
- stdout: string;
1078
- stderr: string;
1079
- }
1080
-
1081
- /** What a {@link TerraformCommandHook} answers for one command (chant#4865). */
1082
- export interface TerraformCommandRun {
1083
- /** Variables laid over the command's own, such as a span receiver's exporter or a trace parent. */
1084
- env?: Record<string, string>;
1085
- /** Called once the command ends, on a failure too, before the step reads its result. */
1086
- after?: (result: TerraformCommandResult) => void | Promise<void>;
1087
- }
1088
-
1089
- /**
1090
- * A product's hook around each engine command a `terraform-apply` step runs
1091
- * for a root (chant#4865), `terraformApplyCapabilityWith({ onCommand })`:
1092
- * called before the command with what it is, and its answer's `after` once
1093
- * it ends. terragucci times each root's plan and apply with it and sets its
1094
- * span receiver's variables, as its own runs of the binary did.
1095
- */
1096
- export type TerraformCommandHook = (command: TerraformCommand) => TerraformCommandRun | void | Promise<TerraformCommandRun | void>;
1097
-
1098
- /** The hook, and the root and method it is for, that the commands run in an async call see (chant#4865). */
1099
- interface CommandScope {
1100
- hook: TerraformCommandHook;
1101
- root: string;
1102
- upstream?: string;
1103
- method: string;
1104
- }
1105
-
1106
- /**
1107
- * The command scope of the step call that runs the engine (chant#4865). An
1108
- * async-local store, so each root a wave runs at once (chant#4864) sees its
1109
- * own, and the activities need no hook argument threaded through.
1110
- */
1111
- const commandScope = new AsyncLocalStorage<CommandScope>();
1112
-
1113
- /**
1114
- * Run `fn` with every engine command it runs going through `hook`, as
1115
- * commands for `scope.root` (chant#4865). Inside another scope, a field this
1116
- * one leaves out (`upstream`) is not kept from the outer.
1117
- */
1118
- export function withTerraformCommandHook<T>(hook: TerraformCommandHook | undefined, scope: Omit<CommandScope, "hook">, fn: () => Promise<T>): Promise<T> {
1119
- if (!hook) return fn();
1120
- return commandScope.run({ hook, ...scope }, fn);
1121
- }
1122
-
1123
- /** Run `fn` with the commands it runs marked as a read of `upstream`'s state for the scope's root (chant#4865). Outside a scope, `fn` as it is. */
1124
- export function asUpstreamCommands<T>(upstream: string, fn: () => Promise<T>): Promise<T> {
1125
- const scope = commandScope.getStore();
1126
- return scope ? commandScope.run({ ...scope, upstream }, fn) : fn();
1127
- }
1128
-
1129
- /** The words of a command line the builders here wrote: plain words, and `quoteArg`'s single-quoted ones. */
1130
- export function commandWords(line: string): string[] {
1131
- const words: string[] = [];
1132
- let word: string | undefined;
1133
- for (let i = 0; i < line.length; i++) {
1134
- const c = line[i]!;
1135
- if (c === " ") {
1136
- if (word !== undefined) words.push(word);
1137
- word = undefined;
1138
- } else if (c === "'") {
1139
- const end = line.indexOf("'", i + 1);
1140
- const stop = end < 0 ? line.length : end;
1141
- word = (word ?? "") + line.slice(i + 1, stop);
1142
- i = stop;
1143
- } else if (c === "\\" && i + 1 < line.length) {
1144
- word = (word ?? "") + line[++i];
1145
- } else {
1146
- word = (word ?? "") + c;
1147
- }
1148
- }
1149
- if (word !== undefined) words.push(word);
1150
- return words;
1151
- }
1046
+ export {
1047
+ asUpstreamCommands,
1048
+ commandWords,
1049
+ withTerraformCommandHook,
1050
+ type TerraformCommand,
1051
+ type TerraformCommandHook,
1052
+ type TerraformCommandResult,
1053
+ type TerraformCommandRun,
1054
+ } from "../command-hook";
1152
1055
 
1153
1056
  /** Run `cmd` in `dir`, forwarding the signal so an abort kills the child. */
1154
1057
  function runPlain(cmd: string, dir: string, env: Record<string, string>, signal?: AbortSignal, unset: readonly string[] = []): Promise<{ stdout: string; stderr: string }> {
@@ -1169,14 +1072,12 @@ async function run(
1169
1072
  signal?: AbortSignal,
1170
1073
  unset: readonly string[] = [],
1171
1074
  ): Promise<{ stdout: string; stderr: string }> {
1172
- const scope = commandScope.getStore();
1075
+ const scope = currentCommandScope();
1173
1076
  if (!scope) return runPlain(cmd, dir, env, signal, unset);
1174
1077
  const words = commandWords(cmd);
1175
1078
  const args = words.slice(1);
1176
1079
  const command: TerraformCommand = {
1177
- root: scope.root,
1178
- ...(scope.upstream !== undefined ? { upstream: scope.upstream } : {}),
1179
- method: scope.method,
1080
+ ...scopedCommand(scope),
1180
1081
  binary: words[0] ?? "",
1181
1082
  name: args.find((a) => !a.startsWith("-")) ?? "",
1182
1083
  args,
@@ -0,0 +1,182 @@
1
+ /**
2
+ * The hook a product puts around each engine command `terraform-apply`
3
+ * runs (chant#4865), and the async-local scope that carries it to the
4
+ * activities that run the binary. Kept apart from
5
+ * `./activities/terraform.ts` so the composed group's runs
6
+ * (`../components/compose-group.ts`, through `BinaryExec`) and the engine
7
+ * timings (`../engine-timings.ts`) read the same scope (chant#4887).
8
+ */
9
+ import { AsyncLocalStorage } from "node:async_hooks";
10
+ import type { BinaryExec, Ran } from "../state/exec";
11
+
12
+ /**
13
+ * One engine command as a product's {@link TerraformCommandHook} sees it
14
+ * before it runs (chant#4865).
15
+ */
16
+ export interface TerraformCommand {
17
+ /** The step's root the command runs for. */
18
+ root: string;
19
+ /** The upstream root whose state the command reads for `root`'s `reads`, when it runs for one. */
20
+ upstream?: string;
21
+ /**
22
+ * The roots a composed group's command runs for (chant#4887): a group's
23
+ * plan and apply run one engine command for all its members, in the
24
+ * composed directory. `root` is then the first of them.
25
+ */
26
+ members?: string[];
27
+ /** The step method that runs it: `plan`, `run` (the apply) or `outputs`. */
28
+ method: string;
29
+ /** The binary, as the command line names it. */
30
+ binary: string;
31
+ /** The subcommand, the first argument that is not a flag: `init`, `plan`, `show`, `apply`, `output`, `workspace`, `live-plan-set`. */
32
+ name: string;
33
+ /** The arguments after the binary, unquoted. */
34
+ args: string[];
35
+ /** The command line as the shell runs it. */
36
+ line: string;
37
+ /** The directory it runs in. */
38
+ cwd: string;
39
+ /** The variables chant sets for it over the job's environment, such as `TF_IN_AUTOMATION`, `TF_WORKSPACE` and the run hook's `processEnv`. */
40
+ env: Record<string, string>;
41
+ }
42
+
43
+ /** How a {@link TerraformCommand} came out, for the hook's `after` (chant#4865). */
44
+ export interface TerraformCommandResult {
45
+ /** The exit code; null when the command did not exit on its own (a signal, an abort, a spawn failure). */
46
+ code: number | null;
47
+ /** How long it ran, in seconds. */
48
+ seconds: number;
49
+ stdout: string;
50
+ stderr: string;
51
+ }
52
+
53
+ /** What a {@link TerraformCommandHook} answers for one command (chant#4865). */
54
+ export interface TerraformCommandRun {
55
+ /** Variables laid over the command's own, such as a span receiver's exporter or a trace parent. */
56
+ env?: Record<string, string>;
57
+ /** Called once the command ends, on a failure too, before the step reads its result. */
58
+ after?: (result: TerraformCommandResult) => void | Promise<void>;
59
+ }
60
+
61
+ /**
62
+ * A product's hook around each engine command a `terraform-apply` step runs
63
+ * for a root (chant#4865), `terraformApplyCapabilityWith({ onCommand })`:
64
+ * called before the command with what it is, and its answer's `after` once
65
+ * it ends. terragucci times each root's plan and apply with it and sets its
66
+ * span receiver's variables, as its own runs of the binary did.
67
+ */
68
+ export type TerraformCommandHook = (command: TerraformCommand) => TerraformCommandRun | void | Promise<TerraformCommandRun | void>;
69
+
70
+ /** The hook, and the root and method it is for, that the commands run in an async call see (chant#4865). */
71
+ export interface CommandScope {
72
+ hook: TerraformCommandHook;
73
+ root: string;
74
+ upstream?: string;
75
+ /** The members of a composed group, for its commands (chant#4887). */
76
+ members?: string[];
77
+ method: string;
78
+ }
79
+
80
+ /**
81
+ * The command scope of the step call that runs the engine (chant#4865). An
82
+ * async-local store, so each root a wave runs at once (chant#4864) sees its
83
+ * own, and the activities need no hook argument threaded through.
84
+ */
85
+ const commandScope = new AsyncLocalStorage<CommandScope>();
86
+
87
+ /**
88
+ * Run `fn` with every engine command it runs going through `hook`, as
89
+ * commands for `scope.root` (chant#4865). Inside another scope, a field this
90
+ * one leaves out (`upstream`, `members`) is not kept from the outer.
91
+ */
92
+ export function withTerraformCommandHook<T>(hook: TerraformCommandHook | undefined, scope: Omit<CommandScope, "hook">, fn: () => Promise<T>): Promise<T> {
93
+ if (!hook) return fn();
94
+ return commandScope.run({ hook, ...scope }, fn);
95
+ }
96
+
97
+ /** Run `fn` with the commands it runs marked as a read of `upstream`'s state for the scope's root (chant#4865). Outside a scope, `fn` as it is. */
98
+ export function asUpstreamCommands<T>(upstream: string, fn: () => Promise<T>): Promise<T> {
99
+ const scope = commandScope.getStore();
100
+ return scope ? commandScope.run({ ...scope, upstream }, fn) : fn();
101
+ }
102
+
103
+ /** The scope the current async call runs in, or undefined outside one. */
104
+ export function currentCommandScope(): CommandScope | undefined {
105
+ return commandScope.getStore();
106
+ }
107
+
108
+ /** The fields of a {@link TerraformCommand} that come from the scope. */
109
+ export function scopedCommand(scope: CommandScope): Pick<TerraformCommand, "root" | "upstream" | "members" | "method"> {
110
+ return {
111
+ root: scope.root,
112
+ ...(scope.upstream !== undefined ? { upstream: scope.upstream } : {}),
113
+ ...(scope.members !== undefined ? { members: [...scope.members] } : {}),
114
+ method: scope.method,
115
+ };
116
+ }
117
+
118
+ /**
119
+ * `exec` with each run going through the command hook of the step call it
120
+ * runs in (chant#4887), as the activities' own commands do: a composed
121
+ * group's `init`, `plan`, `show` and `apply`, and the `version` its estate
122
+ * check asks. The hook sees the variables the run sets over the job's
123
+ * environment, its variables are laid over the run's, and its `after` gets
124
+ * the exit code, time and output. Outside a scope, `exec` as it is.
125
+ */
126
+ export function hookedBinaryExec(exec: BinaryExec): BinaryExec {
127
+ return async (binary, args, dir, env, io) => {
128
+ const scope = commandScope.getStore();
129
+ if (!scope) return exec(binary, args, dir, env, io);
130
+ const own: Record<string, string> = { TF_IN_AUTOMATION: "1" };
131
+ for (const [k, v] of Object.entries(env)) if (v !== undefined && process.env[k] !== v) own[k] = v;
132
+ const command: TerraformCommand = {
133
+ ...scopedCommand(scope),
134
+ binary,
135
+ name: args.find((a) => !a.startsWith("-")) ?? "",
136
+ args: [...args],
137
+ line: [binary, `-chdir=${dir}`, ...args].map((w) => (/^[\w@%+=:,./-]+$/.test(w) ? w : quoteWord(w))).join(" "),
138
+ cwd: dir,
139
+ env: own,
140
+ };
141
+ const given = (await scope.hook(command)) ?? {};
142
+ const start = performance.now();
143
+ let ran: Ran;
144
+ try {
145
+ ran = await exec(binary, args, dir, given.env ? { ...env, ...given.env } : env, io);
146
+ } catch (err) {
147
+ await given.after?.({ code: null, seconds: (performance.now() - start) / 1000, stdout: "", stderr: err instanceof Error ? err.message : String(err) });
148
+ throw err;
149
+ }
150
+ // `out` holds stdout and stderr together, as they came; stderr alone is what is left once stdout is taken out of it.
151
+ const stderr = ran.out.startsWith(ran.stdout) ? ran.out.slice(ran.stdout.length) : ran.out;
152
+ await given.after?.({ code: ran.code, seconds: (performance.now() - start) / 1000, stdout: ran.stdout, stderr });
153
+ return ran;
154
+ };
155
+ }
156
+
157
+ /** A word single-quoted for a shell, as `quoteArg` writes it. */
158
+ const quoteWord = (w: string): string => `'${w.replace(/'/g, `'\\''`)}'`;
159
+
160
+ /** The words of a command line the builders here wrote: plain words, and `quoteArg`'s single-quoted ones. */
161
+ export function commandWords(line: string): string[] {
162
+ const words: string[] = [];
163
+ let word: string | undefined;
164
+ for (let i = 0; i < line.length; i++) {
165
+ const c = line[i]!;
166
+ if (c === " ") {
167
+ if (word !== undefined) words.push(word);
168
+ word = undefined;
169
+ } else if (c === "'") {
170
+ const end = line.indexOf("'", i + 1);
171
+ const stop = end < 0 ? line.length : end;
172
+ word = (word ?? "") + line.slice(i + 1, stop);
173
+ i = stop;
174
+ } else if (c === "\\" && i + 1 < line.length) {
175
+ word = (word ?? "") + line[++i];
176
+ } else {
177
+ word = (word ?? "") + c;
178
+ }
179
+ }
180
+ if (word !== undefined) words.push(word);
181
+ return words;
182
+ }
@@ -29,3 +29,4 @@ export * from "./source-forms";
29
29
  export * from "./proxies";
30
30
  export { moduleConsumers, isLocalSource, type ModuleConsumer } from "../pin/consumers";
31
31
  export { usedSources, type UsedSource, type UnreadSource, type UsedSourcesOptions } from "../used-sources";
32
+ export { MODULE_CONSUMERS_FLOOR, USED_SOURCES_FLOOR } from "../chant-floors";
@@ -0,0 +1,18 @@
1
+ /** chant#4910: the engine's environment without the forge's tokens, and the binary runner that applies it. */
2
+ import { describe, expect, test } from "vitest";
3
+ import { engineEnv, tokenlessBinaryExec, type BinaryExec } from "./exec";
4
+
5
+ describe("engineEnv (chant#4910)", () => {
6
+ test("drops chant's forge token variables, the product's own, and any variable holding one's value, but never a TF_ variable", () => {
7
+ const env = { PATH: "/bin", GITHUB_TOKEN: "ghs_1", TG_TOKEN: "glpat-2", GITLAB_TOKEN: "glpat-2", MY_COPY: "ghs_1", TF_VAR_token: "glpat-2", TF_HTTP_PASSWORD: "ghs_1", AWS_REGION: "eu-west-1" };
8
+ expect(engineEnv(env, ["TG_TOKEN"])).toEqual({ PATH: "/bin", TF_VAR_token: "glpat-2", TF_HTTP_PASSWORD: "ghs_1", AWS_REGION: "eu-west-1" });
9
+ expect(engineEnv({ TG_TOKEN: "x", PATH: "/bin" })).toEqual({ TG_TOKEN: "x", PATH: "/bin" });
10
+ });
11
+
12
+ test("tokenlessBinaryExec runs the binary with that environment", async () => {
13
+ let got: NodeJS.ProcessEnv = {};
14
+ const exec: BinaryExec = async (_b, _a, _d, env) => ((got = env), { code: 0, stdout: "", out: "" });
15
+ await tokenlessBinaryExec(["TG_MERGE_TOKEN"], exec)("tofu", ["plan"], "/w", { TG_MERGE_TOKEN: "m", CI_JOB_TOKEN: "j", HOME: "/h" });
16
+ expect(got).toEqual({ HOME: "/h" });
17
+ });
18
+ });
package/src/state/exec.ts CHANGED
@@ -6,6 +6,7 @@
6
6
  * `BinaryExec`, `runBinary`, `StateFile`, `StateResource` and `pullState`.
7
7
  */
8
8
  import { spawn } from "node:child_process";
9
+ import { FORGE_TOKEN_VARS } from "@intentius/chant/ci-handover";
9
10
 
10
11
  export interface Ran {
11
12
  code: number;
@@ -68,6 +69,43 @@ export const runBinary: BinaryExec = (binary, args, dir, env, io) =>
68
69
  });
69
70
  });
70
71
 
72
+ /**
73
+ * `env` without the forge's tokens, for the engine (chant#4910): the
74
+ * providers, modules and external data sources the binary runs are the
75
+ * change's own code, and none of it needs the forge's API. Left out: chant's
76
+ * {@link FORGE_TOKEN_VARS}, the product's own token variables (`tokens`,
77
+ * such as terragucci's `TG_TOKEN` and `TG_MERGE_TOKEN`), and any other
78
+ * variable holding the same value as one of them (GitLab's `GITLAB_TOKEN`
79
+ * set from the product's, or a variable a product's `token_env` names). A
80
+ * variable whose name starts with `TF_` is passed as set, so a backend
81
+ * password or a `TF_VAR_` given to the binary on purpose still reaches it.
82
+ *
83
+ * Lifted from terragucci's `binaryEnv` (binary-env.ts).
84
+ */
85
+ export function engineEnv(env: NodeJS.ProcessEnv, tokens: readonly string[] = []): NodeJS.ProcessEnv {
86
+ const names = new Set<string>([...FORGE_TOKEN_VARS, ...tokens]);
87
+ const secrets = new Set([...names].map((n) => env[n]).filter((v): v is string => typeof v === "string" && v.length > 0));
88
+ const out: NodeJS.ProcessEnv = {};
89
+ for (const [k, v] of Object.entries(env)) {
90
+ if (names.has(k)) continue;
91
+ if (v !== undefined && secrets.has(v) && !k.startsWith("TF_")) continue;
92
+ out[k] = v;
93
+ }
94
+ return out;
95
+ }
96
+
97
+ /**
98
+ * {@link runBinary} with the forge's tokens left out of the binary's
99
+ * environment ({@link engineEnv}), `tokens` naming the product's own
100
+ * (chant#4910). A product that ran the binary through its own wrapper over
101
+ * `runBinary` passes this one wherever a `BinaryExec` is taken. `runBinary`
102
+ * itself still passes the environment as given, since a project's provider
103
+ * may read a forge token on purpose (the GitHub provider reads `GITHUB_TOKEN`).
104
+ */
105
+ export function tokenlessBinaryExec(tokens: readonly string[] = [], exec: BinaryExec = runBinary): BinaryExec {
106
+ return (binary, args, dir, env, io) => exec(binary, args, dir, engineEnv(env, tokens), io);
107
+ }
108
+
71
109
  /** A state file as `state pull` prints it (format version 4); only what a run touches is named. */
72
110
  export interface StateFile {
73
111
  version: number;
package/src/state-ops.ts CHANGED
@@ -52,6 +52,7 @@ import { gitlabStore, shownUrl, type GitLabFetch } from "./state/gitlab";
52
52
  import type { StoreFetch } from "./state/object-store";
53
53
  import { stateStore } from "./state/store";
54
54
  import { gitlabVersionSource, stateVersionSource } from "./state/versions";
55
+ import { actorFromEnv } from "@intentius/chant/actor";
55
56
 
56
57
  /** The ops the gated requests of this module wait at, one per kind, as terragucci named them. */
57
58
  export const STATE_OPS = {
@@ -377,7 +378,8 @@ export interface CopyStepInput {
377
378
  lockTimeout?: string | false;
378
379
  }
379
380
 
380
- const actorOf = (env: NodeJS.ProcessEnv): string => env.GITHUB_ACTOR || env.GITLAB_USER_LOGIN || env.USER || env.USERNAME || "unknown";
381
+ /** Who asks when the caller names nobody: core's actor fallback (#4911). */
382
+ const actorOf = (env: NodeJS.ProcessEnv): string => actorFromEnv(env);
381
383
 
382
384
  const dirOf = (cwd: string | undefined, input: { root: string; dir?: string }): string => input.dir ?? join(cwd ?? process.cwd(), input.root);
383
385
 
@@ -2,7 +2,8 @@
2
2
  * Terragrunt units as roots (#3414). `./units`, `./wave` and `./mocks` are pure and
3
3
  * bundle without TypeScript; `./run` spawns `terragrunt`. `./affected` picks
4
4
  * the units a git range touches (#3415). `./capability` is `terragrunt-apply`,
5
- * the units as an op wave's step (#4842).
5
+ * the units as an op wave's step (#4842). `./place` is where a unit's binary runs
6
+ * as Terragrunt prepares it (#4910).
6
7
  */
7
8
 
8
9
  export * from "./units";
@@ -11,3 +12,4 @@ export * from "./mocks";
11
12
  export * from "./run";
12
13
  export * from "./affected";
13
14
  export * from "./capability";
15
+ export * from "./place";
@@ -0,0 +1,42 @@
1
+ /**
2
+ * chant#4910: where a Terragrunt unit's binary runs, as Terragrunt prepares
3
+ * it. The fake Terragrunt runs the `TG_TF_PATH` wrapper itself, in a working
4
+ * directory of its own and with an input as a `TF_VAR_`, as Terragrunt does;
5
+ * the binary the wrapper then runs is `true`. No Terragrunt is installed.
6
+ */
7
+ import { spawnSync } from "node:child_process";
8
+ import { mkdirSync, mkdtempSync, realpathSync } from "node:fs";
9
+ import { tmpdir } from "node:os";
10
+ import { join } from "node:path";
11
+ import { describe, expect, test } from "vitest";
12
+ import { terragruntUnitPlace, UnitPlaceError } from "./place";
13
+ import type { TerragruntExec } from "./run";
14
+
15
+ const tmp = (): string => realpathSync(mkdtempSync(join(tmpdir(), "chant-tg-place-")));
16
+
17
+ describe("terragruntUnitPlace (chant#4910)", () => {
18
+ test("the unit's working directory, and the variables Terragrunt adds, from the wrapper's record", async () => {
19
+ const project = tmp();
20
+ const cache = join(project, ".terragrunt-cache", "abc", "unit");
21
+ mkdirSync(cache, { recursive: true });
22
+ const seen: Array<{ file: string; args: readonly string[]; cwd: string }> = [];
23
+ const exec: TerragruntExec = async (file, args, o) => {
24
+ seen.push({ file, args, cwd: o.cwd });
25
+ const r = spawnSync(o.env.TG_TF_PATH!, ["init"], { cwd: cache, env: { ...o.env, TF_VAR_region: "eu-west-1" }, encoding: "utf-8" });
26
+ return { code: r.status, stdout: r.stdout, stderr: r.stderr };
27
+ };
28
+ const place = await terragruntUnitPlace({ projectDir: project, unit: "live/queues", binary: "true", env: { PATH: process.env.PATH, JOB: "1" }, work: tmp(), exec });
29
+ expect(seen[0]).toMatchObject({ file: "terragrunt", cwd: project });
30
+ expect(seen[0]!.args).toEqual(["run", "--non-interactive", "--no-color", "--working-dir", "live/queues", "--", "init", "-input=false", "-no-color"]);
31
+ expect(place.dir).toBe(cache);
32
+ expect(place.env).toMatchObject({ JOB: "1", TF_VAR_region: "eu-west-1", TG_NON_INTERACTIVE: "true" });
33
+ expect(place.env.TG_TF_PATH).toBeUndefined();
34
+ });
35
+
36
+ test("a failed init, or one that never runs the binary, is refused with why", async () => {
37
+ const failed: TerragruntExec = async () => ({ code: 1, stdout: "", stderr: "ERROR no such unit" });
38
+ await expect(terragruntUnitPlace({ projectDir: tmp(), unit: "u", binary: "tofu", env: {}, work: tmp(), exec: failed })).rejects.toThrow(new UnitPlaceError("terragrunt could not prepare u (init failed): ERROR no such unit"));
39
+ const skipped: TerragruntExec = async () => ({ code: 0, stdout: "", stderr: "" });
40
+ await expect(terragruntUnitPlace({ projectDir: tmp(), unit: "u", binary: "tofu", env: {}, work: tmp(), exec: skipped })).rejects.toThrow(/without running tofu/);
41
+ });
42
+ });
@@ -0,0 +1,75 @@
1
+ /**
2
+ * Where a Terragrunt unit's binary runs (chant#4910): its working directory
3
+ * and environment as Terragrunt prepares them, so a reader or writer of the
4
+ * unit's state (a state migration, an export, an ephemeral environment) runs
5
+ * the binary there as for a plain root.
6
+ *
7
+ * `terragrunt run -- init` in the unit, with `TG_TF_PATH` a wrapper that
8
+ * records the directory and the environment Terragrunt runs the binary in
9
+ * (its working directory, in Terragrunt's cache when the unit names a
10
+ * source, with backend.tf and provider.tf generated, and its inputs as
11
+ * `TF_VAR_` variables) and then runs the binary. The wrapper records with
12
+ * the running Node (`process.execPath`), so it needs nothing on `PATH`.
13
+ *
14
+ * Lifted from terragucci's `unitPlace` (migrate.ts), so terragucci deletes
15
+ * its copy.
16
+ */
17
+ import { chmodSync, existsSync, mkdtempSync, readFileSync, writeFileSync } from "node:fs";
18
+ import { basename, join } from "node:path";
19
+ import { defaultTerragruntExec, type TerragruntExec } from "./run";
20
+
21
+ /** Where a unit's binary runs: its directory, and the job's environment with what Terragrunt adds. */
22
+ export interface UnitPlace {
23
+ dir: string;
24
+ env: NodeJS.ProcessEnv;
25
+ }
26
+
27
+ /** A unit Terragrunt could not prepare. */
28
+ export class UnitPlaceError extends Error {
29
+ constructor(message: string) {
30
+ super(message);
31
+ this.name = "UnitPlaceError";
32
+ }
33
+ }
34
+
35
+ export interface UnitPlaceInput {
36
+ /** The directory Terragrunt runs in; `unit` is relative to it. */
37
+ projectDir: string;
38
+ unit: string;
39
+ /** The binary Terragrunt calls through the wrapper. */
40
+ binary: string;
41
+ env: NodeJS.ProcessEnv;
42
+ /** A directory of the caller's, which the wrapper and its record go under. */
43
+ work: string;
44
+ /** The `terragrunt` executable. Default `terragrunt` on `PATH`. */
45
+ terragrunt?: string;
46
+ /** How Terragrunt runs. Default {@link defaultTerragruntExec}. */
47
+ exec?: TerragruntExec;
48
+ }
49
+
50
+ /** The variables of the wrapper's own run that are not Terragrunt's additions. */
51
+ const SHELL_VARS = new Set(["TG_TF_PATH", "PWD", "OLDPWD", "SHLVL", "_"]);
52
+
53
+ /** The unit's place, as Terragrunt prepares it. Throws {@link UnitPlaceError} when its init fails or never runs the binary. */
54
+ export async function terragruntUnitPlace(input: UnitPlaceInput): Promise<UnitPlace> {
55
+ const { unit, binary, env } = input;
56
+ const capture = mkdtempSync(join(input.work, "unit-"));
57
+ const file = join(capture, "place.json");
58
+ const wrapper = join(capture, basename(binary) || "tofu");
59
+ const q = (x: string): string => `'${x.replace(/'/g, `'\\''`)}'`;
60
+ const record = `require("fs").writeFileSync(process.argv[1], JSON.stringify({ dir: process.cwd(), env: process.env }))`;
61
+ writeFileSync(wrapper, `#!/bin/sh\n${q(process.execPath)} -e ${q(record)} ${q(file)}\nexec ${q(binary)} "$@"\n`);
62
+ chmodSync(wrapper, 0o755);
63
+ const run = await (input.exec ?? defaultTerragruntExec)(input.terragrunt ?? "terragrunt", ["run", "--non-interactive", "--no-color", "--working-dir", unit, "--", "init", "-input=false", "-no-color"], {
64
+ cwd: input.projectDir,
65
+ env: { ...(env as Record<string, string>), TG_TF_PATH: wrapper, TG_NON_INTERACTIVE: "true" },
66
+ });
67
+ if (run.code !== 0) throw new UnitPlaceError(`terragrunt could not prepare ${unit} (init failed): ${lastLines(run.stderr || run.stdout)}`);
68
+ if (!existsSync(file)) throw new UnitPlaceError(`terragrunt prepared ${unit} without running ${binary}, so where it runs is not known`);
69
+ const got = JSON.parse(readFileSync(file, "utf-8")) as { dir: string; env: Record<string, string> };
70
+ // The binary's own variables from Terragrunt: the unit's inputs, and any credentials an iam_role gave it.
71
+ const added = Object.fromEntries(Object.entries(got.env).filter(([k, v]) => env[k] !== v && !SHELL_VARS.has(k)));
72
+ return { dir: got.dir, env: { ...env, ...added } };
73
+ }
74
+
75
+ const lastLines = (s: string, n = 4): string => s.trim().split("\n").slice(-n).join(" ").slice(0, 400);