@intentius/chant-lexicon-terraform 0.124.88 → 0.124.90

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/reach.ts ADDED
@@ -0,0 +1,196 @@
1
+ /**
2
+ * What a change reaches (#4984), beside ./changed-roots.ts: the roots,
3
+ * Terragrunt units, Atmos instances or synthesized roots a change from
4
+ * `from` to `to` reaches, read from git alone. Moved from terragucci's
5
+ * `reachedRoots`, `reachedUnits`, `reachedInstances` and
6
+ * `reachedSynthesized` (comment-apply.ts), for a product's reach line
7
+ * (`productPlan.reach`, #4979): the lock job and the decision before merge
8
+ * run it at the target branch's commit, so none of the change's code runs.
9
+ *
10
+ * Each follows what reads a reached target: a root that reads a reached
11
+ * root's state, a unit that names a reached unit in a `dependency` or
12
+ * `dependencies` block, an instance that depends on a reached one, followed
13
+ * through. The product hands the reads it knows (terragucci's
14
+ * `terraform_remote_state` edges and `waves.after`, Atmos's dependencies);
15
+ * the Terragrunt unit reads are parsed here, at both ends of the range.
16
+ * Markdown files reach nothing. When a change can reach anything, or git
17
+ * cannot diff the range, every target is reached and `every` says why.
18
+ */
19
+
20
+ import { posix } from "node:path";
21
+ import { changedRoots } from "./changed-roots";
22
+
23
+ /** Git in a checkout: an argv after `git`, and what it answered. */
24
+ export type ReachGit = (args: string[]) => { status: number | null; stdout: string; stderr: string };
25
+
26
+ /** What a change reaches, sorted, and why it is every target when it is. */
27
+ export interface Reach {
28
+ targets: string[];
29
+ every?: string;
30
+ }
31
+
32
+ /** The targets each target reads, so a change to one of those reaches it too. */
33
+ export type ReachReads = ReadonlyMap<string, ReadonlySet<string>>;
34
+
35
+ /** Changed files that reach nothing: documentation. */
36
+ export const READS_NOTHING = /\.md$/i;
37
+
38
+ /** The files the change from `from` to `to` changes (`git diff --name-only from...to`), or undefined when git cannot say. */
39
+ export function changedFilesBetween(git: ReachGit, from: string, to: string): string[] | undefined {
40
+ const diff = git(["diff", "--name-only", "--no-renames", `${from}...${to}`]);
41
+ if (diff.status !== 0) return undefined;
42
+ return diff.stdout.split("\n").map((l) => l.trim()).filter(Boolean);
43
+ }
44
+
45
+ /** Add to `selected` every target that reads a selected one, followed through. */
46
+ export function followReads(selected: Set<string>, reads: ReachReads | undefined): void {
47
+ if (!reads) return;
48
+ for (let grew = true; grew; ) {
49
+ grew = false;
50
+ for (const [target, from] of reads) {
51
+ if (!selected.has(target) && [...from].some((d) => selected.has(d))) {
52
+ selected.add(target);
53
+ grew = true;
54
+ }
55
+ }
56
+ }
57
+ }
58
+
59
+ const sorted = (xs: Iterable<string>): string[] => [...new Set(xs)].sort();
60
+
61
+ /**
62
+ * The plain roots a change reaches: the roots whose files it changes, by
63
+ * chant's path rules (./changed-roots.ts: a root's directory, the local
64
+ * modules it calls, its var files), and every root that reads one of those.
65
+ * Every root when git cannot diff the range.
66
+ */
67
+ export function reachedRoots(repo: string, git: ReachGit, from: string, to: string, roots: readonly string[], reads?: ReachReads): Reach {
68
+ const all = sorted(roots);
69
+ const files = changedFilesBetween(git, from, to);
70
+ if (files === undefined) return { targets: all, every: "git could not list the files it changes" };
71
+ const selected = new Set(changedRoots(repo, Object.fromEntries(all.map((r) => [r, { dir: r }])), files));
72
+ followReads(selected, reads);
73
+ return { targets: [...selected].filter((r) => all.includes(r)).sort() };
74
+ }
75
+
76
+ /** The body of each top-level `<keyword> ... {` block in HCL text, braces matched, strings skipped. */
77
+ function hclBlocks(text: string, keyword: string): string[] {
78
+ const out: string[] = [];
79
+ const head = new RegExp(`^\\s*${keyword}\\b[^{\\n]*\\{`, "gm");
80
+ for (let m = head.exec(text); m; m = head.exec(text)) {
81
+ let depth = 1;
82
+ let i = m.index + m[0].length;
83
+ const start = i;
84
+ for (let quoted = false; i < text.length && depth > 0; i++) {
85
+ const c = text[i];
86
+ if (quoted) {
87
+ if (c === "\\") i++;
88
+ else if (c === '"') quoted = false;
89
+ } else if (c === '"') quoted = true;
90
+ else if (c === "{") depth++;
91
+ else if (c === "}") depth--;
92
+ }
93
+ out.push(text.slice(start, i - 1));
94
+ head.lastIndex = i;
95
+ }
96
+ return out;
97
+ }
98
+
99
+ /**
100
+ * The paths a unit's `terragrunt.hcl` names in its `dependency` blocks'
101
+ * `config_path` and its `dependencies` block's `paths`, when each is a plain
102
+ * string. A path built with a function or an interpolation is not read:
103
+ * only Terragrunt can evaluate it, and no Terragrunt runs on a change's
104
+ * files before a review.
105
+ */
106
+ export function literalTerragruntDependencies(text: string): string[] {
107
+ const clean = text.replace(/(^|[^:"$])(#|\/\/).*$/gm, "$1");
108
+ const plain = /^"([^"$]*)"$/;
109
+ const out: string[] = [];
110
+ for (const b of hclBlocks(clean, "dependency")) {
111
+ const m = /^\s*config_path\s*=\s*("[^"\n]*")\s*$/m.exec(b);
112
+ const p = m && plain.exec(m[1]!);
113
+ if (p) out.push(p[1]!);
114
+ }
115
+ for (const b of hclBlocks(clean, "dependencies")) {
116
+ const m = /\bpaths\s*=\s*\[([^\]]*)\]/.exec(b);
117
+ for (const item of m ? m[1]!.split(",").map((x) => x.trim()).filter(Boolean) : []) {
118
+ const p = plain.exec(item);
119
+ if (p) out.push(p[1]!);
120
+ }
121
+ }
122
+ return out;
123
+ }
124
+
125
+ /**
126
+ * The Terragrunt units a change reaches. A unit is reached when a file
127
+ * under its directory changes (the deepest unit holding the file), and so
128
+ * is every unit that names a reached one with a plain path, at either end
129
+ * of the range. A changed file under no unit's directory (`root.hcl`, a
130
+ * module, a stack template) may change any unit, so it reaches every one.
131
+ */
132
+ export function reachedUnits(git: ReachGit, from: string, to: string, units: readonly string[]): Reach {
133
+ const all = sorted(units);
134
+ const files0 = changedFilesBetween(git, from, to);
135
+ if (files0 === undefined) return { targets: all, every: "git could not list the files it changes" };
136
+ const files = files0.filter((f) => !READS_NOTHING.test(f));
137
+ const deepest = [...all].sort((a, b) => b.length - a.length);
138
+ const selected = new Set<string>();
139
+ for (const f of files) {
140
+ const u = deepest.find((d) => f.startsWith(`${d}/`));
141
+ if (!u) return { targets: all, every: `it changes \`${f}\`, which is in no unit's directory` };
142
+ selected.add(u);
143
+ }
144
+ const reads = new Map<string, Set<string>>();
145
+ for (const u of all) {
146
+ const deps = new Set<string>();
147
+ for (const ref of [from, to]) {
148
+ const shown = git(["show", `${ref}:${u}/terragrunt.hcl`]);
149
+ if (shown.status !== 0) continue;
150
+ for (const p of literalTerragruntDependencies(shown.stdout)) deps.add(posix.normalize(posix.join(u, p)).replace(/\/+$/, ""));
151
+ }
152
+ reads.set(u, deps);
153
+ }
154
+ followReads(selected, reads);
155
+ return { targets: [...selected].sort() };
156
+ }
157
+
158
+ /**
159
+ * The Atmos instances a change reaches. `componentOf` names each instance's
160
+ * component directory. A changed file under one reaches that component's
161
+ * instances; any other file (a stack manifest, a catalog, `atmos.yaml`) can
162
+ * change any instance's vars or backend, so it reaches every instance.
163
+ * Instances that read a reached one follow.
164
+ */
165
+ export function reachedInstances(git: ReachGit, from: string, to: string, instances: readonly string[], componentOf: ReadonlyMap<string, string | undefined>, reads?: ReachReads): Reach {
166
+ const all = sorted(instances);
167
+ const files0 = changedFilesBetween(git, from, to);
168
+ if (files0 === undefined) return { targets: all, every: "git could not list the files it changes" };
169
+ const files = files0.filter((f) => !READS_NOTHING.test(f));
170
+ const selected = new Set<string>();
171
+ for (const f of files) {
172
+ const owners = all.filter((r) => {
173
+ const c = componentOf.get(r)?.replace(/\/+$/, "");
174
+ return c !== undefined && f.startsWith(`${c}/`);
175
+ });
176
+ if (owners.length === 0) return { targets: all, every: `it changes \`${f}\`, which can change any Atmos instance` };
177
+ for (const r of owners) selected.add(r);
178
+ }
179
+ followReads(selected, reads);
180
+ return { targets: [...selected].sort() };
181
+ }
182
+
183
+ /**
184
+ * The roots a change reaches when a command writes them (`synth`, CDK
185
+ * Terrain's stacks). The roots are not in git, so no diff names them, and
186
+ * the command is the change's own code: any changed file that is not
187
+ * Markdown can change any root.
188
+ */
189
+ export function reachedSynthesized(git: ReachGit, from: string, to: string, roots: readonly string[]): Reach {
190
+ const all = sorted(roots);
191
+ const files = changedFilesBetween(git, from, to);
192
+ if (files === undefined) return { targets: all, every: "git could not list the files it changes" };
193
+ const file = files.find((f) => !READS_NOTHING.test(f));
194
+ if (file === undefined) return { targets: [] };
195
+ return { targets: all, every: `it changes \`${file}\`, and the synth command writes every root from the repo's files` };
196
+ }
@@ -7,9 +7,10 @@
7
7
  */
8
8
 
9
9
  import { execFile } from "node:child_process";
10
- import { existsSync, mkdirSync, readFileSync, realpathSync, rmSync, writeFileSync } from "node:fs";
11
- import { dirname, join, resolve } from "node:path";
10
+ import { chmodSync, existsSync, mkdirSync, readFileSync, realpathSync, rmSync, writeFileSync } from "node:fs";
11
+ import { basename, dirname, join, resolve } from "node:path";
12
12
  import type { ChangeSetPart, ChangeSetPlanner } from "@intentius/chant/change-set";
13
+ import type { TerraformCommand, TerraformCommandHook, TerraformCommandRun } from "../op/command-hook";
13
14
  import { plannerForBinary } from "../change-set";
14
15
  import {
15
16
  parseTerragruntFind,
@@ -42,6 +43,7 @@ import {
42
43
  terragruntUnitResults,
43
44
  terragruntWaveArgs,
44
45
  terragruntWaveParts,
46
+ type TerragruntReportRow,
45
47
  type TerragruntUnitResult,
46
48
  } from "./wave";
47
49
 
@@ -180,6 +182,119 @@ export interface TerragruntWaveRunInput extends TerragruntRunOptions {
180
182
  * the plan, which fails the unit with its report row as before.
181
183
  */
182
184
  initTurn?: <T>(fn: () => Promise<T>) => Promise<T>;
185
+ /**
186
+ * The product's hook around each unit's plan (chant#4954), the
187
+ * `TerraformCommandHook` `terraform-apply` takes as `onCommand`, such as
188
+ * `engineTimings().onCommand`. Terragrunt runs the binary itself, so the
189
+ * hook is asked for every unit of the wave before `run --all`, as a `plan`
190
+ * command for that unit (`root` is the unit's path, `method` is `plan`).
191
+ * The variables it answers reach that unit's plan alone, through a
192
+ * `TG_TF_PATH` wrapper that matches the `-out` path Terragrunt gives it.
193
+ * Once the wave ends, each unit's `after` gets its time (`at`) and result
194
+ * from the run report. Needs `binary`: without it Terragrunt picks the
195
+ * binary per unit, and the hook is not called.
196
+ */
197
+ onCommand?: TerraformCommandHook;
198
+ }
199
+
200
+ /** A word single-quoted for `sh`. */
201
+ const shq = (s: string): string => `'${s.replace(/'/g, `'\\''`)}'`;
202
+
203
+ /**
204
+ * The `TG_TF_PATH` wrapper for one wave (chant#4954): a shell script that
205
+ * runs `binary` with the variables of the unit whose plan it runs.
206
+ * Terragrunt hands each unit's plan `-out=<out-dir>/<unit>/tfplan.tfplan`,
207
+ * which names the unit; every other call (init, show, output, version) runs
208
+ * the binary as it is. `outDirs` are the wave's plan directory as given and
209
+ * as its real path. Lifted from terragucci's `unitWrapper` (report/stage.ts).
210
+ */
211
+ export function terragruntUnitWrapper(binary: string, outDirs: readonly string[], units: ReadonlyMap<string, Record<string, string>>): string {
212
+ const lines = [
213
+ "#!/bin/sh",
214
+ "# chant: each unit's plan runs with the variables its command hook answered. Written for one wave.",
215
+ 'out=""',
216
+ 'prev=""',
217
+ 'for a in "$@"; do',
218
+ ' case "$a" in -out=*|--out=*) out="${a#*=}" ;; esac',
219
+ ' if [ "$prev" = "-out" ] || [ "$prev" = "--out" ]; then out="$a"; fi',
220
+ ' prev="$a"',
221
+ "done",
222
+ 'case "$out" in',
223
+ ];
224
+ for (const [unit, vars] of units) {
225
+ const set = Object.entries(vars).filter(([k]) => /^[A-Za-z_][A-Za-z0-9_]*$/.test(k));
226
+ if (set.length === 0) continue;
227
+ const patterns = [...new Set(outDirs.map((d) => join(d, unit, "tfplan.tfplan")))].map(shq).join(" | ");
228
+ lines.push(` ${patterns}) export ${set.map(([k, v]) => `${k}=${shq(v)}`).join(" ")} ;;`);
229
+ }
230
+ lines.push("esac", `exec ${shq(binary)} "$@"`);
231
+ return lines.join("\n") + "\n";
232
+ }
233
+
234
+ /**
235
+ * Ask `hook` for each unit's plan before the wave runs (chant#4954), and
236
+ * write the wrapper that hands each its variables when any has some.
237
+ * Answers the variables the run adds and what to call once the run report
238
+ * is read.
239
+ */
240
+ async function unitCommandRuns(
241
+ hook: TerraformCommandHook,
242
+ input: TerragruntWaveRunInput & { binary: string },
243
+ work: string,
244
+ outDir: string,
245
+ waveArgs: readonly string[],
246
+ ): Promise<{ env: Record<string, string>; done: (report: Map<string, TerragruntReportRow>) => Promise<void> }> {
247
+ const units = [...new Set(input.units)].sort();
248
+ const args = waveArgs.slice(waveArgs.indexOf("--") + 1);
249
+ const runs = new Map<string, TerraformCommandRun>();
250
+ for (const unit of units) {
251
+ const command: TerraformCommand = {
252
+ root: unit,
253
+ method: "plan",
254
+ binary: input.binary,
255
+ name: "plan",
256
+ args: [...args],
257
+ line: [input.binary, ...args].map((w) => (/^[\w@%+=:,./-]+$/.test(w) ? w : shq(w))).join(" "),
258
+ cwd: resolve(input.dir, unit),
259
+ env: {},
260
+ };
261
+ runs.set(unit, (await hook(command)) ?? {});
262
+ }
263
+ const vars = new Map([...runs].map(([unit, run]) => [unit, run.env ?? {}] as const));
264
+ let env: Record<string, string> = {};
265
+ if ([...vars.values()].some((v) => Object.keys(v).length > 0)) {
266
+ let real = outDir;
267
+ try {
268
+ real = join(realpathSync(work), "plans");
269
+ } catch {
270
+ // The path as given is matched alone.
271
+ }
272
+ const bin = join(work, "unit-bin");
273
+ mkdirSync(bin, { recursive: true });
274
+ // The wrapper carries the binary's name, which Terragrunt prefixes its log lines with.
275
+ const wrapper = join(bin, basename(input.binary) || "tofu");
276
+ writeFileSync(wrapper, terragruntUnitWrapper(input.binary, [outDir, real], vars));
277
+ chmodSync(wrapper, 0o755);
278
+ env = { TG_TF_PATH: wrapper };
279
+ }
280
+ const done = async (report: Map<string, TerragruntReportRow>): Promise<void> => {
281
+ for (const [unit, run] of runs) {
282
+ if (!run.after) continue;
283
+ const row = report.get(unit);
284
+ const start = row?.started ? Date.parse(row.started) : NaN;
285
+ const end = row?.ended ? Date.parse(row.ended) : NaN;
286
+ const timed = Number.isFinite(start) && Number.isFinite(end) && end >= start;
287
+ const why = row && row.result !== "succeeded" ? [row.result, row.reason, row.cause].filter(Boolean).join(": ") : "";
288
+ await run.after({
289
+ code: row ? (row.result === "succeeded" ? 0 : 1) : null,
290
+ seconds: timed ? (end - start) / 1000 : 0,
291
+ stdout: "",
292
+ stderr: row ? why : "not in Terragrunt's run report, so it did not run",
293
+ ...(timed ? { at: { start, end } } : {}),
294
+ });
295
+ }
296
+ };
297
+ return { env, done };
183
298
  }
184
299
 
185
300
  /**
@@ -345,8 +460,17 @@ export async function planTerragruntWave(input: TerragruntWaveRunInput): Promise
345
460
  ...(input.destroy ? { destroy: true } : {}),
346
461
  ...(input.parallelism !== undefined ? { parallelism: input.parallelism } : {}),
347
462
  });
348
- const r = await exec(tg, args, { cwd: input.dir, env: terragruntEnv(input.binary) });
349
- const report = existsSync(reportFile) ? parseTerragruntReport(readFileSync(reportFile, "utf8")) : new Map();
463
+ // chant#4954: each unit's plan goes through the product's hook, its variables through a TG_TF_PATH wrapper.
464
+ const units = input.onCommand && input.binary ? await unitCommandRuns(input.onCommand, { ...input, binary: input.binary }, work, outDir, args) : undefined;
465
+ let r: ExecResult;
466
+ try {
467
+ r = await exec(tg, args, { cwd: input.dir, env: { ...terragruntEnv(input.binary), ...(units?.env ?? {}) } });
468
+ } catch (err) {
469
+ await units?.done(new Map());
470
+ throw err;
471
+ }
472
+ const report = existsSync(reportFile) ? parseTerragruntReport(readFileSync(reportFile, "utf8")) : new Map<string, TerragruntReportRow>();
473
+ await units?.done(report);
350
474
  const planFor = (unit: string): unknown => {
351
475
  const f = join(jsonOutDir, unit, "tfplan.json");
352
476
  return existsSync(f) ? (JSON.parse(readFileSync(f, "utf8")) as unknown) : undefined;