@intentius/chant-lexicon-terraform 0.124.76 → 0.124.77
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/integrity.json +2 -2
- package/dist/manifest.json +1 -1
- package/dist/pr-env.d.ts +138 -0
- package/dist/pr-env.d.ts.map +1 -0
- package/dist/pr-env.js +248 -0
- package/dist/state/gitlab.d.ts +74 -0
- package/dist/state/gitlab.d.ts.map +1 -0
- package/dist/state/gitlab.js +275 -0
- package/dist/state/index.d.ts +2 -0
- package/dist/state/index.d.ts.map +1 -1
- package/dist/state/index.js +2 -0
- package/dist/state/store.d.ts +2 -1
- package/dist/state/store.d.ts.map +1 -1
- package/dist/state/versions.d.ts +39 -0
- package/dist/state/versions.d.ts.map +1 -0
- package/dist/state/versions.js +85 -0
- package/dist/state-migration.d.ts +85 -0
- package/dist/state-migration.d.ts.map +1 -0
- package/dist/state-migration.js +296 -0
- package/dist/state-ops.d.ts +54 -87
- package/dist/state-ops.d.ts.map +1 -1
- package/dist/state-ops.js +120 -77
- package/package.json +2 -2
- package/src/pr-env.test.ts +132 -0
- package/src/pr-env.ts +312 -0
- package/src/state/gitlab.test.ts +226 -0
- package/src/state/gitlab.ts +318 -0
- package/src/state/index.ts +2 -0
- package/src/state/store.ts +2 -1
- package/src/state/versions.ts +103 -0
- package/src/state-export-unlock.test.ts +230 -0
- package/src/state-migration.test.ts +236 -0
- package/src/state-migration.ts +353 -0
- package/src/state-ops.ts +132 -130
package/src/pr-env.ts
ADDED
|
@@ -0,0 +1,312 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* A pull request's copy of Terraform roots (#4762): each root initialised
|
|
3
|
+
* under its backend's state key with the environment's suffix, planned,
|
|
4
|
+
* applied, and destroyed when the pull request closes or its TTL passes.
|
|
5
|
+
* chant core runs the environment (`components/pr-env-run`: the gate, the
|
|
6
|
+
* records, the TTL, the sweep) and its component rules
|
|
7
|
+
* (`components/pr-env-components`); this module is the Terraform part.
|
|
8
|
+
*
|
|
9
|
+
* Why a key suffix and not a workspace: the other stages read a root's state
|
|
10
|
+
* from its backend block, and a workspace would put the copy in the same
|
|
11
|
+
* namespace as the root under a name only the binary resolves. A suffixed
|
|
12
|
+
* key is a state of its own that the backend block, the lock and the bucket
|
|
13
|
+
* listing all name plainly.
|
|
14
|
+
*
|
|
15
|
+
* s3, azurerm `key`: `envs/dev/terraform.tfstate` becomes
|
|
16
|
+
* `envs/dev/terraform-pr-7.tfstate`
|
|
17
|
+
* gcs `prefix`: `envs/dev` becomes `envs/dev-pr-7`
|
|
18
|
+
* local `path`
|
|
19
|
+
* http `address`, when it is a GitLab project's state API: the
|
|
20
|
+
* copy is the state `<name>-pr-7`, and its lock and unlock
|
|
21
|
+
* addresses take the same name
|
|
22
|
+
*
|
|
23
|
+
* The copy's `-backend-config` comes from the root's code and the job's
|
|
24
|
+
* `TF_CLI_ARGS` (`declaredBackend`). A root another tool prepares, such as
|
|
25
|
+
* a Terragrunt unit whose `remote_state` reads the suffix from the
|
|
26
|
+
* environment, passes `prepare`; its recorded backend must then carry the
|
|
27
|
+
* suffix, or the copy is refused, so a copy never plans, applies or destroys
|
|
28
|
+
* at a root's own key.
|
|
29
|
+
*
|
|
30
|
+
* Lifted from terragucci's `ephemeral.ts` (`suffixedKey`, `KEY_ATTRIBUTE`,
|
|
31
|
+
* `gitlabCopy`, `copyBackend`, `unitBackend`, `carriesSuffix`, `planCopy`,
|
|
32
|
+
* `copyAdapter`, `ephemeralTeardown`), so terragucci deletes its copy and
|
|
33
|
+
* keeps its words: its checkout of the pull request, its synth command, its
|
|
34
|
+
* Terragrunt preparation and its binary per root.
|
|
35
|
+
*/
|
|
36
|
+
import { join } from "node:path";
|
|
37
|
+
import { componentsPrEnvAdapter, componentsPrEnvTeardown, type PrEnvComponentPlan } from "@intentius/chant/components/pr-env-components";
|
|
38
|
+
import type { PrEnvAdapter } from "@intentius/chant/components/pr-env-run";
|
|
39
|
+
import type { LivePrEnvironment, PrEnvTeardownAdapter } from "@intentius/chant/components/pr-environment";
|
|
40
|
+
import type { BinaryExec } from "./state/exec";
|
|
41
|
+
import { declaredBackend, initialisedBackend } from "./state/backend";
|
|
42
|
+
import { isGitLabAddress, shownUrl, suffixedGitLabAddress } from "./state/gitlab";
|
|
43
|
+
import { applyPlanFile, planCopy } from "./state-ops";
|
|
44
|
+
|
|
45
|
+
/** The backends a copy can be given a key of its own on, and the attribute that holds the key. */
|
|
46
|
+
export const KEY_ATTRIBUTE: Readonly<Record<string, string>> = { s3: "key", azurerm: "key", gcs: "prefix", local: "path", http: "address" };
|
|
47
|
+
|
|
48
|
+
/** The http backend's attributes that name the state too: its lock's, `<address>/lock` on GitLab. */
|
|
49
|
+
export const HTTP_LOCK_ATTRIBUTES = ["lock_address", "unlock_address"] as const;
|
|
50
|
+
|
|
51
|
+
const BACKENDS = Object.keys(KEY_ATTRIBUTE).join(", ");
|
|
52
|
+
|
|
53
|
+
/** A root no copy can be made of, or a prepared root whose key is its own. A config error: nothing planned. */
|
|
54
|
+
export class CopyRefusedError extends Error {
|
|
55
|
+
constructor(message: string) {
|
|
56
|
+
super(message);
|
|
57
|
+
this.name = "CopyRefusedError";
|
|
58
|
+
}
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
/**
|
|
62
|
+
* A state key with the suffix: before a closing `.tfstate`, else at the end.
|
|
63
|
+
* `envs/dev/terraform.tfstate` becomes `envs/dev/terraform-pr-7.tfstate`.
|
|
64
|
+
*/
|
|
65
|
+
export function suffixedKey(key: string, suffix: string): string {
|
|
66
|
+
const k = key.replace(/\/+$/, "");
|
|
67
|
+
return /\.tfstate$/.test(k) ? k.replace(/\.tfstate$/, `-${suffix}.tfstate`) : `${k}-${suffix}`;
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
/** Whether a key or location carries a copy's suffix as {@link suffixedKey} puts it. A GitLab lock address is read without its `/lock`. */
|
|
71
|
+
export const carriesSuffix = (key: string, suffix: string): boolean => new RegExp(`-${suffix.replace(/[.*+?^${}()|[\]\\]/g, "\\$&")}(\\.tfstate)?/*$`).test(key.replace(/\/lock\/?$/, ""));
|
|
72
|
+
|
|
73
|
+
/** Where a copy's state is, for messages and the record. */
|
|
74
|
+
function locationOf(type: string, config: Record<string, unknown>, key: string): string {
|
|
75
|
+
const s = (k: string): string | undefined => (typeof config[k] === "string" && config[k] !== "" ? (config[k] as string) : undefined);
|
|
76
|
+
if (type === "http") return shownUrl(key);
|
|
77
|
+
if (type === "s3" && s("bucket")) return `s3://${s("bucket")}/${key}`;
|
|
78
|
+
if (type === "gcs" && s("bucket")) return `gs://${s("bucket")}/${key}`;
|
|
79
|
+
if (type === "azurerm" && s("container_name")) return `az://${s("storage_account_name") ?? "azure"}/${s("container_name")}/${key}`;
|
|
80
|
+
return key;
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
/** A copy's backend: the attribute and suffixed key, any other attribute it sets, and where the copy is. */
|
|
84
|
+
export interface CopyBackend {
|
|
85
|
+
type: string;
|
|
86
|
+
attribute: string;
|
|
87
|
+
key: string;
|
|
88
|
+
location: string;
|
|
89
|
+
/** Further `-backend-config` pairs: a GitLab copy's lock addresses. */
|
|
90
|
+
extra?: Record<string, string>;
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
/**
|
|
94
|
+
* A GitLab-managed state's attributes for its copy: the address and each
|
|
95
|
+
* lock address with the state name suffixed. Each is the configured one,
|
|
96
|
+
* else its `TF_HTTP_*` variable, as the binary reads it.
|
|
97
|
+
*/
|
|
98
|
+
export function gitlabCopy(root: string, config: Record<string, unknown>, env: NodeJS.ProcessEnv, suffix: string): { key: string; extra: Record<string, string> } {
|
|
99
|
+
const given = (name: string): string | undefined => (typeof config[name] === "string" && config[name] !== "" ? (config[name] as string) : env[`TF_HTTP_${name.toUpperCase()}`] || undefined);
|
|
100
|
+
const address = given("address");
|
|
101
|
+
if (!address) throw new CopyRefusedError(`${root}'s http backend names no address, in the block or TF_HTTP_ADDRESS, so there is no state name to give its copy a suffix`);
|
|
102
|
+
const key = suffixedGitLabAddress(address, suffix);
|
|
103
|
+
if (!key) throw new CopyRefusedError(`${root}'s http backend address ${shownUrl(address)} is not a GitLab project's state API (.../projects/<id>/terraform/state/<name>), and a copy of an http backend's state is made on GitLab-managed state only`);
|
|
104
|
+
const extra: Record<string, string> = {};
|
|
105
|
+
for (const name of HTTP_LOCK_ATTRIBUTES) {
|
|
106
|
+
const v = given(name);
|
|
107
|
+
if (!v) continue;
|
|
108
|
+
const suffixed = suffixedGitLabAddress(v, suffix);
|
|
109
|
+
if (!suffixed || suffixedGitLabAddress(v.replace(/\/lock\/?$/, ""), suffix) !== key) throw new CopyRefusedError(`${root}'s http backend ${name} ${shownUrl(v)} is not the lock of its state, ${shownUrl(address)}/lock, so its copy's lock could not be named`);
|
|
110
|
+
extra[name] = suffixed;
|
|
111
|
+
}
|
|
112
|
+
return { key, extra };
|
|
113
|
+
}
|
|
114
|
+
|
|
115
|
+
/**
|
|
116
|
+
* The `-backend-config` a root's copy is initialised with, from the backend
|
|
117
|
+
* its code declares with the job's flags over it. Throws
|
|
118
|
+
* {@link CopyRefusedError} for a root no copy can be made of: a `cloud`
|
|
119
|
+
* block, a backend with no key attribute, a block that names no key.
|
|
120
|
+
*/
|
|
121
|
+
export function copyBackend(dir: string, root: string, suffix: string, env: NodeJS.ProcessEnv = {}, binary?: string): CopyBackend {
|
|
122
|
+
const b = declaredBackend(dir, { env, ...(binary ? { binary } : {}) });
|
|
123
|
+
if (b?.type === "cloud") throw new CopyRefusedError(`${root} keeps its state in HCP Terraform (a cloud block), where a state is a workspace and not a key; a copy is made of roots whose backend is ${BACKENDS}`);
|
|
124
|
+
const type = b?.type ?? "local";
|
|
125
|
+
const attribute = KEY_ATTRIBUTE[type];
|
|
126
|
+
if (!attribute) throw new CopyRefusedError(`${root}'s backend is ${type}, and a copy is given a state key of its own on ${BACKENDS} backends only`);
|
|
127
|
+
const config = b?.config ?? {};
|
|
128
|
+
if (type === "http") {
|
|
129
|
+
const copy = gitlabCopy(root, config, env, suffix);
|
|
130
|
+
return { type, attribute, key: copy.key, location: shownUrl(copy.key), ...(Object.keys(copy.extra).length ? { extra: copy.extra } : {}) };
|
|
131
|
+
}
|
|
132
|
+
const raw = config[attribute];
|
|
133
|
+
const given = typeof raw === "string" && raw !== "" ? raw : type === "local" ? "terraform.tfstate" : undefined;
|
|
134
|
+
if (!given) throw new CopyRefusedError(`${root}'s ${type} backend names no ${attribute}, so there is no key to give its copy a suffix; write the ${attribute} in the block`);
|
|
135
|
+
const key = suffixedKey(given, suffix);
|
|
136
|
+
return { type, attribute, key, location: locationOf(type, config, key) };
|
|
137
|
+
}
|
|
138
|
+
|
|
139
|
+
/**
|
|
140
|
+
* The backend a prepared root was initialised with, checked to be its copy:
|
|
141
|
+
* its key attribute, and a GitLab state's lock addresses, carry the suffix.
|
|
142
|
+
* Throws {@link CopyRefusedError} when it does not; `hint` says how to make
|
|
143
|
+
* the product's config read the suffix.
|
|
144
|
+
*/
|
|
145
|
+
export function preparedCopyBackend(dir: string, root: string, suffix: string, env: NodeJS.ProcessEnv = {}, hint?: string): CopyBackend {
|
|
146
|
+
const how = hint ? `; ${hint}` : "";
|
|
147
|
+
const b = initialisedBackend(dir, env);
|
|
148
|
+
if (!b) throw new CopyRefusedError(`${root}: it was prepared with no backend the binary recorded, so its copy has no key of its own${how}`);
|
|
149
|
+
const attribute = KEY_ATTRIBUTE[b.type];
|
|
150
|
+
if (!attribute) throw new CopyRefusedError(`${root}'s backend is ${b.type}, and a copy is given a state key of its own on ${BACKENDS} backends only`);
|
|
151
|
+
const raw = b.config[attribute];
|
|
152
|
+
const key = typeof raw === "string" ? raw : "";
|
|
153
|
+
if (b.type === "http" && !isGitLabAddress(key)) throw new CopyRefusedError(`${root}'s http backend ${key ? `address ${shownUrl(key)} is not a GitLab project's state API` : "names no address"}, and a copy of an http backend's state is made on GitLab-managed state only`);
|
|
154
|
+
if (!carriesSuffix(key, suffix)) throw new CopyRefusedError(`${root}: its ${b.type} backend's ${attribute} is ${key ? (b.type === "http" ? shownUrl(key) : key) : "empty"} as it was prepared, which is the root's own state, so its copy is refused${how}`);
|
|
155
|
+
if (b.type === "http") {
|
|
156
|
+
const own = HTTP_LOCK_ATTRIBUTES.map((a) => b.config[a]).find((l): l is string => typeof l === "string" && !carriesSuffix(l, suffix));
|
|
157
|
+
if (own) throw new CopyRefusedError(`${root}: its http backend's lock address is ${shownUrl(own)} as it was prepared, the lock of the root's own state, so its copy is refused; make lock_address and unlock_address read the suffix as the address does`);
|
|
158
|
+
}
|
|
159
|
+
return { type: b.type, attribute, key, location: locationOf(b.type, b.config, key) };
|
|
160
|
+
}
|
|
161
|
+
|
|
162
|
+
/** One root of a copy: where the binary runs it, and how. */
|
|
163
|
+
export interface CopyRoot {
|
|
164
|
+
/** The root's name, as the records and the report name it. */
|
|
165
|
+
root: string;
|
|
166
|
+
/** The root's directory in the checkout planned. */
|
|
167
|
+
dir: string;
|
|
168
|
+
binary: string;
|
|
169
|
+
env?: NodeJS.ProcessEnv;
|
|
170
|
+
/**
|
|
171
|
+
* Prepare a root another tool initialises (a Terragrunt unit), with the
|
|
172
|
+
* suffix set as its config reads it: where the binary then runs, and with
|
|
173
|
+
* which env. Its recorded backend must carry the suffix.
|
|
174
|
+
*/
|
|
175
|
+
prepare?: (suffix: string) => Promise<{ dir: string; env: NodeJS.ProcessEnv }>;
|
|
176
|
+
}
|
|
177
|
+
|
|
178
|
+
export interface CopyPlanInput {
|
|
179
|
+
copy: CopyRoot;
|
|
180
|
+
/** The environment's suffix: `pr-<n>`. */
|
|
181
|
+
suffix: string;
|
|
182
|
+
/** Where the plan file goes. */
|
|
183
|
+
planFile: string;
|
|
184
|
+
destroy?: boolean;
|
|
185
|
+
lockTimeout?: string | false;
|
|
186
|
+
exec?: BinaryExec;
|
|
187
|
+
/** How the product's config reads the suffix, for a prepared root's refusal. */
|
|
188
|
+
hint?: string;
|
|
189
|
+
}
|
|
190
|
+
|
|
191
|
+
/**
|
|
192
|
+
* Plan one root's copy: init it under its suffixed key (or prepare it, then
|
|
193
|
+
* check its key), plan (a destroy with `destroy`), and read the plan's
|
|
194
|
+
* counts. A run of the binary that fails is the plan's `error`; a root no
|
|
195
|
+
* copy can be made of throws {@link CopyRefusedError}.
|
|
196
|
+
*/
|
|
197
|
+
export async function terraformCopyPlan(input: CopyPlanInput): Promise<PrEnvComponentPlan> {
|
|
198
|
+
const { copy, suffix } = input;
|
|
199
|
+
const exec = input.exec;
|
|
200
|
+
const none = { component: copy.root, planDigest: "", create: 0, update: 0, replace: 0, delete: 0 };
|
|
201
|
+
let dir = copy.dir;
|
|
202
|
+
let env = copy.env ?? process.env;
|
|
203
|
+
let backend: CopyBackend;
|
|
204
|
+
let backendConfig: Record<string, string> = {};
|
|
205
|
+
if (copy.prepare) {
|
|
206
|
+
let prepared: { dir: string; env: NodeJS.ProcessEnv };
|
|
207
|
+
try {
|
|
208
|
+
prepared = await copy.prepare(suffix);
|
|
209
|
+
} catch (e) {
|
|
210
|
+
return { ...none, error: (e as Error).message, apply: async () => {} };
|
|
211
|
+
}
|
|
212
|
+
({ dir, env } = prepared);
|
|
213
|
+
backend = preparedCopyBackend(dir, copy.root, suffix, env, input.hint);
|
|
214
|
+
} else {
|
|
215
|
+
backend = copyBackend(dir, copy.root, suffix, env, copy.binary);
|
|
216
|
+
backendConfig = { [backend.attribute]: backend.key, ...backend.extra };
|
|
217
|
+
}
|
|
218
|
+
const run = { root: copy.root, binary: copy.binary, dir, env, ...(exec ? { exec } : {}) };
|
|
219
|
+
try {
|
|
220
|
+
const read = await planCopy({ ...run, backendConfig, planFile: input.planFile, ...(copy.prepare ? { init: false } : {}), ...(input.destroy ? { destroy: true } : {}), ...(input.lockTimeout !== undefined ? { lockTimeout: input.lockTimeout } : {}) });
|
|
221
|
+
const count = (action: string): number => read.part.entries.filter((e) => e.action === action).length;
|
|
222
|
+
return {
|
|
223
|
+
component: copy.root,
|
|
224
|
+
planDigest: read.planDigest,
|
|
225
|
+
create: count("create"),
|
|
226
|
+
update: count("update"),
|
|
227
|
+
replace: count("replace"),
|
|
228
|
+
delete: count("delete"),
|
|
229
|
+
location: backend.location,
|
|
230
|
+
apply: () => applyPlanFile({ ...run, planFile: input.planFile, ...(input.lockTimeout !== undefined ? { lockTimeout: input.lockTimeout } : {}) }),
|
|
231
|
+
};
|
|
232
|
+
} catch (e) {
|
|
233
|
+
return { ...none, location: backend.location, error: (e as Error).message, apply: async () => {} };
|
|
234
|
+
}
|
|
235
|
+
}
|
|
236
|
+
|
|
237
|
+
export interface TerraformPrEnvInput {
|
|
238
|
+
/** The roots of the copy, in apply order, from the pull request's head. */
|
|
239
|
+
roots(target: { pr: number; env: string; head: string }): Promise<CopyRoot[]>;
|
|
240
|
+
/** A directory the plan files go in. */
|
|
241
|
+
work: string;
|
|
242
|
+
exec?: BinaryExec;
|
|
243
|
+
lockTimeout?: string | false;
|
|
244
|
+
hint?: string;
|
|
245
|
+
/** What a copy with no roots says. */
|
|
246
|
+
nothing?: string;
|
|
247
|
+
log?: (line: string) => void;
|
|
248
|
+
}
|
|
249
|
+
|
|
250
|
+
/**
|
|
251
|
+
* The adapter `prEnvUp` takes for a copy of Terraform roots: each root's copy
|
|
252
|
+
* planned under `-<env>` (`pr-<n>`) and applied in order. `refused` holds a
|
|
253
|
+
* {@link CopyRefusedError}, which a product raises as a config error.
|
|
254
|
+
*/
|
|
255
|
+
export function terraformPrEnvAdapter(input: TerraformPrEnvInput): PrEnvAdapter & { refused?: Error } {
|
|
256
|
+
let copies = new Map<string, CopyRoot>();
|
|
257
|
+
return componentsPrEnvAdapter({
|
|
258
|
+
async components(target) {
|
|
259
|
+
const roots = await input.roots(target);
|
|
260
|
+
copies = new Map(roots.map((r) => [r.root, r]));
|
|
261
|
+
return roots.map((r) => r.root);
|
|
262
|
+
},
|
|
263
|
+
plan: (component, i, target) =>
|
|
264
|
+
terraformCopyPlan({ copy: copies.get(component)!, suffix: target.env, planFile: join(input.work, `${i}.tfplan`), ...(input.exec ? { exec: input.exec } : {}), ...(input.lockTimeout !== undefined ? { lockTimeout: input.lockTimeout } : {}), ...(input.hint ? { hint: input.hint } : {}) }),
|
|
265
|
+
...(input.nothing ? { nothing: () => input.nothing! } : {}),
|
|
266
|
+
...(input.log ? { log: input.log } : {}),
|
|
267
|
+
});
|
|
268
|
+
}
|
|
269
|
+
|
|
270
|
+
export interface TerraformPrEnvTeardownInput {
|
|
271
|
+
/** The name outcomes are attributed to: the product's. */
|
|
272
|
+
name: string;
|
|
273
|
+
/**
|
|
274
|
+
* The roots as the commit the copy last applied from has them, with what
|
|
275
|
+
* it holds released by `done`. A root missing from `roots` fails alone.
|
|
276
|
+
*/
|
|
277
|
+
checkout(live: LivePrEnvironment): Promise<{ roots: CopyRoot[]; done?: () => void }>;
|
|
278
|
+
work: string;
|
|
279
|
+
exec?: BinaryExec;
|
|
280
|
+
lockTimeout?: string | false;
|
|
281
|
+
hint?: string;
|
|
282
|
+
log?: (line: string) => void;
|
|
283
|
+
}
|
|
284
|
+
|
|
285
|
+
/**
|
|
286
|
+
* The teardown `prEnvDown` and `prEnvSweep` take for a copy of Terraform
|
|
287
|
+
* roots: a root whose recorded state location carries `-<env>` is destroyed,
|
|
288
|
+
* through a planned destroy of its copy, in reverse apply order; any other
|
|
289
|
+
* is a hole, so nothing at a root's own key is destroyed. It stands in for
|
|
290
|
+
* the terraform lexicon's own teardown.
|
|
291
|
+
*/
|
|
292
|
+
export function terraformPrEnvTeardown(input: TerraformPrEnvTeardownInput): PrEnvTeardownAdapter {
|
|
293
|
+
return componentsPrEnvTeardown({
|
|
294
|
+
name: input.name,
|
|
295
|
+
lexicons: ["terraform"],
|
|
296
|
+
type: "terraform_root",
|
|
297
|
+
owns: (location, env) => carriesSuffix(location, env),
|
|
298
|
+
async checkout(live) {
|
|
299
|
+
const code = await input.checkout(live);
|
|
300
|
+
const roots = new Map(code.roots.map((r) => [r.root, r]));
|
|
301
|
+
return {
|
|
302
|
+
async plan(component, i) {
|
|
303
|
+
const copy = roots.get(component);
|
|
304
|
+
if (!copy) throw new Error(`${component} is not at ${live.commit.slice(0, 8)}, so its copy cannot be planned`);
|
|
305
|
+
return terraformCopyPlan({ copy, suffix: live.env, planFile: join(input.work, `destroy-${i}.tfplan`), destroy: true, ...(input.exec ? { exec: input.exec } : {}), ...(input.lockTimeout !== undefined ? { lockTimeout: input.lockTimeout } : {}), ...(input.hint ? { hint: input.hint } : {}) });
|
|
306
|
+
},
|
|
307
|
+
...(code.done ? { done: code.done } : {}),
|
|
308
|
+
};
|
|
309
|
+
},
|
|
310
|
+
...(input.log ? { log: input.log } : {}),
|
|
311
|
+
});
|
|
312
|
+
}
|
|
@@ -0,0 +1,226 @@
|
|
|
1
|
+
// The GitLab-managed state store (#4773), against a GitLab state API fake listening on 127.0.0.1: serial, versions,
|
|
2
|
+
// the lock probe and release, the lock a run takes, a write under it, and the states kept beside it. No real GitLab.
|
|
3
|
+
import { createServer, type IncomingMessage, type Server } from "node:http";
|
|
4
|
+
import type { AddressInfo } from "node:net";
|
|
5
|
+
import { afterEach, beforeEach, describe, expect, it } from "vitest";
|
|
6
|
+
import { gitlabBesideAddress, gitlabState, gitlabStore, isGitLabAddress, shownUrl, suffixedGitLabAddress, type GitLabState } from "./gitlab";
|
|
7
|
+
import { stateStore } from "./store";
|
|
8
|
+
|
|
9
|
+
const USER = "gitlab-ci-token";
|
|
10
|
+
const TOKEN = "glcbt-secret";
|
|
11
|
+
const BASIC = `Basic ${Buffer.from(`${USER}:${TOKEN}`).toString("base64")}`;
|
|
12
|
+
|
|
13
|
+
interface Fake {
|
|
14
|
+
url: string;
|
|
15
|
+
/** Each state by name: its versions by serial, the latest last. */
|
|
16
|
+
states: Map<string, Array<{ serial: number; text: string }>>;
|
|
17
|
+
/** Each held lock by state name: its info as taken. */
|
|
18
|
+
locks: Map<string, Record<string, unknown>>;
|
|
19
|
+
requests: Array<{ method: string; path: string; query: string; authorization?: string }>;
|
|
20
|
+
close(): Promise<void>;
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
const body = (req: IncomingMessage): Promise<string> =>
|
|
24
|
+
new Promise((res) => {
|
|
25
|
+
const parts: Buffer[] = [];
|
|
26
|
+
req.on("data", (c: Buffer) => parts.push(c));
|
|
27
|
+
req.on("end", () => res(Buffer.concat(parts).toString("utf-8")));
|
|
28
|
+
});
|
|
29
|
+
|
|
30
|
+
/** GitLab's Terraform state API as the http backend calls it, for project 7. */
|
|
31
|
+
async function startFakeGitLab(): Promise<Fake> {
|
|
32
|
+
const states: Fake["states"] = new Map();
|
|
33
|
+
const locks: Fake["locks"] = new Map();
|
|
34
|
+
const requests: Fake["requests"] = [];
|
|
35
|
+
const server: Server = createServer(async (req, res) => {
|
|
36
|
+
const u = new URL(req.url ?? "/", "http://127.0.0.1");
|
|
37
|
+
const text = await body(req);
|
|
38
|
+
requests.push({ method: req.method ?? "", path: u.pathname, query: u.search, ...(req.headers.authorization ? { authorization: req.headers.authorization } : {}) });
|
|
39
|
+
const send = (status: number, out = "") => {
|
|
40
|
+
res.writeHead(status, out ? { "content-type": "application/json" } : {});
|
|
41
|
+
res.end(req.method === "HEAD" ? undefined : out);
|
|
42
|
+
};
|
|
43
|
+
if (req.headers.authorization !== BASIC) return send(401, JSON.stringify({ message: "401 Unauthorized" }));
|
|
44
|
+
const m = u.pathname.match(/^\/api\/v4\/projects\/7\/terraform\/state\/([^/]+)(\/lock|\/versions\/([^/]+))?$/);
|
|
45
|
+
if (!m) return send(404);
|
|
46
|
+
const name = decodeURIComponent(m[1]!);
|
|
47
|
+
const versions = states.get(name) ?? [];
|
|
48
|
+
if (m[2] === "/lock") {
|
|
49
|
+
const held = locks.get(name);
|
|
50
|
+
if (req.method === "LOCK" || req.method === "POST") {
|
|
51
|
+
if (held) return send(409, JSON.stringify(held));
|
|
52
|
+
locks.set(name, JSON.parse(text) as Record<string, unknown>);
|
|
53
|
+
return send(200);
|
|
54
|
+
}
|
|
55
|
+
if (req.method === "UNLOCK" || req.method === "DELETE") {
|
|
56
|
+
const id = (JSON.parse(text || "{}") as { ID?: string }).ID ?? u.searchParams.get("ID");
|
|
57
|
+
if (held && held.ID !== id) return send(409, JSON.stringify(held));
|
|
58
|
+
locks.delete(name);
|
|
59
|
+
return send(200);
|
|
60
|
+
}
|
|
61
|
+
return send(405);
|
|
62
|
+
}
|
|
63
|
+
if (m[3] !== undefined) {
|
|
64
|
+
const v = versions.find((x) => String(x.serial) === decodeURIComponent(m[3]!));
|
|
65
|
+
return v ? send(200, v.text) : send(404);
|
|
66
|
+
}
|
|
67
|
+
if (req.method === "GET") {
|
|
68
|
+
const latest = versions.at(-1);
|
|
69
|
+
return latest ? send(200, latest.text) : send(404);
|
|
70
|
+
}
|
|
71
|
+
if (req.method === "POST") {
|
|
72
|
+
const held = locks.get(name);
|
|
73
|
+
if (held && held.ID !== (u.searchParams.get("ID") ?? undefined)) return send(409, JSON.stringify(held));
|
|
74
|
+
const serial = (JSON.parse(text) as { serial?: number }).serial ?? versions.length;
|
|
75
|
+
states.set(name, [...versions, { serial, text }]);
|
|
76
|
+
return send(200, "{}");
|
|
77
|
+
}
|
|
78
|
+
if (req.method === "DELETE") {
|
|
79
|
+
if (!states.has(name)) return send(404);
|
|
80
|
+
states.delete(name);
|
|
81
|
+
return send(204);
|
|
82
|
+
}
|
|
83
|
+
return send(405);
|
|
84
|
+
});
|
|
85
|
+
await new Promise<void>((r) => server.listen(0, "127.0.0.1", () => r()));
|
|
86
|
+
const port = (server.address() as AddressInfo).port;
|
|
87
|
+
return { url: `http://127.0.0.1:${port}`, states, locks, requests, close: () => new Promise((r) => server.close(() => r())) };
|
|
88
|
+
}
|
|
89
|
+
|
|
90
|
+
let gl: Fake;
|
|
91
|
+
let s: GitLabState;
|
|
92
|
+
|
|
93
|
+
const config = (name: string, extra: Record<string, unknown> = {}): Record<string, unknown> => {
|
|
94
|
+
const address = `${gl.url}/api/v4/projects/7/terraform/state/${name}`;
|
|
95
|
+
return { address, lock_address: `${address}/lock`, unlock_address: `${address}/lock`, username: USER, password: TOKEN, ...extra };
|
|
96
|
+
};
|
|
97
|
+
|
|
98
|
+
const state = (serial: number, extra: Record<string, unknown> = {}) => JSON.stringify({ version: 4, serial, lineage: "l-1", resources: [], ...extra });
|
|
99
|
+
|
|
100
|
+
beforeEach(async () => {
|
|
101
|
+
gl = await startFakeGitLab();
|
|
102
|
+
s = gitlabState(config("prod"), {})!;
|
|
103
|
+
});
|
|
104
|
+
|
|
105
|
+
afterEach(async () => {
|
|
106
|
+
await gl.close();
|
|
107
|
+
});
|
|
108
|
+
|
|
109
|
+
describe("gitlabState", () => {
|
|
110
|
+
it("reads the backend's configuration, else the job's TF_HTTP_* variables", () => {
|
|
111
|
+
expect(s).toMatchObject({ backend: "http", name: "prod", lockMethod: "LOCK", unlockMethod: "UNLOCK", auth: BASIC });
|
|
112
|
+
const address = `${gl.url}/api/v4/projects/7/terraform/state/stage%20one`;
|
|
113
|
+
const fromEnv = gitlabState({}, { TF_HTTP_ADDRESS: address, TF_HTTP_LOCK_ADDRESS: `${address}/lock`, TF_HTTP_UNLOCK_ADDRESS: `${address}/lock`, TF_HTTP_LOCK_METHOD: "POST", TF_HTTP_UNLOCK_METHOD: "DELETE", TF_HTTP_USERNAME: USER, TF_HTTP_PASSWORD: TOKEN })!;
|
|
114
|
+
expect(fromEnv).toMatchObject({ name: "stage one", lockMethod: "POST", unlockMethod: "DELETE", auth: BASIC, lockAddress: `${address}/lock` });
|
|
115
|
+
expect(gitlabState({ address: "https://state.example.com/tf/prod" }, {})).toBeUndefined();
|
|
116
|
+
expect(isGitLabAddress("https://gitlab.example.com/api/v4/projects/group%2Fapp/terraform/state/prod")).toBe(true);
|
|
117
|
+
expect(isGitLabAddress("not a url")).toBe(false);
|
|
118
|
+
});
|
|
119
|
+
|
|
120
|
+
it("names a suffixed copy of the state and its lock, and shows a URL without its credentials", () => {
|
|
121
|
+
const a = "https://gitlab.example.com/api/v4/projects/7/terraform/state/prod";
|
|
122
|
+
expect(suffixedGitLabAddress(a, "pr-12")).toBe(`${a}-pr-12`);
|
|
123
|
+
expect(suffixedGitLabAddress(`${a}/lock`, "pr-12")).toBe(`${a}-pr-12/lock`);
|
|
124
|
+
expect(suffixedGitLabAddress("https://example.com/other", "x")).toBeUndefined();
|
|
125
|
+
expect(shownUrl("https://u:p@gitlab.example.com/api/v4/x?ID=1")).toBe("https://gitlab.example.com/api/v4/x");
|
|
126
|
+
});
|
|
127
|
+
});
|
|
128
|
+
|
|
129
|
+
describe("gitlabStore reads", () => {
|
|
130
|
+
it("answers the serial and lineage of the state GitLab holds, and undefined when it holds none", async () => {
|
|
131
|
+
const store = gitlabStore(s);
|
|
132
|
+
expect(await store.serial()).toBeUndefined();
|
|
133
|
+
gl.states.set("prod", [{ serial: 3, text: state(3) }, { serial: 4, text: state(4) }]);
|
|
134
|
+
expect(await store.serial()).toEqual({ serial: 4, lineage: "l-1" });
|
|
135
|
+
expect(gl.requests.every((r) => r.authorization === BASIC)).toBe(true);
|
|
136
|
+
});
|
|
137
|
+
|
|
138
|
+
it("checks a version with a HEAD and reads it by its serial", async () => {
|
|
139
|
+
gl.states.set("prod", [{ serial: 3, text: state(3) }, { serial: 4, text: state(4) }]);
|
|
140
|
+
const store = gitlabStore(s);
|
|
141
|
+
expect(await store.hasVersion(3)).toBe(true);
|
|
142
|
+
expect(await store.hasVersion("9")).toBe(false);
|
|
143
|
+
expect(gl.requests.filter((r) => r.path.endsWith("/versions/3")).map((r) => r.method)).toEqual(["HEAD"]);
|
|
144
|
+
expect(JSON.parse((await store.readVersion("3"))!)).toMatchObject({ serial: 3 });
|
|
145
|
+
expect(await store.readVersion(9)).toBeUndefined();
|
|
146
|
+
});
|
|
147
|
+
|
|
148
|
+
it("names the credentials, never their values, when GitLab refuses them", async () => {
|
|
149
|
+
const store = gitlabStore(gitlabState(config("prod", { password: "wrong" }), {})!);
|
|
150
|
+
const err = await store.serial().catch((e: Error) => e);
|
|
151
|
+
expect((err as Error).message).toMatch(/^GitLab answered 401 to a read of http:\/\/127\.0\.0\.1:\d+\/api\/v4\/projects\/7\/terraform\/state\/prod: the backend's username and password/);
|
|
152
|
+
expect((err as Error).message).not.toContain("wrong");
|
|
153
|
+
});
|
|
154
|
+
});
|
|
155
|
+
|
|
156
|
+
describe("gitlabStore locks", () => {
|
|
157
|
+
it("probes a free lock by taking and releasing it, and reads a held one's holder", async () => {
|
|
158
|
+
const store = gitlabStore(s, undefined, "terragucci");
|
|
159
|
+
expect(await store.probeLock()).toBeUndefined();
|
|
160
|
+
const probe = gl.requests.filter((r) => r.path.endsWith("/lock")).map((r) => r.method);
|
|
161
|
+
expect(probe).toEqual(["LOCK", "UNLOCK"]);
|
|
162
|
+
expect(gl.locks.size).toBe(0);
|
|
163
|
+
gl.locks.set("prod", { ID: "abc", Operation: "OperationTypeApply", Who: "runner@ci", Created: "2026-11-08T10:00:00Z", Info: "" });
|
|
164
|
+
expect(JSON.parse((await store.probeLock())!)).toEqual({ ID: "abc", Who: "runner@ci", Created: "2026-11-08T10:00:00Z" });
|
|
165
|
+
expect(await store.heldLock()).toEqual({ id: "abc", info: JSON.stringify({ ID: "abc", Who: "runner@ci", Created: "2026-11-08T10:00:00Z" }) });
|
|
166
|
+
});
|
|
167
|
+
|
|
168
|
+
it("releases a lock by its ID, and throws when another ID holds it", async () => {
|
|
169
|
+
const store = gitlabStore(s);
|
|
170
|
+
gl.locks.set("prod", { ID: "abc" });
|
|
171
|
+
await expect(store.releaseLock("other")).rejects.toThrow(/did not release lock other .*another lock holds the state now/);
|
|
172
|
+
await store.releaseLock("abc");
|
|
173
|
+
expect(gl.locks.size).toBe(0);
|
|
174
|
+
});
|
|
175
|
+
|
|
176
|
+
it("refuses a lock call on a backend with no lock address", async () => {
|
|
177
|
+
const store = gitlabStore(gitlabState(config("prod", { lock_address: undefined, unlock_address: undefined }), {})!);
|
|
178
|
+
await expect(store.probeLock()).rejects.toThrow(/names no lock_address/);
|
|
179
|
+
});
|
|
180
|
+
|
|
181
|
+
it("takes the lock, writes the state with the lock's ID, and releases it", async () => {
|
|
182
|
+
const store = stateStoreOf(s);
|
|
183
|
+
expect(store.backend).toBe("http");
|
|
184
|
+
expect(await store.lock(JSON.stringify({ ID: "run-1", Who: "chant" }))).toBe(true);
|
|
185
|
+
expect(await gitlabStore(s).lock(JSON.stringify({ ID: "run-2" }))).toBe(false);
|
|
186
|
+
// The binary's write with -lock=false carries no ID, which GitLab refuses while the lock is held: the store writes.
|
|
187
|
+
const bare = await fetch(s.address, { method: "POST", headers: { authorization: BASIC }, body: state(5) });
|
|
188
|
+
expect(bare.status).toBe(409);
|
|
189
|
+
await store.write!(state(5));
|
|
190
|
+
expect(gl.requests.at(-1)).toMatchObject({ method: "POST", query: "?ID=run-1" });
|
|
191
|
+
expect(gl.states.get("prod")!.at(-1)!.serial).toBe(5);
|
|
192
|
+
await store.unlock();
|
|
193
|
+
expect(gl.locks.size).toBe(0);
|
|
194
|
+
await expect(store.write!(state(6))).rejects.toThrow(/none is held/);
|
|
195
|
+
});
|
|
196
|
+
|
|
197
|
+
it("adopts a held lock, so unlock releases it by its ID", async () => {
|
|
198
|
+
gl.locks.set("prod", { ID: "earlier", Info: "chant compose" });
|
|
199
|
+
const store = gitlabStore(s);
|
|
200
|
+
const held = (await store.heldLock())!;
|
|
201
|
+
store.adopt(held);
|
|
202
|
+
await store.unlock();
|
|
203
|
+
expect(gl.locks.size).toBe(0);
|
|
204
|
+
});
|
|
205
|
+
});
|
|
206
|
+
|
|
207
|
+
describe("gitlabStore beside", () => {
|
|
208
|
+
it("keeps what is beside the state in another state of the project, the name with the suffix after it", async () => {
|
|
209
|
+
const store = gitlabStore(s);
|
|
210
|
+
expect(gitlabBesideAddress(s, ".chant-unwritten")).toBe(`${s.address}.chant-unwritten`);
|
|
211
|
+
expect(store.besideLocation(".chant-unwritten")).toBe(`${s.address}.chant-unwritten`);
|
|
212
|
+
expect(await store.readBeside(".chant-unwritten")).toBeUndefined();
|
|
213
|
+
await store.writeBeside(".chant-unwritten", state(8));
|
|
214
|
+
expect(gl.states.get("prod.chant-unwritten")!.at(-1)!.serial).toBe(8);
|
|
215
|
+
expect(JSON.parse((await store.readBeside(".chant-unwritten"))!)).toMatchObject({ serial: 8 });
|
|
216
|
+
await store.removeBeside(".chant-unwritten");
|
|
217
|
+
expect(gl.states.has("prod.chant-unwritten")).toBe(false);
|
|
218
|
+
await store.removeBeside(".chant-unwritten");
|
|
219
|
+
});
|
|
220
|
+
});
|
|
221
|
+
|
|
222
|
+
/** The store through the StateStore interface the other stores share. */
|
|
223
|
+
function stateStoreOf(g: GitLabState) {
|
|
224
|
+
const store: ReturnType<typeof stateStore> = gitlabStore(g);
|
|
225
|
+
return store;
|
|
226
|
+
}
|