@intentius/chant-lexicon-terraform 0.124.77 → 0.124.79

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 (43) hide show
  1. package/dist/change-set.d.ts +1 -0
  2. package/dist/change-set.d.ts.map +1 -1
  3. package/dist/change-set.js +19 -0
  4. package/dist/components/capability-plugin.d.ts +9 -1
  5. package/dist/components/capability-plugin.d.ts.map +1 -1
  6. package/dist/components/capability-plugin.js +44 -22
  7. package/dist/drift-record.d.ts +24 -0
  8. package/dist/drift-record.d.ts.map +1 -1
  9. package/dist/drift-record.js +52 -0
  10. package/dist/drift-rows.d.ts +49 -4
  11. package/dist/drift-rows.d.ts.map +1 -1
  12. package/dist/drift-rows.js +87 -19
  13. package/dist/integrity.json +2 -2
  14. package/dist/manifest.json +1 -1
  15. package/dist/op/activities/terraform.d.ts +11 -0
  16. package/dist/op/activities/terraform.d.ts.map +1 -1
  17. package/dist/op/activities/terraform.js +9 -1
  18. package/dist/terragrunt/capability.d.ts +129 -0
  19. package/dist/terragrunt/capability.d.ts.map +1 -0
  20. package/dist/terragrunt/capability.js +347 -0
  21. package/dist/terragrunt/index.d.ts +3 -1
  22. package/dist/terragrunt/index.d.ts.map +1 -1
  23. package/dist/terragrunt/index.js +3 -1
  24. package/dist/terragrunt/units.d.ts +9 -0
  25. package/dist/terragrunt/units.d.ts.map +1 -1
  26. package/dist/terragrunt/units.js +25 -0
  27. package/package.json +2 -2
  28. package/src/__fixtures__/drift-rows/refresh-plan-module.json +48 -0
  29. package/src/change-set.test.ts +27 -1
  30. package/src/change-set.ts +19 -0
  31. package/src/components/capability-plugin.test.ts +61 -6
  32. package/src/components/capability-plugin.ts +48 -23
  33. package/src/components/compose-op-wave.test.ts +134 -0
  34. package/src/drift-record.test.ts +62 -0
  35. package/src/drift-record.ts +58 -0
  36. package/src/drift-rows.test.ts +90 -1
  37. package/src/drift-rows.ts +89 -18
  38. package/src/op/activities/terraform.test.ts +21 -0
  39. package/src/op/activities/terraform.ts +20 -1
  40. package/src/terragrunt/capability.test.ts +327 -0
  41. package/src/terragrunt/capability.ts +431 -0
  42. package/src/terragrunt/index.ts +3 -1
  43. package/src/terragrunt/units.ts +25 -0
@@ -0,0 +1,327 @@
1
+ /**
2
+ * `terragrunt-apply` (#4842): a Terragrunt repo's units as an op wave's
3
+ * step, found at plan time and run with one `run --all`, against output
4
+ * recorded from Terragrunt 1.1.6 (`../__fixtures__/terragrunt/`). Nothing
5
+ * here runs Terragrunt.
6
+ */
7
+
8
+ import { afterAll, describe, expect, it } from "vitest";
9
+ import { mkdirSync, mkdtempSync, readFileSync, rmSync, writeFileSync } from "node:fs";
10
+ import { tmpdir } from "node:os";
11
+ import { dirname, join } from "node:path";
12
+ import { fileURLToPath } from "node:url";
13
+ import { CapabilityRegistry } from "@intentius/chant/components/capability";
14
+ import { memoryGateLedgerPort, runOpWave, type OpWavesSpec } from "@intentius/chant/op";
15
+ import { computePlanDigest } from "@intentius/chant/lifecycle/plan-digest";
16
+ import {
17
+ parseTerragruntFind,
18
+ terragruntApplyCapability,
19
+ terragruntListedWaves,
20
+ terragruntUnitPinResolver,
21
+ terragruntUnitPins,
22
+ TerragruntWaveError,
23
+ type TerragruntApplyDeps,
24
+ type TerragruntExec,
25
+ } from "./index";
26
+ import { terraformCapabilityPlugin } from "../components/capability-plugin";
27
+
28
+ const recorded = join(dirname(fileURLToPath(import.meta.url)), "../__fixtures__/terragrunt/recorded");
29
+ const read = (p: string): string => readFileSync(join(recorded, p), "utf8");
30
+ const units = parseTerragruntFind(read("find.json"));
31
+
32
+ const tmps: string[] = [];
33
+ afterAll(() => {
34
+ for (const t of tmps) rmSync(t, { recursive: true, force: true });
35
+ });
36
+ const tmp = (): string => {
37
+ const t = mkdtempSync(join(tmpdir(), "chant-tg-cap-"));
38
+ tmps.push(t);
39
+ return t;
40
+ };
41
+
42
+ interface Call {
43
+ file: string;
44
+ args: string[];
45
+ env: Record<string, string>;
46
+ }
47
+
48
+ /** Terragrunt as recorded: find, render with no dependency, and wave 1's plan and apply (`failPlan` swaps in a report with a failed unit). */
49
+ const stub = (calls: Call[], opts: { failPlan?: boolean; planJson?: (unit: string) => string | undefined } = {}): TerragruntExec => async (f, args, o) => {
50
+ calls.push({ file: f, args: [...args], env: o.env });
51
+ if (args[0] === "find") return { code: 0, stdout: read("find.json"), stderr: "" };
52
+ if (args[0] === "render") return { code: 0, stdout: '{"dependency":null}', stderr: "" };
53
+ const at = (flag: string): string | undefined => (args.includes(flag) ? args[args.indexOf(flag) + 1] : undefined);
54
+ const report = at("--report-file")!;
55
+ mkdirSync(dirname(report), { recursive: true });
56
+ const cmd = args[args.indexOf("--") + 1];
57
+ const filtered = args.filter((_, i) => args[i - 1] === "--filter").map((f) => f.replace(/^\{\.\//, "").replace(/\}$/, ""));
58
+ if (cmd === "plan" && opts.failPlan) {
59
+ writeFileSync(report, JSON.stringify([{ Name: filtered[0], Result: "failed", Reason: "run error", Cause: "exit status 1" }]));
60
+ return { code: 1, stdout: "", stderr: "" };
61
+ }
62
+ writeFileSync(report, read(cmd === "plan" ? "wave-1/plan-report.json" : "wave-1/apply-report.json"));
63
+ if (cmd === "plan") {
64
+ for (const u of filtered) {
65
+ for (const [dir, file, text] of [
66
+ [at("--json-out-dir")!, "tfplan.json", opts.planJson?.(u) ?? read(`wave-1/json/${u}/tfplan.json`)],
67
+ [at("--out-dir")!, "tfplan.tfplan", "binary plan"],
68
+ ] as const) {
69
+ mkdirSync(join(dir, u), { recursive: true });
70
+ writeFileSync(join(dir, u, file), text);
71
+ }
72
+ }
73
+ }
74
+ return { code: 0, stdout: "", stderr: "" };
75
+ };
76
+
77
+ const runAll = (calls: Call[]) => calls.filter((c) => c.args[0] === "run").map((c) => [c.args[c.args.indexOf("--") + 1], ...c.args.filter((_, i) => c.args[i - 1] === "--filter")]);
78
+
79
+ function spec(dir: string, together = true): OpWavesSpec {
80
+ return {
81
+ name: "tf-apply",
82
+ op: "tf-apply",
83
+ gateName: "wave-{k}",
84
+ step: { kind: "terragrunt-apply", unit: "{target}", dir },
85
+ waves: [
86
+ { name: "layer-1", runs: [], discover: true, ...(together ? { together: true } : {}), gate: "never" },
87
+ { name: "layer-2", runs: [], discover: true, ...(together ? { together: true } : {}), gate: "never" },
88
+ ],
89
+ };
90
+ }
91
+
92
+ const wave = (s: OpWavesSpec, cwd: string, exec: TerragruntExec, runHooks?: Parameters<typeof runOpWave>[0]["runHooks"], deps: Omit<TerragruntApplyDeps, "exec"> = {}) =>
93
+ runOpWave({
94
+ spec: s,
95
+ specFile: "waves.json",
96
+ wave: 1,
97
+ cwd,
98
+ show: () => ({ sha: "b".repeat(40), text: JSON.stringify(s) }),
99
+ gates: memoryGateLedgerPort(),
100
+ capabilities: new CapabilityRegistry().register(terragruntApplyCapability({ exec, ...deps })),
101
+ log: () => {},
102
+ ...(runHooks ? { runHooks } : {}),
103
+ });
104
+
105
+ describe("terragrunt-apply finds a wave's units", () => {
106
+ it("answers wave k of the graph's layers, and how many there are", async () => {
107
+ const calls: Call[] = [];
108
+ const cap = terragruntApplyCapability({ exec: stub(calls) });
109
+ const dir = tmp();
110
+ expect(await cap.discoverRuns!({ env: "prod", wave: { name: "layer", number: 1 } }, { unit: "{target}", dir })).toEqual({ targets: ["live/dev/vpc", "live/prod/vpc"], waves: 3 });
111
+ expect(await cap.discoverRuns!({ env: "prod", wave: { name: "layer", number: 4 } }, { unit: "{target}", dir })).toEqual({ targets: [], waves: 3 });
112
+ expect(calls[0]!.args.slice(0, 2)).toEqual(["find", "--json"]);
113
+ });
114
+
115
+ it("cuts the waves a pipeline lists again by the graph, and refuses a list that is stale", () => {
116
+ expect(terragruntListedWaves([["live/dev/vpc", "live/dev/db", "live/dev/app"], ["live/prod/vpc", "live/prod/app"]], units)).toEqual([
117
+ ["live/dev/vpc"],
118
+ ["live/dev/db"],
119
+ ["live/dev/app"],
120
+ ["live/prod/vpc"],
121
+ ["live/prod/app"],
122
+ ]);
123
+ expect(() => terragruntListedWaves([["live/dev/db"], ["live/dev/vpc"]], units)).toThrow(TerragruntWaveError);
124
+ expect(() => terragruntListedWaves([["live/dev/db"], ["live/dev/vpc"]], units)).toThrow("live/dev/db depends on live/dev/vpc, which the pipeline applies in a later wave");
125
+ });
126
+
127
+ it("is one of the terraform lexicon's capabilities", () => {
128
+ expect(terraformCapabilityPlugin.capabilities().map((c) => c.kind)).toContain("terragrunt-apply");
129
+ });
130
+ });
131
+
132
+ describe("an op wave on terragrunt-apply (#4842)", () => {
133
+ it("a together wave plans its units with one run --all and applies those saved plans with one", async () => {
134
+ const dir = tmp();
135
+ const calls: Call[] = [];
136
+ const s = spec(dir);
137
+ const result = await wave(s, dir, stub(calls));
138
+ expect(result.exitCode).toBe(0);
139
+ expect(runAll(calls)).toEqual([
140
+ ["plan", "{./live/dev/vpc}", "{./live/prod/vpc}"],
141
+ ["apply", "{./live/dev/vpc}", "{./live/prod/vpc}"],
142
+ ]);
143
+ // The apply reads the plans the plan saved.
144
+ const outDirs = calls.filter((c) => c.args[0] === "run").map((c) => c.args[c.args.indexOf("--out-dir") + 1]);
145
+ expect(outDirs[0]).toBe(outDirs[1]);
146
+ expect(result.applied).toEqual(["live/dev/vpc", "live/prod/vpc"]);
147
+ expect(result.decision?.discovered).toEqual({ waves: 3 });
148
+ expect(result.decision?.members.map((m) => m.member)).toEqual(["live/dev/vpc", "live/prod/vpc"]);
149
+ // Each unit's plan file names its plan document, for a cost hold and the after-plan hooks.
150
+ const file = JSON.parse(readFileSync(join(dir, ".chant/op-waves/tf-apply/wave-1/live_dev_vpc.plan.json"), "utf-8")) as { artifact: { planJson: string } };
151
+ expect(JSON.parse(readFileSync(file.artifact.planJson, "utf-8"))).toEqual(JSON.parse(read("wave-1/json/live/dev/vpc/tfplan.json")));
152
+ });
153
+
154
+ it("a wave planned run by run plans and applies each unit alone", async () => {
155
+ const dir = tmp();
156
+ const calls: Call[] = [];
157
+ const result = await wave(spec(dir, false), dir, stub(calls));
158
+ expect(result.exitCode).toBe(0);
159
+ expect(runAll(calls)).toEqual([
160
+ ["plan", "{./live/dev/vpc}"],
161
+ ["plan", "{./live/prod/vpc}"],
162
+ ["apply", "{./live/dev/vpc}"],
163
+ ["apply", "{./live/prod/vpc}"],
164
+ ]);
165
+ });
166
+
167
+ it("a run hook's environment reaches every Terragrunt call of the wave, and a check runs before the plan", async () => {
168
+ const dir = tmp();
169
+ const calls: Call[] = [];
170
+ const checked: string[] = [];
171
+ const result = await wave(spec(dir), dir, stub(calls), {
172
+ name: "role",
173
+ beforePlan: (r) => ({ env: { AWS_ROLE_ARN: "arn:aws:iam::1:role/apply" }, afterInit: async () => void checked.push(r.target) }),
174
+ });
175
+ expect(result.exitCode).toBe(0);
176
+ expect(checked).toEqual(["live/dev/vpc", "live/prod/vpc"]);
177
+ for (const c of calls.filter((x) => x.args[0] === "run")) expect(c.env.AWS_ROLE_ARN).toBe("arn:aws:iam::1:role/apply");
178
+ });
179
+
180
+ it("units the run hooks give different environments cannot run as one run --all", async () => {
181
+ const dir = tmp();
182
+ const calls: Call[] = [];
183
+ await expect(wave(spec(dir), dir, stub(calls), { name: "role", beforePlan: (r) => ({ env: { AWS_ROLE_ARN: `arn:${r.target}` } }) })).rejects.toThrow(/one environment, and the run hooks gave them different ones/);
184
+ expect(runAll(calls)).toEqual([]);
185
+ });
186
+
187
+ it("a unit that does not plan fails the wave, and nothing applies", async () => {
188
+ const dir = tmp();
189
+ const calls: Call[] = [];
190
+ await expect(wave(spec(dir), dir, stub(calls, { failPlan: true }))).rejects.toThrow(/terragrunt-apply: live\/dev\/vpc: failed \(run error: exit status 1\)/);
191
+ expect(runAll(calls).map((c) => c[0])).toEqual(["plan"]);
192
+ });
193
+ });
194
+
195
+ describe("units that pin their own releases (#4849)", () => {
196
+ /** Where each Terragrunt call of the wave ran: the command, its units, the Terragrunt and TG_TF_PATH, and the out dir. */
197
+ const runs = (calls: Call[]) =>
198
+ calls.filter((c) => c.args[0] === "run").map((c) => ({
199
+ cmd: c.args[c.args.indexOf("--") + 1],
200
+ units: c.args.filter((_, i) => c.args[i - 1] === "--filter"),
201
+ terragrunt: c.file,
202
+ binary: c.env.TG_TF_PATH,
203
+ outDir: c.args[c.args.indexOf("--out-dir") + 1],
204
+ }));
205
+ const prodPinned: TerragruntApplyDeps["resolveUnit"] = (unit) => (unit === "live/prod/vpc" ? { terragrunt: "/opt/tg-1.1.5/terragrunt", binary: "/opt/tofu-1.9.0/tofu" } : {});
206
+
207
+ it("a together wave plans and applies one run --all per pair of releases, each with its own binaries and saved plans", async () => {
208
+ const dir = tmp();
209
+ const calls: Call[] = [];
210
+ const s = { ...spec(dir), step: { ...spec(dir).step!, terragrunt: "/opt/tg/terragrunt", binary: "/opt/tofu/tofu" } };
211
+ const result = await wave(s, dir, stub(calls), undefined, { resolveUnit: prodPinned });
212
+ expect(result.exitCode).toBe(0);
213
+ const r = runs(calls);
214
+ expect(r.map(({ cmd, units, terragrunt, binary }) => ({ cmd, units, terragrunt, binary }))).toEqual([
215
+ { cmd: "plan", units: ["{./live/dev/vpc}"], terragrunt: "/opt/tg/terragrunt", binary: "/opt/tofu/tofu" },
216
+ { cmd: "plan", units: ["{./live/prod/vpc}"], terragrunt: "/opt/tg-1.1.5/terragrunt", binary: "/opt/tofu-1.9.0/tofu" },
217
+ { cmd: "apply", units: ["{./live/dev/vpc}"], terragrunt: "/opt/tg/terragrunt", binary: "/opt/tofu/tofu" },
218
+ { cmd: "apply", units: ["{./live/prod/vpc}"], terragrunt: "/opt/tg-1.1.5/terragrunt", binary: "/opt/tofu-1.9.0/tofu" },
219
+ ]);
220
+ // Each group applies the plans it saved, in a directory of its own.
221
+ expect(r[2]!.outDir).toBe(r[0]!.outDir);
222
+ expect(r[3]!.outDir).toBe(r[1]!.outDir);
223
+ expect(r[0]!.outDir).not.toBe(r[1]!.outDir);
224
+ expect(result.applied).toEqual(["live/dev/vpc", "live/prod/vpc"]);
225
+ // Each unit keeps its own member digest, beside the wave plan's.
226
+ const members = result.decision!.members.map((m) => m.member);
227
+ expect(members).toEqual(["live/dev/vpc", "live/prod/vpc"]);
228
+ for (const unit of ["live/dev/vpc", "live/prod/vpc"]) {
229
+ const file = JSON.parse(readFileSync(join(dir, `.chant/op-waves/tf-apply/wave-1/${unit.replaceAll("/", "_")}.plan.json`), "utf-8")) as { artifact: { planJson: string } };
230
+ expect(JSON.parse(readFileSync(file.artifact.planJson, "utf-8"))).toEqual(JSON.parse(read(`wave-1/json/${unit}/tfplan.json`)));
231
+ }
232
+ });
233
+
234
+ it("a wave whose units all run the step's releases plans as one run --all, with the same wave plan digest", async () => {
235
+ const cap = terragruntApplyCapability({ exec: stub([]), resolveUnit: () => ({}) });
236
+ const dir = tmp();
237
+ const members = ["live/dev/vpc", "live/prod/vpc"].map((unit) => ({ component: unit, step: { unit, dir }, input: { unit, dir }, ctx: { env: "prod" } }));
238
+ const calls: Call[] = [];
239
+ const planned = await terragruntApplyCapability({ exec: stub(calls), resolveUnit: () => ({}) }).planGroup!({ env: "prod" } as never, members as never);
240
+ expect(runs(calls).map((c) => c.cmd)).toEqual(["plan"]);
241
+ expect(planned.digest).toBe(computePlanDigest("terragrunt-apply-wave", { units: ["live/dev/vpc", "live/prod/vpc"], destroy: false }));
242
+ expect(cap.kind).toBe("terragrunt-apply");
243
+ });
244
+
245
+ it("a wave planned run by run plans and applies each unit with its own releases", async () => {
246
+ const dir = tmp();
247
+ const calls: Call[] = [];
248
+ const result = await wave(spec(dir, false), dir, stub(calls), undefined, { resolveUnit: prodPinned });
249
+ expect(result.exitCode).toBe(0);
250
+ expect(runs(calls).map(({ cmd, terragrunt, binary }) => [cmd, terragrunt, binary])).toEqual([
251
+ ["plan", "terragrunt", undefined],
252
+ ["plan", "/opt/tg-1.1.5/terragrunt", "/opt/tofu-1.9.0/tofu"],
253
+ ["apply", "terragrunt", undefined],
254
+ ["apply", "/opt/tg-1.1.5/terragrunt", "/opt/tofu-1.9.0/tofu"],
255
+ ]);
256
+ });
257
+
258
+ it("reads a unit's pins as terragucci does, and asks the product where each pinned release is", async () => {
259
+ const dir = tmp();
260
+ const put = (path: string, text: string) => {
261
+ mkdirSync(dirname(join(dir, path)), { recursive: true });
262
+ writeFileSync(join(dir, path), text);
263
+ };
264
+ put("live/a/.opentofu-version", "# pinned\nv1.9.0\n");
265
+ put("live/a/terragrunt.hcl", 'terragrunt_version_constraint = "= 1.1.5" # exact\n');
266
+ put("live/b/main.tf", 'terraform {\n required_version = "= 1.8.2"\n}\n');
267
+ put("live/c/.opentofu-version", "latest\n");
268
+ put("live/c/terragrunt.hcl", 'terragrunt_version_constraint = ">= 1.1"\n');
269
+ put("live/d/.terraform-version", "1.13.1\n");
270
+ expect(terragruntUnitPins(dir, "live/a", "tofu")).toEqual({ binary: { tool: "tofu", version: "1.9.0", source: ".opentofu-version" }, terragrunt: { version: "1.1.5", source: "terragrunt_version_constraint" } });
271
+ expect(terragruntUnitPins(dir, "live/b", "/usr/bin/tofu")).toEqual({ binary: { tool: "tofu", version: "1.8.2", source: "required_version" } });
272
+ expect(terragruntUnitPins(dir, "live/c", "tofu")).toEqual({});
273
+ // A .terraform-version pins terraform only; a stand-in binary pins nothing.
274
+ expect(terragruntUnitPins(dir, "live/d", "tofu")).toEqual({});
275
+ expect(terragruntUnitPins(dir, "live/d", "terraform")).toEqual({ binary: { tool: "terraform", version: "1.13.1", source: ".terraform-version" } });
276
+ expect(terragruntUnitPins(dir, "live/a", "choudoufu")).toEqual({ terragrunt: { version: "1.1.5", source: "terragrunt_version_constraint" } });
277
+ // A pin the step's release already is runs the step's.
278
+ const asked: string[] = [];
279
+ const resolve = terragruntUnitPinResolver({
280
+ release: (tool, version) => {
281
+ asked.push(`${tool}@${version}`);
282
+ return `/opt/${tool}-${version}/${tool}`;
283
+ },
284
+ carried: { tofu: "1.8.2" },
285
+ });
286
+ const input = { unit: "{target}", dir, binary: "tofu" };
287
+ expect(await resolve("live/a", input)).toEqual({ binary: "/opt/tofu-1.9.0/tofu", terragrunt: "/opt/terragrunt-1.1.5/terragrunt" });
288
+ expect(await resolve("live/b", input)).toEqual({});
289
+ expect(await resolve("live/c", input)).toEqual({});
290
+ expect(asked).toEqual(["tofu@1.9.0", "terragrunt@1.1.5"]);
291
+ });
292
+
293
+ it("units whose pins name the same releases share one run --all", async () => {
294
+ const dir = tmp();
295
+ const calls: Call[] = [];
296
+ const result = await wave(spec(dir), dir, stub(calls), undefined, { resolveUnit: () => ({ binary: "/opt/tofu-1.9.0/tofu" }) });
297
+ expect(result.exitCode).toBe(0);
298
+ expect(runs(calls).map(({ cmd, units, binary }) => [cmd, units.length, binary])).toEqual([
299
+ ["plan", 2, "/opt/tofu-1.9.0/tofu"],
300
+ ["apply", 2, "/opt/tofu-1.9.0/tofu"],
301
+ ]);
302
+ });
303
+ });
304
+
305
+ describe("a unit whose plan changes only outputs (#4849)", () => {
306
+ // The recorded live/dev/vpc plan, with its resource already in place and only its output changing.
307
+ const outputOnly = (unit: string): string | undefined => {
308
+ if (unit !== "live/dev/vpc") return undefined;
309
+ const plan = JSON.parse(read(`wave-1/json/${unit}/tfplan.json`)) as { resource_changes: { change: Record<string, unknown> }[]; output_changes: Record<string, Record<string, unknown>> };
310
+ for (const r of plan.resource_changes) r.change = { ...r.change, actions: ["no-op"], before: r.change.after };
311
+ plan.output_changes.id = { ...plan.output_changes.id, actions: ["update"], before: "old-id" };
312
+ return JSON.stringify(plan);
313
+ };
314
+
315
+ it("is a change, so digest.over changing keeps it in the wave digest", async () => {
316
+ const decisionOf = async (over: "all" | "changing") => {
317
+ const dir = tmp();
318
+ const s: OpWavesSpec = { ...spec(dir), digest: { over } };
319
+ const result = await wave(s, dir, stub([], { planJson: outputOnly }));
320
+ expect(result.exitCode).toBe(0);
321
+ return result.decision!;
322
+ };
323
+ const changing = await decisionOf("changing");
324
+ expect(changing.members.find((m) => m.member === "live/dev/vpc")).not.toHaveProperty("empty");
325
+ expect(changing.digest).toBe((await decisionOf("all")).digest);
326
+ });
327
+ });