@intentius/chant-lexicon-terraform 0.124.79 → 0.124.81

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 (42) hide show
  1. package/dist/components/capability-plugin.d.ts +74 -1
  2. package/dist/components/capability-plugin.d.ts.map +1 -1
  3. package/dist/components/capability-plugin.js +239 -138
  4. package/dist/components/compose-group.d.ts +2 -1
  5. package/dist/components/compose-group.d.ts.map +1 -1
  6. package/dist/components/compose-group.js +6 -6
  7. package/dist/index.d.ts +1 -1
  8. package/dist/index.d.ts.map +1 -1
  9. package/dist/index.js +3 -1
  10. package/dist/init-turn.d.ts +21 -0
  11. package/dist/init-turn.d.ts.map +1 -0
  12. package/dist/init-turn.js +30 -0
  13. package/dist/integrity.json +2 -2
  14. package/dist/manifest.json +1 -1
  15. package/dist/op/activities/terraform.d.ts +22 -0
  16. package/dist/op/activities/terraform.d.ts.map +1 -1
  17. package/dist/op/activities/terraform.js +29 -5
  18. package/dist/state-reads.d.ts +20 -3
  19. package/dist/state-reads.d.ts.map +1 -1
  20. package/dist/state-reads.js +20 -4
  21. package/dist/terragrunt/capability.d.ts +5 -0
  22. package/dist/terragrunt/capability.d.ts.map +1 -1
  23. package/dist/terragrunt/capability.js +21 -7
  24. package/dist/terragrunt/run.d.ts +8 -0
  25. package/dist/terragrunt/run.d.ts.map +1 -1
  26. package/dist/terragrunt/run.js +12 -6
  27. package/dist/terragrunt/wave.d.ts +9 -0
  28. package/dist/terragrunt/wave.d.ts.map +1 -1
  29. package/dist/terragrunt/wave.js +22 -0
  30. package/package.json +2 -2
  31. package/src/components/capability-plugin.ts +279 -135
  32. package/src/components/compose-group.ts +8 -7
  33. package/src/components/terraform-apply-op-wave.test.ts +295 -0
  34. package/src/components/terraform-apply-parallel.test.ts +158 -0
  35. package/src/index.ts +5 -0
  36. package/src/init-turn.ts +36 -0
  37. package/src/op/activities/terraform.ts +45 -4
  38. package/src/state-reads.ts +23 -5
  39. package/src/terragrunt/capability.test.ts +94 -0
  40. package/src/terragrunt/capability.ts +22 -7
  41. package/src/terragrunt/run.ts +18 -4
  42. package/src/terragrunt/wave.ts +22 -0
@@ -0,0 +1,295 @@
1
+ /**
2
+ * `terraform-apply` as an op wave's step, run by core's `runOpWave` against a
3
+ * fake `terraform` on `PATH` (chant#4854, terragucci issue 1303). The fake
4
+ * logs each command with its directory and `TF_WORKSPACE`, keeps the
5
+ * workspaces a root's backend lists in `.ws` beside the root, refuses an
6
+ * init in a workspace that is not listed (as a real backend does) and a
7
+ * `workspace select` while `TF_WORKSPACE` is set, and answers `output -json`
8
+ * from `outputs-<workspace>.json`. Nothing reaches a cloud.
9
+ */
10
+
11
+ import { chmodSync, existsSync, mkdirSync, mkdtempSync, readFileSync, realpathSync, rmSync, writeFileSync } from "node:fs";
12
+ import { tmpdir } from "node:os";
13
+ import { join } from "node:path";
14
+ import { afterAll, afterEach, describe, expect, test, vi } from "vitest";
15
+ import { CapabilityRegistry, type Capability } from "@intentius/chant/components/capability";
16
+ import { memoryGateLedgerPort, opWavePlanPath, runOpWave, type OpWaveRunHooks, type OpWavesSpec, type RunOpWaveResult } from "@intentius/chant/op";
17
+ import { terraformApplyCapability, terraformApplyCapabilityWith, type TerraformRootResolver } from "./capability-plugin";
18
+ import { atmosStateReads } from "../state-reads";
19
+
20
+ const made: string[] = [];
21
+ afterAll(() => {
22
+ for (const d of made) rmSync(d, { recursive: true, force: true });
23
+ });
24
+ afterEach(() => vi.unstubAllEnvs());
25
+
26
+ /** A fake terraform. `$FAKE_LOG` gets one line per command: `<name> <dir> ws=<TF_WORKSPACE or -> <args>`. */
27
+ const fake = (name: string) => `#!/bin/sh
28
+ ws="\${TF_WORKSPACE-}"
29
+ echo "${name} $PWD ws=\${TF_WORKSPACE--} $*" >> "$FAKE_LOG"
30
+ [ -n "\${FAKE_ENV_LOG-}" ] && echo "\${PWD##*/} $1 role=\${ROLE--}" >> "$FAKE_ENV_LOG"
31
+ case "$1" in
32
+ init)
33
+ if [ -n "$ws" ] && [ "$ws" != "default" ] && ! grep -qx "$ws" .ws 2>/dev/null; then
34
+ echo "Error: Currently selected workspace \\"$ws\\" does not exist" >&2; exit 1
35
+ fi
36
+ exit 0 ;;
37
+ workspace)
38
+ if [ -n "\${TF_WORKSPACE+x}" ]; then echo "Error: The selected workspace is currently overridden using the TF_WORKSPACE environment variable." >&2; exit 1; fi
39
+ for a in "$@"; do w="$a"; done
40
+ grep -qx "$w" .ws 2>/dev/null || echo "$w" >> .ws
41
+ exit 0 ;;
42
+ plan)
43
+ for a in "$@"; do case "$a" in -out=*) out="\${a#-out=}" ;; esac; done
44
+ echo plan > "$out"
45
+ exit 2 ;;
46
+ show)
47
+ echo '{"format_version":"1.2","resource_changes":[{"address":"terraform_data.x","mode":"managed","type":"terraform_data","name":"x","change":{"actions":["create"],"before":null,"after":{}}}]}'
48
+ exit 0 ;;
49
+ apply) exit 0 ;;
50
+ output)
51
+ f="outputs-\${ws:-default}.json"
52
+ if [ -f "$f" ]; then cat "$f"; else echo '{}'; fi
53
+ exit 0 ;;
54
+ esac
55
+ echo "fake ${name}: unexpected $*" >&2
56
+ exit 1
57
+ `;
58
+
59
+ /** A project with roots at `roots/<name>`, a `chant.config.json` naming `binary`, and a fake `terraform` on PATH. */
60
+ function project(roots: string[], config: Record<string, unknown> = { binary: "terraform" }): { dir: string; log: string; bin: string } {
61
+ const dir = realpathSync(mkdtempSync(join(tmpdir(), "chant-tf-op-wave-")));
62
+ made.push(dir);
63
+ const bin = join(dir, "bin");
64
+ mkdirSync(bin);
65
+ for (const name of ["terraform", "tofu"]) {
66
+ writeFileSync(join(bin, name), fake(name));
67
+ chmodSync(join(bin, name), 0o755);
68
+ }
69
+ const log = join(dir, "fake.log");
70
+ writeFileSync(log, "");
71
+ vi.stubEnv("PATH", `${bin}:${process.env.PATH ?? ""}`);
72
+ vi.stubEnv("FAKE_LOG", log);
73
+ for (const r of roots) {
74
+ mkdirSync(join(dir, "roots", r), { recursive: true });
75
+ writeFileSync(join(dir, "roots", r, "main.tf"), "");
76
+ }
77
+ writeFileSync(join(dir, "chant.config.json"), JSON.stringify({ lexicons: ["terraform"], terraform: config }));
78
+ return { dir, log, bin };
79
+ }
80
+
81
+ /** The fake's log, with the project's directory as `.`. */
82
+ const lines = (p: { dir: string; log: string }): string[] =>
83
+ readFileSync(p.log, "utf-8")
84
+ .split("\n")
85
+ .filter(Boolean)
86
+ .map((l) => l.split(p.dir).join("."));
87
+
88
+ /** A log line without the plan file's name. */
89
+ const plain = (l: string): string => l.replace(/ -out=\S+| \S+\.tfplan$/g, "");
90
+
91
+ const registry = (cap: Capability<never, unknown> = terraformApplyCapability as unknown as Capability<never, unknown>) => new CapabilityRegistry().register(cap);
92
+
93
+ function spec(runs: OpWavesSpec["waves"][number]["runs"], step: Record<string, unknown> = {}): OpWavesSpec {
94
+ return {
95
+ name: "tf-apply",
96
+ op: "tf-apply",
97
+ gateName: "wave-{k}",
98
+ step: { kind: "terraform-apply", root: "{target}", dir: "{target}", lockTimeout: false, ...step },
99
+ waves: [{ name: "wave-1", gate: "never", runs }],
100
+ } as OpWavesSpec;
101
+ }
102
+
103
+ function wave(cwd: string, s: OpWavesSpec, o: { runHooks?: OpWaveRunHooks; capabilities?: CapabilityRegistry } = {}): Promise<RunOpWaveResult> {
104
+ return runOpWave({
105
+ spec: s,
106
+ specFile: "waves.json",
107
+ wave: 1,
108
+ cwd,
109
+ show: () => ({ sha: "b".repeat(40), text: JSON.stringify(s) }),
110
+ gates: memoryGateLedgerPort(),
111
+ capabilities: o.capabilities ?? registry(),
112
+ ...(o.runHooks ? { runHooks: o.runHooks } : {}),
113
+ log: () => {},
114
+ });
115
+ }
116
+
117
+ describe("a root that names its workspace (chant#4854)", () => {
118
+ test("inits in default, selects or creates the workspace with TF_WORKSPACE unset, then plans, applies and reads outputs in it", async () => {
119
+ const p = project(["app"]);
120
+ const result = await wave(p.dir, spec([{ target: "roots/app", input: { workspace: "ue1-prod" } }], { cwd: p.dir }));
121
+ expect(result.exitCode).toBe(0);
122
+ expect(result.applied).toEqual(["roots/app"]);
123
+ expect(lines(p).map(plain)).toEqual([
124
+ "terraform ./roots/app ws=default init -input=false",
125
+ "terraform ./roots/app ws=- workspace select -or-create=true ue1-prod",
126
+ "terraform ./roots/app ws=ue1-prod plan -input=false -detailed-exitcode",
127
+ "terraform ./roots/app ws=ue1-prod show -json",
128
+ "terraform ./roots/app ws=ue1-prod apply -input=false",
129
+ "terraform ./roots/app ws=ue1-prod output -json",
130
+ ]);
131
+ expect(readFileSync(join(p.dir, "roots/app/.ws"), "utf-8")).toBe("ue1-prod\n");
132
+ });
133
+
134
+ test("the step's workspace wins over a TF_WORKSPACE the run hook's env sets", async () => {
135
+ const p = project(["app"]);
136
+ const runHooks: OpWaveRunHooks = { name: "product", beforePlan: () => ({ env: { TF_WORKSPACE: "ue1-prod", ROLE: "app" } }) };
137
+ const result = await wave(p.dir, spec([{ target: "roots/app", input: { workspace: "ue1-prod" } }], { cwd: p.dir }), { runHooks });
138
+ expect(result.exitCode).toBe(0);
139
+ const log = lines(p);
140
+ expect(log[0]).toBe("terraform ./roots/app ws=default init -input=false");
141
+ expect(log[1]).toBe("terraform ./roots/app ws=- workspace select -or-create=true ue1-prod");
142
+ expect(log.filter((l) => / (plan|apply|output) /.test(l)).every((l) => l.includes(" ws=ue1-prod "))).toBe(true);
143
+ });
144
+
145
+ test("a workspace the backend lists already is selected, not created again", async () => {
146
+ const p = project(["app"]);
147
+ writeFileSync(join(p.dir, "roots/app/.ws"), "ue1-prod\n");
148
+ const result = await wave(p.dir, spec([{ target: "roots/app", input: { workspace: "ue1-prod" } }], { cwd: p.dir }));
149
+ expect(result.exitCode).toBe(0);
150
+ expect(readFileSync(join(p.dir, "roots/app/.ws"), "utf-8")).toBe("ue1-prod\n");
151
+ });
152
+
153
+ test("without the input, a run hook's TF_WORKSPACE of a new workspace fails init, as before", async () => {
154
+ const p = project(["app"]);
155
+ const runHooks: OpWaveRunHooks = { name: "product", beforePlan: () => ({ env: { TF_WORKSPACE: "ue1-prod" } }) };
156
+ await expect(wave(p.dir, spec([{ target: "roots/app" }], { cwd: p.dir }), { runHooks })).rejects.toThrow(/Currently selected workspace "ue1-prod" does not exist/);
157
+ expect(lines(p)).toEqual(["terraform ./roots/app ws=ue1-prod init -input=false"]);
158
+ expect(existsSync(join(p.dir, "roots/app/.ws"))).toBe(false);
159
+ });
160
+
161
+ test("a root that names no workspace runs as before: no select, and no TF_WORKSPACE", async () => {
162
+ const p = project(["app"]);
163
+ const result = await wave(p.dir, spec([{ target: "roots/app" }], { cwd: p.dir }));
164
+ expect(result.exitCode).toBe(0);
165
+ expect(lines(p).some((l) => l.includes(" workspace "))).toBe(false);
166
+ expect(lines(p).every((l) => l.includes(" ws=- "))).toBe(true);
167
+ });
168
+ });
169
+
170
+ describe("where the step looks for chant.config (chant#4854)", () => {
171
+ test("with no cwd in the step, a wave run in process for another directory reads that directory's config", async () => {
172
+ const p = project(["app"], { binary: "tofu" });
173
+ // The process runs elsewhere: this checkout's own chant.config is not the project's.
174
+ expect(process.cwd()).not.toBe(p.dir);
175
+ const result = await wave(p.dir, spec([{ target: "roots/app" }]));
176
+ expect(result.exitCode).toBe(0);
177
+ expect(result.applied).toEqual(["roots/app"]);
178
+ // The project's binary, in the project's directory.
179
+ expect(lines(p).map((l) => l.split(" ").slice(0, 3).join(" "))).toEqual(["tofu ./roots/app ws=-", "tofu ./roots/app ws=-", "tofu ./roots/app ws=-", "tofu ./roots/app ws=-", "tofu ./roots/app ws=-"]);
180
+ });
181
+
182
+ test("a cwd the step names still wins", async () => {
183
+ const p = project(["app"], { binary: "tofu" });
184
+ const other = project(["app"], { binary: "terraform" });
185
+ const result = await wave(p.dir, spec([{ target: "roots/app" }], { cwd: other.dir }));
186
+ expect(result.exitCode).toBe(0);
187
+ // Both fakes log to the last project's file; each line names the directory and binary it ran.
188
+ const ran = lines(other);
189
+ expect(ran.length).toBeGreaterThan(0);
190
+ expect(ran.every((l) => l.startsWith("terraform ./roots/app "))).toBe(true);
191
+ });
192
+ });
193
+
194
+ describe("a root that runs another binary than chant.config names (terragucci issue 1303)", () => {
195
+ test("a product's resolveRoot gives each root its own binary, by path, for its plan, apply and outputs", async () => {
196
+ const p = project(["a", "b"], { binary: "terraform" });
197
+ const asked: string[] = [];
198
+ const resolveRoot: TerraformRootResolver = async (root, input) => {
199
+ asked.push(`${root} ${input.cwd === p.dir ? "at the wave's directory" : String(input.cwd)}`);
200
+ return root === "roots/b" ? { binary: join(p.bin, "tofu") } : {};
201
+ };
202
+ const capabilities = registry(terraformApplyCapabilityWith({ resolveRoot }) as unknown as Capability<never, unknown>);
203
+ const result = await wave(p.dir, spec([{ target: "roots/a" }, { target: "roots/b" }]), { capabilities });
204
+ expect(result.exitCode).toBe(0);
205
+ expect(result.applied).toEqual(["roots/a", "roots/b"]);
206
+ const ran = lines(p).map((l) => l.split(" ").slice(0, 3).join(" ") + " " + l.split(" ")[3]);
207
+ expect(ran).toEqual([
208
+ "terraform ./roots/a ws=- init",
209
+ "terraform ./roots/a ws=- plan",
210
+ "terraform ./roots/a ws=- show",
211
+ "tofu ./roots/b ws=- init",
212
+ "tofu ./roots/b ws=- plan",
213
+ "tofu ./roots/b ws=- show",
214
+ "terraform ./roots/a ws=- apply",
215
+ "terraform ./roots/a ws=- output",
216
+ "tofu ./roots/b ws=- apply",
217
+ "tofu ./roots/b ws=- output",
218
+ ]);
219
+ // Asked once per plan and once per apply, with the step's input as the wave filled it.
220
+ expect(asked).toEqual(["roots/a at the wave's directory", "roots/b at the wave's directory", "roots/a at the wave's directory", "roots/b at the wave's directory"]);
221
+ });
222
+
223
+ test("the change set names the planner each root ran", async () => {
224
+ const p = project(["b"], { binary: "terraform" });
225
+ const capabilities = registry(terraformApplyCapabilityWith({ resolveRoot: () => ({ binary: join(p.bin, "tofu") }) }) as unknown as Capability<never, unknown>);
226
+ const s = spec([{ target: "roots/b" }]);
227
+ const result = await wave(p.dir, s, { capabilities });
228
+ expect(result.exitCode).toBe(0);
229
+ const file = JSON.parse(readFileSync(join(p.dir, opWavePlanPath(s, 1, "roots/b")), "utf-8")) as { changeSet?: { member: { planner: string } } };
230
+ expect(file.changeSet?.member.planner).toBe("tofu");
231
+ });
232
+
233
+ test("a step's own binary runs without a resolver, and a resolver that answers nothing leaves the config's", async () => {
234
+ const p = project(["a"], { binary: "terraform" });
235
+ expect((await wave(p.dir, spec([{ target: "roots/a" }], { binary: "tofu" }))).exitCode).toBe(0);
236
+ expect(lines(p).every((l) => l.startsWith("tofu "))).toBe(true);
237
+ const q = project(["a"], { binary: "terraform" });
238
+ const capabilities = registry(terraformApplyCapabilityWith({ resolveRoot: () => ({}) }) as unknown as Capability<never, unknown>);
239
+ expect((await wave(q.dir, spec([{ target: "roots/a" }]), { capabilities })).exitCode).toBe(0);
240
+ expect(lines(q).every((l) => l.startsWith("terraform "))).toBe(true);
241
+ });
242
+
243
+ test("a resolver that throws fails the root's plan", async () => {
244
+ const p = project(["a"], { binary: "terraform" });
245
+ const capabilities = registry(terraformApplyCapabilityWith({ resolveRoot: () => { throw new Error("tofu 1.9.1 is not installed"); } }) as unknown as Capability<never, unknown>);
246
+ await expect(wave(p.dir, spec([{ target: "roots/a" }]), { capabilities })).rejects.toThrow(/tofu 1.9.1 is not installed/);
247
+ expect(lines(p)).toEqual([]);
248
+ });
249
+ });
250
+
251
+ describe("a root that reads an Atmos instance's outputs (terragucci issue 1303)", () => {
252
+ /** `roots/net`, an instance in workspace ue1-net with a vpc_id, and `roots/app`, which reads it. */
253
+ function instances(netOutputs: string | undefined) {
254
+ const p = project(["net", "app"], { binary: "terraform" });
255
+ writeFileSync(join(p.dir, "roots/net/.ws"), "ue1-net\n");
256
+ if (netOutputs !== undefined) writeFileSync(join(p.dir, "roots/net/outputs-ue1-net.json"), netOutputs);
257
+ const envLog = join(p.dir, "env.log");
258
+ writeFileSync(envLog, "");
259
+ vi.stubEnv("FAKE_ENV_LOG", envLog);
260
+ // As terragucci's edges file lists the read, with where the upstream is and its workspace.
261
+ const reads = atmosStateReads({ reads: [{ var: "vpc_id", upstream: "roots/net", output: ["vpc", "id"], function: "!terraform.state net vpc.id" }] }, (u) => u, (u) => ({ dir: u, workspace: "ue1-net" }));
262
+ const resolveRoot: TerraformRootResolver = (root) => (root === "roots/net" ? { binary: join(p.bin, "tofu"), readEnv: { ROLE: "net-reader" } } : {});
263
+ const capabilities = registry(terraformApplyCapabilityWith({ resolveRoot }) as unknown as Capability<never, unknown>);
264
+ const runHooks: OpWaveRunHooks = { name: "product", beforePlan: () => ({ env: { ROLE: "app" } }) };
265
+ const s = spec([{ target: "roots/app", input: { workspace: "ue1-app", reads } }]);
266
+ return { p, envLog, capabilities, runHooks, s };
267
+ }
268
+
269
+ test("reads the upstream in its own directory and workspace, with its binary and role, and plans on the value", async () => {
270
+ const { p, envLog, capabilities, runHooks, s } = instances('{"vpc":{"value":{"id":"vpc-1"}}}');
271
+ const result = await wave(p.dir, s, { capabilities, runHooks });
272
+ expect(result.exitCode).toBe(0);
273
+ const log = lines(p).map(plain);
274
+ expect(log.slice(0, 3)).toEqual([
275
+ "tofu ./roots/net ws=default init -input=false",
276
+ "tofu ./roots/net ws=- workspace select -or-create=true ue1-net",
277
+ "tofu ./roots/net ws=ue1-net output -json",
278
+ ]);
279
+ expect(log.find((l) => l.includes(" plan "))).toBe("terraform ./roots/app ws=ue1-app plan -input=false -detailed-exitcode -var=vpc_id=vpc-1");
280
+ const roles = readFileSync(envLog, "utf-8").split("\n").filter(Boolean);
281
+ expect(roles.filter((l) => l.startsWith("net "))).toEqual(["net init role=net-reader", "net workspace role=net-reader", "net output role=net-reader"]);
282
+ expect(roles.filter((l) => l.startsWith("app ")).every((l) => l.endsWith("role=app"))).toBe(true);
283
+ });
284
+
285
+ test("an upstream with no state in its workspace yet keeps the root from planning, naming what to apply first", async () => {
286
+ const { p, capabilities, runHooks, s } = instances(undefined);
287
+ await expect(wave(p.dir, s, { capabilities, runHooks })).rejects.toThrow("not planned: it reads vpc_id from roots/net, which has no state yet; apply roots/net first");
288
+ expect(lines(p).some((l) => l.includes("./roots/app "))).toBe(false);
289
+ });
290
+
291
+ test("a read that names a directory or workspace that is not one is refused", async () => {
292
+ const p = project(["app"], { binary: "terraform" });
293
+ await expect(wave(p.dir, spec([{ target: "roots/app", input: { reads: [{ var: "x", root: "roots/net", output: ["x"], workspace: " " }] } }]))).rejects.toThrow("reads[0].workspace must name a workspace");
294
+ });
295
+ });
@@ -0,0 +1,158 @@
1
+ /**
2
+ * `terraform-apply` roots planned and applied `parallelism` at once on one
3
+ * provider cache (chant#4864), run by core's `runOpWave` against a fake
4
+ * `terraform` on `PATH`. The fake sleeps per command (`$FAKE_SLEEP_INIT`,
5
+ * and a root's own `delay` file for plan and apply), and marks each command
6
+ * in flight with a file under `$FAKE_STATE/<command>/`, moving it to
7
+ * `$FAKE_STATE/done/` when it ends. Every command logs how many of its kind
8
+ * were in flight as it started, so the test reads the most that ever ran
9
+ * together. The roots' `show -json` names the root, so each has its own
10
+ * plan digest. Nothing reaches a cloud.
11
+ */
12
+
13
+ import { chmodSync, existsSync, mkdirSync, mkdtempSync, readFileSync, realpathSync, rmSync, writeFileSync } from "node:fs";
14
+ import { tmpdir } from "node:os";
15
+ import { join } from "node:path";
16
+ import { afterAll, afterEach, describe, expect, test, vi } from "vitest";
17
+ import { CapabilityRegistry, type Capability } from "@intentius/chant/components/capability";
18
+ import { memoryGateLedgerPort, opWavePlanPath, runOpWave, type OpWavesSpec, type RunOpWaveResult } from "@intentius/chant/op";
19
+ import { terraformApplyCapability } from "./capability-plugin";
20
+
21
+ const made: string[] = [];
22
+ afterAll(() => {
23
+ for (const d of made) rmSync(d, { recursive: true, force: true });
24
+ });
25
+ afterEach(() => vi.unstubAllEnvs());
26
+
27
+ const FAKE = `#!/bin/sh
28
+ cmd="$1"
29
+ mkdir -p "$FAKE_STATE/$cmd" "$FAKE_STATE/done"
30
+ mark="$FAKE_STATE/$cmd/\${PWD##*/}.$$"
31
+ : > "$mark"
32
+ n=$(ls "$FAKE_STATE/$cmd" | wc -l | tr -d ' ')
33
+ echo "$cmd \${PWD##*/} $n" >> "$FAKE_LOG"
34
+ root="\${PWD##*/}"
35
+ case "$cmd" in
36
+ init) sleep "\${FAKE_SLEEP_INIT:-0.1}" ;;
37
+ plan)
38
+ sleep "$(cat delay 2>/dev/null || echo 0.2)"
39
+ for a in "$@"; do case "$a" in -out=*) out="\${a#-out=}" ;; esac; done
40
+ echo plan > "$out" ;;
41
+ apply) sleep "$(cat delay 2>/dev/null || echo 0.2)" ;;
42
+ show) echo "{\\"format_version\\":\\"1.2\\",\\"resource_changes\\":[{\\"address\\":\\"terraform_data.$root\\",\\"mode\\":\\"managed\\",\\"type\\":\\"terraform_data\\",\\"name\\":\\"$root\\",\\"change\\":{\\"actions\\":[\\"create\\"],\\"before\\":null,\\"after\\":{}}}]}" ;;
43
+ output) echo '{}' ;;
44
+ esac
45
+ mv "$mark" "$FAKE_STATE/done/"
46
+ [ "$cmd" = plan ] && exit 2
47
+ exit 0
48
+ `;
49
+
50
+ const ROOTS = ["a", "b", "c", "d"];
51
+
52
+ /** A project with `roots/<name>` for each root, each planning slower than the next, and the fake on PATH. */
53
+ function project(): { dir: string; log: string } {
54
+ const dir = realpathSync(mkdtempSync(join(tmpdir(), "chant-tf-par-")));
55
+ made.push(dir);
56
+ const bin = join(dir, "bin");
57
+ mkdirSync(bin);
58
+ writeFileSync(join(bin, "terraform"), FAKE);
59
+ chmodSync(join(bin, "terraform"), 0o755);
60
+ const log = join(dir, "fake.log");
61
+ writeFileSync(log, "");
62
+ mkdirSync(join(dir, "state"));
63
+ vi.stubEnv("PATH", `${bin}:${process.env.PATH ?? ""}`);
64
+ vi.stubEnv("FAKE_LOG", log);
65
+ vi.stubEnv("FAKE_STATE", join(dir, "state"));
66
+ vi.stubEnv("FAKE_SLEEP_INIT", "0.03");
67
+ for (const [i, r] of ROOTS.entries()) {
68
+ mkdirSync(join(dir, "roots", r), { recursive: true });
69
+ writeFileSync(join(dir, "roots", r, "main.tf"), "");
70
+ writeFileSync(join(dir, "roots", r, "delay"), String(0.06 + 0.03 * (ROOTS.length - i)));
71
+ }
72
+ writeFileSync(join(dir, "chant.config.json"), JSON.stringify({ lexicons: ["terraform"], terraform: { binary: "terraform" } }));
73
+ return { dir, log };
74
+ }
75
+
76
+ /** The fake's log as `{ command: [root, in flight] }`, in the order the commands started. */
77
+ function started(log: string): Record<string, Array<[string, number]>> {
78
+ const out: Record<string, Array<[string, number]>> = {};
79
+ for (const line of readFileSync(log, "utf-8").split("\n").filter(Boolean)) {
80
+ const [cmd, root, n] = line.split(" ");
81
+ (out[cmd!] ??= []).push([root!, Number(n)]);
82
+ }
83
+ return out;
84
+ }
85
+
86
+ const most = (rows: Array<[string, number]> | undefined) => Math.max(0, ...(rows ?? []).map(([, n]) => n));
87
+
88
+ function spec(parallelism?: number): OpWavesSpec {
89
+ return {
90
+ name: "tf-apply",
91
+ op: "tf-apply",
92
+ gateName: "wave-{k}",
93
+ ...(parallelism !== undefined ? { parallelism } : {}),
94
+ step: { kind: "terraform-apply", root: "{target}", dir: "{target}", lockTimeout: false },
95
+ waves: [{ name: "wave-1", gate: "never", runs: ROOTS.map((r) => ({ target: `roots/${r}` })) }],
96
+ } as OpWavesSpec;
97
+ }
98
+
99
+ async function wave(cwd: string, s: OpWavesSpec): Promise<{ result: RunOpWaveResult; files: string[] }> {
100
+ const result = await runOpWave({
101
+ spec: s,
102
+ specFile: "waves.json",
103
+ wave: 1,
104
+ cwd,
105
+ show: () => ({ sha: "b".repeat(40), text: JSON.stringify(s) }),
106
+ gates: memoryGateLedgerPort(),
107
+ capabilities: new CapabilityRegistry().register(terraformApplyCapability as unknown as Capability<never, unknown>),
108
+ log: () => {},
109
+ });
110
+ const files = ROOTS.map((r) => {
111
+ const f = join(cwd, opWavePlanPath(s, 1, `roots/${r}`));
112
+ return existsSync(f) ? readFileSync(f, "utf-8") : "";
113
+ });
114
+ return { result, files };
115
+ }
116
+
117
+ describe("terraform-apply roots at once on one provider cache (chant#4864)", () => {
118
+ test("the plans and applies overlap, and no two inits ever run at once", async () => {
119
+ const p = project();
120
+ vi.stubEnv("TF_PLUGIN_CACHE_DIR", join(p.dir, "cache"));
121
+ const { result } = await wave(p.dir, spec(4));
122
+ expect(result.exitCode).toBe(0);
123
+ expect(result.applied).toEqual(ROOTS.map((r) => `roots/${r}`));
124
+ const s = started(p.log);
125
+ expect(s.init?.length).toBe(4);
126
+ expect(most(s.init)).toBe(1);
127
+ expect(most(s.plan)).toBeGreaterThan(1);
128
+ expect(most(s.apply)).toBeGreaterThan(1);
129
+ });
130
+
131
+ test("the digest, members and plan files are the ones one root at a time makes", async () => {
132
+ const run = async (parallelism?: number) => {
133
+ const p = project();
134
+ vi.stubEnv("TF_PLUGIN_CACHE_DIR", join(p.dir, "cache"));
135
+ const got = await wave(p.dir, spec(parallelism));
136
+ return { ...got, started: started(p.log), dir: p.dir };
137
+ };
138
+ const one = await run();
139
+ const many = await run(4);
140
+ expect(most(one.started.plan)).toBe(1);
141
+ expect(most(many.started.plan)).toBeGreaterThan(1);
142
+ expect(many.result.decision?.digest).toBeDefined();
143
+ expect(many.result.decision?.digest).toBe(one.result.decision?.digest);
144
+ expect(many.result.decision?.members).toEqual(one.result.decision?.members);
145
+ // A plan file names its plan by path inside the project, so it reads the same with each project's directory taken out.
146
+ const local = (files: string[], dir: string) => files.map((f) => f.split(dir).join("."));
147
+ expect(local(many.files, many.dir)).toEqual(local(one.files, one.dir));
148
+ expect(new Set(many.result.decision?.members.map((m) => m.planDigest)).size).toBe(4);
149
+ });
150
+
151
+ test("with no cache the roots' inits run at once, each in its own directory", async () => {
152
+ const p = project();
153
+ vi.stubEnv("TF_PLUGIN_CACHE_DIR", "");
154
+ const { result } = await wave(p.dir, spec(4));
155
+ expect(result.exitCode).toBe(0);
156
+ expect(most(started(p.log).init)).toBeGreaterThan(1);
157
+ });
158
+ });
package/src/index.ts CHANGED
@@ -8,6 +8,11 @@ export { terraformPlugin } from "./plugin";
8
8
  export {
9
9
  terraformCapabilityPlugin,
10
10
  terraformApplyCapability,
11
+ // terragucci issue 1303: a product's resolver of what each root runs.
12
+ terraformApplyCapabilityWith,
13
+ type TerraformApplyDeps,
14
+ type TerraformRootResolver,
15
+ type TerraformRootTools,
11
16
  TerraformPlanMovedError,
12
17
  TERRAFORM_VERB_FAMILIES,
13
18
  // #4455: a choudoufu member's outputs, read for core's `member-outputs`.
@@ -0,0 +1,36 @@
1
+ /**
2
+ * An init's turn (chant#4864). An op wave with `parallelism` over 1 plans
3
+ * and applies several roots at once and gives each run the wave's serial
4
+ * section (`DeployContext.serial`). Terraform's provider cache
5
+ * (`TF_PLUGIN_CACHE_DIR`) is not safe for two inits writing it at once, so
6
+ * `terraform-apply` and `terragrunt-apply` take their inits in the section,
7
+ * keyed on the cache directory: one init at a time among the runs that share
8
+ * it, their plans and applies side by side. Without a cache, `terraform-apply`
9
+ * keys an init on the directory it runs in, since two runs reading the same
10
+ * upstream would init it at once. Without a section (runs one at a time)
11
+ * nothing changes.
12
+ */
13
+
14
+ import { resolve } from "node:path";
15
+
16
+ /** A section as `DeployContext.serial` gives it. */
17
+ export type InitSection = <T>(key: string, fn: () => Promise<T>) => Promise<T>;
18
+
19
+ /** The provider cache a run's inits write: its own environment's `TF_PLUGIN_CACHE_DIR`, else the job's. Undefined when neither names one. */
20
+ export function pluginCacheDir(env?: Record<string, string | undefined>): string | undefined {
21
+ const dir = env && "TF_PLUGIN_CACHE_DIR" in env ? env.TF_PLUGIN_CACHE_DIR : process.env.TF_PLUGIN_CACHE_DIR;
22
+ return dir ? resolve(dir) : undefined;
23
+ }
24
+
25
+ /** The key an init takes its turn under: the provider cache, else `dir` when given. Undefined when it needs no turn. */
26
+ export function initTurnKey(env: Record<string, string | undefined> | undefined, dir?: string): string | undefined {
27
+ const cache = pluginCacheDir(env);
28
+ if (cache) return `terraform-plugin-cache:${cache}`;
29
+ return dir ? `terraform-init:${resolve(dir)}` : undefined;
30
+ }
31
+
32
+ /** Run `fn`, an init, in its turn: inside `serial` under {@link initTurnKey}, or as it is when there is no section or no key. */
33
+ export function inInitTurn<T>(serial: InitSection | undefined, env: Record<string, string | undefined> | undefined, dir: string | undefined, fn: () => Promise<T>): Promise<T> {
34
+ const key = serial ? initTurnKey(env, dir) : undefined;
35
+ return key ? serial!(key, fn) : fn();
36
+ }
@@ -216,6 +216,23 @@ export interface TerraformRootArgs {
216
216
  * `DeployContext.processEnv`.
217
217
  */
218
218
  env?: Record<string, string>;
219
+ /**
220
+ * The workspace the root runs in, in place of its entry's (chant#4854),
221
+ * created when the backend does not list it yet. `terraformInit` inits in
222
+ * `default`, which every backend has, then runs `workspace select
223
+ * -or-create=true <workspace>` with `TF_WORKSPACE` unset, since init
224
+ * refuses a selected workspace the backend does not list. Every other
225
+ * command runs with `TF_WORKSPACE` set to it, over `env`. An entry's own
226
+ * `workspace` keeps its old behaviour: `TF_WORKSPACE` on every command,
227
+ * init included, and nothing created.
228
+ */
229
+ workspace?: string;
230
+ /**
231
+ * The binary to run, a name on `PATH` or a path, in place of
232
+ * `terraform.binary` (terragucci issue 1303): a root that pins its own
233
+ * release. Absent: `terraform.binary`.
234
+ */
235
+ binary?: string;
219
236
  }
220
237
 
221
238
  export interface TerraformInitArgs extends TerraformRootArgs {
@@ -621,12 +638,22 @@ export function terraformEnvironment(root?: Pick<TerraformRootConfig, "workspace
621
638
  ...(root?.workspace ? { TF_WORKSPACE: root.workspace } : {}),
622
639
  // chant#4842: the run's own environment, which resolveRoot ties to the root it resolved.
623
640
  ...(root ? rootProcessEnv.get(root) : undefined),
641
+ // chant#4854: a workspace the step names wins over the run's environment.
642
+ ...(root && rootOwnWorkspace.has(root) ? { TF_WORKSPACE: rootOwnWorkspace.get(root)! } : {}),
624
643
  };
625
644
  }
626
645
 
627
646
  /** The `env` a root was resolved with (chant#4842), by the resolved root's entry, so every command run for it gets it. */
628
647
  const rootProcessEnv = new WeakMap<object, Record<string, string>>();
629
648
 
649
+ /** The workspace a root was resolved with (chant#4854, `TerraformRootArgs.workspace`), which init selects or creates. */
650
+ const rootOwnWorkspace = new WeakMap<object, string>();
651
+
652
+ /** `<binary> workspace select -or-create=true <workspace>` (chant#4854): select the workspace, creating it when the backend does not list it. */
653
+ export function terraformWorkspaceSelectCommand(opts: { binary: string; workspace: string }): string {
654
+ return `${opts.binary} workspace select -or-create=true ${quoteArg(opts.workspace)}`;
655
+ }
656
+
630
657
  /**
631
658
  * `terraform init`, with the root's `backendConfigFiles` as
632
659
  * `-backend-config=<path>` flags and then its `backendConfig` as
@@ -976,8 +1003,10 @@ async function resolveRoot(args: TerraformRootArgs, signal?: AbortSignal): Promi
976
1003
  const roots = namespace?.roots ?? {};
977
1004
  // chant#4842: a root with no entry and a dir of its own plans that directory.
978
1005
  const declared = roots[args.root] ?? (args.dir !== undefined ? { dir: args.dir } : undefined);
979
- const root = declared && args.env ? { ...declared } : declared;
1006
+ const own = args.workspace !== undefined && args.workspace !== "" ? args.workspace : undefined;
1007
+ const root = declared && (args.env || own) ? { ...declared, ...(own ? { workspace: own } : {}) } : declared;
980
1008
  if (root && args.env) rootProcessEnv.set(root, args.env);
1009
+ if (root && own) rootOwnWorkspace.set(root, own);
981
1010
  if (!root) {
982
1011
  const known = Object.keys(roots).sort();
983
1012
  throw new Error(
@@ -986,7 +1015,7 @@ async function resolveRoot(args: TerraformRootArgs, signal?: AbortSignal): Promi
986
1015
  (known.length > 0 ? `known roots: ${known.join(", ")}` : "the namespace declares no roots"),
987
1016
  );
988
1017
  }
989
- const binary = terraformBinary(namespace);
1018
+ const binary = args.binary !== undefined && args.binary !== "" ? args.binary : terraformBinary(namespace);
990
1019
  const dir = resolve(projectRoot, args.dir ?? root.dir);
991
1020
  await ensureChoudoufuVersion(binary, signal);
992
1021
  const estate = binary === "choudoufu" ? detectLiveEstate(dir) : undefined;
@@ -1019,8 +1048,11 @@ async function run(
1019
1048
  dir: string,
1020
1049
  env: Record<string, string>,
1021
1050
  signal?: AbortSignal,
1051
+ unset: readonly string[] = [],
1022
1052
  ): Promise<{ stdout: string; stderr: string }> {
1023
- return execAsync(cmd, { cwd: dir, env: { ...process.env, ...env }, signal, maxBuffer: MAX_BUFFER });
1053
+ const merged: NodeJS.ProcessEnv = { ...process.env, ...env };
1054
+ for (const name of unset) delete merged[name];
1055
+ return execAsync(cmd, { cwd: dir, env: merged, signal, maxBuffer: MAX_BUFFER });
1024
1056
  }
1025
1057
 
1026
1058
  /**
@@ -1072,9 +1104,17 @@ export async function terraformInit(
1072
1104
  const rootPath = workspaceRelativePath(dir);
1073
1105
  const result: TerraformInitResult = { dir, ...(root.workspace ? { workspace: root.workspace } : {}), ...(rootPath !== undefined ? { rootPath } : {}) };
1074
1106
 
1107
+ // chant#4854: a workspace the step names is created after an init in default, which every backend lists.
1108
+ const own = rootOwnWorkspace.get(root);
1075
1109
  let out: { stdout: string; stderr: string };
1076
1110
  try {
1077
- out = await run(cmd, dir, terraformEnvironment(root), signal);
1111
+ out = await run(cmd, dir, own ? { ...terraformEnvironment(root), TF_WORKSPACE: "default" } : terraformEnvironment(root), signal);
1112
+ if (own) {
1113
+ report(out.stdout, out.stderr);
1114
+ // TF_WORKSPACE would override the select, so the binary runs it with none.
1115
+ const { TF_WORKSPACE: _selected, ...selectEnv } = terraformEnvironment(root);
1116
+ out = await run(terraformWorkspaceSelectCommand({ binary, workspace: own }), dir, selectEnv, signal, ["TF_WORKSPACE"]);
1117
+ }
1078
1118
  } catch (err) {
1079
1119
  throw withKnownOutputs(err, result);
1080
1120
  }
@@ -1351,6 +1391,7 @@ export async function choudoufuWaveRoot(
1351
1391
  const resolved = await resolveRoot(args, signal);
1352
1392
  if (resolved.binary !== "choudoufu") return { wave: false, reason: `the root runs ${resolved.binary}` };
1353
1393
  if (!resolved.live) return { wave: false, reason: "the root declares no live estate" };
1394
+ if (args.workspace) return { wave: false, reason: "the step names the root's workspace, which live-plan-set does not select or create" };
1354
1395
  if (args.vars && Object.values(args.vars).some((v) => v !== undefined)) {
1355
1396
  return { wave: false, reason: "the step passes -var values, which live-plan-set does not take" };
1356
1397
  }