@intentius/chant-lexicon-terraform 0.124.78 → 0.124.80
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/change-set.d.ts +1 -0
- package/dist/change-set.d.ts.map +1 -1
- package/dist/change-set.js +19 -0
- package/dist/components/capability-plugin.d.ts +75 -2
- package/dist/components/capability-plugin.d.ts.map +1 -1
- package/dist/components/capability-plugin.js +250 -142
- package/dist/components/compose-group.d.ts +2 -1
- package/dist/components/compose-group.d.ts.map +1 -1
- package/dist/components/compose-group.js +6 -6
- package/dist/index.d.ts +1 -1
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +3 -1
- package/dist/integrity.json +2 -2
- package/dist/manifest.json +1 -1
- package/dist/op/activities/terraform.d.ts +33 -0
- package/dist/op/activities/terraform.d.ts.map +1 -1
- package/dist/op/activities/terraform.js +37 -5
- package/dist/state-reads.d.ts +20 -3
- package/dist/state-reads.d.ts.map +1 -1
- package/dist/state-reads.js +20 -4
- package/dist/terragrunt/capability.d.ts +129 -0
- package/dist/terragrunt/capability.d.ts.map +1 -0
- package/dist/terragrunt/capability.js +352 -0
- package/dist/terragrunt/index.d.ts +3 -1
- package/dist/terragrunt/index.d.ts.map +1 -1
- package/dist/terragrunt/index.js +3 -1
- package/dist/terragrunt/units.d.ts +9 -0
- package/dist/terragrunt/units.d.ts.map +1 -1
- package/dist/terragrunt/units.js +25 -0
- package/package.json +2 -2
- package/src/change-set.test.ts +27 -1
- package/src/change-set.ts +19 -0
- package/src/components/capability-plugin.test.ts +61 -6
- package/src/components/capability-plugin.ts +290 -139
- package/src/components/compose-group.ts +8 -7
- package/src/components/compose-op-wave.test.ts +134 -0
- package/src/components/terraform-apply-op-wave.test.ts +295 -0
- package/src/index.ts +5 -0
- package/src/op/activities/terraform.test.ts +21 -0
- package/src/op/activities/terraform.ts +64 -4
- package/src/state-reads.ts +23 -5
- package/src/terragrunt/capability.test.ts +348 -0
- package/src/terragrunt/capability.ts +437 -0
- package/src/terragrunt/index.ts +3 -1
- package/src/terragrunt/units.ts +25 -0
package/src/state-reads.ts
CHANGED
|
@@ -12,6 +12,14 @@
|
|
|
12
12
|
* as `-var`, under the step's own `vars`, which win. A read whose upstream
|
|
13
13
|
* has no state, or no such output yet, waits: the root is not planned on a
|
|
14
14
|
* stand-in, and the wave says which upstream to apply first.
|
|
15
|
+
*
|
|
16
|
+
* An Atmos instance is read the way terragucci's `upstreamOutputs` reads it
|
|
17
|
+
* (terragucci issue 1303): `dir` names its directory when it has no
|
|
18
|
+
* `terraform.roots` entry, and `workspace` the workspace its state is in, so
|
|
19
|
+
* the read inits in `default`, selects or creates the workspace with
|
|
20
|
+
* `TF_WORKSPACE` unset, then runs `output -json` in it. A product's
|
|
21
|
+
* `resolveRoot` answers the binary and the environment (its role) the read
|
|
22
|
+
* runs with, never the reader's.
|
|
15
23
|
*/
|
|
16
24
|
|
|
17
25
|
/** One var a root reads from another root's state. */
|
|
@@ -22,6 +30,10 @@ export interface TerraformStateRead {
|
|
|
22
30
|
root: string;
|
|
23
31
|
/** The output, then the keys into its value. */
|
|
24
32
|
output: string[];
|
|
33
|
+
/** The upstream's directory, from the project root, when it has no `terraform.roots` entry (terragucci issue 1303). */
|
|
34
|
+
dir?: string;
|
|
35
|
+
/** The workspace the upstream's state is in (terragucci issue 1303), selected or created after an init in `default`. */
|
|
36
|
+
workspace?: string;
|
|
25
37
|
}
|
|
26
38
|
|
|
27
39
|
/** What filling a root's reads came to. */
|
|
@@ -51,6 +63,8 @@ export function stateReadsProblems(reads: unknown): string[] {
|
|
|
51
63
|
else vars.add(read.var);
|
|
52
64
|
if (typeof read.root !== "string" || read.root === "") problems.push(`reads[${i}].root must name a terraform.roots entry`);
|
|
53
65
|
if (!Array.isArray(read.output) || read.output.length === 0 || !read.output.every((k) => typeof k === "string" && k !== "")) problems.push(`reads[${i}].output must be the output's name, then any keys into its value`);
|
|
66
|
+
if (read.dir !== undefined && (typeof read.dir !== "string" || read.dir === "")) problems.push(`reads[${i}].dir must name the upstream's directory`);
|
|
67
|
+
if (read.workspace !== undefined && (typeof read.workspace !== "string" || read.workspace.trim() === "")) problems.push(`reads[${i}].workspace must name a workspace`);
|
|
54
68
|
}
|
|
55
69
|
return problems;
|
|
56
70
|
}
|
|
@@ -71,11 +85,11 @@ export function readOutputValue(outputs: Record<string, unknown>, output: readon
|
|
|
71
85
|
* Fill `reads` from each upstream's outputs, read once per upstream through
|
|
72
86
|
* `outputsOf`, which throws when the state cannot be read.
|
|
73
87
|
*/
|
|
74
|
-
export async function fillStateReads(reads: readonly TerraformStateRead[], outputsOf: (root: string) => Promise<Record<string, unknown>>): Promise<FilledStateReads> {
|
|
88
|
+
export async function fillStateReads(reads: readonly TerraformStateRead[], outputsOf: (root: string, read: TerraformStateRead) => Promise<Record<string, unknown>>): Promise<FilledStateReads> {
|
|
75
89
|
const result: FilledStateReads = { vars: {}, waiting: [], errors: [] };
|
|
76
90
|
const cache = new Map<string, Promise<Record<string, unknown>>>();
|
|
77
91
|
for (const read of reads) {
|
|
78
|
-
if (!cache.has(read.root)) cache.set(read.root, outputsOf(read.root));
|
|
92
|
+
if (!cache.has(read.root)) cache.set(read.root, outputsOf(read.root, read));
|
|
79
93
|
let outputs: Record<string, unknown>;
|
|
80
94
|
try {
|
|
81
95
|
outputs = await cache.get(read.root)!;
|
|
@@ -103,14 +117,18 @@ export function waitingReadsReason(waiting: FilledStateReads["waiting"]): string
|
|
|
103
117
|
* The reads an Atmos instance's edges file lists (`{ reads: [{ var,
|
|
104
118
|
* upstream, output }] }`, as terragucci's `atmos write` leaves beside each
|
|
105
119
|
* instance), with each upstream mapped to its `terraform.roots` entry by
|
|
106
|
-
* `rootOf` (by default the upstream's own path).
|
|
120
|
+
* `rootOf` (by default the upstream's own path). `at` answers, per
|
|
121
|
+
* upstream, its directory and the workspace its state is in (terragucci
|
|
122
|
+
* issue 1303), which the read inits and selects.
|
|
107
123
|
*/
|
|
108
|
-
export function atmosStateReads(edges: unknown, rootOf: (upstream: string) => string = (u) => u): TerraformStateRead[] {
|
|
124
|
+
export function atmosStateReads(edges: unknown, rootOf: (upstream: string) => string = (u) => u, at?: (upstream: string) => { dir?: string; workspace?: string }): TerraformStateRead[] {
|
|
109
125
|
const reads = (edges as { reads?: unknown } | null)?.reads;
|
|
110
126
|
if (!Array.isArray(reads)) return [];
|
|
111
127
|
return reads.flatMap((r) => {
|
|
112
128
|
const read = r as { var?: unknown; upstream?: unknown; output?: unknown } | null;
|
|
113
129
|
if (!read || typeof read.var !== "string" || typeof read.upstream !== "string" || !Array.isArray(read.output) || !read.output.every((k) => typeof k === "string")) return [];
|
|
114
|
-
|
|
130
|
+
// terragucci issue 1303: where the upstream instance is and which workspace holds its state, when the caller knows.
|
|
131
|
+
const where = at?.(read.upstream);
|
|
132
|
+
return [{ var: read.var, root: rootOf(read.upstream), output: read.output as string[], ...(where?.dir ? { dir: where.dir } : {}), ...(where?.workspace ? { workspace: where.workspace } : {}) }];
|
|
115
133
|
});
|
|
116
134
|
}
|
|
@@ -0,0 +1,348 @@
|
|
|
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
|
+
});
|
|
328
|
+
|
|
329
|
+
describe("terragrunt-apply runs where the wave runs (chant#4854)", () => {
|
|
330
|
+
it("with no dir in the step, a wave run in process for another directory finds, plans and applies there", async () => {
|
|
331
|
+
const dir = tmp();
|
|
332
|
+
const calls: Array<Call & { cwd: string }> = [];
|
|
333
|
+
const inner = stub(calls);
|
|
334
|
+
const exec: TerragruntExec = (f, args, o) => {
|
|
335
|
+
const at = calls.length;
|
|
336
|
+
return inner(f, args, o).then((r) => {
|
|
337
|
+
calls[at]!.cwd = o.cwd;
|
|
338
|
+
return r;
|
|
339
|
+
});
|
|
340
|
+
};
|
|
341
|
+
const s = spec(dir);
|
|
342
|
+
const { dir: _dir, ...step } = s.step as Record<string, unknown>;
|
|
343
|
+
const result = await wave({ ...s, step: step as OpWavesSpec["step"] }, dir, exec);
|
|
344
|
+
expect(result.exitCode).toBe(0);
|
|
345
|
+
expect(calls.length).toBeGreaterThan(0);
|
|
346
|
+
expect(new Set(calls.map((c) => c.cwd))).toEqual(new Set([dir]));
|
|
347
|
+
});
|
|
348
|
+
});
|