@geekmidas/cli 10.0.0-alpha.5 → 10.0.0-alpha.50
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/README.md +196 -257
- package/bin/adjacent-tsconfig.mjs +97 -0
- package/bin/gkm.mjs +16 -2
- package/bin/owning-tsconfig-jsx-register.mjs +18 -0
- package/bin/owning-tsconfig-jsx.mjs +108 -0
- package/dist/{HostingerProvider-h3EbEp-Z.cjs → HostingerProvider-BZFGYYVA.cjs} +21 -22
- package/dist/HostingerProvider-BZFGYYVA.cjs.map +1 -0
- package/dist/{HostingerProvider-Cft7lEYr.mjs → HostingerProvider-GF6NAZAK.mjs} +20 -21
- package/dist/HostingerProvider-GF6NAZAK.mjs.map +1 -0
- package/dist/{LocalStateProvider-Db9SEjyH.cjs → LocalStateProvider-7HR6SS2t.cjs} +11 -6
- package/dist/LocalStateProvider-7HR6SS2t.cjs.map +1 -0
- package/dist/{LocalStateProvider-Bn6tS-oL.mjs → LocalStateProvider-D7QyJoLM.mjs} +10 -4
- package/dist/LocalStateProvider-D7QyJoLM.mjs.map +1 -0
- package/dist/{openapi-CA0yJUTN.cjs → OpenApiTsGenerator-BN0PIZxd.cjs} +157 -372
- package/dist/OpenApiTsGenerator-BN0PIZxd.cjs.map +1 -0
- package/dist/{openapi-kkQbsRVj.mjs → OpenApiTsGenerator-BzIGy8KE.mjs} +128 -324
- package/dist/OpenApiTsGenerator-BzIGy8KE.mjs.map +1 -0
- package/dist/{Route53Provider-RdpUDJQk.mjs → Route53Provider-Cvj40A8I.mjs} +12 -12
- package/dist/Route53Provider-Cvj40A8I.mjs.map +1 -0
- package/dist/{Route53Provider-di2fMM5N.cjs → Route53Provider-DVILBsvV.cjs} +20 -21
- package/dist/Route53Provider-DVILBsvV.cjs.map +1 -0
- package/dist/{SSMStateProvider-BRt2py-A.mjs → SSMStateProvider-B0Hu1MKZ.mjs} +13 -4
- package/dist/SSMStateProvider-B0Hu1MKZ.mjs.map +1 -0
- package/dist/{SSMStateProvider-DyX8s9HI.cjs → SSMStateProvider-VrQ-yeqd.cjs} +17 -9
- package/dist/SSMStateProvider-VrQ-yeqd.cjs.map +1 -0
- package/dist/aws-DkHdTV-p.cjs +61 -0
- package/dist/aws-DkHdTV-p.cjs.map +1 -0
- package/dist/aws-DojzQnLd.mjs +61 -0
- package/dist/aws-DojzQnLd.mjs.map +1 -0
- package/dist/backends-B9V2XD2M.cjs +82 -0
- package/dist/backends-B9V2XD2M.cjs.map +1 -0
- package/dist/backends-CSuLl0YP.mjs +59 -0
- package/dist/backends-CSuLl0YP.mjs.map +1 -0
- package/dist/{backup-provisioner-BrsHwYG0.cjs → backup-provisioner-BmhIO_Yr.cjs} +21 -19
- package/dist/backup-provisioner-BmhIO_Yr.cjs.map +1 -0
- package/dist/{backup-provisioner-Bgj5Vi_L.mjs → backup-provisioner-DIVCbfee.mjs} +10 -7
- package/dist/backup-provisioner-DIVCbfee.mjs.map +1 -0
- package/dist/{bundler-Be8Zg4Wd.mjs → bundler--fyybf1N.mjs} +19 -30
- package/dist/bundler--fyybf1N.mjs.map +1 -0
- package/dist/{bundler-BY1OmV1e.cjs → bundler-By6_sXiL.cjs} +20 -32
- package/dist/bundler-By6_sXiL.cjs.map +1 -0
- package/dist/{config-C21ocYNd.cjs → config-CsYSyUya.cjs} +209 -210
- package/dist/config-CsYSyUya.cjs.map +1 -0
- package/dist/{config-WRdsQYZK.mjs → config-D8J5rrTb.mjs} +142 -154
- package/dist/config-D8J5rrTb.mjs.map +1 -0
- package/dist/config.cjs +9 -6
- package/dist/config.d.cts +62 -33
- package/dist/config.d.cts.map +1 -1
- package/dist/config.d.mts +62 -33
- package/dist/config.d.mts.map +1 -1
- package/dist/config.mjs +3 -5
- package/dist/deploy/sniffer-envkit-patch.cjs +18 -10
- package/dist/deploy/sniffer-envkit-patch.cjs.map +1 -1
- package/dist/deploy/sniffer-envkit-patch.d.cts +0 -2
- package/dist/deploy/sniffer-envkit-patch.d.cts.map +1 -1
- package/dist/deploy/sniffer-envkit-patch.d.mts +0 -2
- package/dist/deploy/sniffer-envkit-patch.d.mts.map +1 -1
- package/dist/deploy/sniffer-envkit-patch.mjs +10 -2
- package/dist/deploy/sniffer-envkit-patch.mjs.map +1 -1
- package/dist/deploy/sniffer-hooks.cjs +13 -7
- package/dist/deploy/sniffer-hooks.cjs.map +1 -1
- package/dist/deploy/sniffer-hooks.d.cts +1 -2
- package/dist/deploy/sniffer-hooks.d.cts.map +1 -1
- package/dist/deploy/sniffer-hooks.d.mts +1 -2
- package/dist/deploy/sniffer-hooks.d.mts.map +1 -1
- package/dist/deploy/sniffer-hooks.mjs +9 -2
- package/dist/deploy/sniffer-hooks.mjs.map +1 -1
- package/dist/deploy/sniffer-loader.cjs +16 -7
- package/dist/deploy/sniffer-loader.cjs.map +1 -1
- package/dist/deploy/sniffer-loader.d.cts +1 -1
- package/dist/deploy/sniffer-loader.d.mts +1 -1
- package/dist/deploy/sniffer-loader.mjs +13 -2
- package/dist/deploy/sniffer-loader.mjs.map +1 -1
- package/dist/deploy/sniffer-routes-worker.cjs +11 -11
- package/dist/deploy/sniffer-routes-worker.cjs.map +1 -1
- package/dist/deploy/sniffer-routes-worker.d.cts +1 -1
- package/dist/deploy/sniffer-routes-worker.d.mts +1 -1
- package/dist/deploy/sniffer-routes-worker.mjs +3 -3
- package/dist/deploy/sniffer-routes-worker.mjs.map +1 -1
- package/dist/deploy/sniffer-worker.cjs +15 -6
- package/dist/deploy/sniffer-worker.cjs.map +1 -1
- package/dist/deploy/sniffer-worker.d.cts +1 -3
- package/dist/deploy/sniffer-worker.d.cts.map +1 -1
- package/dist/deploy/sniffer-worker.d.mts +1 -3
- package/dist/deploy/sniffer-worker.d.mts.map +1 -1
- package/dist/deploy/sniffer-worker.mjs +15 -4
- package/dist/deploy/sniffer-worker.mjs.map +1 -1
- package/dist/{discover-CAlFgYec.mjs → discover-Cf2jAcgF.mjs} +56 -63
- package/dist/discover-Cf2jAcgF.mjs.map +1 -0
- package/dist/{discover-DTmFeloL.cjs → discover-DXNviAh-.cjs} +86 -110
- package/dist/discover-DXNviAh-.cjs.map +1 -0
- package/dist/{emit-CiZS_5Ub.mjs → emit-C1e8qD08.mjs} +13 -26
- package/dist/emit-C1e8qD08.mjs.map +1 -0
- package/dist/{emit-CbcRolsa.cjs → emit-Dmc1nvdS.cjs} +31 -57
- package/dist/emit-Dmc1nvdS.cjs.map +1 -0
- package/dist/encryption-C7OGMve_.cjs +91 -0
- package/dist/encryption-C7OGMve_.cjs.map +1 -0
- package/dist/encryption-DyQnPsZN.mjs +90 -0
- package/dist/encryption-DyQnPsZN.mjs.map +1 -0
- package/dist/github-BfpadQWQ.mjs +213 -0
- package/dist/github-BfpadQWQ.mjs.map +1 -0
- package/dist/github-BrI1tv6u.cjs +213 -0
- package/dist/github-BrI1tv6u.cjs.map +1 -0
- package/dist/{index-CHB4tSzX.d.cts → index-BQhJXpaQ.d.mts} +4 -23
- package/dist/index-BQhJXpaQ.d.mts.map +1 -0
- package/dist/{index-BThUzEIp.d.mts → index-DHJePE0y.d.cts} +4 -23
- package/dist/index-DHJePE0y.d.cts.map +1 -0
- package/dist/index.cjs +9531 -7200
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.cts +1 -1
- package/dist/index.d.mts +1 -1
- package/dist/index.mjs +9412 -7108
- package/dist/index.mjs.map +1 -1
- package/dist/migrate-B00XpVaO.mjs +736 -0
- package/dist/migrate-B00XpVaO.mjs.map +1 -0
- package/dist/migrate-CNlew9Ek.cjs +743 -0
- package/dist/migrate-CNlew9Ek.cjs.map +1 -0
- package/dist/openapi.cjs +198 -8
- package/dist/openapi.cjs.map +1 -0
- package/dist/openapi.d.cts +53 -15
- package/dist/openapi.d.cts.map +1 -1
- package/dist/openapi.d.mts +53 -15
- package/dist/openapi.d.mts.map +1 -1
- package/dist/openapi.mjs +193 -5
- package/dist/openapi.mjs.map +1 -0
- package/dist/ready-WfT-eWlm.mjs +861 -0
- package/dist/ready-WfT-eWlm.mjs.map +1 -0
- package/dist/ready-lr2Fm-ep.cjs +932 -0
- package/dist/ready-lr2Fm-ep.cjs.map +1 -0
- package/dist/reconcile/public.cjs +3 -5
- package/dist/reconcile/public.d.cts +30 -33
- package/dist/reconcile/public.d.cts.map +1 -1
- package/dist/reconcile/public.d.mts +30 -33
- package/dist/reconcile/public.d.mts.map +1 -1
- package/dist/reconcile/public.mjs +3 -4
- package/dist/{reconcile-CFs8C_1a.mjs → reconcile--I7V4Klj.mjs} +4 -7
- package/dist/reconcile--I7V4Klj.mjs.map +1 -0
- package/dist/{reconcile-D0pimNiy.cjs → reconcile-CuCQBa5-.cjs} +5 -8
- package/dist/reconcile-CuCQBa5-.cjs.map +1 -0
- package/dist/rolldown-runtime-BhDjJH2R.mjs +15 -0
- package/dist/{chunk-CUT6urMc.cjs → rolldown-runtime-C0BPl7ul.cjs} +23 -10
- package/dist/storage-BzaUxmqe.mjs +124 -0
- package/dist/storage-BzaUxmqe.mjs.map +1 -0
- package/dist/storage-CSGoC3bK.cjs +152 -0
- package/dist/storage-CSGoC3bK.cjs.map +1 -0
- package/dist/store-DQgxT_WR.cjs +211 -0
- package/dist/store-DQgxT_WR.cjs.map +1 -0
- package/dist/store-DeGATCS-.mjs +182 -0
- package/dist/store-DeGATCS-.mjs.map +1 -0
- package/dist/types-BbrNu83u.d.mts +6229 -0
- package/dist/types-BbrNu83u.d.mts.map +1 -0
- package/dist/types-DNqEsctD.d.cts +6230 -0
- package/dist/types-DNqEsctD.d.cts.map +1 -0
- package/dist/vitest.cjs +65 -0
- package/dist/vitest.cjs.map +1 -0
- package/dist/vitest.d.cts +29 -0
- package/dist/vitest.d.cts.map +1 -0
- package/dist/vitest.d.mts +29 -0
- package/dist/vitest.d.mts.map +1 -0
- package/dist/vitest.mjs +65 -0
- package/dist/vitest.mjs.map +1 -0
- package/dist/workspace/index.cjs +3 -4
- package/dist/workspace/index.d.cts +3 -3
- package/dist/workspace/index.d.mts +3 -3
- package/dist/workspace/index.mjs +2 -3
- package/dist/{credentials-BBIRJVp4.mjs → workspace-BsbxbqV-.mjs} +2201 -1732
- package/dist/workspace-BsbxbqV-.mjs.map +1 -0
- package/dist/{workspace-BcucjwvA.cjs → workspace-DEibcoBe.cjs} +1038 -1118
- package/dist/workspace-DEibcoBe.cjs.map +1 -0
- package/dist/{workspace-BFMrAKqI.mjs → workspace-DNzWgM1J.mjs} +869 -972
- package/dist/workspace-DNzWgM1J.mjs.map +1 -0
- package/dist/{credentials-G1JwBoXH.cjs → workspace-j6ZxvJqq.cjs} +2337 -1849
- package/dist/workspace-j6ZxvJqq.cjs.map +1 -0
- package/package.json +29 -20
- package/dist/CachedStateProvider-BLo7zv7S.cjs +0 -3
- package/dist/CachedStateProvider-BxoAm-B2.mjs +0 -3
- package/dist/CachedStateProvider-CqPdW7CB.cjs +0 -60
- package/dist/CachedStateProvider-CqPdW7CB.cjs.map +0 -1
- package/dist/CachedStateProvider-DTniehMP.mjs +0 -54
- package/dist/CachedStateProvider-DTniehMP.mjs.map +0 -1
- package/dist/HostingerProvider-Cft7lEYr.mjs.map +0 -1
- package/dist/HostingerProvider-h3EbEp-Z.cjs.map +0 -1
- package/dist/LocalStateProvider-Bn6tS-oL.mjs.map +0 -1
- package/dist/LocalStateProvider-Db9SEjyH.cjs.map +0 -1
- package/dist/Route53Provider-RdpUDJQk.mjs.map +0 -1
- package/dist/Route53Provider-di2fMM5N.cjs.map +0 -1
- package/dist/SSMStateProvider-BRt2py-A.mjs.map +0 -1
- package/dist/SSMStateProvider-DyX8s9HI.cjs.map +0 -1
- package/dist/backup-provisioner-Bgj5Vi_L.mjs.map +0 -1
- package/dist/backup-provisioner-BrsHwYG0.cjs.map +0 -1
- package/dist/bundler-BY1OmV1e.cjs.map +0 -1
- package/dist/bundler-Be8Zg4Wd.mjs.map +0 -1
- package/dist/chunk-Duj1WY3L.mjs +0 -7
- package/dist/config-C21ocYNd.cjs.map +0 -1
- package/dist/config-WRdsQYZK.mjs.map +0 -1
- package/dist/credentials-B5zRHdAl.mjs +0 -141
- package/dist/credentials-B5zRHdAl.mjs.map +0 -1
- package/dist/credentials-BBIRJVp4.mjs.map +0 -1
- package/dist/credentials-BapzjyV5.cjs +0 -195
- package/dist/credentials-BapzjyV5.cjs.map +0 -1
- package/dist/credentials-G1JwBoXH.cjs.map +0 -1
- package/dist/discover-CAlFgYec.mjs.map +0 -1
- package/dist/discover-DTmFeloL.cjs.map +0 -1
- package/dist/dokploy-api-BS2xnJLZ.mjs +0 -3
- package/dist/dokploy-api-BsVH0y_s.cjs +0 -718
- package/dist/dokploy-api-BsVH0y_s.cjs.map +0 -1
- package/dist/dokploy-api-BuxDRo3l.cjs +0 -3
- package/dist/dokploy-api-aTFPz7_4.mjs +0 -706
- package/dist/dokploy-api-aTFPz7_4.mjs.map +0 -1
- package/dist/emit-CbcRolsa.cjs.map +0 -1
- package/dist/emit-CiZS_5Ub.mjs.map +0 -1
- package/dist/encryption-C6jaU0hE.cjs +0 -54
- package/dist/encryption-C6jaU0hE.cjs.map +0 -1
- package/dist/encryption-CHzzeR8a.cjs +0 -4
- package/dist/encryption-CQUncu7v.mjs +0 -3
- package/dist/encryption-D-abyhZx.mjs +0 -42
- package/dist/encryption-D-abyhZx.mjs.map +0 -1
- package/dist/fullstack-secrets-C-GGohke.cjs +0 -111
- package/dist/fullstack-secrets-C-GGohke.cjs.map +0 -1
- package/dist/fullstack-secrets-D1o9-G5l.mjs +0 -87
- package/dist/fullstack-secrets-D1o9-G5l.mjs.map +0 -1
- package/dist/generator-CNzBw7g5.cjs +0 -289
- package/dist/generator-CNzBw7g5.cjs.map +0 -1
- package/dist/generator-VE2JrFlY.mjs +0 -253
- package/dist/generator-VE2JrFlY.mjs.map +0 -1
- package/dist/index-BThUzEIp.d.mts.map +0 -1
- package/dist/index-CHB4tSzX.d.cts.map +0 -1
- package/dist/openapi-CA0yJUTN.cjs.map +0 -1
- package/dist/openapi-kkQbsRVj.mjs.map +0 -1
- package/dist/openapi-react-query-Byltvm_7.mjs +0 -167
- package/dist/openapi-react-query-Byltvm_7.mjs.map +0 -1
- package/dist/openapi-react-query-CmJ1xDbn.cjs +0 -173
- package/dist/openapi-react-query-CmJ1xDbn.cjs.map +0 -1
- package/dist/openapi-react-query.cjs +0 -4
- package/dist/openapi-react-query.d.cts +0 -11
- package/dist/openapi-react-query.d.cts.map +0 -1
- package/dist/openapi-react-query.d.mts +0 -11
- package/dist/openapi-react-query.d.mts.map +0 -1
- package/dist/openapi-react-query.mjs +0 -4
- package/dist/reconcile-CFs8C_1a.mjs.map +0 -1
- package/dist/reconcile-D0pimNiy.cjs.map +0 -1
- package/dist/setup-B5_oHHUj.cjs +0 -11
- package/dist/setup-Cm2UnAdh.cjs +0 -473
- package/dist/setup-Cm2UnAdh.cjs.map +0 -1
- package/dist/setup-DXvRLTZN.mjs +0 -443
- package/dist/setup-DXvRLTZN.mjs.map +0 -1
- package/dist/setup-ZijS4qeC.mjs +0 -11
- package/dist/storage-BJmzYxt8.cjs +0 -363
- package/dist/storage-BJmzYxt8.cjs.map +0 -1
- package/dist/storage-BM9hGpgv.cjs +0 -7
- package/dist/storage-Bi24WfoW.mjs +0 -297
- package/dist/storage-Bi24WfoW.mjs.map +0 -1
- package/dist/storage-CYlGwWTj.mjs +0 -3
- package/dist/sync-CmEuNZsB.mjs +0 -76
- package/dist/sync-CmEuNZsB.mjs.map +0 -1
- package/dist/sync-D306yOWu.cjs +0 -4
- package/dist/sync-SizaChmC.cjs +0 -93
- package/dist/sync-SizaChmC.cjs.map +0 -1
- package/dist/sync-hOqAWHR6.mjs +0 -3
- package/dist/types-C-arnQ1r.d.mts +0 -2184
- package/dist/types-C-arnQ1r.d.mts.map +0 -1
- package/dist/types-abJQuk7m.d.cts +0 -2184
- package/dist/types-abJQuk7m.d.cts.map +0 -1
- package/dist/workspace-BFMrAKqI.mjs.map +0 -1
- package/dist/workspace-BcucjwvA.cjs.map +0 -1
- package/dist/workspace-DdJss8I6.cjs +0 -9
- package/dist/workspace-k4l3rsFh.mjs +0 -8
|
@@ -1,321 +1,266 @@
|
|
|
1
|
-
import {
|
|
2
|
-
import {
|
|
3
|
-
import {
|
|
4
|
-
import {
|
|
5
|
-
import {
|
|
6
|
-
import {
|
|
1
|
+
import { t as __exportAll } from "./rolldown-runtime-BhDjJH2R.mjs";
|
|
2
|
+
import { t as discover } from "./discover-Cf2jAcgF.mjs";
|
|
3
|
+
import { c as loadWorkspaceAppInfo, f as appKey, l as loadWorkspaceConfig, r as NotInAnApp, t as ConfigNotFound } from "./config-D8J5rrTb.mjs";
|
|
4
|
+
import { E as TEST_STAGE, t as allConstructGlobs } from "./workspace-DNzWgM1J.mjs";
|
|
5
|
+
import { i as toEmbeddableSecrets } from "./storage-BzaUxmqe.mjs";
|
|
6
|
+
import { i as FileSecretsStore, n as secretsStoreFor } from "./store-DeGATCS-.mjs";
|
|
7
|
+
import { l as readFakes, u as DEFAULT_CACHE } from "./ready-WfT-eWlm.mjs";
|
|
8
|
+
import { n as eventsBackendFor, r as providerOf, t as cacheBackendFor } from "./backends-CSuLl0YP.mjs";
|
|
9
|
+
import { existsSync } from "node:fs";
|
|
10
|
+
import { mkdir, readFile, readdir, rm, writeFile } from "node:fs/promises";
|
|
11
|
+
import { networkInterfaces } from "node:os";
|
|
7
12
|
import { dirname, join, resolve } from "node:path";
|
|
8
|
-
import {
|
|
9
|
-
import {
|
|
10
|
-
import { execFile, execSync } from "node:child_process";
|
|
13
|
+
import { execFile, spawn } from "node:child_process";
|
|
14
|
+
import { DEFAULT_POSTGRES_VERSION, cacheTable, cookieDomain, dependenciesOf, dependentsOf, externalApiUrl, kebabCase, mobileOrigins, provideKey, providedKeyFor, provisionOrder, publicEnvFor, schemeBase } from "@geekmidas/manifest";
|
|
11
15
|
import { createServer } from "node:net";
|
|
12
16
|
import { config } from "dotenv";
|
|
13
|
-
import {
|
|
17
|
+
import { createHash } from "node:crypto";
|
|
18
|
+
import { formatKeyring } from "@geekmidas/constructs/encryption";
|
|
19
|
+
import { ownerRole, readerRole, roleStatements } from "@geekmidas/db/pg/roles";
|
|
20
|
+
import * as snsUrl from "@geekmidas/events/sns/url";
|
|
21
|
+
import * as sqsUrl from "@geekmidas/events/sqs/url";
|
|
14
22
|
import { CreateBucketCommand, GetBucketPolicyCommand, HeadBucketCommand, PutBucketPolicyCommand, S3Client } from "@aws-sdk/client-s3";
|
|
23
|
+
import { CreateTopicCommand, GetTopicAttributesCommand, SNSClient } from "@aws-sdk/client-sns";
|
|
24
|
+
import { CreateQueueCommand, GetQueueUrlCommand, SQSClient } from "@aws-sdk/client-sqs";
|
|
15
25
|
import { Client } from "pg";
|
|
26
|
+
import { stringify } from "yaml";
|
|
16
27
|
import { promisify } from "node:util";
|
|
17
|
-
import { createHash } from "node:crypto";
|
|
18
|
-
import { ownerRole, readerRole, roleStatements } from "@geekmidas/db/pg/roles";
|
|
19
28
|
import { cacheTableStatements } from "@geekmidas/cache/postgres";
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
const CACHE_BACKENDS = [
|
|
23
|
-
"upstash",
|
|
24
|
-
"elasticache",
|
|
25
|
-
"db"
|
|
26
|
-
];
|
|
27
|
-
const EMAIL_BACKENDS = [
|
|
28
|
-
"resend",
|
|
29
|
-
"ses",
|
|
30
|
-
"smtp"
|
|
31
|
-
];
|
|
32
|
-
const STORAGE_BACKENDS = [
|
|
33
|
-
"minio",
|
|
34
|
-
"s3",
|
|
35
|
-
"r2"
|
|
36
|
-
];
|
|
37
|
-
/**
|
|
38
|
-
* The cache backend a workspace selected, or the one its target implies.
|
|
39
|
-
*
|
|
40
|
-
* Defaulted here rather than by every caller, so "what happens when nobody
|
|
41
|
-
* said" is answered once and the answer is greppable — and now answered by
|
|
42
|
-
* *where it deploys*, because there is no single right answer for both a
|
|
43
|
-
* Lambda and a box running its own Postgres.
|
|
44
|
-
*
|
|
45
|
-
* `on` is the deploy target, not where this code happens to be running. A
|
|
46
|
-
* local `gkm dev` for an AWS app still defaults to Upstash, because a backend
|
|
47
|
-
* that differed between local and deployed would be worse than a slower one —
|
|
48
|
-
* which is the property the type's own documentation asks for.
|
|
49
|
-
*/
|
|
50
|
-
function cacheBackendOf(value, on = "aws") {
|
|
51
|
-
return isBackend(value, CACHE_BACKENDS) ? value : DEFAULT_CACHE[on];
|
|
52
|
-
}
|
|
53
|
-
/** The email backend a workspace selected, defaulting to SES on every target. */
|
|
54
|
-
function emailBackendOf(value) {
|
|
55
|
-
return isBackend(value, EMAIL_BACKENDS) ? value : DEFAULT_EMAIL;
|
|
56
|
-
}
|
|
57
|
-
/** The storage backend a workspace selected, or the one its target implies. */
|
|
58
|
-
function storageBackendOf(value, on = "aws") {
|
|
59
|
-
return isBackend(value, STORAGE_BACKENDS) ? value : DEFAULT_STORAGE[on];
|
|
60
|
-
}
|
|
61
|
-
/**
|
|
62
|
-
* Which family of defaults a workspace's deploy target belongs to.
|
|
63
|
-
*
|
|
64
|
-
* One question, asked once: **does this target run containers the project
|
|
65
|
-
* controls?** Dokploy and a bare server do, so a bucket or a cache can live
|
|
66
|
-
* beside the app; AWS, Vercel and Cloudflare do not, so the default has to be
|
|
67
|
-
* something managed. That is the whole of the distinction, and it is why
|
|
68
|
-
* `MainProvider` is the right axis rather than the deploy target's own name —
|
|
69
|
-
* three targets share one answer.
|
|
70
|
-
*
|
|
71
|
-
* An AWS deploy names no `deploy.default` at all, because it goes through SST,
|
|
72
|
-
* which is why the absence is what selects `aws`.
|
|
73
|
-
*/
|
|
74
|
-
function providerOf(workspace) {
|
|
75
|
-
const target = workspace.deploy?.default;
|
|
76
|
-
return target === "dokploy" || target === "server" ? "server" : "aws";
|
|
77
|
-
}
|
|
78
|
-
/**
|
|
79
|
-
* Whether a string names a backend, as opposed to being one of the other things
|
|
80
|
-
* a `services:` value can be.
|
|
81
|
-
*
|
|
82
|
-
* A misspelling falls through to the default rather than throwing, which is the
|
|
83
|
-
* wrong trade for something with a cost attached — so callers that can report it
|
|
84
|
-
* should use {@link unknownBackend} to say so.
|
|
85
|
-
*/
|
|
86
|
-
function isBackend(value, backends) {
|
|
87
|
-
return typeof value === "string" && backends.includes(value);
|
|
88
|
-
}
|
|
89
|
-
|
|
90
|
-
//#endregion
|
|
91
|
-
//#region src/reconcile/caddyfile.ts
|
|
92
|
-
/**
|
|
93
|
-
* The kinds the edge serves.
|
|
94
|
-
*
|
|
95
|
-
* Everything that owns a *public address*. Deployed, each of these has a real
|
|
96
|
-
* hostname and a certificate, so this is what makes the local shape the same
|
|
97
|
-
* one rather than a near-enough one — and the application is indifferent
|
|
98
|
-
* either way, because it reads whichever address was injected and never
|
|
99
|
-
* composes one.
|
|
100
|
-
*
|
|
101
|
-
* Adding a kind here is the whole of adding it to the edge.
|
|
102
|
-
*/
|
|
103
|
-
const EDGE_KINDS = {
|
|
104
|
-
"file-server": true,
|
|
105
|
-
"rest-api": true,
|
|
106
|
-
site: true
|
|
107
|
-
};
|
|
29
|
+
//#region src/credentials/index.ts
|
|
30
|
+
const logger = console;
|
|
108
31
|
/**
|
|
109
|
-
*
|
|
110
|
-
*
|
|
111
|
-
* A surface and a site are served by processes `gkm dev` starts on the host,
|
|
112
|
-
* not by containers — so the edge has to leave Docker's network to reach them.
|
|
113
|
-
* Docker Desktop resolves this name already; on Linux the compose definition
|
|
114
|
-
* maps it to the host gateway.
|
|
32
|
+
* Load environment files
|
|
33
|
+
* @internal Exported for testing
|
|
115
34
|
*/
|
|
116
|
-
|
|
35
|
+
function loadEnvFiles(envConfig, cwd = process.cwd()) {
|
|
36
|
+
const loaded = [];
|
|
37
|
+
const missing = [];
|
|
38
|
+
const envFiles = envConfig ? Array.isArray(envConfig) ? envConfig : [envConfig] : [".env"];
|
|
39
|
+
for (const envFile of envFiles) {
|
|
40
|
+
const envPath = resolve(cwd, envFile);
|
|
41
|
+
if (existsSync(envPath)) {
|
|
42
|
+
config({
|
|
43
|
+
path: envPath,
|
|
44
|
+
override: true,
|
|
45
|
+
quiet: true
|
|
46
|
+
});
|
|
47
|
+
loaded.push(envFile);
|
|
48
|
+
} else if (envConfig) missing.push(envFile);
|
|
49
|
+
}
|
|
50
|
+
return {
|
|
51
|
+
loaded,
|
|
52
|
+
missing
|
|
53
|
+
};
|
|
54
|
+
}
|
|
117
55
|
/**
|
|
118
|
-
*
|
|
119
|
-
*
|
|
120
|
-
* `.localhost` rather than a made-up TLD because it resolves to loopback
|
|
121
|
-
* without touching `/etc/hosts` — macOS and systemd-resolved both answer for
|
|
122
|
-
* subdomains of it, and Caddy issues internal certificates for it by default.
|
|
56
|
+
* Check if a port is available
|
|
57
|
+
* @internal Exported for testing
|
|
123
58
|
*/
|
|
124
|
-
|
|
59
|
+
async function isPortAvailable(port) {
|
|
60
|
+
return new Promise((resolve) => {
|
|
61
|
+
const server = createServer();
|
|
62
|
+
server.once("error", (err) => {
|
|
63
|
+
if (err.code === "EADDRINUSE") resolve(false);
|
|
64
|
+
else resolve(false);
|
|
65
|
+
});
|
|
66
|
+
server.once("listening", () => {
|
|
67
|
+
server.close();
|
|
68
|
+
resolve(true);
|
|
69
|
+
});
|
|
70
|
+
server.listen(port);
|
|
71
|
+
});
|
|
72
|
+
}
|
|
125
73
|
/**
|
|
126
|
-
*
|
|
127
|
-
*
|
|
128
|
-
* The leading label is the *server's* stage-scoped name rather than the
|
|
129
|
-
* bucket's, which is the difference that matters: two servers over one bucket
|
|
130
|
-
* are a legitimate arrangement — two cache behaviours, one origin — and naming
|
|
131
|
-
* the host after the bucket would make them collide.
|
|
74
|
+
* Find an available port starting from the preferred port
|
|
75
|
+
* @internal Exported for testing
|
|
132
76
|
*/
|
|
133
|
-
function
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
const host = hostFor(resource, project);
|
|
139
|
-
if (resource.kind === "file-server") {
|
|
140
|
-
const origin = resource.of ? byId.get(resource.of) : void 0;
|
|
141
|
-
if (!origin) continue;
|
|
142
|
-
sites.push({
|
|
143
|
-
host,
|
|
144
|
-
upstream: "http://minio:9000",
|
|
145
|
-
rewrite: `/${origin.name}{uri}`
|
|
146
|
-
});
|
|
147
|
-
continue;
|
|
148
|
-
}
|
|
149
|
-
const address = addresses[resource.id];
|
|
150
|
-
if (!address) continue;
|
|
151
|
-
const port = portOf(address);
|
|
152
|
-
if (!port) continue;
|
|
153
|
-
sites.push({
|
|
154
|
-
host,
|
|
155
|
-
upstream: `http://${HOST_GATEWAY}:${port}`
|
|
156
|
-
});
|
|
77
|
+
async function findAvailablePort(preferredPort, maxAttempts = 10) {
|
|
78
|
+
for (let i = 0; i < maxAttempts; i++) {
|
|
79
|
+
const port = preferredPort + i;
|
|
80
|
+
if (await isPortAvailable(port)) return port;
|
|
81
|
+
logger.log(`⚠️ Port ${port} is in use, trying ${port + 1}...`);
|
|
157
82
|
}
|
|
158
|
-
|
|
83
|
+
throw new Error(`Could not find an available port after trying ${maxAttempts} ports starting from ${preferredPort}`);
|
|
159
84
|
}
|
|
160
|
-
|
|
161
|
-
|
|
85
|
+
const PORT_STATE_PATH = ".gkm/ports.json";
|
|
86
|
+
/**
|
|
87
|
+
* Load saved port state from .gkm/ports.json.
|
|
88
|
+
* @internal Exported for testing
|
|
89
|
+
*/
|
|
90
|
+
async function loadPortState(workspaceRoot) {
|
|
162
91
|
try {
|
|
163
|
-
const
|
|
164
|
-
return
|
|
92
|
+
const raw = await readFile(join(workspaceRoot, PORT_STATE_PATH), "utf-8");
|
|
93
|
+
return JSON.parse(raw);
|
|
165
94
|
} catch {
|
|
166
|
-
return
|
|
95
|
+
return {};
|
|
167
96
|
}
|
|
168
97
|
}
|
|
169
98
|
/**
|
|
170
|
-
*
|
|
171
|
-
*
|
|
172
|
-
* The project is a label rather than a decoration: it is what keeps two
|
|
173
|
-
* checkouts from answering on one address. Omitted when there is no project to
|
|
174
|
-
* name, because an empty label makes `uploads..localhost` — a host nothing
|
|
175
|
-
* resolves and no certificate can be issued for.
|
|
99
|
+
* Save port state to .gkm/ports.json.
|
|
100
|
+
* @internal Exported for testing
|
|
176
101
|
*/
|
|
177
|
-
function
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
LOCAL_TLD
|
|
182
|
-
].filter(Boolean).join(".");
|
|
102
|
+
async function savePortState(workspaceRoot, ports) {
|
|
103
|
+
const dir = join(workspaceRoot, ".gkm");
|
|
104
|
+
await mkdir(dir, { recursive: true });
|
|
105
|
+
await writeFile(join(workspaceRoot, PORT_STATE_PATH), `${JSON.stringify(ports, null, 2)}\n`);
|
|
183
106
|
}
|
|
184
107
|
/**
|
|
185
|
-
*
|
|
186
|
-
*
|
|
187
|
-
*
|
|
188
|
-
* differ and they live in their own files. That split is not tidiness: one
|
|
189
|
-
* container serves every stage — the same way one Postgres holds `orders` and
|
|
190
|
-
* `orders_test` — so a single Caddyfile would mean `gkm test` deleting the
|
|
191
|
-
* routes `gkm dev` is serving, and each command silently breaking the other.
|
|
108
|
+
* Load and flatten secrets for an app from encrypted storage.
|
|
109
|
+
* For workspace app: maps {APP}_DATABASE_URL → DATABASE_URL.
|
|
110
|
+
* @internal Exported for testing
|
|
192
111
|
*/
|
|
193
|
-
function
|
|
194
|
-
|
|
195
|
-
|
|
196
|
-
|
|
197
|
-
|
|
198
|
-
|
|
112
|
+
async function loadSecretsForApp(store, stage, appName) {
|
|
113
|
+
let secrets = {};
|
|
114
|
+
const stageSecrets = await store.read(stage);
|
|
115
|
+
if (stageSecrets) {
|
|
116
|
+
logger.log(`🔐 Loading secrets from stage: ${stage}`);
|
|
117
|
+
secrets = toEmbeddableSecrets(stageSecrets);
|
|
118
|
+
}
|
|
119
|
+
if (Object.keys(secrets).length === 0) return {};
|
|
120
|
+
if (!appName) return secrets;
|
|
121
|
+
const prefix = appName.toUpperCase();
|
|
122
|
+
const mapped = { ...secrets };
|
|
123
|
+
const appDbUrl = secrets[`${prefix}_DATABASE_URL`];
|
|
124
|
+
if (appDbUrl) mapped.DATABASE_URL = appDbUrl;
|
|
125
|
+
return mapped;
|
|
199
126
|
}
|
|
200
|
-
|
|
201
|
-
|
|
202
|
-
|
|
203
|
-
|
|
204
|
-
|
|
127
|
+
/**
|
|
128
|
+
* Walk up the directory tree to find the root containing .gkm/secrets/.
|
|
129
|
+
* @internal Exported for testing
|
|
130
|
+
*/
|
|
131
|
+
function findSecretsRoot(startDir) {
|
|
132
|
+
let dir = startDir;
|
|
133
|
+
while (dir !== "/") {
|
|
134
|
+
if (existsSync(join(dir, ".gkm", "secrets"))) return dir;
|
|
135
|
+
const parent = dirname(dir);
|
|
136
|
+
if (parent === dir) break;
|
|
137
|
+
dir = parent;
|
|
138
|
+
}
|
|
139
|
+
return startDir;
|
|
205
140
|
}
|
|
206
141
|
/**
|
|
207
|
-
*
|
|
208
|
-
*
|
|
209
|
-
*
|
|
210
|
-
* origin's business, and the client asks for the key it stored.
|
|
211
|
-
*
|
|
212
|
-
* `header_up Host {upstream_hostport}` matters more than it looks. MinIO routes
|
|
213
|
-
* and signs on the Host header, so forwarding the requested hostname would make
|
|
214
|
-
* it look for a bucket named after the domain.
|
|
142
|
+
* Generate the credentials injection code snippet.
|
|
143
|
+
* This is the common logic used by both entry wrapper and exec preload.
|
|
144
|
+
* @internal
|
|
215
145
|
*/
|
|
216
|
-
function
|
|
217
|
-
|
|
218
|
-
# No file server is declared in this stage, so it routes nothing.
|
|
219
|
-
`;
|
|
220
|
-
const blocks = sites.map(({ host, upstream, rewrite }) => `
|
|
221
|
-
https://${host} {
|
|
222
|
-
tls internal
|
|
146
|
+
function generateCredentialsInjection(secretsJsonPath) {
|
|
147
|
+
return `import { existsSync, readFileSync } from 'node:fs';
|
|
223
148
|
|
|
224
|
-
|
|
225
|
-
|
|
226
|
-
|
|
227
|
-
|
|
228
|
-
|
|
229
|
-
|
|
230
|
-
|
|
231
|
-
|
|
232
|
-
header_up Host {host}
|
|
233
|
-
header_up X-Forwarded-Proto {scheme}`}
|
|
234
|
-
}
|
|
235
|
-
}`);
|
|
236
|
-
return `${GENERATED}${blocks.join("\n")}\n`;
|
|
149
|
+
// Inject dev secrets via globalThis and process.env
|
|
150
|
+
// Using globalThis.__gkm_credentials__ avoids CJS/ESM interop issues where
|
|
151
|
+
// Object.assign on the Credentials export only mutates one module copy.
|
|
152
|
+
const secretsPath = '${secretsJsonPath}';
|
|
153
|
+
if (existsSync(secretsPath)) {
|
|
154
|
+
const secrets = JSON.parse(readFileSync(secretsPath, 'utf-8'));
|
|
155
|
+
globalThis.__gkm_credentials__ = secrets;
|
|
156
|
+
Object.assign(process.env, secrets);
|
|
237
157
|
}
|
|
238
|
-
const GENERATED = `# Generated by gkm from the construct manifest — do not edit.
|
|
239
|
-
#
|
|
240
|
-
# One host per declared file server, in front of the object store that holds its
|
|
241
|
-
# objects. This is the shape a file server has deployed — a domain serving a
|
|
242
|
-
# bucket — which MinIO alone cannot produce: its virtual-host mode reads the
|
|
243
|
-
# leading label as the bucket name.
|
|
244
|
-
#
|
|
245
|
-
# Certificates come from Caddy's own CA. "gkm dev" and "gkm test" export its
|
|
246
|
-
# root and point Node at it, so nothing has to be installed to use these URLs
|
|
247
|
-
# from code; a browser needs the root trusted once.
|
|
248
158
|
`;
|
|
249
|
-
|
|
250
|
-
//#endregion
|
|
251
|
-
//#region src/reconcile/clients.ts
|
|
252
|
-
/** The credential local containers are brought up with. */
|
|
253
|
-
const LOCAL_USER$2 = "geekmidas";
|
|
159
|
+
}
|
|
254
160
|
/**
|
|
255
|
-
*
|
|
256
|
-
*
|
|
257
|
-
*
|
|
258
|
-
* and a schema must be created from inside its own database — so the database is
|
|
259
|
-
* part of each query rather than fixed when the client is built.
|
|
161
|
+
* Create a preload script that injects secrets into Credentials.
|
|
162
|
+
* Used by `gkm exec` to inject secrets before running any command.
|
|
163
|
+
* @internal Exported for testing
|
|
260
164
|
*/
|
|
261
|
-
function
|
|
262
|
-
|
|
263
|
-
|
|
264
|
-
|
|
265
|
-
|
|
266
|
-
|
|
267
|
-
|
|
268
|
-
database: database ?? LOCAL_USER$2
|
|
269
|
-
});
|
|
270
|
-
await client.connect();
|
|
271
|
-
try {
|
|
272
|
-
const result = await client.query(sql, values);
|
|
273
|
-
return result.rows;
|
|
274
|
-
} finally {
|
|
275
|
-
await client.end();
|
|
276
|
-
}
|
|
277
|
-
} };
|
|
165
|
+
async function createCredentialsPreload(preloadPath, secretsJsonPath) {
|
|
166
|
+
const content = `/**
|
|
167
|
+
* Credentials preload generated by 'gkm exec'
|
|
168
|
+
* This file is loaded via NODE_OPTIONS="--import <path>"
|
|
169
|
+
*/
|
|
170
|
+
${generateCredentialsInjection(secretsJsonPath)}`;
|
|
171
|
+
await writeFile(preloadPath, content);
|
|
278
172
|
}
|
|
279
|
-
/**
|
|
280
|
-
|
|
281
|
-
|
|
282
|
-
|
|
283
|
-
|
|
284
|
-
|
|
285
|
-
|
|
286
|
-
|
|
287
|
-
|
|
288
|
-
|
|
289
|
-
|
|
173
|
+
/**
|
|
174
|
+
* Create a wrapper script that injects secrets before importing the entry file.
|
|
175
|
+
* @internal Exported for testing
|
|
176
|
+
*/
|
|
177
|
+
async function createEntryWrapper(wrapperPath, entryPath, secretsJsonPath) {
|
|
178
|
+
const content = `#!/usr/bin/env node
|
|
179
|
+
/**
|
|
180
|
+
* Entry wrapper generated by 'gkm dev --entry'
|
|
181
|
+
*/
|
|
182
|
+
${secretsJsonPath ? `${generateCredentialsInjection(secretsJsonPath)}
|
|
183
|
+
` : ""}// Import and run the user's entry file (dynamic import ensures secrets load first)
|
|
184
|
+
await import('${entryPath}');
|
|
185
|
+
`;
|
|
186
|
+
await writeFile(wrapperPath, content);
|
|
187
|
+
}
|
|
188
|
+
/**
|
|
189
|
+
* Prepare credentials for dev/exec/test modes.
|
|
190
|
+
* Loads workspace config, secrets, resolves Docker ports, rewrites URLs,
|
|
191
|
+
* injects PORT, dependency URLs, and writes credentials JSON.
|
|
192
|
+
*
|
|
193
|
+
* @param options.stage - The stage whose secrets to load. Default: the project's `stages.local`.
|
|
194
|
+
* @param options.startDocker - Start Docker Compose services after port resolution. Default: false.
|
|
195
|
+
* @param options.secretsFileName - Custom secrets JSON filename. Default: 'dev-secrets-{appName}.json' or 'dev-secrets.json'.
|
|
196
|
+
* @internal Exported for testing
|
|
197
|
+
*/
|
|
198
|
+
async function prepareEntryCredentials(options) {
|
|
199
|
+
const cwd = options.cwd ?? process.cwd();
|
|
200
|
+
let workspaceAppPort;
|
|
201
|
+
let secretsRoot = cwd;
|
|
202
|
+
let appName;
|
|
203
|
+
let appInfo;
|
|
204
|
+
let workspace;
|
|
205
|
+
try {
|
|
206
|
+
appInfo = await loadWorkspaceAppInfo(cwd);
|
|
207
|
+
workspace = appInfo.workspace;
|
|
208
|
+
workspaceAppPort = appInfo.app.port;
|
|
209
|
+
secretsRoot = appInfo.workspaceRoot;
|
|
210
|
+
appName = appInfo.appName;
|
|
211
|
+
} catch (error) {
|
|
212
|
+
if (!(error instanceof NotInAnApp || error instanceof ConfigNotFound)) throw error;
|
|
213
|
+
workspace = await loadWorkspaceConfig(cwd).then((loaded) => loaded.workspace).catch(() => void 0);
|
|
214
|
+
secretsRoot = workspace?.root ?? findSecretsRoot(cwd);
|
|
215
|
+
}
|
|
216
|
+
if (appName && workspace && options.explicitPort === void 0) {
|
|
217
|
+
const { assignAppPorts, describeHolder, holderOf, withAppPorts } = await import("./index.mjs").then((n) => n.a);
|
|
218
|
+
const assigned = await assignAppPorts(workspace, [appName], {
|
|
219
|
+
free: isPortAvailable,
|
|
220
|
+
holder: holderOf
|
|
221
|
+
});
|
|
222
|
+
for (const { app, from, to, holder } of assigned.moved) logger.log(`↪️ ${app}: ${from} is held by ${describeHolder(holder)}, which is not this workspace's — using ${to}`);
|
|
223
|
+
workspace = withAppPorts(workspace, assigned.ports);
|
|
224
|
+
workspaceAppPort = assigned.ports[appName] ?? workspaceAppPort;
|
|
225
|
+
}
|
|
226
|
+
if (appName) {
|
|
227
|
+
const { APP_TAG_ENV, appTag } = await import("./index.mjs").then((n) => n.a);
|
|
228
|
+
process.env[APP_TAG_ENV] = appTag(secretsRoot, appName);
|
|
229
|
+
}
|
|
230
|
+
const resolvedPort = options.explicitPort ?? workspaceAppPort ?? 3e3;
|
|
231
|
+
const stage = options.stage ?? workspace?.stages.local;
|
|
232
|
+
const store = stage ? workspace ? await secretsStoreFor(workspace, stage) : new FileSecretsStore(secretsRoot) : void 0;
|
|
233
|
+
const credentials = store && stage ? await loadSecretsForApp(store, stage, appName) : {};
|
|
234
|
+
credentials.PORT = String(resolvedPort);
|
|
235
|
+
if (appInfo?.app.type === "mobile") credentials.RCT_METRO_PORT = String(resolvedPort);
|
|
236
|
+
const declaredKeys = [];
|
|
237
|
+
let reconciled;
|
|
238
|
+
if (workspace) {
|
|
239
|
+
const { reconcileWorkspace } = await Promise.resolve().then(() => workspace_exports);
|
|
240
|
+
reconciled = await reconcileWorkspace(workspace, {
|
|
241
|
+
stage: options.reconcileStage ?? workspace.stages.local,
|
|
242
|
+
start: options.startDocker ?? false
|
|
243
|
+
});
|
|
244
|
+
Object.assign(credentials, reconciled.env);
|
|
245
|
+
declaredKeys.push(...Object.keys(reconciled.env));
|
|
246
|
+
if (Object.keys(reconciled.env).length > 0) logger.log(`🔌 Resolved ${Object.keys(reconciled.env).length} declared URL(s)`);
|
|
247
|
+
}
|
|
248
|
+
const secretsDir = join(secretsRoot, ".gkm");
|
|
249
|
+
await mkdir(secretsDir, { recursive: true });
|
|
250
|
+
const secretsFileName = options.secretsFileName ?? (appName ? `dev-secrets-${appName}.json` : "dev-secrets.json");
|
|
251
|
+
const secretsJsonPath = join(secretsDir, secretsFileName);
|
|
252
|
+
await writeFile(secretsJsonPath, JSON.stringify(credentials, null, 2));
|
|
290
253
|
return {
|
|
291
|
-
|
|
292
|
-
|
|
293
|
-
|
|
294
|
-
|
|
295
|
-
|
|
296
|
-
|
|
297
|
-
|
|
298
|
-
}
|
|
299
|
-
async create(bucket) {
|
|
300
|
-
await s3.send(new CreateBucketCommand({ Bucket: bucket }));
|
|
301
|
-
},
|
|
302
|
-
async policy(bucket) {
|
|
303
|
-
try {
|
|
304
|
-
const result = await s3.send(new GetBucketPolicyCommand({ Bucket: bucket }));
|
|
305
|
-
return result.Policy;
|
|
306
|
-
} catch {
|
|
307
|
-
return void 0;
|
|
308
|
-
}
|
|
309
|
-
},
|
|
310
|
-
async setPolicy(bucket, policy) {
|
|
311
|
-
await s3.send(new PutBucketPolicyCommand({
|
|
312
|
-
Bucket: bucket,
|
|
313
|
-
Policy: policy
|
|
314
|
-
}));
|
|
315
|
-
}
|
|
254
|
+
credentials,
|
|
255
|
+
resolvedPort,
|
|
256
|
+
secretsJsonPath,
|
|
257
|
+
appName,
|
|
258
|
+
secretsRoot,
|
|
259
|
+
appInfo,
|
|
260
|
+
declaredKeys,
|
|
261
|
+
...reconciled ? { reconciled } : {}
|
|
316
262
|
};
|
|
317
263
|
}
|
|
318
|
-
|
|
319
264
|
//#endregion
|
|
320
265
|
//#region src/reconcile/containers.ts
|
|
321
266
|
/**
|
|
@@ -348,7 +293,7 @@ function postgresImage(version) {
|
|
|
348
293
|
*/
|
|
349
294
|
const DEFAULT_IMAGES = {
|
|
350
295
|
postgres: postgresImage(DEFAULT_POSTGRES_VERSION),
|
|
351
|
-
minio: "
|
|
296
|
+
minio: "pgsty/minio:RELEASE.2026-08-04T00-00-00Z",
|
|
352
297
|
mailpit: "axllent/mailpit:latest",
|
|
353
298
|
redis: "redis:8-alpine",
|
|
354
299
|
"redis-http": "hiett/serverless-redis-http:latest",
|
|
@@ -376,7 +321,8 @@ const PORTS = {
|
|
|
376
321
|
}, {
|
|
377
322
|
key: "minio-console",
|
|
378
323
|
inside: 9001,
|
|
379
|
-
label: "minio console"
|
|
324
|
+
label: "minio console",
|
|
325
|
+
web: true
|
|
380
326
|
}],
|
|
381
327
|
mailpit: [{
|
|
382
328
|
key: "mailpit",
|
|
@@ -385,7 +331,8 @@ const PORTS = {
|
|
|
385
331
|
}, {
|
|
386
332
|
key: "mailpit-web",
|
|
387
333
|
inside: 8025,
|
|
388
|
-
label: "mailpit inbox"
|
|
334
|
+
label: "mailpit inbox",
|
|
335
|
+
web: true
|
|
389
336
|
}],
|
|
390
337
|
redis: [{
|
|
391
338
|
key: "redis",
|
|
@@ -404,7 +351,8 @@ const PORTS = {
|
|
|
404
351
|
}, {
|
|
405
352
|
key: "rabbitmq-management",
|
|
406
353
|
inside: 15672,
|
|
407
|
-
label: "rabbitmq console"
|
|
354
|
+
label: "rabbitmq console",
|
|
355
|
+
web: true
|
|
408
356
|
}],
|
|
409
357
|
localstack: [{
|
|
410
358
|
key: "localstack",
|
|
@@ -417,10 +365,48 @@ const PORTS = {
|
|
|
417
365
|
label: "https edge"
|
|
418
366
|
}]
|
|
419
367
|
};
|
|
420
|
-
/** The
|
|
421
|
-
function
|
|
368
|
+
/** The port key — and container name, for an image — of an API's fake. */
|
|
369
|
+
function fakeKey(id) {
|
|
370
|
+
return `${kebabCase(id)}-fake`;
|
|
371
|
+
}
|
|
372
|
+
/**
|
|
373
|
+
* The ports a container needs published. Empty for one gkm does not know.
|
|
374
|
+
*
|
|
375
|
+
* `fakes` answers for the containers no table can: an external API's fake
|
|
376
|
+
* listens wherever its declaration says.
|
|
377
|
+
*/
|
|
378
|
+
function portsOf(container, fakes = {}) {
|
|
379
|
+
const fake = fakes[container];
|
|
380
|
+
if (fake?.port !== void 0) return [{
|
|
381
|
+
key: container,
|
|
382
|
+
inside: fake.port,
|
|
383
|
+
label: `${fake.id} fake`
|
|
384
|
+
}];
|
|
422
385
|
return PORTS[container] ?? [];
|
|
423
386
|
}
|
|
387
|
+
/**
|
|
388
|
+
* Every port the containers publish — not only the one an app connects to.
|
|
389
|
+
*
|
|
390
|
+
* The consoles and the inbox are most of why these are real containers, and an
|
|
391
|
+
* inbox nobody can find is an inbox nobody opens: a sign-in link gets fished
|
|
392
|
+
* out of `docker ps` instead.
|
|
393
|
+
*/
|
|
394
|
+
function serviceAddresses(containers, ports, fakes = {}) {
|
|
395
|
+
return containers.flatMap((container) => portsOf(container, fakes).flatMap((port) => {
|
|
396
|
+
const assigned = ports[port.key];
|
|
397
|
+
if (assigned === void 0) return [];
|
|
398
|
+
return [{
|
|
399
|
+
container,
|
|
400
|
+
label: port.label,
|
|
401
|
+
address: port.web ? `http://localhost:${assigned}` : `localhost:${assigned}`
|
|
402
|
+
}];
|
|
403
|
+
}));
|
|
404
|
+
}
|
|
405
|
+
/** The lines `gkm dev` and `gkm setup` print under the services heading. */
|
|
406
|
+
function describeServices(services) {
|
|
407
|
+
const width = Math.max(0, ...services.map(({ label }) => label.length));
|
|
408
|
+
return services.map(({ label, address }) => ` ${label.padEnd(width)} ${address}`);
|
|
409
|
+
}
|
|
424
410
|
/** The key a container's primary port is allocated under. */
|
|
425
411
|
function primaryPortKey(container) {
|
|
426
412
|
return portsOf(container)[0]?.key ?? container;
|
|
@@ -431,8 +417,9 @@ function primaryPortKey(container) {
|
|
|
431
417
|
* What allocation is handed: it assigns ports to names and knows nothing about
|
|
432
418
|
* what listens on them.
|
|
433
419
|
*/
|
|
434
|
-
function portKeys(containers) {
|
|
435
|
-
|
|
420
|
+
function portKeys(containers, fakes = {}) {
|
|
421
|
+
const keys = [...containers].sort().flatMap((container) => portsOf(container, fakes).map((port) => port.key));
|
|
422
|
+
return [.../* @__PURE__ */ new Set([...keys, ...Object.keys(fakes).sort()])];
|
|
436
423
|
}
|
|
437
424
|
/**
|
|
438
425
|
* The named volume a container's data lives in.
|
|
@@ -441,945 +428,1705 @@ function portKeys(containers) {
|
|
|
441
428
|
* survives days of dev is noise rather than state.
|
|
442
429
|
*/
|
|
443
430
|
function volumeOf(container) {
|
|
444
|
-
|
|
431
|
+
return {
|
|
445
432
|
postgres: "postgres-data",
|
|
446
433
|
minio: "minio-data",
|
|
447
434
|
redis: "redis-data",
|
|
448
435
|
rabbitmq: "rabbitmq-data",
|
|
449
436
|
localstack: "localstack-data",
|
|
450
437
|
caddy: "caddy-data"
|
|
451
|
-
};
|
|
452
|
-
return volumes[container];
|
|
438
|
+
}[container];
|
|
453
439
|
}
|
|
454
|
-
|
|
455
440
|
//#endregion
|
|
456
|
-
//#region src/reconcile/
|
|
457
|
-
/** The credential every derived container is brought up with locally. */
|
|
458
|
-
const LOCAL_USER$1 = "geekmidas";
|
|
441
|
+
//#region src/reconcile/caddyfile.ts
|
|
459
442
|
/**
|
|
460
|
-
* The
|
|
443
|
+
* The kinds the edge serves.
|
|
461
444
|
*
|
|
462
|
-
*
|
|
463
|
-
*
|
|
464
|
-
*
|
|
445
|
+
* Everything that owns a *public address*. Deployed, each of these has a real
|
|
446
|
+
* hostname and a certificate, so this is what makes the local shape the same
|
|
447
|
+
* one rather than a near-enough one — and the application is indifferent
|
|
448
|
+
* either way, because it reads whichever address was injected and never
|
|
449
|
+
* composes one.
|
|
450
|
+
*
|
|
451
|
+
* Adding a kind here is the whole of adding it to the edge.
|
|
465
452
|
*/
|
|
466
|
-
const
|
|
453
|
+
const EDGE_KINDS = {
|
|
454
|
+
"file-server": true,
|
|
455
|
+
"rest-api": true,
|
|
456
|
+
site: true
|
|
457
|
+
};
|
|
467
458
|
/**
|
|
468
|
-
*
|
|
459
|
+
* How the edge reaches the host, from inside its container.
|
|
469
460
|
*
|
|
470
|
-
*
|
|
471
|
-
*
|
|
472
|
-
*
|
|
461
|
+
* A surface and a site are served by processes `gkm dev` starts on the host,
|
|
462
|
+
* not by containers — so the edge has to leave Docker's network to reach them.
|
|
463
|
+
* Docker Desktop resolves this name already; on Linux the compose definition
|
|
464
|
+
* maps it to the host gateway.
|
|
473
465
|
*/
|
|
474
|
-
|
|
475
|
-
|
|
476
|
-
|
|
477
|
-
|
|
478
|
-
|
|
479
|
-
|
|
480
|
-
|
|
481
|
-
|
|
482
|
-
|
|
483
|
-
|
|
466
|
+
const HOST_GATEWAY = "host.docker.internal";
|
|
467
|
+
/**
|
|
468
|
+
* The suffix every local host shares.
|
|
469
|
+
*
|
|
470
|
+
* `.localhost` rather than a made-up TLD because it resolves to loopback
|
|
471
|
+
* without touching `/etc/hosts` — macOS and systemd-resolved both answer for
|
|
472
|
+
* subdomains of it, and Caddy issues internal certificates for it by default.
|
|
473
|
+
*/
|
|
474
|
+
const LOCAL_TLD = "localhost";
|
|
475
|
+
/**
|
|
476
|
+
* The sites a plan implies, one per file server.
|
|
477
|
+
*
|
|
478
|
+
* The leading label is the *server's* stage-scoped name rather than the
|
|
479
|
+
* bucket's, which is the difference that matters: two servers over one bucket
|
|
480
|
+
* are a legitimate arrangement — two cache behaviours, one origin — and naming
|
|
481
|
+
* the host after the bucket would make them collide.
|
|
482
|
+
*/
|
|
483
|
+
function sitesFor(plan, project, addresses = {}) {
|
|
484
|
+
const byId = new Map(plan.resources.map((r) => [r.id, r]));
|
|
485
|
+
const sites = [];
|
|
486
|
+
for (const resource of plan.resources) {
|
|
487
|
+
if (!EDGE_KINDS[resource.kind]) continue;
|
|
488
|
+
const host = hostFor(resource, project);
|
|
489
|
+
if (resource.kind === "file-server") {
|
|
490
|
+
const origin = resource.of ? byId.get(resource.of) : void 0;
|
|
491
|
+
if (!origin) continue;
|
|
492
|
+
sites.push({
|
|
493
|
+
host,
|
|
494
|
+
upstream: "http://minio:9000",
|
|
495
|
+
rewrite: `/${origin.name}{uri}`
|
|
496
|
+
});
|
|
497
|
+
continue;
|
|
498
|
+
}
|
|
499
|
+
const address = addresses[resource.id];
|
|
500
|
+
if (!address) continue;
|
|
501
|
+
const port = portOf(address);
|
|
502
|
+
if (!port) continue;
|
|
503
|
+
sites.push({
|
|
504
|
+
host,
|
|
505
|
+
upstream: `http://${HOST_GATEWAY}:${port}`
|
|
506
|
+
});
|
|
484
507
|
}
|
|
485
|
-
return
|
|
486
|
-
name: options.project,
|
|
487
|
-
services,
|
|
488
|
-
...Object.keys(volumes).length ? { volumes } : {}
|
|
489
|
-
};
|
|
490
|
-
}
|
|
491
|
-
/** Render a compose document as the file it becomes. */
|
|
492
|
-
function toYaml(file) {
|
|
493
|
-
return `# Generated by gkm from the construct manifest — do not edit.
|
|
494
|
-
# Containers are derived from what the app declares; images and anything no
|
|
495
|
-
# construct implies are yours, in gkm.config.ts.
|
|
496
|
-
#
|
|
497
|
-
# Use "gkm dev" or "gkm test" to start these. Running "docker compose up"
|
|
498
|
-
# directly will not inject secrets.
|
|
499
|
-
${stringify(file, { lineWidth: 0 })}`;
|
|
508
|
+
return sites;
|
|
500
509
|
}
|
|
501
|
-
/**
|
|
502
|
-
|
|
503
|
-
|
|
504
|
-
|
|
505
|
-
|
|
506
|
-
|
|
507
|
-
|
|
508
|
-
super("No image is known for this container; pin one in config");
|
|
509
|
-
this.name = "UnknownContainer";
|
|
510
|
-
this.container = container;
|
|
510
|
+
/** The port a local address answers on, defaulting by scheme. */
|
|
511
|
+
function portOf(address) {
|
|
512
|
+
try {
|
|
513
|
+
const { port, protocol } = new URL(address);
|
|
514
|
+
return port || (protocol === "https:" ? "443" : "80");
|
|
515
|
+
} catch {
|
|
516
|
+
return;
|
|
511
517
|
}
|
|
512
|
-
}
|
|
513
|
-
/**
|
|
514
|
-
|
|
515
|
-
|
|
516
|
-
|
|
517
|
-
|
|
518
|
-
|
|
519
|
-
|
|
520
|
-
|
|
521
|
-
|
|
522
|
-
|
|
523
|
-
|
|
524
|
-
|
|
525
|
-
|
|
526
|
-
|
|
527
|
-
|
|
528
|
-
|
|
529
|
-
|
|
530
|
-
|
|
531
|
-
|
|
532
|
-
|
|
533
|
-
|
|
534
|
-
|
|
535
|
-
|
|
536
|
-
|
|
537
|
-
|
|
538
|
-
|
|
539
|
-
|
|
540
|
-
|
|
541
|
-
|
|
542
|
-
|
|
543
|
-
|
|
544
|
-
|
|
545
|
-
|
|
546
|
-
|
|
547
|
-
|
|
548
|
-
|
|
549
|
-
|
|
550
|
-
|
|
551
|
-
|
|
552
|
-
|
|
553
|
-
|
|
554
|
-
|
|
555
|
-
|
|
556
|
-
|
|
557
|
-
|
|
558
|
-
|
|
559
|
-
|
|
560
|
-
|
|
561
|
-
|
|
562
|
-
|
|
563
|
-
|
|
564
|
-
|
|
565
|
-
|
|
566
|
-
|
|
567
|
-
|
|
568
|
-
|
|
569
|
-
|
|
570
|
-
|
|
571
|
-
|
|
572
|
-
|
|
573
|
-
|
|
574
|
-
|
|
575
|
-
|
|
576
|
-
|
|
577
|
-
}
|
|
578
|
-
|
|
579
|
-
|
|
580
|
-
|
|
581
|
-
|
|
582
|
-
|
|
583
|
-
|
|
584
|
-
|
|
585
|
-
|
|
586
|
-
|
|
587
|
-
|
|
588
|
-
|
|
589
|
-
|
|
590
|
-
|
|
591
|
-
|
|
592
|
-
|
|
593
|
-
|
|
594
|
-
|
|
595
|
-
|
|
596
|
-
|
|
597
|
-
|
|
598
|
-
|
|
599
|
-
|
|
600
|
-
|
|
601
|
-
|
|
602
|
-
|
|
603
|
-
|
|
604
|
-
|
|
605
|
-
|
|
606
|
-
|
|
607
|
-
|
|
608
|
-
|
|
609
|
-
|
|
610
|
-
|
|
611
|
-
|
|
612
|
-
|
|
613
|
-
|
|
614
|
-
|
|
615
|
-
|
|
616
|
-
|
|
617
|
-
|
|
618
|
-
|
|
619
|
-
|
|
620
|
-
|
|
621
|
-
|
|
622
|
-
|
|
623
|
-
|
|
624
|
-
|
|
625
|
-
|
|
626
|
-
|
|
627
|
-
|
|
628
|
-
|
|
629
|
-
|
|
630
|
-
|
|
631
|
-
|
|
632
|
-
|
|
633
|
-
|
|
634
|
-
|
|
635
|
-
|
|
636
|
-
|
|
637
|
-
|
|
638
|
-
|
|
639
|
-
|
|
640
|
-
|
|
641
|
-
|
|
642
|
-
|
|
643
|
-
|
|
644
|
-
|
|
645
|
-
|
|
646
|
-
|
|
647
|
-
|
|
648
|
-
|
|
649
|
-
|
|
650
|
-
|
|
651
|
-
|
|
652
|
-
|
|
653
|
-
|
|
654
|
-
|
|
655
|
-
|
|
656
|
-
|
|
657
|
-
|
|
658
|
-
|
|
518
|
+
}
|
|
519
|
+
/**
|
|
520
|
+
* Where one file server answers, including the stage its name carries.
|
|
521
|
+
*
|
|
522
|
+
* The project is a label rather than a decoration: it is what keeps two
|
|
523
|
+
* checkouts from answering on one address. Omitted when there is no project to
|
|
524
|
+
* name, because an empty label makes `uploads..localhost` — a host nothing
|
|
525
|
+
* resolves and no certificate can be issued for.
|
|
526
|
+
*/
|
|
527
|
+
function hostFor(resource, project) {
|
|
528
|
+
return [
|
|
529
|
+
resource.root && project ? stageOf(resource) : resource.name,
|
|
530
|
+
project,
|
|
531
|
+
LOCAL_TLD
|
|
532
|
+
].filter(Boolean).join(".");
|
|
533
|
+
}
|
|
534
|
+
/** The stage a resource's name carries, or nothing for the local stage's. */
|
|
535
|
+
function stageOf(resource) {
|
|
536
|
+
const base = resource.id.toLowerCase();
|
|
537
|
+
return resource.name === base ? void 0 : resource.name.slice(base.length + 1);
|
|
538
|
+
}
|
|
539
|
+
/**
|
|
540
|
+
* The static root config, which only ever imports.
|
|
541
|
+
*
|
|
542
|
+
* Written once and identical for every stage, because the *sites* are what
|
|
543
|
+
* differ and they live in their own files. That split is not tidiness: one
|
|
544
|
+
* container serves every stage — the same way one Postgres holds `orders` and
|
|
545
|
+
* `orders_test` — so a single Caddyfile would mean `gkm test` deleting the
|
|
546
|
+
* routes `gkm dev` is serving, and each command silently breaking the other.
|
|
547
|
+
*/
|
|
548
|
+
function caddyfileRoot() {
|
|
549
|
+
return `${GENERATED}
|
|
550
|
+
{
|
|
551
|
+
# No ACME, no public DNS: the CA is local and so are the names.
|
|
552
|
+
local_certs
|
|
553
|
+
auto_https disable_redirects
|
|
554
|
+
}
|
|
555
|
+
|
|
556
|
+
# One file per stage, so two stages share this edge the way they already share
|
|
557
|
+
# one Postgres and one MinIO.
|
|
558
|
+
import /etc/caddy/sites/*.caddy
|
|
559
|
+
`;
|
|
560
|
+
}
|
|
561
|
+
/**
|
|
562
|
+
* One stage's sites, as the file imported above.
|
|
563
|
+
*
|
|
564
|
+
* `rewrite` rather than a path prefix on the address: the bucket is the
|
|
565
|
+
* origin's business, and the client asks for the key it stored.
|
|
566
|
+
*
|
|
567
|
+
* `header_up Host {upstream_hostport}` matters more than it looks. MinIO routes
|
|
568
|
+
* and signs on the Host header, so forwarding the requested hostname would make
|
|
569
|
+
* it look for a bucket named after the domain.
|
|
570
|
+
*/
|
|
571
|
+
function toCaddyfile(sites) {
|
|
572
|
+
if (sites.length === 0) return `${GENERATED}
|
|
573
|
+
# No file server is declared in this stage, so it routes nothing.
|
|
574
|
+
`;
|
|
575
|
+
const blocks = sites.map(({ host, upstream, rewrite }) => `
|
|
576
|
+
https://${host} {
|
|
577
|
+
tls internal
|
|
578
|
+
|
|
579
|
+
reverse_proxy ${upstream} {${rewrite ? `
|
|
580
|
+
rewrite ${rewrite}
|
|
581
|
+
# MinIO routes and signs on the Host header, so forwarding the requested
|
|
582
|
+
# hostname would make it look for a bucket named after the domain.
|
|
583
|
+
header_up Host {upstream_hostport}` : `
|
|
584
|
+
# The application reads whatever address was injected, so it has to see
|
|
585
|
+
# the one a caller used — that is what makes a redirect, a cookie domain
|
|
586
|
+
# and a generated link point back here rather than at the upstream.
|
|
587
|
+
header_up Host {host}
|
|
588
|
+
header_up X-Forwarded-Proto {scheme}`}
|
|
589
|
+
}
|
|
590
|
+
}`);
|
|
591
|
+
return `${GENERATED}${blocks.join("\n")}\n`;
|
|
592
|
+
}
|
|
593
|
+
const GENERATED = `# Generated by gkm from the construct manifest — do not edit.
|
|
594
|
+
#
|
|
595
|
+
# One host per declared file server, in front of the object store that holds its
|
|
596
|
+
# objects. This is the shape a file server has deployed — a domain serving a
|
|
597
|
+
# bucket — which MinIO alone cannot produce: its virtual-host mode reads the
|
|
598
|
+
# leading label as the bucket name.
|
|
599
|
+
#
|
|
600
|
+
# Certificates come from Caddy's own CA. "gkm dev" and "gkm test" export its
|
|
601
|
+
# root and point Node at it, so nothing has to be installed to use these URLs
|
|
602
|
+
# from code; a browser needs the root trusted once.
|
|
603
|
+
`;
|
|
604
|
+
//#endregion
|
|
605
|
+
//#region src/reconcile/emulator.ts
|
|
606
|
+
/**
|
|
607
|
+
* The AWS emulator (floci, LocalStack-compatible) the `sns` backend runs on
|
|
608
|
+
* locally — the facts the compose file, the env writer and the applier must
|
|
609
|
+
* agree on.
|
|
610
|
+
*
|
|
611
|
+
* The emulator has one account and answers with deterministic ARNs and queue
|
|
612
|
+
* URLs, which is what lets a connection string be composed before the topic
|
|
613
|
+
* or queue it names has been created.
|
|
614
|
+
*/
|
|
615
|
+
/** The emulator's single account. */
|
|
616
|
+
const EMULATOR_ACCOUNT = "000000000000";
|
|
617
|
+
const EMULATOR_REGION = "us-east-1";
|
|
618
|
+
/**
|
|
619
|
+
* The emulator's credential. The `LSIA` prefix is what LocalStack required on
|
|
620
|
+
* issued keys; floci does not care, and keeping it means a project that pins
|
|
621
|
+
* the old image still works.
|
|
622
|
+
*/
|
|
623
|
+
const EMULATOR_CREDENTIALS = {
|
|
624
|
+
accessKeyId: "LSIAQAAAAAAVNCBMPNSG",
|
|
625
|
+
secretAccessKey: "geekmidas"
|
|
626
|
+
};
|
|
627
|
+
/** The emulator's address from the host, on the port it was published on. */
|
|
628
|
+
function emulatorEndpoint(port) {
|
|
629
|
+
return `http://localhost:${port}`;
|
|
630
|
+
}
|
|
631
|
+
function emulatorTopicArn(name) {
|
|
632
|
+
return `arn:aws:sns:${EMULATOR_REGION}:${EMULATOR_ACCOUNT}:${name}`;
|
|
633
|
+
}
|
|
634
|
+
function emulatorQueueUrl(name, port) {
|
|
635
|
+
return `${emulatorEndpoint(port)}/${EMULATOR_ACCOUNT}/${name}`;
|
|
636
|
+
}
|
|
637
|
+
//#endregion
|
|
638
|
+
//#region src/workspace/rootSite.ts
|
|
639
|
+
/**
|
|
640
|
+
* Which site the base domain points at — deployed and locally alike.
|
|
641
|
+
*
|
|
642
|
+
* Three rules, in order, and the last one is the point:
|
|
643
|
+
*
|
|
644
|
+
* 1. **One site** — it is the root. Unambiguous, so nothing has to be said.
|
|
645
|
+
* 2. **Named `web`** — the convention wins, because people already rely on it.
|
|
646
|
+
* 3. **Declared `root: true`** — the site says so itself.
|
|
647
|
+
*
|
|
648
|
+
* Anything else throws. It used to take the *first* site it iterated, and that
|
|
649
|
+
* order came from the manifest, which is built in glob traversal order — so two
|
|
650
|
+
* sites with neither named `web` meant renaming a file could move the root
|
|
651
|
+
* domain, silently.
|
|
652
|
+
*
|
|
653
|
+
* One rule for both targets: deployed the root site gets the base domain, and
|
|
654
|
+
* locally it gets the project's bare host behind the edge. Before this was
|
|
655
|
+
* shared, the local edge put every site on a subdomain, so what a developer
|
|
656
|
+
* browsed was not the shape production served.
|
|
657
|
+
*
|
|
658
|
+
* @returns the root site's name, or `undefined` when there are no sites
|
|
659
|
+
* @throws {AmbiguousRootSite} when several sites could be the root and none says it is
|
|
660
|
+
*/
|
|
661
|
+
function rootSite(sites) {
|
|
662
|
+
if (sites.length === 0) return void 0;
|
|
663
|
+
if (sites.length === 1) return sites[0].name;
|
|
664
|
+
if (sites.some(({ name }) => name === "web")) return "web";
|
|
665
|
+
const declared = sites.filter(({ root }) => root === true);
|
|
666
|
+
if (declared.length === 1) return declared[0].name;
|
|
667
|
+
throw new AmbiguousRootSite(sites.map(({ name }) => name), declared.length > 1);
|
|
668
|
+
}
|
|
669
|
+
/** Several sites could hold the base domain, and none of them says it does. */
|
|
670
|
+
var AmbiguousRootSite = class extends Error {
|
|
671
|
+
sites;
|
|
672
|
+
tooMany;
|
|
673
|
+
constructor(sites, tooMany = false) {
|
|
674
|
+
super(tooMany ? `More than one site declares \`root: true\` — ${sites.join(", ")}. The base domain points at one of them.` : `${sites.length} sites and nothing says which holds the base domain: ${sites.join(", ")}. Name one of them \`web\`, or declare \`root: true\` on the one the base domain points at. It used to be whichever the glob reached first, which is not a thing to decide a hostname.`);
|
|
675
|
+
this.sites = sites;
|
|
676
|
+
this.tooMany = tooMany;
|
|
677
|
+
this.name = "AmbiguousRootSite";
|
|
678
|
+
}
|
|
679
|
+
};
|
|
680
|
+
//#endregion
|
|
681
|
+
//#region src/reconcile/plan.ts
|
|
682
|
+
/**
|
|
683
|
+
* The reconcile plan — what a manifest wants to exist, for one stage.
|
|
684
|
+
*
|
|
685
|
+
* Pure. Turning the plan into running containers and provisioned resources is
|
|
686
|
+
* the applier's job; deciding *what* should exist is this, so the decisions can
|
|
687
|
+
* be asserted as data rather than by starting Docker.
|
|
688
|
+
*
|
|
689
|
+
* One plan serves every stage. `gkm dev` reconciles `development` and `gkm test`
|
|
690
|
+
* reconciles `test`; they differ only in what the resources are called, never in
|
|
691
|
+
* what infrastructure exists. Roles are cluster-scoped in Postgres while grants
|
|
692
|
+
* are per-object, so one container and one role pair serve both.
|
|
693
|
+
*/
|
|
694
|
+
/**
|
|
695
|
+
* Which container serves a kind.
|
|
696
|
+
*
|
|
697
|
+
* Derived kinds map to the same container as their parent — a schema tenant
|
|
698
|
+
* lives in its parent's Postgres — so the set is deduplicated rather than
|
|
699
|
+
* counted.
|
|
700
|
+
*/
|
|
701
|
+
const CONTAINERS = {
|
|
702
|
+
database: "postgres",
|
|
703
|
+
"database-reader": "postgres",
|
|
704
|
+
"database-schema": "postgres",
|
|
705
|
+
objects: "minio",
|
|
706
|
+
"file-server": "caddy",
|
|
707
|
+
email: "mailpit"
|
|
708
|
+
};
|
|
709
|
+
/**
|
|
710
|
+
* The container a cache backend needs locally.
|
|
711
|
+
*
|
|
712
|
+
* Each one matches the protocol its deployed form speaks, which is the property
|
|
713
|
+
* worth having: a cache that behaves differently in the two places is worse than
|
|
714
|
+
* a slower one.
|
|
715
|
+
*
|
|
716
|
+
* `db` is deliberately absent, the same way `pgboss` is absent from
|
|
717
|
+
* `EVENT_CONTAINERS`: the cache is a table in a database the manifest already
|
|
718
|
+
* declares, so mapping it here would start a container for a project that needs
|
|
719
|
+
* none.
|
|
720
|
+
*/
|
|
721
|
+
const CACHE_CONTAINERS = {
|
|
722
|
+
upstash: "redis-http",
|
|
723
|
+
elasticache: "redis"
|
|
724
|
+
};
|
|
725
|
+
/**
|
|
726
|
+
* Kinds that resolve a value without anything running.
|
|
727
|
+
*
|
|
728
|
+
* A secret is the case: it has no address, so there is nothing to start and
|
|
729
|
+
* nothing to connect to — but it still appears in the plan, because the target
|
|
730
|
+
* still has to resolve it.
|
|
731
|
+
*/
|
|
732
|
+
const CONTAINERLESS = {
|
|
733
|
+
secret: true,
|
|
734
|
+
credential: true,
|
|
735
|
+
encryption: true,
|
|
736
|
+
"rest-api": true,
|
|
737
|
+
site: true,
|
|
738
|
+
"mobile-app": true,
|
|
739
|
+
"external-api": true
|
|
740
|
+
};
|
|
741
|
+
/**
|
|
742
|
+
* Containers that cannot run alone.
|
|
743
|
+
*
|
|
744
|
+
* The one case is the cache: `serverless-redis-http` is a proxy and needs the
|
|
745
|
+
* Redis it proxies. Kept here rather than in the compose definition so the plan
|
|
746
|
+
* -- which is what decides what must be running -- still names everything.
|
|
747
|
+
*/
|
|
748
|
+
const REQUIRES = { "redis-http": ["redis"] };
|
|
749
|
+
/**
|
|
750
|
+
* The role each kind's one key plays, where it is not a URL.
|
|
751
|
+
*
|
|
752
|
+
* The twin of `ProvidesByKind` in `@geekmidas/manifest`: that is what a
|
|
753
|
+
* construct declares, this is what the local target resolves, and both derive
|
|
754
|
+
* the key from the same `provideKey` so they cannot disagree.
|
|
755
|
+
*/
|
|
756
|
+
const ROLES = {
|
|
757
|
+
queue: "publisherConnectionString",
|
|
758
|
+
topic: "publisherConnectionString",
|
|
759
|
+
credential: "credentials",
|
|
760
|
+
"mobile-app": "scheme"
|
|
761
|
+
};
|
|
762
|
+
/** The kinds whose container is the events backend's rather than their own. */
|
|
763
|
+
const EVENT_KINDS = {
|
|
764
|
+
queue: true,
|
|
765
|
+
topic: true
|
|
766
|
+
};
|
|
767
|
+
/**
|
|
768
|
+
* Which kinds provision something *inside* their container.
|
|
769
|
+
*
|
|
770
|
+
* A database creates a database, a bucket creates a bucket; mail creates
|
|
771
|
+
* nothing — Mailpit accepts whatever is sent to it and has no per-stage object
|
|
772
|
+
* to name. Both still appear in the plan, because both still resolve a URL; the
|
|
773
|
+
* flag is the difference between "must be running" and "must be created".
|
|
774
|
+
*/
|
|
775
|
+
const PROVISIONS = {
|
|
776
|
+
database: true,
|
|
777
|
+
"database-reader": true,
|
|
778
|
+
"database-schema": true,
|
|
779
|
+
objects: true,
|
|
780
|
+
cache: true,
|
|
781
|
+
"file-server": true
|
|
782
|
+
};
|
|
783
|
+
/**
|
|
784
|
+
* The container each events backend needs, where it needs one of its own.
|
|
785
|
+
*
|
|
786
|
+
* `pgboss` is deliberately absent rather than mapped to `postgres`: it is a
|
|
787
|
+
* schema tenant in a database the manifest already declares, so mapping it here
|
|
788
|
+
* would start a Postgres for a project that declared no database at all.
|
|
789
|
+
*
|
|
790
|
+
* This one comes from config rather than from a declaration because there are
|
|
791
|
+
* no `topic` or `queue` kinds yet. When they land the mapping moves to
|
|
792
|
+
* {@link CONTAINERS} keyed by kind, and this constant goes with them.
|
|
793
|
+
*/
|
|
794
|
+
const EVENT_CONTAINERS = {
|
|
795
|
+
sns: "localstack",
|
|
796
|
+
rabbitmq: "rabbitmq"
|
|
797
|
+
};
|
|
798
|
+
/** The backend a project gets when it declares queues or topics and says nothing. */
|
|
799
|
+
const DEFAULT_EVENTS = "pgboss";
|
|
800
|
+
/**
|
|
801
|
+
* How a resource of each kind separates its stage suffix.
|
|
802
|
+
*
|
|
803
|
+
* Postgres identifiers are snake_case and a hyphen would need quoting
|
|
804
|
+
* everywhere; buckets and queues are kebab and disallow underscores in some
|
|
805
|
+
* providers. The suffix is the same word either way.
|
|
806
|
+
*/
|
|
807
|
+
const SEPARATORS = {
|
|
808
|
+
database: "_",
|
|
809
|
+
"database-reader": "_",
|
|
810
|
+
"database-schema": "_",
|
|
811
|
+
objects: "-"
|
|
812
|
+
};
|
|
813
|
+
/**
|
|
814
|
+
* The stage-scoped name for a resource.
|
|
815
|
+
*
|
|
816
|
+
* The local stage carries no suffix, so `gkm dev` keeps using `orders` and a
|
|
817
|
+
* developer's psql history, saved connections, and muscle memory survive a
|
|
818
|
+
* change of stage name. Every other stage is suffixed.
|
|
819
|
+
*/
|
|
820
|
+
function resourceName(id, kind, stage, localStage) {
|
|
821
|
+
const base = id.toLowerCase();
|
|
822
|
+
if (stage === localStage) return base;
|
|
823
|
+
return `${base}${SEPARATORS[kind] ?? "-"}${stage}`;
|
|
824
|
+
}
|
|
825
|
+
/**
|
|
826
|
+
* The container a kind needs, or `undefined` if it needs none.
|
|
827
|
+
*
|
|
828
|
+
* Queues and topics have no container of their own — they live in whichever
|
|
829
|
+
* broker the project selected, which is why the backend is an argument here
|
|
830
|
+
* rather than a second pass that adds containers after the fact.
|
|
831
|
+
*/
|
|
832
|
+
function containerFor(kind, events = DEFAULT_EVENTS, cache = DEFAULT_CACHE.aws, derived = false, edge = true) {
|
|
833
|
+
if (EVENT_KINDS[kind]) return EVENT_CONTAINERS[events] ?? CONTAINERS.database;
|
|
834
|
+
if (kind === "file-server") return edge ? "caddy" : CONTAINERS.objects;
|
|
835
|
+
if (kind === "cache") {
|
|
836
|
+
if (derived) return CONTAINERS.database;
|
|
837
|
+
return CACHE_CONTAINERS[cache] ?? CONTAINERS.database;
|
|
838
|
+
}
|
|
839
|
+
return CONTAINERS[kind];
|
|
840
|
+
}
|
|
841
|
+
/**
|
|
842
|
+
* Build the plan for a manifest and stage.
|
|
843
|
+
*
|
|
844
|
+
* `order` is the caller's provisioning order — parents before children — which
|
|
845
|
+
* `provisionOrder` produces from the manifest. Passing it in keeps this
|
|
846
|
+
* function free of the traversal and makes the ordering assertable on its own.
|
|
847
|
+
*/
|
|
848
|
+
function planFor(manifest, stage, order, options = {}) {
|
|
849
|
+
const containers = /* @__PURE__ */ new Set();
|
|
850
|
+
const resources = [];
|
|
851
|
+
const fakes = {};
|
|
852
|
+
const events = options.events ?? "pgboss";
|
|
853
|
+
const cache = options.cache ?? DEFAULT_CACHE.aws;
|
|
854
|
+
for (const id of order) {
|
|
855
|
+
const declaration = manifest[id];
|
|
856
|
+
if (!declaration) continue;
|
|
857
|
+
const local = declaration.kind === "external-api" ? options.fakes?.[id] : void 0;
|
|
858
|
+
const fake = local && {
|
|
859
|
+
key: fakeKey(id),
|
|
860
|
+
...local
|
|
659
861
|
};
|
|
660
|
-
|
|
661
|
-
|
|
662
|
-
|
|
663
|
-
|
|
664
|
-
|
|
665
|
-
|
|
666
|
-
AWS_ACCESS_KEY_ID: "LSIAQAAAAAAVNCBMPNSG",
|
|
667
|
-
AWS_SECRET_ACCESS_KEY: LOCAL_USER$1
|
|
668
|
-
},
|
|
669
|
-
volumes: ["localstack-data:/var/lib/localstack"],
|
|
670
|
-
healthcheck: {
|
|
671
|
-
test: [
|
|
672
|
-
"CMD",
|
|
673
|
-
"curl",
|
|
674
|
-
"-f",
|
|
675
|
-
"http://localhost:4566/_localstack/health"
|
|
676
|
-
],
|
|
677
|
-
interval: "10s",
|
|
678
|
-
timeout: "5s",
|
|
679
|
-
retries: 5,
|
|
680
|
-
start_period: "10s"
|
|
681
|
-
}
|
|
862
|
+
if (fake) fakes[fake.key] = {
|
|
863
|
+
id,
|
|
864
|
+
...fake.image ? {
|
|
865
|
+
image: fake.image,
|
|
866
|
+
port: fake.port
|
|
867
|
+
} : {}
|
|
682
868
|
};
|
|
683
|
-
|
|
869
|
+
const container = fake?.image ? fake.key : containerFor(declaration.kind, events, cache, "of" in declaration && typeof declaration.of === "string", options.edge !== false);
|
|
870
|
+
if (!container && !CONTAINERLESS[declaration.kind]) continue;
|
|
871
|
+
if (container) {
|
|
872
|
+
containers.add(container);
|
|
873
|
+
for (const required of REQUIRES[container] ?? []) containers.add(required);
|
|
874
|
+
}
|
|
875
|
+
resources.push({
|
|
876
|
+
id,
|
|
877
|
+
kind: declaration.kind,
|
|
878
|
+
...container ? { container } : {},
|
|
879
|
+
name: resourceName(id, declaration.kind, stage, options.localStage),
|
|
880
|
+
envKey: providedKeyFor(id, declaration.kind, ROLES[declaration.kind] ?? "url"),
|
|
881
|
+
provisions: PROVISIONS[declaration.kind] === true,
|
|
882
|
+
...declaration.kind === "rest-api" ? { callers: dependentsOf(manifest, id) } : {},
|
|
883
|
+
...declaration.kind === "site" ? { publicEnv: publicEnvFor(declaration, manifest) } : {},
|
|
884
|
+
...declaration.kind === "mobile-app" ? {
|
|
885
|
+
publicEnv: publicEnvFor(declaration, manifest),
|
|
886
|
+
...declaration.scheme ? { scheme: declaration.scheme } : {}
|
|
887
|
+
} : {},
|
|
888
|
+
...declaration.kind === "file-server" && declaration.open?.length ? { open: declaration.open } : {},
|
|
889
|
+
...fake ? { fake: {
|
|
890
|
+
key: fake.key,
|
|
891
|
+
credentials: fake.credentials
|
|
892
|
+
} } : {},
|
|
893
|
+
...declaration.kind === "external-api" && !fake ? { url: declaration.url } : {},
|
|
894
|
+
..."of" in declaration ? { of: declaration.of } : {},
|
|
895
|
+
..."schema" in declaration && declaration.schema ? { schema: declaration.schema } : {},
|
|
896
|
+
..."version" in declaration && declaration.version ? { version: declaration.version } : {},
|
|
897
|
+
..."roles" in declaration && declaration.roles === false ? { roles: false } : {},
|
|
898
|
+
..."table" in declaration && declaration.table ? { table: declaration.table } : {}
|
|
899
|
+
});
|
|
900
|
+
}
|
|
901
|
+
if (options.edge !== false && resources.some((r) => EDGE_KINDS[r.kind] === true)) containers.add("caddy");
|
|
902
|
+
for (const extra of options.extraContainers ?? []) containers.add(extra);
|
|
903
|
+
if (events === "pgboss" && resources.some((r) => EVENT_KINDS[r.kind]) && !resources.some((r) => r.kind === "database")) throw new PgBossNeedsDatabase(resources.filter((r) => EVENT_KINDS[r.kind]).map((r) => r.id));
|
|
904
|
+
if (cache === "db") {
|
|
905
|
+
const placeless = resources.filter((r) => r.kind === "cache" && !r.of);
|
|
906
|
+
if (placeless.length > 0) {
|
|
907
|
+
const databases = resources.filter((r) => r.kind === "database");
|
|
908
|
+
const [only] = databases;
|
|
909
|
+
if (!only) throw new CacheNeedsDatabase(resources.filter((r) => r.kind === "cache").map((r) => r.id));
|
|
910
|
+
if (databases.length > 1) throw new CacheIsAmbiguous(placeless.map((r) => r.id), databases.map((r) => r.id));
|
|
911
|
+
for (const resource of placeless) resource.of = only.id;
|
|
912
|
+
}
|
|
684
913
|
}
|
|
914
|
+
const sites = resources.filter((r) => r.kind === "site");
|
|
915
|
+
const root = rootSite(sites.map((r) => {
|
|
916
|
+
const declared = manifest[r.id];
|
|
917
|
+
return {
|
|
918
|
+
name: appKey(r.id),
|
|
919
|
+
...declared?.kind === "site" && declared.root ? { root: true } : {}
|
|
920
|
+
};
|
|
921
|
+
}));
|
|
922
|
+
for (const site of sites) if (appKey(site.id) === root) site.root = true;
|
|
923
|
+
const workerBroker = events === "pgboss" && Object.values(manifest).some((d) => d?.kind === "worker") && resources.some((r) => r.kind === "database");
|
|
924
|
+
return {
|
|
925
|
+
stage,
|
|
926
|
+
events,
|
|
927
|
+
cache,
|
|
928
|
+
containers: [...containers],
|
|
929
|
+
resources,
|
|
930
|
+
...Object.keys(fakes).length ? { fakes } : {},
|
|
931
|
+
...workerBroker ? { workerBroker: true } : {}
|
|
932
|
+
};
|
|
685
933
|
}
|
|
686
934
|
/**
|
|
687
|
-
*
|
|
935
|
+
* Queues or topics were declared with pg-boss and no database to hold them.
|
|
688
936
|
*
|
|
689
|
-
*
|
|
690
|
-
*
|
|
691
|
-
*
|
|
937
|
+
* Not a reconcile failure but a manifest one: the fix is a line of application
|
|
938
|
+
* code either way — declare a database, or select a backend that brings its own
|
|
939
|
+
* broker.
|
|
692
940
|
*/
|
|
693
|
-
|
|
694
|
-
|
|
695
|
-
|
|
696
|
-
|
|
697
|
-
|
|
698
|
-
|
|
699
|
-
}
|
|
700
|
-
/** A planned port that allocation never assigned. */
|
|
701
|
-
var UnassignedPort = class extends Error {
|
|
702
|
-
/** The port key that went unassigned, e.g. `minio-console`. */
|
|
703
|
-
key;
|
|
704
|
-
constructor(key) {
|
|
705
|
-
super("No port was assigned for this container port");
|
|
706
|
-
this.name = "UnassignedPort";
|
|
707
|
-
this.key = key;
|
|
941
|
+
var CacheNeedsDatabase = class extends Error {
|
|
942
|
+
ids;
|
|
943
|
+
constructor(ids) {
|
|
944
|
+
super(`A cache backed by the database needs a declared database, and none was declared, and on this deploy target that is where a cache lives. Declare one. Caches affected: ${ids.join(", ")}.`);
|
|
945
|
+
this.ids = ids;
|
|
946
|
+
this.name = "CacheNeedsDatabase";
|
|
708
947
|
}
|
|
709
948
|
};
|
|
710
|
-
|
|
711
|
-
|
|
712
|
-
|
|
713
|
-
|
|
714
|
-
|
|
715
|
-
|
|
716
|
-
|
|
717
|
-
|
|
718
|
-
|
|
719
|
-
|
|
720
|
-
|
|
721
|
-
|
|
722
|
-
|
|
723
|
-
|
|
724
|
-
|
|
725
|
-
|
|
726
|
-
|
|
727
|
-
|
|
728
|
-
|
|
729
|
-
|
|
730
|
-
|
|
731
|
-
|
|
732
|
-
|
|
733
|
-
|
|
734
|
-
|
|
735
|
-
|
|
736
|
-
|
|
737
|
-
composePath,
|
|
738
|
-
"up",
|
|
739
|
-
"-d",
|
|
740
|
-
"--remove-orphans",
|
|
741
|
-
"--wait",
|
|
742
|
-
`--wait-timeout=${Math.floor(HEALTH_TIMEOUT_MS / 1e3)}`,
|
|
743
|
-
...services
|
|
744
|
-
]);
|
|
745
|
-
},
|
|
746
|
-
async copyOut(composePath, service, from, to) {
|
|
747
|
-
await mkdir(dirname(to), { recursive: true });
|
|
748
|
-
await run("docker", [
|
|
749
|
-
"compose",
|
|
750
|
-
"-f",
|
|
751
|
-
composePath,
|
|
752
|
-
"cp",
|
|
753
|
-
`${service}:${from}`,
|
|
754
|
-
to
|
|
755
|
-
]);
|
|
756
|
-
},
|
|
757
|
-
async reload(composePath, service) {
|
|
758
|
-
await run("docker", [
|
|
759
|
-
"compose",
|
|
760
|
-
"-f",
|
|
761
|
-
composePath,
|
|
762
|
-
"exec",
|
|
763
|
-
"-T",
|
|
764
|
-
service,
|
|
765
|
-
"caddy",
|
|
766
|
-
"reload",
|
|
767
|
-
"--config",
|
|
768
|
-
"/etc/caddy/Caddyfile"
|
|
769
|
-
]);
|
|
770
|
-
},
|
|
771
|
-
async healthy(composePath, services) {
|
|
772
|
-
if (services.length === 0) return true;
|
|
773
|
-
try {
|
|
774
|
-
const { stdout } = await run("docker", [
|
|
775
|
-
"compose",
|
|
776
|
-
"-f",
|
|
777
|
-
composePath,
|
|
778
|
-
"ps",
|
|
779
|
-
"--format",
|
|
780
|
-
"json",
|
|
781
|
-
...services
|
|
782
|
-
]);
|
|
783
|
-
const running = /* @__PURE__ */ new Set();
|
|
784
|
-
for (const line of stdout.trim().split("\n").filter(Boolean)) {
|
|
785
|
-
const entry = JSON.parse(line);
|
|
786
|
-
const ok = entry.State === "running" && (entry.Health === void 0 || entry.Health === "" || entry.Health === "healthy");
|
|
787
|
-
if (ok && entry.Service) running.add(entry.Service);
|
|
788
|
-
}
|
|
789
|
-
return services.every((service) => running.has(service));
|
|
790
|
-
} catch {
|
|
791
|
-
return false;
|
|
792
|
-
}
|
|
949
|
+
/**
|
|
950
|
+
* A `'db'` cache — a server target's default — in an app that declares more
|
|
951
|
+
* than one database.
|
|
952
|
+
*
|
|
953
|
+
* The target says *a* database and the graph offers several, so there is no
|
|
954
|
+
* answer to give — and the wrong kind of failure would be to pick one, since a
|
|
955
|
+
* cache silently landing in the wrong database is a bug that surfaces as
|
|
956
|
+
* missing rows much later. The fix is a stronger statement in application code:
|
|
957
|
+
* `orders.cache('Sessions')` names the database, which is a fact about the
|
|
958
|
+
* application rather than a deployment choice.
|
|
959
|
+
*/
|
|
960
|
+
var CacheIsAmbiguous = class extends Error {
|
|
961
|
+
ids;
|
|
962
|
+
databases;
|
|
963
|
+
constructor(ids, databases) {
|
|
964
|
+
super(`A cache backed by the database needs to know which one, and this app declares ${databases.length}: ${databases.join(", ")}. Declare the cache from its database — e.g. ${databases[0]}.cache('Sessions') — rather than as a bare new Cache(). Caches affected: ${ids.join(", ")}.`);
|
|
965
|
+
this.ids = ids;
|
|
966
|
+
this.databases = databases;
|
|
967
|
+
this.name = "CacheIsAmbiguous";
|
|
968
|
+
}
|
|
969
|
+
};
|
|
970
|
+
var PgBossNeedsDatabase = class extends Error {
|
|
971
|
+
ids;
|
|
972
|
+
constructor(ids) {
|
|
973
|
+
super(`pg-boss keeps its queues in a declared database, and none was declared. Declare one: on this deploy target, events are carried by pg-boss. Queues and topics affected: ${ids.join(", ")}.`);
|
|
974
|
+
this.ids = ids;
|
|
975
|
+
this.name = "PgBossNeedsDatabase";
|
|
793
976
|
}
|
|
794
977
|
};
|
|
795
|
-
|
|
796
978
|
//#endregion
|
|
797
|
-
//#region src/reconcile/
|
|
798
|
-
/** The stage whose resources carry no suffix. */
|
|
799
|
-
const DEFAULT_STAGE = "development";
|
|
979
|
+
//#region src/reconcile/env.ts
|
|
800
980
|
/**
|
|
801
|
-
*
|
|
981
|
+
* The URLs a reconciled stage injects.
|
|
802
982
|
*
|
|
803
|
-
*
|
|
804
|
-
*
|
|
805
|
-
*
|
|
983
|
+
* One `<NAME>_URL` per construct, and the protocol picks the driver. This is the
|
|
984
|
+
* seam between the target that writes a URL and the client that parses it, so
|
|
985
|
+
* every component of it is read off the resource — never inherited from ambient
|
|
986
|
+
* environment. A bucket can live in a different region than the function reading
|
|
987
|
+
* it, and `AWS_REGION` in a Lambda is the *function's* region, so a URL that
|
|
988
|
+
* omits `?region=` breaks cross-region silently at runtime.
|
|
989
|
+
*
|
|
990
|
+
* Locally that self-containment is what lets the port be whatever was free: the
|
|
991
|
+
* app reads `ORDERS_URL` and never sees a host or a port.
|
|
806
992
|
*/
|
|
807
|
-
const CONTAINERS = {
|
|
808
|
-
database: "postgres",
|
|
809
|
-
"database-reader": "postgres",
|
|
810
|
-
"database-schema": "postgres",
|
|
811
|
-
objects: "minio",
|
|
812
|
-
"file-server": "caddy",
|
|
813
|
-
email: "mailpit"
|
|
814
|
-
};
|
|
815
993
|
/**
|
|
816
|
-
* The
|
|
994
|
+
* The credential local containers are brought up with.
|
|
817
995
|
*
|
|
818
|
-
*
|
|
819
|
-
*
|
|
820
|
-
*
|
|
996
|
+
* The design's `roles: false` case — both URLs fall back to the cluster's master
|
|
997
|
+
* credential. A deliberate downgrade, not a default: it holds until the role DDL
|
|
998
|
+
* lands in `@geekmidas/db`, at which point the runtime role's URL replaces this
|
|
999
|
+
* and the owner URL stops being reachable from any manifest edge.
|
|
1000
|
+
*/
|
|
1001
|
+
const LOCAL_USER$2 = "geekmidas";
|
|
1002
|
+
/** The host containers are published on. */
|
|
1003
|
+
const LOCAL_HOST = "localhost";
|
|
1004
|
+
/** The region local buckets claim, so the client has one to sign with. */
|
|
1005
|
+
const LOCAL_REGION = "us-east-1";
|
|
1006
|
+
/**
|
|
1007
|
+
* The credentials the S3 client resolves for MinIO.
|
|
821
1008
|
*
|
|
822
|
-
* `
|
|
823
|
-
*
|
|
824
|
-
*
|
|
825
|
-
*
|
|
1009
|
+
* An `s3://` URL deliberately carries none: deployed, the SDK reads them from
|
|
1010
|
+
* the execution role, so a URL that embedded a key would be one more thing to
|
|
1011
|
+
* rotate and one more thing to leak into a log. Locally there is no role, and
|
|
1012
|
+
* the same chain reads these — which is why they are injected beside the URL
|
|
1013
|
+
* rather than written into it.
|
|
826
1014
|
*/
|
|
827
|
-
const
|
|
828
|
-
|
|
829
|
-
|
|
1015
|
+
const STORAGE_CREDENTIALS = {
|
|
1016
|
+
AWS_ACCESS_KEY_ID: LOCAL_USER$2,
|
|
1017
|
+
AWS_SECRET_ACCESS_KEY: LOCAL_USER$2,
|
|
1018
|
+
AWS_REGION: LOCAL_REGION
|
|
830
1019
|
};
|
|
1020
|
+
/** The token the local cache proxy accepts. Matches the compose definition. */
|
|
1021
|
+
const LOCAL_TOKEN$1 = "geekmidas";
|
|
1022
|
+
/** The port Mailpit's inbox — its web UI and HTTP API — is published under. */
|
|
1023
|
+
const MAILPIT_INBOX_PORT = "mailpit-web";
|
|
1024
|
+
/** The schema pg-boss keeps its tables in, inside the declared database. */
|
|
1025
|
+
const PGBOSS_SCHEMA = "pgboss";
|
|
1026
|
+
/** The exchange every local topic and queue shares on RabbitMQ. */
|
|
1027
|
+
const RABBITMQ_EXCHANGE = "geekmidas.events";
|
|
831
1028
|
/**
|
|
832
|
-
*
|
|
1029
|
+
* Every env key a reconciled plan resolves, and its value.
|
|
1030
|
+
*
|
|
1031
|
+
* Keys come from the declaration rather than being re-derived, so what the build
|
|
1032
|
+
* publishes and what the app reads cannot drift.
|
|
1033
|
+
*/
|
|
1034
|
+
function envFor(plan, options) {
|
|
1035
|
+
const env = {};
|
|
1036
|
+
const resolved = {};
|
|
1037
|
+
for (const resource of plan.resources) {
|
|
1038
|
+
const url = urlFor(resource, plan, options.ports, options.project ?? "", options.addresses);
|
|
1039
|
+
if (url) resolved[resource.id] = url;
|
|
1040
|
+
}
|
|
1041
|
+
for (const resource of plan.resources) {
|
|
1042
|
+
const url = resolved[resource.id];
|
|
1043
|
+
if (resource.kind === "rest-api") Object.assign(env, surfaceEnv(resource, url, resolved, {
|
|
1044
|
+
plan,
|
|
1045
|
+
...options.addresses ? { addresses: options.addresses } : {},
|
|
1046
|
+
...options.lanAddress ? { lanAddress: options.lanAddress } : {},
|
|
1047
|
+
...options.metroPorts ? { metroPorts: options.metroPorts } : {}
|
|
1048
|
+
}));
|
|
1049
|
+
if (url) env[resource.envKey] = url;
|
|
1050
|
+
if (isDatabase(resource.kind) && resource.roles !== false) {
|
|
1051
|
+
const owner = ownerUrl(resource, plan, options.ports, options.project ?? "");
|
|
1052
|
+
if (owner) env[provideKey(resource.id, "ownerUrl")] = owner;
|
|
1053
|
+
}
|
|
1054
|
+
if (resource.kind === "external-api" && url && resource.fake) env[provideKey(resource.id, "credentials")] = resource.fake.credentials;
|
|
1055
|
+
if (resource.kind === "email" && url) {
|
|
1056
|
+
env[provideKey(resource.id, "from")] = options.mailFrom ?? `noreply@${LOCAL_HOST}`;
|
|
1057
|
+
const inbox = options.ports[MAILPIT_INBOX_PORT];
|
|
1058
|
+
if (inbox !== void 0) env[provideKey(resource.id, "inboxUrl")] = `http://${LOCAL_HOST}:${inbox}`;
|
|
1059
|
+
}
|
|
1060
|
+
}
|
|
1061
|
+
Object.assign(env, publicEnv(plan, env, options.addresses));
|
|
1062
|
+
Object.assign(env, brokerEnv(plan, options.ports));
|
|
1063
|
+
if (plan.resources.some((r) => r.kind === "objects" && env[r.envKey])) Object.assign(env, STORAGE_CREDENTIALS);
|
|
1064
|
+
return env;
|
|
1065
|
+
}
|
|
1066
|
+
/**
|
|
1067
|
+
* The values a site's bundler inlines, under the names it inlines them by.
|
|
1068
|
+
*
|
|
1069
|
+
* A rename and nothing more. `API_URL` was resolved once, by the same code that
|
|
1070
|
+
* resolved it for the server; a site reads the same value under `VITE_API_URL`
|
|
1071
|
+
* because that prefix is how its bundler is told to ship it. Nothing is derived
|
|
1072
|
+
* twice, so a site and its API cannot come to disagree about where the API is.
|
|
1073
|
+
*
|
|
1074
|
+
* A source that resolved to nothing is skipped rather than written empty: an
|
|
1075
|
+
* inlined empty string is a frontend that builds and then fails at runtime
|
|
1076
|
+
* against `http:///`, where a missing variable fails at build with the name of
|
|
1077
|
+
* the thing that is missing.
|
|
1078
|
+
*/
|
|
1079
|
+
function publicEnv(plan, resolved, addresses = {}) {
|
|
1080
|
+
const env = {};
|
|
1081
|
+
const owners = new Map(plan.resources.map((r) => [r.envKey, r.id]));
|
|
1082
|
+
for (const resource of plan.resources) for (const [key, source] of Object.entries(resource.publicEnv ?? {})) {
|
|
1083
|
+
const value = (resource.kind === "mobile-app" ? addresses[owners.get(source) ?? ""] : void 0) ?? resolved[source];
|
|
1084
|
+
if (value) env[key] = value;
|
|
1085
|
+
}
|
|
1086
|
+
return env;
|
|
1087
|
+
}
|
|
1088
|
+
/**
|
|
1089
|
+
* What a surface publishes beyond its own address: who may call it, and where
|
|
1090
|
+
* a cookie set by it is readable.
|
|
833
1091
|
*
|
|
834
|
-
*
|
|
835
|
-
*
|
|
836
|
-
*
|
|
1092
|
+
* Both are read off the same list — the constructs that declared an edge to
|
|
1093
|
+
* this surface — which is why neither appears in application code. A surface
|
|
1094
|
+
* that listed its own callers would be edited every time something new called
|
|
1095
|
+
* it, and the thing being edited is already recorded in the graph.
|
|
1096
|
+
*
|
|
1097
|
+
* An empty origin list is written, and a surface with no address writes nothing
|
|
1098
|
+
* at all. The two cases look alike and are not: "nothing depends on this yet" is
|
|
1099
|
+
* a real state a target should publish, while "this surface has no address here"
|
|
1100
|
+
* means it is not running in this stage, and keys belong with the address they
|
|
1101
|
+
* describe.
|
|
837
1102
|
*/
|
|
838
|
-
|
|
839
|
-
|
|
840
|
-
|
|
841
|
-
|
|
842
|
-
|
|
843
|
-
|
|
1103
|
+
function surfaceEnv(resource, url, resolved = {}, local = {}) {
|
|
1104
|
+
if (!url) return {};
|
|
1105
|
+
const origins = [...new Set((resource.callers ?? []).map((caller) => resolved[caller]).filter((address) => Boolean(address)).map(originOf).filter((origin) => Boolean(origin)))].sort();
|
|
1106
|
+
const domain = cookieDomain([url, ...origins]);
|
|
1107
|
+
const mobile = (resource.callers ?? []).map((caller) => local.plan?.resources.find((r) => r.id === caller && r.kind === "mobile-app")).filter((caller) => Boolean(caller));
|
|
1108
|
+
const hosts = [...local.lanAddress ? [local.lanAddress] : [], LOCAL_HOST];
|
|
1109
|
+
const schemes = mobile.flatMap((caller) => {
|
|
1110
|
+
const scheme = resolved[caller.id];
|
|
1111
|
+
const port = local.metroPorts?.[caller.id];
|
|
1112
|
+
return scheme ? mobileOrigins(scheme, {
|
|
1113
|
+
hosts,
|
|
1114
|
+
...port ? { port } : {}
|
|
1115
|
+
}) : [];
|
|
1116
|
+
});
|
|
1117
|
+
const own = local.addresses?.[resource.id];
|
|
1118
|
+
const device = mobile.length > 0 && local.lanAddress && own ? own.replace(/\/\/(localhost|127\.0\.0\.1)(?=[:/]|$)/, `//${local.lanAddress}`) : void 0;
|
|
1119
|
+
return {
|
|
1120
|
+
[provideKey(resource.id, "trustedOrigins")]: [...origins, ...new Set(schemes)].join(","),
|
|
1121
|
+
...domain ? { [provideKey(resource.id, "cookieDomain")]: domain } : {},
|
|
1122
|
+
...device ? { [provideKey(resource.id, "deviceUrl")]: device } : {}
|
|
1123
|
+
};
|
|
1124
|
+
}
|
|
1125
|
+
/** An address reduced to the origin a browser compares against. */
|
|
1126
|
+
function originOf(address) {
|
|
1127
|
+
try {
|
|
1128
|
+
return new URL(address).origin;
|
|
1129
|
+
} catch {
|
|
1130
|
+
return;
|
|
1131
|
+
}
|
|
1132
|
+
}
|
|
844
1133
|
/**
|
|
845
|
-
*
|
|
1134
|
+
* The runtime role of whatever a derived node hangs off.
|
|
846
1135
|
*
|
|
847
|
-
*
|
|
848
|
-
*
|
|
849
|
-
*
|
|
1136
|
+
* A reader's parent may itself be a schema tenant, so this follows `of` by one
|
|
1137
|
+
* hop rather than assuming the cluster — the reader of a tenant reads that
|
|
1138
|
+
* tenant's schema, not the database's.
|
|
850
1139
|
*/
|
|
851
|
-
|
|
1140
|
+
function parentRole(resource, plan) {
|
|
1141
|
+
const parent = plan.resources.find((r) => r.id === resource.of);
|
|
1142
|
+
return parent ? localRole(parent) : resource.name;
|
|
1143
|
+
}
|
|
1144
|
+
/** Whether a kind connects to Postgres and therefore has an owner role. */
|
|
1145
|
+
function isDatabase(kind) {
|
|
1146
|
+
return kind === "database" || kind === "database-schema";
|
|
1147
|
+
}
|
|
852
1148
|
/**
|
|
853
|
-
* The
|
|
1149
|
+
* The URL a migrator connects with — the owner role, which can create, alter
|
|
1150
|
+
* and drop.
|
|
854
1151
|
*
|
|
855
|
-
*
|
|
856
|
-
*
|
|
857
|
-
*
|
|
1152
|
+
* Never in `provides`, which is the whole point. A handler that could reach this
|
|
1153
|
+
* could `DROP TABLE`, and the security property the role split exists for is
|
|
1154
|
+
* that it cannot: the value is injected by `gkm exec` for the migrate step and
|
|
1155
|
+
* by nothing else.
|
|
858
1156
|
*/
|
|
859
|
-
|
|
860
|
-
|
|
861
|
-
|
|
862
|
-
|
|
863
|
-
|
|
864
|
-
|
|
865
|
-
|
|
866
|
-
|
|
867
|
-
|
|
868
|
-
}
|
|
1157
|
+
function ownerUrl(resource, plan, ports, project) {
|
|
1158
|
+
if (!resource.container) return void 0;
|
|
1159
|
+
const port = ports[primaryPortKey(resource.container)];
|
|
1160
|
+
if (port === void 0) return void 0;
|
|
1161
|
+
const owner = ownerRole(localRole(resource));
|
|
1162
|
+
return postgres(port, rootDatabase(resource, plan), {
|
|
1163
|
+
user: owner,
|
|
1164
|
+
password: localRolePassword(project, plan, owner)
|
|
1165
|
+
});
|
|
1166
|
+
}
|
|
869
1167
|
/**
|
|
870
|
-
*
|
|
1168
|
+
* The shared broker keys, when anything is published or consumed.
|
|
871
1169
|
*
|
|
872
|
-
* A
|
|
873
|
-
*
|
|
874
|
-
*
|
|
875
|
-
*
|
|
1170
|
+
* A queue's own key is the producer's. These two are the *connection* itself,
|
|
1171
|
+
* which locally is one broker for every queue and topic in the project: the
|
|
1172
|
+
* generated pollers open a single connection and subscribe each worker by name
|
|
1173
|
+
* on it. Deployed there is no such thing — a Lambda is handed its own event
|
|
1174
|
+
* source — so this pair exists for the local target and says so.
|
|
876
1175
|
*/
|
|
877
|
-
|
|
878
|
-
|
|
879
|
-
"
|
|
880
|
-
|
|
881
|
-
|
|
882
|
-
|
|
883
|
-
|
|
884
|
-
};
|
|
1176
|
+
function brokerEnv(plan, ports) {
|
|
1177
|
+
const carrier = plan.resources.find((r) => r.kind === "queue" || r.kind === "topic");
|
|
1178
|
+
const publisher = carrier ? urlFor(carrier, plan, ports, "") : plan.workerBroker ? workerBroker(plan, ports) : void 0;
|
|
1179
|
+
if (!publisher) return {};
|
|
1180
|
+
if (plan.events === "sns") return {};
|
|
1181
|
+
return { EVENT_PUBLISHER_CONNECTION_STRING: publisher };
|
|
1182
|
+
}
|
|
885
1183
|
/**
|
|
886
|
-
* The
|
|
887
|
-
*
|
|
888
|
-
* `pgboss` is deliberately absent rather than mapped to `postgres`: it is a
|
|
889
|
-
* schema tenant in a database the manifest already declares, so mapping it here
|
|
890
|
-
* would start a Postgres for a project that declared no database at all.
|
|
891
|
-
*
|
|
892
|
-
* This one comes from config rather than from a declaration because there are
|
|
893
|
-
* no `topic` or `queue` kinds yet. When they land the mapping moves to
|
|
894
|
-
* {@link CONTAINERS} keyed by kind, and this constant goes with them.
|
|
1184
|
+
* The broker a worker schedules its crons through, when nothing else declared
|
|
1185
|
+
* one — pg-boss in the declared database, on that database's port.
|
|
895
1186
|
*/
|
|
896
|
-
|
|
897
|
-
|
|
898
|
-
|
|
899
|
-
|
|
900
|
-
|
|
901
|
-
|
|
1187
|
+
function workerBroker(plan, ports) {
|
|
1188
|
+
const database = plan.resources.find((r) => r.kind === "database");
|
|
1189
|
+
if (!database?.container) return void 0;
|
|
1190
|
+
const port = ports[primaryPortKey(database.container)];
|
|
1191
|
+
return port === void 0 ? void 0 : broker(plan, port);
|
|
1192
|
+
}
|
|
902
1193
|
/**
|
|
903
|
-
*
|
|
1194
|
+
* The URL for one planned resource.
|
|
904
1195
|
*
|
|
905
|
-
*
|
|
906
|
-
* everywhere; buckets and queues are kebab and disallow underscores in some
|
|
907
|
-
* providers. The suffix is the same word either way.
|
|
1196
|
+
* Every part of it comes from the resource and the port it was published on.
|
|
908
1197
|
*/
|
|
909
|
-
|
|
910
|
-
|
|
911
|
-
"
|
|
912
|
-
"
|
|
913
|
-
|
|
914
|
-
|
|
1198
|
+
function urlFor(resource, plan, ports, project, addresses = {}) {
|
|
1199
|
+
if (resource.kind === "secret") return localSecret(project, plan, resource);
|
|
1200
|
+
if (resource.kind === "encryption") return localKeyring(project, plan, resource);
|
|
1201
|
+
if (resource.kind === "mobile-app") return schemeBase(project, resource.scheme);
|
|
1202
|
+
if (resource.kind === "credential") return void 0;
|
|
1203
|
+
if (resource.kind === "external-api") {
|
|
1204
|
+
if (!resource.fake) return resource.url === void 0 ? void 0 : externalApiUrl({
|
|
1205
|
+
id: resource.id,
|
|
1206
|
+
url: resource.url
|
|
1207
|
+
}, plan.stage);
|
|
1208
|
+
const port = ports[resource.fake.key];
|
|
1209
|
+
return port === void 0 ? void 0 : `http://${LOCAL_HOST}:${port}`;
|
|
1210
|
+
}
|
|
1211
|
+
if (resource.kind === "rest-api" || resource.kind === "site") {
|
|
1212
|
+
const address = addresses[resource.id];
|
|
1213
|
+
const edge = plan.containers.includes("caddy") ? ports[primaryPortKey("caddy")] : void 0;
|
|
1214
|
+
return address && edge !== void 0 ? `https://${hostFor(resource, project)}:${edge}` : address;
|
|
1215
|
+
}
|
|
1216
|
+
if (!resource.container) return void 0;
|
|
1217
|
+
const port = ports[primaryPortKey(resource.container)];
|
|
1218
|
+
if (port === void 0) return void 0;
|
|
1219
|
+
switch (resource.kind) {
|
|
1220
|
+
case "database":
|
|
1221
|
+
case "database-schema":
|
|
1222
|
+
case "database-reader": {
|
|
1223
|
+
const database = rootDatabase(resource, plan);
|
|
1224
|
+
if (resource.roles === false) {
|
|
1225
|
+
const schema = schemaOf(resource, plan);
|
|
1226
|
+
return schema ? `${postgres(port, database)}?search_path=${schema}` : postgres(port, database);
|
|
1227
|
+
}
|
|
1228
|
+
const role = resource.kind === "database-reader" ? readerRole(parentRole(resource, plan)) : localRole(resource);
|
|
1229
|
+
return postgres(port, database, {
|
|
1230
|
+
user: role,
|
|
1231
|
+
password: localRolePassword(project, plan, role)
|
|
1232
|
+
});
|
|
1233
|
+
}
|
|
1234
|
+
case "email": return `smtp://${LOCAL_HOST}:${port}`;
|
|
1235
|
+
case "objects": return `s3://${resource.name}?region=${LOCAL_REGION}&endpoint=http://${LOCAL_HOST}:${port}&forcePathStyle=true`;
|
|
1236
|
+
case "file-server": {
|
|
1237
|
+
const origin = plan.resources.find((r) => r.id === resource.of);
|
|
1238
|
+
if (!origin) return void 0;
|
|
1239
|
+
return plan.containers.includes("caddy") ? `https://${hostFor(resource, project)}:${port}` : `http://${LOCAL_HOST}:${port}/${origin.name}`;
|
|
1240
|
+
}
|
|
1241
|
+
case "cache":
|
|
1242
|
+
if (resource.of) {
|
|
1243
|
+
const parent = plan.resources.find((r) => r.id === resource.of);
|
|
1244
|
+
const url = parent ? urlFor(parent, plan, ports, project, addresses) : void 0;
|
|
1245
|
+
return url ? withTable(url, resource.table ?? cacheTable(resource.id)) : void 0;
|
|
1246
|
+
}
|
|
1247
|
+
switch (plan.cache) {
|
|
1248
|
+
case "elasticache": return `redis://${LOCAL_HOST}:${port}`;
|
|
1249
|
+
default: return `http://:${LOCAL_TOKEN$1}@${LOCAL_HOST}:${port}`;
|
|
1250
|
+
}
|
|
1251
|
+
case "queue":
|
|
1252
|
+
case "topic": return broker(plan, port, resource);
|
|
1253
|
+
default: return;
|
|
1254
|
+
}
|
|
1255
|
+
}
|
|
915
1256
|
/**
|
|
916
|
-
* The
|
|
1257
|
+
* The producer's connection string, composed from the backend the plan chose.
|
|
917
1258
|
*
|
|
918
|
-
* The
|
|
919
|
-
*
|
|
920
|
-
*
|
|
1259
|
+
* The protocol is what picks the transport, so a producer never branches: the
|
|
1260
|
+
* same `.publish()` reaches pg-boss here and SQS deployed because the string it
|
|
1261
|
+
* was handed said so.
|
|
921
1262
|
*/
|
|
922
|
-
function
|
|
923
|
-
|
|
924
|
-
|
|
925
|
-
|
|
1263
|
+
function broker(plan, port, resource) {
|
|
1264
|
+
switch (plan.events) {
|
|
1265
|
+
case "pgboss": {
|
|
1266
|
+
const database = plan.resources.find((r) => r.kind === "database");
|
|
1267
|
+
if (!database) throw new PgBossNeedsDatabase([]);
|
|
1268
|
+
return `pgboss://${LOCAL_USER$2}:${LOCAL_USER$2}@${LOCAL_HOST}:${port}/${database.name}?schema=${PGBOSS_SCHEMA}`;
|
|
1269
|
+
}
|
|
1270
|
+
case "rabbitmq": return `rabbitmq://${LOCAL_USER$2}:${LOCAL_USER$2}@${LOCAL_HOST}:${port}?exchange=${RABBITMQ_EXCHANGE}`;
|
|
1271
|
+
case "sns": {
|
|
1272
|
+
if (!resource) throw new UnprovisionedEventsBackend("sns");
|
|
1273
|
+
const endpoint = emulatorEndpoint(port);
|
|
1274
|
+
const address = resource.kind === "topic" ? snsUrl.build({
|
|
1275
|
+
topicArn: emulatorTopicArn(resource.name),
|
|
1276
|
+
region: EMULATOR_REGION,
|
|
1277
|
+
endpoint
|
|
1278
|
+
}) : sqsUrl.build({
|
|
1279
|
+
queueUrl: emulatorQueueUrl(resource.name, port),
|
|
1280
|
+
region: EMULATOR_REGION,
|
|
1281
|
+
endpoint
|
|
1282
|
+
});
|
|
1283
|
+
const url = new URL(address);
|
|
1284
|
+
url.searchParams.set("accessKeyId", EMULATOR_CREDENTIALS.accessKeyId);
|
|
1285
|
+
url.searchParams.set("secretAccessKey", EMULATOR_CREDENTIALS.secretAccessKey);
|
|
1286
|
+
return url.toString();
|
|
1287
|
+
}
|
|
1288
|
+
}
|
|
1289
|
+
}
|
|
1290
|
+
/** A local Postgres URL, master credential and all. */
|
|
1291
|
+
function postgres(port, database, credential = {
|
|
1292
|
+
user: LOCAL_USER$2,
|
|
1293
|
+
password: LOCAL_USER$2
|
|
1294
|
+
}) {
|
|
1295
|
+
return `postgres://${credential.user}:${credential.password}@${LOCAL_HOST}:${port}/${database}`;
|
|
926
1296
|
}
|
|
927
1297
|
/**
|
|
928
|
-
* The
|
|
1298
|
+
* The role a handler connects as, for one database or tenant.
|
|
929
1299
|
*
|
|
930
|
-
*
|
|
931
|
-
*
|
|
932
|
-
*
|
|
1300
|
+
* The construct's id, lowercased and stage-scoped exactly as its database or
|
|
1301
|
+
* schema is — so `Orders` in the `test` stage connects as `orders_test`, and two
|
|
1302
|
+
* stages sharing one cluster cannot share a credential. Roles are cluster-scoped
|
|
1303
|
+
* in Postgres, which is why the suffix is not optional here the way it is for a
|
|
1304
|
+
* schema.
|
|
933
1305
|
*/
|
|
934
|
-
function
|
|
935
|
-
|
|
936
|
-
if (kind === "file-server") return edge ? "caddy" : CONTAINERS.objects;
|
|
937
|
-
if (kind === "cache") {
|
|
938
|
-
if (derived) return CONTAINERS.database;
|
|
939
|
-
return CACHE_CONTAINERS[cache] ?? CONTAINERS.database;
|
|
940
|
-
}
|
|
941
|
-
return CONTAINERS[kind];
|
|
1306
|
+
function localRole(resource) {
|
|
1307
|
+
return resource.name;
|
|
942
1308
|
}
|
|
943
1309
|
/**
|
|
944
|
-
*
|
|
1310
|
+
* The password for a derived local role.
|
|
945
1311
|
*
|
|
946
|
-
*
|
|
947
|
-
* `
|
|
948
|
-
*
|
|
1312
|
+
* Derived rather than random, for the same reason the local secret is: a
|
|
1313
|
+
* password that changed on every `gkm dev` would lock a developer out of the
|
|
1314
|
+
* data they had a moment ago. Seeded by the project so two checkouts do not
|
|
1315
|
+
* share one, and by the stage so `test` and `development` do not.
|
|
1316
|
+
*
|
|
1317
|
+
* It is a *local* credential by construction — the cluster is on loopback and
|
|
1318
|
+
* the seed is a checkout path — and no deployed target uses this function.
|
|
949
1319
|
*/
|
|
950
|
-
function
|
|
951
|
-
|
|
952
|
-
|
|
953
|
-
|
|
954
|
-
|
|
955
|
-
|
|
956
|
-
|
|
957
|
-
|
|
958
|
-
|
|
959
|
-
|
|
960
|
-
|
|
961
|
-
|
|
962
|
-
|
|
963
|
-
|
|
964
|
-
|
|
965
|
-
|
|
966
|
-
|
|
967
|
-
|
|
968
|
-
|
|
969
|
-
|
|
970
|
-
|
|
971
|
-
|
|
972
|
-
|
|
973
|
-
|
|
974
|
-
|
|
975
|
-
|
|
976
|
-
|
|
977
|
-
|
|
978
|
-
|
|
979
|
-
});
|
|
1320
|
+
function localRolePassword(project, plan, role) {
|
|
1321
|
+
return createHash("sha256").update(`${project}:${plan.stage}:role:${role}`).digest("base64url").slice(0, 32);
|
|
1322
|
+
}
|
|
1323
|
+
/**
|
|
1324
|
+
* A cache's address: its database's URL, carrying the table it reads.
|
|
1325
|
+
*
|
|
1326
|
+
* Appended rather than kept beside the URL as a second key, for the reason the
|
|
1327
|
+
* design gives everywhere else — an address and what it opens are one fact, and
|
|
1328
|
+
* two keys are one more thing to keep in step.
|
|
1329
|
+
*/
|
|
1330
|
+
function withTable(url, table) {
|
|
1331
|
+
const parsed = new URL(url);
|
|
1332
|
+
parsed.searchParams.set("table", table);
|
|
1333
|
+
return parsed.toString();
|
|
1334
|
+
}
|
|
1335
|
+
/**
|
|
1336
|
+
* The physical database a derived resource lives in.
|
|
1337
|
+
*
|
|
1338
|
+
* Walks `of` to the top rather than reading the immediate parent, because a
|
|
1339
|
+
* reader's parent may itself be a schema tenant — two hops from the database
|
|
1340
|
+
* that actually exists.
|
|
1341
|
+
*/
|
|
1342
|
+
function rootDatabase(resource, plan) {
|
|
1343
|
+
const byId = new Map(plan.resources.map((r) => [r.id, r]));
|
|
1344
|
+
let current = resource;
|
|
1345
|
+
while (current.of) {
|
|
1346
|
+
const parent = byId.get(current.of);
|
|
1347
|
+
if (!parent) break;
|
|
1348
|
+
current = parent;
|
|
980
1349
|
}
|
|
981
|
-
|
|
982
|
-
|
|
983
|
-
|
|
984
|
-
|
|
985
|
-
|
|
986
|
-
|
|
987
|
-
|
|
988
|
-
|
|
989
|
-
|
|
990
|
-
if (databases.length > 1) throw new CacheIsAmbiguous(placeless.map((r) => r.id), databases.map((r) => r.id));
|
|
991
|
-
for (const resource of placeless) resource.of = only.id;
|
|
992
|
-
}
|
|
1350
|
+
return current.name;
|
|
1351
|
+
}
|
|
1352
|
+
/** The schema a derived resource puts on its search path, if it has one. */
|
|
1353
|
+
function schemaOf(resource, plan) {
|
|
1354
|
+
const byId = new Map(plan.resources.map((r) => [r.id, r]));
|
|
1355
|
+
let current = resource;
|
|
1356
|
+
while (current) {
|
|
1357
|
+
if (current.kind === "database-schema") return current.schema;
|
|
1358
|
+
current = current.of ? byId.get(current.of) : void 0;
|
|
993
1359
|
}
|
|
994
|
-
return {
|
|
995
|
-
stage,
|
|
996
|
-
events,
|
|
997
|
-
cache,
|
|
998
|
-
containers: [...containers],
|
|
999
|
-
resources
|
|
1000
|
-
};
|
|
1001
1360
|
}
|
|
1002
1361
|
/**
|
|
1003
|
-
*
|
|
1362
|
+
* A broker address asked for without the topic or queue it would name.
|
|
1004
1363
|
*
|
|
1005
|
-
*
|
|
1006
|
-
*
|
|
1007
|
-
* broker.
|
|
1364
|
+
* On SNS each topic and queue is its own address, so there is no project-wide
|
|
1365
|
+
* one to hand out — a cron's schedule store, say, cannot live there.
|
|
1008
1366
|
*/
|
|
1009
|
-
var
|
|
1010
|
-
|
|
1011
|
-
|
|
1012
|
-
|
|
1013
|
-
this.
|
|
1367
|
+
var UnprovisionedEventsBackend = class extends Error {
|
|
1368
|
+
backend;
|
|
1369
|
+
constructor(backend) {
|
|
1370
|
+
super(`'${backend}' has no single broker address: each topic and queue is addressed by its own ARN. Something asked for one without naming the topic or queue.`);
|
|
1371
|
+
this.backend = backend;
|
|
1372
|
+
this.name = "UnprovisionedEventsBackend";
|
|
1014
1373
|
}
|
|
1015
1374
|
};
|
|
1016
1375
|
/**
|
|
1017
|
-
*
|
|
1376
|
+
* The keyring an `Encryption` construct resolves to locally.
|
|
1377
|
+
*
|
|
1378
|
+
* Derived like a secret — stable across restarts, distinct per project, stage
|
|
1379
|
+
* and construct, never written to disk — so what `gkm dev` encrypted yesterday
|
|
1380
|
+
* still opens today. One key, because nothing local is worth rotating: a
|
|
1381
|
+
* server stage's keyring is generated and rotated in its secrets instead.
|
|
1382
|
+
*/
|
|
1383
|
+
function localKeyring(project, plan, resource) {
|
|
1384
|
+
const derive = (purpose) => createHash("sha256").update(`${project}:${plan.stage}:${resource.id}:${purpose}`).digest();
|
|
1385
|
+
return formatKeyring({
|
|
1386
|
+
keys: [{
|
|
1387
|
+
id: "k1",
|
|
1388
|
+
key: derive("k1")
|
|
1389
|
+
}],
|
|
1390
|
+
index: derive("index")
|
|
1391
|
+
});
|
|
1392
|
+
}
|
|
1393
|
+
/**
|
|
1394
|
+
* The value a secret resolves to locally.
|
|
1018
1395
|
*
|
|
1019
|
-
*
|
|
1020
|
-
*
|
|
1021
|
-
*
|
|
1022
|
-
*
|
|
1023
|
-
* `orders.cache('Sessions')` names the database, which is a fact about the
|
|
1024
|
-
* application rather than a deployment choice.
|
|
1396
|
+
* A hash of what identifies it, so it is stable, distinct per project, stage
|
|
1397
|
+
* and construct, and never written to disk as a literal. Deployed this is the
|
|
1398
|
+
* one kind whose value does *not* come from here — a secret manager generates
|
|
1399
|
+
* it once and the target reads it back.
|
|
1025
1400
|
*/
|
|
1026
|
-
|
|
1027
|
-
|
|
1028
|
-
|
|
1029
|
-
this.ids = ids;
|
|
1030
|
-
this.databases = databases;
|
|
1031
|
-
this.name = "CacheIsAmbiguous";
|
|
1032
|
-
}
|
|
1033
|
-
};
|
|
1034
|
-
var PgBossNeedsDatabase = class extends Error {
|
|
1035
|
-
constructor(ids) {
|
|
1036
|
-
super(`pg-boss keeps its queues in a declared database, and none was declared. Declare one, or set services.events to 'rabbitmq' or 'sns'. Queues and topics affected: ${ids.join(", ")}.`);
|
|
1037
|
-
this.ids = ids;
|
|
1038
|
-
this.name = "PgBossNeedsDatabase";
|
|
1039
|
-
}
|
|
1040
|
-
};
|
|
1041
|
-
|
|
1401
|
+
function localSecret(project, plan, resource) {
|
|
1402
|
+
return createHash("sha256").update(`${project}:${plan.stage}:${resource.id}`).digest("base64url").slice(0, 43);
|
|
1403
|
+
}
|
|
1042
1404
|
//#endregion
|
|
1043
|
-
//#region src/reconcile/
|
|
1405
|
+
//#region src/reconcile/apps.ts
|
|
1044
1406
|
/**
|
|
1045
|
-
* The
|
|
1407
|
+
* The apps, as services beside the containers their constructs derive.
|
|
1046
1408
|
*
|
|
1047
|
-
*
|
|
1048
|
-
*
|
|
1049
|
-
*
|
|
1050
|
-
*
|
|
1409
|
+
* `gkm docker` used to write these by hand: every backend got
|
|
1410
|
+
* `DATABASE_URL=postgresql://…@postgres:5432/app` and `REDIS_URL=redis://redis`,
|
|
1411
|
+
* which was right only for a project whose single database happened to be
|
|
1412
|
+
* called `Database` — an `Orders` database publishes `ORDERS_URL`, a schema
|
|
1413
|
+
* tenant has a URL of its own, and buckets and mail were not wired at all.
|
|
1414
|
+
*
|
|
1415
|
+
* Now an app's environment is exactly what reconcile hands `gkm dev`: every key
|
|
1416
|
+
* the declared constructs provide, from the same derivation. The only
|
|
1417
|
+
* difference is where the addresses point — at the containers by service name
|
|
1418
|
+
* on the compose network, rather than at `localhost` on the ports published for
|
|
1419
|
+
* the host.
|
|
1420
|
+
*
|
|
1421
|
+
* Always from the *local* stage's plan, whichever stage is being reconciled:
|
|
1422
|
+
* `gkm test` rewrites the same file, and running the tests must not quietly
|
|
1423
|
+
* point these apps at the test databases.
|
|
1051
1424
|
*/
|
|
1052
|
-
|
|
1053
|
-
|
|
1054
|
-
const LOCAL_HOST = "localhost";
|
|
1055
|
-
/** The region local buckets claim, so the client has one to sign with. */
|
|
1056
|
-
const LOCAL_REGION = "us-east-1";
|
|
1425
|
+
/** The profile app services carry, so `gkm dev` starts only the containers. */
|
|
1426
|
+
const APPS_PROFILE = "apps";
|
|
1057
1427
|
/**
|
|
1058
|
-
*
|
|
1428
|
+
* Where an app's image is built from.
|
|
1059
1429
|
*
|
|
1060
|
-
* An
|
|
1061
|
-
*
|
|
1062
|
-
* rotate and one more thing to leak into a log. Locally there is no role, and
|
|
1063
|
-
* the same chain reads these — which is why they are injected beside the URL
|
|
1064
|
-
* rather than written into it.
|
|
1430
|
+
* An app at the project root is a single-app project, whose `gkm docker`
|
|
1431
|
+
* writes `.gkm/docker/Dockerfile`; a workspace writes one per app.
|
|
1065
1432
|
*/
|
|
1066
|
-
|
|
1067
|
-
|
|
1068
|
-
|
|
1069
|
-
AWS_REGION: LOCAL_REGION
|
|
1070
|
-
};
|
|
1071
|
-
/** The token the local cache proxy accepts. Matches the compose definition. */
|
|
1072
|
-
const LOCAL_TOKEN = "geekmidas";
|
|
1073
|
-
/** The schema pg-boss keeps its tables in, inside the declared database. */
|
|
1074
|
-
const PGBOSS_SCHEMA = "pgboss";
|
|
1075
|
-
/** The exchange every local topic and queue shares on RabbitMQ. */
|
|
1076
|
-
const RABBITMQ_EXCHANGE = "geekmidas.events";
|
|
1433
|
+
function dockerfileOf(appName, path) {
|
|
1434
|
+
return path === "." ? ".gkm/docker/Dockerfile" : `.gkm/docker/Dockerfile.${appName}`;
|
|
1435
|
+
}
|
|
1077
1436
|
/**
|
|
1078
|
-
* Every
|
|
1437
|
+
* Every construct key, addressed across the compose network.
|
|
1079
1438
|
*
|
|
1080
|
-
*
|
|
1081
|
-
*
|
|
1439
|
+
* `envFor` is asked with a placeholder port for every port key, so each
|
|
1440
|
+
* `localhost:<placeholder>` in its answer names exactly one container port —
|
|
1441
|
+
* two containers listening on the same port inside cannot be confused — and is
|
|
1442
|
+
* rewritten to `<container>:<inside>`.
|
|
1082
1443
|
*/
|
|
1083
|
-
function
|
|
1084
|
-
const
|
|
1085
|
-
|
|
1086
|
-
|
|
1087
|
-
|
|
1088
|
-
|
|
1444
|
+
function inNetworkEnv(workspace, manifest, fakes = {}) {
|
|
1445
|
+
const plan = planFor(manifest, workspace.stages.local, provisionOrder(manifest), {
|
|
1446
|
+
localStage: workspace.stages.local,
|
|
1447
|
+
...backendsOf(workspace),
|
|
1448
|
+
fakes
|
|
1449
|
+
});
|
|
1450
|
+
const placeholders = {};
|
|
1451
|
+
const targets = /* @__PURE__ */ new Map();
|
|
1452
|
+
let next = 49152;
|
|
1453
|
+
for (const container of [...plan.containers].sort()) for (const port of portsOf(container, plan.fakes)) {
|
|
1454
|
+
placeholders[port.key] = next;
|
|
1455
|
+
targets.set(next, `${container}:${port.inside}`);
|
|
1456
|
+
next += 1;
|
|
1089
1457
|
}
|
|
1090
|
-
for (const
|
|
1091
|
-
|
|
1092
|
-
|
|
1093
|
-
|
|
1094
|
-
|
|
1095
|
-
|
|
1096
|
-
|
|
1458
|
+
for (const key of portKeys(plan.containers, plan.fakes)) placeholders[key] ??= next++;
|
|
1459
|
+
const env = envFor(plan, {
|
|
1460
|
+
ports: placeholders,
|
|
1461
|
+
project: workspace.name,
|
|
1462
|
+
addresses: surfaceAddresses(workspace, manifest, (app, port) => `http://${app}:${port}`)
|
|
1463
|
+
});
|
|
1464
|
+
const rewrite = (value) => value.replace(/(?:localhost|127\.0\.0\.1):(\d+)/g, (match, port) => {
|
|
1465
|
+
return targets.get(Number(port)) ?? match;
|
|
1466
|
+
});
|
|
1467
|
+
return Object.fromEntries(Object.entries(env).map(([key, value]) => [key, rewrite(value)]));
|
|
1468
|
+
}
|
|
1469
|
+
/**
|
|
1470
|
+
* The keys one app's environment may hold: what its own declaration provides
|
|
1471
|
+
* and requires, and what each construct it has an edge to provides.
|
|
1472
|
+
*
|
|
1473
|
+
* The edges, not the manifest. Every app used to be handed every key the
|
|
1474
|
+
* workspace resolved — a web app got the database's owner URL and the auth
|
|
1475
|
+
* server's signing secret, and the API got the auth server's database — which
|
|
1476
|
+
* is exactly what a declared edge exists to rule out. A deploy composes an
|
|
1477
|
+
* app's environment from its edges; so does this.
|
|
1478
|
+
*
|
|
1479
|
+
* One hop: a surface an app calls is reached over HTTP, so its URL is the
|
|
1480
|
+
* app's business and its database is not. A site also gets the public
|
|
1481
|
+
* variant of each key its bundle inlines (`NEXT_PUBLIC_API_URL`), and a surface
|
|
1482
|
+
* the edges of the workers whose crons and subscribers its server runs.
|
|
1483
|
+
*
|
|
1484
|
+
* `undefined` for an app no declaration describes, which gets nothing but its
|
|
1485
|
+
* port.
|
|
1486
|
+
*/
|
|
1487
|
+
function appEnvKeys(manifest, appName, runnables = {}) {
|
|
1488
|
+
const entry = Object.entries(manifest).find(([id, d]) => (d.kind === "rest-api" || d.kind === "site" || d.kind === "mobile-app") && appKey(id) === appName);
|
|
1489
|
+
if (!entry) return void 0;
|
|
1490
|
+
const [id, declaration] = entry;
|
|
1491
|
+
const keys = /* @__PURE__ */ new Set([
|
|
1492
|
+
...declaration.provides ?? [],
|
|
1493
|
+
...declaration.requires ?? [],
|
|
1494
|
+
...declaration.kind === "rest-api" ? declaration.endpoints.flatMap((endpoint) => endpoint.requires ?? []) : []
|
|
1495
|
+
]);
|
|
1496
|
+
const edges = [...dependenciesOf(declaration).map((edge) => edge.target), ...runnables[id] ?? []];
|
|
1497
|
+
if (declaration.kind === "rest-api" && declaration.endpoints.length === 0) {
|
|
1498
|
+
const runsWorkers = Object.values(manifest).some((other) => other.kind === "worker");
|
|
1499
|
+
for (const [otherId, other] of Object.entries(manifest)) {
|
|
1500
|
+
if (other.kind !== "worker") continue;
|
|
1501
|
+
edges.push(...dependenciesOf(other).map((edge) => edge.target), ...runnables[otherId] ?? []);
|
|
1502
|
+
keys.add("EVENT_PUBLISHER_CONNECTION_STRING");
|
|
1503
|
+
}
|
|
1504
|
+
if (runsWorkers) for (const other of Object.values(manifest)) {
|
|
1505
|
+
if (other.kind !== "queue" && other.kind !== "topic") continue;
|
|
1506
|
+
for (const key of other.provides ?? []) keys.add(key);
|
|
1097
1507
|
}
|
|
1098
|
-
if (resource.kind === "email" && url) env[provideKey(resource.id, "from")] = options.mailFrom ?? `noreply@${LOCAL_HOST}`;
|
|
1099
1508
|
}
|
|
1100
|
-
|
|
1101
|
-
|
|
1102
|
-
|
|
1103
|
-
|
|
1509
|
+
for (const id of edges) {
|
|
1510
|
+
const target = manifest[id];
|
|
1511
|
+
if (!target) continue;
|
|
1512
|
+
for (const key of target.provides ?? []) keys.add(key);
|
|
1513
|
+
if (target.kind === "queue" || target.kind === "topic") keys.add("EVENT_PUBLISHER_CONNECTION_STRING");
|
|
1514
|
+
if (target.kind === "objects") {
|
|
1515
|
+
keys.add("AWS_ACCESS_KEY_ID");
|
|
1516
|
+
keys.add("AWS_SECRET_ACCESS_KEY");
|
|
1517
|
+
keys.add("AWS_REGION");
|
|
1518
|
+
}
|
|
1519
|
+
}
|
|
1520
|
+
if (declaration.kind === "site" || declaration.kind === "mobile-app") for (const key of Object.keys(publicEnvFor(declaration, manifest))) keys.add(key);
|
|
1521
|
+
return keys;
|
|
1522
|
+
}
|
|
1523
|
+
/** One compose service per app the workspace runs, behind the apps profile. */
|
|
1524
|
+
function appServices(workspace, manifest, containers, runnables = {}, fakes = {}) {
|
|
1525
|
+
const env = inNetworkEnv(workspace, manifest, fakes);
|
|
1526
|
+
const services = {};
|
|
1527
|
+
for (const [name, app] of Object.entries(workspace.apps)) {
|
|
1528
|
+
if (app.type === "mobile" || !app.port) continue;
|
|
1529
|
+
const key = appKey(name);
|
|
1530
|
+
const health = app.type === "web" ? "/" : "/health";
|
|
1531
|
+
const allowed = appEnvKeys(manifest, name, runnables);
|
|
1532
|
+
const own = Object.fromEntries(Object.entries(env).filter(([envKey]) => allowed?.has(envKey)));
|
|
1533
|
+
services[key] = {
|
|
1534
|
+
image: `${key}:\${TAG:-latest}`,
|
|
1535
|
+
build: {
|
|
1536
|
+
context: ".",
|
|
1537
|
+
dockerfile: dockerfileOf(name, app.path)
|
|
1538
|
+
},
|
|
1539
|
+
profiles: [APPS_PROFILE],
|
|
1540
|
+
ports: [`${app.port}:${app.port}`],
|
|
1541
|
+
environment: {
|
|
1542
|
+
NODE_ENV: "production",
|
|
1543
|
+
PORT: String(app.port),
|
|
1544
|
+
...own
|
|
1545
|
+
},
|
|
1546
|
+
...containers.some((c) => c !== "caddy") ? { depends_on: containers.filter((c) => c !== "caddy") } : {},
|
|
1547
|
+
healthcheck: {
|
|
1548
|
+
test: [
|
|
1549
|
+
"CMD",
|
|
1550
|
+
"wget",
|
|
1551
|
+
"-q",
|
|
1552
|
+
"--spider",
|
|
1553
|
+
`http://localhost:${app.port}${health}`
|
|
1554
|
+
],
|
|
1555
|
+
interval: "10s",
|
|
1556
|
+
timeout: "5s",
|
|
1557
|
+
retries: 5
|
|
1558
|
+
}
|
|
1559
|
+
};
|
|
1560
|
+
}
|
|
1561
|
+
return services;
|
|
1104
1562
|
}
|
|
1563
|
+
//#endregion
|
|
1564
|
+
//#region src/reconcile/clients.ts
|
|
1105
1565
|
/**
|
|
1106
|
-
* The
|
|
1566
|
+
* The real Postgres and MinIO clients the applier runs against.
|
|
1107
1567
|
*
|
|
1108
|
-
*
|
|
1109
|
-
*
|
|
1110
|
-
*
|
|
1111
|
-
* twice, so a site and its API cannot come to disagree about where the API is.
|
|
1568
|
+
* Kept apart from `provision.ts` so the rules there — what gets created, in what
|
|
1569
|
+
* order, and never dropping anything — are asserted against fakes, while the
|
|
1570
|
+
* drivers live here where there is nothing to assert but wiring.
|
|
1112
1571
|
*
|
|
1113
|
-
*
|
|
1114
|
-
*
|
|
1115
|
-
* against `http:///`, where a missing variable fails at build with the name of
|
|
1116
|
-
* the thing that is missing.
|
|
1572
|
+
* Both are constructed from the same local addresses the env writer composes,
|
|
1573
|
+
* so the applier cannot end up talking to a different container than the app.
|
|
1117
1574
|
*/
|
|
1118
|
-
|
|
1119
|
-
|
|
1120
|
-
for (const resource of plan.resources) for (const [key, source] of Object.entries(resource.publicEnv ?? {})) {
|
|
1121
|
-
const value = resolved[source];
|
|
1122
|
-
if (value) env[key] = value;
|
|
1123
|
-
}
|
|
1124
|
-
return env;
|
|
1125
|
-
}
|
|
1575
|
+
/** The credential local containers are brought up with. */
|
|
1576
|
+
const LOCAL_USER$1 = "geekmidas";
|
|
1126
1577
|
/**
|
|
1127
|
-
*
|
|
1128
|
-
* a cookie set by it is readable.
|
|
1129
|
-
*
|
|
1130
|
-
* Both are read off the same list — the constructs that declared an edge to
|
|
1131
|
-
* this surface — which is why neither appears in application code. A surface
|
|
1132
|
-
* that listed its own callers would be edited every time something new called
|
|
1133
|
-
* it, and the thing being edited is already recorded in the graph.
|
|
1578
|
+
* A Postgres client that opens one connection per database it is asked about.
|
|
1134
1579
|
*
|
|
1135
|
-
*
|
|
1136
|
-
*
|
|
1137
|
-
*
|
|
1138
|
-
* means it is not running in this stage, and keys belong with the address they
|
|
1139
|
-
* describe.
|
|
1580
|
+
* `CREATE DATABASE` cannot run from a connection to the database being created,
|
|
1581
|
+
* and a schema must be created from inside its own database — so the database is
|
|
1582
|
+
* part of each query rather than fixed when the client is built.
|
|
1140
1583
|
*/
|
|
1141
|
-
function
|
|
1142
|
-
|
|
1143
|
-
|
|
1144
|
-
|
|
1584
|
+
function pgClient(port) {
|
|
1585
|
+
return { async query(database, sql, values) {
|
|
1586
|
+
const client = new Client({
|
|
1587
|
+
host: "localhost",
|
|
1588
|
+
port,
|
|
1589
|
+
user: LOCAL_USER$1,
|
|
1590
|
+
password: LOCAL_USER$1,
|
|
1591
|
+
database: database ?? LOCAL_USER$1
|
|
1592
|
+
});
|
|
1593
|
+
await client.connect();
|
|
1594
|
+
try {
|
|
1595
|
+
return (await client.query(sql, values)).rows;
|
|
1596
|
+
} finally {
|
|
1597
|
+
await client.end();
|
|
1598
|
+
}
|
|
1599
|
+
} };
|
|
1600
|
+
}
|
|
1601
|
+
/** A bucket client pointed at the local MinIO. */
|
|
1602
|
+
function bucketClient(port) {
|
|
1603
|
+
const s3 = new S3Client({
|
|
1604
|
+
region: "us-east-1",
|
|
1605
|
+
endpoint: `http://localhost:${port}`,
|
|
1606
|
+
forcePathStyle: true,
|
|
1607
|
+
credentials: {
|
|
1608
|
+
accessKeyId: LOCAL_USER$1,
|
|
1609
|
+
secretAccessKey: LOCAL_USER$1
|
|
1610
|
+
}
|
|
1611
|
+
});
|
|
1612
|
+
return {
|
|
1613
|
+
async exists(bucket) {
|
|
1614
|
+
try {
|
|
1615
|
+
await s3.send(new HeadBucketCommand({ Bucket: bucket }));
|
|
1616
|
+
return true;
|
|
1617
|
+
} catch {
|
|
1618
|
+
return false;
|
|
1619
|
+
}
|
|
1620
|
+
},
|
|
1621
|
+
async create(bucket) {
|
|
1622
|
+
await s3.send(new CreateBucketCommand({ Bucket: bucket }));
|
|
1623
|
+
},
|
|
1624
|
+
async policy(bucket) {
|
|
1625
|
+
try {
|
|
1626
|
+
return (await s3.send(new GetBucketPolicyCommand({ Bucket: bucket }))).Policy;
|
|
1627
|
+
} catch {
|
|
1628
|
+
return;
|
|
1629
|
+
}
|
|
1630
|
+
},
|
|
1631
|
+
async setPolicy(bucket, policy) {
|
|
1632
|
+
await s3.send(new PutBucketPolicyCommand({
|
|
1633
|
+
Bucket: bucket,
|
|
1634
|
+
Policy: policy
|
|
1635
|
+
}));
|
|
1636
|
+
}
|
|
1637
|
+
};
|
|
1638
|
+
}
|
|
1639
|
+
/** A topic and queue client pointed at the local AWS emulator. */
|
|
1640
|
+
function carrierClient(port) {
|
|
1641
|
+
const config = {
|
|
1642
|
+
region: EMULATOR_REGION,
|
|
1643
|
+
endpoint: emulatorEndpoint(port),
|
|
1644
|
+
credentials: EMULATOR_CREDENTIALS
|
|
1645
|
+
};
|
|
1646
|
+
const sns = new SNSClient(config);
|
|
1647
|
+
const sqs = new SQSClient(config);
|
|
1145
1648
|
return {
|
|
1146
|
-
|
|
1147
|
-
|
|
1649
|
+
async topicExists(name) {
|
|
1650
|
+
try {
|
|
1651
|
+
await sns.send(new GetTopicAttributesCommand({ TopicArn: emulatorTopicArn(name) }));
|
|
1652
|
+
return true;
|
|
1653
|
+
} catch {
|
|
1654
|
+
return false;
|
|
1655
|
+
}
|
|
1656
|
+
},
|
|
1657
|
+
async createTopic(name) {
|
|
1658
|
+
await sns.send(new CreateTopicCommand({ Name: name }));
|
|
1659
|
+
},
|
|
1660
|
+
async queueExists(name) {
|
|
1661
|
+
try {
|
|
1662
|
+
await sqs.send(new GetQueueUrlCommand({ QueueName: name }));
|
|
1663
|
+
return true;
|
|
1664
|
+
} catch {
|
|
1665
|
+
return false;
|
|
1666
|
+
}
|
|
1667
|
+
},
|
|
1668
|
+
async createQueue(name) {
|
|
1669
|
+
await sqs.send(new CreateQueueCommand({ QueueName: name }));
|
|
1670
|
+
}
|
|
1148
1671
|
};
|
|
1149
1672
|
}
|
|
1150
|
-
|
|
1151
|
-
|
|
1152
|
-
try {
|
|
1153
|
-
return new URL(address).origin;
|
|
1154
|
-
} catch {
|
|
1155
|
-
return void 0;
|
|
1156
|
-
}
|
|
1157
|
-
}
|
|
1673
|
+
//#endregion
|
|
1674
|
+
//#region src/reconcile/compose.ts
|
|
1158
1675
|
/**
|
|
1159
|
-
* The
|
|
1676
|
+
* The compose document a plan implies.
|
|
1160
1677
|
*
|
|
1161
|
-
*
|
|
1162
|
-
*
|
|
1163
|
-
*
|
|
1678
|
+
* The third declaration of the same containers disappears here: today a
|
|
1679
|
+
* `services: ['postgres','minio']` block is kept in step with the code by hand,
|
|
1680
|
+
* which is the triplication this design exists to remove, in the one place you
|
|
1681
|
+
* touch every day. A `database` implies Postgres; the list is derived.
|
|
1682
|
+
*
|
|
1683
|
+
* Returns an object rather than a string for the same reason `plan` is pure —
|
|
1684
|
+
* what should exist is asserted as data, and turning it into YAML is one line at
|
|
1685
|
+
* the edge.
|
|
1686
|
+
*
|
|
1687
|
+
* What stays config is unchanged: images and versions are pinned explicitly, and
|
|
1688
|
+
* a container no construct implies is still declared by hand. Neutral facts come
|
|
1689
|
+
* from the declaration, provider detail from config.
|
|
1164
1690
|
*/
|
|
1165
|
-
|
|
1166
|
-
|
|
1167
|
-
return parent ? localRole(parent) : resource.name;
|
|
1168
|
-
}
|
|
1169
|
-
/** Whether a kind connects to Postgres and therefore has an owner role. */
|
|
1170
|
-
function isDatabase(kind) {
|
|
1171
|
-
return kind === "database" || kind === "database-schema";
|
|
1172
|
-
}
|
|
1691
|
+
/** The credential every derived container is brought up with locally. */
|
|
1692
|
+
const LOCAL_USER = "geekmidas";
|
|
1173
1693
|
/**
|
|
1174
|
-
* The
|
|
1175
|
-
* and drop.
|
|
1694
|
+
* The token the local cache accepts.
|
|
1176
1695
|
*
|
|
1177
|
-
*
|
|
1178
|
-
*
|
|
1179
|
-
*
|
|
1180
|
-
* by nothing else.
|
|
1696
|
+
* Fixed rather than generated: it is only ever reachable on a port this project
|
|
1697
|
+
* published, and the URL that carries it is derived, so nothing is made safer
|
|
1698
|
+
* by making it unguessable and a stable one keeps `redis-cli` sessions working.
|
|
1181
1699
|
*/
|
|
1182
|
-
|
|
1183
|
-
if (!resource.container) return void 0;
|
|
1184
|
-
const port = ports[primaryPortKey(resource.container)];
|
|
1185
|
-
if (port === void 0) return void 0;
|
|
1186
|
-
const owner = ownerRole(localRole(resource));
|
|
1187
|
-
return postgres(port, rootDatabase(resource, plan), {
|
|
1188
|
-
user: owner,
|
|
1189
|
-
password: localRolePassword(project, plan, owner)
|
|
1190
|
-
});
|
|
1191
|
-
}
|
|
1700
|
+
const LOCAL_TOKEN = "geekmidas";
|
|
1192
1701
|
/**
|
|
1193
|
-
*
|
|
1702
|
+
* Build the compose document for a plan.
|
|
1194
1703
|
*
|
|
1195
|
-
*
|
|
1196
|
-
*
|
|
1197
|
-
*
|
|
1198
|
-
* on it. Deployed there is no such thing — a Lambda is handed its own event
|
|
1199
|
-
* source — so this pair exists for the local target and says so.
|
|
1704
|
+
* Every container is published on its assigned port, never on the image's
|
|
1705
|
+
* default: the point of allocation is that two projects can run at once, and
|
|
1706
|
+
* one container publishing 5432 puts that back.
|
|
1200
1707
|
*/
|
|
1201
|
-
function
|
|
1202
|
-
const
|
|
1203
|
-
|
|
1204
|
-
const
|
|
1205
|
-
|
|
1708
|
+
function composeFor(plan, options) {
|
|
1709
|
+
const services = {};
|
|
1710
|
+
const volumes = {};
|
|
1711
|
+
for (const container of [...plan.containers].sort()) {
|
|
1712
|
+
const declared = container === "postgres" ? plan.resources.find((r) => r.version)?.version : void 0;
|
|
1713
|
+
const fake = plan.fakes?.[container];
|
|
1714
|
+
const image = declared ? postgresImage(declared) : fake?.image ?? DEFAULT_IMAGES[container];
|
|
1715
|
+
if (!image) throw new UnknownContainer(container);
|
|
1716
|
+
const published = publish(container, options.ports, plan.fakes);
|
|
1717
|
+
services[container] = fake ? {
|
|
1718
|
+
image,
|
|
1719
|
+
restart: "unless-stopped",
|
|
1720
|
+
ports: published
|
|
1721
|
+
} : define(container, image, published);
|
|
1722
|
+
const volume = volumeOf(container);
|
|
1723
|
+
if (volume) volumes[volume] = {};
|
|
1724
|
+
}
|
|
1206
1725
|
return {
|
|
1207
|
-
|
|
1208
|
-
|
|
1726
|
+
name: options.project,
|
|
1727
|
+
services: {
|
|
1728
|
+
...services,
|
|
1729
|
+
...options.apps
|
|
1730
|
+
},
|
|
1731
|
+
...Object.keys(volumes).length ? { volumes } : {}
|
|
1209
1732
|
};
|
|
1210
1733
|
}
|
|
1211
|
-
/**
|
|
1212
|
-
|
|
1213
|
-
|
|
1214
|
-
|
|
1215
|
-
|
|
1216
|
-
|
|
1217
|
-
|
|
1218
|
-
|
|
1219
|
-
|
|
1220
|
-
|
|
1221
|
-
|
|
1222
|
-
|
|
1734
|
+
/** Render a compose document as the file it becomes. */
|
|
1735
|
+
function toYaml(file) {
|
|
1736
|
+
return `# Generated by gkm from the construct manifest — do not edit.
|
|
1737
|
+
# Containers are derived from what the app declares. Image pins and anything
|
|
1738
|
+
# no construct implies go in your own docker-compose.yml, which is merged over
|
|
1739
|
+
# this file and wins:
|
|
1740
|
+
#
|
|
1741
|
+
# docker compose -f docker-compose.constructs.yml -f docker-compose.yml …
|
|
1742
|
+
#
|
|
1743
|
+
# "gkm dev" and "gkm test" start the containers. The apps sit behind the
|
|
1744
|
+
# "apps" profile: run "gkm setup", then
|
|
1745
|
+
# docker compose -f docker-compose.constructs.yml --profile apps up --build
|
|
1746
|
+
${stringify(file, { lineWidth: 0 })}`;
|
|
1747
|
+
}
|
|
1748
|
+
/** A container with no image, from config or from the defaults. */
|
|
1749
|
+
var UnknownContainer = class extends Error {
|
|
1750
|
+
/** The container that could not be resolved. */
|
|
1751
|
+
container;
|
|
1752
|
+
/** The containers that have a default, so the caller can see the set. */
|
|
1753
|
+
known = Object.keys(DEFAULT_IMAGES).sort();
|
|
1754
|
+
constructor(container) {
|
|
1755
|
+
super("No image is known for this container; pin one in config");
|
|
1756
|
+
this.name = "UnknownContainer";
|
|
1757
|
+
this.container = container;
|
|
1223
1758
|
}
|
|
1224
|
-
|
|
1225
|
-
|
|
1226
|
-
|
|
1227
|
-
switch (
|
|
1228
|
-
case "
|
|
1229
|
-
|
|
1230
|
-
|
|
1231
|
-
|
|
1232
|
-
|
|
1233
|
-
|
|
1234
|
-
|
|
1759
|
+
};
|
|
1760
|
+
/** One container's definition, published on the port it was assigned. */
|
|
1761
|
+
function define(container, image, published) {
|
|
1762
|
+
switch (container) {
|
|
1763
|
+
case "postgres": return {
|
|
1764
|
+
image,
|
|
1765
|
+
restart: "unless-stopped",
|
|
1766
|
+
ports: published,
|
|
1767
|
+
environment: {
|
|
1768
|
+
POSTGRES_USER: LOCAL_USER,
|
|
1769
|
+
POSTGRES_PASSWORD: LOCAL_USER,
|
|
1770
|
+
POSTGRES_DB: LOCAL_USER
|
|
1771
|
+
},
|
|
1772
|
+
volumes: ["postgres-data:/var/lib/postgresql"],
|
|
1773
|
+
healthcheck: {
|
|
1774
|
+
test: ["CMD-SHELL", `pg_isready -U ${LOCAL_USER}`],
|
|
1775
|
+
interval: "5s",
|
|
1776
|
+
timeout: "3s",
|
|
1777
|
+
retries: 10,
|
|
1778
|
+
start_period: "10s"
|
|
1235
1779
|
}
|
|
1236
|
-
|
|
1237
|
-
|
|
1238
|
-
|
|
1239
|
-
|
|
1240
|
-
|
|
1241
|
-
|
|
1242
|
-
|
|
1243
|
-
|
|
1244
|
-
|
|
1245
|
-
|
|
1246
|
-
|
|
1247
|
-
|
|
1248
|
-
|
|
1249
|
-
|
|
1250
|
-
|
|
1251
|
-
|
|
1252
|
-
|
|
1253
|
-
|
|
1780
|
+
};
|
|
1781
|
+
case "minio": return {
|
|
1782
|
+
image,
|
|
1783
|
+
restart: "unless-stopped",
|
|
1784
|
+
ports: published,
|
|
1785
|
+
environment: {
|
|
1786
|
+
MINIO_ROOT_USER: LOCAL_USER,
|
|
1787
|
+
MINIO_ROOT_PASSWORD: LOCAL_USER
|
|
1788
|
+
},
|
|
1789
|
+
command: "server --console-address \":9001\" /data",
|
|
1790
|
+
volumes: ["minio-data:/data"],
|
|
1791
|
+
healthcheck: {
|
|
1792
|
+
test: [
|
|
1793
|
+
"CMD",
|
|
1794
|
+
"mc",
|
|
1795
|
+
"ready",
|
|
1796
|
+
"local"
|
|
1797
|
+
],
|
|
1798
|
+
interval: "10s",
|
|
1799
|
+
timeout: "5s",
|
|
1800
|
+
retries: 5,
|
|
1801
|
+
start_period: "10s"
|
|
1802
|
+
}
|
|
1803
|
+
};
|
|
1804
|
+
case "caddy": return {
|
|
1805
|
+
image,
|
|
1806
|
+
restart: "unless-stopped",
|
|
1807
|
+
ports: published,
|
|
1808
|
+
volumes: [
|
|
1809
|
+
"./.gkm/Caddyfile:/etc/caddy/Caddyfile:ro",
|
|
1810
|
+
"./.gkm/caddy-sites:/etc/caddy/sites:ro",
|
|
1811
|
+
"caddy-data:/data"
|
|
1812
|
+
],
|
|
1813
|
+
extra_hosts: ["host.docker.internal:host-gateway"],
|
|
1814
|
+
healthcheck: {
|
|
1815
|
+
test: [
|
|
1816
|
+
"CMD",
|
|
1817
|
+
"caddy",
|
|
1818
|
+
"version"
|
|
1819
|
+
],
|
|
1820
|
+
interval: "10s",
|
|
1821
|
+
timeout: "5s",
|
|
1822
|
+
retries: 5
|
|
1823
|
+
}
|
|
1824
|
+
};
|
|
1825
|
+
case "mailpit": return {
|
|
1826
|
+
image,
|
|
1827
|
+
restart: "unless-stopped",
|
|
1828
|
+
ports: published,
|
|
1829
|
+
environment: {
|
|
1830
|
+
MP_SMTP_AUTH_ACCEPT_ANY: "1",
|
|
1831
|
+
MP_SMTP_AUTH_ALLOW_INSECURE: "1"
|
|
1832
|
+
},
|
|
1833
|
+
healthcheck: {
|
|
1834
|
+
test: [
|
|
1835
|
+
"CMD",
|
|
1836
|
+
"wget",
|
|
1837
|
+
"-q",
|
|
1838
|
+
"--spider",
|
|
1839
|
+
"http://localhost:8025"
|
|
1840
|
+
],
|
|
1841
|
+
interval: "10s",
|
|
1842
|
+
timeout: "5s",
|
|
1843
|
+
retries: 5
|
|
1844
|
+
}
|
|
1845
|
+
};
|
|
1846
|
+
case "redis": return {
|
|
1847
|
+
image,
|
|
1848
|
+
restart: "unless-stopped",
|
|
1849
|
+
ports: published,
|
|
1850
|
+
volumes: ["redis-data:/data"],
|
|
1851
|
+
healthcheck: {
|
|
1852
|
+
test: [
|
|
1853
|
+
"CMD",
|
|
1854
|
+
"redis-cli",
|
|
1855
|
+
"ping"
|
|
1856
|
+
],
|
|
1857
|
+
interval: "5s",
|
|
1858
|
+
timeout: "3s",
|
|
1859
|
+
retries: 5
|
|
1860
|
+
}
|
|
1861
|
+
};
|
|
1862
|
+
case "redis-http": return {
|
|
1863
|
+
image,
|
|
1864
|
+
restart: "unless-stopped",
|
|
1865
|
+
ports: published,
|
|
1866
|
+
environment: {
|
|
1867
|
+
SRH_MODE: "env",
|
|
1868
|
+
SRH_TOKEN: LOCAL_TOKEN,
|
|
1869
|
+
SRH_CONNECTION_STRING: "redis://redis:6379"
|
|
1870
|
+
},
|
|
1871
|
+
depends_on: ["redis"],
|
|
1872
|
+
healthcheck: {
|
|
1873
|
+
test: [
|
|
1874
|
+
"CMD",
|
|
1875
|
+
"wget",
|
|
1876
|
+
"-q",
|
|
1877
|
+
"-O",
|
|
1878
|
+
"/dev/null",
|
|
1879
|
+
"http://127.0.0.1:80/"
|
|
1880
|
+
],
|
|
1881
|
+
interval: "5s",
|
|
1882
|
+
timeout: "3s",
|
|
1883
|
+
retries: 10
|
|
1254
1884
|
}
|
|
1255
|
-
|
|
1256
|
-
|
|
1257
|
-
|
|
1885
|
+
};
|
|
1886
|
+
case "rabbitmq": return {
|
|
1887
|
+
image,
|
|
1888
|
+
restart: "unless-stopped",
|
|
1889
|
+
ports: published,
|
|
1890
|
+
environment: {
|
|
1891
|
+
RABBITMQ_DEFAULT_USER: LOCAL_USER,
|
|
1892
|
+
RABBITMQ_DEFAULT_PASS: LOCAL_USER
|
|
1893
|
+
},
|
|
1894
|
+
volumes: ["rabbitmq-data:/var/lib/rabbitmq"],
|
|
1895
|
+
healthcheck: {
|
|
1896
|
+
test: [
|
|
1897
|
+
"CMD",
|
|
1898
|
+
"rabbitmq-diagnostics",
|
|
1899
|
+
"-q",
|
|
1900
|
+
"ping"
|
|
1901
|
+
],
|
|
1902
|
+
interval: "10s",
|
|
1903
|
+
timeout: "5s",
|
|
1904
|
+
retries: 5
|
|
1258
1905
|
}
|
|
1259
|
-
|
|
1260
|
-
case "
|
|
1261
|
-
|
|
1262
|
-
|
|
1263
|
-
|
|
1264
|
-
|
|
1265
|
-
|
|
1266
|
-
|
|
1267
|
-
|
|
1268
|
-
|
|
1269
|
-
|
|
1270
|
-
|
|
1271
|
-
|
|
1272
|
-
|
|
1273
|
-
|
|
1274
|
-
|
|
1275
|
-
|
|
1276
|
-
|
|
1277
|
-
|
|
1278
|
-
|
|
1279
|
-
|
|
1906
|
+
};
|
|
1907
|
+
case "localstack": return {
|
|
1908
|
+
image,
|
|
1909
|
+
restart: "unless-stopped",
|
|
1910
|
+
ports: published,
|
|
1911
|
+
extra_hosts: ["host.docker.internal:host-gateway"],
|
|
1912
|
+
environment: {
|
|
1913
|
+
AWS_DEFAULT_REGION: EMULATOR_REGION,
|
|
1914
|
+
AWS_ACCESS_KEY_ID: EMULATOR_CREDENTIALS.accessKeyId,
|
|
1915
|
+
AWS_SECRET_ACCESS_KEY: EMULATOR_CREDENTIALS.secretAccessKey
|
|
1916
|
+
},
|
|
1917
|
+
volumes: ["localstack-data:/var/lib/localstack"],
|
|
1918
|
+
healthcheck: {
|
|
1919
|
+
test: [
|
|
1920
|
+
"CMD",
|
|
1921
|
+
"bash",
|
|
1922
|
+
"-c",
|
|
1923
|
+
"exec 3<>/dev/tcp/localhost/4566"
|
|
1924
|
+
],
|
|
1925
|
+
interval: "10s",
|
|
1926
|
+
timeout: "5s",
|
|
1927
|
+
retries: 5,
|
|
1928
|
+
start_period: "10s"
|
|
1929
|
+
}
|
|
1930
|
+
};
|
|
1931
|
+
default: throw new UnknownContainer(container);
|
|
1280
1932
|
}
|
|
1281
1933
|
}
|
|
1282
|
-
/** A local Postgres URL, master credential and all. */
|
|
1283
|
-
function postgres(port, database, credential = {
|
|
1284
|
-
user: LOCAL_USER,
|
|
1285
|
-
password: LOCAL_USER
|
|
1286
|
-
}) {
|
|
1287
|
-
return `postgres://${credential.user}:${credential.password}@${LOCAL_HOST}:${port}/${database}`;
|
|
1288
|
-
}
|
|
1289
1934
|
/**
|
|
1290
|
-
* The
|
|
1935
|
+
* The published port mappings for a container.
|
|
1291
1936
|
*
|
|
1292
|
-
*
|
|
1293
|
-
*
|
|
1294
|
-
*
|
|
1295
|
-
* in Postgres, which is why the suffix is not optional here the way it is for a
|
|
1296
|
-
* schema.
|
|
1937
|
+
* Every port is looked up by its own key rather than being derived as an offset
|
|
1938
|
+
* from the container's first, so MinIO's console cannot land on whatever was
|
|
1939
|
+
* allocated to the next container.
|
|
1297
1940
|
*/
|
|
1298
|
-
function
|
|
1299
|
-
return
|
|
1941
|
+
function publish(container, ports, fakes) {
|
|
1942
|
+
return portsOf(container, fakes).map((port) => {
|
|
1943
|
+
const assigned = ports[port.key];
|
|
1944
|
+
if (assigned === void 0) throw new UnassignedPort(port.key);
|
|
1945
|
+
return `${assigned}:${port.inside}`;
|
|
1946
|
+
});
|
|
1300
1947
|
}
|
|
1948
|
+
/** A planned port that allocation never assigned. */
|
|
1949
|
+
var UnassignedPort = class extends Error {
|
|
1950
|
+
/** The port key that went unassigned, e.g. `minio-console`. */
|
|
1951
|
+
key;
|
|
1952
|
+
constructor(key) {
|
|
1953
|
+
super("No port was assigned for this container port");
|
|
1954
|
+
this.name = "UnassignedPort";
|
|
1955
|
+
this.key = key;
|
|
1956
|
+
}
|
|
1957
|
+
};
|
|
1958
|
+
//#endregion
|
|
1959
|
+
//#region src/reconcile/docker.ts
|
|
1301
1960
|
/**
|
|
1302
|
-
* The
|
|
1961
|
+
* The real Docker, behind the small interface reconcile needs.
|
|
1303
1962
|
*
|
|
1304
|
-
*
|
|
1305
|
-
*
|
|
1306
|
-
*
|
|
1307
|
-
* share one, and by the stage so `test` and `development` do not.
|
|
1963
|
+
* Shells out to `docker compose` rather than talking to the daemon socket: it is
|
|
1964
|
+
* what a developer runs by hand when something is wrong, so the two cannot
|
|
1965
|
+
* disagree, and it keeps Docker out of this package's dependencies.
|
|
1308
1966
|
*
|
|
1309
|
-
*
|
|
1310
|
-
*
|
|
1967
|
+
* Every call is scoped to the generated compose file. Nothing here can touch a
|
|
1968
|
+
* container this project did not generate.
|
|
1311
1969
|
*/
|
|
1312
|
-
|
|
1313
|
-
return createHash("sha256").update(`${project}:${plan.stage}:role:${role}`).digest("base64url").slice(0, 32);
|
|
1314
|
-
}
|
|
1970
|
+
const run = promisify(execFile);
|
|
1315
1971
|
/**
|
|
1316
|
-
*
|
|
1972
|
+
* The generated file, then the project's own if it has one.
|
|
1317
1973
|
*
|
|
1318
|
-
*
|
|
1319
|
-
*
|
|
1320
|
-
* two keys are one more thing to keep in step.
|
|
1974
|
+
* Merged in that order so the project's wins — an `image:` there pins a
|
|
1975
|
+
* container this file derived, and a service there is added beside them.
|
|
1321
1976
|
*/
|
|
1322
|
-
function
|
|
1323
|
-
const
|
|
1324
|
-
|
|
1325
|
-
|
|
1977
|
+
function composeFiles(composePath) {
|
|
1978
|
+
const project = join(dirname(composePath), "docker-compose.yml");
|
|
1979
|
+
return [
|
|
1980
|
+
"-f",
|
|
1981
|
+
composePath,
|
|
1982
|
+
...existsSync(project) ? ["-f", project] : []
|
|
1983
|
+
];
|
|
1326
1984
|
}
|
|
1327
|
-
/**
|
|
1328
|
-
|
|
1329
|
-
|
|
1330
|
-
|
|
1331
|
-
|
|
1332
|
-
|
|
1333
|
-
|
|
1334
|
-
|
|
1335
|
-
|
|
1336
|
-
|
|
1337
|
-
|
|
1338
|
-
const parent = byId.get(current.of);
|
|
1339
|
-
if (!parent) break;
|
|
1340
|
-
current = parent;
|
|
1985
|
+
/** How long to wait for containers to pass their health checks. */
|
|
1986
|
+
const HEALTH_TIMEOUT_MS = 12e4;
|
|
1987
|
+
/** `docker compose up` exited unsuccessfully; its own output said why. */
|
|
1988
|
+
var ComposeUpFailed = class extends Error {
|
|
1989
|
+
services;
|
|
1990
|
+
code;
|
|
1991
|
+
constructor(services, code) {
|
|
1992
|
+
super(`docker compose could not start ${services.join(", ")} (exit ${code ?? "signal"}). Its output above names the container; \`docker compose logs <service>\` shows why it stopped.`);
|
|
1993
|
+
this.services = services;
|
|
1994
|
+
this.code = code;
|
|
1995
|
+
this.name = "ComposeUpFailed";
|
|
1341
1996
|
}
|
|
1342
|
-
|
|
1997
|
+
};
|
|
1998
|
+
/** Run compose with its output on this terminal rather than captured. */
|
|
1999
|
+
function shown(args, services) {
|
|
2000
|
+
return new Promise((resolve, reject) => {
|
|
2001
|
+
const child = spawn("docker", args, { stdio: [
|
|
2002
|
+
"ignore",
|
|
2003
|
+
"inherit",
|
|
2004
|
+
"inherit"
|
|
2005
|
+
] });
|
|
2006
|
+
child.on("error", reject);
|
|
2007
|
+
child.on("close", (code) => code === 0 ? resolve() : reject(new ComposeUpFailed(services, code)));
|
|
2008
|
+
});
|
|
1343
2009
|
}
|
|
1344
|
-
|
|
1345
|
-
|
|
1346
|
-
|
|
1347
|
-
|
|
1348
|
-
|
|
1349
|
-
|
|
1350
|
-
|
|
2010
|
+
const dockerCli = {
|
|
2011
|
+
async publishedPort(composePath, service, inside) {
|
|
2012
|
+
try {
|
|
2013
|
+
const { stdout } = await run("docker", [
|
|
2014
|
+
"compose",
|
|
2015
|
+
...composeFiles(composePath),
|
|
2016
|
+
"port",
|
|
2017
|
+
service,
|
|
2018
|
+
String(inside)
|
|
2019
|
+
]);
|
|
2020
|
+
const port = Number(stdout.trim().split(":").pop());
|
|
2021
|
+
return Number.isFinite(port) && port > 0 ? port : void 0;
|
|
2022
|
+
} catch {
|
|
2023
|
+
return;
|
|
2024
|
+
}
|
|
2025
|
+
},
|
|
2026
|
+
async up(composePath, services, options = {}) {
|
|
2027
|
+
const args = [
|
|
2028
|
+
"compose",
|
|
2029
|
+
...composeFiles(composePath),
|
|
2030
|
+
"up",
|
|
2031
|
+
"-d",
|
|
2032
|
+
"--remove-orphans",
|
|
2033
|
+
"--wait",
|
|
2034
|
+
`--wait-timeout=${Math.floor(HEALTH_TIMEOUT_MS / 1e3)}`,
|
|
2035
|
+
...services
|
|
2036
|
+
];
|
|
2037
|
+
if (options.show) return shown(args, services);
|
|
2038
|
+
await run("docker", args);
|
|
2039
|
+
},
|
|
2040
|
+
async copyOut(composePath, service, from, to) {
|
|
2041
|
+
await mkdir(dirname(to), { recursive: true });
|
|
2042
|
+
await run("docker", [
|
|
2043
|
+
"compose",
|
|
2044
|
+
...composeFiles(composePath),
|
|
2045
|
+
"cp",
|
|
2046
|
+
`${service}:${from}`,
|
|
2047
|
+
to
|
|
2048
|
+
]);
|
|
2049
|
+
},
|
|
2050
|
+
async reload(composePath, service) {
|
|
2051
|
+
await run("docker", [
|
|
2052
|
+
"compose",
|
|
2053
|
+
...composeFiles(composePath),
|
|
2054
|
+
"exec",
|
|
2055
|
+
"-T",
|
|
2056
|
+
service,
|
|
2057
|
+
"caddy",
|
|
2058
|
+
"reload",
|
|
2059
|
+
"--config",
|
|
2060
|
+
"/etc/caddy/Caddyfile"
|
|
2061
|
+
]);
|
|
2062
|
+
},
|
|
2063
|
+
async healthy(composePath, services) {
|
|
2064
|
+
if (services.length === 0) return true;
|
|
2065
|
+
try {
|
|
2066
|
+
const { stdout } = await run("docker", [
|
|
2067
|
+
"compose",
|
|
2068
|
+
...composeFiles(composePath),
|
|
2069
|
+
"ps",
|
|
2070
|
+
"--format",
|
|
2071
|
+
"json",
|
|
2072
|
+
...services
|
|
2073
|
+
]);
|
|
2074
|
+
const running = /* @__PURE__ */ new Set();
|
|
2075
|
+
for (const line of stdout.trim().split("\n").filter(Boolean)) {
|
|
2076
|
+
const entry = JSON.parse(line);
|
|
2077
|
+
if (entry.State === "running" && (entry.Health === void 0 || entry.Health === "" || entry.Health === "healthy") && entry.Service) running.add(entry.Service);
|
|
2078
|
+
}
|
|
2079
|
+
return services.every((service) => running.has(service));
|
|
2080
|
+
} catch {
|
|
2081
|
+
return false;
|
|
2082
|
+
}
|
|
1351
2083
|
}
|
|
1352
|
-
|
|
1353
|
-
|
|
2084
|
+
};
|
|
2085
|
+
//#endregion
|
|
2086
|
+
//#region src/reconcile/lan.ts
|
|
1354
2087
|
/**
|
|
1355
|
-
*
|
|
1356
|
-
*
|
|
1357
|
-
* SNS and SQS are addressed by ARN, which means the topic and queue have to be
|
|
1358
|
-
* created in LocalStack before a URL can name them. Until that provisioning
|
|
1359
|
-
* lands, saying so is better than handing out a string that fails on first use.
|
|
2088
|
+
* This machine's address on the local network — what a phone running the app
|
|
2089
|
+
* reaches it on.
|
|
1360
2090
|
*/
|
|
1361
|
-
var UnprovisionedEventsBackend = class extends Error {
|
|
1362
|
-
constructor(backend) {
|
|
1363
|
-
super(`The local target cannot derive addresses for '${backend}' yet — its topics and queues are named by ARN, which nothing creates locally. Use 'pgboss' (the default) or 'rabbitmq' for local development.`);
|
|
1364
|
-
this.backend = backend;
|
|
1365
|
-
this.name = "UnprovisionedEventsBackend";
|
|
1366
|
-
}
|
|
1367
|
-
};
|
|
1368
2091
|
/**
|
|
1369
|
-
* The
|
|
2092
|
+
* The first private IPv4 address of an interface that is up and not loopback.
|
|
1370
2093
|
*
|
|
1371
|
-
*
|
|
1372
|
-
*
|
|
1373
|
-
*
|
|
1374
|
-
*
|
|
2094
|
+
* Private ranges only (`10/8`, `172.16/12`, `192.168/16`): a public address
|
|
2095
|
+
* would mean listening for a phone on the open internet, and a link-local one
|
|
2096
|
+
* (`169.254/16`) is what an interface has when it found no network at all.
|
|
2097
|
+
* Undefined when there is none — a laptop on no network still runs, its mobile
|
|
2098
|
+
* app reaching only a simulator.
|
|
1375
2099
|
*/
|
|
1376
|
-
function
|
|
1377
|
-
|
|
2100
|
+
function lanAddress(interfaces = networkInterfaces()) {
|
|
2101
|
+
for (const entries of Object.values(interfaces)) for (const entry of entries ?? []) {
|
|
2102
|
+
if (entry.family !== "IPv4" || entry.internal) continue;
|
|
2103
|
+
if (isPrivate(entry.address)) return entry.address;
|
|
2104
|
+
}
|
|
2105
|
+
}
|
|
2106
|
+
function isPrivate(address) {
|
|
2107
|
+
return /^10\./.test(address) || /^192\.168\./.test(address) || /^172\.(1[6-9]|2\d|3[01])\./.test(address);
|
|
1378
2108
|
}
|
|
1379
|
-
|
|
1380
2109
|
//#endregion
|
|
1381
2110
|
//#region src/reconcile/ports.ts
|
|
1382
2111
|
/**
|
|
2112
|
+
* Port allocation — why two projects can run at once.
|
|
2113
|
+
*
|
|
2114
|
+
* Publishing `5432:5432` means the second project on your machine cannot start.
|
|
2115
|
+
* Nothing solves that except giving each project its own ports, and what makes
|
|
2116
|
+
* it *possible* is the single-`<NAME>_URL` rule: an app reads `ORDERS_URL` and
|
|
2117
|
+
* never sees a host or a port, so reconcile is free to bind whatever is
|
|
2118
|
+
* available and compose the URL from what it bound. An app reading `DB_HOST` and
|
|
2119
|
+
* `DB_PORT` separately could not be moved this way.
|
|
2120
|
+
*
|
|
2121
|
+
* Assignments are **persisted, not recomputed**. Recomputing means adding a
|
|
2122
|
+
* construct renumbers its neighbours, and every running container, saved
|
|
2123
|
+
* connection string, and `psql` history goes stale at once.
|
|
2124
|
+
*
|
|
2125
|
+
* Pure, with the liveness probe injected — a collision with another project's
|
|
2126
|
+
* stack is a fact about the machine, and keeping it a parameter is what lets the
|
|
2127
|
+
* rules be asserted without binding a socket.
|
|
2128
|
+
*/
|
|
2129
|
+
/**
|
|
1383
2130
|
* The window ports are drawn from.
|
|
1384
2131
|
*
|
|
1385
2132
|
* Above the registered range so nothing here needs privileges, and below the
|
|
@@ -1437,6 +2184,27 @@ async function allocate(project, containers, existing = {}, probe = isPortFree)
|
|
|
1437
2184
|
}
|
|
1438
2185
|
return assignments;
|
|
1439
2186
|
}
|
|
2187
|
+
/**
|
|
2188
|
+
* The ports to keep: what running containers are observed on, then what was
|
|
2189
|
+
* saved — minus any saved entry whose port something else already holds.
|
|
2190
|
+
*
|
|
2191
|
+
* Spreading one over the other let an observed port override its own key and
|
|
2192
|
+
* leave a *different* key still saved on the same number: two services on one
|
|
2193
|
+
* port, and whichever answered first won. Saved keys for containers the plan no
|
|
2194
|
+
* longer has — a backend switched and back — were the usual source. Observed
|
|
2195
|
+
* wins because it is what is actually bound; a dropped entry is simply
|
|
2196
|
+
* allocated again.
|
|
2197
|
+
*/
|
|
2198
|
+
function keptPorts(saved = {}, observed = {}) {
|
|
2199
|
+
const kept = { ...observed };
|
|
2200
|
+
const taken = new Set(Object.values(observed));
|
|
2201
|
+
for (const [key, port] of Object.entries(saved).sort(([a], [b]) => a < b ? -1 : 1)) {
|
|
2202
|
+
if (key in kept || taken.has(port)) continue;
|
|
2203
|
+
kept[key] = port;
|
|
2204
|
+
taken.add(port);
|
|
2205
|
+
}
|
|
2206
|
+
return kept;
|
|
2207
|
+
}
|
|
1440
2208
|
/** The window was exhausted. */
|
|
1441
2209
|
var NoPortAvailable = class extends Error {
|
|
1442
2210
|
/** Where the search started. */
|
|
@@ -1477,17 +2245,33 @@ async function nextFree(from, taken, probe) {
|
|
|
1477
2245
|
* rather than as a connection failure ten minutes later.
|
|
1478
2246
|
*/
|
|
1479
2247
|
function isPortFree(port) {
|
|
1480
|
-
return new Promise((resolve
|
|
2248
|
+
return new Promise((resolve) => {
|
|
1481
2249
|
const server = createServer();
|
|
1482
|
-
server.once("error", () => resolve
|
|
1483
|
-
server.once("listening", () => server.close(() => resolve
|
|
2250
|
+
server.once("error", () => resolve(false));
|
|
2251
|
+
server.once("listening", () => server.close(() => resolve(true)));
|
|
1484
2252
|
server.listen(port, "0.0.0.0");
|
|
1485
2253
|
});
|
|
1486
2254
|
}
|
|
1487
|
-
|
|
1488
2255
|
//#endregion
|
|
1489
2256
|
//#region src/reconcile/provision.ts
|
|
1490
2257
|
/**
|
|
2258
|
+
* Creating what the URLs name.
|
|
2259
|
+
*
|
|
2260
|
+
* Convergent and idempotent: every statement asks whether the thing exists
|
|
2261
|
+
* first, so reconciling repeatedly is free and a half-applied run recovers by
|
|
2262
|
+
* being run again. That property is what lets `gkm dev` do this on every start.
|
|
2263
|
+
*
|
|
2264
|
+
* The line the design draws is at data, not at side effects — this creates
|
|
2265
|
+
* databases, schemas, and buckets, and it never seeds, resets, or drops. A
|
|
2266
|
+
* developer who wants their data gone asks for it explicitly.
|
|
2267
|
+
*
|
|
2268
|
+
* Statement generation is separated from application for the reason the design
|
|
2269
|
+
* gives: the same DDL has to run in-process locally and from a provisioner in a
|
|
2270
|
+
* VPC, and one implementation is what stops "works locally, fails deployed".
|
|
2271
|
+
* When the role DDL lands in `@geekmidas/db` this delegates to it rather than
|
|
2272
|
+
* growing its own.
|
|
2273
|
+
*/
|
|
2274
|
+
/**
|
|
1491
2275
|
* The Postgres work a plan implies.
|
|
1492
2276
|
*
|
|
1493
2277
|
* Databases before the schemas inside them, which `provisionOrder` already
|
|
@@ -1584,6 +2368,7 @@ function rolesFor(resource, schema, plan, project) {
|
|
|
1584
2368
|
owner,
|
|
1585
2369
|
...reader ? { reader } : {},
|
|
1586
2370
|
schema,
|
|
2371
|
+
...resource.kind === "database" ? { database: rootDatabase(resource, plan) } : {},
|
|
1587
2372
|
passwords: {
|
|
1588
2373
|
runtime: localRolePassword(project, plan, runtime),
|
|
1589
2374
|
owner: localRolePassword(project, plan, owner),
|
|
@@ -1629,8 +2414,7 @@ async function applyPostgres(client, statements) {
|
|
|
1629
2414
|
});
|
|
1630
2415
|
continue;
|
|
1631
2416
|
}
|
|
1632
|
-
|
|
1633
|
-
if (rows.length > 0) {
|
|
2417
|
+
if ((await client.query(statement.database, statement.exists.sql, statement.exists.values)).length > 0) {
|
|
1634
2418
|
applied.push({
|
|
1635
2419
|
id: statement.id,
|
|
1636
2420
|
describe: statement.describe,
|
|
@@ -1697,6 +2481,66 @@ function bucketPolicy(bucket, open) {
|
|
|
1697
2481
|
});
|
|
1698
2482
|
}
|
|
1699
2483
|
/**
|
|
2484
|
+
* The topics and queues to create on the AWS emulator.
|
|
2485
|
+
*
|
|
2486
|
+
* Only on the `sns` backend: pg-boss makes its own tables on first connect and
|
|
2487
|
+
* a RabbitMQ exchange is declared by the client, but an SNS topic and an SQS
|
|
2488
|
+
* queue must exist before anything can publish to them.
|
|
2489
|
+
*/
|
|
2490
|
+
function carrierNames(plan) {
|
|
2491
|
+
if (plan.events !== "sns") return {
|
|
2492
|
+
topics: [],
|
|
2493
|
+
queues: []
|
|
2494
|
+
};
|
|
2495
|
+
const named = (kind) => plan.resources.filter((r) => r.kind === kind).map((r) => r.name);
|
|
2496
|
+
return {
|
|
2497
|
+
topics: named("topic"),
|
|
2498
|
+
queues: named("queue")
|
|
2499
|
+
};
|
|
2500
|
+
}
|
|
2501
|
+
/** Create each topic and queue the plan names, skipping those that exist. */
|
|
2502
|
+
async function applyCarriers(client, carriers) {
|
|
2503
|
+
const applied = [];
|
|
2504
|
+
for (const name of carriers.topics) {
|
|
2505
|
+
const created = !await client.topicExists(name);
|
|
2506
|
+
if (created) await client.createTopic(name);
|
|
2507
|
+
applied.push({
|
|
2508
|
+
id: name,
|
|
2509
|
+
describe: `topic ${name}`,
|
|
2510
|
+
created
|
|
2511
|
+
});
|
|
2512
|
+
}
|
|
2513
|
+
for (const name of carriers.queues) {
|
|
2514
|
+
const created = !await client.queueExists(name);
|
|
2515
|
+
if (created) await client.createQueue(name);
|
|
2516
|
+
applied.push({
|
|
2517
|
+
id: name,
|
|
2518
|
+
describe: `queue ${name}`,
|
|
2519
|
+
created
|
|
2520
|
+
});
|
|
2521
|
+
}
|
|
2522
|
+
return applied;
|
|
2523
|
+
}
|
|
2524
|
+
/** The Postgres databases the plan creates — each is a `CREATE DATABASE`. */
|
|
2525
|
+
function postgresDatabaseNames(plan) {
|
|
2526
|
+
return plan.resources.filter((resource) => resource.kind === "database" && resource.provisions && resource.container === "postgres").map((resource) => resource.name);
|
|
2527
|
+
}
|
|
2528
|
+
/**
|
|
2529
|
+
* Whether every one of these databases exists. A database dropped behind the
|
|
2530
|
+
* recorded state's back — by another checkout's test teardown, against the
|
|
2531
|
+
* same container — makes the state a lie, and only asking Postgres finds out.
|
|
2532
|
+
* Unreachable counts as missing: applying again is what finds out why.
|
|
2533
|
+
*/
|
|
2534
|
+
async function databasesExist(client, names) {
|
|
2535
|
+
if (names.length === 0) return true;
|
|
2536
|
+
try {
|
|
2537
|
+
const rows = await client.query(void 0, "SELECT datname FROM pg_catalog.pg_database WHERE datname = ANY($1)", [names]);
|
|
2538
|
+
return new Set(rows.map((row) => row.datname)).size === names.length;
|
|
2539
|
+
} catch {
|
|
2540
|
+
return false;
|
|
2541
|
+
}
|
|
2542
|
+
}
|
|
2543
|
+
/**
|
|
1700
2544
|
* Apply each served bucket's open-path policy, skipping those already correct.
|
|
1701
2545
|
*
|
|
1702
2546
|
* Compared as parsed JSON rather than as text: the same policy re-serialised
|
|
@@ -1755,9 +2599,20 @@ async function applyBuckets(client, buckets) {
|
|
|
1755
2599
|
}
|
|
1756
2600
|
return applied;
|
|
1757
2601
|
}
|
|
1758
|
-
|
|
1759
2602
|
//#endregion
|
|
1760
2603
|
//#region src/reconcile/state.ts
|
|
2604
|
+
/**
|
|
2605
|
+
* What reconcile records, and the hash that lets it do nothing.
|
|
2606
|
+
*
|
|
2607
|
+
* Reconciling on every `gkm dev` is only acceptable if the converged case is
|
|
2608
|
+
* free, so the desired plan is hashed: matching hash plus healthy containers
|
|
2609
|
+
* means there is nothing to do. Without that, the automatic reconcile the design
|
|
2610
|
+
* asks for would tax every start.
|
|
2611
|
+
*
|
|
2612
|
+
* Almost nothing needs recording — roles are queried, migrations have their own
|
|
2613
|
+
* table, containers are remembered by Docker. Only ports and this hash have no
|
|
2614
|
+
* other owner.
|
|
2615
|
+
*/
|
|
1761
2616
|
const STATE_DIR = ".gkm/reconcile";
|
|
1762
2617
|
/**
|
|
1763
2618
|
* The hash of everything a reconcile would act on.
|
|
@@ -1786,7 +2641,7 @@ async function loadState(root, stage) {
|
|
|
1786
2641
|
const state = JSON.parse(raw);
|
|
1787
2642
|
return state.stage === stage ? state : void 0;
|
|
1788
2643
|
} catch {
|
|
1789
|
-
return
|
|
2644
|
+
return;
|
|
1790
2645
|
}
|
|
1791
2646
|
}
|
|
1792
2647
|
/** Record what was applied, so the next run can skip it. */
|
|
@@ -1795,14 +2650,46 @@ async function saveState(root, state) {
|
|
|
1795
2650
|
await mkdir(dirname(path), { recursive: true });
|
|
1796
2651
|
await writeFile(path, `${JSON.stringify(state, null, 2)}\n`);
|
|
1797
2652
|
}
|
|
2653
|
+
/**
|
|
2654
|
+
* Forget what was applied, so the next reconcile of this stage converges from
|
|
2655
|
+
* nothing — creating again whatever was dropped underneath the record.
|
|
2656
|
+
*/
|
|
2657
|
+
async function forgetState(root, stage) {
|
|
2658
|
+
await rm(statePath(root, stage), { force: true });
|
|
2659
|
+
}
|
|
1798
2660
|
function statePath(root, stage) {
|
|
1799
2661
|
return join(root, STATE_DIR, `${stage}.json`);
|
|
1800
2662
|
}
|
|
1801
|
-
|
|
1802
2663
|
//#endregion
|
|
1803
2664
|
//#region src/reconcile/index.ts
|
|
1804
|
-
/**
|
|
1805
|
-
|
|
2665
|
+
/**
|
|
2666
|
+
* Reconcile — the one convergent function `gkm dev`, `gkm test`, and `gkm setup`
|
|
2667
|
+
* all call.
|
|
2668
|
+
*
|
|
2669
|
+
* Three phases, and only this one has side effects: declaring is pure (`gkm
|
|
2670
|
+
* build` and the dev watcher), running reads `<NAME>_URL` and provisions
|
|
2671
|
+
* nothing. Reconcile computes the desired state, compares, and applies the
|
|
2672
|
+
* difference — which is what makes "when does it run" answerable with
|
|
2673
|
+
* *whenever, repeatedly*.
|
|
2674
|
+
*
|
|
2675
|
+
* It is safe to run automatically because its blast radius is entirely local:
|
|
2676
|
+
* this project's containers, this project's `.gkm/`. Nothing it does can reach a
|
|
2677
|
+
* cloud. The line is drawn at data, not at side effects — it allocates ports,
|
|
2678
|
+
* writes compose, starts containers, and creates roles; it never seeds, resets,
|
|
2679
|
+
* or drops.
|
|
2680
|
+
*
|
|
2681
|
+
* Docker is injected rather than imported so the rules above can be asserted
|
|
2682
|
+
* without a daemon. The default implementation is the real one.
|
|
2683
|
+
*/
|
|
2684
|
+
/**
|
|
2685
|
+
* Where the generated compose file is written, relative to the project root.
|
|
2686
|
+
*
|
|
2687
|
+
* At the root, beside the project's own `docker-compose.yml`, because the two
|
|
2688
|
+
* are one stack: this file is everything the constructs imply and is never
|
|
2689
|
+
* edited, that one is anything they cannot express — an image pin, an extra
|
|
2690
|
+
* service — and is merged over it, so it wins.
|
|
2691
|
+
*/
|
|
2692
|
+
const COMPOSE_PATH = "docker-compose.constructs.yml";
|
|
1806
2693
|
/**
|
|
1807
2694
|
* The local edge's config, beside the compose file that mounts it.
|
|
1808
2695
|
*
|
|
@@ -1838,32 +2725,37 @@ const CADDY_ROOT_IN_CONTAINER = "/data/caddy/pki/authorities/local/root.crt";
|
|
|
1838
2725
|
* hash and one health check.
|
|
1839
2726
|
*/
|
|
1840
2727
|
async function reconcile(options) {
|
|
1841
|
-
const { root, project, manifest, stage, start = true, provision = true, docker = dockerCli, probe = isPortFree, sql = pgClient, buckets = bucketClient } = options;
|
|
2728
|
+
const { root, project, manifest, stage, start = true, provision = true, docker = dockerCli, probe = isPortFree, sql = pgClient, buckets = bucketClient, carriers = carrierClient } = options;
|
|
1842
2729
|
const plan = planFor(manifest, stage, provisionOrder(manifest), {
|
|
2730
|
+
localStage: options.localStage,
|
|
1843
2731
|
events: options.events,
|
|
1844
2732
|
cache: options.cache,
|
|
1845
2733
|
extraContainers: options.extraContainers,
|
|
2734
|
+
...options.fakes ? { fakes: options.fakes } : {},
|
|
1846
2735
|
...options.edge === void 0 ? {} : { edge: options.edge }
|
|
1847
2736
|
});
|
|
1848
2737
|
const composePath = join(root, COMPOSE_PATH);
|
|
1849
|
-
const observed = await observedPorts(docker, composePath, plan.containers);
|
|
1850
|
-
const ports = await allocate(project, portKeys(plan.containers),
|
|
1851
|
-
...options.saved,
|
|
1852
|
-
...observed
|
|
1853
|
-
}, probe);
|
|
2738
|
+
const observed = await observedPorts(docker, composePath, plan.containers, plan.fakes);
|
|
2739
|
+
const ports = await allocate(project, portKeys(plan.containers, plan.fakes), keptPorts(options.saved, observed), probe);
|
|
1854
2740
|
const compose = composeFor(plan, {
|
|
1855
2741
|
project,
|
|
1856
2742
|
ports,
|
|
1857
|
-
...options.
|
|
2743
|
+
...options.apps ? { apps: options.apps(plan.containers) } : {}
|
|
1858
2744
|
});
|
|
1859
2745
|
const caddyfile = toCaddyfile(sitesFor(plan, project, options.addresses));
|
|
1860
|
-
const hash = planHash(plan, compose, {
|
|
1861
|
-
|
|
2746
|
+
const hash = planHash(plan, compose, {
|
|
2747
|
+
caddyfile,
|
|
2748
|
+
postgres: postgresStatements(plan, project).map((statement) => statement.create).join(";\n")
|
|
2749
|
+
});
|
|
2750
|
+
const services = serviceAddresses(plan.containers, ports, plan.fakes);
|
|
2751
|
+
const lan = plan.resources.some((r) => r.kind === "mobile-app") ? options.lanAddress === void 0 ? lanAddress() : options.lanAddress ?? void 0 : void 0;
|
|
1862
2752
|
const env = envFor(plan, {
|
|
1863
2753
|
ports,
|
|
1864
2754
|
project,
|
|
1865
2755
|
...options.mailFrom ? { mailFrom: options.mailFrom } : {},
|
|
1866
|
-
...options.addresses ? { addresses: options.addresses } : {}
|
|
2756
|
+
...options.addresses ? { addresses: options.addresses } : {},
|
|
2757
|
+
...lan ? { lanAddress: lan } : {},
|
|
2758
|
+
...options.metroPorts ? { metroPorts: options.metroPorts } : {}
|
|
1867
2759
|
});
|
|
1868
2760
|
if (plan.containers.includes("caddy")) env.NODE_EXTRA_CA_CERTS = join(root, LOCAL_CA_PATH);
|
|
1869
2761
|
const result = {
|
|
@@ -1871,678 +2763,255 @@ async function reconcile(options) {
|
|
|
1871
2763
|
plan,
|
|
1872
2764
|
compose,
|
|
1873
2765
|
ports,
|
|
1874
|
-
|
|
2766
|
+
services,
|
|
1875
2767
|
env,
|
|
1876
2768
|
provisioned: [],
|
|
2769
|
+
fakes: options.fakes ?? {},
|
|
1877
2770
|
hash,
|
|
1878
2771
|
changed: false
|
|
1879
2772
|
};
|
|
1880
2773
|
const recorded = await loadState(root, stage);
|
|
1881
|
-
const
|
|
1882
|
-
if (
|
|
2774
|
+
const postgresPort = ports[primaryPortKey("postgres")];
|
|
2775
|
+
if (recorded?.hash === hash && existsSync(composePath) && (!start || await docker.healthy(composePath, plan.containers)) && (!start || !provision || postgresPort === void 0 || await databasesExist(sql(postgresPort), postgresDatabaseNames(plan)))) return result;
|
|
1883
2776
|
await write(composePath, toYaml(compose));
|
|
1884
2777
|
if (plan.containers.includes("caddy")) {
|
|
1885
2778
|
await write(join(root, CADDYFILE_PATH), caddyfileRoot());
|
|
1886
|
-
await write(join(root, caddySitesPath(stage)), caddyfile);
|
|
1887
|
-
|
|
1888
|
-
|
|
1889
|
-
|
|
1890
|
-
|
|
1891
|
-
|
|
1892
|
-
await saveState(root, {
|
|
1893
|
-
hash,
|
|
1894
|
-
stage
|
|
1895
|
-
});
|
|
1896
|
-
return {
|
|
1897
|
-
...result,
|
|
1898
|
-
provisioned,
|
|
1899
|
-
changed: true
|
|
1900
|
-
};
|
|
1901
|
-
}
|
|
1902
|
-
/**
|
|
1903
|
-
* Create the databases, schemas, and buckets the plan names.
|
|
1904
|
-
*
|
|
1905
|
-
* Idempotent throughout, so this runs on every non-converged reconcile rather
|
|
1906
|
-
* than being something to remember.
|
|
1907
|
-
*/
|
|
1908
|
-
async function create(plan, ports, sql, buckets, project) {
|
|
1909
|
-
const applied = [];
|
|
1910
|
-
const postgresPort = ports[primaryPortKey("postgres")];
|
|
1911
|
-
const statements = postgresStatements(plan, project);
|
|
1912
|
-
if (postgresPort !== void 0 && statements.length > 0) applied.push(...await applyPostgres(sql(postgresPort), statements));
|
|
1913
|
-
const minioPort = ports[primaryPortKey("minio")];
|
|
1914
|
-
const names = bucketNames(plan);
|
|
1915
|
-
const policies = bucketPolicies(plan);
|
|
1916
|
-
if (minioPort !== void 0 && (names.length > 0 || policies.length > 0)) {
|
|
1917
|
-
const client = buckets(minioPort);
|
|
1918
|
-
if (names.length > 0) applied.push(...await applyBuckets(client, names));
|
|
1919
|
-
if (policies.length > 0) applied.push(...await applyPolicies(client, policies));
|
|
1920
|
-
}
|
|
1921
|
-
return applied;
|
|
1922
|
-
}
|
|
1923
|
-
/**
|
|
1924
|
-
* The host ports of containers already running.
|
|
1925
|
-
*
|
|
1926
|
-
* Keyed the same way allocation keys them, so the two merge without either
|
|
1927
|
-
* knowing about the other.
|
|
1928
|
-
*/
|
|
1929
|
-
async function observedPorts(docker, composePath, containers) {
|
|
1930
|
-
const observed = {};
|
|
1931
|
-
for (const container of containers) for (const port of portsOf(container)) {
|
|
1932
|
-
const running = await docker.publishedPort(composePath, container, port.inside);
|
|
1933
|
-
if (running !== void 0) observed[port.key] = running;
|
|
1934
|
-
}
|
|
1935
|
-
return observed;
|
|
1936
|
-
}
|
|
1937
|
-
/**
|
|
1938
|
-
* Where each container can be reached on this machine.
|
|
1939
|
-
*
|
|
1940
|
-
* The primary port only — a container's console is for a human to open, not
|
|
1941
|
-
* something an app connects to.
|
|
1942
|
-
*/
|
|
1943
|
-
function addressesFor(containers, ports) {
|
|
1944
|
-
const addresses = {};
|
|
1945
|
-
for (const container of containers) {
|
|
1946
|
-
const port = ports[primaryPortKey(container)];
|
|
1947
|
-
if (port !== void 0) addresses[container] = `localhost:${port}`;
|
|
1948
|
-
}
|
|
1949
|
-
return addresses;
|
|
1950
|
-
}
|
|
1951
|
-
/** Write a file only when its content would change, so mtimes stay meaningful. */
|
|
1952
|
-
async function write(path, content) {
|
|
1953
|
-
try {
|
|
1954
|
-
if (await readFile(path, "utf-8") === content) return;
|
|
1955
|
-
} catch {}
|
|
1956
|
-
await mkdir(dirname(path), { recursive: true });
|
|
1957
|
-
await writeFile(path, content);
|
|
1958
|
-
}
|
|
1959
|
-
|
|
1960
|
-
//#endregion
|
|
1961
|
-
//#region src/reconcile/workspace.ts
|
|
1962
|
-
/**
|
|
1963
|
-
* Every constructs glob in the workspace, resolved against its app.
|
|
1964
|
-
*
|
|
1965
|
-
* Absolute because discovery globs from a single cwd while apps live in
|
|
1966
|
-
* different directories.
|
|
1967
|
-
*/
|
|
1968
|
-
function constructGlobs(workspace) {
|
|
1969
|
-
return allConstructGlobs(workspace);
|
|
1970
|
-
}
|
|
1971
|
-
/** Whether any app has adopted the constructs glob. */
|
|
1972
|
-
function usesConstructs(workspace) {
|
|
1973
|
-
return constructGlobs(workspace).length > 0;
|
|
1974
|
-
}
|
|
1975
|
-
/**
|
|
1976
|
-
* Image pins, by container. The config half of the derived/config split.
|
|
1977
|
-
*
|
|
1978
|
-
* Read from `services.images` rather than from the backend keys beside it.
|
|
1979
|
-
* They used to share one key, which is how `cache: true` came to mean "start a
|
|
1980
|
-
* Redis" — the last way a container could exist because config asked for one
|
|
1981
|
-
* instead of because something declared it.
|
|
1982
|
-
*/
|
|
1983
|
-
function imagePins(workspace) {
|
|
1984
|
-
return { ...workspace.services.images };
|
|
1985
|
-
}
|
|
1986
|
-
/**
|
|
1987
|
-
* The containers a workspace's declarations imply, without reconciling.
|
|
1988
|
-
*
|
|
1989
|
-
* The same derivation `reconcileWorkspace` makes, available to callers that
|
|
1990
|
-
* need to know *what would run* without starting anything or writing ports —
|
|
1991
|
-
* `gkm setup` deciding which credentials to generate, `gkm docker` writing a
|
|
1992
|
-
* compose file. Those used to each read a boolean per service out of config,
|
|
1993
|
-
* which is how a container could exist because config asked for one rather than
|
|
1994
|
-
* because something declared it.
|
|
1995
|
-
*/
|
|
1996
|
-
async function derivedContainers(workspace, stage, manifest) {
|
|
1997
|
-
const found = manifest ?? await discover({
|
|
1998
|
-
patterns: constructGlobs(workspace),
|
|
1999
|
-
cwd: workspace.root
|
|
2000
|
-
});
|
|
2001
|
-
return planFor(found, stage, provisionOrder(found), {
|
|
2002
|
-
events: workspace.services.events,
|
|
2003
|
-
cache: cacheBackendOf(workspace.services.cache, providerOf(workspace))
|
|
2004
|
-
}).containers;
|
|
2005
|
-
}
|
|
2006
|
-
/**
|
|
2007
|
-
* Reconcile a workspace for one stage.
|
|
2008
|
-
*
|
|
2009
|
-
* Ports are loaded and saved around the call rather than inside it, so the loop
|
|
2010
|
-
* stays a pure-ish function of its inputs and the store keeps its single owner.
|
|
2011
|
-
*/
|
|
2012
|
-
async function reconcileWorkspace(workspace, options) {
|
|
2013
|
-
const manifest = options.manifest ?? await discover({
|
|
2014
|
-
patterns: constructGlobs(workspace),
|
|
2015
|
-
cwd: workspace.root
|
|
2016
|
-
});
|
|
2017
|
-
const result = await reconcile({
|
|
2018
|
-
root: workspace.root,
|
|
2019
|
-
project: workspace.name,
|
|
2020
|
-
manifest,
|
|
2021
|
-
stage: options.stage,
|
|
2022
|
-
events: workspace.services.events,
|
|
2023
|
-
cache: cacheBackendOf(workspace.services.cache, providerOf(workspace)),
|
|
2024
|
-
images: imagePins(workspace),
|
|
2025
|
-
saved: await loadPortState(workspace.root),
|
|
2026
|
-
addresses: surfaceAddresses(workspace, manifest),
|
|
2027
|
-
...options.start === void 0 ? {} : { start: options.start }
|
|
2028
|
-
});
|
|
2029
|
-
await savePortState(workspace.root, { ...result.ports });
|
|
2030
|
-
return result;
|
|
2031
|
-
}
|
|
2032
|
-
/**
|
|
2033
|
-
* Where each declared surface and site answers locally.
|
|
2034
|
-
*
|
|
2035
|
-
* The addresses come from the ports the workspace already assigns; which
|
|
2036
|
-
* construct sits at which one comes from the manifest. That split is the point:
|
|
2037
|
-
* this used to answer "who may call whom" by listing every app the workspace
|
|
2038
|
-
* runs, which is a different question that happened to give the same answer in
|
|
2039
|
-
* a single-repo workspace and no answer at all deployed. Now it answers only
|
|
2040
|
-
* "where does this construct answer", and the graph answers the rest.
|
|
2041
|
-
*
|
|
2042
|
-
* A site is matched to its app by `path`, because that is the one thing a site
|
|
2043
|
-
* declaration and a workspace app both name. Surfaces are matched to the
|
|
2044
|
-
* backend app: every `rest-api` in a process answers on that process's port, so
|
|
2045
|
-
* an app serving both its own API and an auth server publishes one address
|
|
2046
|
-
* twice — which is exactly what it does at runtime.
|
|
2047
|
-
*/
|
|
2048
|
-
function surfaceAddresses(workspace, manifest) {
|
|
2049
|
-
const addresses = {};
|
|
2050
|
-
for (const [id, declaration] of Object.entries(manifest)) {
|
|
2051
|
-
if (declaration.kind !== "rest-api" && declaration.kind !== "site") continue;
|
|
2052
|
-
const host = declaration.kind === "site" ? appKey(id) : appKeyOfHost(manifest, id);
|
|
2053
|
-
const app = host ? workspace.apps[host] : void 0;
|
|
2054
|
-
if (app?.port) addresses[id] = localAddress(app.port);
|
|
2055
|
-
}
|
|
2056
|
-
return addresses;
|
|
2057
|
-
}
|
|
2058
|
-
/** The app key serving a surface, following a mount to its host. */
|
|
2059
|
-
function appKeyOfHost(manifest, id) {
|
|
2060
|
-
const host = hostOf(manifest, id);
|
|
2061
|
-
return host ? appKey(host) : void 0;
|
|
2062
|
-
}
|
|
2063
|
-
/** Where a local process answers, given the port the workspace gave it. */
|
|
2064
|
-
function localAddress(port) {
|
|
2065
|
-
return `http://localhost:${port}`;
|
|
2066
|
-
}
|
|
2067
|
-
|
|
2068
|
-
//#endregion
|
|
2069
|
-
//#region src/credentials/index.ts
|
|
2070
|
-
const logger = console;
|
|
2071
|
-
/**
|
|
2072
|
-
* Load environment files
|
|
2073
|
-
* @internal Exported for testing
|
|
2074
|
-
*/
|
|
2075
|
-
function loadEnvFiles(envConfig, cwd = process.cwd()) {
|
|
2076
|
-
const loaded = [];
|
|
2077
|
-
const missing = [];
|
|
2078
|
-
const envFiles = envConfig ? Array.isArray(envConfig) ? envConfig : [envConfig] : [".env"];
|
|
2079
|
-
for (const envFile of envFiles) {
|
|
2080
|
-
const envPath = resolve(cwd, envFile);
|
|
2081
|
-
if (existsSync(envPath)) {
|
|
2082
|
-
config({
|
|
2083
|
-
path: envPath,
|
|
2084
|
-
override: true,
|
|
2085
|
-
quiet: true
|
|
2086
|
-
});
|
|
2087
|
-
loaded.push(envFile);
|
|
2088
|
-
} else if (envConfig) missing.push(envFile);
|
|
2089
|
-
}
|
|
2090
|
-
return {
|
|
2091
|
-
loaded,
|
|
2092
|
-
missing
|
|
2093
|
-
};
|
|
2094
|
-
}
|
|
2095
|
-
/**
|
|
2096
|
-
* Check if a port is available
|
|
2097
|
-
* @internal Exported for testing
|
|
2098
|
-
*/
|
|
2099
|
-
async function isPortAvailable(port) {
|
|
2100
|
-
return new Promise((resolve$1) => {
|
|
2101
|
-
const server = createServer();
|
|
2102
|
-
server.once("error", (err) => {
|
|
2103
|
-
if (err.code === "EADDRINUSE") resolve$1(false);
|
|
2104
|
-
else resolve$1(false);
|
|
2105
|
-
});
|
|
2106
|
-
server.once("listening", () => {
|
|
2107
|
-
server.close();
|
|
2108
|
-
resolve$1(true);
|
|
2109
|
-
});
|
|
2110
|
-
server.listen(port);
|
|
2111
|
-
});
|
|
2112
|
-
}
|
|
2113
|
-
/**
|
|
2114
|
-
* Find an available port starting from the preferred port
|
|
2115
|
-
* @internal Exported for testing
|
|
2116
|
-
*/
|
|
2117
|
-
async function findAvailablePort(preferredPort, maxAttempts = 10) {
|
|
2118
|
-
for (let i = 0; i < maxAttempts; i++) {
|
|
2119
|
-
const port = preferredPort + i;
|
|
2120
|
-
if (await isPortAvailable(port)) return port;
|
|
2121
|
-
logger.log(`⚠️ Port ${port} is in use, trying ${port + 1}...`);
|
|
2122
|
-
}
|
|
2123
|
-
throw new Error(`Could not find an available port after trying ${maxAttempts} ports starting from ${preferredPort}`);
|
|
2124
|
-
}
|
|
2125
|
-
const PORT_STATE_PATH = ".gkm/ports.json";
|
|
2126
|
-
/**
|
|
2127
|
-
* Parse docker-compose.yml and extract all port mappings that use env var interpolation.
|
|
2128
|
-
* Entries like `'${POSTGRES_HOST_PORT:-5432}:5432'` are captured.
|
|
2129
|
-
* Fixed port mappings like `'5050:80'` are skipped.
|
|
2130
|
-
* @internal Exported for testing
|
|
2131
|
-
*/
|
|
2132
|
-
function parseComposePortMappings(composePath) {
|
|
2133
|
-
if (!existsSync(composePath)) return [];
|
|
2134
|
-
const content = readFileSync(composePath, "utf-8");
|
|
2135
|
-
const compose = parse$1(content);
|
|
2136
|
-
if (!compose?.services) return [];
|
|
2137
|
-
const results = [];
|
|
2138
|
-
for (const [serviceName, serviceConfig] of Object.entries(compose.services)) for (const portMapping of serviceConfig?.ports ?? []) {
|
|
2139
|
-
const match = String(portMapping).match(/\$\{(\w+):-(\d+)\}:(\d+)/);
|
|
2140
|
-
if (match?.[1] && match[2] && match[3]) results.push({
|
|
2141
|
-
service: serviceName,
|
|
2142
|
-
envVar: match[1],
|
|
2143
|
-
defaultPort: Number(match[2]),
|
|
2144
|
-
containerPort: Number(match[3])
|
|
2145
|
-
});
|
|
2146
|
-
}
|
|
2147
|
-
return results;
|
|
2148
|
-
}
|
|
2149
|
-
/**
|
|
2150
|
-
* Load saved port state from .gkm/ports.json.
|
|
2151
|
-
* @internal Exported for testing
|
|
2152
|
-
*/
|
|
2153
|
-
async function loadPortState(workspaceRoot) {
|
|
2154
|
-
try {
|
|
2155
|
-
const raw = await readFile(join(workspaceRoot, PORT_STATE_PATH), "utf-8");
|
|
2156
|
-
return JSON.parse(raw);
|
|
2157
|
-
} catch {
|
|
2158
|
-
return {};
|
|
2159
|
-
}
|
|
2160
|
-
}
|
|
2161
|
-
/**
|
|
2162
|
-
* Save port state to .gkm/ports.json.
|
|
2163
|
-
* @internal Exported for testing
|
|
2164
|
-
*/
|
|
2165
|
-
async function savePortState(workspaceRoot, ports) {
|
|
2166
|
-
const dir = join(workspaceRoot, ".gkm");
|
|
2167
|
-
await mkdir(dir, { recursive: true });
|
|
2168
|
-
await writeFile(join(workspaceRoot, PORT_STATE_PATH), `${JSON.stringify(ports, null, 2)}\n`);
|
|
2169
|
-
}
|
|
2170
|
-
/**
|
|
2171
|
-
* Check if a project's own Docker container is running and return its host port.
|
|
2172
|
-
* Uses `docker compose port` scoped to the project's compose file.
|
|
2173
|
-
* @internal Exported for testing
|
|
2174
|
-
*/
|
|
2175
|
-
function getContainerHostPort(workspaceRoot, service, containerPort) {
|
|
2176
|
-
try {
|
|
2177
|
-
const result = execSync(`docker compose port ${service} ${containerPort}`, {
|
|
2178
|
-
cwd: workspaceRoot,
|
|
2179
|
-
stdio: "pipe"
|
|
2180
|
-
}).toString().trim();
|
|
2181
|
-
const match = result.match(/:(\d+)$/);
|
|
2182
|
-
return match ? Number(match[1]) : null;
|
|
2183
|
-
} catch {
|
|
2184
|
-
return null;
|
|
2779
|
+
await write(join(root, caddySitesPath(stage)), caddyfile);
|
|
2780
|
+
await pruneCaddySites(root, [
|
|
2781
|
+
stage,
|
|
2782
|
+
options.localStage,
|
|
2783
|
+
TEST_STAGE
|
|
2784
|
+
]);
|
|
2185
2785
|
}
|
|
2186
|
-
|
|
2187
|
-
|
|
2188
|
-
|
|
2189
|
-
* Priority: running container → saved state → find available port.
|
|
2190
|
-
* Persists resolved ports to .gkm/ports.json.
|
|
2191
|
-
* @internal Exported for testing
|
|
2192
|
-
*/
|
|
2193
|
-
async function resolveServicePorts(workspaceRoot) {
|
|
2194
|
-
const composePath = join(workspaceRoot, "docker-compose.yml");
|
|
2195
|
-
const mappings = parseComposePortMappings(composePath);
|
|
2196
|
-
if (mappings.length === 0) return {
|
|
2197
|
-
dockerEnv: {},
|
|
2198
|
-
ports: {},
|
|
2199
|
-
mappings: []
|
|
2200
|
-
};
|
|
2201
|
-
const savedState = await loadPortState(workspaceRoot);
|
|
2202
|
-
const dockerEnv = {};
|
|
2203
|
-
const ports = {};
|
|
2204
|
-
const assignedPorts = /* @__PURE__ */ new Set();
|
|
2205
|
-
logger.log("\n🔌 Resolving service ports...");
|
|
2206
|
-
for (const mapping of mappings) {
|
|
2207
|
-
const containerPort = getContainerHostPort(workspaceRoot, mapping.service, mapping.containerPort);
|
|
2208
|
-
if (containerPort !== null) {
|
|
2209
|
-
ports[mapping.envVar] = containerPort;
|
|
2210
|
-
dockerEnv[mapping.envVar] = String(containerPort);
|
|
2211
|
-
assignedPorts.add(containerPort);
|
|
2212
|
-
logger.log(` 🔄 ${mapping.service}:${mapping.containerPort}: reusing existing container on port ${containerPort}`);
|
|
2213
|
-
continue;
|
|
2214
|
-
}
|
|
2215
|
-
const savedPort = savedState[mapping.envVar];
|
|
2216
|
-
if (savedPort && !assignedPorts.has(savedPort) && await isPortAvailable(savedPort)) {
|
|
2217
|
-
ports[mapping.envVar] = savedPort;
|
|
2218
|
-
dockerEnv[mapping.envVar] = String(savedPort);
|
|
2219
|
-
assignedPorts.add(savedPort);
|
|
2220
|
-
logger.log(` 💾 ${mapping.service}:${mapping.containerPort}: using saved port ${savedPort}`);
|
|
2221
|
-
continue;
|
|
2222
|
-
}
|
|
2223
|
-
let resolvedPort = await findAvailablePort(mapping.defaultPort);
|
|
2224
|
-
while (assignedPorts.has(resolvedPort)) resolvedPort = await findAvailablePort(resolvedPort + 1);
|
|
2225
|
-
ports[mapping.envVar] = resolvedPort;
|
|
2226
|
-
dockerEnv[mapping.envVar] = String(resolvedPort);
|
|
2227
|
-
assignedPorts.add(resolvedPort);
|
|
2228
|
-
if (resolvedPort !== mapping.defaultPort) logger.log(` ⚡ ${mapping.service}:${mapping.containerPort}: port ${mapping.defaultPort} occupied, using port ${resolvedPort}`);
|
|
2229
|
-
else logger.log(` ✅ ${mapping.service}:${mapping.containerPort}: using default port ${resolvedPort}`);
|
|
2786
|
+
if (start && plan.containers.length > 0) {
|
|
2787
|
+
options.progress?.(`🐳 Starting ${plan.containers.join(", ")} — the first start pulls each image, which can take a few minutes`);
|
|
2788
|
+
await docker.up(composePath, plan.containers, options.progress ? { show: true } : {});
|
|
2230
2789
|
}
|
|
2231
|
-
await
|
|
2790
|
+
if (start && plan.containers.includes("caddy")) await docker.reload(composePath, "caddy").catch(() => {});
|
|
2791
|
+
if (start && plan.containers.includes("caddy")) await docker.copyOut(composePath, "caddy", CADDY_ROOT_IN_CONTAINER, join(root, LOCAL_CA_PATH)).catch(() => {});
|
|
2792
|
+
if (start && provision) options.progress?.("🗄️ Creating what the constructs declare — databases, roles, buckets, topics");
|
|
2793
|
+
const provisioned = start && provision ? await create(plan, ports, sql, buckets, carriers, project) : [];
|
|
2794
|
+
await saveState(root, {
|
|
2795
|
+
hash,
|
|
2796
|
+
stage
|
|
2797
|
+
});
|
|
2232
2798
|
return {
|
|
2233
|
-
|
|
2234
|
-
|
|
2235
|
-
|
|
2799
|
+
...result,
|
|
2800
|
+
provisioned,
|
|
2801
|
+
changed: true
|
|
2236
2802
|
};
|
|
2237
2803
|
}
|
|
2238
2804
|
/**
|
|
2239
|
-
*
|
|
2240
|
-
*
|
|
2241
|
-
*
|
|
2805
|
+
* Create the databases, schemas, buckets, topics and queues the plan names.
|
|
2806
|
+
*
|
|
2807
|
+
* Idempotent throughout, so this runs on every non-converged reconcile rather
|
|
2808
|
+
* than being something to remember.
|
|
2242
2809
|
*/
|
|
2243
|
-
function
|
|
2244
|
-
|
|
2245
|
-
|
|
2246
|
-
|
|
2247
|
-
|
|
2810
|
+
async function create(plan, ports, sql, buckets, emulatorCarriers, project) {
|
|
2811
|
+
const applied = [];
|
|
2812
|
+
const postgresPort = ports[primaryPortKey("postgres")];
|
|
2813
|
+
const statements = postgresStatements(plan, project);
|
|
2814
|
+
if (postgresPort !== void 0 && statements.length > 0) applied.push(...await applyPostgres(sql(postgresPort), statements));
|
|
2815
|
+
const minioPort = ports[primaryPortKey("minio")];
|
|
2816
|
+
const names = bucketNames(plan);
|
|
2817
|
+
const policies = bucketPolicies(plan);
|
|
2818
|
+
if (minioPort !== void 0 && (names.length > 0 || policies.length > 0)) {
|
|
2819
|
+
const client = buckets(minioPort);
|
|
2820
|
+
if (names.length > 0) applied.push(...await applyBuckets(client, names));
|
|
2821
|
+
if (policies.length > 0) applied.push(...await applyPolicies(client, policies));
|
|
2822
|
+
}
|
|
2823
|
+
const emulatorPort = ports[primaryPortKey("localstack")];
|
|
2824
|
+
const carriers = carrierNames(plan);
|
|
2825
|
+
if (emulatorPort !== void 0 && carriers.topics.length + carriers.queues.length > 0) applied.push(...await applyCarriers(emulatorCarriers(emulatorPort), carriers));
|
|
2826
|
+
return applied;
|
|
2248
2827
|
}
|
|
2249
2828
|
/**
|
|
2250
|
-
*
|
|
2251
|
-
*
|
|
2252
|
-
*
|
|
2253
|
-
*
|
|
2829
|
+
* The host ports of containers already running.
|
|
2830
|
+
*
|
|
2831
|
+
* Keyed the same way allocation keys them, so the two merge without either
|
|
2832
|
+
* knowing about the other.
|
|
2254
2833
|
*/
|
|
2255
|
-
function
|
|
2256
|
-
const
|
|
2257
|
-
const
|
|
2258
|
-
|
|
2259
|
-
|
|
2260
|
-
for (const mapping of mappings) {
|
|
2261
|
-
serviceNames.add(mapping.service);
|
|
2262
|
-
const resolved = ports[mapping.envVar];
|
|
2263
|
-
if (resolved !== void 0) portReplacements.push({
|
|
2264
|
-
defaultPort: mapping.defaultPort,
|
|
2265
|
-
resolvedPort: resolved
|
|
2266
|
-
});
|
|
2267
|
-
}
|
|
2268
|
-
for (const [key, value] of Object.entries(result)) {
|
|
2269
|
-
if (!key.endsWith("_HOST")) continue;
|
|
2270
|
-
if (serviceNames.has(value)) result[key] = "localhost";
|
|
2271
|
-
}
|
|
2272
|
-
for (const [key, value] of Object.entries(result)) {
|
|
2273
|
-
if (!key.endsWith("_PORT")) continue;
|
|
2274
|
-
for (const { defaultPort, resolvedPort } of portReplacements) if (value === String(defaultPort)) result[key] = String(resolvedPort);
|
|
2275
|
-
}
|
|
2276
|
-
for (const [key, value] of Object.entries(result)) {
|
|
2277
|
-
if (!key.endsWith("_URL") && !key.endsWith("_ENDPOINT") && !key.endsWith("_CONNECTION_STRING") && key !== "DATABASE_URL") continue;
|
|
2278
|
-
let rewritten = value;
|
|
2279
|
-
for (const name of serviceNames) rewritten = rewritten.replace(new RegExp(`@${name}:`, "g"), "@localhost:");
|
|
2280
|
-
for (const { defaultPort, resolvedPort } of portReplacements) rewritten = replacePortInUrl(rewritten, defaultPort, resolvedPort);
|
|
2281
|
-
result[key] = rewritten;
|
|
2834
|
+
async function observedPorts(docker, composePath, containers, fakes) {
|
|
2835
|
+
const observed = {};
|
|
2836
|
+
for (const container of containers) for (const port of portsOf(container, fakes)) {
|
|
2837
|
+
const running = await docker.publishedPort(composePath, container, port.inside);
|
|
2838
|
+
if (running !== void 0) observed[port.key] = running;
|
|
2282
2839
|
}
|
|
2283
|
-
return
|
|
2840
|
+
return observed;
|
|
2284
2841
|
}
|
|
2842
|
+
/** Write a file only when its content would change, so mtimes stay meaningful. */
|
|
2285
2843
|
/**
|
|
2286
|
-
*
|
|
2287
|
-
*
|
|
2288
|
-
*
|
|
2289
|
-
*
|
|
2844
|
+
* Remove the routes of stages this project no longer reconciles.
|
|
2845
|
+
*
|
|
2846
|
+
* Only the local stage and `test` are ever written, so any other file is left
|
|
2847
|
+
* over from a stage that was renamed — `development` becoming `dev`. Caddy
|
|
2848
|
+
* imports every file, and the leftover declares the same hosts as its
|
|
2849
|
+
* replacement: it refuses the whole config as ambiguous, and every URL behind
|
|
2850
|
+
* the edge goes with it.
|
|
2290
2851
|
*/
|
|
2291
|
-
function
|
|
2292
|
-
|
|
2293
|
-
|
|
2294
|
-
|
|
2295
|
-
...portEnv
|
|
2296
|
-
};
|
|
2852
|
+
async function pruneCaddySites(root, stages) {
|
|
2853
|
+
const dir = dirname(join(root, caddySitesPath(TEST_STAGE)));
|
|
2854
|
+
const keep = new Set(stages.map((stage) => stage && `${stage}.caddy`));
|
|
2855
|
+
for (const file of await readdir(dir)) if (file.endsWith(".caddy") && !keep.has(file)) await rm(join(dir, file));
|
|
2297
2856
|
}
|
|
2298
|
-
|
|
2299
|
-
|
|
2300
|
-
|
|
2301
|
-
|
|
2302
|
-
|
|
2303
|
-
|
|
2304
|
-
const content = readFileSync(composePath, "utf-8");
|
|
2305
|
-
const compose = parse$1(content);
|
|
2306
|
-
return Object.keys(compose?.services ?? {});
|
|
2857
|
+
async function write(path, content) {
|
|
2858
|
+
try {
|
|
2859
|
+
if (await readFile(path, "utf-8") === content) return;
|
|
2860
|
+
} catch {}
|
|
2861
|
+
await mkdir(dirname(path), { recursive: true });
|
|
2862
|
+
await writeFile(path, content);
|
|
2307
2863
|
}
|
|
2864
|
+
//#endregion
|
|
2865
|
+
//#region src/reconcile/workspace.ts
|
|
2308
2866
|
/**
|
|
2309
|
-
*
|
|
2310
|
-
*
|
|
2867
|
+
* Workspace config → reconcile.
|
|
2868
|
+
*
|
|
2869
|
+
* Nothing here is read from a `services:` block — there is none. Which
|
|
2870
|
+
* containers exist comes from the manifest; which backend a cache or a broker
|
|
2871
|
+
* resolves to comes from the deploy target; and an image pin is the project's
|
|
2872
|
+
* own `docker-compose.yml`, merged over the generated file.
|
|
2873
|
+
*
|
|
2874
|
+
* Shared by `gkm setup`, `gkm dev`, and `gkm test` so all three converge on the
|
|
2875
|
+
* same state, differing only in the stage they pass.
|
|
2311
2876
|
*/
|
|
2312
|
-
|
|
2313
|
-
|
|
2314
|
-
|
|
2315
|
-
|
|
2316
|
-
|
|
2317
|
-
|
|
2318
|
-
|
|
2319
|
-
|
|
2320
|
-
|
|
2321
|
-
|
|
2322
|
-
env: buildDockerComposeEnv(secretsEnv, portEnv)
|
|
2323
|
-
});
|
|
2324
|
-
logger.log("✅ Services started");
|
|
2325
|
-
} catch (error) {
|
|
2326
|
-
logger.error("❌ Failed to start services:", error.message);
|
|
2327
|
-
throw error;
|
|
2328
|
-
}
|
|
2329
|
-
}
|
|
2877
|
+
var workspace_exports = /* @__PURE__ */ __exportAll({
|
|
2878
|
+
FAKE_ENV: () => FAKE_ENV,
|
|
2879
|
+
backendsOf: () => backendsOf,
|
|
2880
|
+
constructGlobs: () => constructGlobs,
|
|
2881
|
+
derivedContainers: () => derivedContainers,
|
|
2882
|
+
fakesApply: () => fakesApply,
|
|
2883
|
+
metroPorts: () => metroPorts,
|
|
2884
|
+
reconcileWorkspace: () => reconcileWorkspace,
|
|
2885
|
+
surfaceAddresses: () => surfaceAddresses
|
|
2886
|
+
});
|
|
2330
2887
|
/**
|
|
2331
|
-
*
|
|
2332
|
-
*
|
|
2333
|
-
*
|
|
2334
|
-
*
|
|
2888
|
+
* Every constructs glob in the workspace, resolved against its app.
|
|
2889
|
+
*
|
|
2890
|
+
* Absolute because discovery globs from a single cwd while apps live in
|
|
2891
|
+
* different directories.
|
|
2335
2892
|
*/
|
|
2336
|
-
|
|
2337
|
-
|
|
2338
|
-
|
|
2339
|
-
|
|
2340
|
-
|
|
2341
|
-
const
|
|
2342
|
-
|
|
2343
|
-
|
|
2344
|
-
|
|
2345
|
-
|
|
2346
|
-
cwd: workspace.root,
|
|
2347
|
-
stdio: "inherit",
|
|
2348
|
-
env: buildDockerComposeEnv(secretsEnv, portEnv)
|
|
2349
|
-
});
|
|
2350
|
-
logger.log("✅ Services started");
|
|
2351
|
-
} catch (error) {
|
|
2352
|
-
logger.error("❌ Failed to start services:", error.message);
|
|
2353
|
-
throw error;
|
|
2354
|
-
}
|
|
2893
|
+
function constructGlobs(workspace) {
|
|
2894
|
+
return allConstructGlobs(workspace);
|
|
2895
|
+
}
|
|
2896
|
+
/** The backends a workspace's target implies, as the plan takes them. */
|
|
2897
|
+
function backendsOf(workspace) {
|
|
2898
|
+
const on = providerOf(workspace);
|
|
2899
|
+
return {
|
|
2900
|
+
events: eventsBackendFor(on),
|
|
2901
|
+
cache: cacheBackendFor(on)
|
|
2902
|
+
};
|
|
2355
2903
|
}
|
|
2356
2904
|
/**
|
|
2357
|
-
*
|
|
2358
|
-
*
|
|
2359
|
-
*
|
|
2905
|
+
* The containers a workspace's declarations imply, without reconciling.
|
|
2906
|
+
*
|
|
2907
|
+
* The same derivation `reconcileWorkspace` makes, available to callers that
|
|
2908
|
+
* need to know *what would run* without starting anything or writing ports —
|
|
2909
|
+
* `gkm setup` deciding which credentials to generate, `gkm docker` writing a
|
|
2910
|
+
* compose file. Those used to each read a boolean per service out of config,
|
|
2911
|
+
* which is how a container could exist because config asked for one rather than
|
|
2912
|
+
* because something declared it.
|
|
2360
2913
|
*/
|
|
2361
|
-
async function
|
|
2362
|
-
|
|
2363
|
-
|
|
2364
|
-
|
|
2365
|
-
|
|
2366
|
-
|
|
2367
|
-
|
|
2368
|
-
|
|
2369
|
-
|
|
2370
|
-
}
|
|
2371
|
-
if (Object.keys(secrets).length === 0) return {};
|
|
2372
|
-
if (!appName) return secrets;
|
|
2373
|
-
const prefix = appName.toUpperCase();
|
|
2374
|
-
const mapped = { ...secrets };
|
|
2375
|
-
const appDbUrl = secrets[`${prefix}_DATABASE_URL`];
|
|
2376
|
-
if (appDbUrl) mapped.DATABASE_URL = appDbUrl;
|
|
2377
|
-
return mapped;
|
|
2914
|
+
async function derivedContainers(workspace, stage, manifest) {
|
|
2915
|
+
const found = manifest ?? await discover({
|
|
2916
|
+
patterns: constructGlobs(workspace),
|
|
2917
|
+
cwd: workspace.root
|
|
2918
|
+
});
|
|
2919
|
+
return planFor(found, stage, provisionOrder(found), {
|
|
2920
|
+
localStage: workspace.stages.local,
|
|
2921
|
+
...backendsOf(workspace)
|
|
2922
|
+
}).containers;
|
|
2378
2923
|
}
|
|
2379
2924
|
/**
|
|
2380
|
-
*
|
|
2381
|
-
*
|
|
2925
|
+
* Set by `gkm dev --fake` for the apps it starts. Each app's own `gkm dev`
|
|
2926
|
+
* reconciles too, and must answer the way the workspace did.
|
|
2382
2927
|
*/
|
|
2383
|
-
|
|
2384
|
-
let dir = startDir;
|
|
2385
|
-
while (dir !== "/") {
|
|
2386
|
-
if (existsSync(join(dir, ".gkm", "secrets"))) return dir;
|
|
2387
|
-
const parent = dirname(dir);
|
|
2388
|
-
if (parent === dir) break;
|
|
2389
|
-
dir = parent;
|
|
2390
|
-
}
|
|
2391
|
-
return startDir;
|
|
2392
|
-
}
|
|
2928
|
+
const FAKE_ENV = "GKM_FAKE";
|
|
2393
2929
|
/**
|
|
2394
|
-
*
|
|
2395
|
-
*
|
|
2396
|
-
*
|
|
2930
|
+
* Whether external APIs are faked: always for `gkm test`, and for `gkm dev`
|
|
2931
|
+
* only when asked — by `--fake`, or by the workspace that started this app
|
|
2932
|
+
* with it. Never for anything acting on a deployed stage.
|
|
2397
2933
|
*/
|
|
2398
|
-
function
|
|
2399
|
-
return
|
|
2400
|
-
|
|
2401
|
-
// Inject dev secrets via globalThis and process.env
|
|
2402
|
-
// Using globalThis.__gkm_credentials__ avoids CJS/ESM interop issues where
|
|
2403
|
-
// Object.assign on the Credentials export only mutates one module copy.
|
|
2404
|
-
const secretsPath = '${secretsJsonPath}';
|
|
2405
|
-
if (existsSync(secretsPath)) {
|
|
2406
|
-
const secrets = JSON.parse(readFileSync(secretsPath, 'utf-8'));
|
|
2407
|
-
globalThis.__gkm_credentials__ = secrets;
|
|
2408
|
-
Object.assign(process.env, secrets);
|
|
2409
|
-
}
|
|
2410
|
-
`;
|
|
2934
|
+
function fakesApply(stage, fake, env = process.env) {
|
|
2935
|
+
return fake === true || stage === "test" || env["GKM_FAKE"] === "1";
|
|
2411
2936
|
}
|
|
2412
2937
|
/**
|
|
2413
|
-
*
|
|
2414
|
-
*
|
|
2415
|
-
*
|
|
2938
|
+
* Reconcile a workspace for one stage.
|
|
2939
|
+
*
|
|
2940
|
+
* Ports are loaded and saved around the call rather than inside it, so the loop
|
|
2941
|
+
* stays a pure-ish function of its inputs and the store keeps its single owner.
|
|
2416
2942
|
*/
|
|
2417
|
-
async function
|
|
2418
|
-
const
|
|
2419
|
-
|
|
2420
|
-
|
|
2421
|
-
|
|
2422
|
-
|
|
2423
|
-
|
|
2943
|
+
async function reconcileWorkspace(workspace, options) {
|
|
2944
|
+
const runnables = {};
|
|
2945
|
+
const discovered = await discover({
|
|
2946
|
+
patterns: constructGlobs(workspace),
|
|
2947
|
+
cwd: workspace.root,
|
|
2948
|
+
runnables
|
|
2949
|
+
});
|
|
2950
|
+
const manifest = options.manifest ?? discovered;
|
|
2951
|
+
const fakes = fakesApply(options.stage, options.fake) ? await readFakes(workspace.root, manifest) : {};
|
|
2952
|
+
const result = await reconcile({
|
|
2953
|
+
root: workspace.root,
|
|
2954
|
+
project: workspace.name,
|
|
2955
|
+
manifest,
|
|
2956
|
+
stage: options.stage,
|
|
2957
|
+
localStage: workspace.stages.local,
|
|
2958
|
+
...backendsOf(workspace),
|
|
2959
|
+
saved: await loadPortState(workspace.root),
|
|
2960
|
+
addresses: surfaceAddresses(workspace, manifest),
|
|
2961
|
+
metroPorts: metroPorts(workspace, manifest),
|
|
2962
|
+
fakes,
|
|
2963
|
+
apps: (containers) => appServices(workspace, manifest, containers, runnables, fakes),
|
|
2964
|
+
...options.start === void 0 ? {} : { start: options.start },
|
|
2965
|
+
...options.progress ? { progress: options.progress } : {}
|
|
2966
|
+
});
|
|
2967
|
+
await savePortState(workspace.root, { ...result.ports });
|
|
2968
|
+
return result;
|
|
2424
2969
|
}
|
|
2425
2970
|
/**
|
|
2426
|
-
*
|
|
2427
|
-
*
|
|
2971
|
+
* Where each mobile app's Metro runs: its app's port, which `gkm exec` hands
|
|
2972
|
+
* Expo as `RCT_METRO_PORT`. Kept apart from the surfaces' addresses — a phone
|
|
2973
|
+
* reaches Metro by `exp://`, not through the edge.
|
|
2428
2974
|
*/
|
|
2429
|
-
|
|
2430
|
-
const
|
|
2431
|
-
|
|
2432
|
-
|
|
2433
|
-
|
|
2434
|
-
|
|
2435
|
-
|
|
2436
|
-
|
|
2437
|
-
await import('${entryPath}');
|
|
2438
|
-
`;
|
|
2439
|
-
await writeFile(wrapperPath, content);
|
|
2975
|
+
function metroPorts(workspace, manifest) {
|
|
2976
|
+
const ports = {};
|
|
2977
|
+
for (const [id, declaration] of Object.entries(manifest)) {
|
|
2978
|
+
if (declaration.kind !== "mobile-app") continue;
|
|
2979
|
+
const port = workspace.apps[appKey(id)]?.port;
|
|
2980
|
+
if (port) ports[id] = port;
|
|
2981
|
+
}
|
|
2982
|
+
return ports;
|
|
2440
2983
|
}
|
|
2441
2984
|
/**
|
|
2442
|
-
*
|
|
2443
|
-
* Loads workspace config, secrets, resolves Docker ports, rewrites URLs,
|
|
2444
|
-
* injects PORT, dependency URLs, and writes credentials JSON.
|
|
2985
|
+
* Where each declared surface and site answers locally.
|
|
2445
2986
|
*
|
|
2446
|
-
*
|
|
2447
|
-
*
|
|
2448
|
-
*
|
|
2449
|
-
*
|
|
2450
|
-
*
|
|
2451
|
-
*
|
|
2452
|
-
*
|
|
2987
|
+
* The addresses come from the ports the workspace already assigns; which
|
|
2988
|
+
* construct sits at which one comes from the manifest. That split is the point:
|
|
2989
|
+
* this used to answer "who may call whom" by listing every app the workspace
|
|
2990
|
+
* runs, which is a different question that happened to give the same answer in
|
|
2991
|
+
* a single-repo workspace and no answer at all deployed. Now it answers only
|
|
2992
|
+
* "where does this construct answer", and the graph answers the rest.
|
|
2993
|
+
*
|
|
2994
|
+
* A site is matched to its app by `path`, because that is the one thing a site
|
|
2995
|
+
* declaration and a workspace app both name. Surfaces are matched to the
|
|
2996
|
+
* backend app: every `rest-api` in a process answers on that process's port, so
|
|
2997
|
+
* an app serving both its own API and an auth server publishes one address
|
|
2998
|
+
* twice — which is exactly what it does at runtime.
|
|
2453
2999
|
*/
|
|
2454
|
-
|
|
2455
|
-
const
|
|
2456
|
-
const
|
|
2457
|
-
|
|
2458
|
-
|
|
2459
|
-
|
|
2460
|
-
|
|
2461
|
-
try {
|
|
2462
|
-
appInfo = await loadWorkspaceAppInfo(cwd);
|
|
2463
|
-
workspaceAppPort = appInfo.app.port;
|
|
2464
|
-
secretsRoot = appInfo.workspaceRoot;
|
|
2465
|
-
appName = appInfo.appName;
|
|
2466
|
-
} catch {
|
|
2467
|
-
secretsRoot = findSecretsRoot(cwd);
|
|
2468
|
-
appName = getAppNameFromCwd(cwd) ?? void 0;
|
|
2469
|
-
}
|
|
2470
|
-
const resolvedPort = options.explicitPort ?? workspaceAppPort ?? 3e3;
|
|
2471
|
-
const credentials = await loadSecretsForApp(secretsRoot, appName, options.stages);
|
|
2472
|
-
credentials.PORT = String(resolvedPort);
|
|
2473
|
-
const declaredKeys = [];
|
|
2474
|
-
if (appInfo && usesConstructs(appInfo.workspace)) {
|
|
2475
|
-
const { reconcileWorkspace: reconcileWorkspace$1 } = await import("./workspace-k4l3rsFh.mjs");
|
|
2476
|
-
const reconciled = await reconcileWorkspace$1(appInfo.workspace, {
|
|
2477
|
-
stage: options.reconcileStage ?? "development",
|
|
2478
|
-
start: options.startDocker ?? false
|
|
2479
|
-
});
|
|
2480
|
-
Object.assign(credentials, reconciled.env);
|
|
2481
|
-
declaredKeys.push(...Object.keys(reconciled.env));
|
|
2482
|
-
if (Object.keys(reconciled.env).length > 0) logger.log(`🔌 Resolved ${Object.keys(reconciled.env).length} declared URL(s)`);
|
|
2483
|
-
} else {
|
|
2484
|
-
const composePath = join(secretsRoot, "docker-compose.yml");
|
|
2485
|
-
const mappings = parseComposePortMappings(composePath);
|
|
2486
|
-
if (mappings.length > 0) {
|
|
2487
|
-
let resolvedPorts;
|
|
2488
|
-
if (portMode === "full") resolvedPorts = await resolveServicePorts(secretsRoot);
|
|
2489
|
-
else {
|
|
2490
|
-
const savedPorts = await loadPortState(secretsRoot);
|
|
2491
|
-
const ports = {};
|
|
2492
|
-
for (const mapping of mappings) {
|
|
2493
|
-
const containerPort = getContainerHostPort(secretsRoot, mapping.service, mapping.containerPort);
|
|
2494
|
-
if (containerPort !== null) ports[mapping.envVar] = containerPort;
|
|
2495
|
-
else {
|
|
2496
|
-
const saved = savedPorts[mapping.envVar];
|
|
2497
|
-
if (saved !== void 0) ports[mapping.envVar] = saved;
|
|
2498
|
-
}
|
|
2499
|
-
}
|
|
2500
|
-
resolvedPorts = {
|
|
2501
|
-
dockerEnv: {},
|
|
2502
|
-
ports,
|
|
2503
|
-
mappings
|
|
2504
|
-
};
|
|
2505
|
-
}
|
|
2506
|
-
if (options.startDocker) if (appInfo) await startWorkspaceServices(appInfo.workspace, resolvedPorts.dockerEnv, credentials);
|
|
2507
|
-
else await startComposeServices(secretsRoot, resolvedPorts.dockerEnv, credentials);
|
|
2508
|
-
if (Object.keys(resolvedPorts.ports).length > 0) {
|
|
2509
|
-
const rewritten = rewriteUrlsWithPorts(credentials, resolvedPorts);
|
|
2510
|
-
Object.assign(credentials, rewritten);
|
|
2511
|
-
logger.log(`🔌 Applied ${Object.keys(resolvedPorts.ports).length} port mapping(s)`);
|
|
2512
|
-
}
|
|
2513
|
-
}
|
|
2514
|
-
}
|
|
2515
|
-
if (appInfo?.appName) {
|
|
2516
|
-
const depEnv = getDependencyEnvVars(appInfo.workspace, appInfo.appName);
|
|
2517
|
-
Object.assign(credentials, depEnv);
|
|
2518
|
-
}
|
|
2519
|
-
if (!credentials.EVENT_PUBLISHER_CONNECTION_STRING && !credentials.EVENT_SUBSCRIBER_CONNECTION_STRING && credentials.PGBOSS_DB_USER) {
|
|
2520
|
-
const pgbossUrl = generatePgBossUrl({
|
|
2521
|
-
username: credentials.PGBOSS_DB_USER,
|
|
2522
|
-
password: credentials.PGBOSS_DB_PASSWORD ?? "",
|
|
2523
|
-
host: credentials.POSTGRES_HOST ?? "localhost",
|
|
2524
|
-
port: Number(credentials.POSTGRES_PORT ?? "5432"),
|
|
2525
|
-
database: credentials.POSTGRES_DB ?? "app"
|
|
2526
|
-
});
|
|
2527
|
-
credentials.EVENT_PUBLISHER_CONNECTION_STRING = pgbossUrl;
|
|
2528
|
-
credentials.EVENT_SUBSCRIBER_CONNECTION_STRING = pgbossUrl;
|
|
3000
|
+
function surfaceAddresses(workspace, manifest, at = (_app, port) => localAddress(port)) {
|
|
3001
|
+
const addresses = {};
|
|
3002
|
+
for (const [id, declaration] of Object.entries(manifest)) {
|
|
3003
|
+
if (declaration.kind !== "rest-api" && declaration.kind !== "site") continue;
|
|
3004
|
+
const host = appKey(id);
|
|
3005
|
+
const port = workspace.apps[host]?.port;
|
|
3006
|
+
if (port) addresses[id] = at(host, port);
|
|
2529
3007
|
}
|
|
2530
|
-
|
|
2531
|
-
|
|
2532
|
-
|
|
2533
|
-
|
|
2534
|
-
|
|
2535
|
-
return {
|
|
2536
|
-
credentials,
|
|
2537
|
-
resolvedPort,
|
|
2538
|
-
secretsJsonPath,
|
|
2539
|
-
appName,
|
|
2540
|
-
secretsRoot,
|
|
2541
|
-
appInfo,
|
|
2542
|
-
declaredKeys
|
|
2543
|
-
};
|
|
3008
|
+
return addresses;
|
|
3009
|
+
}
|
|
3010
|
+
/** Where a local process answers, given the port the workspace gave it. */
|
|
3011
|
+
function localAddress(port) {
|
|
3012
|
+
return `http://localhost:${port}`;
|
|
2544
3013
|
}
|
|
2545
|
-
|
|
2546
3014
|
//#endregion
|
|
2547
|
-
export {
|
|
2548
|
-
|
|
3015
|
+
export { loadEnvFiles as C, isPortAvailable as S, prepareEntryCredentials as T, fakeKey as _, reconcileWorkspace as a, createEntryWrapper as b, LOCAL_CA_PATH as c, postgresDatabaseNames as d, quoteIdentifier as f, describeServices as g, rootSite as h, derivedContainers as i, forgetState as l, resourceName as m, backendsOf as n, workspace_exports as o, pgClient as p, constructGlobs as r, COMPOSE_PATH as s, FAKE_ENV as t, applyPostgres as u, primaryPortKey as v, loadSecretsForApp as w, findAvailablePort as x, createCredentialsPreload as y };
|
|
3016
|
+
|
|
3017
|
+
//# sourceMappingURL=workspace-BsbxbqV-.mjs.map
|