@intentius/chant-lexicon-terraform 0.124.85 → 0.124.86
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/ci-setup.d.ts +11 -2
- package/dist/ci-setup.d.ts.map +1 -1
- package/dist/ci-setup.js +11 -6
- package/dist/components/capability-plugin.d.ts +12 -0
- package/dist/components/capability-plugin.d.ts.map +1 -1
- package/dist/components/capability-plugin.js +22 -4
- package/dist/compose.d.ts.map +1 -1
- package/dist/compose.js +1 -1
- package/dist/estate-migration.d.ts +101 -0
- package/dist/estate-migration.d.ts.map +1 -0
- package/dist/estate-migration.js +471 -0
- package/dist/integrity.json +2 -2
- package/dist/manifest.json +1 -1
- package/dist/op/activities/terraform.d.ts +65 -0
- package/dist/op/activities/terraform.d.ts.map +1 -1
- package/dist/op/activities/terraform.js +94 -1
- package/dist/state/backend.d.ts +46 -9
- package/dist/state/backend.d.ts.map +1 -1
- package/dist/state/backend.js +84 -31
- package/dist/state/gitlab.d.ts +1 -1
- package/dist/state/gitlab.d.ts.map +1 -1
- package/dist/state/gitlab.js +27 -14
- package/dist/state/index.d.ts +1 -0
- package/dist/state/index.d.ts.map +1 -1
- package/dist/state/index.js +1 -0
- package/dist/state/s3.d.ts +9 -3
- package/dist/state/s3.d.ts.map +1 -1
- package/dist/state/s3.js +4 -3
- package/dist/state/source.d.ts +92 -0
- package/dist/state/source.d.ts.map +1 -0
- package/dist/state/source.js +254 -0
- package/dist/state/store.d.ts +41 -4
- package/dist/state/store.d.ts.map +1 -1
- package/dist/state/store.js +125 -10
- package/dist/state/versions.d.ts +77 -10
- package/dist/state/versions.d.ts.map +1 -1
- package/dist/state/versions.js +118 -64
- package/dist/state-migration.d.ts +43 -1
- package/dist/state-migration.d.ts.map +1 -1
- package/dist/state-migration.js +251 -20
- package/dist/state-ops.d.ts +2 -3
- package/dist/state-ops.d.ts.map +1 -1
- package/dist/state-ops.js +4 -7
- package/package.json +2 -2
- package/src/ci-setup.test.ts +20 -0
- package/src/ci-setup.ts +11 -6
- package/src/components/capability-plugin.ts +36 -3
- package/src/components/terraform-apply-command-hook.test.ts +157 -0
- package/src/compose.ts +1 -0
- package/src/estate-migration.test.ts +186 -0
- package/src/estate-migration.ts +563 -0
- package/src/op/activities/terraform.ts +153 -3
- package/src/state/backend-verdict.test.ts +4 -3
- package/src/state/backend.test.ts +3 -1
- package/src/state/backend.ts +108 -33
- package/src/state/gitlab.ts +25 -12
- package/src/state/index.ts +1 -0
- package/src/state/s3.ts +5 -4
- package/src/state/source.ts +289 -0
- package/src/state/store.ts +145 -11
- package/src/state/versions.test.ts +365 -0
- package/src/state/versions.ts +148 -68
- package/src/state-migration-backends.test.ts +277 -0
- package/src/state-migration.ts +265 -23
- package/src/state-ops.ts +6 -8
|
@@ -0,0 +1,92 @@
|
|
|
1
|
+
import type { StoreFetch } from "./object-store.js";
|
|
2
|
+
/** A source cannot be read: its config, its answer, or its token. Nothing was written. */
|
|
3
|
+
export declare class StateSourceError extends Error {
|
|
4
|
+
constructor(message: string);
|
|
5
|
+
}
|
|
6
|
+
/** The backends a move reads from without the binary: a TFE-API workspace, and a state file. */
|
|
7
|
+
export declare const EXTERNAL_BACKENDS: string[];
|
|
8
|
+
/** A state as the source gives it: its JSON, and the source's version id when it keeps versions. */
|
|
9
|
+
export interface ReadState {
|
|
10
|
+
text: string;
|
|
11
|
+
version?: string;
|
|
12
|
+
}
|
|
13
|
+
/** A source of a backend move that the job reads itself. */
|
|
14
|
+
export interface ExternalSource {
|
|
15
|
+
backend: string;
|
|
16
|
+
/** Where the state is, as the record and the digest name it. */
|
|
17
|
+
location: string;
|
|
18
|
+
read(): Promise<ReadState | null>;
|
|
19
|
+
/** Take the source's lock; returns its release. Throws StateSourceError when another holds it. */
|
|
20
|
+
lock?(reason: string): Promise<() => Promise<void>>;
|
|
21
|
+
}
|
|
22
|
+
/** Problems with the config of an external source, as `<at>.from.config...` lines. */
|
|
23
|
+
export declare function externalProblems(at: string, backend: string, config: Record<string, unknown>): string[];
|
|
24
|
+
/** The variable Terraform and OpenTofu read a host's token from: dots to `_`, dashes to `__`. */
|
|
25
|
+
export declare const tokenVariable: (host: string) => string;
|
|
26
|
+
/** A host's token and where it was found: a variable's name or a file's path, never the value. */
|
|
27
|
+
export interface HostToken {
|
|
28
|
+
token: string;
|
|
29
|
+
from: string;
|
|
30
|
+
}
|
|
31
|
+
/** The token for a host, found as Terraform and OpenTofu find it, by chant's reader. */
|
|
32
|
+
export declare function tokenFor(host: string, env: NodeJS.ProcessEnv): Promise<HostToken | undefined>;
|
|
33
|
+
/** The source a backend move reads from, when the job reads it itself; undefined for a backend read through the root. */
|
|
34
|
+
export declare function externalSource(repo: string, backend: string, config: Record<string, unknown>, env: NodeJS.ProcessEnv, fetchFn?: StoreFetch): ExternalSource | undefined;
|
|
35
|
+
/** A state file: in the repo, or at an absolute path the job can read. No versions, no lock; its digest is in the migration's. */
|
|
36
|
+
export declare function fileSource(repo: string, path: string): ExternalSource;
|
|
37
|
+
interface Json {
|
|
38
|
+
data?: {
|
|
39
|
+
id?: string;
|
|
40
|
+
attributes?: Record<string, unknown>;
|
|
41
|
+
};
|
|
42
|
+
}
|
|
43
|
+
/** A JSON:API resource as the TFE API and Scalr's give it. */
|
|
44
|
+
export interface ApiResource {
|
|
45
|
+
id: string;
|
|
46
|
+
type?: string;
|
|
47
|
+
attributes?: Record<string, unknown>;
|
|
48
|
+
relationships?: Record<string, {
|
|
49
|
+
data?: {
|
|
50
|
+
id?: string;
|
|
51
|
+
type?: string;
|
|
52
|
+
} | {
|
|
53
|
+
id?: string;
|
|
54
|
+
type?: string;
|
|
55
|
+
}[] | null;
|
|
56
|
+
}>;
|
|
57
|
+
}
|
|
58
|
+
/** A call's answer: its status and body. */
|
|
59
|
+
export interface ApiAnswer {
|
|
60
|
+
status: number;
|
|
61
|
+
text: string;
|
|
62
|
+
}
|
|
63
|
+
/** A client of a host that speaks the TFE API: its token, discovery and JSON:API reads. */
|
|
64
|
+
export interface TfeClient {
|
|
65
|
+
host: string;
|
|
66
|
+
/** Whether a token was found for the host; never printed. */
|
|
67
|
+
hasToken(): Promise<boolean>;
|
|
68
|
+
call(url: string, method?: string, body?: string): Promise<ApiAnswer>;
|
|
69
|
+
/** The body of a 2xx answer as JSON; a 401 or 404 says what to check, naming `what`. */
|
|
70
|
+
json<T = Json>(what: string, r: ApiAnswer): T;
|
|
71
|
+
/** Where the token the calls send was found (a variable's name or a file's path), once a call has run. */
|
|
72
|
+
tokenFrom(): string | undefined;
|
|
73
|
+
/** The `tfe.v2` base that discovery names, ending in `/`. */
|
|
74
|
+
api(): Promise<string>;
|
|
75
|
+
/** Every resource of a list, page by page (`meta.pagination.next-page`). */
|
|
76
|
+
list(what: string, url: string): Promise<{
|
|
77
|
+
data: ApiResource[];
|
|
78
|
+
included: ApiResource[];
|
|
79
|
+
}>;
|
|
80
|
+
}
|
|
81
|
+
/**
|
|
82
|
+
* A client of `host` over the TFE API, as the remote backend and the cloud
|
|
83
|
+
* block reach it: discovery at /.well-known/terraform.json, the token in
|
|
84
|
+
* `TF_TOKEN_<host>` or credentials.tfrc.json (else `tokenEnv`, when named),
|
|
85
|
+
* sent only to the host and the API discovery names. `location` heads every
|
|
86
|
+
* error.
|
|
87
|
+
*/
|
|
88
|
+
export declare function tfeClient(host: string, location: string, env: NodeJS.ProcessEnv, fetchFn?: StoreFetch, tokenEnv?: string): TfeClient;
|
|
89
|
+
/** A workspace's state over the TFE API, as the remote backend and the cloud block read it. */
|
|
90
|
+
export declare function workspaceSource(backend: string, host: string, org: string, name: string, env: NodeJS.ProcessEnv, fetchFn?: StoreFetch): ExternalSource;
|
|
91
|
+
export {};
|
|
92
|
+
//# sourceMappingURL=source.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"source.d.ts","sourceRoot":"","sources":["../../src/state/source.ts"],"names":[],"mappings":"AA0BA,OAAO,KAAK,EAAE,UAAU,EAAE,MAAM,gBAAgB,CAAC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AAC,CAAC;IAC9C,0GAA0G;IAC1G,SAAS,IAAI,MAAM,GAAG,SAAS,CAAC;IAChC,6DAA6D;IAC7D,GAAG,IAAI,OAAO,CAAC,MAAM,CAAC,CAAC;IACvB,4EAA4E;IAC5E,IAAI,CAAC,IAAI,EAAE,MAAM,EAAE,GAAG,EAAE,MAAM,GAAG,OAAO,CAAC;QAAE,IAAI,EAAE,WAAW,EAAE,CAAC;QAAC,QAAQ,EAAE,WAAW,EAAE,CAAA;KAAE,CAAC,CAAC;CAC5F;AAED;;;;;;GAMG;AACH,wBAAgB,SAAS,CAAC,IAAI,EAAE,MAAM,EAAE,QAAQ,EAAE,MAAM,EAAE,GAAG,EAAE,MAAM,CAAC,UAAU,EAAE,OAAO,GAAE,UAA2C,EAAE,QAAQ,CAAC,EAAE,MAAM,GAAG,SAAS,CAgEpK;AAED,+FAA+F;AAC/F,wBAAgB,eAAe,CAAC,OAAO,EAAE,MAAM,EAAE,IAAI,EAAE,MAAM,EAAE,GAAG,EAAE,MAAM,EAAE,IAAI,EAAE,MAAM,EAAE,GAAG,EAAE,MAAM,CAAC,UAAU,EAAE,OAAO,GAAE,UAA2C,GAAG,cAAc,CAsDtL"}
|
|
@@ -0,0 +1,254 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Where a backend move (#4806, `../state-migration.ts`) reads a state that no
|
|
3
|
+
* backend block of the root can reach with an override file: a workspace of
|
|
4
|
+
* HCP Terraform or another service that speaks its API (a `remote` backend
|
|
5
|
+
* or a `cloud` block), and a state file exported from a platform that keeps
|
|
6
|
+
* state its own way.
|
|
7
|
+
*
|
|
8
|
+
* A workspace's state is read over the API the stock `remote` backend and
|
|
9
|
+
* `cloud` block use: service discovery at
|
|
10
|
+
* `https://<hostname>/.well-known/terraform.json` gives the `tfe.v2` base,
|
|
11
|
+
* then the workspace by organization and name, its current state version
|
|
12
|
+
* and that version's download URL. The token is the one Terraform and
|
|
13
|
+
* OpenTofu use for the host: `TF_TOKEN_<host>`, else a `credentials` block
|
|
14
|
+
* in the CLI config, else `~/.terraform.d/credentials.tfrc.json`, read by
|
|
15
|
+
* chant core's reader (`@intentius/chant/terraform/credentials`, #4563). A
|
|
16
|
+
* migration holds the workspace's lock while it writes, as it holds an S3
|
|
17
|
+
* state's lock file, and the workspace's state is left where it was.
|
|
18
|
+
*
|
|
19
|
+
* `tfeClient` is the one client of that API, which a product's importers of
|
|
20
|
+
* TFE and Scalr workspaces use too.
|
|
21
|
+
*
|
|
22
|
+
* Lifted from terragucci's `state-source.ts`, so terragucci deletes it.
|
|
23
|
+
*/
|
|
24
|
+
import { existsSync, readFileSync } from "node:fs";
|
|
25
|
+
import { isAbsolute, join } from "node:path";
|
|
26
|
+
import { terraformCredential } from "@intentius/chant/terraform/credentials";
|
|
27
|
+
/** A source cannot be read: its config, its answer, or its token. Nothing was written. */
|
|
28
|
+
export class StateSourceError extends Error {
|
|
29
|
+
constructor(message) {
|
|
30
|
+
super(message);
|
|
31
|
+
this.name = "StateSourceError";
|
|
32
|
+
}
|
|
33
|
+
}
|
|
34
|
+
/** The backends a move reads from without the binary: a TFE-API workspace, and a state file. */
|
|
35
|
+
export const EXTERNAL_BACKENDS = ["remote", "cloud", "file"];
|
|
36
|
+
const DEFAULT_HOST = "app.terraform.io";
|
|
37
|
+
const HOST = /^[a-z0-9]([a-z0-9.-]*[a-z0-9])?(:\d+)?$/i;
|
|
38
|
+
/** Problems with the config of an external source, as `<at>.from.config...` lines. */
|
|
39
|
+
export function externalProblems(at, backend, config) {
|
|
40
|
+
const p = [];
|
|
41
|
+
if (backend === "file") {
|
|
42
|
+
for (const k of Object.keys(config))
|
|
43
|
+
if (k !== "path")
|
|
44
|
+
p.push(`${at}.from.config.${k} is not a key; a file source has path`);
|
|
45
|
+
if (typeof config.path !== "string" || !config.path.trim())
|
|
46
|
+
p.push(`${at}.from.config.path must name the state file`);
|
|
47
|
+
else if (!isAbsolute(config.path) && config.path.split("/").includes(".."))
|
|
48
|
+
p.push(`${at}.from.config.path must be in the repo or an absolute path, not ${config.path}`);
|
|
49
|
+
return p;
|
|
50
|
+
}
|
|
51
|
+
for (const k of Object.keys(config)) {
|
|
52
|
+
if (k === "token")
|
|
53
|
+
p.push(`${at}.from.config.token: a token does not belong in the repo; the job reads TF_TOKEN_<host> or credentials.tfrc.json`);
|
|
54
|
+
else if (!["hostname", "organization", "workspaces"].includes(k))
|
|
55
|
+
p.push(`${at}.from.config.${k} is not a key; a ${backend} source has hostname, organization and workspaces`);
|
|
56
|
+
}
|
|
57
|
+
if (config.hostname !== undefined && (typeof config.hostname !== "string" || !HOST.test(config.hostname)))
|
|
58
|
+
p.push(`${at}.from.config.hostname must be a host name, such as ${DEFAULT_HOST}`);
|
|
59
|
+
if (typeof config.organization !== "string" || !config.organization)
|
|
60
|
+
p.push(`${at}.from.config.organization must name the organization`);
|
|
61
|
+
const ws = config.workspaces && typeof config.workspaces === "object" && !Array.isArray(config.workspaces) ? config.workspaces : undefined;
|
|
62
|
+
if (!ws || typeof ws.name !== "string" || !ws.name)
|
|
63
|
+
p.push(`${at}.from.config.workspaces.name must name the one workspace whose state moves`);
|
|
64
|
+
for (const k of Object.keys(ws ?? {}))
|
|
65
|
+
if (k !== "name")
|
|
66
|
+
p.push(`${at}.from.config.workspaces.${k}: a move reads one workspace, named by workspaces.name`);
|
|
67
|
+
return p;
|
|
68
|
+
}
|
|
69
|
+
/** The variable Terraform and OpenTofu read a host's token from: dots to `_`, dashes to `__`. */
|
|
70
|
+
export const tokenVariable = (host) => `TF_TOKEN_${host.replace(/-/g, "__").replace(/\./g, "_")}`;
|
|
71
|
+
/** The token for a host, found as Terraform and OpenTofu find it, by chant's reader. */
|
|
72
|
+
export async function tokenFor(host, env) {
|
|
73
|
+
const got = terraformCredential(host, { env });
|
|
74
|
+
return got ? { token: got.token, from: got.from } : undefined;
|
|
75
|
+
}
|
|
76
|
+
/** What to set for a host's token: its variable, or a credentials block for a host whose port has no variable form. */
|
|
77
|
+
const tokenSetting = (host) => (/:\d+$/.test(host) && !host.endsWith(":443") ? `a credentials "${host}" block in the CLI config` : tokenVariable(host.replace(/:443$/, "")));
|
|
78
|
+
/** The source a backend move reads from, when the job reads it itself; undefined for a backend read through the root. */
|
|
79
|
+
export function externalSource(repo, backend, config, env, fetchFn) {
|
|
80
|
+
if (backend === "file")
|
|
81
|
+
return fileSource(repo, String(config.path));
|
|
82
|
+
if (backend !== "remote" && backend !== "cloud")
|
|
83
|
+
return undefined;
|
|
84
|
+
const ws = config.workspaces;
|
|
85
|
+
return workspaceSource(backend, typeof config.hostname === "string" ? config.hostname : DEFAULT_HOST, String(config.organization), ws.name, env, fetchFn);
|
|
86
|
+
}
|
|
87
|
+
/** A state file: in the repo, or at an absolute path the job can read. No versions, no lock; its digest is in the migration's. */
|
|
88
|
+
export function fileSource(repo, path) {
|
|
89
|
+
const full = isAbsolute(path) ? path : join(repo, path);
|
|
90
|
+
return {
|
|
91
|
+
backend: "file",
|
|
92
|
+
location: path,
|
|
93
|
+
read: async () => {
|
|
94
|
+
if (!existsSync(full))
|
|
95
|
+
return null;
|
|
96
|
+
const text = readFileSync(full, "utf-8");
|
|
97
|
+
let doc;
|
|
98
|
+
try {
|
|
99
|
+
doc = JSON.parse(text);
|
|
100
|
+
}
|
|
101
|
+
catch {
|
|
102
|
+
throw new StateSourceError(`${path} is not a state file: it is not JSON`);
|
|
103
|
+
}
|
|
104
|
+
if (doc.version !== 4 || typeof doc.lineage !== "string" || typeof doc.serial !== "number" || !Array.isArray(doc.resources))
|
|
105
|
+
throw new StateSourceError(`${path} is not a state file of format version 4 with a lineage, a serial and resources`);
|
|
106
|
+
return { text };
|
|
107
|
+
},
|
|
108
|
+
};
|
|
109
|
+
}
|
|
110
|
+
/**
|
|
111
|
+
* A client of `host` over the TFE API, as the remote backend and the cloud
|
|
112
|
+
* block reach it: discovery at /.well-known/terraform.json, the token in
|
|
113
|
+
* `TF_TOKEN_<host>` or credentials.tfrc.json (else `tokenEnv`, when named),
|
|
114
|
+
* sent only to the host and the API discovery names. `location` heads every
|
|
115
|
+
* error.
|
|
116
|
+
*/
|
|
117
|
+
export function tfeClient(host, location, env, fetchFn = fetch, tokenEnv) {
|
|
118
|
+
let found;
|
|
119
|
+
/** The token, looked up once on the first call: `TF_TOKEN_<host>` or the CLI's credentials, else `tokenEnv`. */
|
|
120
|
+
const lookup = () => (found ?? (found = tokenFor(host, env).then((t) => t ?? (tokenEnv && env[tokenEnv] ? { token: env[tokenEnv], from: tokenEnv } : undefined))));
|
|
121
|
+
let held;
|
|
122
|
+
const origin = `https://${host}`;
|
|
123
|
+
let base;
|
|
124
|
+
const call = async (url, method = "GET", body) => {
|
|
125
|
+
held = await lookup();
|
|
126
|
+
const token = held?.token;
|
|
127
|
+
// The token goes to the host and the API discovery names (Spacelift's is on another host), as the binary sends it; never to a download elsewhere.
|
|
128
|
+
const sameHost = [host, base ? new URL(base).host : host].includes(new URL(url).host);
|
|
129
|
+
const headers = { accept: "application/vnd.api+json", ...(body ? { "content-type": "application/vnd.api+json" } : {}), ...(token && sameHost ? { authorization: `Bearer ${token}` } : {}) };
|
|
130
|
+
let r;
|
|
131
|
+
try {
|
|
132
|
+
r = await fetchFn(url, { method, headers, ...(body ? { body } : {}) });
|
|
133
|
+
}
|
|
134
|
+
catch (e) {
|
|
135
|
+
const cause = e.cause?.message;
|
|
136
|
+
throw new StateSourceError(`${location}: ${method} ${new URL(url).pathname} failed: ${e.message}${cause ? ` (${cause})` : ""}`);
|
|
137
|
+
}
|
|
138
|
+
return { status: r.status, text: await r.text() };
|
|
139
|
+
};
|
|
140
|
+
const tokenHint = () => (held ? `the token in ${held.from} may not read it` : `there is no token for ${host}: set ${tokenSetting(host)}${tokenEnv ? ` or ${tokenEnv}` : ""}`);
|
|
141
|
+
const json = (what, r) => {
|
|
142
|
+
if (r.status === 401 || r.status === 404)
|
|
143
|
+
throw new StateSourceError(`${location}: ${what} answered ${r.status}; ${tokenHint()}`);
|
|
144
|
+
if (r.status >= 300)
|
|
145
|
+
throw new StateSourceError(`${location}: ${what} answered ${r.status}: ${r.text.slice(0, 200)}`);
|
|
146
|
+
try {
|
|
147
|
+
return JSON.parse(r.text);
|
|
148
|
+
}
|
|
149
|
+
catch {
|
|
150
|
+
throw new StateSourceError(`${location}: ${what} answered with something that is not JSON`);
|
|
151
|
+
}
|
|
152
|
+
};
|
|
153
|
+
const api = async () => {
|
|
154
|
+
if (base)
|
|
155
|
+
return base;
|
|
156
|
+
const r = await call(`${origin}/.well-known/terraform.json`);
|
|
157
|
+
let doc = {};
|
|
158
|
+
try {
|
|
159
|
+
doc = r.status < 300 ? JSON.parse(r.text) : {};
|
|
160
|
+
}
|
|
161
|
+
catch {
|
|
162
|
+
doc = {};
|
|
163
|
+
}
|
|
164
|
+
const v2 = doc["tfe.v2"];
|
|
165
|
+
if (typeof v2 !== "string")
|
|
166
|
+
throw new StateSourceError(`${location}: ${host} names no tfe.v2 API in /.well-known/terraform.json, so it does not speak the remote backend's protocol`);
|
|
167
|
+
base = new URL(v2, `${origin}/`).toString().replace(/\/?$/, "/");
|
|
168
|
+
return base;
|
|
169
|
+
};
|
|
170
|
+
const list = async (what, url) => {
|
|
171
|
+
const data = [];
|
|
172
|
+
const included = [];
|
|
173
|
+
for (let page = 1, guard = 0; page && guard < 1000; guard++) {
|
|
174
|
+
const u = new URL(url);
|
|
175
|
+
u.searchParams.set("page[number]", String(page));
|
|
176
|
+
u.searchParams.set("page[size]", "100");
|
|
177
|
+
const doc = json(what, await call(u.toString()));
|
|
178
|
+
if (!Array.isArray(doc.data))
|
|
179
|
+
throw new StateSourceError(`${location}: ${what} answered with no list`);
|
|
180
|
+
data.push(...doc.data);
|
|
181
|
+
if (Array.isArray(doc.included))
|
|
182
|
+
included.push(...doc.included);
|
|
183
|
+
const next = doc.meta?.pagination?.["next-page"];
|
|
184
|
+
page = typeof next === "number" && next > page ? next : 0;
|
|
185
|
+
}
|
|
186
|
+
return { data, included };
|
|
187
|
+
};
|
|
188
|
+
return { host, hasToken: async () => (await lookup()) !== undefined, call, json, api, list, tokenFrom: () => held?.from };
|
|
189
|
+
}
|
|
190
|
+
/** A workspace's state over the TFE API, as the remote backend and the cloud block read it. */
|
|
191
|
+
export function workspaceSource(backend, host, org, name, env, fetchFn = fetch) {
|
|
192
|
+
const location = `${backend}://${host}/${org}/${name}`;
|
|
193
|
+
const origin = `https://${host}`;
|
|
194
|
+
const { call, api, tokenFrom } = tfeClient(host, location, env, fetchFn);
|
|
195
|
+
const json = (what, r) => {
|
|
196
|
+
// The state read's own wording: the workspace, not "it".
|
|
197
|
+
if (r.status === 401 || r.status === 404) {
|
|
198
|
+
const from = tokenFrom();
|
|
199
|
+
throw new StateSourceError(`${location}: ${what} answered ${r.status}; ${from ? `the token in ${from} may not read the workspace` : `the job has no token for ${host}: set ${tokenSetting(host)}`}`);
|
|
200
|
+
}
|
|
201
|
+
if (r.status >= 300)
|
|
202
|
+
throw new StateSourceError(`${location}: ${what} answered ${r.status}: ${r.text.slice(0, 200)}`);
|
|
203
|
+
try {
|
|
204
|
+
return JSON.parse(r.text);
|
|
205
|
+
}
|
|
206
|
+
catch {
|
|
207
|
+
throw new StateSourceError(`${location}: ${what} answered with something that is not JSON`);
|
|
208
|
+
}
|
|
209
|
+
};
|
|
210
|
+
let id;
|
|
211
|
+
const workspace = async () => {
|
|
212
|
+
if (id)
|
|
213
|
+
return id;
|
|
214
|
+
const b = await api();
|
|
215
|
+
const got = json("the workspace", await call(`${b}organizations/${encodeURIComponent(org)}/workspaces/${encodeURIComponent(name)}`));
|
|
216
|
+
if (!got.data?.id)
|
|
217
|
+
throw new StateSourceError(`${location}: the workspace answered with no id`);
|
|
218
|
+
id = got.data.id;
|
|
219
|
+
return id;
|
|
220
|
+
};
|
|
221
|
+
return {
|
|
222
|
+
backend,
|
|
223
|
+
location,
|
|
224
|
+
read: async () => {
|
|
225
|
+
const b = await api();
|
|
226
|
+
const ws = await workspace();
|
|
227
|
+
const r = await call(`${b}workspaces/${ws}/current-state-version`);
|
|
228
|
+
// The workspace answered, so no current version is no state yet.
|
|
229
|
+
if (r.status === 404)
|
|
230
|
+
return null;
|
|
231
|
+
const sv = json("the current state version", r);
|
|
232
|
+
const url = sv.data?.attributes?.["hosted-state-download-url"];
|
|
233
|
+
if (!sv.data?.id || typeof url !== "string")
|
|
234
|
+
throw new StateSourceError(`${location}: the current state version names no download URL`);
|
|
235
|
+
const got = await call(new URL(url, `${origin}/`).toString());
|
|
236
|
+
if (got.status >= 300)
|
|
237
|
+
throw new StateSourceError(`${location}: the download of ${sv.data.id} answered ${got.status}`);
|
|
238
|
+
return { text: got.text, version: sv.data.id };
|
|
239
|
+
},
|
|
240
|
+
lock: async (reason) => {
|
|
241
|
+
const b = await api();
|
|
242
|
+
const ws = await workspace();
|
|
243
|
+
const r = await call(`${b}workspaces/${ws}/actions/lock`, "POST", JSON.stringify({ reason }));
|
|
244
|
+
if (r.status === 409)
|
|
245
|
+
throw new StateSourceError(`its workspace ${location} is locked, so nothing was written`);
|
|
246
|
+
json("the workspace lock", r);
|
|
247
|
+
return async () => {
|
|
248
|
+
const u = await call(`${b}workspaces/${ws}/actions/unlock`, "POST");
|
|
249
|
+
if (u.status >= 300)
|
|
250
|
+
throw new StateSourceError(`${location}: the unlock answered ${u.status}`);
|
|
251
|
+
};
|
|
252
|
+
},
|
|
253
|
+
};
|
|
254
|
+
}
|
package/dist/state/store.d.ts
CHANGED
|
@@ -1,5 +1,15 @@
|
|
|
1
|
-
import type {
|
|
1
|
+
import type { ObjectVersion } from "@intentius/chant/object-store";
|
|
2
|
+
import type { AzureState, LockableState } from "./backend.js";
|
|
2
3
|
import { type StoreFetch } from "./object-store.js";
|
|
4
|
+
/** The version of the state a store holds now. */
|
|
5
|
+
export interface StoreVersion {
|
|
6
|
+
/** Whether there is a state at all. */
|
|
7
|
+
exists: boolean;
|
|
8
|
+
/** Its id, when the store keeps versions and can give this one back later. */
|
|
9
|
+
versionId?: string;
|
|
10
|
+
/** Why the store keeps no version of it, when it keeps none. */
|
|
11
|
+
off?: string;
|
|
12
|
+
}
|
|
3
13
|
/** The lock a state's backend holds: the ID `force-unlock` takes, and the lock info the binary (or a run) wrote. */
|
|
4
14
|
export interface LockHolder {
|
|
5
15
|
id: string;
|
|
@@ -9,7 +19,7 @@ export interface LockHolder {
|
|
|
9
19
|
/** A state where it is, for what locks it and keeps beside it. */
|
|
10
20
|
export interface StateStore {
|
|
11
21
|
/** The backend's type; `http` is a GitLab-managed state (./gitlab.ts). */
|
|
12
|
-
readonly backend: LockableState["backend"]
|
|
22
|
+
readonly backend: LockableState["backend"];
|
|
13
23
|
/** Where the state is. */
|
|
14
24
|
readonly location: string;
|
|
15
25
|
/** Where its lock is: the lock object, or the blob whose lease is the lock. */
|
|
@@ -42,11 +52,38 @@ export interface StateStore {
|
|
|
42
52
|
* (azurerm) is taken over by its ID, the others need nothing.
|
|
43
53
|
*/
|
|
44
54
|
adopt(held: LockHolder): void;
|
|
55
|
+
/**
|
|
56
|
+
* The version the store holds now (#4793). With `pin`, an azurerm blob
|
|
57
|
+
* that keeps snapshots and no versions gets a snapshot, whose id names it;
|
|
58
|
+
* without, it names none. The other stores read the version they keep.
|
|
59
|
+
*/
|
|
60
|
+
version(pin?: boolean): Promise<StoreVersion>;
|
|
61
|
+
/** Whether the store holds the version `id` names, without reading it. */
|
|
62
|
+
hasVersion(id: string): Promise<boolean>;
|
|
63
|
+
/** That version's text, or undefined when the store holds no such version. */
|
|
64
|
+
readVersion(id: string): Promise<string | undefined>;
|
|
65
|
+
/** Whether the state is still the version `id` names. */
|
|
66
|
+
isAt(id: string): Promise<boolean>;
|
|
67
|
+
/**
|
|
68
|
+
* Every version the store keeps, newest first: ids and times, never a
|
|
69
|
+
* state's contents. Undefined for a store that lists none: a local file,
|
|
70
|
+
* and GitLab's state API, which has no list of a state's versions.
|
|
71
|
+
*/
|
|
72
|
+
listVersions?: () => Promise<ObjectVersion[]>;
|
|
45
73
|
}
|
|
46
74
|
/** The ID in a lock info's JSON, or "" when it names none. */
|
|
47
75
|
export declare function lockId(text: string): string;
|
|
48
76
|
/** The metadata an azurerm backend keeps a lock's info in, base64 JSON, on the leased blob. */
|
|
49
77
|
export declare const LOCK_METADATA = "terraformlockid";
|
|
50
|
-
/**
|
|
51
|
-
|
|
78
|
+
/**
|
|
79
|
+
* The account's blob endpoint: the state's, or `https://<account>.blob.<suffix>`
|
|
80
|
+
* with the storage suffix the cloud's metadata at `metadata_host` names (#4793).
|
|
81
|
+
*/
|
|
82
|
+
export declare function azureEndpoint(o: AzureState, fetchFn?: StoreFetch): Promise<string>;
|
|
83
|
+
/**
|
|
84
|
+
* The store a state is in, opened with the credentials stateObject chose.
|
|
85
|
+
* `dir` is the root a local path is relative to. A GitLab-managed state's
|
|
86
|
+
* store names `who` in the lock info of its probe (./gitlab.ts).
|
|
87
|
+
*/
|
|
88
|
+
export declare function stateStore(o: LockableState, dir: string, fetchFn?: StoreFetch, who?: string): StateStore;
|
|
52
89
|
//# sourceMappingURL=store.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"store.d.ts","sourceRoot":"","sources":["../../src/state/store.ts"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"store.d.ts","sourceRoot":"","sources":["../../src/state/store.ts"],"names":[],"mappings":"AAsCA,OAAO,KAAK,EAAE,aAAa,EAAE,MAAM,+BAA+B,CAAC;AAEnE,OAAO,KAAK,EAAE,UAAU,EAAwB,aAAa,EAAW,MAAM,WAAW,CAAC;AAG1F,OAAO,EAA4B,KAAK,UAAU,EAAE,MAAM,gBAAgB,CAAC;AAG3E,kDAAkD;AAClD,MAAM,WAAW,YAAY;IAC3B,uCAAuC;IACvC,MAAM,EAAE,OAAO,CAAC;IAChB,8EAA8E;IAC9E,SAAS,CAAC,EAAE,MAAM,CAAC;IACnB,gEAAgE;IAChE,GAAG,CAAC,EAAE,MAAM,CAAC;CACd;AAED,oHAAoH;AACpH,MAAM,WAAW,UAAU;IACzB,EAAE,EAAE,MAAM,CAAC;IACX,sCA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