@intentius/chant-lexicon-terraform 0.124.85 → 0.124.87

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (65) hide show
  1. package/dist/ci-setup.d.ts +11 -2
  2. package/dist/ci-setup.d.ts.map +1 -1
  3. package/dist/ci-setup.js +11 -6
  4. package/dist/components/capability-plugin.d.ts +12 -0
  5. package/dist/components/capability-plugin.d.ts.map +1 -1
  6. package/dist/components/capability-plugin.js +22 -4
  7. package/dist/compose.d.ts.map +1 -1
  8. package/dist/compose.js +1 -1
  9. package/dist/estate-migration.d.ts +101 -0
  10. package/dist/estate-migration.d.ts.map +1 -0
  11. package/dist/estate-migration.js +471 -0
  12. package/dist/integrity.json +2 -2
  13. package/dist/manifest.json +1 -1
  14. package/dist/op/activities/terraform.d.ts +65 -0
  15. package/dist/op/activities/terraform.d.ts.map +1 -1
  16. package/dist/op/activities/terraform.js +94 -1
  17. package/dist/state/backend.d.ts +46 -9
  18. package/dist/state/backend.d.ts.map +1 -1
  19. package/dist/state/backend.js +84 -31
  20. package/dist/state/gitlab.d.ts +1 -1
  21. package/dist/state/gitlab.d.ts.map +1 -1
  22. package/dist/state/gitlab.js +27 -14
  23. package/dist/state/index.d.ts +1 -0
  24. package/dist/state/index.d.ts.map +1 -1
  25. package/dist/state/index.js +1 -0
  26. package/dist/state/s3.d.ts +9 -3
  27. package/dist/state/s3.d.ts.map +1 -1
  28. package/dist/state/s3.js +4 -3
  29. package/dist/state/source.d.ts +92 -0
  30. package/dist/state/source.d.ts.map +1 -0
  31. package/dist/state/source.js +254 -0
  32. package/dist/state/store.d.ts +41 -4
  33. package/dist/state/store.d.ts.map +1 -1
  34. package/dist/state/store.js +125 -10
  35. package/dist/state/versions.d.ts +77 -10
  36. package/dist/state/versions.d.ts.map +1 -1
  37. package/dist/state/versions.js +118 -64
  38. package/dist/state-migration.d.ts +43 -1
  39. package/dist/state-migration.d.ts.map +1 -1
  40. package/dist/state-migration.js +251 -20
  41. package/dist/state-ops.d.ts +2 -3
  42. package/dist/state-ops.d.ts.map +1 -1
  43. package/dist/state-ops.js +4 -7
  44. package/package.json +2 -2
  45. package/src/ci-setup.test.ts +20 -0
  46. package/src/ci-setup.ts +11 -6
  47. package/src/components/capability-plugin.ts +36 -3
  48. package/src/components/terraform-apply-command-hook.test.ts +157 -0
  49. package/src/compose.ts +1 -0
  50. package/src/estate-migration.test.ts +186 -0
  51. package/src/estate-migration.ts +563 -0
  52. package/src/op/activities/terraform.ts +153 -3
  53. package/src/state/backend-verdict.test.ts +4 -3
  54. package/src/state/backend.test.ts +3 -1
  55. package/src/state/backend.ts +108 -33
  56. package/src/state/gitlab.ts +25 -12
  57. package/src/state/index.ts +1 -0
  58. package/src/state/s3.ts +5 -4
  59. package/src/state/source.ts +289 -0
  60. package/src/state/store.ts +145 -11
  61. package/src/state/versions.test.ts +365 -0
  62. package/src/state/versions.ts +148 -68
  63. package/src/state-migration-backends.test.ts +277 -0
  64. package/src/state-migration.ts +265 -23
  65. package/src/state-ops.ts +6 -8
@@ -10,6 +10,12 @@
10
10
  * restores a root's state put back to a version a migration recorded (a
11
11
  * revert), refused when the state moved past what that
12
12
  * migration left
13
+ * backends a root's state moved to the backend its code names now, from
14
+ * the backend it was in (read through the root with an
15
+ * override file naming that backend, in a data dir of its own),
16
+ * from a TFE-API workspace or from an exported state file
17
+ * (`./state/source.ts`), #4806. The backend the code names must
18
+ * hold no state; the state read is left where it was
13
19
  *
14
20
  * Planning (planTerraformMigration) initialises each root in a data dir of
15
21
  * its own, reads its backend version id and pulls its state, keeps only
@@ -23,26 +29,46 @@
23
29
  * a write that stops half way leaves a resource in two states rather than
24
30
  * in none. Then each root plans against its real backend.
25
31
  *
32
+ * A backend that keeps no versions (pg, kubernetes, consul, an http backend
33
+ * other than GitLab's) takes the general path, for each kind (#4806): the
34
+ * binary reads it with `state pull` and writes it with `state push` under
35
+ * the backend's own lock, which refuses a state of another lineage or an
36
+ * older serial, and the digest binds the state's contents, since there is
37
+ * no version id to bind. A backend move into one replaces the empty state a
38
+ * backend writes at init (kubernetes does) with `-force`; the digest bound
39
+ * that it was empty.
40
+ *
41
+ * A backend move's root names its source (`MigrationRootPlan.source`): the
42
+ * request binds it, under the locks the engine reads it again, and the lock
43
+ * of a workspace it is read from is taken first, through the workspace's
44
+ * API.
45
+ *
26
46
  * State contents never leave the work dir. The record holds version ids and
27
47
  * digests.
28
48
  *
29
49
  * Lifted from terragucci's `migrate.ts` (`moveResources`, `addressMatches`,
30
50
  * `derivedLineage`, `stateDigest`, `resourceChanges`, `backendBlock`,
31
- * `proofPlan`, `planMigration`'s moves and reverts, `applyMigration`,
32
- * `movedSince`), so terragucci deletes its copy. A backend move (a state
33
- * read from another backend, a TFE workspace or an exported file) and a
34
- * migration of choudoufu's live estates stay terragucci's for now.
51
+ * `proofPlan`, `planMigration`, `applyMigration`, `movedSince`, `takeLocks`,
52
+ * `readSource`, `withBackend`, `isPulled`, `PULLED_BACKENDS`,
53
+ * `backendRefusal`, `parseBackends`, `pulledProblems`, `locationOf`), so
54
+ * terragucci deletes its copy. A migration of choudoufu's live estates is
55
+ * `./estate-migration.ts`.
35
56
  */
36
57
  import { createHash } from "node:crypto";
37
58
  import { hostname } from "node:os";
38
59
  import { join, resolve } from "node:path";
39
60
  import { mkdirSync, readFileSync, rmSync, writeFileSync } from "node:fs";
40
61
  import { initRoot, planStateCheck, pushStateFile } from "./state-ops.js";
41
- import { isRefused, isStored, stateLocation, stateObject } from "./state/backend.js";
62
+ import { initialisedBackend, isRefused, isStored, readableStateObject, stateLocation, stateObject } from "./state/backend.js";
42
63
  import { pullState, runBinary } from "./state/exec.js";
43
- import { takeLocksWithSources } from "./state/locks.js";
64
+ import { SourceLockedError, StateLockedError, takeLocksWithSources } from "./state/locks.js";
65
+ import { EXTERNAL_BACKENDS, externalProblems, externalSource } from "./state/source.js";
44
66
  import { stateStore } from "./state/store.js";
45
- import { stateVersionSource } from "./state/versions.js";
67
+ import { keepsHistory, NO_HISTORY, recordedAddress, stateVersionSource } from "./state/versions.js";
68
+ /** The backends a migration reads and writes through the binary, under the backend's own lock: those that keep no versions (#4806). */
69
+ export const PULLED_BACKENDS = Object.keys(NO_HISTORY);
70
+ /** The backends a migration locks and writes itself. */
71
+ export const MIGRATED_BACKENDS = ["s3", "gcs", "azurerm", "local", "http"];
46
72
  /** A migration that cannot be planned: an address in no state, a root that is refused. Nothing was written. */
47
73
  export class MigrationError extends Error {
48
74
  constructor(message) {
@@ -68,6 +94,71 @@ export function derivedLineage(...parts) {
68
94
  return `${h.slice(0, 8)}-${h.slice(8, 12)}-4${h.slice(13, 16)}-8${h.slice(17, 20)}-${h.slice(20, 32)}`;
69
95
  }
70
96
  const emptyState = (lineage, serial, terraformVersion = "1.0.0") => ({ version: 4, terraform_version: terraformVersion, serial, lineage, outputs: {}, resources: [], check_results: null });
97
+ /** A state with no resources and no outputs: what a backend writes at init before anything is applied. */
98
+ const isEmptyState = (s) => s.resources.length === 0 && Object.keys(s.outputs ?? {}).length === 0;
99
+ /** The backend as a pulled state, or undefined when it is not one that keeps no versions. */
100
+ export function pulledState(b, env = {}) {
101
+ if (!PULLED_BACKENDS.includes(b.type) || keepsHistory(b, env) !== false)
102
+ return undefined;
103
+ const location = recordedAddress(b);
104
+ return { backend: b.type, pulled: true, ...(location ? { location } : {}) };
105
+ }
106
+ const isPulledState = (o) => "pulled" in o;
107
+ /** Where a migrated state is, as the record names it. */
108
+ export const migratedLocation = (o) => (isPulledState(o) ? o.location : stateLocation(o));
109
+ /** Settings of a pulled backend that hold a credential, which the job gives in the backend's own variables instead. */
110
+ const SECRET_KEYS = { password: "the backend's password variable", token: "the backend's token variable", client_key: "KUBE_CLIENT_KEY_DATA", access_token: "CONSUL_HTTP_TOKEN" };
111
+ /** The settings a backend move's `from.config` must give, by backend: where the state is. */
112
+ const FROM_NEEDS = { s3: ["bucket", "key"], gcs: ["bucket"], azurerm: ["storage_account_name", "container_name", "key"], kubernetes: ["secret_suffix"], consul: ["path"], http: ["address"] };
113
+ /**
114
+ * Problems with backend moves as a migration file gives them, one line each,
115
+ * headed `backends[<i>]`: a root that moves twice, a source backend chant
116
+ * does not read, a config that does not say where the state is, a
117
+ * credential written in the repo. None: the moves can be planned.
118
+ */
119
+ export function backendMoveProblems(moves) {
120
+ const p = [];
121
+ const seen = new Set();
122
+ const kinds = [...MIGRATED_BACKENDS, ...PULLED_BACKENDS.filter((b) => !MIGRATED_BACKENDS.includes(b)), ...EXTERNAL_BACKENDS];
123
+ for (const [i, b] of moves.entries()) {
124
+ const at = `backends[${i}]`;
125
+ if (seen.has(b.root))
126
+ p.push(`${at}: ${b.root} moves twice`);
127
+ seen.add(b.root);
128
+ if (!kinds.includes(b.from.backend)) {
129
+ p.push(`${at}.from.backend must be ${kinds.join(", ")}`);
130
+ continue;
131
+ }
132
+ const config = b.from.config;
133
+ if (EXTERNAL_BACKENDS.includes(b.from.backend))
134
+ p.push(...externalProblems(at, b.from.backend, config));
135
+ if (PULLED_BACKENDS.includes(b.from.backend)) {
136
+ for (const k of Object.keys(config))
137
+ if (k in SECRET_KEYS)
138
+ p.push(`${at}.from.config.${k}: a credential does not belong in the repo; the job gives it in ${SECRET_KEYS[k]}`);
139
+ const conn = config.conn_str;
140
+ if (b.from.backend === "pg" && typeof conn === "string" && (/^postgres(ql)?:\/\/[^/@]*:[^/@]*@/i.test(conn) || /(^|\s)password\s*=/.test(conn)))
141
+ p.push(`${at}.from.config.conn_str holds a password, which does not belong in the repo; the job gives it in PGPASSWORD`);
142
+ }
143
+ const needs = FROM_NEEDS[b.from.backend];
144
+ if (needs && needs.some((k) => typeof config[k] !== "string"))
145
+ p.push(`${at}.from.config must name the ${needs.join(", ").replace(/, ([^,]*)$/, " and $1")} of the state`);
146
+ }
147
+ return p;
148
+ }
149
+ /** An HCL value from the JSON of a backend's configuration. */
150
+ function hcl(v) {
151
+ if (v && typeof v === "object" && !Array.isArray(v))
152
+ return `{ ${Object.entries(v).filter(([, x]) => x !== null && x !== undefined).map(([k, x]) => `${k} = ${hcl(x)}`).join(", ")} }`;
153
+ if (Array.isArray(v))
154
+ return `[${v.map(hcl).join(", ")}]`;
155
+ return JSON.stringify(v);
156
+ }
157
+ /** A terraform block whose backend is `type` with `config`, for an override file. */
158
+ export function backendBlock(type, config) {
159
+ const body = Object.entries(config).filter(([, v]) => v !== null && v !== undefined).map(([k, v]) => ` ${k} = ${hcl(v)}\n`).join("");
160
+ return `terraform {\n backend ${JSON.stringify(type)} {\n${body} }\n}\n`;
161
+ }
71
162
  /**
72
163
  * The states after the moves. `before` holds each affected root's state, or
73
164
  * null when it has none. A changed state keeps its lineage and gets the next
@@ -131,6 +222,8 @@ export const DEFAULT_OVERRIDE_FILE = "chant_migrate_override.tf";
131
222
  export const DEFAULT_MIGRATE_LOCK = { operation: "chant migrate", info: "a state migration is writing this state" };
132
223
  /** A root's version id now, when its store keeps versions. `pin` lets an azurerm blob with snapshots only take one to name it. */
133
224
  async function versionNow(o, fetchFn, pin) {
225
+ if (isPulledState(o))
226
+ return undefined;
134
227
  const src = stateVersionSource(o, fetchFn, { pin });
135
228
  if ("refused" in src)
136
229
  return undefined;
@@ -146,10 +239,16 @@ export async function planTerraformMigration(input) {
146
239
  const log = input.log ?? (() => { });
147
240
  const lockOp = input.lock ?? DEFAULT_MIGRATE_LOCK;
148
241
  const override = input.overrideFile ?? DEFAULT_OVERRIDE_FILE;
149
- if (!!input.moves?.length === !!input.restores?.length)
150
- throw new MigrationError(`migration ${input.name}: give it moves or restores, one of the two`);
242
+ const kinds = [input.moves?.length && "moves", input.restores?.length && "restores", input.backends?.length && "backends"].filter(Boolean);
243
+ if (kinds.length !== 1)
244
+ throw new MigrationError(`migration ${input.name}: give it moves, restores or backends, one of the three`);
245
+ if (input.backends) {
246
+ const problems = backendMoveProblems(input.backends);
247
+ if (problems.length > 0)
248
+ throw new MigrationError(`migration ${input.name}:\n ${problems.join("\n ")}`);
249
+ }
151
250
  mkdirSync(input.work, { recursive: true });
152
- const named = [...new Set(input.moves ? input.moves.flatMap((m) => [m.from, m.to]) : input.restores.map((r) => r.root))].sort();
251
+ const named = [...new Set(input.moves ? input.moves.flatMap((m) => [m.from, m.to]) : input.restores ? input.restores.map((r) => r.root) : input.backends.map((b) => b.root))].sort();
153
252
  const byName = new Map(input.roots.map((r) => [r.root, r]));
154
253
  const missing = named.filter((n) => !byName.has(n));
155
254
  if (missing.length > 0)
@@ -165,23 +264,46 @@ export async function planTerraformMigration(input) {
165
264
  env = { ...env, TF_DATA_DIR: join(input.work, `data-${i}`) };
166
265
  await initRoot({ ...r, env, exec, reconfigure: true });
167
266
  }
168
- const o = stateObject(r.dir, env, undefined, r.binary);
169
- if (isRefused(o))
170
- throw new MigrationError(`migration ${input.name} cannot run: ${name}: ${o.reason}`);
267
+ const found = stateObject(r.dir, env, undefined, r.binary);
268
+ const recorded = initialisedBackend(r.dir, env);
269
+ const o = !isRefused(found) ? found : found.refused === "backend" && recorded ? pulledState(recorded, env) : undefined;
270
+ if (!o)
271
+ throw new MigrationError(`migration ${input.name} cannot run: ${name}: ${found.reason}`);
171
272
  runs.set(name, { ...r, env });
172
273
  objects.set(name, o);
173
274
  versions.set(name, await versionNow(o, input.fetch, false));
174
275
  before.set(name, await pullState(exec, r.binary, name, r.dir, env));
175
276
  }
176
277
  const after = new Map();
278
+ const sources = new Map();
279
+ const replaces = new Set();
177
280
  if (input.moves) {
178
281
  for (const [root, s] of moveResources(input.name, input.moves, before))
179
282
  after.set(root, s);
180
283
  }
284
+ else if (input.backends) {
285
+ for (const [i, b] of input.backends.entries()) {
286
+ const run = runs.get(b.root);
287
+ const target = objects.get(b.root);
288
+ const source = sourceOf(input, b, run, target, i);
289
+ // A backend that writes an empty state of its own lineage at init (kubernetes) has it replaced; the digest binds that it was empty.
290
+ const held = before.get(b.root);
291
+ if (held && isPulledState(target) && isEmptyState(held))
292
+ replaces.add(b.root);
293
+ else if (held)
294
+ throw new MigrationError(`migration ${input.name}: the backend ${b.root}'s code names, ${migratedLocation(target) ?? target.backend}, already holds a state, so a move would overwrite it`);
295
+ const read = await readSource(source, run, exec, input.fetch, override);
296
+ const where = "external" in source ? source.external.location : (migratedLocation(source.object) ?? b.from.backend);
297
+ if (!read.state)
298
+ throw new MigrationError(`migration ${input.name}: ${where} holds no state of ${b.root} to move`);
299
+ sources.set(b.root, { source, at: { ...(where ? { location: where } : {}), ...(read.version ? { version_id: read.version } : {}), digest: stateDigest(read.state) } });
300
+ after.set(b.root, read.state);
301
+ }
302
+ }
181
303
  else {
182
304
  for (const r of input.restores) {
183
305
  const o = objects.get(r.root);
184
- const location = stateLocation(o);
306
+ const location = migratedLocation(o);
185
307
  if (location !== r.location)
186
308
  throw new MigrationError(`migration ${input.name}: ${r.root}'s state is at ${location}, and the revert restores ${r.location}`);
187
309
  const v = versions.get(r.root);
@@ -194,6 +316,8 @@ export async function planTerraformMigration(input) {
194
316
  after.set(r.root, emptyState(lineage, serial, now?.terraform_version));
195
317
  continue;
196
318
  }
319
+ if (isPulledState(o))
320
+ throw new MigrationError(`migration ${input.name}: ${r.root}'s ${o.backend} backend keeps no versions to put back`);
197
321
  const src = stateVersionSource(o, input.fetch);
198
322
  if ("refused" in src)
199
323
  throw new MigrationError(`migration ${input.name}: ${r.root}: ${src.refused}`);
@@ -213,16 +337,77 @@ export async function planTerraformMigration(input) {
213
337
  const run = runs.get(name);
214
338
  const file = join(input.work, `${i}.tfstate`);
215
339
  writeFileSync(file, `${JSON.stringify(state, null, 2)}\n`);
216
- planned.set(name, { run, object: objects.get(name), file, gained: state.resources.length - (before.get(name)?.resources.length ?? 0) });
340
+ const src = sources.get(name);
341
+ planned.set(name, { run, object: objects.get(name), file, gained: state.resources.length - (before.get(name)?.resources.length ?? 0), ...(src ? { source: src.source } : {}), ...(replaces.has(name) ? { replacesEmpty: true } : {}) });
217
342
  const proof = await proofPlan(run, exec, file, join(input.work, `proof-${i}`), override);
218
343
  log(`${name}: ${proof.changes.length === 0 ? "no changes against its new state" : `${proof.changes.length} change${proof.changes.length === 1 ? "" : "s"} against its new state: ${proof.changes.join(", ")}`}`);
219
344
  const was = before.get(name) ?? null;
220
345
  const v = versions.get(name);
221
- const location = stateLocation(objects.get(name));
222
- roots.push({ root: name, ...(location ? { location } : {}), before: { ...(v ? { version_id: v } : {}), digest: was ? stateDigest(was) : null }, after: { digest: stateDigest(state) }, proof });
346
+ const location = migratedLocation(objects.get(name));
347
+ roots.push({ root: name, ...(location ? { location } : {}), before: { ...(v ? { version_id: v } : {}), digest: was ? stateDigest(was) : null }, after: { digest: stateDigest(state) }, proof, ...(src ? { source: src.at } : {}) });
223
348
  }
224
349
  return { roots, engine: migrationEngine(planned, input, exec, lockOp) };
225
350
  }
351
+ /**
352
+ * Where a backend move reads its state from: an external source (a TFE-API
353
+ * workspace, a state file), or the backend `from` names, read through the
354
+ * root. Throws MigrationError for a backend chant does not read, an s3
355
+ * backend that takes no lock file, and a source the root's code names
356
+ * already.
357
+ */
358
+ function sourceOf(input, b, run, target, i) {
359
+ const external = externalSource(input.repo ?? process.cwd(), b.from.backend, b.from.config, run.env, input.fetch);
360
+ if (external)
361
+ return { external };
362
+ const from = { type: b.from.backend, config: b.from.config };
363
+ const pulled = pulledState(from, run.env);
364
+ let object;
365
+ if (pulled)
366
+ object = pulled;
367
+ else {
368
+ const o = readableStateObject(run.dir, run.env, from);
369
+ if (isRefused(o))
370
+ throw new MigrationError(`migration ${input.name} cannot run: the backend ${b.root} moves from: ${o.reason}`);
371
+ if (o.backend === "s3" && !o.lockfile)
372
+ throw new MigrationError(`migration ${input.name} cannot run: the backend ${b.root} moves from: its s3 backend takes no lock file (use_lockfile = true), so a migration could not hold its lock`);
373
+ object = o;
374
+ }
375
+ const at = migratedLocation(object);
376
+ if (at !== undefined && at === migratedLocation(target))
377
+ throw new MigrationError(`migration ${input.name}: ${b.root}'s code names the backend it moves from, ${at}; change the backend block in the same change`);
378
+ return { object, block: backendBlock(b.from.backend, b.from.config), data: join(input.work, `source-${i}`) };
379
+ }
380
+ /** A source's state and version id now: an external source read directly, a backend through the root with an override file naming it, in its own data dir. */
381
+ async function readSource(s, run, exec, fetchFn, override) {
382
+ if ("external" in s) {
383
+ const got = await s.external.read();
384
+ if (!got)
385
+ return { state: null };
386
+ let state;
387
+ try {
388
+ state = JSON.parse(got.text);
389
+ }
390
+ catch {
391
+ throw new MigrationError(`${s.external.location} holds something that is not a state`);
392
+ }
393
+ if (!Array.isArray(state.resources))
394
+ state.resources = [];
395
+ return { state, ...(got.version ? { version: got.version } : {}) };
396
+ }
397
+ const at = join(run.dir, override);
398
+ const env = { ...run.env, TF_DATA_DIR: s.data };
399
+ writeFileSync(at, s.block);
400
+ let state;
401
+ try {
402
+ await initRoot({ ...run, env, exec, reconfigure: true });
403
+ state = await pullState(exec, run.binary, run.root, run.dir, env);
404
+ }
405
+ finally {
406
+ rmSync(at, { force: true });
407
+ }
408
+ const version = await versionNow(s.object, fetchFn, false);
409
+ return { state, ...(version ? { version } : {}) };
410
+ }
226
411
  /**
227
412
  * Plan a root against a state file: an override file switches its backend to
228
413
  * that local file, in a data dir of its own, so the root's `.terraform` and
@@ -242,21 +427,66 @@ async function proofPlan(run, exec, file, data, override) {
242
427
  rmSync(at, { force: true });
243
428
  }
244
429
  }
430
+ const stored = (o) => !isPulledState(o) && isStored(o);
245
431
  function migrationEngine(planned, input, exec, lockOp) {
246
432
  const log = input.log ?? (() => { });
433
+ const override = input.overrideFile ?? DEFAULT_OVERRIDE_FILE;
247
434
  const stores = new Map();
248
435
  for (const [root, p] of planned)
249
- if (isStored(p.object))
436
+ if (stored(p.object))
250
437
  stores.set(root, stateStore(p.object, p.run.dir, input.fetch));
438
+ // A backend move holds the lock of the state it reads too, after the states it writes, and a source workspace's lock before any.
439
+ const sourceStores = [];
440
+ const sourceLocks = [];
441
+ for (const [root, p] of planned) {
442
+ if (!p.source)
443
+ continue;
444
+ if ("external" in p.source) {
445
+ const ext = p.source.external;
446
+ if (ext.lock)
447
+ sourceLocks.push({ root, location: ext.location, lock: (reason) => ext.lock(reason) });
448
+ }
449
+ else if (stored(p.source.object))
450
+ sourceStores.push({ root, store: stateStore(p.source.object, p.run.dir, input.fetch) });
451
+ }
251
452
  const pinned = new Map();
252
453
  return {
253
454
  async lock() {
254
- const got = await takeLocksWithSources({ sources: [], stores: [...stores].map(([root, store]) => ({ root, store })), now: new Date().toISOString(), op: lockOp, who: input.who ?? `chant@${hostname()}` });
455
+ let got;
456
+ try {
457
+ got = await takeLocksWithSources({
458
+ sources: sourceLocks,
459
+ stores: [...[...stores].map(([root, store]) => ({ root, store })), ...sourceStores],
460
+ now: new Date().toISOString(),
461
+ op: lockOp,
462
+ sourceOp: input.sourceLock ?? { operation: lockOp.operation, info: "a state migration is reading this state" },
463
+ who: input.who ?? `chant@${hostname()}`,
464
+ });
465
+ }
466
+ catch (e) {
467
+ if (e instanceof SourceLockedError)
468
+ throw new MigrationError(`${e.root}: ${e.reason}`);
469
+ if (e instanceof StateLockedError) {
470
+ const h = e.holder;
471
+ const by = h ? [h.who && `by ${h.who}`, h.operation, h.info, h.created && `since ${h.created}`].filter(Boolean).join(", ") : "";
472
+ throw new MigrationError(`${e.root}: its state is locked (${e.lockLocation}${by ? `: ${by}` : ""}), so nothing was written`);
473
+ }
474
+ throw e;
475
+ }
255
476
  return async () => {
256
477
  for (const l of got.locks.reverse())
257
478
  await l.release().catch((e) => log(`${l.root}: the lock could not be released: ${e.message}`));
479
+ for (const l of got.sources.reverse())
480
+ await l.release().catch((e) => log(`${l.root}: the lock of ${l.location} could not be released: ${e.message}`));
258
481
  };
259
482
  },
483
+ async source(root) {
484
+ const p = planned.get(root);
485
+ if (!p.source)
486
+ return { digest: null };
487
+ const read = await readSource(p.source, p.run, exec, input.fetch, override);
488
+ return { ...(read.version ? { version_id: read.version } : {}), digest: read.state ? stateDigest(read.state) : null };
489
+ },
260
490
  async current(root) {
261
491
  const p = planned.get(root);
262
492
  const v = await versionNow(p.object, input.fetch, false);
@@ -278,10 +508,11 @@ function migrationEngine(planned, input, exec, lockOp) {
278
508
  for (const [root, p] of order) {
279
509
  const store = stores.get(root);
280
510
  // A state whose lock keeps the binary out (azurerm's lease) is written under it; the binary writes the others with the lock held for it.
511
+ // Over the empty state a backend wrote at init, of another lineage, the push is forced; under the locks its digest was found still empty.
281
512
  if (store?.write)
282
513
  await store.write(readFileSync(p.file, "utf-8"));
283
514
  else
284
- await pushStateFile({ ...p.run, exec, file: p.file, ...(store ? { lock: false } : {}) });
515
+ await pushStateFile({ ...p.run, exec, file: p.file, ...(store ? { lock: false } : p.replacesEmpty ? { force: true } : {}) });
285
516
  log(`${root}: wrote its new state`);
286
517
  }
287
518
  },
@@ -6,7 +6,6 @@ import { type StateExportInput, type StateExportOutcome } from "@intentius/chant
6
6
  import { type StateLockSource, type StateUnlockInput, type StateUnlockOutcome } from "@intentius/chant/state-unlock";
7
7
  import { type StateObject } from "./state/backend.js";
8
8
  import { type BinaryExec } from "./state/exec.js";
9
- import { type GitLabState } from "./state/gitlab.js";
10
9
  import type { StoreFetch } from "./state/object-store.js";
11
10
  /** The ops the gated requests of this module wait at, one per kind, as terragucci named them. */
12
11
  export declare const STATE_OPS: {
@@ -127,8 +126,8 @@ export interface RootBackendRun extends RootRun {
127
126
  /** The backend's HTTP calls, for tests. */
128
127
  fetch?: StoreFetch;
129
128
  }
130
- /** Where a root's state is: a state object, or a GitLab-managed state (./state/gitlab.ts), which the lock verdict refuses and export and release read. */
131
- type RootStateAt = StateObject | GitLabState;
129
+ /** Where a root's state is: a state object, a GitLab-managed state among them (./state/gitlab.ts, #4793). */
130
+ type RootStateAt = StateObject;
132
131
  export interface TerraformStateExportInput extends Omit<StateExportInput, "source" | "subject">, RootBackendRun {
133
132
  }
134
133
  /**
@@ -1 +1 @@
1
- {"version":3,"file":"state-ops.d.ts","sourceRoot":"","sources":["../src/state-ops.ts"],"names":[],"mappings":"AAuCA,OAAO,KAAK,EAAE,UAAU,EAAkB,MAAM,wCAAwC,CAAC;AACzF,OAAO,KAA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AAG,CAAC,EAAE,MAAM,CAAC;QAAC,IAAI,EAAE,MAAM,CAAC;QAAC,IAAI,CAAC,EAAE,OAAO,CAAC;QAAC,KAAK,CAAC,EAAE,OAAO,CAAA;KAAE,CAAC,CAAC;IAC5F,6EAA6E;IAC7E,KAAK,EAAE,MAAM,CAAC;CACf;AAED,qFAAqF;AACrF,MAAM,WAAW,aAAa;IAC5B,IAAI,EAAE,MAAM,CAAC;IACb,GAAG,CAAC,EAAE,MAAM,CAAC;IACb,GAAG,CAAC,EAAE,MAAM,CAAC;IACb,MAAM,CAAC,EAAE,MAAM,CAAC;IAChB,aAAa,EAAE,MAAM,CAAC,MAAM,EAAE,MAAM,CAAC,CAAC;IACtC,OAAO,CAAC,EAAE,OAAO,CAAC;IAClB,WAAW,CAAC,EAAE,MAAM,GAAG,KAAK,CAAC;CAC9B;AAsBD,eAAO,MAAM,8BAA8B,EAAE,UAAU,CAAC,oBAAoB,EAAE;IAAE,IAAI,EAAE,MAAM,CAAC;IAAC,EAAE,EAAE,MAAM,CAAA;CAAE,CAgBzG,CAAC;AAEF,eAAO,MAAM,4BAA4B,EAAE,UAAU,CAAC,kBAAkB,EAAE;IAAE,KAAK,EAAE,MAAM,EAAE,CAAA;CAAE,CAqB5F,CAAC;AAQF,eAAO,MAAM,uBAAuB,EAAE,UAAU,CAAC,aAAa,EAAE;IAAE,IAAI,EAAE,MAAM,CAAC;IAAC,UAAU,EAAE,MAAM,CAAA;CAAE,CAiBnG,CAAC;AAEF,qEAAqE;AACrE,eAAO,MAAM,4BAA4B;UAnE6C,MAAM;QAAM,MAAM;;WAkBrB,MAAM,EAAE;;UA6BnB,MAAM;gBAAc,MAAM;GAoB0C,CAAC"}
package/dist/state-ops.js CHANGED
@@ -44,9 +44,9 @@ import { exportStateVersion, StateExportError } from "@intentius/chant/state-exp
44
44
  import { parseLockInfo, releaseStateLock, StateUnlockError } from "@intentius/chant/state-unlock";
45
45
  import { plannerForBinary, terraformChangeSetPart } from "./change-set.js";
46
46
  import { DEFAULT_LOCK_TIMEOUT, lockTimeoutArgs } from "./lock-timeout.js";
47
- import { initialisedBackend, isRefused, stateObject } from "./state/backend.js";
47
+ import { initialisedBackend, isGitLabState, isRefused, stateObject } from "./state/backend.js";
48
48
  import { lastLines, runBinary } from "./state/exec.js";
49
- import { gitlabState, gitlabStore, shownUrl } from "./state/gitlab.js";
49
+ import { gitlabStore, shownUrl } from "./state/gitlab.js";
50
50
  import { stateStore } from "./state/store.js";
51
51
  import { gitlabVersionSource, stateVersionSource } from "./state/versions.js";
52
52
  /** The ops the gated requests of this module wait at, one per kind, as terragucci named them. */
@@ -185,20 +185,17 @@ async function withBackend(r, act, readOnly = false) {
185
185
  rmSync(data, { recursive: true, force: true });
186
186
  }
187
187
  }
188
- const isGitLab = (o) => "address" in o;
188
+ const isGitLab = isGitLabState;
189
189
  /**
190
190
  * The root's state. A read needs no lock file, so an s3 backend that locks
191
191
  * with DynamoDB is read as one that takes the lock file: the refusal is
192
- * about holding its lock, which a read never does. An http backend whose
193
- * address is GitLab's state API is that state.
192
+ * about holding its lock, which a read never does.
194
193
  */
195
194
  function objectOf(dir, env, binary, readOnly) {
196
195
  const o = stateObject(dir, env, undefined, binary);
197
196
  if (!isRefused(o))
198
197
  return o;
199
198
  const recorded = initialisedBackend(dir, env);
200
- if (o.backend === "http" && recorded?.type === "http")
201
- return gitlabState(recorded.config, env) ?? o;
202
199
  if (!readOnly || o.refused !== "no-lockfile")
203
200
  return o;
204
201
  return stateObject(dir, env, recorded ? { ...recorded, config: { ...recorded.config, use_lockfile: true } } : undefined, binary, { backendConfig: { use_lockfile: "true" } });
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@intentius/chant-lexicon-terraform",
3
- "version": "0.124.85",
3
+ "version": "0.124.87",
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.85",
96
+ "@intentius/chant": "^0.124.87",
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,20 @@
1
+ import { describe, expect, test } from "vitest";
2
+ import { CI_TOOL_SHA256 } from "@intentius/chant/ci-image";
3
+ import { CHOUDOUFU_CI_VERSION, choudoufuInstallLine } from "./ci-setup";
4
+
5
+ describe("choudoufuInstallLine", () => {
6
+ test("checks the archive against the release's SHA256SUMS and chant's recorded sha256 (#4784)", () => {
7
+ const line = choudoufuInstallLine();
8
+ expect(line.startsWith("command -v choudoufu >/dev/null 2>&1 || { ")).toBe(true);
9
+ expect(line).toContain(`https://github.com/INTENTIUS/choudoufu/releases/download/v${CHOUDOUFU_CI_VERSION}/SHA256SUMS`);
10
+ expect(line).toContain(`(amd64) want=${CI_TOOL_SHA256[`choudoufu-${CHOUDOUFU_CI_VERSION}-linux-amd64`]} ;;`);
11
+ expect(line).toContain(`(arm64) want=${CI_TOOL_SHA256[`choudoufu-${CHOUDOUFU_CI_VERSION}-linux-arm64`]} ;;`);
12
+ expect(line).toContain('sha256sum -c -');
13
+ expect(line).toContain(`ln -sf "/opt/chant/choudoufu-${CHOUDOUFU_CI_VERSION}/choudoufu" /usr/local/bin/choudoufu`);
14
+ });
15
+
16
+ test("a version chant records no sha256 for needs one", () => {
17
+ expect(() => choudoufuInstallLine("0.32.0")).toThrow(/no recorded sha256 for choudoufu 0.32.0/);
18
+ expect(choudoufuInstallLine("0.32.0", { sha256: { amd64: "a".repeat(64), arm64: "b".repeat(64) } })).toContain(`want=${"a".repeat(64)}`);
19
+ });
20
+ });
package/src/ci-setup.ts CHANGED
@@ -2,7 +2,7 @@
2
2
  * The engine install lines a Terraform project's pull request pipeline runs
3
3
  * in chant's default image (#4550, `LexiconPlugin.ciSetup`): OpenTofu, and
4
4
  * choudoufu when a root runs it. Each line skips the install when the binary
5
- * is already on PATH.
5
+ * is already on PATH, and the choudoufu line checks the archive's sha256.
6
6
  *
7
7
  * Whether a root runs choudoufu is read from the text of the project's
8
8
  * `chant.config.ts` (`binary: "choudoufu"`), never by running it, as lexicon
@@ -11,15 +11,20 @@
11
11
  import { existsSync, readFileSync } from "node:fs";
12
12
  import { join } from "node:path";
13
13
  import { PR_LOOP_SETUP } from "@intentius/chant/components/pr-pipeline";
14
+ import { ciToolInstallLine, type CiToolInstallOptions } from "@intentius/chant/ci-image";
14
15
 
15
16
  /** The choudoufu release the default lines install. A product's images pin their own. */
16
17
  export const CHOUDOUFU_CI_VERSION = "0.31.0";
17
18
 
18
- /** choudoufu into /usr/local/bin from its release archive. */
19
- export function choudoufuInstallLine(version = CHOUDOUFU_CI_VERSION): string {
20
- const arch = `$(uname -m | sed 's/x86_64/amd64/;s/aarch64/arm64/')`;
21
- const url = `https://github.com/INTENTIUS/choudoufu/releases/download/v${version}/choudoufu_v${version}_linux_${arch}.tar.gz`;
22
- return `command -v choudoufu >/dev/null 2>&1 || (mkdir -p /tmp/choudoufu && curl -fsSL "${url}" | tar -xz -C /tmp/choudoufu && install "$(find /tmp/choudoufu -type f -name choudoufu | head -n 1)" /usr/local/bin/choudoufu)`;
19
+ /**
20
+ * choudoufu from its release archive, checked against the release's
21
+ * SHA256SUMS and the sha256 chant records for it (`CI_TOOL_SHA256` in
22
+ * `@intentius/chant/ci-image`, #4784), then linked into /usr/local/bin. It
23
+ * skips when a `choudoufu` is already on PATH. A version chant records no
24
+ * sha256 for needs `sha256`, from the release's SHA256SUMS.
25
+ */
26
+ export function choudoufuInstallLine(version = CHOUDOUFU_CI_VERSION, options: { sha256?: CiToolInstallOptions["sha256"] } = {}): string {
27
+ return ciToolInstallLine("choudoufu", version, { onPath: true, requirePin: true, ...(options.sha256 ? { sha256: options.sha256 } : {}) });
23
28
  }
24
29
 
25
30
  /** Whether the project's config names choudoufu as a root's binary. */
@@ -166,10 +166,13 @@ import {
166
166
  choudoufuPlanSet,
167
167
  choudoufuWaveApply,
168
168
  choudoufuWaveRoot,
169
+ asUpstreamCommands,
169
170
  terraformApply,
170
171
  terraformInit,
171
172
  terraformOutputs,
172
173
  terraformPlan,
174
+ withTerraformCommandHook,
175
+ type TerraformCommandHook,
173
176
  } from "../op/activities/terraform";
174
177
  import { terraformPlanMoved } from "../exit-codes";
175
178
  import { terraformPlanShape } from "../plan-shape";
@@ -250,8 +253,20 @@ export type TerraformRootResolver = (root: string, input: TerraformApplyStepInpu
250
253
  export interface TerraformApplyDeps {
251
254
  /** What each root runs. Default: the step's `binary`, else `terraform.binary`. */
252
255
  resolveRoot?: TerraformRootResolver;
256
+ /**
257
+ * Called around each engine command the step runs for a root (chant#4865):
258
+ * its `init`, `workspace select`, `plan`, `show`, `apply` and `output`, a
259
+ * choudoufu root's `live-plan-set` and `live-wave-apply`, and the `init`
260
+ * and `output` of each upstream its `reads` name. The hook sees the
261
+ * command's name, args, directory and environment, may add variables to
262
+ * it, and its `after` gets the exit code, duration and output. Roots a
263
+ * wave runs at once (chant#4864) each see their own commands.
264
+ */
265
+ onCommand?: TerraformCommandHook;
253
266
  }
254
267
 
268
+ export type { TerraformCommand, TerraformCommandHook, TerraformCommandResult, TerraformCommandRun } from "../op/activities/terraform";
269
+
255
270
  /** What `terraform-apply` returns. `outputs` is what a dependent's `stackOutput()` reads. */
256
271
  export interface TerraformApplyStepOutput {
257
272
  root: string;
@@ -303,9 +318,12 @@ async function withReads(input: TerraformApplyStepInput, resolveRoot?: Terraform
303
318
  ...(tools?.binary ? { binary: tools.binary } : {}),
304
319
  ...(tools?.readEnv ? { env: tools.readEnv } : {}),
305
320
  };
306
- // chant#4864: runs that plan at once may read the same upstream, so its init takes a turn.
307
- await inInitTurn(serial, at.env, initDir(at), () => terraformInit(at));
308
- return terraformOutputs(at);
321
+ // chant#4865: the upstream's init and output reach the product's command hook as a read for this root.
322
+ return asUpstreamCommands(root, async () => {
323
+ // chant#4864: runs that plan at once may read the same upstream, so its init takes a turn.
324
+ await inInitTurn(serial, at.env, initDir(at), () => terraformInit(at));
325
+ return terraformOutputs(at);
326
+ });
309
327
  });
310
328
  if (filled.errors.length > 0) throw new Error(`terraform-apply: root "${input.root}": ${filled.errors.join("; ")}`);
311
329
  if (filled.waiting.length > 0) return { waiting: waitingReadsReason(filled.waiting), upstreams: [...new Set(filled.waiting.map((w) => w.read.root))].sort() };
@@ -684,6 +702,21 @@ async function plannerOf(input: TerraformApplyStepInput, plan?: CapabilityPlan):
684
702
  * project's binary, as before.
685
703
  */
686
704
  export function terraformApplyCapabilityWith(deps: TerraformApplyDeps = {}): Capability<TerraformApplyStepInput, TerraformApplyStepOutput> {
705
+ const cap = terraformApplyMethods(deps);
706
+ const hook = deps.onCommand;
707
+ if (!hook) return cap;
708
+ // chant#4865: each method that runs the engine for a root runs it through the product's hook, as commands for that root.
709
+ const scope = (method: string, step: TerraformApplyStepInput) => ({ root: typeof step?.root === "string" ? step.root : "", method });
710
+ return {
711
+ ...cap,
712
+ plan: (ctx, step) => withTerraformCommandHook(hook, scope("plan", step), () => cap.plan(ctx, step)),
713
+ run: (ctx, step) => withTerraformCommandHook(hook, scope("run", step), () => cap.run(ctx, step)),
714
+ outputs: (ctx, step) => withTerraformCommandHook(hook, scope("outputs", step), () => cap.outputs!(ctx, step)),
715
+ };
716
+ }
717
+
718
+ /** The step's methods with `deps.resolveRoot`, before {@link terraformApplyCapabilityWith} puts its command hook around them. */
719
+ function terraformApplyMethods(deps: TerraformApplyDeps): Capability<TerraformApplyStepInput, TerraformApplyStepOutput> & Required<Pick<Capability<TerraformApplyStepInput, TerraformApplyStepOutput>, "plan" | "outputs">> {
687
720
  return {
688
721
  kind: "terraform-apply",
689
722
  // A root's apply has no undo short of applying the previous configuration,