@intentius/chant-lexicon-terraform 0.124.75 → 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
|
@@ -0,0 +1,275 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* GitLab-managed Terraform state (#4773): an http backend whose address is a
|
|
3
|
+
* GitLab project's state API, `<api>/projects/<id>/terraform/state/<name>`,
|
|
4
|
+
* with `<address>/lock` as its lock and unlock address.
|
|
5
|
+
*
|
|
6
|
+
* GitLab keeps each version of a state by its serial. `GET <address>` is the
|
|
7
|
+
* current state, `GET <address>/versions/<serial>` one version, and `POST`
|
|
8
|
+
* and `DELETE` on `<address>/lock` (the backend's `LOCK` and `UNLOCK` by
|
|
9
|
+
* default) take and release the lock. A lock that is held answers a take
|
|
10
|
+
* with 409 and the holder's lock info. While a lock is held GitLab takes a
|
|
11
|
+
* write of the state only with the lock's ID (`POST <address>?ID=<id>`), so
|
|
12
|
+
* the binary cannot write with `-lock=false` under chant's lock and the store
|
|
13
|
+
* writes the state itself, as the azurerm store does under its lease.
|
|
14
|
+
*
|
|
15
|
+
* The backend's configuration comes from the block, a `-backend-config` file
|
|
16
|
+
* or flag (which `init` records), or the job's `TF_HTTP_*` variables (which
|
|
17
|
+
* it does not record): each attribute is the recorded value, else its
|
|
18
|
+
* variable (`TF_HTTP_ADDRESS`, `TF_HTTP_LOCK_ADDRESS`,
|
|
19
|
+
* `TF_HTTP_UNLOCK_ADDRESS`, `TF_HTTP_LOCK_METHOD`, `TF_HTTP_UNLOCK_METHOD`),
|
|
20
|
+
* as the binary reads it. Every call is made with the backend's own
|
|
21
|
+
* credentials, `username` and `password` (`TF_HTTP_USERNAME`,
|
|
22
|
+
* `TF_HTTP_PASSWORD`): the job token in a pipeline, a personal access token
|
|
23
|
+
* at a shell.
|
|
24
|
+
*
|
|
25
|
+
* What a run keeps beside the state goes in another state of the same
|
|
26
|
+
* project, named the state's name with the suffix after it, so it is under
|
|
27
|
+
* the project's own access controls.
|
|
28
|
+
*
|
|
29
|
+
* Lifted from terragucci's `gitlab-state.ts` (INTENTIUS/terragucci 70b7509),
|
|
30
|
+
* so terragucci deletes its copy.
|
|
31
|
+
*/
|
|
32
|
+
import { randomUUID } from "node:crypto";
|
|
33
|
+
import { defaultFetch, StoreError } from "./object-store.js";
|
|
34
|
+
/** The path of GitLab's state API: `.../api/v4/projects/<id>/terraform/state/<name>`. */
|
|
35
|
+
const STATE_API_PATH = /\/api\/v4\/projects\/[^/]+\/terraform\/state\/([^/]+)\/?$/;
|
|
36
|
+
const str = (v) => (typeof v === "string" && v !== "" ? v : undefined);
|
|
37
|
+
/** Whether an http backend's address is a GitLab project's state API. */
|
|
38
|
+
export function isGitLabAddress(address) {
|
|
39
|
+
if (!address)
|
|
40
|
+
return false;
|
|
41
|
+
try {
|
|
42
|
+
return STATE_API_PATH.test(new URL(address).pathname);
|
|
43
|
+
}
|
|
44
|
+
catch {
|
|
45
|
+
return false;
|
|
46
|
+
}
|
|
47
|
+
}
|
|
48
|
+
/** A URL with its credentials and query left out, to print and record. */
|
|
49
|
+
export function shownUrl(raw) {
|
|
50
|
+
try {
|
|
51
|
+
const u = new URL(raw);
|
|
52
|
+
return `${u.protocol}//${u.host}${u.pathname}`;
|
|
53
|
+
}
|
|
54
|
+
catch {
|
|
55
|
+
return raw;
|
|
56
|
+
}
|
|
57
|
+
}
|
|
58
|
+
/** The http backend's attribute, as the binary reads it: recorded, else its `TF_HTTP_*` variable. */
|
|
59
|
+
const attribute = (config, env, name) => str(config[name]) ?? str(env[`TF_HTTP_${name.toUpperCase()}`]);
|
|
60
|
+
/** The GitLab-managed state an http backend's configuration names, or undefined when its address is not GitLab's state API. */
|
|
61
|
+
export function gitlabState(config, env = process.env) {
|
|
62
|
+
const address = attribute(config, env, "address");
|
|
63
|
+
if (!address || !isGitLabAddress(address))
|
|
64
|
+
return undefined;
|
|
65
|
+
const name = decodeURIComponent(new URL(address).pathname.match(STATE_API_PATH)[1]);
|
|
66
|
+
const username = attribute(config, env, "username");
|
|
67
|
+
const password = attribute(config, env, "password");
|
|
68
|
+
const lockAddress = attribute(config, env, "lock_address");
|
|
69
|
+
const unlockAddress = attribute(config, env, "unlock_address");
|
|
70
|
+
return {
|
|
71
|
+
backend: "http",
|
|
72
|
+
address,
|
|
73
|
+
name,
|
|
74
|
+
...(lockAddress ? { lockAddress } : {}),
|
|
75
|
+
...(unlockAddress ? { unlockAddress } : {}),
|
|
76
|
+
lockMethod: attribute(config, env, "lock_method") ?? "LOCK",
|
|
77
|
+
unlockMethod: attribute(config, env, "unlock_method") ?? "UNLOCK",
|
|
78
|
+
...(username || password ? { auth: `Basic ${Buffer.from(`${username ?? ""}:${password ?? ""}`).toString("base64")}` } : {}),
|
|
79
|
+
};
|
|
80
|
+
}
|
|
81
|
+
/**
|
|
82
|
+
* A GitLab state address with its state name suffixed `-<suffix>`, and a
|
|
83
|
+
* lock address with the state name before `/lock` suffixed the same way.
|
|
84
|
+
* Undefined when the address is not GitLab's state API (or its lock).
|
|
85
|
+
*/
|
|
86
|
+
export function suffixedGitLabAddress(address, suffix) {
|
|
87
|
+
let u;
|
|
88
|
+
try {
|
|
89
|
+
u = new URL(address);
|
|
90
|
+
}
|
|
91
|
+
catch {
|
|
92
|
+
return undefined;
|
|
93
|
+
}
|
|
94
|
+
const m = u.pathname.match(/^(.*\/api\/v4\/projects\/[^/]+\/terraform\/state\/)([^/]+?)(\/lock)?\/?$/);
|
|
95
|
+
if (!m)
|
|
96
|
+
return undefined;
|
|
97
|
+
u.pathname = `${m[1]}${m[2]}-${suffix}${m[3] ?? ""}`;
|
|
98
|
+
return u.toString();
|
|
99
|
+
}
|
|
100
|
+
const headers = (s, json = false) => ({ ...(s.auth ? { authorization: s.auth } : {}), ...(json ? { "content-type": "application/json" } : {}) });
|
|
101
|
+
const base = (address) => address.replace(/\/+$/, "");
|
|
102
|
+
/** An error that names the call and GitLab's answer, never the credentials. */
|
|
103
|
+
function answered(what, url, status, text) {
|
|
104
|
+
const why = status === 401 ? ": the backend's username and password (TF_HTTP_USERNAME, TF_HTTP_PASSWORD) are not a token GitLab accepts" : status === 403 ? ": the token may not read this project's state (reads need Developer)" : "";
|
|
105
|
+
return new StoreError(`GitLab answered ${status} to ${what} ${shownUrl(url)}${why}${text && !why ? `: ${text.trim().slice(0, 200)}` : ""}`);
|
|
106
|
+
}
|
|
107
|
+
const held = (status) => status === 409 || status === 423;
|
|
108
|
+
/** The URL of one version of the state. */
|
|
109
|
+
export const gitlabVersionUrl = (s, serial) => `${base(s.address)}/versions/${encodeURIComponent(String(serial))}`;
|
|
110
|
+
/** The address of the state beside this one whose name is this one's with `suffix` after it. */
|
|
111
|
+
export function gitlabBesideAddress(s, suffix) {
|
|
112
|
+
const u = new URL(s.address);
|
|
113
|
+
u.pathname = `${u.pathname.replace(/\/+$/, "")}${encodeURIComponent(suffix)}`;
|
|
114
|
+
return u.toString();
|
|
115
|
+
}
|
|
116
|
+
/** The holder in GitLab's answer to a take of a lock that is held: its ID, its GitLab user and when it was taken. GitLab answers the other fields with the ask's own, so they are left out. */
|
|
117
|
+
function heldBy(text) {
|
|
118
|
+
try {
|
|
119
|
+
const v = JSON.parse(text);
|
|
120
|
+
return JSON.stringify(Object.fromEntries(["ID", "Who", "Created"].filter((k) => typeof v[k] === "string").map((k) => [k, v[k]])));
|
|
121
|
+
}
|
|
122
|
+
catch {
|
|
123
|
+
return text;
|
|
124
|
+
}
|
|
125
|
+
}
|
|
126
|
+
const idOf = (text) => {
|
|
127
|
+
try {
|
|
128
|
+
const v = JSON.parse(text);
|
|
129
|
+
return typeof v.ID === "string" ? v.ID : "";
|
|
130
|
+
}
|
|
131
|
+
catch {
|
|
132
|
+
return "";
|
|
133
|
+
}
|
|
134
|
+
};
|
|
135
|
+
/**
|
|
136
|
+
* The GitLab-managed state as a store. `who` names the probe's lock info
|
|
137
|
+
* (its `Who`, and the start of its ID), so a holder that reads the lock
|
|
138
|
+
* while the probe has it sees whose it is.
|
|
139
|
+
*/
|
|
140
|
+
export function gitlabStore(s, fetchFn = defaultFetch, who = "chant") {
|
|
141
|
+
const location = shownUrl(s.address);
|
|
142
|
+
let lockedId;
|
|
143
|
+
const needLock = () => {
|
|
144
|
+
if (!s.lockAddress || !s.unlockAddress)
|
|
145
|
+
throw new StoreError(`the http backend of ${location} names no ${s.lockAddress ? "unlock_address" : "lock_address"}`);
|
|
146
|
+
return s.lockAddress;
|
|
147
|
+
};
|
|
148
|
+
/** Take the lock with `info`: true when taken, the held answer's text when another holds it. */
|
|
149
|
+
const take = async (info) => {
|
|
150
|
+
const at = needLock();
|
|
151
|
+
const r = await fetchFn(at, { method: s.lockMethod, headers: headers(s, true), body: info });
|
|
152
|
+
const text = await r.text();
|
|
153
|
+
if (held(r.status))
|
|
154
|
+
return { heldText: text };
|
|
155
|
+
if (!r.ok)
|
|
156
|
+
throw answered("a lock of", at, r.status, text);
|
|
157
|
+
return true;
|
|
158
|
+
};
|
|
159
|
+
const releaseLock = async (id) => {
|
|
160
|
+
if (!s.unlockAddress)
|
|
161
|
+
throw new StoreError(`the http backend of ${location} names no unlock_address`);
|
|
162
|
+
const r = await fetchFn(s.unlockAddress, { method: s.unlockMethod, headers: headers(s, true), body: JSON.stringify({ ID: id }) });
|
|
163
|
+
const text = await r.text();
|
|
164
|
+
if (held(r.status))
|
|
165
|
+
throw new StoreError(`GitLab did not release lock ${id} on ${shownUrl(s.unlockAddress)}: another lock holds the state now`);
|
|
166
|
+
if (!r.ok)
|
|
167
|
+
throw answered("a release of", s.unlockAddress, r.status, text);
|
|
168
|
+
};
|
|
169
|
+
const probeLock = async () => {
|
|
170
|
+
const id = `${who}-probe-${randomUUID()}`;
|
|
171
|
+
const info = JSON.stringify({ ID: id, Operation: "OperationTypeInvalid", Info: `${who} reads who holds the lock`, Who: who, Version: "", Created: new Date().toISOString(), Path: "" });
|
|
172
|
+
const got = await take(info);
|
|
173
|
+
if (got !== true)
|
|
174
|
+
return heldBy(got.heldText);
|
|
175
|
+
await releaseLock(id);
|
|
176
|
+
return undefined;
|
|
177
|
+
};
|
|
178
|
+
/** The text at `url`, or undefined when GitLab holds no state there. */
|
|
179
|
+
const read = async (url) => {
|
|
180
|
+
const r = await fetchFn(url, { method: "GET", headers: headers(s) });
|
|
181
|
+
if (r.status === 404 || r.status === 204)
|
|
182
|
+
return undefined;
|
|
183
|
+
const text = await r.text();
|
|
184
|
+
if (!r.ok)
|
|
185
|
+
throw answered("a read of", url, r.status, text);
|
|
186
|
+
return text;
|
|
187
|
+
};
|
|
188
|
+
const post = async (url, body) => {
|
|
189
|
+
const r = await fetchFn(url, { method: "POST", headers: headers(s, true), body });
|
|
190
|
+
const text = await r.text();
|
|
191
|
+
if (held(r.status))
|
|
192
|
+
throw new StoreError(`GitLab refused a write of ${shownUrl(url)}: its lock is held by another ID`);
|
|
193
|
+
if (!r.ok)
|
|
194
|
+
throw answered("a write of", url, r.status, text);
|
|
195
|
+
};
|
|
196
|
+
return {
|
|
197
|
+
backend: "http",
|
|
198
|
+
location,
|
|
199
|
+
lockLocation: shownUrl(s.lockAddress ?? s.address),
|
|
200
|
+
serial: async () => {
|
|
201
|
+
const text = await read(s.address);
|
|
202
|
+
if (text === undefined)
|
|
203
|
+
return undefined;
|
|
204
|
+
let doc;
|
|
205
|
+
try {
|
|
206
|
+
doc = JSON.parse(text);
|
|
207
|
+
}
|
|
208
|
+
catch {
|
|
209
|
+
throw new StoreError(`GitLab answered ${location} with no state`);
|
|
210
|
+
}
|
|
211
|
+
if (typeof doc.serial !== "number")
|
|
212
|
+
throw new StoreError(`the state at ${location} has no serial`);
|
|
213
|
+
return { serial: doc.serial, ...(typeof doc.lineage === "string" ? { lineage: doc.lineage } : {}) };
|
|
214
|
+
},
|
|
215
|
+
hasVersion: async (serial) => {
|
|
216
|
+
const url = gitlabVersionUrl(s, serial);
|
|
217
|
+
const r = await fetchFn(url, { method: "HEAD", headers: headers(s) });
|
|
218
|
+
if (r.status === 404)
|
|
219
|
+
return false;
|
|
220
|
+
if (!r.ok)
|
|
221
|
+
throw answered("a check of", url, r.status, "");
|
|
222
|
+
return true;
|
|
223
|
+
},
|
|
224
|
+
readVersion: async (serial) => {
|
|
225
|
+
const url = gitlabVersionUrl(s, serial);
|
|
226
|
+
const r = await fetchFn(url, { method: "GET", headers: headers(s) });
|
|
227
|
+
if (r.status === 404)
|
|
228
|
+
return undefined;
|
|
229
|
+
const text = await r.text();
|
|
230
|
+
if (!r.ok)
|
|
231
|
+
throw answered("a read of", url, r.status, text);
|
|
232
|
+
return text;
|
|
233
|
+
},
|
|
234
|
+
probeLock,
|
|
235
|
+
releaseLock,
|
|
236
|
+
heldLock: async () => {
|
|
237
|
+
const info = await probeLock();
|
|
238
|
+
return info === undefined ? undefined : { id: idOf(info), info };
|
|
239
|
+
},
|
|
240
|
+
lock: async (info) => {
|
|
241
|
+
if ((await take(info)) !== true)
|
|
242
|
+
return false;
|
|
243
|
+
lockedId = idOf(info);
|
|
244
|
+
return true;
|
|
245
|
+
},
|
|
246
|
+
unlock: async () => {
|
|
247
|
+
if (lockedId === undefined)
|
|
248
|
+
return;
|
|
249
|
+
await releaseLock(lockedId);
|
|
250
|
+
lockedId = undefined;
|
|
251
|
+
},
|
|
252
|
+
write: async (body) => {
|
|
253
|
+
if (lockedId === undefined)
|
|
254
|
+
throw new StoreError(`${location} is written under its lock, and none is held`);
|
|
255
|
+
const u = new URL(s.address);
|
|
256
|
+
u.searchParams.set("ID", lockedId);
|
|
257
|
+
await post(u.toString(), body);
|
|
258
|
+
},
|
|
259
|
+
readBeside: (suffix) => read(gitlabBesideAddress(s, suffix)),
|
|
260
|
+
writeBeside: (suffix, body) => post(gitlabBesideAddress(s, suffix), body),
|
|
261
|
+
removeBeside: async (suffix) => {
|
|
262
|
+
const url = gitlabBesideAddress(s, suffix);
|
|
263
|
+
const r = await fetchFn(url, { method: "DELETE", headers: headers(s) });
|
|
264
|
+
if (r.status === 404)
|
|
265
|
+
return;
|
|
266
|
+
const text = await r.text();
|
|
267
|
+
if (!r.ok)
|
|
268
|
+
throw answered("a delete of", url, r.status, text);
|
|
269
|
+
},
|
|
270
|
+
besideLocation: (suffix) => shownUrl(gitlabBesideAddress(s, suffix)),
|
|
271
|
+
adopt: (h) => {
|
|
272
|
+
lockedId = h.id;
|
|
273
|
+
},
|
|
274
|
+
};
|
|
275
|
+
}
|
package/dist/state/index.d.ts
CHANGED
|
@@ -6,10 +6,12 @@
|
|
|
6
6
|
export * from "./object-store.js";
|
|
7
7
|
export * from "./backend.js";
|
|
8
8
|
export * from "./store.js";
|
|
9
|
+
export * from "./gitlab.js";
|
|
9
10
|
export * from "./exec.js";
|
|
10
11
|
export * from "./locks.js";
|
|
11
12
|
export * from "./root-states.js";
|
|
12
13
|
export { S3Client, s3FromEnv, type S3Target } from "./s3.js";
|
|
13
14
|
export { GcsClient, type GcsTarget } from "./gcs.js";
|
|
14
15
|
export { AzureBlobClient, type AzureTarget } from "./azure-blob.js";
|
|
16
|
+
export * from "./versions.js";
|
|
15
17
|
//# sourceMappingURL=index.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/state/index.ts"],"names":[],"mappings":"AAAA;;;;GAIG;AAEH,cAAc,gBAAgB,CAAC;AAC/B,cAAc,WAAW,CAAC;AAC1B,cAAc,SAAS,CAAC;AACxB,cAAc,QAAQ,CAAC;AACvB,cAAc,SAAS,CAAC;AACxB,cAAc,eAAe,CAAC;AAC9B,OAAO,EAAE,QAAQ,EAAE,SAAS,EAAE,KAAK,QAAQ,EAAE,MAAM,MAAM,CAAC;AAC1D,OAAO,EAAE,SAAS,EAAE,KAAK,SAAS,EAAE,MAAM,OAAO,CAAC;AAClD,OAAO,EAAE,eAAe,EAAE,KAAK,WAAW,EAAE,MAAM,cAAc,CAAC"}
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/state/index.ts"],"names":[],"mappings":"AAAA;;;;GAIG;AAEH,cAAc,gBAAgB,CAAC;AAC/B,cAAc,WAAW,CAAC;AAC1B,cAAc,SAAS,CAAC;AACxB,cAAc,UAAU,CAAC;AACzB,cAAc,QAAQ,CAAC;AACvB,cAAc,SAAS,CAAC;AACxB,cAAc,eAAe,CAAC;AAC9B,OAAO,EAAE,QAAQ,EAAE,SAAS,EAAE,KAAK,QAAQ,EAAE,MAAM,MAAM,CAAC;AAC1D,OAAO,EAAE,SAAS,EAAE,KAAK,SAAS,EAAE,MAAM,OAAO,CAAC;AAClD,OAAO,EAAE,eAAe,EAAE,KAAK,WAAW,EAAE,MAAM,cAAc,CAAC;AACjE,cAAc,YAAY,CAAC"}
|
package/dist/state/index.js
CHANGED
|
@@ -6,9 +6,11 @@
|
|
|
6
6
|
export * from "./object-store.js";
|
|
7
7
|
export * from "./backend.js";
|
|
8
8
|
export * from "./store.js";
|
|
9
|
+
export * from "./gitlab.js";
|
|
9
10
|
export * from "./exec.js";
|
|
10
11
|
export * from "./locks.js";
|
|
11
12
|
export * from "./root-states.js";
|
|
12
13
|
export { S3Client, s3FromEnv } from "./s3.js";
|
|
13
14
|
export { GcsClient } from "./gcs.js";
|
|
14
15
|
export { AzureBlobClient } from "./azure-blob.js";
|
|
16
|
+
export * from "./versions.js";
|
package/dist/state/store.d.ts
CHANGED
|
@@ -8,7 +8,8 @@ export interface LockHolder {
|
|
|
8
8
|
}
|
|
9
9
|
/** A state where it is, for what locks it and keeps beside it. */
|
|
10
10
|
export interface StateStore {
|
|
11
|
-
|
|
11
|
+
/** The backend's type; `http` is a GitLab-managed state (./gitlab.ts). */
|
|
12
|
+
readonly backend: LockableState["backend"] | "http";
|
|
12
13
|
/** Where the state is. */
|
|
13
14
|
readonly location: string;
|
|
14
15
|
/** Where its lock is: the lock object, or the blob whose lease is the lock. */
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"store.d.ts","sourceRoot":"","sources":["../../src/state/store.ts"],"names":[],"mappings":"AAyBA,OAAO,KAAK,EAAoC,aAAa,EAAW,MAAM,WAAW,CAAC;AAE1F,OAAO,EAAc,KAAK,UAAU,EAAE,MAAM,gBAAgB,CAAC;AAG7D,oHAAoH;AACpH,MAAM,WAAW,UAAU;IACzB,EAAE,EAAE,MAAM,CAAC;IACX,sCAAsC;IACtC,IAAI,EAAE,MAAM,CAAC;CACd;AAED,kEAAkE;AAClE,MAAM,WAAW,UAAU;IACzB,QAAQ,CAAC,OAAO,EAAE,aAAa,CAAC,SAAS,CAAC,CAAC;
|
|
1
|
+
{"version":3,"file":"store.d.ts","sourceRoot":"","sources":["../../src/state/store.ts"],"names":[],"mappings":"AAyBA,OAAO,KAAK,EAAoC,aAAa,EAAW,MAA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AM,GAAG,MAAM,CAO3C;AAiDD,+FAA+F;AAC/F,eAAO,MAAM,aAAa,oBAAoB,CAAC;AAsF/C,6HAA6H;AAC7H,wBAAgB,UAAU,CAAC,CAAC,EAAE,aAAa,EAAE,GAAG,EAAE,MAAM,EAAE,OAAO,CAAC,EAAE,UAAU,GAAG,UAAU,CAK1F"}
|
|
@@ -0,0 +1,39 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* A root's state read by version (#4762), for chant core's state export
|
|
3
|
+
* (`@intentius/chant/state-export`): the version a bucket or container
|
|
4
|
+
* holds now, whether it holds another, and one version's text.
|
|
5
|
+
*
|
|
6
|
+
* s3 bucket versioning: the object's `x-amz-version-id`
|
|
7
|
+
* gcs object versioning: a version is a generation
|
|
8
|
+
* azurerm blob versioning: the blob's `x-ms-version-id`; with none, the
|
|
9
|
+
* snapshots the backend takes when it sets `snapshot = true`, and
|
|
10
|
+
* a request for the current version takes one, whose id names it
|
|
11
|
+
* http a GitLab-managed state (./gitlab.ts): GitLab keeps each version
|
|
12
|
+
* by its serial, so the version id is the serial
|
|
13
|
+
*
|
|
14
|
+
* A local state keeps no versions, and is refused. Nothing here writes a
|
|
15
|
+
* state; the one write is the azurerm snapshot above.
|
|
16
|
+
*
|
|
17
|
+
* Lifted from terragucci's `backend.ts` (`version`, `hasVersion`,
|
|
18
|
+
* `readVersion` of its stores), so terragucci deletes its copy.
|
|
19
|
+
*/
|
|
20
|
+
import type { StateVersionSource } from "@intentius/chant/state-export";
|
|
21
|
+
import { type StateObject } from "./backend.js";
|
|
22
|
+
import type { GitLabStateStore } from "./gitlab.js";
|
|
23
|
+
import type { StoreFetch } from "./object-store.js";
|
|
24
|
+
/** What the version reads cover, for a refusal. */
|
|
25
|
+
export declare const READS_VERSIONS = "a state export reads versions of s3, gcs and azurerm states and GitLab-managed state";
|
|
26
|
+
/**
|
|
27
|
+
* A root's state as a version source, or why it has none: a refused backend,
|
|
28
|
+
* or a local state, which keeps no versions. With `pin: false` an azurerm
|
|
29
|
+
* blob that keeps no versions names none for its current state, where the
|
|
30
|
+
* default takes a snapshot to name it.
|
|
31
|
+
*/
|
|
32
|
+
export declare function stateVersionSource(o: StateObject, fetchFn?: StoreFetch, options?: {
|
|
33
|
+
pin?: boolean;
|
|
34
|
+
}): StateVersionSource | {
|
|
35
|
+
refused: string;
|
|
36
|
+
};
|
|
37
|
+
/** A GitLab-managed state as a version source: the version id is the serial. */
|
|
38
|
+
export declare function gitlabVersionSource(store: GitLabStateStore): StateVersionSource;
|
|
39
|
+
//# sourceMappingURL=versions.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"versions.d.ts","sourceRoot":"","sources":["../../src/state/versions.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;GAkBG;AACH,OAAO,KAAK,EAAgB,kBAAkB,EAAE,MAAM,+BAA+B,CAAC;AAEtF,OAAO,EAA4B,KAAK,WAAW,EAAoB,MAAM,WAAW,CAAC;AAEzF,OAAO,KAAK,EAAE,gBAAgB,EAAE,MAAM,UAAU,CAAC;AACjD,OAAO,KAAK,EAAE,UAAU,EAAE,MAAM,gBAAgB,CAAC;AAGjD,mDAAmD;AACnD,eAAO,MAAM,cAAc,yFAAyF,CAAC;AAiDrH;;;;;GAKG;AACH,wBAAgB,kBAAkB,CAAC,CAAC,EAAE,WAAW,EAAE,OAAO,CAAC,EAAE,UAAU,EAAE,OAAO,GAAE;IAAE,GAAG,CAAC,EAAE,OAAO,CAAA;CAAO,GAAG,kBAAkB,GAAG;IAAE,OAAO,EAAE,MAAM,CAAA;CAAE,CAMlJ;AAED,gFAAgF;AAChF,wBAAgB,mBAAmB,CAAC,KAAK,EAAE,gBAAgB,GAAG,kBAAkB,CAU/E"}
|
|
@@ -0,0 +1,85 @@
|
|
|
1
|
+
import { AzureBlobClient } from "./azure-blob.js";
|
|
2
|
+
import { isRefused, stateLocation } from "./backend.js";
|
|
3
|
+
import { GcsClient } from "./gcs.js";
|
|
4
|
+
import { S3Client } from "./s3.js";
|
|
5
|
+
/** What the version reads cover, for a refusal. */
|
|
6
|
+
export const READS_VERSIONS = "a state export reads versions of s3, gcs and azurerm states and GitLab-managed state";
|
|
7
|
+
function s3Versions(o, fetchFn) {
|
|
8
|
+
const client = new S3Client(o.target, fetchFn);
|
|
9
|
+
return {
|
|
10
|
+
location: `s3://${o.bucket}/${o.key}`,
|
|
11
|
+
async current() {
|
|
12
|
+
const head = await client.head(o.key);
|
|
13
|
+
if (!head.exists)
|
|
14
|
+
return { exists: false };
|
|
15
|
+
return head.versionId ? { exists: true, versionId: head.versionId } : { exists: true, off: `bucket versioning is off for ${o.bucket}, so it keeps only the latest state` };
|
|
16
|
+
},
|
|
17
|
+
hasVersion: (id) => client.hasVersion(o.key, id),
|
|
18
|
+
readVersion: (id) => client.readVersion(o.key, id),
|
|
19
|
+
};
|
|
20
|
+
}
|
|
21
|
+
function gcsVersions(o, fetchFn) {
|
|
22
|
+
const client = new GcsClient(o.target, fetchFn);
|
|
23
|
+
return {
|
|
24
|
+
location: `gs://${o.bucket}/${o.key}`,
|
|
25
|
+
async current() {
|
|
26
|
+
const head = await client.head(o.key);
|
|
27
|
+
if (!head.exists)
|
|
28
|
+
return { exists: false };
|
|
29
|
+
if ((await client.versioning()) === false)
|
|
30
|
+
return { exists: true, off: `object versioning is off for ${o.bucket}, so it keeps only the latest state` };
|
|
31
|
+
return { exists: true, ...(head.generation ? { versionId: head.generation } : {}) };
|
|
32
|
+
},
|
|
33
|
+
hasVersion: async (id) => (await client.head(o.key, id)).exists,
|
|
34
|
+
readVersion: (id) => client.readGeneration(o.key, id),
|
|
35
|
+
};
|
|
36
|
+
}
|
|
37
|
+
function azureVersions(o, fetchFn, pin = true) {
|
|
38
|
+
const client = new AzureBlobClient({ account: o.account, container: o.container, endpoint: o.endpoint, ...o.auth }, fetchFn);
|
|
39
|
+
// A blob that keeps versions names each by its version id; else by a snapshot.
|
|
40
|
+
const at = async (id) => ((await client.head(o.key)).versionId ? { versionid: id } : { snapshot: id });
|
|
41
|
+
return {
|
|
42
|
+
location: `az://${o.account}/${o.container}/${o.key}`,
|
|
43
|
+
async current() {
|
|
44
|
+
const head = await client.head(o.key);
|
|
45
|
+
if (!head.exists)
|
|
46
|
+
return { exists: false };
|
|
47
|
+
if (head.versionId)
|
|
48
|
+
return { exists: true, versionId: head.versionId };
|
|
49
|
+
if (!o.snapshot)
|
|
50
|
+
return { exists: true, off: `blob versioning is off for ${o.account}, and the backend takes no snapshots (snapshot = true), so it keeps only the latest state` };
|
|
51
|
+
return pin ? { exists: true, versionId: await client.snapshot(o.key) } : { exists: true, off: "the blob keeps no versions, and only a snapshot would name this one" };
|
|
52
|
+
},
|
|
53
|
+
hasVersion: async (id) => (await client.head(o.key, await at(id))).exists,
|
|
54
|
+
readVersion: async (id) => client.readAt(o.key, await at(id)),
|
|
55
|
+
};
|
|
56
|
+
}
|
|
57
|
+
/**
|
|
58
|
+
* A root's state as a version source, or why it has none: a refused backend,
|
|
59
|
+
* or a local state, which keeps no versions. With `pin: false` an azurerm
|
|
60
|
+
* blob that keeps no versions names none for its current state, where the
|
|
61
|
+
* default takes a snapshot to name it.
|
|
62
|
+
*/
|
|
63
|
+
export function stateVersionSource(o, fetchFn, options = {}) {
|
|
64
|
+
if (isRefused(o))
|
|
65
|
+
return { refused: o.reason };
|
|
66
|
+
if (o.backend === "local")
|
|
67
|
+
return { refused: `the state is a local file, ${stateLocation(o)}, which keeps no versions; ${READS_VERSIONS}` };
|
|
68
|
+
if (o.backend === "s3")
|
|
69
|
+
return s3Versions(o, fetchFn);
|
|
70
|
+
if (o.backend === "gcs")
|
|
71
|
+
return gcsVersions(o, fetchFn);
|
|
72
|
+
return azureVersions(o, fetchFn, options.pin ?? true);
|
|
73
|
+
}
|
|
74
|
+
/** A GitLab-managed state as a version source: the version id is the serial. */
|
|
75
|
+
export function gitlabVersionSource(store) {
|
|
76
|
+
return {
|
|
77
|
+
location: store.location,
|
|
78
|
+
async current() {
|
|
79
|
+
const now = await store.serial();
|
|
80
|
+
return now ? { exists: true, versionId: String(now.serial) } : { exists: false };
|
|
81
|
+
},
|
|
82
|
+
hasVersion: async (id) => /^\d+$/.test(id) && (await store.hasVersion(id)),
|
|
83
|
+
readVersion: (id) => store.readVersion(id),
|
|
84
|
+
};
|
|
85
|
+
}
|
|
@@ -0,0 +1,85 @@
|
|
|
1
|
+
import type { MigrationRootPlan, StateMigrationEngine } from "@intentius/chant/state-migration";
|
|
2
|
+
import { type RootRun } from "./state-ops.js";
|
|
3
|
+
import { type BinaryExec, type StateFile, type StateResource } from "./state/exec.js";
|
|
4
|
+
import { type LockOperation } from "./state/locks.js";
|
|
5
|
+
import type { StoreFetch } from "./state/object-store.js";
|
|
6
|
+
/** Resources that move from one root's state to another's. */
|
|
7
|
+
export interface Move {
|
|
8
|
+
from: string;
|
|
9
|
+
to: string;
|
|
10
|
+
addresses: string[];
|
|
11
|
+
}
|
|
12
|
+
/** A root's state put back to a version a migration recorded. */
|
|
13
|
+
export interface Restore {
|
|
14
|
+
root: string;
|
|
15
|
+
/** Where the state is, as the migration recorded it. */
|
|
16
|
+
location: string;
|
|
17
|
+
/** The version to put back; null when the root had no state before, so it gets an empty one. */
|
|
18
|
+
version_id: string | null;
|
|
19
|
+
/** The version the migration left: a state that moved past it is refused. */
|
|
20
|
+
from_version_id: string | null;
|
|
21
|
+
}
|
|
22
|
+
/** A migration that cannot be planned: an address in no state, a root that is refused. Nothing was written. */
|
|
23
|
+
export declare class MigrationError extends Error {
|
|
24
|
+
constructor(message: string);
|
|
25
|
+
}
|
|
26
|
+
/** A state's digest: sha256 of its JSON as parsed and written back, so whitespace never moves it. */
|
|
27
|
+
export declare const stateDigest: (s: StateFile) => string;
|
|
28
|
+
/** Whether a migration address names a state entry: the managed resource itself, or a module call it sits in, with its data sources. */
|
|
29
|
+
export declare function addressMatches(address: string, r: StateResource): boolean;
|
|
30
|
+
/** A lineage that a migration and a root always give the same: a state planned twice has one digest. */
|
|
31
|
+
export declare function derivedLineage(...parts: string[]): string;
|
|
32
|
+
/**
|
|
33
|
+
* The states after the moves. `before` holds each affected root's state, or
|
|
34
|
+
* null when it has none. A changed state keeps its lineage and gets the next
|
|
35
|
+
* serial; a root with no state gets a new one, its lineage derived from the
|
|
36
|
+
* migration and the root. Throws MigrationError naming every address that
|
|
37
|
+
* is in no state, or already in the root it moves to.
|
|
38
|
+
*/
|
|
39
|
+
export declare function moveResources(name: string, moves: readonly Move[], before: ReadonlyMap<string, StateFile | null>): Map<string, StateFile>;
|
|
40
|
+
/** The resource changes of a plan's JSON, as `<address>: <actions>`; no-ops and reads are not changes. */
|
|
41
|
+
export declare function resourceChanges(plan: unknown): string[];
|
|
42
|
+
/** A terraform block whose backend is the local file `path`, for the proof plan's override file. Terraform and OpenTofu merge any `*_override.tf` over the root's files. */
|
|
43
|
+
export declare const localBackendOverride: (path: string) => string;
|
|
44
|
+
/** The override file a proof plan writes into a root, and removes, unless the product names its own. */
|
|
45
|
+
export declare const DEFAULT_OVERRIDE_FILE = "chant_migrate_override.tf";
|
|
46
|
+
/** What a migration's lock says, unless the product names its own. */
|
|
47
|
+
export declare const DEFAULT_MIGRATE_LOCK: LockOperation;
|
|
48
|
+
/** One root a migration reads or writes. */
|
|
49
|
+
export interface MigrationRootRun extends Omit<RootRun, "exec"> {
|
|
50
|
+
/** Run `init` in a data dir of the migration's own (default), or false for a root another tool initialised in `dir` with `env`. */
|
|
51
|
+
init?: boolean;
|
|
52
|
+
}
|
|
53
|
+
export interface TerraformMigrationInput {
|
|
54
|
+
/** The migration's name: what its gate is called, and what a new root's lineage derives from. */
|
|
55
|
+
name: string;
|
|
56
|
+
/** Resources that move between roots. */
|
|
57
|
+
moves?: Move[];
|
|
58
|
+
/** States put back to a version a migration recorded. */
|
|
59
|
+
restores?: Restore[];
|
|
60
|
+
/** Every root the moves or restores name. */
|
|
61
|
+
roots: MigrationRootRun[];
|
|
62
|
+
/** The work dir: new states, plans and data dirs. The caller removes it. */
|
|
63
|
+
work: string;
|
|
64
|
+
exec?: BinaryExec;
|
|
65
|
+
fetch?: StoreFetch;
|
|
66
|
+
/** What the migration's lock says. */
|
|
67
|
+
lock?: LockOperation;
|
|
68
|
+
/** Who the lock names. Default `chant@<host>`. */
|
|
69
|
+
who?: string;
|
|
70
|
+
/** The proof plan's override file. Default {@link DEFAULT_OVERRIDE_FILE}. */
|
|
71
|
+
overrideFile?: string;
|
|
72
|
+
log?: (line: string) => void;
|
|
73
|
+
}
|
|
74
|
+
/** A planned migration: each root's plan for core, and the engine that writes it. */
|
|
75
|
+
export interface TerraformMigrationPlan {
|
|
76
|
+
roots: MigrationRootPlan[];
|
|
77
|
+
engine: StateMigrationEngine;
|
|
78
|
+
}
|
|
79
|
+
/**
|
|
80
|
+
* Plan a migration: read every affected state and its version, build the new
|
|
81
|
+
* states in `work`, and plan each root against its new state. Throws
|
|
82
|
+
* MigrationError for a root that is refused or a move that does not fit.
|
|
83
|
+
*/
|
|
84
|
+
export declare function planTerraformMigration(input: TerraformMigrationInput): Promise<TerraformMigrationPlan>;
|
|
85
|
+
//# sourceMappingURL=state-migration.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"state-migration.d.ts","sourceRoot":"","sources":["../src/state-migration.ts"],"names":[],"mappings":"AAuCA,OAAO,KAAK,EAAkB,iBAAiB,EAAoB,oBAAoB,EAAE,MAAM,kCAAkC,CAAC;AAClI,OAAO,EAA2C,KAAK,OAAO,EAAE,MAAM,aAAa,CAAC;AAEpF,OAAO,EAAwB,KAAK,UAAU,EAAE,KAAK,SAAS,EAAE,KAAK,aAAa,EAAE,MAAM,cAAc,CAAC;AACzG,OAAO,EAAwB,KAAK,aAAa,EAAE,MAAM,eAAe,CAAC;AACzE,OAAO,KAAK,EAAE,UAAU,EAAE,MAAM,sBAAsB,CAAC;AAIvD,8DAA8D;AAC9D,MAAM,WAAW,IAAI;IACnB,IAAI,EAAE,MAAM,CAAC;IACb,EAAE,EAAE,MAAM,CAA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