@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/dist/integrity.json
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"algorithm": "sha256",
|
|
3
3
|
"artifacts": {
|
|
4
|
-
"manifest.json": "
|
|
4
|
+
"manifest.json": "298fb681bc936e8405d869e0dfc99b9f6c2772060d69bca84a70192aed53e9f8",
|
|
5
5
|
"meta.json": "ca3d163bab055381827226140568f3bef7eaac187cebd76878e0b63e9e442356",
|
|
6
6
|
"types/index.d.ts": "022927e6a7b351cc4cf52d5f973f60ccd140e5810e17b32fb8868965aa1db161",
|
|
7
7
|
"rules/plan-before-apply.ts": "7c10245b2c3335253b48ee7d8b075d89f43116bde20313a5d1031b343fef1282",
|
|
@@ -59,5 +59,5 @@
|
|
|
59
59
|
"rules/tf047.ts": "bfa86f85c4c5efff297280fc2f15104d6319fa96dfc444a31829fb7353224092",
|
|
60
60
|
"skills/chant-terraform.md": "53dd11c7b289652f92fdaf2c03766abda46d8964e31fe709e8b142ced4f19c50"
|
|
61
61
|
},
|
|
62
|
-
"composite": "
|
|
62
|
+
"composite": "9ef8c9b14b3511224a75923068f4a82271a673d160b81ad35d426ef21aedc432"
|
|
63
63
|
}
|
package/dist/manifest.json
CHANGED
package/dist/pr-env.d.ts
ADDED
|
@@ -0,0 +1,138 @@
|
|
|
1
|
+
import { type PrEnvComponentPlan } from "@intentius/chant/components/pr-env-components";
|
|
2
|
+
import type { PrEnvAdapter } from "@intentius/chant/components/pr-env-run";
|
|
3
|
+
import type { LivePrEnvironment, PrEnvTeardownAdapter } from "@intentius/chant/components/pr-environment";
|
|
4
|
+
import type { BinaryExec } from "./state/exec.js";
|
|
5
|
+
/** The backends a copy can be given a key of its own on, and the attribute that holds the key. */
|
|
6
|
+
export declare const KEY_ATTRIBUTE: Readonly<Record<string, string>>;
|
|
7
|
+
/** The http backend's attributes that name the state too: its lock's, `<address>/lock` on GitLab. */
|
|
8
|
+
export declare const HTTP_LOCK_ATTRIBUTES: readonly ["lock_address", "unlock_address"];
|
|
9
|
+
/** A root no copy can be made of, or a prepared root whose key is its own. A config error: nothing planned. */
|
|
10
|
+
export declare class CopyRefusedError extends Error {
|
|
11
|
+
constructor(message: string);
|
|
12
|
+
}
|
|
13
|
+
/**
|
|
14
|
+
* A state key with the suffix: before a closing `.tfstate`, else at the end.
|
|
15
|
+
* `envs/dev/terraform.tfstate` becomes `envs/dev/terraform-pr-7.tfstate`.
|
|
16
|
+
*/
|
|
17
|
+
export declare function suffixedKey(key: string, suffix: string): string;
|
|
18
|
+
/** Whether a key or location carries a copy's suffix as {@link suffixedKey} puts it. A GitLab lock address is read without its `/lock`. */
|
|
19
|
+
export declare const carriesSuffix: (key: string, suffix: string) => boolean;
|
|
20
|
+
/** A copy's backend: the attribute and suffixed key, any other attribute it sets, and where the copy is. */
|
|
21
|
+
export interface CopyBackend {
|
|
22
|
+
type: string;
|
|
23
|
+
attribute: string;
|
|
24
|
+
key: string;
|
|
25
|
+
location: string;
|
|
26
|
+
/** Further `-backend-config` pairs: a GitLab copy's lock addresses. */
|
|
27
|
+
extra?: Record<string, string>;
|
|
28
|
+
}
|
|
29
|
+
/**
|
|
30
|
+
* A GitLab-managed state's attributes for its copy: the address and each
|
|
31
|
+
* lock address with the state name suffixed. Each is the configured one,
|
|
32
|
+
* else its `TF_HTTP_*` variable, as the binary reads it.
|
|
33
|
+
*/
|
|
34
|
+
export declare function gitlabCopy(root: string, config: Record<string, unknown>, env: NodeJS.ProcessEnv, suffix: string): {
|
|
35
|
+
key: string;
|
|
36
|
+
extra: Record<string, string>;
|
|
37
|
+
};
|
|
38
|
+
/**
|
|
39
|
+
* The `-backend-config` a root's copy is initialised with, from the backend
|
|
40
|
+
* its code declares with the job's flags over it. Throws
|
|
41
|
+
* {@link CopyRefusedError} for a root no copy can be made of: a `cloud`
|
|
42
|
+
* block, a backend with no key attribute, a block that names no key.
|
|
43
|
+
*/
|
|
44
|
+
export declare function copyBackend(dir: string, root: string, suffix: string, env?: NodeJS.ProcessEnv, binary?: string): CopyBackend;
|
|
45
|
+
/**
|
|
46
|
+
* The backend a prepared root was initialised with, checked to be its copy:
|
|
47
|
+
* its key attribute, and a GitLab state's lock addresses, carry the suffix.
|
|
48
|
+
* Throws {@link CopyRefusedError} when it does not; `hint` says how to make
|
|
49
|
+
* the product's config read the suffix.
|
|
50
|
+
*/
|
|
51
|
+
export declare function preparedCopyBackend(dir: string, root: string, suffix: string, env?: NodeJS.ProcessEnv, hint?: string): CopyBackend;
|
|
52
|
+
/** One root of a copy: where the binary runs it, and how. */
|
|
53
|
+
export interface CopyRoot {
|
|
54
|
+
/** The root's name, as the records and the report name it. */
|
|
55
|
+
root: string;
|
|
56
|
+
/** The root's directory in the checkout planned. */
|
|
57
|
+
dir: string;
|
|
58
|
+
binary: string;
|
|
59
|
+
env?: NodeJS.ProcessEnv;
|
|
60
|
+
/**
|
|
61
|
+
* Prepare a root another tool initialises (a Terragrunt unit), with the
|
|
62
|
+
* suffix set as its config reads it: where the binary then runs, and with
|
|
63
|
+
* which env. Its recorded backend must carry the suffix.
|
|
64
|
+
*/
|
|
65
|
+
prepare?: (suffix: string) => Promise<{
|
|
66
|
+
dir: string;
|
|
67
|
+
env: NodeJS.ProcessEnv;
|
|
68
|
+
}>;
|
|
69
|
+
}
|
|
70
|
+
export interface CopyPlanInput {
|
|
71
|
+
copy: CopyRoot;
|
|
72
|
+
/** The environment's suffix: `pr-<n>`. */
|
|
73
|
+
suffix: string;
|
|
74
|
+
/** Where the plan file goes. */
|
|
75
|
+
planFile: string;
|
|
76
|
+
destroy?: boolean;
|
|
77
|
+
lockTimeout?: string | false;
|
|
78
|
+
exec?: BinaryExec;
|
|
79
|
+
/** How the product's config reads the suffix, for a prepared root's refusal. */
|
|
80
|
+
hint?: string;
|
|
81
|
+
}
|
|
82
|
+
/**
|
|
83
|
+
* Plan one root's copy: init it under its suffixed key (or prepare it, then
|
|
84
|
+
* check its key), plan (a destroy with `destroy`), and read the plan's
|
|
85
|
+
* counts. A run of the binary that fails is the plan's `error`; a root no
|
|
86
|
+
* copy can be made of throws {@link CopyRefusedError}.
|
|
87
|
+
*/
|
|
88
|
+
export declare function terraformCopyPlan(input: CopyPlanInput): Promise<PrEnvComponentPlan>;
|
|
89
|
+
export interface TerraformPrEnvInput {
|
|
90
|
+
/** The roots of the copy, in apply order, from the pull request's head. */
|
|
91
|
+
roots(target: {
|
|
92
|
+
pr: number;
|
|
93
|
+
env: string;
|
|
94
|
+
head: string;
|
|
95
|
+
}): Promise<CopyRoot[]>;
|
|
96
|
+
/** A directory the plan files go in. */
|
|
97
|
+
work: string;
|
|
98
|
+
exec?: BinaryExec;
|
|
99
|
+
lockTimeout?: string | false;
|
|
100
|
+
hint?: string;
|
|
101
|
+
/** What a copy with no roots says. */
|
|
102
|
+
nothing?: string;
|
|
103
|
+
log?: (line: string) => void;
|
|
104
|
+
}
|
|
105
|
+
/**
|
|
106
|
+
* The adapter `prEnvUp` takes for a copy of Terraform roots: each root's copy
|
|
107
|
+
* planned under `-<env>` (`pr-<n>`) and applied in order. `refused` holds a
|
|
108
|
+
* {@link CopyRefusedError}, which a product raises as a config error.
|
|
109
|
+
*/
|
|
110
|
+
export declare function terraformPrEnvAdapter(input: TerraformPrEnvInput): PrEnvAdapter & {
|
|
111
|
+
refused?: Error;
|
|
112
|
+
};
|
|
113
|
+
export interface TerraformPrEnvTeardownInput {
|
|
114
|
+
/** The name outcomes are attributed to: the product's. */
|
|
115
|
+
name: string;
|
|
116
|
+
/**
|
|
117
|
+
* The roots as the commit the copy last applied from has them, with what
|
|
118
|
+
* it holds released by `done`. A root missing from `roots` fails alone.
|
|
119
|
+
*/
|
|
120
|
+
checkout(live: LivePrEnvironment): Promise<{
|
|
121
|
+
roots: CopyRoot[];
|
|
122
|
+
done?: () => void;
|
|
123
|
+
}>;
|
|
124
|
+
work: string;
|
|
125
|
+
exec?: BinaryExec;
|
|
126
|
+
lockTimeout?: string | false;
|
|
127
|
+
hint?: string;
|
|
128
|
+
log?: (line: string) => void;
|
|
129
|
+
}
|
|
130
|
+
/**
|
|
131
|
+
* The teardown `prEnvDown` and `prEnvSweep` take for a copy of Terraform
|
|
132
|
+
* roots: a root whose recorded state location carries `-<env>` is destroyed,
|
|
133
|
+
* through a planned destroy of its copy, in reverse apply order; any other
|
|
134
|
+
* is a hole, so nothing at a root's own key is destroyed. It stands in for
|
|
135
|
+
* the terraform lexicon's own teardown.
|
|
136
|
+
*/
|
|
137
|
+
export declare function terraformPrEnvTeardown(input: TerraformPrEnvTeard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;;;;;GAMG;AACH,wBAAgB,sBAAsB,CAAC,KAAK,EAAE,2BAA2B,GAAG,oBAAoB,CAoB/F"}
|
package/dist/pr-env.js
ADDED
|
@@ -0,0 +1,248 @@
|
|
|
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 } from "@intentius/chant/components/pr-env-components";
|
|
38
|
+
import { declaredBackend, initialisedBackend } from "./state/backend.js";
|
|
39
|
+
import { isGitLabAddress, shownUrl, suffixedGitLabAddress } from "./state/gitlab.js";
|
|
40
|
+
import { applyPlanFile, planCopy } from "./state-ops.js";
|
|
41
|
+
/** The backends a copy can be given a key of its own on, and the attribute that holds the key. */
|
|
42
|
+
export const KEY_ATTRIBUTE = { s3: "key", azurerm: "key", gcs: "prefix", local: "path", http: "address" };
|
|
43
|
+
/** The http backend's attributes that name the state too: its lock's, `<address>/lock` on GitLab. */
|
|
44
|
+
export const HTTP_LOCK_ATTRIBUTES = ["lock_address", "unlock_address"];
|
|
45
|
+
const BACKENDS = Object.keys(KEY_ATTRIBUTE).join(", ");
|
|
46
|
+
/** A root no copy can be made of, or a prepared root whose key is its own. A config error: nothing planned. */
|
|
47
|
+
export class CopyRefusedError extends Error {
|
|
48
|
+
constructor(message) {
|
|
49
|
+
super(message);
|
|
50
|
+
this.name = "CopyRefusedError";
|
|
51
|
+
}
|
|
52
|
+
}
|
|
53
|
+
/**
|
|
54
|
+
* A state key with the suffix: before a closing `.tfstate`, else at the end.
|
|
55
|
+
* `envs/dev/terraform.tfstate` becomes `envs/dev/terraform-pr-7.tfstate`.
|
|
56
|
+
*/
|
|
57
|
+
export function suffixedKey(key, suffix) {
|
|
58
|
+
const k = key.replace(/\/+$/, "");
|
|
59
|
+
return /\.tfstate$/.test(k) ? k.replace(/\.tfstate$/, `-${suffix}.tfstate`) : `${k}-${suffix}`;
|
|
60
|
+
}
|
|
61
|
+
/** Whether a key or location carries a copy's suffix as {@link suffixedKey} puts it. A GitLab lock address is read without its `/lock`. */
|
|
62
|
+
export const carriesSuffix = (key, suffix) => new RegExp(`-${suffix.replace(/[.*+?^${}()|[\]\\]/g, "\\$&")}(\\.tfstate)?/*$`).test(key.replace(/\/lock\/?$/, ""));
|
|
63
|
+
/** Where a copy's state is, for messages and the record. */
|
|
64
|
+
function locationOf(type, config, key) {
|
|
65
|
+
const s = (k) => (typeof config[k] === "string" && config[k] !== "" ? config[k] : undefined);
|
|
66
|
+
if (type === "http")
|
|
67
|
+
return shownUrl(key);
|
|
68
|
+
if (type === "s3" && s("bucket"))
|
|
69
|
+
return `s3://${s("bucket")}/${key}`;
|
|
70
|
+
if (type === "gcs" && s("bucket"))
|
|
71
|
+
return `gs://${s("bucket")}/${key}`;
|
|
72
|
+
if (type === "azurerm" && s("container_name"))
|
|
73
|
+
return `az://${s("storage_account_name") ?? "azure"}/${s("container_name")}/${key}`;
|
|
74
|
+
return key;
|
|
75
|
+
}
|
|
76
|
+
/**
|
|
77
|
+
* A GitLab-managed state's attributes for its copy: the address and each
|
|
78
|
+
* lock address with the state name suffixed. Each is the configured one,
|
|
79
|
+
* else its `TF_HTTP_*` variable, as the binary reads it.
|
|
80
|
+
*/
|
|
81
|
+
export function gitlabCopy(root, config, env, suffix) {
|
|
82
|
+
const given = (name) => (typeof config[name] === "string" && config[name] !== "" ? config[name] : env[`TF_HTTP_${name.toUpperCase()}`] || undefined);
|
|
83
|
+
const address = given("address");
|
|
84
|
+
if (!address)
|
|
85
|
+
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`);
|
|
86
|
+
const key = suffixedGitLabAddress(address, suffix);
|
|
87
|
+
if (!key)
|
|
88
|
+
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`);
|
|
89
|
+
const extra = {};
|
|
90
|
+
for (const name of HTTP_LOCK_ATTRIBUTES) {
|
|
91
|
+
const v = given(name);
|
|
92
|
+
if (!v)
|
|
93
|
+
continue;
|
|
94
|
+
const suffixed = suffixedGitLabAddress(v, suffix);
|
|
95
|
+
if (!suffixed || suffixedGitLabAddress(v.replace(/\/lock\/?$/, ""), suffix) !== key)
|
|
96
|
+
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`);
|
|
97
|
+
extra[name] = suffixed;
|
|
98
|
+
}
|
|
99
|
+
return { key, extra };
|
|
100
|
+
}
|
|
101
|
+
/**
|
|
102
|
+
* The `-backend-config` a root's copy is initialised with, from the backend
|
|
103
|
+
* its code declares with the job's flags over it. Throws
|
|
104
|
+
* {@link CopyRefusedError} for a root no copy can be made of: a `cloud`
|
|
105
|
+
* block, a backend with no key attribute, a block that names no key.
|
|
106
|
+
*/
|
|
107
|
+
export function copyBackend(dir, root, suffix, env = {}, binary) {
|
|
108
|
+
const b = declaredBackend(dir, { env, ...(binary ? { binary } : {}) });
|
|
109
|
+
if (b?.type === "cloud")
|
|
110
|
+
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}`);
|
|
111
|
+
const type = b?.type ?? "local";
|
|
112
|
+
const attribute = KEY_ATTRIBUTE[type];
|
|
113
|
+
if (!attribute)
|
|
114
|
+
throw new CopyRefusedError(`${root}'s backend is ${type}, and a copy is given a state key of its own on ${BACKENDS} backends only`);
|
|
115
|
+
const config = b?.config ?? {};
|
|
116
|
+
if (type === "http") {
|
|
117
|
+
const copy = gitlabCopy(root, config, env, suffix);
|
|
118
|
+
return { type, attribute, key: copy.key, location: shownUrl(copy.key), ...(Object.keys(copy.extra).length ? { extra: copy.extra } : {}) };
|
|
119
|
+
}
|
|
120
|
+
const raw = config[attribute];
|
|
121
|
+
const given = typeof raw === "string" && raw !== "" ? raw : type === "local" ? "terraform.tfstate" : undefined;
|
|
122
|
+
if (!given)
|
|
123
|
+
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`);
|
|
124
|
+
const key = suffixedKey(given, suffix);
|
|
125
|
+
return { type, attribute, key, location: locationOf(type, config, key) };
|
|
126
|
+
}
|
|
127
|
+
/**
|
|
128
|
+
* The backend a prepared root was initialised with, checked to be its copy:
|
|
129
|
+
* its key attribute, and a GitLab state's lock addresses, carry the suffix.
|
|
130
|
+
* Throws {@link CopyRefusedError} when it does not; `hint` says how to make
|
|
131
|
+
* the product's config read the suffix.
|
|
132
|
+
*/
|
|
133
|
+
export function preparedCopyBackend(dir, root, suffix, env = {}, hint) {
|
|
134
|
+
const how = hint ? `; ${hint}` : "";
|
|
135
|
+
const b = initialisedBackend(dir, env);
|
|
136
|
+
if (!b)
|
|
137
|
+
throw new CopyRefusedError(`${root}: it was prepared with no backend the binary recorded, so its copy has no key of its own${how}`);
|
|
138
|
+
const attribute = KEY_ATTRIBUTE[b.type];
|
|
139
|
+
if (!attribute)
|
|
140
|
+
throw new CopyRefusedError(`${root}'s backend is ${b.type}, and a copy is given a state key of its own on ${BACKENDS} backends only`);
|
|
141
|
+
const raw = b.config[attribute];
|
|
142
|
+
const key = typeof raw === "string" ? raw : "";
|
|
143
|
+
if (b.type === "http" && !isGitLabAddress(key))
|
|
144
|
+
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`);
|
|
145
|
+
if (!carriesSuffix(key, suffix))
|
|
146
|
+
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}`);
|
|
147
|
+
if (b.type === "http") {
|
|
148
|
+
const own = HTTP_LOCK_ATTRIBUTES.map((a) => b.config[a]).find((l) => typeof l === "string" && !carriesSuffix(l, suffix));
|
|
149
|
+
if (own)
|
|
150
|
+
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`);
|
|
151
|
+
}
|
|
152
|
+
return { type: b.type, attribute, key, location: locationOf(b.type, b.config, key) };
|
|
153
|
+
}
|
|
154
|
+
/**
|
|
155
|
+
* Plan one root's copy: init it under its suffixed key (or prepare it, then
|
|
156
|
+
* check its key), plan (a destroy with `destroy`), and read the plan's
|
|
157
|
+
* counts. A run of the binary that fails is the plan's `error`; a root no
|
|
158
|
+
* copy can be made of throws {@link CopyRefusedError}.
|
|
159
|
+
*/
|
|
160
|
+
export async function terraformCopyPlan(input) {
|
|
161
|
+
const { copy, suffix } = input;
|
|
162
|
+
const exec = input.exec;
|
|
163
|
+
const none = { component: copy.root, planDigest: "", create: 0, update: 0, replace: 0, delete: 0 };
|
|
164
|
+
let dir = copy.dir;
|
|
165
|
+
let env = copy.env ?? process.env;
|
|
166
|
+
let backend;
|
|
167
|
+
let backendConfig = {};
|
|
168
|
+
if (copy.prepare) {
|
|
169
|
+
let prepared;
|
|
170
|
+
try {
|
|
171
|
+
prepared = await copy.prepare(suffix);
|
|
172
|
+
}
|
|
173
|
+
catch (e) {
|
|
174
|
+
return { ...none, error: e.message, apply: async () => { } };
|
|
175
|
+
}
|
|
176
|
+
({ dir, env } = prepared);
|
|
177
|
+
backend = preparedCopyBackend(dir, copy.root, suffix, env, input.hint);
|
|
178
|
+
}
|
|
179
|
+
else {
|
|
180
|
+
backend = copyBackend(dir, copy.root, suffix, env, copy.binary);
|
|
181
|
+
backendConfig = { [backend.attribute]: backend.key, ...backend.extra };
|
|
182
|
+
}
|
|
183
|
+
const run = { root: copy.root, binary: copy.binary, dir, env, ...(exec ? { exec } : {}) };
|
|
184
|
+
try {
|
|
185
|
+
const read = await planCopy({ ...run, backendConfig, planFile: input.planFile, ...(copy.prepare ? { init: false } : {}), ...(input.destroy ? { destroy: true } : {}), ...(input.lockTimeout !== undefined ? { lockTimeout: input.lockTimeout } : {}) });
|
|
186
|
+
const count = (action) => read.part.entries.filter((e) => e.action === action).length;
|
|
187
|
+
return {
|
|
188
|
+
component: copy.root,
|
|
189
|
+
planDigest: read.planDigest,
|
|
190
|
+
create: count("create"),
|
|
191
|
+
update: count("update"),
|
|
192
|
+
replace: count("replace"),
|
|
193
|
+
delete: count("delete"),
|
|
194
|
+
location: backend.location,
|
|
195
|
+
apply: () => applyPlanFile({ ...run, planFile: input.planFile, ...(input.lockTimeout !== undefined ? { lockTimeout: input.lockTimeout } : {}) }),
|
|
196
|
+
};
|
|
197
|
+
}
|
|
198
|
+
catch (e) {
|
|
199
|
+
return { ...none, location: backend.location, error: e.message, apply: async () => { } };
|
|
200
|
+
}
|
|
201
|
+
}
|
|
202
|
+
/**
|
|
203
|
+
* The adapter `prEnvUp` takes for a copy of Terraform roots: each root's copy
|
|
204
|
+
* planned under `-<env>` (`pr-<n>`) and applied in order. `refused` holds a
|
|
205
|
+
* {@link CopyRefusedError}, which a product raises as a config error.
|
|
206
|
+
*/
|
|
207
|
+
export function terraformPrEnvAdapter(input) {
|
|
208
|
+
let copies = new Map();
|
|
209
|
+
return componentsPrEnvAdapter({
|
|
210
|
+
async components(target) {
|
|
211
|
+
const roots = await input.roots(target);
|
|
212
|
+
copies = new Map(roots.map((r) => [r.root, r]));
|
|
213
|
+
return roots.map((r) => r.root);
|
|
214
|
+
},
|
|
215
|
+
plan: (component, i, target) => 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 } : {}) }),
|
|
216
|
+
...(input.nothing ? { nothing: () => input.nothing } : {}),
|
|
217
|
+
...(input.log ? { log: input.log } : {}),
|
|
218
|
+
});
|
|
219
|
+
}
|
|
220
|
+
/**
|
|
221
|
+
* The teardown `prEnvDown` and `prEnvSweep` take for a copy of Terraform
|
|
222
|
+
* roots: a root whose recorded state location carries `-<env>` is destroyed,
|
|
223
|
+
* through a planned destroy of its copy, in reverse apply order; any other
|
|
224
|
+
* is a hole, so nothing at a root's own key is destroyed. It stands in for
|
|
225
|
+
* the terraform lexicon's own teardown.
|
|
226
|
+
*/
|
|
227
|
+
export function terraformPrEnvTeardown(input) {
|
|
228
|
+
return componentsPrEnvTeardown({
|
|
229
|
+
name: input.name,
|
|
230
|
+
lexicons: ["terraform"],
|
|
231
|
+
type: "terraform_root",
|
|
232
|
+
owns: (location, env) => carriesSuffix(location, env),
|
|
233
|
+
async checkout(live) {
|
|
234
|
+
const code = await input.checkout(live);
|
|
235
|
+
const roots = new Map(code.roots.map((r) => [r.root, r]));
|
|
236
|
+
return {
|
|
237
|
+
async plan(component, i) {
|
|
238
|
+
const copy = roots.get(component);
|
|
239
|
+
if (!copy)
|
|
240
|
+
throw new Error(`${component} is not at ${live.commit.slice(0, 8)}, so its copy cannot be planned`);
|
|
241
|
+
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 } : {}) });
|
|
242
|
+
},
|
|
243
|
+
...(code.done ? { done: code.done } : {}),
|
|
244
|
+
};
|
|
245
|
+
},
|
|
246
|
+
...(input.log ? { log: input.log } : {}),
|
|
247
|
+
});
|
|
248
|
+
}
|
|
@@ -0,0 +1,74 @@
|
|
|
1
|
+
import type { ForgeFetch } from "@intentius/chant/pr-forge";
|
|
2
|
+
import type { StateStore } from "./store.js";
|
|
3
|
+
/**
|
|
4
|
+
* The HTTP calls the store makes. A StoreFetch, chant's forge fetch and the
|
|
5
|
+
* global fetch all fit.
|
|
6
|
+
*/
|
|
7
|
+
export type GitLabFetch = ForgeFetch;
|
|
8
|
+
/** Where a GitLab-managed state is, and how to call its API. */
|
|
9
|
+
export interface GitLabState {
|
|
10
|
+
backend: "http";
|
|
11
|
+
/** The state's address as the backend has it. */
|
|
12
|
+
address: string;
|
|
13
|
+
/** The state's name, the last part of its address. */
|
|
14
|
+
name: string;
|
|
15
|
+
lockAddress?: string;
|
|
16
|
+
unlockAddress?: string;
|
|
17
|
+
lockMethod: string;
|
|
18
|
+
unlockMethod: string;
|
|
19
|
+
/** The Authorization header the backend's credentials make, when it has any. */
|
|
20
|
+
auth?: string;
|
|
21
|
+
}
|
|
22
|
+
/** Whether an http backend's address is a GitLab project's state API. */
|
|
23
|
+
export declare function isGitLabAddress(address: string | undefined): boolean;
|
|
24
|
+
/** A URL with its credentials and query left out, to print and record. */
|
|
25
|
+
export declare function shownUrl(raw: string): string;
|
|
26
|
+
/** The GitLab-managed state an http backend's configuration names, or undefined when its address is not GitLab's state API. */
|
|
27
|
+
export declare function gitlabState(config: Record<string, unknown>, env?: NodeJS.ProcessEnv): GitLabState | undefined;
|
|
28
|
+
/**
|
|
29
|
+
* A GitLab state address with its state name suffixed `-<suffix>`, and a
|
|
30
|
+
* lock address with the state name before `/lock` suffixed the same way.
|
|
31
|
+
* Undefined when the address is not GitLab's state API (or its lock).
|
|
32
|
+
*/
|
|
33
|
+
export declare function suffixedGitLabAddress(address: string, suffix: string): string | undefined;
|
|
34
|
+
/** The URL of one version of the state. */
|
|
35
|
+
export declare const gitlabVersionUrl: (s: GitLabState, serial: string | number) => string;
|
|
36
|
+
/** The address of the state beside this one whose name is this one's with `suffix` after it. */
|
|
37
|
+
export declare function gitlabBesideAddress(s: GitLabState, suffix: string): string;
|
|
38
|
+
/** A GitLab-managed state opened as a {@link StateStore}, with the reads GitLab's API has beyond one: its serial, its versions, and a lock released by its ID. */
|
|
39
|
+
export interface GitLabStateStore extends StateStore {
|
|
40
|
+
readonly backend: "http";
|
|
41
|
+
/**
|
|
42
|
+
* The serial and lineage of the state GitLab holds now, or undefined when
|
|
43
|
+
* it holds none. GitLab has no call that answers the serial alone, so this
|
|
44
|
+
* reads the state and keeps those two fields only.
|
|
45
|
+
*/
|
|
46
|
+
serial(): Promise<{
|
|
47
|
+
serial: number;
|
|
48
|
+
lineage?: string;
|
|
49
|
+
} | undefined>;
|
|
50
|
+
/** Whether GitLab keeps the version with this serial: a HEAD, so no state is read. */
|
|
51
|
+
hasVersion(serial: string | number): Promise<boolean>;
|
|
52
|
+
/** One version of the state, or undefined when GitLab keeps no version with that serial. */
|
|
53
|
+
readVersion(serial: string | number): Promise<string | undefined>;
|
|
54
|
+
/**
|
|
55
|
+
* Who holds the lock, as lock info text with its ID, its GitLab user and
|
|
56
|
+
* when it was taken, or undefined when nobody does. GitLab has no call that
|
|
57
|
+
* reads a lock, so this asks for it: a held lock answers 409 with its
|
|
58
|
+
* holder, and a free one is taken by the ask and released at once by its
|
|
59
|
+
* own ID.
|
|
60
|
+
*/
|
|
61
|
+
probeLock(): Promise<string | undefined>;
|
|
62
|
+
/**
|
|
63
|
+
* Release the lock with this ID. GitLab releases it only while that ID
|
|
64
|
+
* holds it: another lock taken since answers 409, which is thrown.
|
|
65
|
+
*/
|
|
66
|
+
releaseLock(id: string): Promise<void>;
|
|
67
|
+
}
|
|
68
|
+
/**
|
|
69
|
+
* The GitLab-managed state as a store. `who` names the probe's lock info
|
|
70
|
+
* (its `Who`, and the start of its ID), so a holder that reads the lock
|
|
71
|
+
* while the probe has it sees whose it is.
|
|
72
|
+
*/
|
|
73
|
+
export declare function gitlabStore(s: GitLabState, fetchFn?: GitLabFetch, who?: string): GitLabStateStore;
|
|
74
|
+
//# sourceMappingURL=gitlab.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"gitlab.d.ts","sourceRoot":"","sources":["../../src/state/gitlab.ts"],"names":[],"mappings":"AAgCA,OAAO,KAAK,EAAE,UAAU,EAAE,MAAM,2BAA2B,CAAC;AAE5D,OAAO,KAAK,EAAc,UAAU,E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