@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
package/dist/terragrunt/run.js
CHANGED
|
@@ -6,8 +6,8 @@
|
|
|
6
6
|
* acceptance test runs the real binary.
|
|
7
7
|
*/
|
|
8
8
|
import { execFile } from "node:child_process";
|
|
9
|
-
import { existsSync, mkdirSync, readFileSync, realpathSync, rmSync, writeFileSync } from "node:fs";
|
|
10
|
-
import { dirname, join, resolve } from "node:path";
|
|
9
|
+
import { chmodSync, existsSync, mkdirSync, readFileSync, realpathSync, rmSync, writeFileSync } from "node:fs";
|
|
10
|
+
import { basename, dirname, join, resolve } from "node:path";
|
|
11
11
|
import { plannerForBinary } from "../change-set.js";
|
|
12
12
|
import { parseTerragruntFind, terragruntFindArgs, terragruntVersionProblem, unitPathFilter, } from "./units.js";
|
|
13
13
|
import { describeMockRefusal, failMockedParts, hasExpandedDependency, markProvisional, parseRenderedDependencies, parseTerragruntOutputs, PROVISIONAL_MARKER, terragruntMockReads, terragruntMockWarnings, terragruntOutputArgs, terragruntOutputUpstreams, terragruntRenderArgs, } from "./mocks.js";
|
|
@@ -92,6 +92,100 @@ export function terraformBinaryWarnings(dir, units, binary) {
|
|
|
92
92
|
export function terragruntPlanner(binary) {
|
|
93
93
|
return plannerForBinary(binary ?? "tofu");
|
|
94
94
|
}
|
|
95
|
+
/** A word single-quoted for `sh`. */
|
|
96
|
+
const shq = (s) => `'${s.replace(/'/g, `'\\''`)}'`;
|
|
97
|
+
/**
|
|
98
|
+
* The `TG_TF_PATH` wrapper for one wave (chant#4954): a shell script that
|
|
99
|
+
* runs `binary` with the variables of the unit whose plan it runs.
|
|
100
|
+
* Terragrunt hands each unit's plan `-out=<out-dir>/<unit>/tfplan.tfplan`,
|
|
101
|
+
* which names the unit; every other call (init, show, output, version) runs
|
|
102
|
+
* the binary as it is. `outDirs` are the wave's plan directory as given and
|
|
103
|
+
* as its real path. Lifted from terragucci's `unitWrapper` (report/stage.ts).
|
|
104
|
+
*/
|
|
105
|
+
export function terragruntUnitWrapper(binary, outDirs, units) {
|
|
106
|
+
const lines = [
|
|
107
|
+
"#!/bin/sh",
|
|
108
|
+
"# chant: each unit's plan runs with the variables its command hook answered. Written for one wave.",
|
|
109
|
+
'out=""',
|
|
110
|
+
'prev=""',
|
|
111
|
+
'for a in "$@"; do',
|
|
112
|
+
' case "$a" in -out=*|--out=*) out="${a#*=}" ;; esac',
|
|
113
|
+
' if [ "$prev" = "-out" ] || [ "$prev" = "--out" ]; then out="$a"; fi',
|
|
114
|
+
' prev="$a"',
|
|
115
|
+
"done",
|
|
116
|
+
'case "$out" in',
|
|
117
|
+
];
|
|
118
|
+
for (const [unit, vars] of units) {
|
|
119
|
+
const set = Object.entries(vars).filter(([k]) => /^[A-Za-z_][A-Za-z0-9_]*$/.test(k));
|
|
120
|
+
if (set.length === 0)
|
|
121
|
+
continue;
|
|
122
|
+
const patterns = [...new Set(outDirs.map((d) => join(d, unit, "tfplan.tfplan")))].map(shq).join(" | ");
|
|
123
|
+
lines.push(` ${patterns}) export ${set.map(([k, v]) => `${k}=${shq(v)}`).join(" ")} ;;`);
|
|
124
|
+
}
|
|
125
|
+
lines.push("esac", `exec ${shq(binary)} "$@"`);
|
|
126
|
+
return lines.join("\n") + "\n";
|
|
127
|
+
}
|
|
128
|
+
/**
|
|
129
|
+
* Ask `hook` for each unit's plan before the wave runs (chant#4954), and
|
|
130
|
+
* write the wrapper that hands each its variables when any has some.
|
|
131
|
+
* Answers the variables the run adds and what to call once the run report
|
|
132
|
+
* is read.
|
|
133
|
+
*/
|
|
134
|
+
async function unitCommandRuns(hook, input, work, outDir, waveArgs) {
|
|
135
|
+
const units = [...new Set(input.units)].sort();
|
|
136
|
+
const args = waveArgs.slice(waveArgs.indexOf("--") + 1);
|
|
137
|
+
const runs = new Map();
|
|
138
|
+
for (const unit of units) {
|
|
139
|
+
const command = {
|
|
140
|
+
root: unit,
|
|
141
|
+
method: "plan",
|
|
142
|
+
binary: input.binary,
|
|
143
|
+
name: "plan",
|
|
144
|
+
args: [...args],
|
|
145
|
+
line: [input.binary, ...args].map((w) => (/^[\w@%+=:,./-]+$/.test(w) ? w : shq(w))).join(" "),
|
|
146
|
+
cwd: resolve(input.dir, unit),
|
|
147
|
+
env: {},
|
|
148
|
+
};
|
|
149
|
+
runs.set(unit, (await hook(command)) ?? {});
|
|
150
|
+
}
|
|
151
|
+
const vars = new Map([...runs].map(([unit, run]) => [unit, run.env ?? {}]));
|
|
152
|
+
let env = {};
|
|
153
|
+
if ([...vars.values()].some((v) => Object.keys(v).length > 0)) {
|
|
154
|
+
let real = outDir;
|
|
155
|
+
try {
|
|
156
|
+
real = join(realpathSync(work), "plans");
|
|
157
|
+
}
|
|
158
|
+
catch {
|
|
159
|
+
// The path as given is matched alone.
|
|
160
|
+
}
|
|
161
|
+
const bin = join(work, "unit-bin");
|
|
162
|
+
mkdirSync(bin, { recursive: true });
|
|
163
|
+
// The wrapper carries the binary's name, which Terragrunt prefixes its log lines with.
|
|
164
|
+
const wrapper = join(bin, basename(input.binary) || "tofu");
|
|
165
|
+
writeFileSync(wrapper, terragruntUnitWrapper(input.binary, [outDir, real], vars));
|
|
166
|
+
chmodSync(wrapper, 0o755);
|
|
167
|
+
env = { TG_TF_PATH: wrapper };
|
|
168
|
+
}
|
|
169
|
+
const done = async (report) => {
|
|
170
|
+
for (const [unit, run] of runs) {
|
|
171
|
+
if (!run.after)
|
|
172
|
+
continue;
|
|
173
|
+
const row = report.get(unit);
|
|
174
|
+
const start = row?.started ? Date.parse(row.started) : NaN;
|
|
175
|
+
const end = row?.ended ? Date.parse(row.ended) : NaN;
|
|
176
|
+
const timed = Number.isFinite(start) && Number.isFinite(end) && end >= start;
|
|
177
|
+
const why = row && row.result !== "succeeded" ? [row.result, row.reason, row.cause].filter(Boolean).join(": ") : "";
|
|
178
|
+
await run.after({
|
|
179
|
+
code: row ? (row.result === "succeeded" ? 0 : 1) : null,
|
|
180
|
+
seconds: timed ? (end - start) / 1000 : 0,
|
|
181
|
+
stdout: "",
|
|
182
|
+
stderr: row ? why : "not in Terragrunt's run report, so it did not run",
|
|
183
|
+
...(timed ? { at: { start, end } } : {}),
|
|
184
|
+
});
|
|
185
|
+
}
|
|
186
|
+
};
|
|
187
|
+
return { env, done };
|
|
188
|
+
}
|
|
95
189
|
/**
|
|
96
190
|
* Thrown when a wave's plans would read `mock_outputs` (#3416). Nothing in
|
|
97
191
|
* the wave was planned, so there is nothing to gate. `reads` names each unit,
|
|
@@ -242,8 +336,18 @@ export async function planTerragruntWave(input) {
|
|
|
242
336
|
...(input.destroy ? { destroy: true } : {}),
|
|
243
337
|
...(input.parallelism !== undefined ? { parallelism: input.parallelism } : {}),
|
|
244
338
|
});
|
|
245
|
-
|
|
339
|
+
// chant#4954: each unit's plan goes through the product's hook, its variables through a TG_TF_PATH wrapper.
|
|
340
|
+
const units = input.onCommand && input.binary ? await unitCommandRuns(input.onCommand, { ...input, binary: input.binary }, work, outDir, args) : undefined;
|
|
341
|
+
let r;
|
|
342
|
+
try {
|
|
343
|
+
r = await exec(tg, args, { cwd: input.dir, env: { ...terragruntEnv(input.binary), ...(units?.env ?? {}) } });
|
|
344
|
+
}
|
|
345
|
+
catch (err) {
|
|
346
|
+
await units?.done(new Map());
|
|
347
|
+
throw err;
|
|
348
|
+
}
|
|
246
349
|
const report = existsSync(reportFile) ? parseTerragruntReport(readFileSync(reportFile, "utf8")) : new Map();
|
|
350
|
+
await units?.done(report);
|
|
247
351
|
const planFor = (unit) => {
|
|
248
352
|
const f = join(jsonOutDir, unit, "tfplan.json");
|
|
249
353
|
return existsSync(f) ? JSON.parse(readFileSync(f, "utf8")) : undefined;
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@intentius/chant-lexicon-terraform",
|
|
3
|
-
"version": "0.124.
|
|
3
|
+
"version": "0.124.89",
|
|
4
4
|
"type": "module",
|
|
5
5
|
"files": [
|
|
6
6
|
"src/",
|
|
@@ -93,7 +93,7 @@
|
|
|
93
93
|
"access": "public"
|
|
94
94
|
},
|
|
95
95
|
"peerDependencies": {
|
|
96
|
-
"@intentius/chant": "^0.124.
|
|
96
|
+
"@intentius/chant": "^0.124.89",
|
|
97
97
|
"zod": "^4.3.6"
|
|
98
98
|
},
|
|
99
99
|
"description": "terraform lexicon for chant, existing Terraform root modules as declarable entities",
|
|
@@ -0,0 +1,261 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* One runner of the engine for a product (chant#4954): a plan stage's own
|
|
3
|
+
* commands and each Terragrunt unit's plan reach the same `onCommand` as
|
|
4
|
+
* `terraform-apply`'s, the trace parent per command is the lexicon's, and
|
|
5
|
+
* every run (async, blocking, Terragrunt, the script chant's step runs)
|
|
6
|
+
* leaves the forge's tokens out. Fake binaries are small `sh` scripts in a
|
|
7
|
+
* temporary directory; nothing reaches a cloud, a forge or a collector.
|
|
8
|
+
*/
|
|
9
|
+
import { spawnSync } from "node:child_process";
|
|
10
|
+
import { chmodSync, mkdirSync, mkdtempSync, readFileSync, writeFileSync } from "node:fs";
|
|
11
|
+
import { tmpdir } from "node:os";
|
|
12
|
+
import { dirname, join } from "node:path";
|
|
13
|
+
import { describe, expect, test } from "vitest";
|
|
14
|
+
import { OtlpTrace } from "@intentius/chant/otlp";
|
|
15
|
+
import { commandTrace, commandTraceEnv, engineRunner, withTerraformCommandHook, type TerraformCommand, type TerraformCommandResult } from "./engine-runner";
|
|
16
|
+
import { engineTimings } from "./engine-timings";
|
|
17
|
+
import type { BinaryExec } from "./state/exec";
|
|
18
|
+
import { planTerragruntWave, terragruntUnitWrapper, type TerragruntExec } from "./terragrunt/run";
|
|
19
|
+
|
|
20
|
+
const tmp = (): string => mkdtempSync(join(tmpdir(), "chant-engine-runner-"));
|
|
21
|
+
|
|
22
|
+
/** An executable `sh` script at `path`. */
|
|
23
|
+
function script(path: string, body: string): string {
|
|
24
|
+
mkdirSync(dirname(path), { recursive: true });
|
|
25
|
+
writeFileSync(path, `#!/bin/sh\n${body}\n`);
|
|
26
|
+
chmodSync(path, 0o755);
|
|
27
|
+
return path;
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
const JOB = { PATH: process.env.PATH ?? "/usr/bin:/bin", GITHUB_TOKEN: "ghs_forge", TG_TOKEN: "tg-secret", COPY: "tg-secret", TF_VAR_token: "tg-secret", KEEP: "yes" };
|
|
31
|
+
|
|
32
|
+
describe("the plan stage's own commands", () => {
|
|
33
|
+
test("go through the command hook of the call they run in, as commands for its root", async () => {
|
|
34
|
+
const seen: TerraformCommand[] = [];
|
|
35
|
+
const ran: Array<{ args: string[]; env: NodeJS.ProcessEnv }> = [];
|
|
36
|
+
const fake: BinaryExec = async (_b, args, _d, env) => {
|
|
37
|
+
ran.push({ args, env });
|
|
38
|
+
return { code: args[0] === "plan" ? 2 : 0, stdout: "out", out: "outerr" };
|
|
39
|
+
};
|
|
40
|
+
const results: TerraformCommandResult[] = [];
|
|
41
|
+
const runner = engineRunner({ tokens: ["TG_TOKEN"], exec: fake });
|
|
42
|
+
await withTerraformCommandHook(
|
|
43
|
+
(c) => {
|
|
44
|
+
seen.push(c);
|
|
45
|
+
// A hook that names a token gets it taken out all the same.
|
|
46
|
+
return { env: { HOOKED: c.name, GITHUB_TOKEN: "put-back" }, after: (r) => void results.push(r) };
|
|
47
|
+
},
|
|
48
|
+
{ root: "roots/a", method: "plan" },
|
|
49
|
+
async () => {
|
|
50
|
+
await runner.exec("tofu", ["init", "-input=false"], "/w/roots/a", { ...JOB });
|
|
51
|
+
await runner.exec("tofu", ["plan", "-detailed-exitcode"], "/w/roots/a", { ...JOB });
|
|
52
|
+
},
|
|
53
|
+
);
|
|
54
|
+
expect(seen.map((c) => [c.root, c.method, c.name])).toEqual([
|
|
55
|
+
["roots/a", "plan", "init"],
|
|
56
|
+
["roots/a", "plan", "plan"],
|
|
57
|
+
]);
|
|
58
|
+
expect(ran[1]!.env.HOOKED).toBe("plan");
|
|
59
|
+
expect(ran[1]!.env.GITHUB_TOKEN).toBeUndefined();
|
|
60
|
+
expect(ran[1]!.env.TG_TOKEN).toBeUndefined();
|
|
61
|
+
expect(ran[1]!.env.COPY).toBeUndefined();
|
|
62
|
+
expect(ran[1]!.env.TF_VAR_token).toBe("tg-secret");
|
|
63
|
+
expect(results.map((r) => r.code)).toEqual([0, 2]);
|
|
64
|
+
expect(results[1]).toMatchObject({ stdout: "out", stderr: "err" });
|
|
65
|
+
});
|
|
66
|
+
|
|
67
|
+
test("outside a hook's call the binary just runs, its tokens left out", async () => {
|
|
68
|
+
const ran: NodeJS.ProcessEnv[] = [];
|
|
69
|
+
const runner = engineRunner({ tokens: ["TG_TOKEN"], exec: async (_b, _a, _d, env) => (ran.push(env), { code: 0, stdout: "", out: "" }) });
|
|
70
|
+
await runner.exec("tofu", ["version"], "/w", { ...JOB });
|
|
71
|
+
expect(ran[0]!.KEEP).toBe("yes");
|
|
72
|
+
expect(ran[0]!.TG_TOKEN).toBeUndefined();
|
|
73
|
+
expect(runner.tokens).toEqual(expect.arrayContaining(["TG_TOKEN", "GITHUB_TOKEN"]));
|
|
74
|
+
});
|
|
75
|
+
});
|
|
76
|
+
|
|
77
|
+
describe("the blocking, Terragrunt and script runs", () => {
|
|
78
|
+
test("execSync runs the binary in its directory with TF_IN_AUTOMATION and no tokens", () => {
|
|
79
|
+
const dir = tmp();
|
|
80
|
+
const bin = script(join(dir, "bin/tofu"), 'echo "args=$*"\necho "tok=${GITHUB_TOKEN:-none} copy=${COPY:-none} tfvar=${TF_VAR_token:-none} auto=$TF_IN_AUTOMATION"\necho oops >&2\nexit 3');
|
|
81
|
+
const r = engineRunner({ tokens: ["TG_TOKEN"] }).execSync(bin, ["plan"], "/w/roots/a", { ...JOB });
|
|
82
|
+
expect(r.code).toBe(3);
|
|
83
|
+
expect(r.stdout).toContain("args=-chdir=/w/roots/a plan");
|
|
84
|
+
expect(r.stdout).toContain("tok=none copy=none tfvar=tg-secret auto=1");
|
|
85
|
+
expect(r.out).toContain("oops");
|
|
86
|
+
});
|
|
87
|
+
|
|
88
|
+
test("execSync answers 127 for a binary that is not there", () => {
|
|
89
|
+
const r = engineRunner().execSync(join(tmp(), "missing"), ["version"], "/w", { ...JOB });
|
|
90
|
+
expect(r.code).toBe(127);
|
|
91
|
+
expect(r.out).toMatch(/ENOENT/);
|
|
92
|
+
});
|
|
93
|
+
|
|
94
|
+
test("terragrunt runs with the job's environment under the call's, less the tokens", async () => {
|
|
95
|
+
const dir = tmp();
|
|
96
|
+
const tg = script(join(dir, "terragrunt"), 'echo "pwd=$(pwd) tok=${GITHUB_TOKEN:-none} copy=${COPY:-none} call=$CALL"');
|
|
97
|
+
const saved = { ...process.env };
|
|
98
|
+
Object.assign(process.env, { GITHUB_TOKEN: "ghs_forge", TG_TOKEN: "tg-secret", COPY: "tg-secret" });
|
|
99
|
+
try {
|
|
100
|
+
const r = await engineRunner({ tokens: ["TG_TOKEN"] }).terragrunt(tg, ["find"], { cwd: dir, env: { CALL: "given" } });
|
|
101
|
+
expect(r.code).toBe(0);
|
|
102
|
+
expect(r.stdout).toContain("tok=none copy=none call=given");
|
|
103
|
+
} finally {
|
|
104
|
+
for (const k of ["GITHUB_TOKEN", "TG_TOKEN", "COPY"]) if (saved[k] === undefined) delete process.env[k];
|
|
105
|
+
}
|
|
106
|
+
});
|
|
107
|
+
|
|
108
|
+
test("script writes one same-named script that drops the tokens, by name and by value, and runs the binary", () => {
|
|
109
|
+
const dir = tmp();
|
|
110
|
+
const bin = script(join(dir, "real/tofu"), 'echo "tok=${GITHUB_TOKEN:-none} tg=${TG_TOKEN:-none} copy=${COPY:-none} tfvar=${TF_VAR_token:-none} keep=$KEEP args=$*"');
|
|
111
|
+
const runner = engineRunner({ tokens: ["TG_TOKEN"] });
|
|
112
|
+
const path = runner.script(join(dir, "scripts"), bin, { ...JOB });
|
|
113
|
+
expect(path.endsWith("/tofu")).toBe(true);
|
|
114
|
+
expect(runner.script(join(dir, "scripts"), bin, { ...JOB })).toBe(path);
|
|
115
|
+
const r = spawnSync(path, ["plan"], { encoding: "utf-8", env: { ...JOB } });
|
|
116
|
+
expect(r.stdout.trim()).toBe("tok=none tg=none copy=none tfvar=tg-secret keep=yes args=plan");
|
|
117
|
+
});
|
|
118
|
+
|
|
119
|
+
test("script finds a bare name on PATH, so it never runs itself", () => {
|
|
120
|
+
const dir = tmp();
|
|
121
|
+
script(join(dir, "real/tofu"), "echo real");
|
|
122
|
+
const env = { ...JOB, PATH: `${join(dir, "scripts-first")}:${join(dir, "real")}:/usr/bin:/bin` };
|
|
123
|
+
const path = engineRunner().script(join(dir, "scripts"), "tofu", env);
|
|
124
|
+
expect(readFileSync(path, "utf-8")).toContain(`'${join(dir, "real/tofu")}'`);
|
|
125
|
+
});
|
|
126
|
+
});
|
|
127
|
+
|
|
128
|
+
describe("the trace parent per command", () => {
|
|
129
|
+
test("commandTraceEnv gives TRACEPARENT and names the span, keeping the job's resource attributes and exporter", () => {
|
|
130
|
+
const trace = new OtlpTrace();
|
|
131
|
+
const span = trace.start("root roots/a");
|
|
132
|
+
const env = commandTraceEnv({ OTEL_RESOURCE_ATTRIBUTES: "team=infra", OTEL_TRACES_EXPORTER: "none" }, trace, span, { prefix: "terragucci", root: "roots/a" });
|
|
133
|
+
expect(env.TRACEPARENT).toBe(`00-${trace.traceId}-${span.spanId}-01`);
|
|
134
|
+
expect(env.OTEL_TRACES_EXPORTER).toBe("none");
|
|
135
|
+
expect(env.OTEL_RESOURCE_ATTRIBUTES).toContain("team=infra");
|
|
136
|
+
expect(env.OTEL_RESOURCE_ATTRIBUTES).toContain(`terragucci.span_id=${span.spanId}`);
|
|
137
|
+
expect(env.OTEL_RESOURCE_ATTRIBUTES).toContain("terragucci.root=roots%2Fa");
|
|
138
|
+
expect(commandTraceEnv({}, trace, span, { prefix: "p", root: "r" }).OTEL_TRACES_EXPORTER).toBe("otlp");
|
|
139
|
+
});
|
|
140
|
+
|
|
141
|
+
test("commandTrace opens a span per command under the root's, and ends it with the exit code", async () => {
|
|
142
|
+
const trace = new OtlpTrace();
|
|
143
|
+
const root = trace.start("root roots/a");
|
|
144
|
+
const hook = commandTrace({ trace, prefix: "terragucci", parent: (c) => (c.root === "roots/a" ? root : undefined), env: {} });
|
|
145
|
+
const runner = engineRunner({ exec: async (_b, args) => ({ code: args[0] === "plan" ? 2 : args[0] === "apply" ? 1 : 0, stdout: "", out: "" }) });
|
|
146
|
+
const envs: string[] = [];
|
|
147
|
+
await withTerraformCommandHook(
|
|
148
|
+
async (c) => {
|
|
149
|
+
const r = await hook(c);
|
|
150
|
+
if (r?.env) envs.push(r.env.TRACEPARENT!);
|
|
151
|
+
return r;
|
|
152
|
+
},
|
|
153
|
+
{ root: "roots/a", method: "plan" },
|
|
154
|
+
async () => {
|
|
155
|
+
await runner.exec("tofu", ["plan"], "/w", {});
|
|
156
|
+
await runner.exec("tofu", ["apply"], "/w", {});
|
|
157
|
+
},
|
|
158
|
+
);
|
|
159
|
+
const spans = trace.spans.filter((s) => s !== root);
|
|
160
|
+
expect(spans.map((s) => [s.name, s.parentSpanId, s.kind, s.attributes["process.exit.code"], s.error])).toEqual([
|
|
161
|
+
["tofu plan", root.spanId, 3, 2, undefined],
|
|
162
|
+
["tofu apply", root.spanId, 3, 1, "exit 1"],
|
|
163
|
+
]);
|
|
164
|
+
expect(envs).toEqual(spans.map((s) => trace.traceparent(s)));
|
|
165
|
+
});
|
|
166
|
+
|
|
167
|
+
test("an upstream's reads are not traced unless asked, and a command timed apart spans its own time", async () => {
|
|
168
|
+
const trace = new OtlpTrace();
|
|
169
|
+
const hook = commandTrace({ trace, prefix: "p", env: {} });
|
|
170
|
+
const base = { root: "roots/a", method: "plan", binary: "tofu", name: "init", args: ["init"], line: "tofu init", cwd: "/w", env: {} };
|
|
171
|
+
expect(await hook({ ...base, upstream: "roots/up" })).toBeUndefined();
|
|
172
|
+
const run = await hook({ ...base, name: "plan", args: ["plan"] });
|
|
173
|
+
await run!.after!({ code: 0, seconds: 2, stdout: "", stderr: "", at: { start: 1_700_000_000_000, end: 1_700_000_002_000 } });
|
|
174
|
+
expect(trace.spans[0]!.start).toBe(1_700_000_000_000n * 1_000_000n);
|
|
175
|
+
expect(trace.spans[0]!.end).toBe(1_700_000_002_000n * 1_000_000n);
|
|
176
|
+
});
|
|
177
|
+
|
|
178
|
+
test("as engineTimings' inner hook, a command gets the trace parent and the receiver's exporter", async () => {
|
|
179
|
+
const trace = new OtlpTrace();
|
|
180
|
+
const timings = await engineTimings({ inner: commandTrace({ trace, prefix: "p", env: {} }) });
|
|
181
|
+
try {
|
|
182
|
+
const envs: NodeJS.ProcessEnv[] = [];
|
|
183
|
+
const runner = engineRunner({ exec: async (_b, _a, _d, env) => (envs.push(env), { code: 0, stdout: "", out: "" }) });
|
|
184
|
+
await withTerraformCommandHook(timings.onCommand, { root: "roots/a", method: "plan" }, () => runner.exec("tofu", ["plan"], "/w", {}));
|
|
185
|
+
expect(envs[0]!.TRACEPARENT).toBe(trace.traceparent(trace.spans[0]!));
|
|
186
|
+
if (!timings.problem) expect(envs[0]!.OTEL_EXPORTER_OTLP_TRACES_ENDPOINT).toMatch(/^http:\/\/127\.0\.0\.1:/);
|
|
187
|
+
expect(timings.root("roots/a")?.commands.map((c) => c.command)).toEqual(["plan"]);
|
|
188
|
+
} finally {
|
|
189
|
+
await timings.close();
|
|
190
|
+
}
|
|
191
|
+
});
|
|
192
|
+
});
|
|
193
|
+
|
|
194
|
+
describe("a Terragrunt unit's plan", () => {
|
|
195
|
+
test("the wrapper sets a unit's variables for the plan whose -out names it, and runs the binary as it is otherwise", () => {
|
|
196
|
+
const text = terragruntUnitWrapper("/bin/tofu", ["/w/plans", "/private/w/plans"], new Map<string, Record<string, string>>([["live/a", { X: "it's" }], ["live/b", {}]]));
|
|
197
|
+
expect(text).toContain(`'/w/plans/live/a/tfplan.tfplan' | '/private/w/plans/live/a/tfplan.tfplan') export X='it'\\''s' ;;`);
|
|
198
|
+
expect(text).not.toContain("live/b");
|
|
199
|
+
expect(text.trimEnd().endsWith(`exec '/bin/tofu' "$@"`)).toBe(true);
|
|
200
|
+
});
|
|
201
|
+
|
|
202
|
+
test("reaches onCommand once per unit, its variables reach that unit's plan alone, and its after has the report's time", async () => {
|
|
203
|
+
const dir = tmp();
|
|
204
|
+
const record = join(dir, "seen");
|
|
205
|
+
const bin = script(join(dir, "bin/tofu"), `echo "$UNIT_VAR" >> '${record}'`);
|
|
206
|
+
const seen: TerraformCommand[] = [];
|
|
207
|
+
const after = new Map<string, TerraformCommandResult>();
|
|
208
|
+
const calls: Array<{ args: readonly string[]; env: Record<string, string> }> = [];
|
|
209
|
+
const exec: TerragruntExec = async (_f, args, opts) => {
|
|
210
|
+
calls.push({ args, env: opts.env });
|
|
211
|
+
const at = (flag: string): string => args[args.indexOf(flag) + 1]!;
|
|
212
|
+
// Terragrunt runs the binary through TG_TF_PATH for each unit's plan, then an init with no -out.
|
|
213
|
+
for (const unit of ["live/a", "live/b"]) spawnSync(opts.env.TG_TF_PATH!, ["plan", `-out=${join(at("--out-dir"), unit, "tfplan.tfplan")}`], { encoding: "utf-8" });
|
|
214
|
+
spawnSync(opts.env.TG_TF_PATH!, ["init"], { encoding: "utf-8", env: { ...process.env, UNIT_VAR: "none" } });
|
|
215
|
+
writeFileSync(
|
|
216
|
+
at("--report-file"),
|
|
217
|
+
JSON.stringify([
|
|
218
|
+
{ Name: "live/a", Result: "succeeded", Started: "2026-11-10T10:00:00Z", Ended: "2026-11-10T10:00:04Z" },
|
|
219
|
+
{ Name: "live/b", Result: "failed", Reason: "run error", Started: "2026-11-10T10:00:00Z", Ended: "2026-11-10T10:00:01Z" },
|
|
220
|
+
]),
|
|
221
|
+
);
|
|
222
|
+
return { code: 1, stdout: "", stderr: "" };
|
|
223
|
+
};
|
|
224
|
+
await planTerragruntWave({
|
|
225
|
+
dir,
|
|
226
|
+
units: ["live/b", "live/a"],
|
|
227
|
+
workDir: "w",
|
|
228
|
+
binary: bin,
|
|
229
|
+
provisional: true,
|
|
230
|
+
exec,
|
|
231
|
+
onCommand: (c) => {
|
|
232
|
+
seen.push(c);
|
|
233
|
+
return { env: { UNIT_VAR: `for ${c.root}` }, after: (r) => void after.set(c.root, r) };
|
|
234
|
+
},
|
|
235
|
+
});
|
|
236
|
+
expect(seen.map((c) => [c.root, c.method, c.name, c.binary])).toEqual([
|
|
237
|
+
["live/a", "plan", "plan", bin],
|
|
238
|
+
["live/b", "plan", "plan", bin],
|
|
239
|
+
]);
|
|
240
|
+
expect(seen[0]!.cwd).toBe(join(dir, "live/a"));
|
|
241
|
+
expect(readFileSync(record, "utf-8").trim().split("\n")).toEqual(["for live/a", "for live/b", "none"]);
|
|
242
|
+
expect(after.get("live/a")).toMatchObject({ code: 0, seconds: 4, at: { start: Date.parse("2026-11-10T10:00:00Z"), end: Date.parse("2026-11-10T10:00:04Z") } });
|
|
243
|
+
expect(after.get("live/b")).toMatchObject({ code: 1, seconds: 1, stderr: "failed: run error" });
|
|
244
|
+
expect(calls[0]!.env.TG_TF_PATH).not.toBe(bin);
|
|
245
|
+
});
|
|
246
|
+
|
|
247
|
+
test("a unit missing from the report gets a null code, and a wave with no binary calls no hook", async () => {
|
|
248
|
+
const dir = tmp();
|
|
249
|
+
const bin = script(join(dir, "bin/tofu"), "true");
|
|
250
|
+
const after: TerraformCommandResult[] = [];
|
|
251
|
+
const exec: TerragruntExec = async (_f, args) => {
|
|
252
|
+
writeFileSync(args[args.indexOf("--report-file") + 1]!, "[]");
|
|
253
|
+
return { code: 0, stdout: "", stderr: "" };
|
|
254
|
+
};
|
|
255
|
+
await planTerragruntWave({ dir, units: ["live/a"], workDir: "w", binary: bin, provisional: true, exec, onCommand: () => ({ after: (r) => void after.push(r) }) });
|
|
256
|
+
expect(after).toMatchObject([{ code: null, seconds: 0 }]);
|
|
257
|
+
let called = false;
|
|
258
|
+
await planTerragruntWave({ dir, units: ["live/a"], workDir: "w2", provisional: true, exec, onCommand: () => void (called = true) });
|
|
259
|
+
expect(called).toBe(false);
|
|
260
|
+
});
|
|
261
|
+
});
|
|
@@ -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
|
+
}
|