@intentius/chant-lexicon-terraform 0.124.87 → 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/action-ops.d.ts.map +1 -1
- package/dist/action-ops.js +3 -1
- package/dist/chant-floors.d.ts +30 -0
- package/dist/chant-floors.d.ts.map +1 -0
- package/dist/chant-floors.js +29 -0
- package/dist/components/capability-plugin.d.ts +15 -1
- package/dist/components/capability-plugin.d.ts.map +1 -1
- package/dist/components/capability-plugin.js +13 -5
- package/dist/components/compose-group.d.ts +5 -1
- package/dist/components/compose-group.d.ts.map +1 -1
- package/dist/components/compose-group.js +7 -4
- 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/engine-timings.d.ts +249 -0
- package/dist/engine-timings.d.ts.map +1 -0
- package/dist/engine-timings.js +569 -0
- package/dist/integrity.json +2 -2
- package/dist/live-roots.d.ts +143 -0
- package/dist/live-roots.d.ts.map +1 -0
- package/dist/live-roots.js +304 -0
- package/dist/manifest.json +1 -1
- package/dist/migration-history.d.ts +105 -0
- package/dist/migration-history.d.ts.map +1 -0
- package/dist/migration-history.js +159 -0
- package/dist/module-catalog/index.d.ts +1 -0
- package/dist/module-catalog/index.d.ts.map +1 -1
- package/dist/module-catalog/index.js +3 -1
- package/dist/op/activities/live-detect.d.ts +15 -0
- package/dist/op/activities/live-detect.d.ts.map +1 -1
- package/dist/op/activities/live-detect.js +94 -0
- package/dist/op/activities/terraform.d.ts +1 -65
- package/dist/op/activities/terraform.d.ts.map +1 -1
- package/dist/op/activities/terraform.js +4 -54
- package/dist/op/command-hook.d.ts +98 -0
- package/dist/op/command-hook.d.ts.map +1 -0
- package/dist/op/command-hook.js +115 -0
- package/dist/registry/index.d.ts +1 -0
- package/dist/registry/index.d.ts.map +1 -1
- package/dist/registry/index.js +1 -0
- package/dist/state/exec.d.ts +23 -0
- package/dist/state/exec.d.ts.map +1 -1
- package/dist/state/exec.js +38 -0
- package/dist/state-ops.d.ts.map +1 -1
- package/dist/state-ops.js +3 -1
- package/dist/terragrunt/index.d.ts +3 -1
- package/dist/terragrunt/index.d.ts.map +1 -1
- package/dist/terragrunt/index.js +3 -1
- package/dist/terragrunt/place.d.ts +27 -0
- package/dist/terragrunt/place.d.ts.map +1 -0
- package/dist/terragrunt/place.js +52 -0
- 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/action-ops.ts +3 -1
- package/src/chant-floors.ts +32 -0
- package/src/components/capability-plugin.ts +27 -6
- package/src/components/compose-group.test.ts +29 -0
- package/src/components/compose-group.ts +12 -5
- package/src/components/terraform-apply-command-hook.test.ts +43 -0
- package/src/engine-runner.test.ts +261 -0
- package/src/engine-runner.ts +191 -0
- package/src/engine-timings.test.ts +203 -0
- package/src/engine-timings.ts +718 -0
- package/src/live-record-store.test.ts +76 -0
- package/src/live-roots.test.ts +80 -0
- package/src/live-roots.ts +347 -0
- package/src/migration-history.test.ts +83 -0
- package/src/migration-history.ts +199 -0
- package/src/module-catalog/index.ts +4 -1
- package/src/op/activities/live-detect.ts +87 -0
- package/src/op/activities/terraform.ts +12 -111
- package/src/op/command-hook.ts +189 -0
- package/src/registry/index.ts +1 -0
- package/src/state/engine-env.test.ts +18 -0
- package/src/state/exec.ts +38 -0
- package/src/state-ops.ts +3 -1
- package/src/terragrunt/index.ts +3 -1
- package/src/terragrunt/place.test.ts +42 -0
- package/src/terragrunt/place.ts +75 -0
- package/src/terragrunt/run.ts +128 -4
|
@@ -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,203 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Per-root engine timings (chant#4887): `engineTimings()`'s hook, passed as
|
|
3
|
+
* `terraformApplyCapabilityWith({ onCommand })`, times each root's commands
|
|
4
|
+
* and collects the spans the engine sends to its loopback receiver, read into
|
|
5
|
+
* per-root plan and apply seconds, span counts and lock waits. A plain wave
|
|
6
|
+
* runs through core's `runOpWave` with a fake `terraform` on `PATH`, a Node
|
|
7
|
+
* script that sends OTLP/JSON spans where the variables point it; a composed
|
|
8
|
+
* group's commands run through `hookedBinaryExec` with a fake exec. Nothing
|
|
9
|
+
* reaches a cloud or a collector off the machine.
|
|
10
|
+
*/
|
|
11
|
+
import { chmodSync, mkdirSync, mkdtempSync, realpathSync, writeFileSync } from "node:fs";
|
|
12
|
+
import { tmpdir } from "node:os";
|
|
13
|
+
import { join } from "node:path";
|
|
14
|
+
import { afterEach, describe, expect, test, vi } from "vitest";
|
|
15
|
+
import { CapabilityRegistry, type Capability } from "@intentius/chant/components/capability";
|
|
16
|
+
import { memoryGateLedgerPort, runOpWave, type OpWavesSpec } from "@intentius/chant/op";
|
|
17
|
+
import { terraformApplyCapabilityWith } from "./components/capability-plugin";
|
|
18
|
+
import { decodeTracesJson, ENGINE_RUN_HEADER, engineTimings, rootTimings, SpanReceiver, type CollectedSpan } from "./engine-timings";
|
|
19
|
+
import { hookedBinaryExec, withTerraformCommandHook } from "./op/command-hook";
|
|
20
|
+
import type { BinaryExec } from "./state/exec";
|
|
21
|
+
|
|
22
|
+
afterEach(() => vi.unstubAllEnvs());
|
|
23
|
+
|
|
24
|
+
/** OTLP/JSON for spans of `ms` milliseconds each. */
|
|
25
|
+
const otlp = (spans: Array<{ name: string; ms: number; attributes?: Record<string, string | number> }>) => ({
|
|
26
|
+
resourceSpans: [
|
|
27
|
+
{
|
|
28
|
+
scopeSpans: [
|
|
29
|
+
{
|
|
30
|
+
spans: spans.map((s, i) => ({
|
|
31
|
+
spanId: `s${i}`,
|
|
32
|
+
name: s.name,
|
|
33
|
+
startTimeUnixNano: "1000000000",
|
|
34
|
+
endTimeUnixNano: String(1_000_000_000 + s.ms * 1_000_000),
|
|
35
|
+
attributes: Object.entries(s.attributes ?? {}).map(([key, v]) => ({ key, value: typeof v === "number" ? { intValue: String(v) } : { stringValue: v } })),
|
|
36
|
+
})),
|
|
37
|
+
},
|
|
38
|
+
],
|
|
39
|
+
},
|
|
40
|
+
],
|
|
41
|
+
});
|
|
42
|
+
|
|
43
|
+
describe("reading the engine's spans", () => {
|
|
44
|
+
test("a root's resources, provider calls and lock waits come from its spans", () => {
|
|
45
|
+
const spans: CollectedSpan[] = decodeTracesJson(
|
|
46
|
+
otlp([
|
|
47
|
+
{ name: "Plan resource instance changes", ms: 120, attributes: { "opentofu.resource_instance.address": "aws_sqs_queue.jobs", "opentofu.resource.type": "aws_sqs_queue" } },
|
|
48
|
+
{ name: "State lock wait", ms: 2500, attributes: { "opentofu.state.backend": "s3", "opentofu.state.lock.operation": "plan", "opentofu.state.lock.attempts": 3 } },
|
|
49
|
+
{ name: "tfplugin5.Provider/PlanResourceChange", ms: 40 },
|
|
50
|
+
]),
|
|
51
|
+
);
|
|
52
|
+
const t = rootTimings(spans, { binary: "tofu", seconds: 4, planSeconds: 3 });
|
|
53
|
+
expect(t).toMatchObject({ seconds: 4, plan_seconds: 3, spans: 3, detail: "resources" });
|
|
54
|
+
expect(t.resources).toEqual([{ address: "aws_sqs_queue.jobs", type: "aws_sqs_queue", ms: 120 }]);
|
|
55
|
+
expect(t.lock_waits).toEqual([{ backend: "s3", operation: "plan", attempts: 3, ms: 2500 }]);
|
|
56
|
+
expect(t.provider_calls).toEqual([{ method: "PlanResourceChange", ms: 40 }]);
|
|
57
|
+
});
|
|
58
|
+
|
|
59
|
+
test("the receiver keeps a batch under the key its exporter named, and refuses one with no token", async () => {
|
|
60
|
+
const r = new SpanReceiver();
|
|
61
|
+
expect(await r.listen()).toBeUndefined();
|
|
62
|
+
const vars = r.exporter("roots/a\u0000plan");
|
|
63
|
+
const token = vars.OTEL_EXPORTER_OTLP_TRACES_HEADERS!.split("=")[1]!;
|
|
64
|
+
const post = (headers: Record<string, string>) =>
|
|
65
|
+
fetch(vars.OTEL_EXPORTER_OTLP_TRACES_ENDPOINT!, { method: "POST", headers: { "content-type": "application/json", ...headers }, body: JSON.stringify(otlp([{ name: "State lock wait", ms: 10 }])) });
|
|
66
|
+
expect((await post({ [ENGINE_RUN_HEADER]: token })).status).toBe(200);
|
|
67
|
+
expect((await post({})).status).toBe(401);
|
|
68
|
+
expect(r.spansOf("roots/a\u0000plan").map((s) => s.name)).toEqual(["State lock wait"]);
|
|
69
|
+
expect(await r.close()).toEqual([]);
|
|
70
|
+
});
|
|
71
|
+
});
|
|
72
|
+
|
|
73
|
+
/** A fake `terraform`: a Node script that answers each command and sends a plan's and an apply's spans where the variables point. */
|
|
74
|
+
const FAKE = (node: string) => `#!${node}
|
|
75
|
+
const fs = require("fs");
|
|
76
|
+
const [cmd, ...rest] = process.argv.slice(2);
|
|
77
|
+
const root = process.cwd().split("/").pop();
|
|
78
|
+
const send = async (spans) => {
|
|
79
|
+
const url = process.env.OTEL_EXPORTER_OTLP_TRACES_ENDPOINT;
|
|
80
|
+
if (!url) return;
|
|
81
|
+
const [k, v] = (process.env.OTEL_EXPORTER_OTLP_TRACES_HEADERS || "").split("=");
|
|
82
|
+
const span = (s, i) => ({ spanId: cmd + i, name: s[0], startTimeUnixNano: "1000000000", endTimeUnixNano: String(1000000000 + s[1] * 1000000), attributes: Object.entries(s[2] || {}).map(([key, x]) => ({ key, value: typeof x === "number" ? { intValue: String(x) } : { stringValue: x } })) });
|
|
83
|
+
await fetch(url, { method: "POST", headers: { "content-type": "application/json", [k]: v }, body: JSON.stringify({ resourceSpans: [{ scopeSpans: [{ spans: spans.map(span) }] }] }) });
|
|
84
|
+
};
|
|
85
|
+
(async () => {
|
|
86
|
+
if (cmd === "plan") {
|
|
87
|
+
const out = rest.find((a) => a.startsWith("-out=")).slice(5);
|
|
88
|
+
fs.writeFileSync(out, "plan");
|
|
89
|
+
await send([["State lock wait", root === "a" ? 300 : 0, { "opentofu.state.lock.attempts": 2 }], ["Plan resource instance changes", 50, { "opentofu.resource_instance.address": "terraform_data." + root }]].filter((s) => s[1] > 0));
|
|
90
|
+
await new Promise((r) => setTimeout(r, 30));
|
|
91
|
+
process.exit(2);
|
|
92
|
+
}
|
|
93
|
+
if (cmd === "apply") {
|
|
94
|
+
await send([["Apply resource instance changes", 80, { "opentofu.resource_instance.address": "terraform_data." + root }]]);
|
|
95
|
+
await new Promise((r) => setTimeout(r, 30));
|
|
96
|
+
}
|
|
97
|
+
if (cmd === "show") console.log(JSON.stringify({ format_version: "1.2", resource_changes: [{ address: "terraform_data." + root, mode: "managed", type: "terraform_data", name: root, change: { actions: ["create"], before: null, after: {} } }] }));
|
|
98
|
+
if (cmd === "output") console.log('{"id":{"value":"x"}}');
|
|
99
|
+
process.exit(0);
|
|
100
|
+
})();
|
|
101
|
+
`;
|
|
102
|
+
|
|
103
|
+
function project(): string {
|
|
104
|
+
const dir = realpathSync(mkdtempSync(join(tmpdir(), "chant-tf-timings-")));
|
|
105
|
+
mkdirSync(join(dir, "bin"));
|
|
106
|
+
writeFileSync(join(dir, "bin", "terraform"), FAKE(process.execPath));
|
|
107
|
+
chmodSync(join(dir, "bin", "terraform"), 0o755);
|
|
108
|
+
vi.stubEnv("PATH", `${join(dir, "bin")}:${process.env.PATH ?? ""}`);
|
|
109
|
+
vi.stubEnv("TF_PLUGIN_CACHE_DIR", "");
|
|
110
|
+
for (const r of ["a", "b"]) {
|
|
111
|
+
mkdirSync(join(dir, "roots", r), { recursive: true });
|
|
112
|
+
writeFileSync(join(dir, "roots", r, "main.tf"), "");
|
|
113
|
+
}
|
|
114
|
+
writeFileSync(join(dir, "chant.config.json"), JSON.stringify({ lexicons: ["terraform"], terraform: { binary: "terraform" } }));
|
|
115
|
+
return dir;
|
|
116
|
+
}
|
|
117
|
+
|
|
118
|
+
const SPEC = {
|
|
119
|
+
name: "tf-apply",
|
|
120
|
+
op: "tf-apply",
|
|
121
|
+
gateName: "wave-{k}",
|
|
122
|
+
parallelism: 2,
|
|
123
|
+
step: { kind: "terraform-apply", root: "{target}", dir: "{target}", lockTimeout: false },
|
|
124
|
+
waves: [{ name: "wave-1", gate: "never", runs: [{ target: "roots/a" }, { target: "roots/b" }] }],
|
|
125
|
+
} as unknown as OpWavesSpec;
|
|
126
|
+
|
|
127
|
+
describe("engineTimings (chant#4887)", () => {
|
|
128
|
+
test("a plain wave's roots get their plan and apply seconds, span counts and lock waits", async () => {
|
|
129
|
+
const dir = project();
|
|
130
|
+
const timings = await engineTimings();
|
|
131
|
+
expect(timings.problem).toBeUndefined();
|
|
132
|
+
const result = await runOpWave({
|
|
133
|
+
spec: SPEC,
|
|
134
|
+
specFile: "waves.json",
|
|
135
|
+
wave: 1,
|
|
136
|
+
cwd: dir,
|
|
137
|
+
show: () => ({ sha: "c".repeat(40), text: JSON.stringify(SPEC) }),
|
|
138
|
+
gates: memoryGateLedgerPort(),
|
|
139
|
+
capabilities: new CapabilityRegistry().register(terraformApplyCapabilityWith({ onCommand: timings.onCommand }) as unknown as Capability<never, unknown>),
|
|
140
|
+
log: () => {},
|
|
141
|
+
});
|
|
142
|
+
expect(result.exitCode).toBe(0);
|
|
143
|
+
await timings.close();
|
|
144
|
+
const roots = timings.roots();
|
|
145
|
+
expect(Object.keys(roots)).toEqual(["roots/a", "roots/b"]);
|
|
146
|
+
const a = roots["roots/a"]!;
|
|
147
|
+
expect(a.commands.map((c) => `${c.method} ${c.command}`)).toEqual(["plan init", "plan plan", "plan show", "run apply", "run output"]);
|
|
148
|
+
expect(a.plan_seconds).toBeGreaterThanOrEqual(0.02);
|
|
149
|
+
expect(a.apply_seconds).toBeGreaterThanOrEqual(0.02);
|
|
150
|
+
expect(a.seconds).toBeGreaterThanOrEqual(a.plan_seconds! + a.apply_seconds!);
|
|
151
|
+
expect(a.spans).toBe(3);
|
|
152
|
+
expect(a.lock_waits).toEqual([{ attempts: 2, ms: 300 }]);
|
|
153
|
+
expect(a.resources.map((r) => r.address)).toEqual(["terraform_data.a", "terraform_data.a"]);
|
|
154
|
+
expect(roots["roots/b"]).toMatchObject({ spans: 2, lock_waits: [] });
|
|
155
|
+
expect(timings.run().roots.map((r) => r.root).sort()).toEqual(["roots/a", "roots/b"]);
|
|
156
|
+
});
|
|
157
|
+
|
|
158
|
+
test("a composed group's commands time every member, and the run counts its resources once", async () => {
|
|
159
|
+
const timings = await engineTimings({ noSpans: true });
|
|
160
|
+
const fake: BinaryExec = async (_bin, args) => {
|
|
161
|
+
await new Promise((r) => setTimeout(r, 10));
|
|
162
|
+
return { code: 0, stdout: args[0] === "plan" ? "Plan: 2 to add" : "", out: "" };
|
|
163
|
+
};
|
|
164
|
+
const exec = hookedBinaryExec(fake);
|
|
165
|
+
const seen: string[] = [];
|
|
166
|
+
const hook = timings.onCommand;
|
|
167
|
+
const watched: typeof hook = async (c) => {
|
|
168
|
+
seen.push(`${c.method} ${c.name} ${(c.members ?? []).join(",")}`);
|
|
169
|
+
return hook(c);
|
|
170
|
+
};
|
|
171
|
+
await withTerraformCommandHook(watched, { root: "roots/a", members: ["roots/a", "roots/b"], method: "plan" }, async () => {
|
|
172
|
+
await exec("tofu", ["init", "-input=false"], "/w/.chant-compose", {});
|
|
173
|
+
await exec("tofu", ["plan", "-out=/w/composed.tfplan"], "/w/.chant-compose", {});
|
|
174
|
+
});
|
|
175
|
+
await withTerraformCommandHook(watched, { root: "roots/a", members: ["roots/a", "roots/b"], method: "run" }, async () => {
|
|
176
|
+
await exec("tofu", ["apply", "-json", "/w/composed.tfplan"], "/w/.chant-compose", {});
|
|
177
|
+
});
|
|
178
|
+
expect(seen).toEqual(["plan init roots/a,roots/b", "plan plan roots/a,roots/b", "run apply roots/a,roots/b"]);
|
|
179
|
+
const roots = timings.roots();
|
|
180
|
+
for (const r of ["roots/a", "roots/b"]) {
|
|
181
|
+
expect(roots[r]!.composed).toEqual(["roots/a", "roots/b"]);
|
|
182
|
+
expect(roots[r]!.plan_seconds).toBeGreaterThan(0);
|
|
183
|
+
expect(roots[r]!.apply_seconds).toBeGreaterThan(0);
|
|
184
|
+
expect(roots[r]!.commands.map((c) => c.command)).toEqual(["init", "plan", "apply"]);
|
|
185
|
+
}
|
|
186
|
+
});
|
|
187
|
+
|
|
188
|
+
test("the product's own hook runs first: its variables reach the command under the receiver's, and its after runs", async () => {
|
|
189
|
+
const afters: string[] = [];
|
|
190
|
+
const got: Array<Record<string, string | undefined>> = [];
|
|
191
|
+
const fake: BinaryExec = async (_bin, _args, _dir, env) => {
|
|
192
|
+
got.push({ TRACEPARENT: env.TRACEPARENT, endpoint: env.OTEL_EXPORTER_OTLP_TRACES_ENDPOINT });
|
|
193
|
+
return { code: 0, stdout: "", out: "" };
|
|
194
|
+
};
|
|
195
|
+
const timings = await engineTimings({ inner: () => ({ env: { TRACEPARENT: "00-abc-def-01" }, after: (r) => void afters.push(String(r.code)) }) });
|
|
196
|
+
await withTerraformCommandHook(timings.onCommand, { root: "roots/a", method: "plan" }, () => hookedBinaryExec(fake)("tofu", ["plan"], "/w", {}));
|
|
197
|
+
expect(got[0]!.TRACEPARENT).toBe("00-abc-def-01");
|
|
198
|
+
expect(got[0]!.endpoint).toMatch(/^http:\/\/127\.0\.0\.1:\d+\/v1\/traces$/);
|
|
199
|
+
expect(afters).toEqual(["0"]);
|
|
200
|
+
expect(timings.root("roots/a")!.commands).toEqual([{ command: "plan", method: "plan", seconds: expect.any(Number), code: 0 }]);
|
|
201
|
+
await timings.close();
|
|
202
|
+
});
|
|
203
|
+
});
|