@intentius/chant-lexicon-terraform 0.124.88 → 0.124.89
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/engine-runner.d.ts +86 -0
- package/dist/engine-runner.d.ts.map +1 -0
- package/dist/engine-runner.js +134 -0
- package/dist/integrity.json +2 -2
- package/dist/live-roots.d.ts +50 -0
- package/dist/live-roots.d.ts.map +1 -1
- package/dist/live-roots.js +99 -1
- package/dist/manifest.json +1 -1
- package/dist/op/command-hook.d.ts +10 -0
- package/dist/op/command-hook.d.ts.map +1 -1
- package/dist/terragrunt/run.d.ts +23 -0
- package/dist/terragrunt/run.d.ts.map +1 -1
- package/dist/terragrunt/run.js +107 -3
- package/package.json +2 -2
- package/src/engine-runner.test.ts +261 -0
- package/src/engine-runner.ts +191 -0
- package/src/live-record-store.test.ts +76 -0
- package/src/live-roots.ts +104 -1
- package/src/op/command-hook.ts +7 -0
- package/src/terragrunt/run.ts +128 -4
|
@@ -0,0 +1,76 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* A live root's record store resolved through chant (chant#4954):
|
|
3
|
+
* choudoufu's resolved store when `live-check -json` was read, else the
|
|
4
|
+
* `record_store` block the files write, and the backend block's strings for
|
|
5
|
+
* an s3 store's region and the bucket check.
|
|
6
|
+
*/
|
|
7
|
+
import { mkdirSync, mkdtempSync, writeFileSync } from "node:fs";
|
|
8
|
+
import { tmpdir } from "node:os";
|
|
9
|
+
import { join } from "node:path";
|
|
10
|
+
import { afterEach, describe, expect, test } from "vitest";
|
|
11
|
+
import { forgetLiveSetups, liveBlockText, liveRecordStore, rememberLiveSetup, rootBackendAttrs, writtenRecordStore } from "./live-roots";
|
|
12
|
+
|
|
13
|
+
afterEach(() => forgetLiveSetups());
|
|
14
|
+
|
|
15
|
+
/** A root directory holding `files`. */
|
|
16
|
+
function root(files: Record<string, string>): string {
|
|
17
|
+
const dir = mkdtempSync(join(tmpdir(), "chant-record-store-"));
|
|
18
|
+
for (const [name, text] of Object.entries(files)) {
|
|
19
|
+
mkdirSync(join(dir, name, ".."), { recursive: true });
|
|
20
|
+
writeFileSync(join(dir, name), text);
|
|
21
|
+
}
|
|
22
|
+
return dir;
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
const S3_BACKEND = 'terraform {\n backend "s3" {\n bucket = "state"\n region = "eu-west-1"\n encrypt = true\n }\n}\n';
|
|
26
|
+
|
|
27
|
+
describe("the store a root's files write", () => {
|
|
28
|
+
test("an s3 record_store block, its key_prefix, bucket and region", () => {
|
|
29
|
+
const dir = root({
|
|
30
|
+
"main.tf": 'terraform {\n live {\n estate = "prod"\n # record_store "local" {}\n record_store "s3" {\n bucket = "records"\n region = "us-east-2"\n key_prefix = "custom/prod"\n }\n }\n}\n',
|
|
31
|
+
});
|
|
32
|
+
expect(writtenRecordStore(dir, "prod")).toEqual({ kind: "s3", bucket: "records", prefix: "custom/prod/", region: "us-east-2" });
|
|
33
|
+
});
|
|
34
|
+
|
|
35
|
+
test("no record_store block is the local store beside the root, under tofu-records/<estate>/", () => {
|
|
36
|
+
const dir = root({ "estate.chdf.hcl": 'estate = "dev"\n' });
|
|
37
|
+
expect(writtenRecordStore(dir, "dev")).toEqual({ kind: "local", dir: join(dir, ".tofu-records"), prefix: "tofu-records/dev/" });
|
|
38
|
+
expect(liveBlockText(dir)).toContain('estate = "dev"');
|
|
39
|
+
});
|
|
40
|
+
|
|
41
|
+
test("an s3 store with no bucket, and a kind it does not read, are unread", () => {
|
|
42
|
+
expect(writtenRecordStore(root({ "main.tf": 'terraform {\n live {\n record_store "s3" {\n }\n }\n}\n' }), "e")).toEqual({ kind: "unread", name: "s3" });
|
|
43
|
+
expect(writtenRecordStore(root({ "main.tf": 'terraform {\n live {\n record_store "kubernetes" {\n namespace = "x"\n }\n }\n}\n' }), "e")).toEqual({ kind: "unread", name: "kubernetes" });
|
|
44
|
+
});
|
|
45
|
+
|
|
46
|
+
test("a root with no live block has no store", () => {
|
|
47
|
+
const dir = root({ "main.tf": S3_BACKEND });
|
|
48
|
+
expect(writtenRecordStore(dir, "e")).toBeUndefined();
|
|
49
|
+
expect(liveRecordStore(dir, "e")).toBeUndefined();
|
|
50
|
+
});
|
|
51
|
+
});
|
|
52
|
+
|
|
53
|
+
describe("the store choudoufu resolved", () => {
|
|
54
|
+
test("wins over the files, an s3 store taking its region from the backend block", () => {
|
|
55
|
+
const dir = root({ "main.tf": 'terraform {\n live {\n estate = "prod"\n }\n}\n', "backend.tf": S3_BACKEND });
|
|
56
|
+
rememberLiveSetup(dir, { estate: "prod", estateSource: "explicit", store: { kind: "s3", bucket: "state", prefix: "tofu-records/prod/", source: "backend" } });
|
|
57
|
+
expect(liveRecordStore(dir, "prod")).toEqual({ kind: "s3", bucket: "state", prefix: "tofu-records/prod/", region: "eu-west-1" });
|
|
58
|
+
});
|
|
59
|
+
|
|
60
|
+
test("a resolved local store is a directory under the root, and another kind is unread", () => {
|
|
61
|
+
const dir = root({ "main.tf": 'terraform {\n live {\n }\n}\n' });
|
|
62
|
+
rememberLiveSetup(dir, { estateSource: "root_path", store: { kind: "local", bucket: "", prefix: "tofu-records/x/", source: "implied" } });
|
|
63
|
+
expect(liveRecordStore(dir, "x")).toEqual({ kind: "local", dir: join(dir, ".tofu-records"), prefix: "tofu-records/x/" });
|
|
64
|
+
forgetLiveSetups();
|
|
65
|
+
rememberLiveSetup(dir, { estateSource: "root_path", store: { kind: "gcs", bucket: "b", prefix: "p/", source: "backend" } });
|
|
66
|
+
expect(liveRecordStore(dir, "x")).toEqual({ kind: "unread", name: "gcs" });
|
|
67
|
+
});
|
|
68
|
+
});
|
|
69
|
+
|
|
70
|
+
describe("the backend block's strings", () => {
|
|
71
|
+
test("are its plain string attributes, and none for a cloud block or no backend", () => {
|
|
72
|
+
expect(rootBackendAttrs(root({ "main.tf": S3_BACKEND }))).toEqual({ bucket: "state", region: "eu-west-1" });
|
|
73
|
+
expect(rootBackendAttrs(root({ "main.tf": 'terraform {\n cloud {\n organization = "o"\n }\n}\n' }))).toEqual({});
|
|
74
|
+
expect(rootBackendAttrs(root({ "main.tf": 'resource "x" "y" {}\n' }))).toEqual({});
|
|
75
|
+
});
|
|
76
|
+
});
|
package/src/live-roots.ts
CHANGED
|
@@ -17,14 +17,22 @@
|
|
|
17
17
|
* {@link checkStoreBucket} and {@link bucketLines}: the setup as a log line,
|
|
18
18
|
* and core's bucket check (`@intentius/chant/bucket-check`, chant#4558) on
|
|
19
19
|
* the resolved store.
|
|
20
|
+
* - {@link liveRecordStore}: where a live root's records are, choudoufu's
|
|
21
|
+
* resolved store over the one its files write ({@link writtenRecordStore}),
|
|
22
|
+
* and {@link rootBackendAttrs}, the backend block's strings the bucket
|
|
23
|
+
* check and an s3 store's region read (chant#4954).
|
|
20
24
|
*
|
|
21
25
|
* Text reads, as `./op/activities/live-detect.ts` does them: no HCL parser
|
|
22
26
|
* is loaded. Lifted from terragucci's `detect.ts` (`liveRoots`, `estateOf`)
|
|
23
|
-
* and `live-setup.ts`,
|
|
27
|
+
* and `live-setup.ts`, and `cdf-records.ts` (`recordStoreOf`), so
|
|
28
|
+
* terragucci deletes its copies.
|
|
24
29
|
*/
|
|
30
|
+
import { existsSync, readFileSync } from "node:fs";
|
|
25
31
|
import { join, resolve } from "node:path";
|
|
26
32
|
import { checkBucket, type BucketReport, type BucketTarget } from "@intentius/chant/bucket-check";
|
|
33
|
+
import { blankComments, blockBody, hclAttr, rootTexts } from "./compose/hcl";
|
|
27
34
|
import { declaresLive, detectLiveEstate, estateOutputReads } from "./op/activities/live-detect";
|
|
35
|
+
import { declaredBackend } from "./state/backend";
|
|
28
36
|
import { runBinary, type BinaryExec } from "./state/exec";
|
|
29
37
|
|
|
30
38
|
export { declaresLive, detectLiveEstate, estateOutputReads } from "./op/activities/live-detect";
|
|
@@ -139,6 +147,101 @@ export function resolvedEstate(dir: string, binary = "choudoufu"): string | unde
|
|
|
139
147
|
return knownLiveSetup(dir)?.estate ?? estateOf(dir, binary).estate;
|
|
140
148
|
}
|
|
141
149
|
|
|
150
|
+
/**
|
|
151
|
+
* Where a live root's records are, for a product that lists them
|
|
152
|
+
* (chant#4954): an S3 bucket, a directory, or a store it does not read
|
|
153
|
+
* (`name` is its kind). `prefix` has one trailing slash.
|
|
154
|
+
*/
|
|
155
|
+
export type RecordStore =
|
|
156
|
+
| { kind: "s3"; bucket: string; prefix: string; region?: string }
|
|
157
|
+
| { kind: "local"; dir: string; prefix: string }
|
|
158
|
+
| { kind: "unread"; name: string };
|
|
159
|
+
|
|
160
|
+
/** A key namespace with exactly one trailing slash, as choudoufu's NamespacePrefix makes it. */
|
|
161
|
+
const namespace = (prefix: string): string => `${prefix.replace(/\/+$/, "")}/`;
|
|
162
|
+
|
|
163
|
+
/**
|
|
164
|
+
* The text of a root's `live` block, comments blanked: its `estate.chdf.hcl`
|
|
165
|
+
* sidecar whole, else the `live` block in a `terraform` block of its HCL
|
|
166
|
+
* files. Undefined when it has neither.
|
|
167
|
+
*/
|
|
168
|
+
export function liveBlockText(dir: string, binary = "choudoufu"): string | undefined {
|
|
169
|
+
const sidecar = join(dir, "estate.chdf.hcl");
|
|
170
|
+
if (existsSync(sidecar)) return blankComments(readFileSync(sidecar, "utf-8"));
|
|
171
|
+
for (const { text, json } of rootTexts(dir, binary)) {
|
|
172
|
+
if (json) continue;
|
|
173
|
+
for (const t of text.matchAll(/^\s*terraform\s*\{/gm)) {
|
|
174
|
+
const body = blockBody(text, t.index!);
|
|
175
|
+
const live = body.match(/\blive\s*\{/);
|
|
176
|
+
if (live) return blockBody(body, live.index!);
|
|
177
|
+
}
|
|
178
|
+
}
|
|
179
|
+
return undefined;
|
|
180
|
+
}
|
|
181
|
+
|
|
182
|
+
/**
|
|
183
|
+
* The record store a live root's files write for `estate` (chant#4954): the
|
|
184
|
+
* `record_store "<kind>"` block in its `live` block or sidecar, its
|
|
185
|
+
* `key_prefix` (default `tofu-records/<estate>/`), an s3 store's `bucket` and
|
|
186
|
+
* `region`, a local store's `path` (default `.tofu-records`, beside the
|
|
187
|
+
* root). No `record_store` block is the local store. Undefined when the root
|
|
188
|
+
* has no live block. Lifted from terragucci's `recordStoreOf`
|
|
189
|
+
* (cdf-records.ts).
|
|
190
|
+
*/
|
|
191
|
+
export function writtenRecordStore(dir: string, estate: string, binary = "choudoufu"): RecordStore | undefined {
|
|
192
|
+
const live = liveBlockText(dir, binary);
|
|
193
|
+
if (live === undefined) return undefined;
|
|
194
|
+
const m = /\brecord_store\s+"([^"]+)"\s*\{/.exec(live);
|
|
195
|
+
const attr = m ? hclAttr(blockBody(live, m.index)) : () => undefined;
|
|
196
|
+
const str = (name: string): string | undefined => {
|
|
197
|
+
const v = attr(name);
|
|
198
|
+
return typeof v === "string" ? v : undefined;
|
|
199
|
+
};
|
|
200
|
+
const prefix = namespace(str("key_prefix") ?? `tofu-records/${estate}`);
|
|
201
|
+
const kind = m?.[1] ?? "local";
|
|
202
|
+
if (kind === "s3") {
|
|
203
|
+
const bucket = str("bucket");
|
|
204
|
+
if (!bucket) return { kind: "unread", name: "s3" };
|
|
205
|
+
const region = str("region");
|
|
206
|
+
return { kind: "s3", bucket, prefix, ...(region ? { region } : {}) };
|
|
207
|
+
}
|
|
208
|
+
if (kind === "local") return { kind: "local", dir: join(dir, str("path") ?? ".tofu-records"), prefix };
|
|
209
|
+
return { kind: "unread", name: kind };
|
|
210
|
+
}
|
|
211
|
+
|
|
212
|
+
/**
|
|
213
|
+
* The string attributes of a root's backend block as its code writes them
|
|
214
|
+
* (chant#4954), for the bucket check's s3 region and azurerm subscription
|
|
215
|
+
* and resource group. Empty for a `cloud` block or no backend. Read with
|
|
216
|
+
* {@link declaredBackend}, without the job's `-backend-config` flags.
|
|
217
|
+
*/
|
|
218
|
+
export function rootBackendAttrs(dir: string, binary = "choudoufu"): Record<string, string> {
|
|
219
|
+
const b = declaredBackend(dir, { binary, env: {} });
|
|
220
|
+
if (!b || b.type === "cloud") return {};
|
|
221
|
+
return Object.fromEntries(Object.entries(b.config).filter((e): e is [string, string] => typeof e[1] === "string"));
|
|
222
|
+
}
|
|
223
|
+
|
|
224
|
+
/**
|
|
225
|
+
* A live root's record store (chant#4954): choudoufu's resolved store when
|
|
226
|
+
* {@link readLiveSetup} read it for the root, else the one its files write
|
|
227
|
+
* ({@link writtenRecordStore}). An s3 store's region, which `live-check
|
|
228
|
+
* -json` does not print, comes from the `record_store` block, else the
|
|
229
|
+
* backend block. Undefined when the root has no live block. Lifted from
|
|
230
|
+
* terragucci's `resolvedRecordStore` (live-setup.ts).
|
|
231
|
+
*/
|
|
232
|
+
export function liveRecordStore(dir: string, estate: string, binary = "choudoufu"): RecordStore | undefined {
|
|
233
|
+
if (liveBlockText(dir, binary) === undefined) return undefined;
|
|
234
|
+
const s = knownLiveSetup(dir)?.store;
|
|
235
|
+
if (!s) return writtenRecordStore(dir, estate, binary);
|
|
236
|
+
if (s.kind === "local") return { kind: "local", dir: resolve(dir, s.bucket || ".tofu-records"), prefix: s.prefix };
|
|
237
|
+
if (s.kind === "s3" && s.bucket) {
|
|
238
|
+
const written = writtenRecordStore(dir, estate, binary);
|
|
239
|
+
const region = (written?.kind === "s3" ? written.region : undefined) ?? rootBackendAttrs(dir, binary).region;
|
|
240
|
+
return { kind: "s3", bucket: s.bucket, prefix: s.prefix, ...(region ? { region } : {}) };
|
|
241
|
+
}
|
|
242
|
+
return { kind: "unread", name: s.kind };
|
|
243
|
+
}
|
|
244
|
+
|
|
142
245
|
const STORE_FROM: Record<string, string> = {
|
|
143
246
|
record_store: "from its record_store block",
|
|
144
247
|
backend: "derived from its backend block",
|
package/src/op/command-hook.ts
CHANGED
|
@@ -48,6 +48,13 @@ export interface TerraformCommandResult {
|
|
|
48
48
|
seconds: number;
|
|
49
49
|
stdout: string;
|
|
50
50
|
stderr: string;
|
|
51
|
+
/**
|
|
52
|
+
* When it ran, in Unix milliseconds, for a command timed apart from its
|
|
53
|
+
* call (chant#4954): a Terragrunt unit's plan, from Terragrunt's run
|
|
54
|
+
* report, whose `after` is called once the whole wave ends. Unset, it ran
|
|
55
|
+
* from the hook's call to its `after`.
|
|
56
|
+
*/
|
|
57
|
+
at?: { start: number; end: number };
|
|
51
58
|
}
|
|
52
59
|
|
|
53
60
|
/** What a {@link TerraformCommandHook} answers for one command (chant#4865). */
|
package/src/terragrunt/run.ts
CHANGED
|
@@ -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
|
-
|
|
349
|
-
const
|
|
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;
|