@intentius/chant-lexicon-terraform 0.124.88 → 0.124.90

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,191 @@
1
+ /**
2
+ * One runner of the engine for a product (chant#4954): every way a product
3
+ * runs tofu, terraform, choudoufu or Terragrunt, with the forge's tokens left
4
+ * out of its environment ({@link engineEnv}), and the trace parent each
5
+ * engine command gets.
6
+ *
7
+ * - {@link engineRunner} gives the runners for one set of product token
8
+ * variables: `exec`, a `BinaryExec` whose runs go through the command hook
9
+ * of the step call they run in (`withTerraformCommandHook`, chant#4865), so
10
+ * a product's plan stage sends its own `init`, `plan` and `show` through
11
+ * the same `onCommand` as `terraform-apply`'s; `execSync`, the same run
12
+ * for a caller that cannot wait; `terragrunt`, a `TerragruntExec`;
13
+ * `script`, a same-named script that drops the tokens and runs the binary,
14
+ * for a step chant runs with the job's environment; and `env`.
15
+ * - {@link commandTrace} is a `TerraformCommandHook` that opens a span per
16
+ * engine command under the product's span for its root, and gives the
17
+ * command `TRACEPARENT` and the resource attributes that name the span
18
+ * ({@link commandTraceEnv}). Pass it as `engineTimings({ inner })`.
19
+ *
20
+ * A Terragrunt unit's plan reaches the same hook through
21
+ * `planTerragruntWave({ onCommand })` (`./terragrunt/run.ts`).
22
+ *
23
+ * Lifted from terragucci's `binary-env.ts` (`binaryEnv`, `tokenlessBinary`,
24
+ * `terragruntExec`), the runs of the binary in its `action.ts`, `apply.ts`,
25
+ * `ephemeral.ts`, `unlock.ts` and `plan.ts`, and the trace half of its
26
+ * `telemetry.ts` (`binaryEnv`) and `report/observe.ts` (`commandHook`).
27
+ */
28
+ import { execFile, spawnSync } from "node:child_process";
29
+ import { createHash } from "node:crypto";
30
+ import { accessSync, constants, existsSync, mkdirSync, statSync, writeFileSync } from "node:fs";
31
+ import { basename, delimiter, join } from "node:path";
32
+ import { FORGE_TOKEN_VARS } from "@intentius/chant/ci-handover";
33
+ import { formatOtlpPairs, parseOtlpPairs, type OtlpSpan, type OtlpTrace } from "@intentius/chant/otlp";
34
+ import { hookedBinaryExec, type TerraformCommand, type TerraformCommandHook } from "./op/command-hook";
35
+ import { engineEnv, runBinary, tokenlessBinaryExec, type BinaryExec, type Ran } from "./state/exec";
36
+ import type { TerragruntExec } from "./terragrunt/run";
37
+
38
+ export { withTerraformCommandHook, asUpstreamCommands, type TerraformCommand, type TerraformCommandHook, type TerraformCommandResult, type TerraformCommandRun } from "./op/command-hook";
39
+
40
+ /** A run of the binary that blocks: `-chdir=<dir>`, `TF_IN_AUTOMATION=1`, as {@link runBinary} runs it. */
41
+ export type BinaryExecSync = (binary: string, args: string[], dir: string, env: NodeJS.ProcessEnv) => Ran;
42
+
43
+ /** What {@link engineRunner} takes. */
44
+ export interface EngineRunnerOptions {
45
+ /** The product's own token variables, left out beside chant's forge token variables (terragucci's `TG_TOKEN`, `TG_MERGE_TOKEN`). */
46
+ tokens?: readonly string[];
47
+ /** The run under `exec`, for tests. Default {@link runBinary}. */
48
+ exec?: BinaryExec;
49
+ }
50
+
51
+ /** The product's runners of the engine, every one with the forge's tokens left out (chant#4954). */
52
+ export interface EngineRunner {
53
+ /** The token variables left out: chant's forge token variables and the product's. */
54
+ readonly tokens: readonly string[];
55
+ /** `env` less every forge token, by name and by value ({@link engineEnv}). */
56
+ env(env: NodeJS.ProcessEnv): NodeJS.ProcessEnv;
57
+ /**
58
+ * The binary, run without blocking. Inside `withTerraformCommandHook(hook,
59
+ * { root, method })` each run goes through `hook` as a command for that
60
+ * root, as `terraform-apply`'s own do; outside one it just runs.
61
+ */
62
+ exec: BinaryExec;
63
+ /** The binary, run to its end before it returns, for a caller that cannot wait. Goes through no hook. */
64
+ execSync: BinaryExecSync;
65
+ /** Terragrunt, as `planTerragruntWave` and the other Terragrunt calls take it, with the job's environment under the call's. */
66
+ terragrunt: TerragruntExec;
67
+ /**
68
+ * The path of a script named as `binary` under `dir` that leaves the forge
69
+ * tokens `env` holds out, by name and by value, then runs `binary` with
70
+ * what it was given: for a step chant runs with the job's environment
71
+ * (`terraform-apply`'s `resolveRoot`), or a `TG_TF_PATH`. A bare name is
72
+ * resolved on `env.PATH` first, so the script never runs itself. One
73
+ * script per binary and set of variables; written once.
74
+ */
75
+ script(dir: string, binary: string, env: NodeJS.ProcessEnv): string;
76
+ }
77
+
78
+ /** Whether `p` is a file this process may run. */
79
+ function runnable(p: string): boolean {
80
+ try {
81
+ accessSync(p, constants.X_OK);
82
+ return statSync(p).isFile();
83
+ } catch {
84
+ return false;
85
+ }
86
+ }
87
+
88
+ /** A word single-quoted for `sh`. */
89
+ const shq = (s: string): string => `'${s.replace(/'/g, `'\\''`)}'`;
90
+
91
+ /**
92
+ * The runners of the engine for a product whose own token variables are
93
+ * `tokens` (chant#4954). A product makes one and runs the binary and
94
+ * Terragrunt only through it.
95
+ */
96
+ export function engineRunner(options: EngineRunnerOptions = {}): EngineRunner {
97
+ const own = [...(options.tokens ?? [])];
98
+ const tokens = [...new Set([...own, ...FORGE_TOKEN_VARS])];
99
+ const env = (e: NodeJS.ProcessEnv): NodeJS.ProcessEnv => engineEnv(e, own);
100
+ // The hook's variables are laid over the run's before the tokens are taken out, so no hook puts one back.
101
+ const exec = hookedBinaryExec(tokenlessBinaryExec(own, options.exec ?? runBinary));
102
+ const execSync: BinaryExecSync = (binary, args, dir, given) => {
103
+ const p = spawnSync(binary, [`-chdir=${dir}`, ...args], { encoding: "utf-8", env: { ...env(given), TF_IN_AUTOMATION: "1" }, maxBuffer: 512 * 1024 * 1024 });
104
+ const stdout = p.stdout ?? "";
105
+ return { code: p.error && p.status === null ? 127 : (p.status ?? 1), stdout, out: `${stdout}${p.stderr ?? ""}${p.error ? p.error.message : ""}` };
106
+ };
107
+ const terragrunt: TerragruntExec = (file, args, opts) =>
108
+ new Promise((done) => {
109
+ execFile(file, [...args], { cwd: opts.cwd, env: env({ ...process.env, ...opts.env }), maxBuffer: 256 * 1024 * 1024 }, (err, stdout, stderr) => {
110
+ const code = err ? (typeof (err as { code?: unknown }).code === "number" ? (err as { code: number }).code : null) : 0;
111
+ done({ code, stdout: String(stdout), stderr: err && code === null ? `${String(stderr)}${err.message}` : String(stderr) });
112
+ });
113
+ });
114
+ const script = (dir: string, given: string, e: NodeJS.ProcessEnv): string => {
115
+ const binary = given.includes("/") ? given : ((e.PATH ?? "").split(delimiter).map((d) => join(d, given)).find((p) => runnable(p)) ?? given);
116
+ const kept = env(e);
117
+ const drop = [...new Set([...tokens, ...Object.keys(e).filter((k) => !(k in kept))])].filter((k) => /^[A-Za-z_][A-Za-z0-9_]*$/.test(k)).sort();
118
+ const at = join(dir, createHash("sha256").update(`${binary}\n${drop.join(" ")}`).digest("hex").slice(0, 16));
119
+ const path = join(at, basename(binary) || "terraform");
120
+ if (!existsSync(path)) {
121
+ mkdirSync(at, { recursive: true });
122
+ writeFileSync(path, `#!/bin/sh\nexec env ${drop.map((k) => `-u ${k}`).join(" ")} ${shq(binary)} "$@"\n`, { mode: 0o755 });
123
+ }
124
+ return path;
125
+ };
126
+ return { tokens, env, exec, execSync, terragrunt, script };
127
+ }
128
+
129
+ /**
130
+ * The variables a command run inside `span` gets (chant#4954): `TRACEPARENT`,
131
+ * so the engine's spans join the trace where it reads it (OpenTofu and
132
+ * choudoufu), the trace's and span's ids and the root as resource attributes
133
+ * (`<prefix>.trace_id`, `<prefix>.span_id`, `<prefix>.root`) over the
134
+ * `OTEL_RESOURCE_ATTRIBUTES` `env` has, so a binary that ignores
135
+ * `TRACEPARENT` still names the span that ran it, and `OTEL_TRACES_EXPORTER`
136
+ * `otlp` unless `env` chose one (`none` keeps it off). Lifted from
137
+ * terragucci's `binaryEnv` (telemetry.ts).
138
+ */
139
+ export function commandTraceEnv(env: NodeJS.ProcessEnv, trace: OtlpTrace, span: OtlpSpan, options: { prefix: string; root: string }): Record<string, string> {
140
+ const resource = {
141
+ ...parseOtlpPairs(env.OTEL_RESOURCE_ATTRIBUTES),
142
+ [`${options.prefix}.trace_id`]: trace.traceId,
143
+ [`${options.prefix}.span_id`]: span.spanId,
144
+ [`${options.prefix}.root`]: options.root,
145
+ };
146
+ return {
147
+ TRACEPARENT: trace.traceparent(span),
148
+ OTEL_TRACES_EXPORTER: env.OTEL_TRACES_EXPORTER || "otlp",
149
+ OTEL_RESOURCE_ATTRIBUTES: formatOtlpPairs(resource),
150
+ };
151
+ }
152
+
153
+ /** What {@link commandTrace} takes. */
154
+ export interface CommandTraceOptions {
155
+ /** The run's trace. */
156
+ trace: OtlpTrace;
157
+ /** The product, which names the resource attributes (`terragucci.trace_id`). */
158
+ prefix: string;
159
+ /** The span a command's span hangs under, such as the product's span for its root. Undefined, or no function, hangs it under the trace's parent. */
160
+ parent?: (command: TerraformCommand) => OtlpSpan | undefined;
161
+ /** The environment the commands run over, whose `OTEL_RESOURCE_ATTRIBUTES` and `OTEL_TRACES_EXPORTER` are kept. Default `process.env`. */
162
+ env?: NodeJS.ProcessEnv;
163
+ /** Trace an upstream's reads for a root too. Default false: they are the upstream's, not the root's. */
164
+ upstreams?: boolean;
165
+ }
166
+
167
+ /**
168
+ * A command hook that traces each engine command (chant#4954): a span
169
+ * `<binary> <name>` (kind client, with `process.executable.name` and
170
+ * `process.command_args`) under `parent(command)`, the variables
171
+ * {@link commandTraceEnv} gives for it, and the span ended with
172
+ * `process.exit.code` once the command ends, failed unless it exited 0 (or 2
173
+ * for a `plan`, `-detailed-exitcode`'s changes). A command timed apart from
174
+ * its call (`at`, a Terragrunt unit's plan) spans the time it ran. Pass it as
175
+ * `engineTimings({ inner })`, so the engine's spans join the trace and reach
176
+ * the timings' receiver.
177
+ */
178
+ export function commandTrace(options: CommandTraceOptions): TerraformCommandHook {
179
+ return (c) => {
180
+ if (c.upstream !== undefined && !options.upstreams) return;
181
+ const span = options.trace.start(`${c.binary} ${c.name}`, options.parent?.(c), { "process.executable.name": c.binary, "process.command_args": c.args.join(" ") }, { kind: 3 });
182
+ return {
183
+ env: commandTraceEnv({ ...(options.env ?? process.env), ...c.env }, options.trace, span, { prefix: options.prefix, root: c.root }),
184
+ after: (r) => {
185
+ if (r.at) span.start = BigInt(Math.round(r.at.start)) * 1_000_000n;
186
+ const ok = r.code === 0 || (c.name === "plan" && r.code === 2);
187
+ options.trace.end(span, { "process.exit.code": r.code ?? -1 }, ok ? undefined : `exit ${r.code}`, r.at?.end);
188
+ },
189
+ };
190
+ };
191
+ }
@@ -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`, so terragucci deletes its copies.
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",
@@ -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). */
@@ -0,0 +1,72 @@
1
+ /**
2
+ * What a change reaches (#4984), moved from terragucci's comment-apply.ts:
3
+ * roots with the reads the product hands, Terragrunt units from their
4
+ * plain dependency paths at both ends of the range, Atmos instances by
5
+ * component, and synthesized roots.
6
+ */
7
+
8
+ import { mkdirSync, mkdtempSync, writeFileSync } from "node:fs";
9
+ import { tmpdir } from "node:os";
10
+ import { join } from "node:path";
11
+ import { describe, expect, test } from "vitest";
12
+ import { literalTerragruntDependencies, reachedInstances, reachedRoots, reachedSynthesized, reachedUnits, type ReachGit } from "./reach";
13
+
14
+ /** Git that answers the diff with `files` and each `show <ref>:<path>` from `shows`. */
15
+ const fakeGit = (files: string[] | null, shows: Record<string, string> = {}): ReachGit => (args) => {
16
+ if (args[0] === "diff") return files === null ? { status: 128, stdout: "", stderr: "bad range" } : { status: 0, stdout: files.join("\n"), stderr: "" };
17
+ if (args[0] === "show") {
18
+ const text = shows[args[1]!.replace(/^[^:]*:/, "")];
19
+ return text === undefined ? { status: 128, stdout: "", stderr: "no such path" } : { status: 0, stdout: text, stderr: "" };
20
+ }
21
+ return { status: 1, stdout: "", stderr: "unexpected" };
22
+ };
23
+
24
+ describe("reachedRoots", () => {
25
+ const repo = mkdtempSync(join(tmpdir(), "chant-reach-"));
26
+ for (const r of ["net", "app", "dns"]) {
27
+ mkdirSync(join(repo, r), { recursive: true });
28
+ writeFileSync(join(repo, r, "main.tf"), "");
29
+ }
30
+ const reads = new Map([["app", new Set(["net"])], ["dns", new Set(["app"])]]);
31
+
32
+ test("a changed root, and every root that reads it, followed through", () => {
33
+ expect(reachedRoots(repo, fakeGit(["net/main.tf"]), "a", "b", ["net", "app", "dns"], reads)).toEqual({ targets: ["app", "dns", "net"] });
34
+ expect(reachedRoots(repo, fakeGit(["dns/main.tf"]), "a", "b", ["net", "app", "dns"], reads)).toEqual({ targets: ["dns"] });
35
+ });
36
+
37
+ test("every root when git cannot diff the range", () => {
38
+ expect(reachedRoots(repo, fakeGit(null), "a", "b", ["net", "app"])).toEqual({ targets: ["app", "net"], every: "git could not list the files it changes" });
39
+ });
40
+ });
41
+
42
+ describe("reachedUnits", () => {
43
+ const hcl = (dep: string) => `dependency "up" {\n config_path = "${dep}"\n}\n`;
44
+
45
+ test("a changed unit and the units that name it, at either end of the range", () => {
46
+ const git = fakeGit(["live/vpc/main.tf"], { "live/app/terragrunt.hcl": hcl("../vpc"), "live/vpc/terragrunt.hcl": "" });
47
+ expect(reachedUnits(git, "a", "b", ["live/vpc", "live/app", "live/dns"])).toEqual({ targets: ["live/app", "live/vpc"] });
48
+ });
49
+
50
+ test("a file in no unit's directory reaches every unit, and Markdown reaches nothing", () => {
51
+ expect(reachedUnits(fakeGit(["root.hcl"]), "a", "b", ["x", "y"])).toEqual({ targets: ["x", "y"], every: "it changes `root.hcl`, which is in no unit's directory" });
52
+ expect(reachedUnits(fakeGit(["README.md"]), "a", "b", ["x", "y"])).toEqual({ targets: [] });
53
+ });
54
+
55
+ test("only plain dependency paths are read", () => {
56
+ expect(literalTerragruntDependencies(`${'dependency "a" {\n config_path = "../a"\n}\n'}dependencies {\n paths = ["../b", "\${local.c}"]\n}\n# dependency "z" { config_path = "../z" }`)).toEqual(["../a", "../b"]);
57
+ });
58
+ });
59
+
60
+ describe("reachedInstances and reachedSynthesized", () => {
61
+ test("an instance by its component, the ones that read it, and a manifest that reaches every one", () => {
62
+ const componentOf = new Map([["vpc-prod", "components/terraform/vpc"], ["app-prod", "components/terraform/app"]]);
63
+ const reads = new Map([["app-prod", new Set(["vpc-prod"])]]);
64
+ expect(reachedInstances(fakeGit(["components/terraform/vpc/main.tf"]), "a", "b", ["vpc-prod", "app-prod"], componentOf, reads)).toEqual({ targets: ["app-prod", "vpc-prod"] });
65
+ expect(reachedInstances(fakeGit(["stacks/prod.yaml"]), "a", "b", ["vpc-prod", "app-prod"], componentOf).every).toBe("it changes `stacks/prod.yaml`, which can change any Atmos instance");
66
+ });
67
+
68
+ test("synthesized roots: any file but Markdown reaches every root", () => {
69
+ expect(reachedSynthesized(fakeGit(["docs/a.md"]), "a", "b", ["s1", "s2"])).toEqual({ targets: [] });
70
+ expect(reachedSynthesized(fakeGit(["main.ts"]), "a", "b", ["s2", "s1"])).toEqual({ targets: ["s1", "s2"], every: "it changes `main.ts`, and the synth command writes every root from the repo's files" });
71
+ });
72
+ });