@intentius/behold 0.29.50 → 0.29.52
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/AGENTS.md +52 -0
- package/demos.json +15 -0
- package/dist/cli.js +1311 -622
- package/example-three-cloud/README.md +38 -0
- package/example-three-cloud/aws/chant.config.ts +8 -0
- package/example-three-cloud/aws/src/site.ts +20 -0
- package/example-three-cloud/azure/chant.config.ts +1 -0
- package/example-three-cloud/azure/src/site.ts +55 -0
- package/example-three-cloud/chant.workspace.json +10 -0
- package/example-three-cloud/dns/chant.config.ts +1 -0
- package/example-three-cloud/dns/ops/front-door.op.ts +21 -0
- package/example-three-cloud/dns/src/route53.ts +62 -0
- package/example-three-cloud/gcp/chant.config.ts +1 -0
- package/example-three-cloud/gcp/src/site.ts +86 -0
- package/example-three-cloud/package-lock.json +715 -0
- package/example-three-cloud/package.json +16 -0
- package/example-three-cloud/scripts/emulators-up.sh +46 -0
- package/example-three-cloud/scripts/front-door.ts +411 -0
- package/package.json +3 -2
- package/web/app.js +132 -6
- package/web/composite-members.js +21 -0
- package/web/composite-members.test.js +25 -0
- package/web/export-note.js +35 -0
- package/web/export-note.test.js +45 -0
- package/web/health-marks.js +146 -0
- package/web/health-marks.test.js +67 -0
- package/web/index.html +7 -0
- package/web/view-url.js +25 -0
- package/web/view-url.test.js +24 -1
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "behold-example-three-cloud",
|
|
3
|
+
"private": true,
|
|
4
|
+
"type": "module",
|
|
5
|
+
"description": "A website in three clouds behind Route 53, as a chant workspace on the floci emulators: behold's three-cloud demo and its exported page.",
|
|
6
|
+
"scripts": {
|
|
7
|
+
"emulators": "bash scripts/emulators-up.sh",
|
|
8
|
+
"front-door": "tsx scripts/front-door.ts all --out export"
|
|
9
|
+
},
|
|
10
|
+
"dependencies": {
|
|
11
|
+
"@intentius/chant": "^0.124.35",
|
|
12
|
+
"@intentius/chant-lexicon-aws": "^0.124.35",
|
|
13
|
+
"@intentius/chant-lexicon-azure": "^0.124.35",
|
|
14
|
+
"@intentius/chant-lexicon-gcp": "^0.124.35"
|
|
15
|
+
}
|
|
16
|
+
}
|
|
@@ -0,0 +1,46 @@
|
|
|
1
|
+
#!/usr/bin/env bash
|
|
2
|
+
# Boot the three emulators the three-cloud demo runs on: floci (AWS),
|
|
3
|
+
# floci-az (Azure) and floci-gcp (GCP). `behold demo three-cloud` runs this as
|
|
4
|
+
# its setup, then serves the workspace with --local, which finds them running
|
|
5
|
+
# under chant's own container names and reuses them.
|
|
6
|
+
#
|
|
7
|
+
# chant's `chant emulator up` would boot its pinned images, without the Docker
|
|
8
|
+
# socket the emulators need to run the website's containers. So the images
|
|
9
|
+
# here are the builds that probe Route 53 health checks (lex00/floci#259) and
|
|
10
|
+
# serve Container Apps and Cloud Run from real containers, each given the
|
|
11
|
+
# socket, and floci-az is told to run Container Apps for real rather than mock
|
|
12
|
+
# them. A container that is already running is left as it is.
|
|
13
|
+
#
|
|
14
|
+
# Names, ports and images can be overridden, so a second copy can run beside
|
|
15
|
+
# the first: FLOCI_CONTAINER, FLOCI_AZ_CONTAINER, FLOCI_GCP_CONTAINER,
|
|
16
|
+
# FLOCI_PORT, FLOCI_AZ_PORT, FLOCI_GCP_PORT, FLOCI_IMAGE, FLOCI_AZ_IMAGE,
|
|
17
|
+
# FLOCI_GCP_IMAGE.
|
|
18
|
+
set -euo pipefail
|
|
19
|
+
|
|
20
|
+
boot() {
|
|
21
|
+
local name=$1 port=$2 inner=$3 image=$4 health=$5; shift 5
|
|
22
|
+
if [ -n "$(docker ps -q -f "name=^${name}\$")" ]; then
|
|
23
|
+
echo "three-cloud: ${name} already running"
|
|
24
|
+
else
|
|
25
|
+
docker rm -f "$name" >/dev/null 2>&1 || true
|
|
26
|
+
echo "three-cloud: starting ${name} (${image}) on :${port}"
|
|
27
|
+
docker run -d --name "$name" -p "${port}:${inner}" -v /var/run/docker.sock:/var/run/docker.sock "$@" "$image" >/dev/null
|
|
28
|
+
fi
|
|
29
|
+
for _ in $(seq 1 60); do
|
|
30
|
+
if curl -fsS "http://localhost:${port}${health}" >/dev/null 2>&1; then
|
|
31
|
+
echo "three-cloud: ${name} ready on http://localhost:${port}"
|
|
32
|
+
return 0
|
|
33
|
+
fi
|
|
34
|
+
sleep 2
|
|
35
|
+
done
|
|
36
|
+
echo "three-cloud: ${name} did not answer on :${port}${health} within two minutes" >&2
|
|
37
|
+
return 1
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
boot "${FLOCI_CONTAINER:-chant-floci}" "${FLOCI_PORT:-4566}" 4566 \
|
|
41
|
+
"${FLOCI_IMAGE:-ghcr.io/lex00/floci:48c6e47}" /_floci/health
|
|
42
|
+
boot "${FLOCI_AZ_CONTAINER:-chant-floci-az}" "${FLOCI_AZ_PORT:-4577}" 4577 \
|
|
43
|
+
"${FLOCI_AZ_IMAGE:-ghcr.io/lex00/floci-az:7f079c2}" /_floci/health \
|
|
44
|
+
-e FLOCI_AZ_SERVICES_CONTAINER_APPS_MOCKED=false
|
|
45
|
+
boot "${FLOCI_GCP_CONTAINER:-chant-floci-gcp}" "${FLOCI_GCP_PORT:-4588}" 4588 \
|
|
46
|
+
"${FLOCI_GCP_IMAGE:-ghcr.io/lex00/floci-gcp:641e6e8}" /_floci-gcp/health
|
|
@@ -0,0 +1,411 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The front door, rebuilt on the floci emulators: apply the four members,
|
|
3
|
+
* turn Azure off, wait for Route 53 to notice, and export the page.
|
|
4
|
+
*
|
|
5
|
+
* tsx scripts/front-door.ts all --out <dir> every step below, in order
|
|
6
|
+
* tsx scripts/front-door.ts apply build and apply gcp, azure, aws, then dns, and wait
|
|
7
|
+
* until `chant lifecycle health` reads azureCheck healthy
|
|
8
|
+
* tsx scripts/front-door.ts azure-off deactivate the Azure app's active revision
|
|
9
|
+
* tsx scripts/front-door.ts wait until azureCheck reads unhealthy
|
|
10
|
+
* tsx scripts/front-door.ts export --out <dir> behold export, opening on env local at the logical zoom
|
|
11
|
+
*
|
|
12
|
+
* dns/ops/front-door.op.ts runs the same steps as an Op's phases. The emulators
|
|
13
|
+
* are the ones `behold demo three-cloud` boots (chant-floci, chant-floci-az,
|
|
14
|
+
* chant-floci-gcp); FLOCI_CONTAINER, FLOCI_AZ_CONTAINER and FLOCI_GCP_CONTAINER
|
|
15
|
+
* name others, and AWS_ENDPOINT_URL, AZURE_ENDPOINT_URL and GCP_ENDPOINT_URL
|
|
16
|
+
* say where they answer.
|
|
17
|
+
*/
|
|
18
|
+
import { execFileSync, spawnSync } from "node:child_process";
|
|
19
|
+
import { mkdirSync, readFileSync, writeFileSync } from "node:fs";
|
|
20
|
+
import { dirname, join, resolve } from "node:path";
|
|
21
|
+
import { fileURLToPath } from "node:url";
|
|
22
|
+
import { awsApply, awsDelete } from "@intentius/chant-lexicon-aws/op/activities";
|
|
23
|
+
import { azApply } from "@intentius/chant-lexicon-azure/op/activities";
|
|
24
|
+
import { gcpApply } from "@intentius/chant-lexicon-gcp/op/activities";
|
|
25
|
+
|
|
26
|
+
const ROOT = resolve(dirname(fileURLToPath(import.meta.url)), "..");
|
|
27
|
+
const WORK = join(ROOT, ".front-door");
|
|
28
|
+
const CHANT = join(ROOT, "node_modules", ".bin", "chant");
|
|
29
|
+
|
|
30
|
+
const ENV = "local";
|
|
31
|
+
// The Azure resource group and the GCP project, as chant's live reads find them:
|
|
32
|
+
// the resource group is named after the environment.
|
|
33
|
+
const RESOURCE_GROUP = ENV;
|
|
34
|
+
const GCP_PROJECT = process.env.GCP_PROJECT_ID ?? "three-cloud";
|
|
35
|
+
const GCP_REGION = process.env.GCP_REGION ?? "asia-northeast1";
|
|
36
|
+
const SUBSCRIPTION = process.env.AZURE_SUBSCRIPTION_ID ?? "00000000-0000-0000-0000-000000000001";
|
|
37
|
+
|
|
38
|
+
const AWS = process.env.AWS_ENDPOINT_URL ?? "http://localhost:4566";
|
|
39
|
+
const AZURE = process.env.AZURE_ENDPOINT_URL ?? "http://localhost:4577";
|
|
40
|
+
const GCP = process.env.GCP_ENDPOINT_URL ?? "http://localhost:4588";
|
|
41
|
+
const CONTAINERS = {
|
|
42
|
+
aws: process.env.FLOCI_CONTAINER ?? "chant-floci",
|
|
43
|
+
azure: process.env.FLOCI_AZ_CONTAINER ?? "chant-floci-az",
|
|
44
|
+
gcp: process.env.FLOCI_GCP_CONTAINER ?? "chant-floci-gcp",
|
|
45
|
+
};
|
|
46
|
+
|
|
47
|
+
/** Everything a chant or behold child needs to reach the emulators. */
|
|
48
|
+
const childEnv: NodeJS.ProcessEnv = {
|
|
49
|
+
...process.env,
|
|
50
|
+
AWS_ENDPOINT_URL: AWS,
|
|
51
|
+
AWS_ACCESS_KEY_ID: process.env.AWS_ACCESS_KEY_ID ?? "test",
|
|
52
|
+
AWS_SECRET_ACCESS_KEY: process.env.AWS_SECRET_ACCESS_KEY ?? "test",
|
|
53
|
+
AWS_REGION: process.env.AWS_REGION ?? "us-east-1",
|
|
54
|
+
AZURE_ENDPOINT_URL: AZURE,
|
|
55
|
+
AZURE_SUBSCRIPTION_ID: SUBSCRIPTION,
|
|
56
|
+
GCP_ENDPOINT_URL: GCP,
|
|
57
|
+
GCP_PROJECT_ID: GCP_PROJECT,
|
|
58
|
+
GOOGLE_CLOUD_PROJECT: GCP_PROJECT,
|
|
59
|
+
GCP_REGION,
|
|
60
|
+
};
|
|
61
|
+
|
|
62
|
+
const say = (line: string): void => void process.stdout.write(`front-door: ${line}\n`);
|
|
63
|
+
|
|
64
|
+
class Refusal extends Error {}
|
|
65
|
+
|
|
66
|
+
// ---------------------------------------------------------------------------
|
|
67
|
+
// Build
|
|
68
|
+
|
|
69
|
+
function build(member: string): string {
|
|
70
|
+
const out = join(WORK, member === "gcp" ? "gcp.yaml" : `${member}.json`);
|
|
71
|
+
mkdirSync(WORK, { recursive: true });
|
|
72
|
+
const r = spawnSync(CHANT, ["build", "src", "-o", out], { cwd: join(ROOT, member), env: childEnv, encoding: "utf8" });
|
|
73
|
+
if (r.status !== 0) throw new Refusal(`chant build failed in ${member}:\n${r.stderr || r.stdout}`);
|
|
74
|
+
return out;
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
/** A copy of a CloudFormation template whose parameters default to `values`,
|
|
78
|
+
* so `awsApply` (which passes no parameters) creates the stack with them. */
|
|
79
|
+
function withDefaults(template: string, values: Record<string, string>): string {
|
|
80
|
+
const doc = JSON.parse(readFileSync(template, "utf8")) as { Parameters?: Record<string, Record<string, unknown>> };
|
|
81
|
+
for (const [name, value] of Object.entries(values)) {
|
|
82
|
+
const p = doc.Parameters?.[name];
|
|
83
|
+
if (!p) throw new Refusal(`${template} declares no parameter ${name}`);
|
|
84
|
+
p.Default = value;
|
|
85
|
+
}
|
|
86
|
+
const out = template.replace(/\.json$/, ".params.json");
|
|
87
|
+
writeFileSync(out, JSON.stringify(doc, null, 2));
|
|
88
|
+
return out;
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
// ---------------------------------------------------------------------------
|
|
92
|
+
// Where the checkers connect
|
|
93
|
+
|
|
94
|
+
/** An emulator container's address on Docker's network, which floci's
|
|
95
|
+
* checkers (in their own container) can reach. */
|
|
96
|
+
function containerAddress(name: string): string {
|
|
97
|
+
let ip = "";
|
|
98
|
+
try {
|
|
99
|
+
ip = execFileSync("docker", ["inspect", "-f", "{{range .NetworkSettings.Networks}}{{.IPAddress}} {{end}}", name], { encoding: "utf8" }).trim().split(/\s+/)[0] ?? "";
|
|
100
|
+
} catch {
|
|
101
|
+
throw new Refusal(`no container named ${name}: boot the emulators first (behold demo three-cloud does)`);
|
|
102
|
+
}
|
|
103
|
+
if (!/^\d+\.\d+\.\d+\.\d+$/.test(ip)) throw new Refusal(`container ${name} has no IPv4 address on a Docker network`);
|
|
104
|
+
return ip;
|
|
105
|
+
}
|
|
106
|
+
|
|
107
|
+
// ---------------------------------------------------------------------------
|
|
108
|
+
// Apply
|
|
109
|
+
|
|
110
|
+
async function applyGcp(): Promise<string> {
|
|
111
|
+
const manifest = build("gcp");
|
|
112
|
+
say(`gcp: applying ${manifest} to ${GCP} (project ${GCP_PROJECT})`);
|
|
113
|
+
await gcpApply({ manifestPath: manifest, endpoint: GCP, project: GCP_PROJECT });
|
|
114
|
+
// Config Connector YAML has no outputs, so the build's `gcpEndpoint` output
|
|
115
|
+
// is not written (chant warns). The URI is read from the service instead.
|
|
116
|
+
// The service's location is the manifest's, or us-central1 where gcpApply
|
|
117
|
+
// puts a service that names none.
|
|
118
|
+
// The RunService's own location: the load balancer's kinds say `global`.
|
|
119
|
+
const location = /kind:\s*RunService[\s\S]*?^\s+location:\s*['"]?([a-z0-9-]+)/m.exec(readFileSync(manifest, "utf8"))?.[1] ?? "us-central1";
|
|
120
|
+
const svc = (await getJson(`${GCP}/v2/projects/${GCP_PROJECT}/locations/${location}/services/three-cloud-site`)) as { uri?: string };
|
|
121
|
+
if (!svc.uri) throw new Refusal(`gcp: the Cloud Run service three-cloud-site has no uri yet (${JSON.stringify(svc).slice(0, 300)})`);
|
|
122
|
+
say(`gcp: Cloud Run answers as ${new URL(svc.uri).hostname}`);
|
|
123
|
+
// Route 53 points at the load balancer's address. A chant older than
|
|
124
|
+
// 0.124.35 skips the load balancer kinds ("no mapper"), and then there is
|
|
125
|
+
// no address: the record gets one nothing answers on (192.0.2.1, kept for
|
|
126
|
+
// documentation by RFC 5737), and the page shows GCP failing its check.
|
|
127
|
+
const addr = (await fetch(`${GCP}/compute/v1/projects/${GCP_PROJECT}/global/addresses/three-cloud-ip`).then((r) => (r.ok ? r.json() : {}))) as { address?: string };
|
|
128
|
+
if (!addr.address) {
|
|
129
|
+
say("gcp: the load balancer was NOT APPLIED (it needs chant 0.124.35 or newer), so the gcp record has nothing behind it");
|
|
130
|
+
return "192.0.2.1";
|
|
131
|
+
}
|
|
132
|
+
return addr.address;
|
|
133
|
+
}
|
|
134
|
+
|
|
135
|
+
async function applyAzure(): Promise<string> {
|
|
136
|
+
const template = build("azure");
|
|
137
|
+
say(`azure: applying ${template} to ${AZURE} (resource group ${RESOURCE_GROUP})`);
|
|
138
|
+
await azApply({ templatePath: template, resourceGroup: RESOURCE_GROUP, location: "westeurope", endpoint: AZURE, subscriptionId: SUBSCRIPTION });
|
|
139
|
+
const app = await getJson(`${appUrl()}?api-version=2024-03-01`);
|
|
140
|
+
const fqdn = (app.properties as { configuration?: { ingress?: { fqdn?: string } } } | undefined)?.configuration?.ingress?.fqdn;
|
|
141
|
+
if (!fqdn) throw new Refusal(`azure: the container app web has no ingress fqdn (${JSON.stringify(app).slice(0, 300)})`);
|
|
142
|
+
await azureOn();
|
|
143
|
+
return fqdn;
|
|
144
|
+
}
|
|
145
|
+
|
|
146
|
+
/** A run before this one turned Azure off, and applying the same app again
|
|
147
|
+
* leaves its revision inactive: turn the newest one back on. */
|
|
148
|
+
async function azureOn(): Promise<void> {
|
|
149
|
+
const list = await getJson(`${appUrl()}/revisions?api-version=2024-03-01`);
|
|
150
|
+
const revisions = (list.value ?? []) as Array<{ name: string; properties?: { active?: boolean; createdTime?: string } }>;
|
|
151
|
+
if (!revisions.length || revisions.some((r) => r.properties?.active !== false)) return;
|
|
152
|
+
const newest = [...revisions].sort((a, b) => (a.properties?.createdTime ?? a.name).localeCompare(b.properties?.createdTime ?? b.name)).at(-1)!;
|
|
153
|
+
const res = await fetch(`${appUrl()}/revisions/${newest.name}/activate?api-version=2024-03-01`, { method: "POST" });
|
|
154
|
+
if (!res.ok) throw new Refusal(`azure: activating revision ${newest.name} answered ${res.status}: ${await res.text()}`);
|
|
155
|
+
say(`azure: revision ${newest.name} activated again`);
|
|
156
|
+
}
|
|
157
|
+
|
|
158
|
+
/** The aws member, or why it could not be applied. */
|
|
159
|
+
async function applyAws(): Promise<{ endpoint: string } | { refused: string }> {
|
|
160
|
+
const template = build("aws");
|
|
161
|
+
const subnets = await defaultSubnets();
|
|
162
|
+
const params = withDefaults(template, { vpcId: subnets.vpc, subnetIds: subnets.ids.join(",") });
|
|
163
|
+
// A stack that rolled back on its first create can't be updated; start it again.
|
|
164
|
+
if ((await stackStatus("three-cloud-aws")) === "ROLLBACK_COMPLETE") {
|
|
165
|
+
await awsDelete({ templatePath: params, stackName: "three-cloud-aws", endpoint: AWS, timeoutMs: 180_000 });
|
|
166
|
+
}
|
|
167
|
+
say(`aws: applying ${params} to ${AWS} as stack three-cloud-aws`);
|
|
168
|
+
try {
|
|
169
|
+
await awsApply({ templatePath: params, stackName: "three-cloud-aws", endpoint: AWS, timeoutMs: 180_000 });
|
|
170
|
+
} catch (e) {
|
|
171
|
+
const why = await stackFailure("three-cloud-aws");
|
|
172
|
+
return { refused: `${e instanceof Error ? e.message : String(e)}${why ? `: ${why}` : ""}` };
|
|
173
|
+
}
|
|
174
|
+
const out = await stackOutputs("three-cloud-aws");
|
|
175
|
+
return out.awsEndpoint ? { endpoint: out.awsEndpoint } : { refused: "the stack has no awsEndpoint output" };
|
|
176
|
+
}
|
|
177
|
+
|
|
178
|
+
/** Where floci's checkers connect for one cloud's check, as Route 53 does
|
|
179
|
+
* for a check with an IPAddress: to the address and port given, sending the
|
|
180
|
+
* endpoint as the Host header. */
|
|
181
|
+
interface Probe {
|
|
182
|
+
address: string;
|
|
183
|
+
port: number;
|
|
184
|
+
}
|
|
185
|
+
|
|
186
|
+
/**
|
|
187
|
+
* A copy of the dns template whose checks connect to the emulators. The
|
|
188
|
+
* source declares checks as they are on AWS, where the checkers resolve each
|
|
189
|
+
* endpoint; on the emulators nothing outside Docker resolves a floci-az or
|
|
190
|
+
* floci-gcp hostname, so each check is given the emulator container's address
|
|
191
|
+
* and port (lex00/floci#259). This is the one place the deployed dns stack
|
|
192
|
+
* differs from its source, and behold shows those two fields as drift.
|
|
193
|
+
*/
|
|
194
|
+
function withProbes(template: string, probes: Record<string, Probe>): void {
|
|
195
|
+
const doc = JSON.parse(readFileSync(template, "utf8")) as { Resources: Record<string, { Properties: { HealthCheckConfig: Record<string, unknown> } }> };
|
|
196
|
+
for (const [check, probe] of Object.entries(probes)) {
|
|
197
|
+
const cfg = doc.Resources[check]?.Properties?.HealthCheckConfig;
|
|
198
|
+
if (!cfg) throw new Refusal(`${template} declares no health check ${check}`);
|
|
199
|
+
cfg.IPAddress = probe.address;
|
|
200
|
+
cfg.Port = probe.port;
|
|
201
|
+
}
|
|
202
|
+
writeFileSync(template, JSON.stringify(doc, null, 2));
|
|
203
|
+
}
|
|
204
|
+
|
|
205
|
+
async function applyDns(endpoints: Record<string, string>, probes: Record<string, Probe>): Promise<void> {
|
|
206
|
+
const params = withDefaults(build("dns"), endpoints);
|
|
207
|
+
withProbes(params, probes);
|
|
208
|
+
// The stack named after the environment, where chant's live read looks for a single-stack project.
|
|
209
|
+
say(`dns: applying ${params} to ${AWS} as stack ${ENV}`);
|
|
210
|
+
await awsApply({ templatePath: params, stackName: ENV, endpoint: AWS, timeoutMs: 180_000 });
|
|
211
|
+
}
|
|
212
|
+
|
|
213
|
+
export async function apply(): Promise<void> {
|
|
214
|
+
const gcp = await applyGcp();
|
|
215
|
+
say(`gcp: the load balancer answers on ${gcp}`);
|
|
216
|
+
const azure = await applyAzure();
|
|
217
|
+
say(`azure: web answers as ${azure}`);
|
|
218
|
+
const aws = await applyAws();
|
|
219
|
+
// An aws stack that doesn't apply leaves its record pointing at a name that
|
|
220
|
+
// doesn't resolve and its check probing an address nothing answers on
|
|
221
|
+
// (192.0.2.1, kept for documentation by RFC 5737), so the page shows AWS
|
|
222
|
+
// failing and the rest still runs.
|
|
223
|
+
if ("refused" in aws) say(`aws: NOT APPLIED, so the aws record has nothing behind it and its check fails: ${aws.refused}`);
|
|
224
|
+
else say(`aws: web answers as ${aws.endpoint}`);
|
|
225
|
+
await applyDns(
|
|
226
|
+
{
|
|
227
|
+
awsEndpoint: "endpoint" in aws ? aws.endpoint : "aws.three-cloud.invalid",
|
|
228
|
+
azureEndpoint: azure,
|
|
229
|
+
gcpEndpoint: gcp,
|
|
230
|
+
},
|
|
231
|
+
{
|
|
232
|
+
// floci runs a load balancer's listener on the listener's own port (80)
|
|
233
|
+
// in its container, not behind :4566.
|
|
234
|
+
awsCheck: { address: "endpoint" in aws ? containerAddress(CONTAINERS.aws) : "192.0.2.1", port: 80 },
|
|
235
|
+
// floci-az serves every app on its own port, by Host header.
|
|
236
|
+
azureCheck: { address: containerAddress(CONTAINERS.azure), port: 4577 },
|
|
237
|
+
// floci-gcp opens a forwarding rule's port (80) in its container.
|
|
238
|
+
gcpCheck: { address: gcp === "192.0.2.1" ? gcp : containerAddress(CONTAINERS.gcp), port: 80 },
|
|
239
|
+
},
|
|
240
|
+
);
|
|
241
|
+
say("dns: applied");
|
|
242
|
+
// Before anything is turned off, the check on Azure has to pass: that is
|
|
243
|
+
// what shows the checkers reach floci-az at all.
|
|
244
|
+
await waitFor("healthy");
|
|
245
|
+
}
|
|
246
|
+
|
|
247
|
+
// ---------------------------------------------------------------------------
|
|
248
|
+
// Turn Azure off
|
|
249
|
+
|
|
250
|
+
function appUrl(): string {
|
|
251
|
+
return `${AZURE}/subscriptions/${SUBSCRIPTION}/resourceGroups/${RESOURCE_GROUP}/providers/Microsoft.App/containerApps/web`;
|
|
252
|
+
}
|
|
253
|
+
|
|
254
|
+
export async function azureOff(): Promise<void> {
|
|
255
|
+
const list = await getJson(`${appUrl()}/revisions?api-version=2024-03-01`);
|
|
256
|
+
const active = ((list.value ?? []) as Array<{ name: string; properties?: { active?: boolean } }>).filter((r) => r.properties?.active !== false);
|
|
257
|
+
if (!active.length) {
|
|
258
|
+
say("azure: no active revision, already off");
|
|
259
|
+
return;
|
|
260
|
+
}
|
|
261
|
+
for (const r of active) {
|
|
262
|
+
const res = await fetch(`${appUrl()}/revisions/${r.name}/deactivate?api-version=2024-03-01`, { method: "POST" });
|
|
263
|
+
if (!res.ok) throw new Refusal(`azure: deactivating revision ${r.name} answered ${res.status}: ${await res.text()}`);
|
|
264
|
+
say(`azure: revision ${r.name} deactivated`);
|
|
265
|
+
}
|
|
266
|
+
}
|
|
267
|
+
|
|
268
|
+
// ---------------------------------------------------------------------------
|
|
269
|
+
// Wait for Route 53 to notice
|
|
270
|
+
|
|
271
|
+
/** A `chant-health/v1` document, as far as this script reads it. */
|
|
272
|
+
interface HealthDoc {
|
|
273
|
+
lexicons?: Record<string, { entities?: Record<string, { state?: string; detail?: string }> }>;
|
|
274
|
+
}
|
|
275
|
+
|
|
276
|
+
/** azureCheck's state and detail as `chant lifecycle health` reads them. */
|
|
277
|
+
function azureCheckHealth(): { state?: string; detail?: string } {
|
|
278
|
+
const r = spawnSync(CHANT, ["lifecycle", "health", ENV, "--json"], { cwd: join(ROOT, "dns"), env: childEnv, encoding: "utf8" });
|
|
279
|
+
let doc: HealthDoc;
|
|
280
|
+
try {
|
|
281
|
+
doc = JSON.parse(r.stdout) as HealthDoc;
|
|
282
|
+
} catch {
|
|
283
|
+
return { detail: (r.stderr || r.stdout).trim().split("\n")[0] };
|
|
284
|
+
}
|
|
285
|
+
return doc.lexicons?.aws?.entities?.azureCheck ?? {};
|
|
286
|
+
}
|
|
287
|
+
|
|
288
|
+
/** Until azureCheck reads `want`: healthy once the apply has settled (the
|
|
289
|
+
* checkers really reach floci-az), unhealthy once Azure is off. */
|
|
290
|
+
export async function waitFor(want: "healthy" | "unhealthy", timeoutMs = 180_000): Promise<void> {
|
|
291
|
+
const until = Date.now() + timeoutMs;
|
|
292
|
+
let last: { state?: string; detail?: string } = {};
|
|
293
|
+
while (Date.now() < until) {
|
|
294
|
+
last = azureCheckHealth();
|
|
295
|
+
if (last.state === want) {
|
|
296
|
+
say(`dns: azureCheck reads ${want} (${last.detail})${want === "unhealthy" ? "; the azure record is out of rotation" : ""}`);
|
|
297
|
+
return;
|
|
298
|
+
}
|
|
299
|
+
await new Promise((r) => setTimeout(r, 5000));
|
|
300
|
+
}
|
|
301
|
+
throw new Refusal(`dns: azureCheck still reads ${last.state ?? "nothing"}, not ${want}, after ${timeoutMs / 1000}s${last.detail ? ` (${last.detail})` : ""}`);
|
|
302
|
+
}
|
|
303
|
+
|
|
304
|
+
// ---------------------------------------------------------------------------
|
|
305
|
+
// Export
|
|
306
|
+
|
|
307
|
+
export const NOTE = "Run this yourself: `npx @intentius/behold demo three-cloud`";
|
|
308
|
+
|
|
309
|
+
export function exportPage(out: string): void {
|
|
310
|
+
// BEHOLD names the behold to run (a checkout's `tsx src/cli.ts`, say);
|
|
311
|
+
// by default it is the published one.
|
|
312
|
+
const behold = (process.env.BEHOLD ?? "npx --yes @intentius/behold").split(" ");
|
|
313
|
+
const args = [...behold.slice(1), "export", ROOT, "--out", resolve(out), "--open-env", ENV, "--open-zoom", "logical", "--note", NOTE];
|
|
314
|
+
say(`export: ${behold[0]} ${args.join(" ")}`);
|
|
315
|
+
const r = spawnSync(behold[0]!, args, { env: childEnv, stdio: "inherit" });
|
|
316
|
+
if (r.status !== 0) throw new Refusal(`behold export exited ${r.status}`);
|
|
317
|
+
}
|
|
318
|
+
|
|
319
|
+
// ---------------------------------------------------------------------------
|
|
320
|
+
// Small helpers
|
|
321
|
+
|
|
322
|
+
async function getJson(url: string): Promise<Record<string, unknown>> {
|
|
323
|
+
const res = await fetch(url);
|
|
324
|
+
const text = await res.text();
|
|
325
|
+
if (!res.ok) throw new Refusal(`GET ${url} answered ${res.status}: ${text.slice(0, 300)}`);
|
|
326
|
+
return JSON.parse(text) as Record<string, unknown>;
|
|
327
|
+
}
|
|
328
|
+
|
|
329
|
+
async function awsQuery(service: string, form: Record<string, string>): Promise<string> {
|
|
330
|
+
const res = await fetch(`${AWS}/`, {
|
|
331
|
+
method: "POST",
|
|
332
|
+
headers: {
|
|
333
|
+
"content-type": "application/x-www-form-urlencoded",
|
|
334
|
+
authorization: `AWS4-HMAC-SHA256 Credential=test/20260101/us-east-1/${service}/aws4_request, SignedHeaders=host, Signature=0`,
|
|
335
|
+
},
|
|
336
|
+
body: new URLSearchParams(form).toString(),
|
|
337
|
+
});
|
|
338
|
+
const text = await res.text();
|
|
339
|
+
if (!res.ok) throw new Refusal(`${service} ${form.Action} answered ${res.status}: ${text.slice(0, 300)}`);
|
|
340
|
+
return text;
|
|
341
|
+
}
|
|
342
|
+
|
|
343
|
+
async function defaultSubnets(): Promise<{ vpc: string; ids: string[] }> {
|
|
344
|
+
const vpcs = await awsQuery("ec2", { Action: "DescribeVpcs", Version: "2016-11-15", "Filter.1.Name": "isDefault", "Filter.1.Value.1": "true" });
|
|
345
|
+
const vpc = /<vpcId>([^<]+)<\/vpcId>/.exec(vpcs)?.[1];
|
|
346
|
+
if (!vpc) throw new Refusal("aws: the emulator has no default VPC");
|
|
347
|
+
const subnets = await awsQuery("ec2", { Action: "DescribeSubnets", Version: "2016-11-15", "Filter.1.Name": "vpc-id", "Filter.1.Value.1": vpc });
|
|
348
|
+
const ids = [...subnets.matchAll(/<subnetId>([^<]+)<\/subnetId>/g)].map((m) => m[1]!).slice(0, 2);
|
|
349
|
+
if (ids.length < 2) throw new Refusal(`aws: the default VPC ${vpc} has fewer than two subnets`);
|
|
350
|
+
return { vpc, ids };
|
|
351
|
+
}
|
|
352
|
+
|
|
353
|
+
async function stackStatus(stack: string): Promise<string | undefined> {
|
|
354
|
+
try {
|
|
355
|
+
const xml = await awsQuery("cloudformation", { Action: "DescribeStacks", Version: "2010-05-15", StackName: stack });
|
|
356
|
+
return /<StackStatus>([^<]+)<\/StackStatus>/.exec(xml)?.[1];
|
|
357
|
+
} catch {
|
|
358
|
+
return undefined;
|
|
359
|
+
}
|
|
360
|
+
}
|
|
361
|
+
|
|
362
|
+
/** The first resource-level reason a stack's events give for failing. */
|
|
363
|
+
async function stackFailure(stack: string): Promise<string | undefined> {
|
|
364
|
+
try {
|
|
365
|
+
const xml = await awsQuery("cloudformation", { Action: "DescribeStackEvents", Version: "2010-05-15", StackName: stack });
|
|
366
|
+
for (const event of xml.split("<member>")) {
|
|
367
|
+
const field = (tag: string) => new RegExp(`<${tag}>([^<]*)</${tag}>`).exec(event)?.[1];
|
|
368
|
+
const id = field("LogicalResourceId");
|
|
369
|
+
const reason = field("ResourceStatusReason");
|
|
370
|
+
if (id && id !== stack && field("ResourceStatus") === "CREATE_FAILED" && reason) {
|
|
371
|
+
return `${id}: ${reason.replace(/'/g, "'").replace(/"/g, '"')}`;
|
|
372
|
+
}
|
|
373
|
+
}
|
|
374
|
+
} catch {
|
|
375
|
+
// no events to read; the applier's own message stands
|
|
376
|
+
}
|
|
377
|
+
return undefined;
|
|
378
|
+
}
|
|
379
|
+
|
|
380
|
+
async function stackOutputs(stack: string): Promise<Record<string, string>> {
|
|
381
|
+
const xml = await awsQuery("cloudformation", { Action: "DescribeStacks", Version: "2010-05-15", StackName: stack });
|
|
382
|
+
const out: Record<string, string> = {};
|
|
383
|
+
for (const m of xml.matchAll(/<OutputKey>([^<]+)<\/OutputKey>\s*<OutputValue>([^<]*)<\/OutputValue>/g)) out[m[1]!] = m[2]!;
|
|
384
|
+
return out;
|
|
385
|
+
}
|
|
386
|
+
|
|
387
|
+
// ---------------------------------------------------------------------------
|
|
388
|
+
|
|
389
|
+
async function main(argv: string[]): Promise<void> {
|
|
390
|
+
const [step, ...rest] = argv;
|
|
391
|
+
const outAt = rest.indexOf("--out");
|
|
392
|
+
const out = outAt >= 0 ? rest[outAt + 1] : undefined;
|
|
393
|
+
if (step === "apply") return apply();
|
|
394
|
+
if (step === "azure-off") return azureOff();
|
|
395
|
+
if (step === "wait") return waitFor("unhealthy");
|
|
396
|
+
if (step === "export" || step === "all") {
|
|
397
|
+
if (!out) throw new Refusal(`${step} needs --out <dir>`);
|
|
398
|
+
if (step === "all") {
|
|
399
|
+
await apply();
|
|
400
|
+
await azureOff();
|
|
401
|
+
await waitFor("unhealthy");
|
|
402
|
+
}
|
|
403
|
+
return exportPage(out);
|
|
404
|
+
}
|
|
405
|
+
throw new Refusal("usage: front-door.ts all|apply|azure-off|wait|export [--out <dir>]");
|
|
406
|
+
}
|
|
407
|
+
|
|
408
|
+
main(process.argv.slice(2)).catch((e: unknown) => {
|
|
409
|
+
process.stderr.write(`front-door: ${e instanceof Error ? e.message : String(e)}\n`);
|
|
410
|
+
process.exit(e instanceof Refusal ? 2 : 1);
|
|
411
|
+
});
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@intentius/behold",
|
|
3
|
-
"version": "0.29.
|
|
3
|
+
"version": "0.29.52",
|
|
4
4
|
"license": "Apache-2.0",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"description": "behold — a live control plane on chant. See your whole estate (every substrate in one graph), coloured by drift; act through delegated, gated Ops.",
|
|
@@ -19,6 +19,7 @@
|
|
|
19
19
|
"example-terraform-estate",
|
|
20
20
|
"example-choudoufu-estate",
|
|
21
21
|
"example-terralith",
|
|
22
|
+
"example-three-cloud",
|
|
22
23
|
"demos.json",
|
|
23
24
|
"AGENTS.md",
|
|
24
25
|
"LICENSE",
|
|
@@ -46,7 +47,7 @@
|
|
|
46
47
|
},
|
|
47
48
|
"dependencies": {
|
|
48
49
|
"@hono/node-server": "^2.0.5",
|
|
49
|
-
"@intentius/chant": "^0.124.
|
|
50
|
+
"@intentius/chant": "^0.124.35",
|
|
50
51
|
"@intentius/chant-k8s-client": "^0.124.24",
|
|
51
52
|
"@intentius/pinhole": "^0.5.0",
|
|
52
53
|
"ajv": "^8.20.0",
|