@geekmidas/cli 10.0.0-alpha.15 → 10.0.0-alpha.151

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.
Files changed (438) hide show
  1. package/README.md +313 -380
  2. package/bin/adjacent-tsconfig.mjs +46 -8
  3. package/bin/gkm.mjs +11 -3
  4. package/bin/owning-tsconfig-jsx-register.mjs +18 -0
  5. package/bin/owning-tsconfig-jsx.mjs +108 -0
  6. package/dist/EndpointGenerator-CusLH-9S.mjs +1037 -0
  7. package/dist/EndpointGenerator-CusLH-9S.mjs.map +1 -0
  8. package/dist/EndpointGenerator-DyLMYClE.cjs +1096 -0
  9. package/dist/EndpointGenerator-DyLMYClE.cjs.map +1 -0
  10. package/dist/GoDaddyProvider-BZYq0-nV.cjs +412 -0
  11. package/dist/GoDaddyProvider-BZYq0-nV.cjs.map +1 -0
  12. package/dist/GoDaddyProvider-D-8GW8cP.mjs +412 -0
  13. package/dist/GoDaddyProvider-D-8GW8cP.mjs.map +1 -0
  14. package/dist/{HostingerProvider-BZFGYYVA.cjs → HostingerProvider-BxRR2ibx.cjs} +23 -9
  15. package/dist/HostingerProvider-BxRR2ibx.cjs.map +1 -0
  16. package/dist/{HostingerProvider-BY26bWwB.mjs → HostingerProvider-C-C4aiKF.mjs} +23 -9
  17. package/dist/HostingerProvider-C-C4aiKF.mjs.map +1 -0
  18. package/dist/LegacyStateStore-BFkNiBxC.mjs +124 -0
  19. package/dist/LegacyStateStore-BFkNiBxC.mjs.map +1 -0
  20. package/dist/LegacyStateStore-DYaamZqR.cjs +124 -0
  21. package/dist/LegacyStateStore-DYaamZqR.cjs.map +1 -0
  22. package/dist/LocalStateStore-7pP4naiB.mjs +167 -0
  23. package/dist/LocalStateStore-7pP4naiB.mjs.map +1 -0
  24. package/dist/LocalStateStore-C0JSjyCg.cjs +178 -0
  25. package/dist/LocalStateStore-C0JSjyCg.cjs.map +1 -0
  26. package/dist/OpenApiTsGenerator-BTMuu1WS.mjs +534 -0
  27. package/dist/OpenApiTsGenerator-BTMuu1WS.mjs.map +1 -0
  28. package/dist/OpenApiTsGenerator-CI-tceXV.cjs +539 -0
  29. package/dist/OpenApiTsGenerator-CI-tceXV.cjs.map +1 -0
  30. package/dist/{Route53Provider-Cvj40A8I.mjs → Route53Provider-Di_SD9CF.mjs} +3 -3
  31. package/dist/Route53Provider-Di_SD9CF.mjs.map +1 -0
  32. package/dist/{Route53Provider-DVILBsvV.cjs → Route53Provider-emLuQHVw.cjs} +3 -3
  33. package/dist/Route53Provider-emLuQHVw.cjs.map +1 -0
  34. package/dist/S3StateStore-BnmbFco-.cjs +222 -0
  35. package/dist/S3StateStore-BnmbFco-.cjs.map +1 -0
  36. package/dist/S3StateStore-Dzyuiy3V.mjs +223 -0
  37. package/dist/S3StateStore-Dzyuiy3V.mjs.map +1 -0
  38. package/dist/SSMStateStore-BLbjoEFc.cjs +204 -0
  39. package/dist/SSMStateStore-BLbjoEFc.cjs.map +1 -0
  40. package/dist/SSMStateStore-BcNH7efc.mjs +205 -0
  41. package/dist/SSMStateStore-BcNH7efc.mjs.map +1 -0
  42. package/dist/SubscriberGenerator-BNJyPmXJ.cjs +848 -0
  43. package/dist/SubscriberGenerator-BNJyPmXJ.cjs.map +1 -0
  44. package/dist/SubscriberGenerator-CzPrixYu.mjs +783 -0
  45. package/dist/SubscriberGenerator-CzPrixYu.mjs.map +1 -0
  46. package/dist/add-SHsas9Yu.mjs +711 -0
  47. package/dist/add-SHsas9Yu.mjs.map +1 -0
  48. package/dist/add-moAE6nV4.cjs +713 -0
  49. package/dist/add-moAE6nV4.cjs.map +1 -0
  50. package/dist/appPorts-BoXMwolc.mjs +249 -0
  51. package/dist/appPorts-BoXMwolc.mjs.map +1 -0
  52. package/dist/appPorts-IW-OZmRi.cjs +296 -0
  53. package/dist/appPorts-IW-OZmRi.cjs.map +1 -0
  54. package/dist/apps-BOgA6AcI.mjs +5534 -0
  55. package/dist/apps-BOgA6AcI.mjs.map +1 -0
  56. package/dist/apps-DkxHjd59.cjs +6148 -0
  57. package/dist/apps-DkxHjd59.cjs.map +1 -0
  58. package/dist/aws-8QQ8n-t6.mjs +89 -0
  59. package/dist/aws-8QQ8n-t6.mjs.map +1 -0
  60. package/dist/aws-DY9NqQIT.cjs +100 -0
  61. package/dist/aws-DY9NqQIT.cjs.map +1 -0
  62. package/dist/awsStore-Bx5nCFnS.mjs +121 -0
  63. package/dist/awsStore-Bx5nCFnS.mjs.map +1 -0
  64. package/dist/awsStore-u3ehhMoY.cjs +138 -0
  65. package/dist/awsStore-u3ehhMoY.cjs.map +1 -0
  66. package/dist/{backup-provisioner-DIVCbfee.mjs → backup-provisioner-C8DnCmxK.mjs} +2 -2
  67. package/dist/backup-provisioner-C8DnCmxK.mjs.map +1 -0
  68. package/dist/{backup-provisioner-BmhIO_Yr.cjs → backup-provisioner-oDEsPj02.cjs} +2 -2
  69. package/dist/backup-provisioner-oDEsPj02.cjs.map +1 -0
  70. package/dist/backups/runner.cjs +519 -0
  71. package/dist/backups/runner.cjs.map +1 -0
  72. package/dist/backups/runner.d.cts +102 -0
  73. package/dist/backups/runner.d.cts.map +1 -0
  74. package/dist/backups/runner.d.mts +102 -0
  75. package/dist/backups/runner.d.mts.map +1 -0
  76. package/dist/backups/runner.mjs +506 -0
  77. package/dist/backups/runner.mjs.map +1 -0
  78. package/dist/{bundler-BeNDZdHQ.cjs → bundler-D0spRLb-.cjs} +59 -14
  79. package/dist/bundler-D0spRLb-.cjs.map +1 -0
  80. package/dist/{bundler-BodTL4kf.mjs → bundler-Dy-Dfchf.mjs} +59 -14
  81. package/dist/bundler-Dy-Dfchf.mjs.map +1 -0
  82. package/dist/cli-BmCyUTZf.cjs +71 -0
  83. package/dist/cli-BmCyUTZf.cjs.map +1 -0
  84. package/dist/cli-BpT_gg4n.mjs +71 -0
  85. package/dist/cli-BpT_gg4n.mjs.map +1 -0
  86. package/dist/compose-0HkjQywT.cjs +3046 -0
  87. package/dist/compose-0HkjQywT.cjs.map +1 -0
  88. package/dist/compose-B3OOowfE.mjs +1560 -0
  89. package/dist/compose-B3OOowfE.mjs.map +1 -0
  90. package/dist/compose-BIXEPrGA.mjs +2957 -0
  91. package/dist/compose-BIXEPrGA.mjs.map +1 -0
  92. package/dist/compose-BP8OeV7P.cjs +1919 -0
  93. package/dist/compose-BP8OeV7P.cjs.map +1 -0
  94. package/dist/config-BLrAd7Of.mjs +659 -0
  95. package/dist/config-BLrAd7Of.mjs.map +1 -0
  96. package/dist/config-CRk2vdSp.d.mts +268 -0
  97. package/dist/config-CRk2vdSp.d.mts.map +1 -0
  98. package/dist/config-juUyBlb1.cjs +772 -0
  99. package/dist/config-juUyBlb1.cjs.map +1 -0
  100. package/dist/config-oBgsbLip.d.cts +268 -0
  101. package/dist/config-oBgsbLip.d.cts.map +1 -0
  102. package/dist/config.cjs +11 -3
  103. package/dist/config.d.cts +4 -117
  104. package/dist/config.d.mts +4 -117
  105. package/dist/config.mjs +3 -3
  106. package/dist/credentialSchemas-BAklWDna.cjs +271 -0
  107. package/dist/credentialSchemas-BAklWDna.cjs.map +1 -0
  108. package/dist/credentialSchemas-mz9QZTYh.mjs +236 -0
  109. package/dist/credentialSchemas-mz9QZTYh.mjs.map +1 -0
  110. package/dist/credentials-B6ZwGsy-.cjs +2272 -0
  111. package/dist/credentials-B6ZwGsy-.cjs.map +1 -0
  112. package/dist/credentials-DRfP9aG7.mjs +1817 -0
  113. package/dist/credentials-DRfP9aG7.mjs.map +1 -0
  114. package/dist/databases-DawSdqBI.cjs +703 -0
  115. package/dist/databases-DawSdqBI.cjs.map +1 -0
  116. package/dist/databases-mzSUFBTw.mjs +588 -0
  117. package/dist/databases-mzSUFBTw.mjs.map +1 -0
  118. package/dist/define-6poNqm5O.mjs +106 -0
  119. package/dist/define-6poNqm5O.mjs.map +1 -0
  120. package/dist/define-crmMcXFS.cjs +153 -0
  121. package/dist/define-crmMcXFS.cjs.map +1 -0
  122. package/dist/deploy/public.cjs +211 -0
  123. package/dist/deploy/public.cjs.map +1 -0
  124. package/dist/deploy/public.d.cts +647 -0
  125. package/dist/deploy/public.d.cts.map +1 -0
  126. package/dist/deploy/public.d.mts +647 -0
  127. package/dist/deploy/public.d.mts.map +1 -0
  128. package/dist/deploy/public.mjs +144 -0
  129. package/dist/deploy/public.mjs.map +1 -0
  130. package/dist/deploy/sniffer-envparser-worker.cjs +54 -0
  131. package/dist/deploy/sniffer-envparser-worker.cjs.map +1 -0
  132. package/dist/deploy/sniffer-envparser-worker.d.cts +1 -0
  133. package/dist/deploy/sniffer-envparser-worker.d.mts +1 -0
  134. package/dist/deploy/sniffer-envparser-worker.mjs +55 -0
  135. package/dist/deploy/sniffer-envparser-worker.mjs.map +1 -0
  136. package/dist/deploy/sniffer-hooks.cjs +1 -1
  137. package/dist/deploy/sniffer-hooks.mjs +1 -1
  138. package/dist/deploy/sniffer-loader.cjs +2 -2
  139. package/dist/deploy/sniffer-loader.mjs +1 -1
  140. package/dist/deploy/sniffer-routes-worker.cjs +2 -2
  141. package/dist/deploy/sniffer-routes-worker.mjs +1 -1
  142. package/dist/deploy-BgOujGGL.mjs +3836 -0
  143. package/dist/deploy-BgOujGGL.mjs.map +1 -0
  144. package/dist/deploy-ciuNiVqo.cjs +3892 -0
  145. package/dist/deploy-ciuNiVqo.cjs.map +1 -0
  146. package/dist/discover-CRywE7yc.cjs +1085 -0
  147. package/dist/discover-CRywE7yc.cjs.map +1 -0
  148. package/dist/discover-DbDrCQ8h.mjs +940 -0
  149. package/dist/discover-DbDrCQ8h.mjs.map +1 -0
  150. package/dist/docker-74kvq2jQ.cjs +1514 -0
  151. package/dist/docker-74kvq2jQ.cjs.map +1 -0
  152. package/dist/docker-CXYz3W2U.mjs +1407 -0
  153. package/dist/docker-CXYz3W2U.mjs.map +1 -0
  154. package/dist/dokploy-CrxDMut1.cjs +38 -0
  155. package/dist/dokploy-CrxDMut1.cjs.map +1 -0
  156. package/dist/dokploy-tNy6ZU64.mjs +38 -0
  157. package/dist/dokploy-tNy6ZU64.mjs.map +1 -0
  158. package/dist/drivers-aM87NQJi.cjs +224 -0
  159. package/dist/drivers-aM87NQJi.cjs.map +1 -0
  160. package/dist/drivers-uxLBq3e3.mjs +195 -0
  161. package/dist/drivers-uxLBq3e3.mjs.map +1 -0
  162. package/dist/eas-B3yv_XVz.mjs +334 -0
  163. package/dist/eas-B3yv_XVz.mjs.map +1 -0
  164. package/dist/eas-CiORdzk0.cjs +337 -0
  165. package/dist/eas-CiORdzk0.cjs.map +1 -0
  166. package/dist/emit-Bj6XSivk.mjs +136 -0
  167. package/dist/emit-Bj6XSivk.mjs.map +1 -0
  168. package/dist/emit-CDC__ubE.cjs +147 -0
  169. package/dist/emit-CDC__ubE.cjs.map +1 -0
  170. package/dist/encryption-Birkm2_E.mjs +89 -0
  171. package/dist/encryption-Birkm2_E.mjs.map +1 -0
  172. package/dist/encryption-drgYmkGP.cjs +90 -0
  173. package/dist/encryption-drgYmkGP.cjs.map +1 -0
  174. package/dist/engine-CW78Wt8x.mjs +3217 -0
  175. package/dist/engine-CW78Wt8x.mjs.map +1 -0
  176. package/dist/engine-CfLaQ0bJ.cjs +3330 -0
  177. package/dist/engine-CfLaQ0bJ.cjs.map +1 -0
  178. package/dist/errors-D8Gwp5kU.mjs +23 -0
  179. package/dist/errors-D8Gwp5kU.mjs.map +1 -0
  180. package/dist/errors-DMC4Umsb.cjs +34 -0
  181. package/dist/errors-DMC4Umsb.cjs.map +1 -0
  182. package/dist/github-BfLlBWB5.cjs +901 -0
  183. package/dist/github-BfLlBWB5.cjs.map +1 -0
  184. package/dist/github-DphkHybE.mjs +900 -0
  185. package/dist/github-DphkHybE.mjs.map +1 -0
  186. package/dist/googlePlay-BWDhYvH_.cjs +931 -0
  187. package/dist/googlePlay-BWDhYvH_.cjs.map +1 -0
  188. package/dist/googlePlay-CjzqTF8t.mjs +931 -0
  189. package/dist/googlePlay-CjzqTF8t.mjs.map +1 -0
  190. package/dist/{index-D-iAVHx-.d.cts → index-B35vTdAW.d.cts} +39 -15
  191. package/dist/index-B35vTdAW.d.cts.map +1 -0
  192. package/dist/{index-Cd-qeOam.d.mts → index-Dv1Zj86i.d.mts} +39 -15
  193. package/dist/index-Dv1Zj86i.d.mts.map +1 -0
  194. package/dist/index.cjs +13594 -12347
  195. package/dist/index.cjs.map +1 -1
  196. package/dist/index.mjs +13541 -12292
  197. package/dist/index.mjs.map +1 -1
  198. package/dist/local-Bt9jPziD.cjs +241 -0
  199. package/dist/local-Bt9jPziD.cjs.map +1 -0
  200. package/dist/local-DBtepRK5.mjs +224 -0
  201. package/dist/local-DBtepRK5.mjs.map +1 -0
  202. package/dist/migrate-2YESt4v9.mjs +267 -0
  203. package/dist/migrate-2YESt4v9.mjs.map +1 -0
  204. package/dist/migrate-B-_cASJt.cjs +272 -0
  205. package/dist/migrate-B-_cASJt.cjs.map +1 -0
  206. package/dist/migrate-BUhe0BuA.cjs +181 -0
  207. package/dist/migrate-BUhe0BuA.cjs.map +1 -0
  208. package/dist/migrate-Dp_Y2XGi.mjs +181 -0
  209. package/dist/migrate-Dp_Y2XGi.mjs.map +1 -0
  210. package/dist/notes-DHN4qThL.mjs +399 -0
  211. package/dist/notes-DHN4qThL.mjs.map +1 -0
  212. package/dist/notes-DUq6Bx1I.cjs +500 -0
  213. package/dist/notes-DUq6Bx1I.cjs.map +1 -0
  214. package/dist/openapi.cjs +213 -7
  215. package/dist/openapi.cjs.map +1 -0
  216. package/dist/openapi.d.cts +32 -8
  217. package/dist/openapi.d.cts.map +1 -1
  218. package/dist/openapi.d.mts +32 -8
  219. package/dist/openapi.d.mts.map +1 -1
  220. package/dist/openapi.mjs +208 -2
  221. package/dist/openapi.mjs.map +1 -0
  222. package/dist/projectBucket-CxsutB3D.cjs +263 -0
  223. package/dist/projectBucket-CxsutB3D.cjs.map +1 -0
  224. package/dist/projectBucket-DzLZlleB.mjs +222 -0
  225. package/dist/projectBucket-DzLZlleB.mjs.map +1 -0
  226. package/dist/provider-BqpjG9QM.cjs +57 -0
  227. package/dist/provider-BqpjG9QM.cjs.map +1 -0
  228. package/dist/provider-CEOKk8NS.mjs +46 -0
  229. package/dist/provider-CEOKk8NS.mjs.map +1 -0
  230. package/dist/publish-BVal0GFd.mjs +123 -0
  231. package/dist/publish-BVal0GFd.mjs.map +1 -0
  232. package/dist/publish-DmTJhVe8.cjs +124 -0
  233. package/dist/publish-DmTJhVe8.cjs.map +1 -0
  234. package/dist/ready-BNh3zsSn.cjs +404 -0
  235. package/dist/ready-BNh3zsSn.cjs.map +1 -0
  236. package/dist/ready-g25dcGzK.mjs +375 -0
  237. package/dist/ready-g25dcGzK.mjs.map +1 -0
  238. package/dist/reconcile/public.cjs +3 -5
  239. package/dist/reconcile/public.d.cts +31 -89
  240. package/dist/reconcile/public.d.cts.map +1 -1
  241. package/dist/reconcile/public.d.mts +31 -89
  242. package/dist/reconcile/public.d.mts.map +1 -1
  243. package/dist/reconcile/public.mjs +3 -3
  244. package/dist/{reconcile-DhQ00DpU.mjs → reconcile--I7V4Klj.mjs} +2 -2
  245. package/dist/{reconcile-DhQ00DpU.mjs.map → reconcile--I7V4Klj.mjs.map} +1 -1
  246. package/dist/{reconcile-Dd2iyILf.cjs → reconcile-CuCQBa5-.cjs} +3 -3
  247. package/dist/{reconcile-Dd2iyILf.cjs.map → reconcile-CuCQBa5-.cjs.map} +1 -1
  248. package/dist/resolve-BwTJI8K7.cjs +316 -0
  249. package/dist/resolve-BwTJI8K7.cjs.map +1 -0
  250. package/dist/resolve-UBq26cZc.mjs +263 -0
  251. package/dist/resolve-UBq26cZc.mjs.map +1 -0
  252. package/dist/rollback-BFKex9TO.cjs +165 -0
  253. package/dist/rollback-BFKex9TO.cjs.map +1 -0
  254. package/dist/rollback-DiOcLTRc.mjs +136 -0
  255. package/dist/rollback-DiOcLTRc.mjs.map +1 -0
  256. package/dist/runtime-CFvcLEJW.d.cts +82 -0
  257. package/dist/runtime-CFvcLEJW.d.cts.map +1 -0
  258. package/dist/runtime-dwsH6ddo.d.mts +82 -0
  259. package/dist/runtime-dwsH6ddo.d.mts.map +1 -0
  260. package/dist/s3-BgG_xW-D.cjs +215 -0
  261. package/dist/s3-BgG_xW-D.cjs.map +1 -0
  262. package/dist/s3-CkrerKdA.mjs +215 -0
  263. package/dist/s3-CkrerKdA.mjs.map +1 -0
  264. package/dist/sandbox/config-worker.cjs +43 -0
  265. package/dist/sandbox/config-worker.cjs.map +1 -0
  266. package/dist/sandbox/config-worker.d.cts +1 -0
  267. package/dist/sandbox/config-worker.d.mts +1 -0
  268. package/dist/sandbox/config-worker.mjs +44 -0
  269. package/dist/sandbox/config-worker.mjs.map +1 -0
  270. package/dist/sandbox/credentials-worker.cjs +53 -0
  271. package/dist/sandbox/credentials-worker.cjs.map +1 -0
  272. package/dist/sandbox/credentials-worker.d.cts +1 -0
  273. package/dist/sandbox/credentials-worker.d.mts +1 -0
  274. package/dist/sandbox/credentials-worker.mjs +54 -0
  275. package/dist/sandbox/credentials-worker.mjs.map +1 -0
  276. package/dist/sandbox/discover-worker.cjs +47 -0
  277. package/dist/sandbox/discover-worker.cjs.map +1 -0
  278. package/dist/sandbox/discover-worker.d.cts +1 -0
  279. package/dist/sandbox/discover-worker.d.mts +1 -0
  280. package/dist/sandbox/discover-worker.mjs +48 -0
  281. package/dist/sandbox/discover-worker.mjs.map +1 -0
  282. package/dist/sandbox/migrate-worker.cjs +75 -0
  283. package/dist/sandbox/migrate-worker.cjs.map +1 -0
  284. package/dist/sandbox/migrate-worker.d.cts +1 -0
  285. package/dist/sandbox/migrate-worker.d.mts +1 -0
  286. package/dist/sandbox/migrate-worker.mjs +76 -0
  287. package/dist/sandbox/migrate-worker.mjs.map +1 -0
  288. package/dist/{workspace-mFpI_ky_.cjs → schemas-BCwKKA42.mjs} +399 -1162
  289. package/dist/schemas-BCwKKA42.mjs.map +1 -0
  290. package/dist/{workspace-CgwmXEtc.mjs → schemas-BQ_v0lEX.cjs} +487 -971
  291. package/dist/schemas-BQ_v0lEX.cjs.map +1 -0
  292. package/dist/secretsManager-CQORubp9.mjs +97 -0
  293. package/dist/secretsManager-CQORubp9.mjs.map +1 -0
  294. package/dist/secretsManager-RAGcEDX1.cjs +108 -0
  295. package/dist/secretsManager-RAGcEDX1.cjs.map +1 -0
  296. package/dist/seeds-B3xvUAQf.cjs +363 -0
  297. package/dist/seeds-B3xvUAQf.cjs.map +1 -0
  298. package/dist/seeds-BCC8vAXR.mjs +298 -0
  299. package/dist/seeds-BCC8vAXR.mjs.map +1 -0
  300. package/dist/serverSetup-CUBu8_Mk.cjs +652 -0
  301. package/dist/serverSetup-CUBu8_Mk.cjs.map +1 -0
  302. package/dist/serverSetup-eh9MUdSN.mjs +652 -0
  303. package/dist/serverSetup-eh9MUdSN.mjs.map +1 -0
  304. package/dist/sniffer-CNTnUmhc.mjs +1824 -0
  305. package/dist/sniffer-CNTnUmhc.mjs.map +1 -0
  306. package/dist/sniffer-CaoQH_kk.cjs +1997 -0
  307. package/dist/sniffer-CaoQH_kk.cjs.map +1 -0
  308. package/dist/snsSubscriptions-DiqwM9Ms.cjs +1173 -0
  309. package/dist/snsSubscriptions-DiqwM9Ms.cjs.map +1 -0
  310. package/dist/snsSubscriptions-zOIIswN0.mjs +1048 -0
  311. package/dist/snsSubscriptions-zOIIswN0.mjs.map +1 -0
  312. package/dist/sst-D6Dvhr6H.mjs +462 -0
  313. package/dist/sst-D6Dvhr6H.mjs.map +1 -0
  314. package/dist/sst-DECRMgjv.cjs +485 -0
  315. package/dist/sst-DECRMgjv.cjs.map +1 -0
  316. package/dist/stack-BZG5HoVN.mjs +2206 -0
  317. package/dist/stack-BZG5HoVN.mjs.map +1 -0
  318. package/dist/stack-CLk9WojF.cjs +2381 -0
  319. package/dist/stack-CLk9WojF.cjs.map +1 -0
  320. package/dist/stage-Cl0DPJ7w.cjs +313 -0
  321. package/dist/stage-Cl0DPJ7w.cjs.map +1 -0
  322. package/dist/stage-DiLPb89O.mjs +302 -0
  323. package/dist/stage-DiLPb89O.mjs.map +1 -0
  324. package/dist/stages-Bs1THSy7.cjs +190 -0
  325. package/dist/stages-Bs1THSy7.cjs.map +1 -0
  326. package/dist/stages-_xCG4Ymu.mjs +190 -0
  327. package/dist/stages-_xCG4Ymu.mjs.map +1 -0
  328. package/dist/target/public.cjs +15 -0
  329. package/dist/target/public.d.cts +28 -0
  330. package/dist/target/public.d.cts.map +1 -0
  331. package/dist/target/public.d.mts +28 -0
  332. package/dist/target/public.d.mts.map +1 -0
  333. package/dist/target/public.mjs +4 -0
  334. package/dist/terminal-XQE106CG.cjs +85 -0
  335. package/dist/terminal-XQE106CG.cjs.map +1 -0
  336. package/dist/terminal-jGj9TtMQ.mjs +85 -0
  337. package/dist/terminal-jGj9TtMQ.mjs.map +1 -0
  338. package/dist/types-Bl_CwZC9.cjs +90 -0
  339. package/dist/types-Bl_CwZC9.cjs.map +1 -0
  340. package/dist/{types-BGo5TaNE.d.mts → types-Dk--xVOT.d.cts} +2234 -439
  341. package/dist/types-Dk--xVOT.d.cts.map +1 -0
  342. package/dist/{types-C5ZNpUad.d.cts → types-DwQYpd_0.d.mts} +2232 -439
  343. package/dist/types-DwQYpd_0.d.mts.map +1 -0
  344. package/dist/types-Y1NfWlyG.mjs +61 -0
  345. package/dist/types-Y1NfWlyG.mjs.map +1 -0
  346. package/dist/utils-D2Nk1Nv2.cjs +205 -0
  347. package/dist/utils-D2Nk1Nv2.cjs.map +1 -0
  348. package/dist/utils-D_fxUqvU.mjs +156 -0
  349. package/dist/utils-D_fxUqvU.mjs.map +1 -0
  350. package/dist/vitest.cjs +65 -0
  351. package/dist/vitest.cjs.map +1 -0
  352. package/dist/vitest.d.cts +29 -0
  353. package/dist/vitest.d.cts.map +1 -0
  354. package/dist/vitest.d.mts +29 -0
  355. package/dist/vitest.d.mts.map +1 -0
  356. package/dist/vitest.mjs +65 -0
  357. package/dist/vitest.mjs.map +1 -0
  358. package/dist/workerRuntime-BXfhbe13.cjs +108 -0
  359. package/dist/workerRuntime-BXfhbe13.cjs.map +1 -0
  360. package/dist/workerRuntime-CsOyBRx2.mjs +79 -0
  361. package/dist/workerRuntime-CsOyBRx2.mjs.map +1 -0
  362. package/dist/workspace/index.cjs +6 -2
  363. package/dist/workspace/index.d.cts +3 -3
  364. package/dist/workspace/index.d.mts +3 -3
  365. package/dist/workspace/index.mjs +2 -2
  366. package/dist/workspace-1szcahj9.cjs +2916 -0
  367. package/dist/workspace-1szcahj9.cjs.map +1 -0
  368. package/dist/workspace-DFGsrjGD.mjs +2480 -0
  369. package/dist/workspace-DFGsrjGD.mjs.map +1 -0
  370. package/package.json +66 -14
  371. package/dist/HostingerProvider-BY26bWwB.mjs.map +0 -1
  372. package/dist/HostingerProvider-BZFGYYVA.cjs.map +0 -1
  373. package/dist/LocalStateProvider-7HR6SS2t.cjs +0 -48
  374. package/dist/LocalStateProvider-7HR6SS2t.cjs.map +0 -1
  375. package/dist/LocalStateProvider-D7QyJoLM.mjs +0 -48
  376. package/dist/LocalStateProvider-D7QyJoLM.mjs.map +0 -1
  377. package/dist/Route53Provider-Cvj40A8I.mjs.map +0 -1
  378. package/dist/Route53Provider-DVILBsvV.cjs.map +0 -1
  379. package/dist/SSMStateProvider-B0Hu1MKZ.mjs +0 -75
  380. package/dist/SSMStateProvider-B0Hu1MKZ.mjs.map +0 -1
  381. package/dist/SSMStateProvider-VrQ-yeqd.cjs +0 -74
  382. package/dist/SSMStateProvider-VrQ-yeqd.cjs.map +0 -1
  383. package/dist/backup-provisioner-BmhIO_Yr.cjs.map +0 -1
  384. package/dist/backup-provisioner-DIVCbfee.mjs.map +0 -1
  385. package/dist/bundler-BeNDZdHQ.cjs.map +0 -1
  386. package/dist/bundler-BodTL4kf.mjs.map +0 -1
  387. package/dist/config-Q1rvKHvw.cjs +0 -467
  388. package/dist/config-Q1rvKHvw.cjs.map +0 -1
  389. package/dist/config-pqGD3hF9.mjs +0 -414
  390. package/dist/config-pqGD3hF9.mjs.map +0 -1
  391. package/dist/config.d.cts.map +0 -1
  392. package/dist/config.d.mts.map +0 -1
  393. package/dist/credentials-BekkKtY_.mjs +0 -2780
  394. package/dist/credentials-BekkKtY_.mjs.map +0 -1
  395. package/dist/credentials-Q6F0uqJq.cjs +0 -2917
  396. package/dist/credentials-Q6F0uqJq.cjs.map +0 -1
  397. package/dist/discover-BERGFg8v.cjs +0 -288
  398. package/dist/discover-BERGFg8v.cjs.map +0 -1
  399. package/dist/discover-CK-n3w6b.mjs +0 -244
  400. package/dist/discover-CK-n3w6b.mjs.map +0 -1
  401. package/dist/emit-BdoPFjZf.cjs +0 -253
  402. package/dist/emit-BdoPFjZf.cjs.map +0 -1
  403. package/dist/emit-Bzq7JsMP.mjs +0 -224
  404. package/dist/emit-Bzq7JsMP.mjs.map +0 -1
  405. package/dist/fullstack-secrets-BET8gqwK.mjs +0 -81
  406. package/dist/fullstack-secrets-BET8gqwK.mjs.map +0 -1
  407. package/dist/fullstack-secrets-DXsWO-5m.cjs +0 -92
  408. package/dist/fullstack-secrets-DXsWO-5m.cjs.map +0 -1
  409. package/dist/generator-LfZU7NlU.mjs +0 -247
  410. package/dist/generator-LfZU7NlU.mjs.map +0 -1
  411. package/dist/generator-Wdfp0jCS.cjs +0 -282
  412. package/dist/generator-Wdfp0jCS.cjs.map +0 -1
  413. package/dist/github-BfflNPVe.cjs +0 -203
  414. package/dist/github-BfflNPVe.cjs.map +0 -1
  415. package/dist/github-CRwnxKmD.mjs +0 -203
  416. package/dist/github-CRwnxKmD.mjs.map +0 -1
  417. package/dist/index-Cd-qeOam.d.mts.map +0 -1
  418. package/dist/index-D-iAVHx-.d.cts.map +0 -1
  419. package/dist/keystore-B4PRlbgE.mjs +0 -81
  420. package/dist/keystore-B4PRlbgE.mjs.map +0 -1
  421. package/dist/keystore-DgGRj2Yy.cjs +0 -98
  422. package/dist/keystore-DgGRj2Yy.cjs.map +0 -1
  423. package/dist/openapi-Dcbme-Us.cjs +0 -2038
  424. package/dist/openapi-Dcbme-Us.cjs.map +0 -1
  425. package/dist/openapi-vy4JzhUv.mjs +0 -1991
  426. package/dist/openapi-vy4JzhUv.mjs.map +0 -1
  427. package/dist/storage-C-30BD1e.cjs +0 -272
  428. package/dist/storage-C-30BD1e.cjs.map +0 -1
  429. package/dist/storage-P_b3w7uj.mjs +0 -231
  430. package/dist/storage-P_b3w7uj.mjs.map +0 -1
  431. package/dist/sync-B3xxXtJT.mjs +0 -88
  432. package/dist/sync-B3xxXtJT.mjs.map +0 -1
  433. package/dist/sync-D7uTIIEi.cjs +0 -111
  434. package/dist/sync-D7uTIIEi.cjs.map +0 -1
  435. package/dist/types-BGo5TaNE.d.mts.map +0 -1
  436. package/dist/types-C5ZNpUad.d.cts.map +0 -1
  437. package/dist/workspace-CgwmXEtc.mjs.map +0 -1
  438. package/dist/workspace-mFpI_ky_.cjs.map +0 -1
@@ -0,0 +1,2957 @@
1
+ import { t as __exportAll } from "./rolldown-runtime-BhDjJH2R.mjs";
2
+ import { t as GkmError } from "./errors-D8Gwp5kU.mjs";
3
+ import { F as toEmbeddableSecrets, M as initStageSecrets, Q as imageRef, a as findRootDomain, m as createComposeState, o as normalizeDnsConfig, ot as run, st as runOutput, v as recordRelease } from "./credentials-DRfP9aG7.mjs";
4
+ import { L as resolveStageTelemetry, ct as ComposeTlsFileMissing, gt as SERVER_IPV4_KEY, yt as isReservedStageKey } from "./workspace-DFGsrjGD.mjs";
5
+ import { B as findBuildRoot, C as ensureLocalCertificate, Dt as stageEnv, E as readLocalAuthority, F as imageLayout, Lt as usesTelemetry, P as fromBuildRoot, S as ensureLocalAuthority, St as postgresStatements, T as localCertificateNames, Tt as envFor, Y as nextPrerenderManifest, _t as applyPolicies, b as bucketClient, ct as BACKUPS_SERVICE, d as backendsOf, dt as backupsRoleStatements, f as constructGlobs, gt as applyNotifications, ht as applyLifecycles, l as workerEnvKeys, mt as applyBuckets, t as appEnvKeys, v as LEGACY_LOCAL_LOGIN, vt as applyPostgres, x as pgClient, y as ensurePostgresLogin } from "./apps-BOgA6AcI.mjs";
6
+ import { t as defineTarget } from "./define-6poNqm5O.mjs";
7
+ import { E as withRedisPassword, W as primaryPortKey, X as LOGS_SERVICE, Z as logsAccess, m as loadLocalCredentials, tt as withLogsPassword, w as runsRedis, x as REDIS_SERVICE, y as withGeneratedSecrets } from "./compose-B3OOowfE.mjs";
8
+ import { i as discover } from "./discover-DbDrCQ8h.mjs";
9
+ import { C as traefikStaticFile, E as assertImagesExist, O as pinImages, S as readEdgeClaims, T as RegistryRequired, _ as EDGE_NETWORK, a as composeProject, b as claimsOverlap, d as stackDir, g as EDGE_DYNAMIC_DIR, h as EDGE_CERTS_DIR, i as LOOPBACK_SERVICES, l as envFile, m as EDGE_AWS_CREDENTIALS, n as LOGS_PORT_ENV, o as composeStack, p as withCaddyConfigs, r as LOG_ROTATION, t as EDGE_PORT_ENV, u as replicaNames, v as EDGE_PROJECT, x as edgeComposeFile, y as EDGE_SERVICE } from "./stack-BZG5HoVN.mjs";
10
+ import { a as ensureDockerignore } from "./docker-CXYz3W2U.mjs";
11
+ import { a as subscriptionKey, c as HostNotPointingAtServer, f as requiredServerAddress, g as stageDns, h as stackHosts, i as recordedPushSubscriptions, n as SUBSCRIPTION_TYPE, o as subscriptionResource, p as serverAddressHint, r as confirmPendingSubscriptions, s as topicConnection, t as PushSubscriptionsUnconfirmed, u as isLocalHost, v as systemLookup } from "./snsSubscriptions-zOIIswN0.mjs";
12
+ import { i as installedFrom } from "./drivers-uxLBq3e3.mjs";
13
+ import { a as dnsResources, c as currentActor, d as DeployJournal, n as reportDatabaseRuns, o as forgetDnsResource, r as reportPlannedSeeds, s as recordDnsChanges, t as DeploySeedsFailed } from "./seeds-BCC8vAXR.mjs";
14
+ import { f as plannedSeeds, o as SeedFailed, p as seedDatabases, u as migrateDatabases } from "./databases-mzSUFBTw.mjs";
15
+ import { dirname, join, relative } from "node:path";
16
+ import { existsSync } from "node:fs";
17
+ import { chmod, mkdir, readFile, readdir, rm, writeFile } from "node:fs/promises";
18
+ import { hostname, userInfo } from "node:os";
19
+ import { spawn } from "node:child_process";
20
+ import { createConnection, createServer } from "node:net";
21
+ import { createHash } from "node:crypto";
22
+ import { provideKey } from "@geekmidas/manifest";
23
+ import { stringify } from "yaml";
24
+ import { request } from "node:http";
25
+ import { request as request$1 } from "node:https";
26
+ //#region src/compose/docker.ts
27
+ /**
28
+ * The Docker operations `gkm compose` needs, behind an interface.
29
+ *
30
+ * Small on purpose, like reconcile's: the rules about *when* each runs — the
31
+ * registry is asked before anything is pulled, nothing is started for a tag
32
+ * that is missing — are asserted against a fake, and this file is only the
33
+ * wiring to the real CLI.
34
+ *
35
+ * Every call is an argument array. Image refs, tags and paths come from the
36
+ * workspace, and none of them reaches a shell.
37
+ */
38
+ /** `output` as `spawn` reads it. */
39
+ const STDIO = {
40
+ inherit: "inherit",
41
+ stderr: [
42
+ "ignore",
43
+ 2,
44
+ 2
45
+ ],
46
+ ignore: "ignore"
47
+ };
48
+ /**
49
+ * The environment a docker command for `engine` runs with: this process's,
50
+ * with `DOCKER_HOST` pointing at the stack's engine when it is a server's.
51
+ */
52
+ function engineEnv(engine, extra = {}) {
53
+ if (!engine.host && Object.keys(extra).length === 0) return void 0;
54
+ return {
55
+ ...process.env,
56
+ ...extra,
57
+ ...engine.host ? { DOCKER_HOST: engine.host } : {}
58
+ };
59
+ }
60
+ /** How a stack's docker commands run: where it is, where output goes. */
61
+ function runOptions(stack, extra) {
62
+ const env = engineEnv(stack, extra);
63
+ return {
64
+ cwd: stack.cwd,
65
+ stdio: STDIO[stack.output ?? "inherit"],
66
+ ...env ? { env } : {},
67
+ ...stack.signal ? { signal: stack.signal } : {}
68
+ };
69
+ }
70
+ /** The network compose puts a project's services on. */
71
+ function projectNetwork(project) {
72
+ return `${project}_default`;
73
+ }
74
+ /**
75
+ * The host ports a `docker ps` Ports column publishes: `0.0.0.0:443->443/tcp,
76
+ * [::]:443->443/tcp, 0.0.0.0:9000-9001->9000-9001/tcp`.
77
+ */
78
+ function publishedPorts(column) {
79
+ const ports = /* @__PURE__ */ new Set();
80
+ for (const match of column.matchAll(/(?:[\d.]+|\[[^\]]*\]):(\d+)(?:-(\d+))?->/g)) {
81
+ const first = Number(match[1]);
82
+ const last = match[2] ? Number(match[2]) : first;
83
+ for (let port = first; port <= last; port++) ports.add(port);
84
+ }
85
+ return [...ports];
86
+ }
87
+ /**
88
+ * The host port a `docker ps` Ports column publishes for `inside`:
89
+ * `127.0.0.1:54321->5432/tcp` is 54321.
90
+ */
91
+ function publishedPort(column, inside) {
92
+ const match = new RegExp(`(?:[\\d.]+|\\[[^\\]]*\\]):(\\d+)->${inside}/tcp`).exec(column);
93
+ return match ? Number(match[1]) : void 0;
94
+ }
95
+ /** Each line `docker ps` printed, as the containers holding `port`. */
96
+ function portHolders(output, port) {
97
+ return output.split("\n").filter(Boolean).map((line) => line.split(" ")).filter(([, , , column = ""]) => publishedPorts(column).includes(port)).map(([container = "", project, service]) => ({
98
+ container,
99
+ ...project ? { project } : {},
100
+ ...service ? { service } : {}
101
+ }));
102
+ }
103
+ /** How long a stack has to pass its health checks. */
104
+ const WAIT_TIMEOUT_S = 180;
105
+ /** `docker` itself failed, before it could say anything about a service. */
106
+ var DockerUnreachable = class extends GkmError {
107
+ engine;
108
+ stderr;
109
+ constructor(engine, stderr) {
110
+ super(`Could not ask Docker on ${engine} which ports its services publish (docker said: ${stderr.trim() || "nothing"}). Check that \`DOCKER_HOST=${engine} docker ps\` works from here.`);
111
+ this.engine = engine;
112
+ this.stderr = stderr;
113
+ this.name = "DockerUnreachable";
114
+ }
115
+ };
116
+ /** `docker compose port` printed nothing a port can be read from. */
117
+ var ServicePortUnknown = class extends GkmError {
118
+ service;
119
+ inside;
120
+ output;
121
+ constructor(service, inside, output) {
122
+ super(`'${service}' publishes no host port for ${inside} (docker said: ${output.trim() || "nothing"}). Is it running? \`docker compose ps\` shows the stack.`);
123
+ this.service = service;
124
+ this.inside = inside;
125
+ this.output = output;
126
+ this.name = "ServicePortUnknown";
127
+ }
128
+ };
129
+ /**
130
+ * Whether `ref` names a registry on this machine's loopback — which the
131
+ * daemon pulls from and pushes to over plain HTTP by default, and `docker
132
+ * manifest inspect` reaches only when told `--insecure` (without it, a local
133
+ * registry's image is reported as no such manifest).
134
+ */
135
+ function isLoopbackRegistry(ref) {
136
+ const first = ref.split("/")[0];
137
+ if (!ref.includes("/") || !/[.:]|^localhost$/.test(first)) return false;
138
+ const host = first.replace(/:\d+$/, "");
139
+ return host === "localhost" || /^127(?:\.\d{1,3}){3}$/.test(host);
140
+ }
141
+ /** `docker push` finished without saying what digest the registry stored. */
142
+ var PushDigestUnknown = class extends GkmError {
143
+ ref;
144
+ output;
145
+ constructor(ref, output) {
146
+ super(`Pushed ${ref}, but docker did not say which digest the registry stored (it said: ${output.trim().split("\n").slice(-3).join(" / ") || "nothing"}). Check the push with \`docker buildx imagetools inspect ${ref}\`.`);
147
+ this.ref = ref;
148
+ this.output = output;
149
+ this.name = "PushDigestUnknown";
150
+ }
151
+ };
152
+ /** The digest `docker push` reports last: `<tag>: digest: sha256:… size: …`. */
153
+ function pushedDigest(output) {
154
+ return [...output.matchAll(/digest: (sha256:[0-9a-f]{64})/g)].at(-1)?.[1];
155
+ }
156
+ function compose(stack, args) {
157
+ return [
158
+ "compose",
159
+ "-p",
160
+ stack.project,
161
+ "-f",
162
+ stack.file,
163
+ ...(stack.overrides ?? []).flatMap((file) => ["-f", file]),
164
+ ...args
165
+ ];
166
+ }
167
+ /** Run a program and keep everything it said, whatever it exits with. */
168
+ function capture(command, args, options = {}) {
169
+ const { timeoutMs = 6e4 } = options;
170
+ return new Promise((resolve, reject) => {
171
+ const child = spawn(command, [...args], {
172
+ stdio: [
173
+ options.input === void 0 ? "ignore" : "pipe",
174
+ "pipe",
175
+ "pipe"
176
+ ],
177
+ shell: false,
178
+ ...options.env ? { env: options.env } : {}
179
+ });
180
+ if (options.input !== void 0) child.stdin?.end(options.input);
181
+ let stdout = "";
182
+ let stderr = "";
183
+ child.stdout?.on("data", (chunk) => {
184
+ stdout += chunk.toString();
185
+ });
186
+ child.stderr?.on("data", (chunk) => {
187
+ stderr += chunk.toString();
188
+ });
189
+ const timer = setTimeout(() => child.kill("SIGKILL"), timeoutMs);
190
+ timer.unref?.();
191
+ child.on("error", (error) => {
192
+ clearTimeout(timer);
193
+ reject(error);
194
+ });
195
+ child.on("close", (code) => {
196
+ clearTimeout(timer);
197
+ resolve({
198
+ code,
199
+ stdout,
200
+ stderr
201
+ });
202
+ });
203
+ });
204
+ }
205
+ /**
206
+ * Whether a failed `docker manifest inspect` means the tag is not there, as
207
+ * opposed to the registry not answering.
208
+ *
209
+ * Read off the message because that is all the CLI gives. Anything not
210
+ * recognisably "no such tag" is treated as the registry being unreachable:
211
+ * a registry that refused credentials must not be reported as a tag that was
212
+ * never pushed, or the fix offered would be the wrong one.
213
+ */
214
+ function isMissingManifest(output) {
215
+ return /no such manifest|manifest unknown|manifest for .* not found|not found: manifest/i.test(output);
216
+ }
217
+ const dockerCompose = {
218
+ async lookup(ref) {
219
+ const { code, stdout, stderr } = await capture("docker", [
220
+ "manifest",
221
+ "inspect",
222
+ ...isLoopbackRegistry(ref) ? ["--insecure"] : [],
223
+ ref
224
+ ]);
225
+ if (code === 0) return {
226
+ ref,
227
+ status: "found"
228
+ };
229
+ const output = `${stderr}\n${stdout}`.trim();
230
+ return isMissingManifest(output) ? {
231
+ ref,
232
+ status: "missing"
233
+ } : {
234
+ ref,
235
+ status: "unreachable",
236
+ detail: output
237
+ };
238
+ },
239
+ async build(stack, services) {
240
+ await run("docker", compose(stack, ["build", ...services]), runOptions(stack, {
241
+ ...process.env,
242
+ DOCKER_BUILDKIT: "1"
243
+ }));
244
+ },
245
+ async pull(stack, services) {
246
+ await run("docker", compose(stack, ["pull", ...services]), runOptions(stack));
247
+ },
248
+ async up(stack, services) {
249
+ await run("docker", compose(stack, [
250
+ "up",
251
+ "-d",
252
+ "--wait",
253
+ `--wait-timeout=${WAIT_TIMEOUT_S}`,
254
+ ...services ? [] : ["--remove-orphans"],
255
+ ...services ?? []
256
+ ]), runOptions(stack));
257
+ },
258
+ async down(stack) {
259
+ await run("docker", compose(stack, ["down"]), runOptions(stack));
260
+ },
261
+ async port(stack, service, inside) {
262
+ const env = engineEnv(stack);
263
+ const output = await runOutput("docker", compose(stack, [
264
+ "port",
265
+ service,
266
+ String(inside)
267
+ ]), {
268
+ cwd: stack.cwd,
269
+ ...env ? { env } : {},
270
+ ...stack.signal ? { signal: stack.signal } : {}
271
+ });
272
+ const port = Number(output.trim().split(":").pop());
273
+ if (!Number.isInteger(port) || port <= 0) throw new ServicePortUnknown(service, inside, output);
274
+ return port;
275
+ },
276
+ async health(stack, service) {
277
+ const env = engineEnv(stack);
278
+ const { code, stdout } = await capture("docker", compose(stack, [
279
+ "ps",
280
+ "--format",
281
+ "{{.Health}}",
282
+ service
283
+ ]), env ? { env } : {});
284
+ if (code !== 0) return void 0;
285
+ return stdout.trim().split("\n")[0] || void 0;
286
+ },
287
+ async push(stack, ref) {
288
+ const output = await runOutput("docker", ["push", ref], {
289
+ cwd: stack.cwd,
290
+ ...stack.signal ? { signal: stack.signal } : {}
291
+ });
292
+ const digest = pushedDigest(output);
293
+ if (!digest) throw new PushDigestUnknown(ref, output);
294
+ return digest;
295
+ },
296
+ async digest(engine, ref) {
297
+ const env = engineEnv(engine);
298
+ const { code, stdout } = await capture("docker", [
299
+ "image",
300
+ "inspect",
301
+ "--format={{json .RepoDigests}} {{.Id}}",
302
+ ref
303
+ ], env ? { env } : {});
304
+ if (code !== 0) return void 0;
305
+ const [json = "[]", id] = stdout.trim().split(" ");
306
+ const repository = ref.split("@")[0].replace(/:[\w][\w.-]*$/, "");
307
+ try {
308
+ const digests = JSON.parse(json);
309
+ const match = Array.isArray(digests) ? digests.find((entry) => typeof entry === "string" && entry.startsWith(`${repository}@`)) : void 0;
310
+ if (match) return match.slice(repository.length + 1);
311
+ } catch {}
312
+ return id || void 0;
313
+ },
314
+ async ensureNetwork(engine, name) {
315
+ const env = engineEnv(engine);
316
+ const options = env ? { env } : {};
317
+ const { code } = await capture("docker", [
318
+ "network",
319
+ "inspect",
320
+ name
321
+ ], options);
322
+ if (code === 0) return;
323
+ const created = await capture("docker", [
324
+ "network",
325
+ "create",
326
+ name
327
+ ], options);
328
+ if (created.code !== 0 && !/already exists/.test(created.stderr)) throw new NetworkCreateFailed(name, created.stderr);
329
+ },
330
+ async publishers(engine, port) {
331
+ const env = engineEnv(engine);
332
+ const { stdout } = await capture("docker", [
333
+ "ps",
334
+ "--format",
335
+ "{{.Names}} {{.Label \"com.docker.compose.project\"}} {{.Label \"com.docker.compose.service\"}} {{.Ports}}"
336
+ ], env ? { env } : {});
337
+ return portHolders(stdout, port);
338
+ },
339
+ async exec(stack, service, command, input) {
340
+ const env = engineEnv(stack);
341
+ const options = env ? { env } : {};
342
+ const { stdout: ids } = await capture("docker", [
343
+ "ps",
344
+ "-q",
345
+ "--filter",
346
+ `label=com.docker.compose.project=${stack.project}`,
347
+ "--filter",
348
+ `label=com.docker.compose.service=${service}`
349
+ ], options);
350
+ const container = ids.trim().split("\n")[0];
351
+ if (!container) return {
352
+ code: null,
353
+ stdout: "",
354
+ stderr: `${service} is not running`
355
+ };
356
+ return capture("docker", [
357
+ "exec",
358
+ ...input === void 0 ? [] : ["-i"],
359
+ container,
360
+ ...command
361
+ ], {
362
+ ...options,
363
+ ...input === void 0 ? {} : { input }
364
+ });
365
+ },
366
+ async container(engine, project, service) {
367
+ const env = engineEnv(engine);
368
+ const { stdout } = await capture("docker", [
369
+ "ps",
370
+ "--filter",
371
+ `label=com.docker.compose.project=${project}`,
372
+ "--filter",
373
+ `label=com.docker.compose.service=${service}`,
374
+ "--format",
375
+ "{{.ID}} {{.Image}}"
376
+ ], env ? { env } : {});
377
+ const [id, image] = stdout.trim().split("\n")[0]?.split(" ") ?? [];
378
+ return id && image ? {
379
+ id,
380
+ image
381
+ } : void 0;
382
+ },
383
+ async published(engine, project, service, inside) {
384
+ const env = engineEnv(engine);
385
+ const { code, stdout, stderr } = await capture("docker", [
386
+ "ps",
387
+ "--filter",
388
+ `label=com.docker.compose.project=${project}`,
389
+ "--filter",
390
+ `label=com.docker.compose.service=${service}`,
391
+ "--format",
392
+ "{{.Ports}}"
393
+ ], env ? { env } : {});
394
+ if (code !== 0) throw new DockerUnreachable(engine.host ?? "this machine", stderr);
395
+ return publishedPort(stdout, inside);
396
+ },
397
+ runOnce(engine, options) {
398
+ return dockerExit([
399
+ "run",
400
+ "--rm",
401
+ ...options.stdin ? ["-i"] : [],
402
+ "--network",
403
+ options.network,
404
+ ...Object.keys(options.env ?? {}).flatMap((name) => ["-e", name]),
405
+ "--entrypoint",
406
+ "",
407
+ options.image,
408
+ ...options.command
409
+ ], {
410
+ env: engineEnv(engine, { ...options.env }) ?? process.env,
411
+ ...options.stdin ? { stdin: options.stdin } : {},
412
+ ...options.output ? { output: options.output } : {}
413
+ });
414
+ }
415
+ };
416
+ /** Spawn `docker`, its output where `output` says; resolve with the exit code. */
417
+ function dockerExit(args, options = {}) {
418
+ const output = STDIO[options.output ?? "inherit"];
419
+ const out = Array.isArray(output) ? output : [
420
+ output,
421
+ output,
422
+ output
423
+ ];
424
+ return new Promise((resolve, reject) => {
425
+ const child = spawn("docker", [...args], {
426
+ stdio: [
427
+ options.stdin ? "pipe" : "ignore",
428
+ out[1],
429
+ out[2]
430
+ ],
431
+ env: options.env ?? process.env,
432
+ shell: false
433
+ });
434
+ if (options.stdin && child.stdin) {
435
+ options.stdin.on("error", (error) => child.stdin?.destroy(error));
436
+ child.stdin.on("error", () => {});
437
+ options.stdin.pipe(child.stdin);
438
+ }
439
+ child.on("error", reject);
440
+ child.on("close", (code) => resolve(code ?? 1));
441
+ });
442
+ }
443
+ /** `docker network create` failed. */
444
+ var NetworkCreateFailed = class extends GkmError {
445
+ network;
446
+ output;
447
+ constructor(network, output) {
448
+ super(`Could not create the Docker network '${network}' (docker said: ${output.trim() || "nothing"}). Create it by hand with \`docker network create ${network}\` and run again.`);
449
+ this.network = network;
450
+ this.output = output;
451
+ this.name = "NetworkCreateFailed";
452
+ }
453
+ };
454
+ //#endregion
455
+ //#region src/compose/server.ts
456
+ /**
457
+ * Where a deployed compose stage runs: its server, reached over SSH.
458
+ *
459
+ * gkm never runs on the server. It runs where the deploy is started — a CI
460
+ * runner, a developer's machine — and drives the server's Docker engine over
461
+ * SSH (`DOCKER_HOST=ssh://user@host`), so the server holds Docker, an SSH
462
+ * login and nothing else: no gkm, no checkout, no cloud credentials.
463
+ *
464
+ * ```ts
465
+ * deploy: { compose: { server: { production: { user: 'deploy' } } } }
466
+ * ```
467
+ *
468
+ * The host is the stage's `GKM_SERVER_IPV4` secret — the address its DNS
469
+ * records already point at — unless `host` names another. The local stage is
470
+ * never given one: it runs on this machine's Docker.
471
+ *
472
+ * What the deploy needs from inside the server — Postgres and MinIO, published
473
+ * on the server's loopback alone — it reaches through an SSH tunnel on the same
474
+ * login, so neither is ever published publicly.
475
+ */
476
+ /** A deployed compose stage with nowhere to run. */
477
+ var ComposeServerMissing = class extends GkmError {
478
+ stage;
479
+ missing;
480
+ constructor(stage, missing) {
481
+ super(missing === "config" ? `'${stage}' is a deployed stage, and gkm deploys it to its server's Docker over SSH — it never starts a deployed stage on this machine's Docker. Name the login in gkm.config.ts: deploy: { compose: { server: { ${stage}: { user: 'deploy' } } } } — the host is the stage's ${SERVER_IPV4_KEY} secret (gkm secrets:set ${SERVER_IPV4_KEY} '<ip>' --stage ${stage}), or set host there too.` : `deploy.compose.server.${stage} names no host, and '${stage}' has no ${SERVER_IPV4_KEY} secret to default to. Set it: gkm secrets:set ${SERVER_IPV4_KEY} '<ip>' --stage ${stage} — or set deploy.compose.server.${stage}.host.`);
482
+ this.stage = stage;
483
+ this.missing = missing;
484
+ this.name = "ComposeServerMissing";
485
+ }
486
+ };
487
+ /** SSH to the stage's server failed before anything was changed there. */
488
+ var ComposeServerUnreachable = class extends GkmError {
489
+ target;
490
+ stderr;
491
+ constructor(target, stderr) {
492
+ super(`Could not reach Docker on ${target} over SSH (ssh said: ${stderr.trim() || "nothing"}). Check that \`ssh ${target} docker version\` works from here: the host key in ~/.ssh/known_hosts, the key in your agent or ~/.ssh/config, and the user in the server's docker group.`);
493
+ this.target = target;
494
+ this.stderr = stderr;
495
+ this.name = "ComposeServerUnreachable";
496
+ }
497
+ };
498
+ /** An SSH tunnel to a port on the server's loopback did not open. */
499
+ var ComposeTunnelFailed = class extends GkmError {
500
+ target;
501
+ remotePort;
502
+ stderr;
503
+ constructor(target, remotePort, stderr) {
504
+ super(`Could not open an SSH tunnel to 127.0.0.1:${remotePort} on ${target} (ssh said: ${stderr.trim() || "nothing"}). The server's sshd must allow TCP forwarding for the deploy user (AllowTcpForwarding yes, the default, or local), and its key in ~/.ssh/authorized_keys must too: a key installed with \`restrict\` refuses forwarding unless it also says \`port-forwarding\` (\`restrict,port-forwarding ssh-ed25519 …\`).`);
505
+ this.target = target;
506
+ this.remotePort = remotePort;
507
+ this.stderr = stderr;
508
+ this.name = "ComposeTunnelFailed";
509
+ }
510
+ };
511
+ /**
512
+ * The stage's server: its login from `deploy.compose.server.<stage>`, its
513
+ * host from there or the stage's `GKM_SERVER_IPV4`. Undefined for the local
514
+ * stage, which runs on this machine.
515
+ *
516
+ * @throws {ComposeServerMissing} for a deployed stage with no login or host
517
+ */
518
+ function composeServer(options) {
519
+ const { stage } = options;
520
+ if (options.local) return void 0;
521
+ const config = options.config?.[stage];
522
+ if (!config) throw new ComposeServerMissing(stage, "config");
523
+ const host = config.host ?? options.custom?.["GKM_SERVER_IPV4"]?.trim();
524
+ if (!host) throw new ComposeServerMissing(stage, "host");
525
+ return {
526
+ user: config.user,
527
+ host,
528
+ port: config.port ?? 22
529
+ };
530
+ }
531
+ /** `user@host`, as a person types it after `ssh`. */
532
+ function sshTarget(server) {
533
+ return `${server.user}@${server.host}`;
534
+ }
535
+ /** The server's Docker engine, as `DOCKER_HOST` names it. */
536
+ function dockerHost(server) {
537
+ const host = server.host.includes(":") ? `[${server.host}]` : server.host;
538
+ return `ssh://${server.user}@${host}${server.port === 22 ? "" : `:${server.port}`}`;
539
+ }
540
+ /** The arguments every `ssh` gkm runs starts with: no prompt, ever. */
541
+ function sshArgs(server) {
542
+ return [
543
+ "-o",
544
+ "BatchMode=yes",
545
+ "-o",
546
+ "ConnectTimeout=20",
547
+ "-p",
548
+ String(server.port),
549
+ sshTarget(server)
550
+ ];
551
+ }
552
+ async function freeLocalPort() {
553
+ const server = createServer();
554
+ await new Promise((resolve, reject) => {
555
+ server.once("error", reject);
556
+ server.listen(0, "127.0.0.1", resolve);
557
+ });
558
+ const address = server.address();
559
+ await new Promise((resolve) => server.close(resolve));
560
+ if (!address || typeof address === "string") return 0;
561
+ return address.port;
562
+ }
563
+ function connects(port) {
564
+ return new Promise((resolve) => {
565
+ const socket = createConnection({
566
+ host: "127.0.0.1",
567
+ port
568
+ });
569
+ socket.once("connect", () => {
570
+ socket.destroy();
571
+ resolve(true);
572
+ });
573
+ socket.once("error", () => resolve(false));
574
+ });
575
+ }
576
+ /** The real one: the system's `ssh`, with the user's keys and known hosts. */
577
+ const sshAccess = {
578
+ check(server) {
579
+ return new Promise((resolve, reject) => {
580
+ const child = spawn("ssh", [
581
+ ...sshArgs(server),
582
+ "docker",
583
+ "version",
584
+ "--format",
585
+ "{{.Server.Version}}"
586
+ ], { stdio: [
587
+ "ignore",
588
+ "ignore",
589
+ "pipe"
590
+ ] });
591
+ let stderr = "";
592
+ child.stderr.on("data", (chunk) => {
593
+ stderr += chunk.toString();
594
+ });
595
+ child.on("error", (error) => reject(new ComposeServerUnreachable(sshTarget(server), error.message)));
596
+ child.on("close", (code) => code === 0 ? resolve() : reject(new ComposeServerUnreachable(sshTarget(server), stderr)));
597
+ });
598
+ },
599
+ async tunnel(server, remotePort) {
600
+ const port = await freeLocalPort();
601
+ const child = spawn("ssh", [
602
+ "-N",
603
+ "-o",
604
+ "ExitOnForwardFailure=yes",
605
+ "-o",
606
+ "ControlPath=none",
607
+ "-L",
608
+ `127.0.0.1:${port}:127.0.0.1:${remotePort}`,
609
+ ...sshArgs(server)
610
+ ], { stdio: [
611
+ "ignore",
612
+ "ignore",
613
+ "pipe"
614
+ ] });
615
+ let stderr = "";
616
+ let exited = false;
617
+ child.stderr.on("data", (chunk) => {
618
+ stderr += chunk.toString();
619
+ });
620
+ child.on("exit", () => {
621
+ exited = true;
622
+ });
623
+ child.on("error", (error) => {
624
+ exited = true;
625
+ stderr += error.message;
626
+ });
627
+ const close = async () => {
628
+ if (exited) return;
629
+ const gone = new Promise((resolve) => child.once("exit", resolve));
630
+ child.kill("SIGTERM");
631
+ await gone;
632
+ };
633
+ for (let attempt = 0; attempt < 150; attempt++) {
634
+ if (exited) break;
635
+ if (await connects(port)) return {
636
+ port,
637
+ close
638
+ };
639
+ await new Promise((resolve) => setTimeout(resolve, 200));
640
+ }
641
+ await close();
642
+ throw new ComposeTunnelFailed(sshTarget(server), remotePort, stderr);
643
+ }
644
+ };
645
+ //#endregion
646
+ //#region src/compose/edge.ts
647
+ /**
648
+ * The server's shared Traefik edge, in Docker: started when a stack needs it,
649
+ * and each stack's routes written to — and removed from — the directory it
650
+ * watches.
651
+ *
652
+ * Everything goes through the server's Docker engine, never its filesystem:
653
+ * the edge's static configuration is handed to it inline, and the directories
654
+ * it watches are volumes each stack writes into with `docker exec`. So the
655
+ * deploy runs anywhere that reaches the engine — a CI runner over SSH — and
656
+ * nothing it writes depends on a path on that machine.
657
+ *
658
+ * gkm-edge (compose project — `GKM_EDGE_PROJECT` renames it)
659
+ * traefik.yml its static configuration (inline config)
660
+ * dynamic/<project>.yml one per stack: routers, services, middlewares
661
+ * certs/<project>.crt|.key a stack's own certificate, where it has one
662
+ */
663
+ /** The edge's compose project, as docker is asked about it. */
664
+ function edgeRef(file, options = {}) {
665
+ return {
666
+ project: EDGE_PROJECT,
667
+ file,
668
+ cwd: dirname(file),
669
+ ...options
670
+ };
671
+ }
672
+ /** Writing to or removing from the edge's volumes failed. */
673
+ var EdgeWriteFailed = class extends GkmError {
674
+ path;
675
+ output;
676
+ constructor(path, output) {
677
+ super(`Could not write ${path} in the shared edge (${EDGE_PROJECT}): ${output.trim() || "it is not running"}. Check it with \`docker compose -p ${EDGE_PROJECT} ps\` on the server, and run gkm compose again.`);
678
+ this.path = path;
679
+ this.output = output;
680
+ this.name = "EdgeWriteFailed";
681
+ }
682
+ };
683
+ /**
684
+ * Something other than the proxy a stack is set to holds the edge's ports.
685
+ *
686
+ * With `proxy: 'traefik'`, the stack's own Caddy — still running from before
687
+ * the switch — or any other container; with `proxy: 'caddy'`, the shared
688
+ * edge, or another stack's Caddy. Refused before anything is started, so the
689
+ * stack is never left half on one proxy and half on the other.
690
+ */
691
+ var ComposeProxyClash = class extends GkmError {
692
+ project;
693
+ proxy;
694
+ port;
695
+ holder;
696
+ constructor(project, proxy, port, holder) {
697
+ const by = holder.project ? `${holder.container} (compose project ${holder.project})` : holder.container;
698
+ const fix = proxy === "traefik" ? holder.project === project && holder.service === "caddy" ? `That is this stack's own Caddy, from before it was switched to proxy: 'traefik'. Move it once: stop it (docker compose -p ${project} stop caddy), then run gkm compose again — the shared edge takes 80/443 and the stack registers with it.` : `The shared edge needs ports 80 and 443. Stop ${holder.container}${holder.project ? ` (docker compose -p ${holder.project} down, or its own gkm compose --down)` : ""}, or move the edge with GKM_COMPOSE_HTTPS_PORT/GKM_COMPOSE_HTTP_PORT.` : holder.project === EDGE_PROJECT ? `That is the server's shared Traefik edge, which other stacks register with. Set deploy.compose.proxy to 'traefik' for this stage so the stack registers with it too, or stop the edge once nothing uses it (docker compose -p ${EDGE_PROJECT} down).` : `Stop ${holder.container}, or publish this stack's Caddy elsewhere with GKM_COMPOSE_HTTPS_PORT/GKM_COMPOSE_HTTP_PORT.`;
699
+ super(`${project} is set to proxy: '${proxy}', and port ${port} is held by ${by}. ${fix}`);
700
+ this.project = project;
701
+ this.proxy = proxy;
702
+ this.port = port;
703
+ this.holder = holder;
704
+ this.name = "ComposeProxyClash";
705
+ }
706
+ };
707
+ /**
708
+ * Refuse a stack whose edge ports are held by anything but its own proxy:
709
+ * the shared edge for `'traefik'`, the stack's own Caddy for `'caddy'`.
710
+ */
711
+ async function assertEdgePorts(docker, options) {
712
+ const { project, proxy, ports } = options;
713
+ for (const port of [ports.https, ports.http]) for (const holder of await docker.publishers(options.engine, port)) if (!(proxy === "traefik" ? holder.project === EDGE_PROJECT : holder.project === project && holder.service === "caddy")) throw new ComposeProxyClash(project, proxy, port, holder);
714
+ }
715
+ /**
716
+ * Write a file into the edge's running container whole or not at all: a
717
+ * temporary name, then a rename. Not `.yml`: the edge reads only `.yml`,
718
+ * `.yaml` and `.toml` files, so it never sees one half written.
719
+ */
720
+ async function put(docker, edge, path, content, mode = "644") {
721
+ const result = await docker.exec(edge, EDGE_SERVICE, [
722
+ "sh",
723
+ "-c",
724
+ "set -e; t=\"$1.tmp\"; cat > \"$t\"; chmod \"$2\" \"$t\"; mv \"$t\" \"$1\"",
725
+ "sh",
726
+ path,
727
+ mode
728
+ ], content);
729
+ if (result.code !== 0) throw new EdgeWriteFailed(path, `${result.stderr}${result.stdout}`);
730
+ }
731
+ /** Remove files from the edge's container; whether the first was there. */
732
+ async function remove(docker, edge, paths) {
733
+ return (await docker.exec(edge, EDGE_SERVICE, [
734
+ "sh",
735
+ "-c",
736
+ "e=1; [ -f \"$1\" ] && e=0; rm -f \"$@\"; exit $e",
737
+ "sh",
738
+ ...paths
739
+ ])).code === 0;
740
+ }
741
+ /**
742
+ * The edge, up: its network and its container — started when it is not
743
+ * running, and recreated when its configuration changed. Idempotent: on a
744
+ * server where it already runs as configured, `up` changes nothing. Its
745
+ * compose file is written to `file`, on this machine: compose reads it here.
746
+ */
747
+ async function ensureEdge(docker, options) {
748
+ const { file, ports } = options;
749
+ const staticConfig = traefikStaticFile(ports);
750
+ const content = edgeComposeFile({
751
+ ports,
752
+ staticConfig,
753
+ logging: options.logging
754
+ });
755
+ await mkdir(dirname(file), { recursive: true });
756
+ if (await readFile(file, "utf-8").catch(() => void 0) !== content) await writeFile(file, content);
757
+ const ref = edgeRef(file, {
758
+ ...options.host ? { host: options.host } : {},
759
+ ...options.output ? { output: options.output } : {},
760
+ ...options.signal ? { signal: options.signal } : {}
761
+ });
762
+ await docker.ensureNetwork(ref, EDGE_NETWORK);
763
+ await docker.up(ref, [EDGE_SERVICE]);
764
+ return [file];
765
+ }
766
+ /** A file inside the edge's running container, or undefined where there is none. */
767
+ async function readEdgeFile(docker, edge, path) {
768
+ const result = await docker.exec(edge, EDGE_SERVICE, ["cat", path]);
769
+ return result.code === 0 ? result.stdout : void 0;
770
+ }
771
+ /** A file written into the edge's running container, whole, readable by it alone. */
772
+ async function writeEdgeSecret(docker, edge, path, content) {
773
+ await put(docker, edge, path, content, "600");
774
+ }
775
+ /**
776
+ * The edge restarted — what it reads once and keeps, a key among them, read
777
+ * again — and waited on until it is healthy. Its own restart policy brings
778
+ * it back; `up` waits for it.
779
+ */
780
+ async function restartEdge(docker, edge) {
781
+ await docker.exec(edge, EDGE_SERVICE, ["kill", "1"]);
782
+ await docker.up(edge, [EDGE_SERVICE]);
783
+ }
784
+ /** A stack's routes, inside the edge's container. */
785
+ function routesPath(project) {
786
+ return `${EDGE_DYNAMIC_DIR}/${project}.yml`;
787
+ }
788
+ /**
789
+ * A host this stack routes is already routed through the shared edge by
790
+ * another project: two routers for one host, and which stack serves it — and
791
+ * which gets its certificate — would be the edge's choice.
792
+ */
793
+ var EdgeHostClaimed = class extends GkmError {
794
+ host;
795
+ owner;
796
+ project;
797
+ edge;
798
+ constructor(host, owner, project, edge = EDGE_PROJECT) {
799
+ super(`${project} routes ${host}, and ${owner} already routes it through the shared edge (${edge}) on this server. A host is served by one stack only. Give ${project} another domain, or take ${owner} off the edge first: gkm compose --stage <stage> --down in its workspace (or remove ${routesPath(owner)} from the edge).`);
800
+ this.host = host;
801
+ this.owner = owner;
802
+ this.project = project;
803
+ this.edge = edge;
804
+ this.name = "EdgeHostClaimed";
805
+ }
806
+ };
807
+ /**
808
+ * Every other stack's claims on the edge, by project: each `dynamic/*.yml`
809
+ * but this project's, read through the engine. None where the edge is not
810
+ * running — nothing is routed through it.
811
+ */
812
+ async function edgeClaimsByProject(docker, edge, project) {
813
+ const claimed = /* @__PURE__ */ new Map();
814
+ const listed = await docker.exec(edge, EDGE_SERVICE, [
815
+ "ls",
816
+ "-1",
817
+ EDGE_DYNAMIC_DIR
818
+ ]);
819
+ if (listed.code !== 0) return claimed;
820
+ for (const name of listed.stdout.split("\n").map((line) => line.trim())) {
821
+ if (!name.endsWith(".yml")) continue;
822
+ const owner = name.slice(0, -4);
823
+ if (!owner || owner === project) continue;
824
+ const file = await readEdgeFile(docker, edge, routesPath(owner));
825
+ if (file === void 0) continue;
826
+ const claims = readEdgeClaims(file);
827
+ if (claims.length > 0) claimed.set(owner, claims);
828
+ }
829
+ return claimed;
830
+ }
831
+ /**
832
+ * Refuse a stack's routes when a host in them is routed through the edge by
833
+ * another project — checked before a deploy changes anything, and again
834
+ * before its routes are written. Its own file, from its last deploy, is
835
+ * replaced, never a clash.
836
+ */
837
+ async function assertEdgeHostsFree(docker, edge, project, routes) {
838
+ const mine = readEdgeClaims(routes);
839
+ if (mine.length === 0) return;
840
+ const others = await edgeClaimsByProject(docker, edge, project);
841
+ for (const [owner, theirs] of [...others].sort(([a], [b]) => a.localeCompare(b))) for (const claim of mine) for (const other of theirs) {
842
+ const host = claimsOverlap(claim, other);
843
+ if (host !== void 0) throw new EdgeHostClaimed(host, owner, project, edge.project);
844
+ }
845
+ }
846
+ function certificatePaths(project) {
847
+ return [`${EDGE_CERTS_DIR}/${project}.crt`, `${EDGE_CERTS_DIR}/${project}.key`];
848
+ }
849
+ /**
850
+ * Register a stack with the edge — refused, before anything is written, when
851
+ * another project routes one of its hosts: its certificate first, where it has one,
852
+ * so the routes that need it never load without it — then its routes, in one
853
+ * rename the edge picks up whole. The certificate — the stage's own, or the
854
+ * one gkm's local CA issued the local stage — is PEM read on this machine and
855
+ * written through the engine. The paths written, inside the edge.
856
+ */
857
+ async function writeEdgeRoutes(docker, edge, project, routes, certificate) {
858
+ await assertEdgeHostsFree(docker, edge, project, routes);
859
+ const written = [];
860
+ const [crt, key] = certificatePaths(project);
861
+ if (certificate) {
862
+ await put(docker, edge, crt, certificate.cert);
863
+ await put(docker, edge, key, certificate.key, "600");
864
+ written.push(crt, key);
865
+ } else await remove(docker, edge, [crt, key]);
866
+ await put(docker, edge, routesPath(project), routes);
867
+ written.push(routesPath(project));
868
+ return written;
869
+ }
870
+ /** What a stack's file holds for the moment between emptying and removal. */
871
+ const UNREGISTERED = "# Unregistered by gkm compose --down.\n";
872
+ /**
873
+ * Unregister a stack: its routes, then its certificate. Whether there was
874
+ * anything to remove — false, too, when the edge is not running.
875
+ */
876
+ async function removeEdgeRoutes(docker, edge, project) {
877
+ const file = routesPath(project);
878
+ if ((await docker.exec(edge, "traefik", [
879
+ "test",
880
+ "-f",
881
+ file
882
+ ])).code !== 0) return false;
883
+ await put(docker, edge, file, UNREGISTERED);
884
+ await remove(docker, edge, [file, ...certificatePaths(project)]);
885
+ return true;
886
+ }
887
+ //#endregion
888
+ //#region src/compose/acmeDns.ts
889
+ /**
890
+ * The key the server's shared Traefik edge answers Let's Encrypt's DNS
891
+ * challenge with, for a stage with a wildcard site.
892
+ *
893
+ * A wildcard certificate is issued only over the DNS challenge: the edge
894
+ * proves it holds `<domain>` by writing a TXT record at
895
+ * `_acme-challenge.<domain>`. So it needs a key that can write that record —
896
+ * and nothing else, because it lives on the server.
897
+ *
898
+ * - **The user**: one per server, under `/gkm/`, tagged with the server it
899
+ * is for. One that exists without that tag is someone else's, and is
900
+ * refused rather than taken over.
901
+ * - **Its policies**: one per stack on the server, named for its compose
902
+ * project, allowing `ChangeResourceRecordSets` on the stage's hosted zone
903
+ * for TXT records named `_acme-challenge.<domain>` alone — plus what
904
+ * finding the zone and waiting for the change take, which read and write
905
+ * nothing. Put back on every deploy.
906
+ * - **Its key**: written into the edge's container, never into the stage's
907
+ * secrets or onto the machine running the deploy. Issued when the edge
908
+ * has none, or holds one IAM no longer knows — and then the edge is
909
+ * restarted, since it keeps the key it first read.
910
+ */
911
+ /** Where gkm's IAM users live. */
912
+ const USER_PATH = "/gkm/";
913
+ /** The tag a server's edge user is found by. */
914
+ const TAG_EDGE = "gkm:edge";
915
+ /** The edge user's name for a server: stable, and safe for IAM. */
916
+ function edgeAcmeUser(server) {
917
+ return `gkm-edge-acme-${createHash("sha256").update(server).digest("hex").slice(0, 12)}`;
918
+ }
919
+ /**
920
+ * What one stage's wildcard certificate needs: its challenge record written
921
+ * in its zone, the change waited on, and the zone found by name — which is
922
+ * how the edge's ACME client finds it.
923
+ */
924
+ function acmeDnsPolicy(zoneId, domain) {
925
+ return JSON.stringify({
926
+ Version: "2012-10-17",
927
+ Statement: [
928
+ {
929
+ Effect: "Allow",
930
+ Action: "route53:ChangeResourceRecordSets",
931
+ Resource: `arn:aws:route53:::hostedzone/${zoneId}`,
932
+ Condition: { "ForAllValues:StringEquals": {
933
+ "route53:ChangeResourceRecordSetsNormalizedRecordNames": [`_acme-challenge.${domain.toLowerCase()}`],
934
+ "route53:ChangeResourceRecordSetsRecordTypes": ["TXT"]
935
+ } }
936
+ },
937
+ {
938
+ Effect: "Allow",
939
+ Action: "route53:ListResourceRecordSets",
940
+ Resource: `arn:aws:route53:::hostedzone/${zoneId}`
941
+ },
942
+ {
943
+ Effect: "Allow",
944
+ Action: "route53:GetChange",
945
+ Resource: "arn:aws:route53:::change/*"
946
+ },
947
+ {
948
+ Effect: "Allow",
949
+ Action: "route53:ListHostedZonesByName",
950
+ Resource: "*"
951
+ }
952
+ ]
953
+ });
954
+ }
955
+ /** An IAM user by the edge user's name that gkm did not create for this server. */
956
+ var EdgeAcmeUserNotOwned = class extends GkmError {
957
+ user;
958
+ server;
959
+ constructor(user, server) {
960
+ super(`The IAM user ${user} exists but is not tagged ${TAG_EDGE}=${server}, so gkm will not take it over to write the shared edge's DNS challenge records. Delete it, or tag it ${TAG_EDGE}=${server} if it is the one gkm created for this server.`);
961
+ this.user = user;
962
+ this.server = server;
963
+ this.name = "EdgeAcmeUserNotOwned";
964
+ }
965
+ };
966
+ /** A wildcard site's domain with no Route 53 hosted zone to answer it in. */
967
+ var AcmeHostedZoneMissing = class extends GkmError {
968
+ domain;
969
+ constructor(domain) {
970
+ super(`No Route 53 hosted zone holds ${domain}, so the shared edge cannot answer the DNS challenge for its wildcard certificate. Create the zone, or set hostedZoneId in its dns entry.`);
971
+ this.domain = domain;
972
+ this.name = "AcmeHostedZoneMissing";
973
+ }
974
+ };
975
+ /**
976
+ * The edge's key for `domain`'s challenge record: its user, this stack's
977
+ * policy on it, and a key in the edge that IAM knows.
978
+ *
979
+ * @throws {EdgeAcmeUserNotOwned} when the user is someone else's
980
+ * @throws {AcmeHostedZoneMissing} when no hosted zone holds the domain
981
+ */
982
+ async function ensureEdgeAcmeKey(docker, options) {
983
+ const { clients, edge, server, project, domain } = options;
984
+ const log = options.log ?? ((line) => console.log(line));
985
+ const IAM = await import("@aws-sdk/client-iam");
986
+ const user = edgeAcmeUser(server);
987
+ const zoneId = options.hostedZoneId ?? await hostedZoneId(clients.route53, options.zone);
988
+ if (!zoneId) throw new AcmeHostedZoneMissing(domain);
989
+ if (options.dryRun) {
990
+ log(`🔑 Would let ${EDGE_PROJECT} write _acme-challenge.${domain} TXT records in ${zoneId}, as IAM user ${user} (dry run — nothing is written)`);
991
+ return {
992
+ user,
993
+ zoneId,
994
+ issued: false
995
+ };
996
+ }
997
+ if (!(await clients.iam.send(new IAM.GetUserCommand({ UserName: user })).catch((error) => {
998
+ if (error?.name === "NoSuchEntity" || error?.name === "NoSuchEntityException") return;
999
+ throw error;
1000
+ }))?.User) await clients.iam.send(new IAM.CreateUserCommand({
1001
+ UserName: user,
1002
+ Path: USER_PATH,
1003
+ Tags: [{
1004
+ Key: TAG_EDGE,
1005
+ Value: server
1006
+ }]
1007
+ }));
1008
+ else {
1009
+ const { Tags = [] } = await clients.iam.send(new IAM.ListUserTagsCommand({ UserName: user }));
1010
+ if (!Tags.some((t) => t.Key === "gkm:edge" && t.Value === server)) throw new EdgeAcmeUserNotOwned(user, server);
1011
+ }
1012
+ await clients.iam.send(new IAM.PutUserPolicyCommand({
1013
+ UserName: user,
1014
+ PolicyName: project,
1015
+ PolicyDocument: acmeDnsPolicy(zoneId, domain)
1016
+ }));
1017
+ log(`🔑 ${EDGE_PROJECT} may write _acme-challenge.${domain} TXT records in ${zoneId}, and nothing else there, as IAM user ${user}`);
1018
+ const written = await readEdgeFile(docker, edge, EDGE_AWS_CREDENTIALS);
1019
+ const held = written?.match(/aws_access_key_id\s*=\s*(\S+)/)?.[1];
1020
+ const { AccessKeyMetadata = [] } = await clients.iam.send(new IAM.ListAccessKeysCommand({ UserName: user }));
1021
+ if (held && AccessKeyMetadata.some((k) => k.AccessKeyId === held)) return {
1022
+ user,
1023
+ zoneId,
1024
+ issued: false
1025
+ };
1026
+ for (const key of AccessKeyMetadata) {
1027
+ if (!key.AccessKeyId) continue;
1028
+ await clients.iam.send(new IAM.DeleteAccessKeyCommand({
1029
+ UserName: user,
1030
+ AccessKeyId: key.AccessKeyId
1031
+ }));
1032
+ }
1033
+ const { AccessKey } = await clients.iam.send(new IAM.CreateAccessKeyCommand({ UserName: user }));
1034
+ await writeEdgeSecret(docker, edge, EDGE_AWS_CREDENTIALS, `# Written by gkm compose — the key ${EDGE_PROJECT} answers DNS challenges with.
1035
+ [default]
1036
+ aws_access_key_id = ${AccessKey?.AccessKeyId}
1037
+ aws_secret_access_key = ${AccessKey?.SecretAccessKey}
1038
+ `);
1039
+ log(`🔑 Wrote a new key for ${user} to ${EDGE_PROJECT} (${EDGE_AWS_CREDENTIALS})`);
1040
+ if (written !== void 0) await restartEdge(docker, edge);
1041
+ return {
1042
+ user,
1043
+ zoneId,
1044
+ issued: true
1045
+ };
1046
+ }
1047
+ /** The id of the hosted zone named exactly `zone`, if there is one. */
1048
+ async function hostedZoneId(route53, zone) {
1049
+ const { ListHostedZonesByNameCommand } = await import("@aws-sdk/client-route-53");
1050
+ const name = zone.endsWith(".") ? zone : `${zone}.`;
1051
+ const { HostedZones = [] } = await route53.send(new ListHostedZonesByNameCommand({
1052
+ DNSName: name,
1053
+ MaxItems: 1
1054
+ }));
1055
+ return HostedZones.find((z) => z.Name === name)?.Id?.replace("/hostedzone/", "");
1056
+ }
1057
+ //#endregion
1058
+ //#region src/compose/secrets.ts
1059
+ function deployedStackSecrets(workspace, stage, stored, manifest) {
1060
+ const withSeed = withGeneratedSecrets(stored ?? initStageSecrets(stage), manifest);
1061
+ const withLogs = resolveStageTelemetry({
1062
+ ...workspace.deploy?.telemetry ? { telemetry: workspace.deploy.telemetry } : {},
1063
+ stage,
1064
+ local: false,
1065
+ target: "compose",
1066
+ runtime: "server",
1067
+ selfHosted: true,
1068
+ used: usesTelemetry(manifest)
1069
+ })?.provider === "self-hosted" ? withLogsPassword(withSeed.secrets) : {
1070
+ secrets: withSeed.secrets,
1071
+ generated: []
1072
+ };
1073
+ const withRedis = runsRedis(manifest, withLogs.secrets.custom ?? {}) ? withRedisPassword(withLogs.secrets) : {
1074
+ secrets: withLogs.secrets,
1075
+ generated: []
1076
+ };
1077
+ return {
1078
+ secrets: withRedis.secrets,
1079
+ generated: [
1080
+ ...withSeed.generated,
1081
+ ...withLogs.generated,
1082
+ ...withRedis.generated
1083
+ ]
1084
+ };
1085
+ }
1086
+ //#endregion
1087
+ //#region src/target/compose/health.ts
1088
+ /**
1089
+ * Asking each app of a stack whether it answers — through Caddy, over HTTPS,
1090
+ * the way a browser reaches it.
1091
+ *
1092
+ * `docker compose up --wait` already knows each container passed its own
1093
+ * health check, asked on 127.0.0.1 inside it. What that cannot say is whether
1094
+ * the stack is reachable: whether the edge routes the host to the app, and
1095
+ * whether the certificate it presents is one a client accepts. So the check
1096
+ * goes through the edge, by hostname, and verifies the certificate.
1097
+ */
1098
+ /** A request that got no answer in time. */
1099
+ var HealthRequestTimedOut = class extends GkmError {
1100
+ url;
1101
+ timeoutMs;
1102
+ constructor(url, timeoutMs) {
1103
+ super(`${url} did not answer within ${timeoutMs}ms. Check the stack is up with \`docker compose ps\`.`);
1104
+ this.url = url;
1105
+ this.timeoutMs = timeoutMs;
1106
+ this.name = "HealthRequestTimedOut";
1107
+ }
1108
+ };
1109
+ /** The most of a body read — a sitemap may be 50 MB, and none needs to be. */
1110
+ const EDGE_BODY_LIMIT = 10485760;
1111
+ /** A body larger than {@link EDGE_BODY_LIMIT}. */
1112
+ var EdgeResponseTooLarge = class extends GkmError {
1113
+ url;
1114
+ limit;
1115
+ constructor(url, limit) {
1116
+ super(`${url} answered with more than ${limit} bytes, which is more than gkm reads. Split it — a sitemap index can list several sitemaps.`);
1117
+ this.url = url;
1118
+ this.limit = limit;
1119
+ this.name = "EdgeResponseTooLarge";
1120
+ }
1121
+ };
1122
+ /**
1123
+ * The real request: one GET to the edge — over HTTPS, the certificate
1124
+ * verified, for an `https:` URL, and plain HTTP for an `http:` one — with
1125
+ * the URL's host as SNI and `Host` wherever it connects.
1126
+ */
1127
+ const edgeFetch = ({ url, ca, connectTo, connectPort, authorization, timeoutMs, signal, read, headers }) => {
1128
+ const target = new URL(url);
1129
+ const secure = target.protocol === "https:";
1130
+ const request$2 = secure ? request$1 : request;
1131
+ return new Promise((resolve, reject) => {
1132
+ const req = request$2({
1133
+ host: connectTo ?? target.hostname,
1134
+ port: connectPort ?? (target.port || (secure ? 443 : 80)),
1135
+ ...secure ? { servername: target.hostname } : {},
1136
+ path: `${target.pathname}${target.search}`,
1137
+ method: "GET",
1138
+ headers: {
1139
+ ...headers,
1140
+ host: target.host,
1141
+ ...authorization ? { authorization } : {}
1142
+ },
1143
+ ...secure && ca ? { ca } : {},
1144
+ ...signal ? { signal } : {},
1145
+ timeout: timeoutMs
1146
+ }, (res) => {
1147
+ const answered = {
1148
+ status: res.statusCode ?? 0,
1149
+ headers: res.headers
1150
+ };
1151
+ if (!read) {
1152
+ res.resume();
1153
+ res.on("end", () => resolve({
1154
+ ...answered,
1155
+ body: ""
1156
+ }));
1157
+ res.on("error", reject);
1158
+ return;
1159
+ }
1160
+ const chunks = [];
1161
+ let size = 0;
1162
+ res.on("data", (chunk) => {
1163
+ size += chunk.length;
1164
+ if (size > 10485760) {
1165
+ req.destroy(new EdgeResponseTooLarge(url, EDGE_BODY_LIMIT));
1166
+ return;
1167
+ }
1168
+ chunks.push(chunk);
1169
+ });
1170
+ res.on("end", () => resolve({
1171
+ ...answered,
1172
+ body: Buffer.concat(chunks).toString("utf-8")
1173
+ }));
1174
+ res.on("error", reject);
1175
+ });
1176
+ req.on("timeout", () => req.destroy(new HealthRequestTimedOut(url, timeoutMs)));
1177
+ req.on("error", reject);
1178
+ req.end();
1179
+ });
1180
+ };
1181
+ /** The real probe: one GET through the edge, answered by its status. */
1182
+ const httpsProbe = (request) => edgeFetch(request).then(({ status }) => status);
1183
+ /** Whether a status means the app answered as it should. */
1184
+ function isHealthy(status) {
1185
+ return status >= 200 && status < 400;
1186
+ }
1187
+ /**
1188
+ * Apps that are not healthy. `via` says which check said so:
1189
+ * - `edge`: verify asked each app through the edge, by its host, and it did
1190
+ * not answer as it should (a worker, with no route, by its container's
1191
+ * health);
1192
+ * - `docker`: `up --wait` failed, and these containers' own health checks
1193
+ * had not passed — the rest of the stack was released and routed first.
1194
+ * One class either way, so a caller matches one failure: apps not healthy.
1195
+ */
1196
+ var ComposeAppsUnhealthy = class extends GkmError {
1197
+ project;
1198
+ apps;
1199
+ proxy;
1200
+ via;
1201
+ constructor(project, apps, proxy = "caddy", via = "edge") {
1202
+ const names = apps.map((a) => a.app).join(" ");
1203
+ super(via === "docker" ? `${project} started, but ${apps.length === 1 ? "this container's health check has" : "these containers' health checks have"} not passed. The rest of the stack is up and routed:\n` + apps.map(({ app, last }) => ` - ${app}: Docker reports it ${last}`).join("\n") + `\nSee why with \`docker compose -p ${project} ps ${names}\` and \`docker compose -p ${project} logs ${names}\`, fix the app, and deploy again.` : `${project} started, but ${apps.length === 1 ? "this app does" : "these apps do"} not answer through ${proxy === "caddy" ? "Caddy" : "the shared Traefik edge"}:\n` + apps.map(({ url, last }) => ` - ${url}: ${last}`).join("\n") + (proxy === "caddy" ? `\nSee why with \`docker compose -p ${project} logs caddy ${names}\`.` : `\nSee why with \`docker compose -p ${EDGE_PROJECT} logs traefik\` and \`docker compose -p ${project} logs ${names}\`.`));
1204
+ this.project = project;
1205
+ this.apps = apps;
1206
+ this.proxy = proxy;
1207
+ this.via = via;
1208
+ this.name = "ComposeAppsUnhealthy";
1209
+ }
1210
+ };
1211
+ //#endregion
1212
+ //#region src/target/compose/topicSubscriptions.ts
1213
+ /**
1214
+ * Every topic subscriber the stack pushes to: a subscriber whose connection
1215
+ * key is a topic's publisher string — a bucket's events key is not — and
1216
+ * whose value on this stage is an SNS one.
1217
+ */
1218
+ function topicPushes(stack, manifest) {
1219
+ const topicKeys = new Set(Object.values(manifest).filter((declaration) => declaration.kind === "topic").map((topic) => provideKey(topic.id, "publisherConnectionString")));
1220
+ const pushes = [];
1221
+ for (const worker of stack.workers) for (const subscriber of worker.push?.subscribers ?? []) {
1222
+ if (!topicKeys.has(subscriber.connectionKey)) continue;
1223
+ const connectionString = worker.env?.[subscriber.connectionKey];
1224
+ if (!connectionString?.startsWith("sns:")) continue;
1225
+ pushes.push({
1226
+ worker: worker.id,
1227
+ name: subscriber.name,
1228
+ endpoint: subscriber.endpoint,
1229
+ connectionKey: subscriber.connectionKey,
1230
+ connectionString,
1231
+ ...subscriber.events ? { events: subscriber.events } : {}
1232
+ });
1233
+ }
1234
+ return pushes;
1235
+ }
1236
+ /** The subscriptions recorded for hand-set topics: those with a key. */
1237
+ function recordedTopicSubscriptions(resources) {
1238
+ return Object.values(resources ?? {}).filter((record) => record.type === "sns-subscription" && typeof record.data?.connectionKey === "string");
1239
+ }
1240
+ /**
1241
+ * Converge each hand-set topic's subscriptions on the subscribers pushed to:
1242
+ * subscribe, bring filters up to date, unsubscribe a route on the worker's
1243
+ * host no subscriber wants any more — and record each ARN, forgetting what
1244
+ * was removed.
1245
+ *
1246
+ * @returns each worker's subscriptions, by export — what it is handed
1247
+ */
1248
+ async function ensureTopicSubscriptions(input) {
1249
+ const { snsUrl, reconcileHttpSubscriptions } = await import("@geekmidas/events/sns");
1250
+ const groups = /* @__PURE__ */ new Map();
1251
+ const group = (topicArn, host, connectionKey) => {
1252
+ const id = `${topicArn}|${host}`;
1253
+ const found = groups.get(id);
1254
+ if (found) return found;
1255
+ const created = {
1256
+ topicArn,
1257
+ host,
1258
+ connectionKey,
1259
+ pushes: [],
1260
+ recorded: []
1261
+ };
1262
+ groups.set(id, created);
1263
+ return created;
1264
+ };
1265
+ for (const push of input.pushes) {
1266
+ const { topicArn } = snsUrl.parse(push.connectionString);
1267
+ const into = group(topicArn, new URL(push.endpoint).host, push.connectionKey);
1268
+ into.connectionString = push.connectionString;
1269
+ into.pushes.push(push);
1270
+ }
1271
+ for (const record of recordedTopicSubscriptions(input.recorded)) group(String(record.data?.topicArn), new URL(String(record.data?.endpoint)).host, String(record.data?.connectionKey)).recorded.push(record);
1272
+ const wanted = new Set(input.pushes.map(({ worker, name }) => subscriptionKey(worker, name)));
1273
+ const arns = {};
1274
+ for (const { topicArn, host, connectionKey, ...members } of groups.values()) {
1275
+ const stale = members.recorded.filter((r) => !wanted.has(r.key));
1276
+ const named = members.connectionString ?? input.secrets[connectionKey];
1277
+ if (!named?.startsWith("sns:")) {
1278
+ for (const record of stale) {
1279
+ input.log(` – forgot ${record.data?.endpoint}: '${connectionKey}' no longer names an SNS topic to unsubscribe it from`);
1280
+ await input.forget(record.key);
1281
+ }
1282
+ continue;
1283
+ }
1284
+ const address = new URL(named);
1285
+ address.searchParams.set("topicArn", topicArn);
1286
+ const connection = await topicConnection({
1287
+ connectionKey,
1288
+ connectionString: address.toString(),
1289
+ stage: input.stage,
1290
+ env: input.env
1291
+ });
1292
+ const result = await reconcileHttpSubscriptions(connection, {
1293
+ endpoints: members.pushes.map((push) => ({
1294
+ name: push.name,
1295
+ endpoint: push.endpoint,
1296
+ ...push.events ? { events: push.events } : {}
1297
+ })),
1298
+ ownedHost: host
1299
+ });
1300
+ await connection.close();
1301
+ for (const push of members.pushes) {
1302
+ const arn = result.subscriptions[push.name];
1303
+ const key = subscriptionKey(push.worker, push.name);
1304
+ if (input.recorded?.[key]?.id !== arn) input.log(` ✓ '${push.name}' subscribed to ${topicArn.split(":").pop()} at ${push.endpoint}`);
1305
+ await input.record(subscriptionResource({
1306
+ worker: push.worker,
1307
+ name: push.name,
1308
+ topicArn,
1309
+ endpoint: push.endpoint,
1310
+ connectionKey: push.connectionKey,
1311
+ pending: result.pending.includes(push.name),
1312
+ ...push.events ? { events: push.events } : {}
1313
+ }, arn));
1314
+ arns[push.worker] = {
1315
+ ...arns[push.worker],
1316
+ [push.name]: arn
1317
+ };
1318
+ }
1319
+ for (const arn of result.removed) input.log(` – unsubscribed ${arn}: no subscriber wants it any more`);
1320
+ for (const record of stale) await input.forget(record.key);
1321
+ }
1322
+ return arns;
1323
+ }
1324
+ /**
1325
+ * How long one page has to answer: a first render fetches from the site's
1326
+ * APIs, so it is given longer than a health check is.
1327
+ */
1328
+ const WARMUP_TIMEOUT_MS = 3e4;
1329
+ /**
1330
+ * Next's on-demand revalidation header: a request carrying the build's
1331
+ * preview-mode id is rendered afresh whatever the page's cache says.
1332
+ */
1333
+ const NEXT_REVALIDATE_HEADER = "x-prerender-revalidate";
1334
+ /**
1335
+ * What Next answered a request from: `HIT` and `STALE` are a cached copy —
1336
+ * on a fresh deploy, the build's — and the request rendered nothing.
1337
+ */
1338
+ const NEXT_CACHE_HEADER = "x-nextjs-cache";
1339
+ const FROM_CACHE = /* @__PURE__ */ new Set(["HIT", "STALE"]);
1340
+ /**
1341
+ * Asks the site for its `/sitemap.xml` — a sitemap index's sitemaps followed
1342
+ * one level — then for `/` and every page it lists, up to `maxUrls` pages in
1343
+ * all, `concurrency` at a time. The sitemap comes first so that, rendered
1344
+ * again with `headers`, it lists what the deployed APIs hold. Only pages on
1345
+ * the site's own origin are asked: a sitemap may list another's, which this
1346
+ * deploy did not start.
1347
+ */
1348
+ async function warmSite(options) {
1349
+ const { fetch, edge = {}, maxUrls = 500, concurrency = 4, timeoutMs = WARMUP_TIMEOUT_MS, headers } = options;
1350
+ const origin = new URL(options.url).origin;
1351
+ const result = {
1352
+ warmed: [],
1353
+ cached: [],
1354
+ warnings: [],
1355
+ leftOut: 0
1356
+ };
1357
+ /** One GET; a failure is a warning, and `undefined`. */
1358
+ const get = async (url, read) => {
1359
+ try {
1360
+ const response = await fetch({
1361
+ url,
1362
+ ...edge,
1363
+ ...headers ? { headers } : {},
1364
+ timeoutMs,
1365
+ read
1366
+ });
1367
+ const cache = response.headers?.[NEXT_CACHE_HEADER];
1368
+ if (typeof cache === "string" && FROM_CACHE.has(cache.toUpperCase())) result.cached.push(url);
1369
+ return response;
1370
+ } catch (error) {
1371
+ if (edge.signal?.aborted) throw error;
1372
+ result.warnings.push({
1373
+ url,
1374
+ reason: error instanceof HealthRequestTimedOut ? `no answer within ${error.timeoutMs}ms` : error instanceof Error ? error.message : String(error)
1375
+ });
1376
+ return;
1377
+ }
1378
+ };
1379
+ const warm = async (url) => {
1380
+ const response = await get(url, false);
1381
+ if (!response) return;
1382
+ if (isHealthy(response.status)) result.warmed.push(url);
1383
+ else result.warnings.push({
1384
+ url,
1385
+ reason: `HTTP ${response.status}`
1386
+ });
1387
+ };
1388
+ /** A sitemap's `<loc>`s, or none where it has none — or did not answer. */
1389
+ const sitemap = async (url) => {
1390
+ const response = await get(url, true);
1391
+ if (!response) return void 0;
1392
+ if (response.status >= 400 && response.status < 500) return void 0;
1393
+ if (!isHealthy(response.status)) {
1394
+ result.warnings.push({
1395
+ url,
1396
+ reason: `HTTP ${response.status}`
1397
+ });
1398
+ return;
1399
+ }
1400
+ return readSitemap(response.body);
1401
+ };
1402
+ const root = `${origin}/`;
1403
+ const pages = /* @__PURE__ */ new Set([root]);
1404
+ const sameOrigin = (loc) => {
1405
+ if (!URL.canParse(loc, root)) return void 0;
1406
+ const url = new URL(loc, root);
1407
+ url.hash = "";
1408
+ return url.origin === origin ? url.href : void 0;
1409
+ };
1410
+ const list = (locs) => {
1411
+ for (const loc of locs) {
1412
+ const url = sameOrigin(loc);
1413
+ if (url) pages.add(url);
1414
+ }
1415
+ };
1416
+ const top = await sitemap(`${origin}/sitemap.xml`);
1417
+ if (top?.kind === "index") {
1418
+ const read = await pool([...new Set(top.locs.map(sameOrigin).filter((u) => u !== void 0))], concurrency, sitemap);
1419
+ for (const child of read) if (child?.kind === "urlset") list(child.locs);
1420
+ } else if (top) list(top.locs);
1421
+ await warm(root);
1422
+ const listed = [...pages].slice(1);
1423
+ const room = Math.max(0, maxUrls - 1);
1424
+ result.leftOut = Math.max(0, listed.length - room);
1425
+ await pool(listed.slice(0, room), concurrency, warm);
1426
+ return result;
1427
+ }
1428
+ /**
1429
+ * Prints the preview-mode id of the prerender manifest at `argv[1]`, and
1430
+ * nothing else of it: the manifest's signing and encryption keys never leave
1431
+ * the container. Exit 3 when the manifest has no id.
1432
+ */
1433
+ const PRINT_PREVIEW_MODE_ID = [
1434
+ "let m;",
1435
+ "try { m = JSON.parse(require('fs').readFileSync(process.argv[1], 'utf8')); }",
1436
+ "catch (e) { process.stderr.write(String(e && e.message)); process.exit(2); }",
1437
+ "const id = m && m.preview && m.preview.previewModeId;",
1438
+ "if (typeof id !== 'string' || id === '') process.exit(3);",
1439
+ "process.stdout.write(id);"
1440
+ ].join(" ");
1441
+ /**
1442
+ * The preview-mode id of a running Next site, from the prerender manifest
1443
+ * of its image — `manifest`, a path inside the container — by running the
1444
+ * image's own `node` in it through `exec` (`docker exec`, on whichever
1445
+ * engine runs the stack). Never read from this machine's disk: the image
1446
+ * that runs is the one whose id counts.
1447
+ *
1448
+ * The id is a secret of the site's: it is returned to the warm-up and
1449
+ * nowhere else — never logged, never written.
1450
+ */
1451
+ async function nextPreviewModeId(exec, manifest) {
1452
+ const { code, stdout, stderr } = await exec([
1453
+ "node",
1454
+ "-e",
1455
+ PRINT_PREVIEW_MODE_ID,
1456
+ manifest
1457
+ ]);
1458
+ if (code === 0 && stdout) return { id: stdout };
1459
+ if (code === 3 || code === 0) return { reason: `${manifest} has no preview.previewModeId, which this version of Next may not write` };
1460
+ const said = stderr.trim().split("\n")[0];
1461
+ if (code === null) return { reason: said || "its container is not running" };
1462
+ return { reason: `could not read ${manifest} in its container (${said || `exit ${code}`})` };
1463
+ }
1464
+ /**
1465
+ * Reads a sitemap (sitemaps.org): each entry's `<loc>`, entity-decoded or
1466
+ * from CDATA. Only an unprefixed `<loc>` is an entry's — an `<image:loc>` is
1467
+ * an image on a page, not a page.
1468
+ */
1469
+ function readSitemap(xml) {
1470
+ const kind = /<(?:[\w-]+:)?sitemapindex[\s>]/.test(xml) ? "index" : "urlset";
1471
+ const locs = [];
1472
+ for (const match of xml.matchAll(/<loc>\s*(?:<!\[CDATA\[([\s\S]*?)\]\]>|([\s\S]*?))\s*<\/loc>/g)) {
1473
+ const loc = match[1] ?? decodeEntities(match[2] ?? "");
1474
+ if (loc.trim()) locs.push(loc.trim());
1475
+ }
1476
+ return {
1477
+ kind,
1478
+ locs
1479
+ };
1480
+ }
1481
+ const ENTITIES = {
1482
+ amp: "&",
1483
+ lt: "<",
1484
+ gt: ">",
1485
+ quot: "\"",
1486
+ apos: "'"
1487
+ };
1488
+ function decodeEntities(text) {
1489
+ return text.replace(/&(#x[\da-f]+|#\d+|amp|lt|gt|quot|apos);/gi, (whole, entity) => {
1490
+ if (entity[0] === "#") {
1491
+ const code = entity[1] === "x" || entity[1] === "X" ? Number.parseInt(entity.slice(2), 16) : Number.parseInt(entity.slice(1), 10);
1492
+ return Number.isFinite(code) ? String.fromCodePoint(code) : whole;
1493
+ }
1494
+ return ENTITIES[entity.toLowerCase()] ?? whole;
1495
+ });
1496
+ }
1497
+ /** Each item through `run`, at most `size` at once, results in order. */
1498
+ async function pool(items, size, run) {
1499
+ const results = new Array(items.length);
1500
+ let next = 0;
1501
+ const worker = async () => {
1502
+ while (next < items.length) {
1503
+ const index = next++;
1504
+ results[index] = await run(items[index]);
1505
+ }
1506
+ };
1507
+ await Promise.all(Array.from({ length: Math.min(size, items.length) }, worker));
1508
+ return results;
1509
+ }
1510
+ //#endregion
1511
+ //#region src/target/compose/phases.ts
1512
+ /**
1513
+ * The compose target's phases: a stage as one Docker Compose stack behind
1514
+ * Caddy — the local stage on this machine's Docker, a deployed stage on its
1515
+ * server's, driven over SSH (`compose/server.ts`) from wherever the deploy
1516
+ * runs.
1517
+ *
1518
+ * The order is the point of this file, and the rules it keeps are these:
1519
+ *
1520
+ * - A tag is checked before anything happens (`validate`). Every app's image
1521
+ * is asked of the registry first; one missing image stops the run naming
1522
+ * them all, and nothing is written, pulled or started.
1523
+ * - The databases exist before the apps do (`provision`). Postgres comes up
1524
+ * alone, its databases, roles and grants are created, its migrations
1525
+ * applied and its seeds run from this machine — through an SSH tunnel to a
1526
+ * server's loopback — and only then do the apps
1527
+ * start — so an app
1528
+ * never boots against a schema that is not there yet.
1529
+ * - What a stage runs is recorded (`release`). Each app's image, the tag it
1530
+ * was released under and the digest it resolved to are written to the
1531
+ * stage's state, so "what is this stage running" has an exact answer.
1532
+ * - What was started answers (`verify`): each app is asked through Caddy,
1533
+ * over HTTPS, with the certificate verified. Then each server-rendered site
1534
+ * is warmed (`warmup.ts`): a page that fails to warm is said, never failed.
1535
+ *
1536
+ * Every image is built inside Docker from a pruned slice of the build root —
1537
+ * the same Dockerfiles `gkm docker` writes — so nothing is built on this
1538
+ * machine before `docker compose build`.
1539
+ *
1540
+ * Docker, the registry, Postgres and the health probe are
1541
+ * injected, so the rules are asserted without a daemon; the defaults are the
1542
+ * real ones.
1543
+ */
1544
+ /** `--push` without `--build`: there is nothing built here to push. */
1545
+ var ComposePushNeedsBuild = class extends GkmError {
1546
+ constructor() {
1547
+ super("--push pushes the images this checkout builds, so it needs --build (and never --pull): gkm compose --stage <stage> --build --push --tag <tag>.");
1548
+ this.name = "ComposePushNeedsBuild";
1549
+ }
1550
+ };
1551
+ /** Digests to run by, on a run that pulls nothing. */
1552
+ var ComposePinNeedsPull = class extends GkmError {
1553
+ constructor() {
1554
+ super("--digests-file pins the images a release pulls, so it needs --tag (or --pull) — or, with --build --push, it is where the pushed digests are written.");
1555
+ this.name = "ComposePinNeedsPull";
1556
+ }
1557
+ };
1558
+ /** A port variable that does not hold a port. */
1559
+ var EdgePortInvalid = class extends GkmError {
1560
+ variable;
1561
+ value;
1562
+ constructor(variable, value) {
1563
+ super(`${variable}=${value} is not a port. Set it to a number between 1 and 65535, or unset it for the default.`);
1564
+ this.variable = variable;
1565
+ this.value = value;
1566
+ this.name = "EdgePortInvalid";
1567
+ }
1568
+ };
1569
+ /** A build with no commit to tag its images with. */
1570
+ var NoGitRevision = class extends GkmError {
1571
+ root;
1572
+ constructor(root) {
1573
+ super(`Images built here are tagged with the commit they were built from, and ${root} is not in a git repository with a commit. Commit, or pass --tag to name the images yourself.`);
1574
+ this.root = root;
1575
+ this.name = "NoGitRevision";
1576
+ }
1577
+ };
1578
+ /**
1579
+ * The project's own file merged over a stage's stack: `docker-compose.<stage>.yml`
1580
+ * at the workspace root. What the generated file cannot know — a port bound
1581
+ * to a tailnet address, another log driver — goes there, and is never
1582
+ * overwritten.
1583
+ */
1584
+ function stackOverrideFile(root, stage) {
1585
+ return join(root, `docker-compose.${stage}.yml`);
1586
+ }
1587
+ /** The commit HEAD is at, short — and `-dirty` when the tree has changes. */
1588
+ async function gitRevision(root) {
1589
+ let sha;
1590
+ try {
1591
+ sha = (await runOutput("git", [
1592
+ "rev-parse",
1593
+ "--short",
1594
+ "HEAD"
1595
+ ], { cwd: root })).trim();
1596
+ } catch {
1597
+ throw new NoGitRevision(root);
1598
+ }
1599
+ if (!sha) throw new NoGitRevision(root);
1600
+ return (await runOutput("git", ["status", "--porcelain"], { cwd: root }).catch(() => "")).trim() ? `${sha}-dirty` : sha;
1601
+ }
1602
+ /** The edge's ports, from the environment — 443 and 80 when unset. */
1603
+ function edgePorts(env = process.env) {
1604
+ const read = (variable, fallback) => {
1605
+ const value = env[variable];
1606
+ if (value === void 0 || value === "") return fallback;
1607
+ const port = Number(value);
1608
+ if (!Number.isInteger(port) || port < 1 || port > 65535) throw new EdgePortInvalid(variable, value);
1609
+ return port;
1610
+ };
1611
+ const logs = env["GKM_COMPOSE_LOGS_PORT"] ? read(LOGS_PORT_ENV, 0) : void 0;
1612
+ return {
1613
+ https: read(EDGE_PORT_ENV.https, 443),
1614
+ http: read(EDGE_PORT_ENV.http, 80),
1615
+ ...logs ? { logs } : {}
1616
+ };
1617
+ }
1618
+ /**
1619
+ * The real ones. Each app is asked for three minutes before it counts as
1620
+ * down: a deployed stage's first ACME certificate can take that long.
1621
+ */
1622
+ const defaultDeps = {
1623
+ docker: dockerCompose,
1624
+ revision: gitRevision,
1625
+ sql: pgClient,
1626
+ buckets: bucketClient,
1627
+ logins: ensurePostgresLogin,
1628
+ migrate: migrateDatabases,
1629
+ seed: seedDatabases,
1630
+ probe: httpsProbe,
1631
+ fetch: edgeFetch,
1632
+ healthAttempts: 90,
1633
+ healthIntervalMs: 2e3,
1634
+ env: process.env,
1635
+ lookup: systemLookup,
1636
+ server: sshAccess
1637
+ };
1638
+ /**
1639
+ * The stack the stage would run, and — for a tag — the registry's word that
1640
+ * every image it names exists. Nothing is written: a missing image stops the
1641
+ * run here, before a file, a secret or a container is touched.
1642
+ */
1643
+ async function validateCompose(ctx, deps, options = {}) {
1644
+ const { workspace, stage, identity, tag } = ctx;
1645
+ const root = workspace.root;
1646
+ const mode = options.mode ?? (ctx.tagGiven ? "pull" : "build");
1647
+ const push = options.push === true;
1648
+ if (push && mode !== "build") throw new ComposePushNeedsBuild();
1649
+ if (options.pin && mode !== "pull") throw new ComposePinNeedsPull();
1650
+ const registry = workspace.deploy?.registry;
1651
+ if (!registry && (push || mode === "pull")) throw new RegistryRequired(push ? "push" : "pull", imageRef(identity, ctx.apps[0] ?? workspace.name, void 0, tag));
1652
+ const background = {};
1653
+ const subscribers = {};
1654
+ const manifest = await discover({
1655
+ patterns: constructGlobs(workspace),
1656
+ cwd: root,
1657
+ background,
1658
+ subscribers
1659
+ });
1660
+ const { secrets, generated } = await stageSecrets(ctx, manifest);
1661
+ const recorded = push ? void 0 : await ctx.state.read(stage);
1662
+ const layout = mode === "build" ? imageLayout(workspace) : void 0;
1663
+ const input = {
1664
+ workspace,
1665
+ manifest,
1666
+ background,
1667
+ subscribers,
1668
+ stage,
1669
+ identity,
1670
+ images: {
1671
+ mode,
1672
+ tag,
1673
+ ...registry ? { registry } : {}
1674
+ },
1675
+ secrets,
1676
+ ...stage === workspace.stages.local ? { localCredentials: (await loadLocalCredentials(workspace)).credentials } : {},
1677
+ allowDevServices: ctx.allowDevServices,
1678
+ ports: edgePorts(deps.env),
1679
+ ...layout ? { layout } : {},
1680
+ ...push ? { buildOnly: true } : {}
1681
+ };
1682
+ const recordedArns = recordedPushSubscriptions(recorded?.resources);
1683
+ const planned = push ? void 0 : composeStack({
1684
+ ...input,
1685
+ recordingSubscriptions: true
1686
+ });
1687
+ const pushes = planned ? topicPushes(planned, manifest) : [];
1688
+ const topics = planned && (pushes.length > 0 || recordedTopicSubscriptions(recorded?.resources).length > 0) ? {
1689
+ pushes,
1690
+ recorded: recorded?.resources
1691
+ } : void 0;
1692
+ const composed = topics && planned ? planned : composeStack({
1693
+ ...input,
1694
+ pushSubscriptions: recordedArns
1695
+ });
1696
+ if (mode === "build") assertRedisClient(workspace, composed, manifest);
1697
+ const pinned = (unpinned) => options.pin ? withPinnedImages(unpinned, options.pin.file, options.pin.digests) : unpinned;
1698
+ const stack = pinned(composed);
1699
+ if (mode === "pull" && !ctx.dryRun) await assertImagesExist(deps.docker, tag, [
1700
+ ...stack.apps,
1701
+ ...stack.workers,
1702
+ ...stack.processors
1703
+ ]);
1704
+ if (stack.redis && !stack.local) ctx.secrets.mask(stack.redis.password);
1705
+ if (stack.logs && (!stack.local || stack.logs.passwordFromSecrets)) {
1706
+ ctx.secrets.mask(stack.logs.password);
1707
+ ctx.secrets.mask(stack.logs.appEnv.OTEL_EXPORTER_OTLP_HEADERS);
1708
+ }
1709
+ if (!push && !stack.local) requiredServerAddress(stage, workspace.domains?.[stage], secrets?.custom);
1710
+ const server = push ? void 0 : composeServer({
1711
+ stage,
1712
+ local: stack.local,
1713
+ config: workspace.deploy?.compose?.server,
1714
+ custom: secrets?.custom
1715
+ });
1716
+ const dir = join(root, stackDir(stage));
1717
+ const override = stackOverrideFile(root, stage);
1718
+ const overrides = existsSync(override) ? [override] : [];
1719
+ if (overrides.length > 0) ctx.logger.info(`🧩 Merging docker-compose.${stage}.yml over the generated stack`);
1720
+ const previous = Object.fromEntries(Object.entries(recorded?.state.releases ?? {}).map(([name, releases]) => {
1721
+ const { ref, tag, digest } = releases.current;
1722
+ return [name, {
1723
+ ref,
1724
+ ...tag ? { tag } : {},
1725
+ ...digest ? { digest } : {}
1726
+ }];
1727
+ }));
1728
+ return {
1729
+ mode,
1730
+ push,
1731
+ stack,
1732
+ ...topics ? { topicSubscriptions: {
1733
+ ...topics,
1734
+ restack: (arns) => {
1735
+ const merged = { ...recordedArns };
1736
+ for (const [worker, own] of Object.entries(arns)) merged[worker] = {
1737
+ ...merged[worker],
1738
+ ...own
1739
+ };
1740
+ return pinned(composeStack({
1741
+ ...input,
1742
+ pushSubscriptions: merged
1743
+ }));
1744
+ }
1745
+ } } : {},
1746
+ ...server ? { server } : {},
1747
+ dir,
1748
+ ref: {
1749
+ project: composeProject(identity),
1750
+ file: join(dir, "docker-compose.yml"),
1751
+ ...overrides.length > 0 ? { overrides } : {},
1752
+ cwd: root,
1753
+ ...server ? { host: dockerHost(server) } : {},
1754
+ output: ctx.childOutput,
1755
+ signal: ctx.signal
1756
+ },
1757
+ secrets,
1758
+ generated,
1759
+ previous,
1760
+ files: [],
1761
+ images: {},
1762
+ changes: []
1763
+ };
1764
+ }
1765
+ /**
1766
+ * What only a compose deploy checks before it changes anything, once the
1767
+ * stage's own keys are: the stage's own certificate is there, and every
1768
+ * public host points at the stage's server — its records written where a
1769
+ * provider holds them, then confirmed — before Caddy or Traefik ask Let's
1770
+ * Encrypt for a certificate it could not issue. A build-only run never asks.
1771
+ */
1772
+ async function readyCompose(ctx, run, deps) {
1773
+ const { stack } = run;
1774
+ if (stack.tls) {
1775
+ const { certFile, keyFile, caFile } = stack.tls;
1776
+ for (const file of [
1777
+ certFile,
1778
+ keyFile,
1779
+ ...caFile ? [caFile] : []
1780
+ ]) if (!existsSync(file)) throw new ComposeTlsFileMissing(ctx.stage, file);
1781
+ }
1782
+ if (stack.local) {
1783
+ await edgeHostsFree(ctx, run, deps);
1784
+ return;
1785
+ }
1786
+ if (run.server && !ctx.dryRun && !run.push) await deps.server.check(run.server);
1787
+ await edgeHostsFree(ctx, run, deps);
1788
+ const prune = await stageHostsDns(ctx, deps, stackHosts(stack).filter((host) => !isLocalHost(host)), run.secrets?.custom, stack.wildcard?.host);
1789
+ if (prune) run.pruneDns = prune;
1790
+ }
1791
+ /**
1792
+ * No host the stack routes through the shared edge is routed there by another
1793
+ * project — asked of the edge on the stage's engine before a record, a
1794
+ * secret, an image or a container is touched, so a clash fails the deploy
1795
+ * with nothing changed. Asked again as the routes are written
1796
+ * (`writeEdgeRoutes`), for an edge that was not running yet.
1797
+ */
1798
+ async function edgeHostsFree(ctx, run, deps) {
1799
+ const { stack, ref } = run;
1800
+ if (ctx.dryRun || run.push) return;
1801
+ if (stack.proxy !== "traefik" || !stack.traefik) return;
1802
+ await assertEdgeHostsFree(deps.docker, edgeRef(edgeFile(run), {
1803
+ ...ref.host ? { host: ref.host } : {},
1804
+ ...ref.output ? { output: ref.output } : {}
1805
+ }), stack.project, stack.traefik);
1806
+ }
1807
+ /** A stack whose caches are in Redis, built for an app that has no client. */
1808
+ var RedisClientMissing = class extends GkmError {
1809
+ apps;
1810
+ constructor(apps) {
1811
+ super(`The stack keeps every cache in Redis, and ${apps.join(", ")} ${apps.length === 1 ? "does" : "do"} not depend on ioredis, the client the generated server reaches it with. Add it to the workspace: pnpm add ioredis`);
1812
+ this.apps = apps;
1813
+ this.name = "RedisClientMissing";
1814
+ }
1815
+ };
1816
+ /**
1817
+ * Every backend and worker image whose process reaches a cache can resolve
1818
+ * `ioredis` — listed by its app or a directory above it, the way Node and
1819
+ * the image's install find it. A process that reaches no cache registers no
1820
+ * cache driver, so it needs no client.
1821
+ */
1822
+ function assertRedisClient(workspace, stack, manifest) {
1823
+ if (!stack.plan.resources.some((r) => r.kind === "cache")) return;
1824
+ const cacheKeys = Object.values(manifest).filter((d) => d.kind === "cache").flatMap((d) => d.provides ?? []);
1825
+ const reachesCache = (keys) => cacheKeys.some((key) => keys?.has(key));
1826
+ const missing = [.../* @__PURE__ */ new Set([...stack.apps.filter((app) => (app.kind === "rest-api" || app.kind === "websocket-api") && reachesCache(appEnvKeys(manifest, app.name))).map((a) => a.name), ...stack.workers.filter((worker) => reachesCache(workerEnvKeys(manifest, worker.id))).map((worker) => worker.host)])].filter((name) => {
1827
+ const app = workspace.apps[name];
1828
+ return app && !installedFrom(join(workspace.root, app.path), "ioredis");
1829
+ }).sort();
1830
+ if (missing.length > 0) throw new RedisClientMissing(missing);
1831
+ }
1832
+ /**
1833
+ * The deploy's DNS step, for a deployed stage: its server's address — required
1834
+ * where it serves a real domain — then its own hosts written through each
1835
+ * domain's provider in `dns` where they differ, read back from it, and
1836
+ * recorded in the stage's state; hosts no provider writes resolved with the
1837
+ * system resolver. A dry run prints the plan and writes nothing, and warns
1838
+ * rather than stops. Returns the prune of the records gkm wrote for hosts the
1839
+ * stage no longer serves — which a dry run prints there and then.
1840
+ *
1841
+ * @throws {ServerAddressMissing} when the stage has a domain and no address
1842
+ * @throws {ServerAddressInvalid} for a value that is not an address
1843
+ * @throws {HostNotPointingAtServer} when a host is not the server
1844
+ */
1845
+ async function stageHostsDns(ctx, deps, hosts, custom, wildcard) {
1846
+ const { workspace, stage } = ctx;
1847
+ const server = requiredServerAddress(stage, workspace.domains?.[stage], custom);
1848
+ if (hosts.length === 0) return;
1849
+ if (ctx.skipDns) {
1850
+ ctx.logger.info("🌐 DNS skipped (--skip-dns): no record is written or checked");
1851
+ return;
1852
+ }
1853
+ if (!server) {
1854
+ ctx.logger.info(`🌐 DNS skipped: no ${SERVER_IPV4_KEY} in '${stage}''s secrets — ${serverAddressHint(stage)}`);
1855
+ return;
1856
+ }
1857
+ const log = (line) => ctx.logger.info(line);
1858
+ if (workspace.dns) log(`🌐 DNS for '${stage}' → ${server.ipv4}${server.ipv6 ? ` / ${server.ipv6}` : ""}${ctx.dryRun ? " (dry run — nothing is written)" : ""}`);
1859
+ try {
1860
+ const stored = await ctx.state.read(stage);
1861
+ const result = await stageDns({
1862
+ stage,
1863
+ hosts,
1864
+ server,
1865
+ dns: workspace.dns,
1866
+ recorded: dnsResources(stored?.resources ?? {}),
1867
+ ...wildcard ? { wildcard } : {},
1868
+ credentials: ctx.credentials,
1869
+ log,
1870
+ lookup: deps.lookup ?? systemLookup,
1871
+ ...ctx.dryRun ? { dryRun: true } : {},
1872
+ ...deps.dnsProviderFor ? { providerFor: deps.dnsProviderFor } : {}
1873
+ });
1874
+ if (!ctx.dryRun) {
1875
+ const journal = await DeployJournal.open(ctx.state, stage, () => createComposeState(stage));
1876
+ const providers = new Map(result.plan.domains.map((d) => [d.domain, d.provider]));
1877
+ await recordDnsChanges(journal, result.changes, (domain) => providers.get(domain));
1878
+ }
1879
+ const parts = [result.confirmed.length > 0 ? `${result.confirmed.length} host${result.confirmed.length === 1 ? "" : "s"} confirmed by the DNS provider` : "", result.resolved.length > 0 ? `${result.resolved.length} host${result.resolved.length === 1 ? "" : "s"} resolve${result.resolved.length === 1 ? "s" : ""} to ${server.ipv4}` : ""].filter(Boolean);
1880
+ if (parts.length > 0) log(`🌐 ${parts.join(", ")}`);
1881
+ if (ctx.dryRun) {
1882
+ await result.prune();
1883
+ return;
1884
+ }
1885
+ return result.recorded.stale.length > 0 ? result.prune : void 0;
1886
+ } catch (error) {
1887
+ if (ctx.dryRun && error instanceof HostNotPointingAtServer) {
1888
+ ctx.logger.warn(`⚠ ${error.message}`);
1889
+ return;
1890
+ }
1891
+ throw error;
1892
+ }
1893
+ }
1894
+ /**
1895
+ * The records gkm wrote for hosts the stage no longer serves, deleted once it
1896
+ * answers on its new ones — and forgotten from its state, deleted or not:
1897
+ * none is gkm's to manage any more.
1898
+ */
1899
+ async function pruneStageDns(ctx, run) {
1900
+ if (!run.pruneDns) return;
1901
+ const pruned = await run.pruneDns();
1902
+ const journal = await DeployJournal.open(ctx.state, ctx.stage, () => createComposeState(ctx.stage));
1903
+ for (const { record } of pruned) await forgetDnsResource(journal, record);
1904
+ const deleted = pruned.filter((p) => p.action === "delete").length;
1905
+ if (deleted > 0) ctx.logger.info(`🌐 ${deleted} record${deleted === 1 ? "" : "s"} deleted`);
1906
+ }
1907
+ /**
1908
+ * The stage's secrets — and, for a deployed stage, everything it generates
1909
+ * once: its seed, and each declared secret and keyring. Kept only once the
1910
+ * run goes ahead (`provision`), so a dry run, or a tag that is not there,
1911
+ * leaves the stage's secrets as it found them.
1912
+ */
1913
+ async function stageSecrets(ctx, manifest) {
1914
+ const stored = await ctx.secrets.read();
1915
+ if (ctx.stage === ctx.workspace.stages.local) return {
1916
+ secrets: stored,
1917
+ generated: []
1918
+ };
1919
+ const { secrets, generated } = deployedStackSecrets(ctx.workspace, ctx.stage, stored, manifest);
1920
+ if (secrets.seed) ctx.secrets.mask(secrets.seed);
1921
+ for (const [key, value] of Object.entries(secrets.custom ?? {})) if (!isReservedStageKey(key)) ctx.secrets.mask(value);
1922
+ return {
1923
+ secrets,
1924
+ generated
1925
+ };
1926
+ }
1927
+ /**
1928
+ * The stack with each app's and worker's image pinned to the digest the file
1929
+ * names: the refs it is checked, pulled and recorded by, and the image each
1930
+ * compose service runs.
1931
+ */
1932
+ function withPinnedImages(stack, file, digests) {
1933
+ const apps = pinImages(file, digests, stack.apps);
1934
+ const workers = pinImages(file, digests, stack.workers);
1935
+ const processors = pinImages(file, digests, stack.processors);
1936
+ const services = { ...stack.compose.services };
1937
+ for (const image of [
1938
+ ...apps,
1939
+ ...workers,
1940
+ ...processors
1941
+ ]) services[image.name] = {
1942
+ ...services[image.name],
1943
+ image: image.ref
1944
+ };
1945
+ return {
1946
+ ...stack,
1947
+ apps,
1948
+ workers,
1949
+ processors,
1950
+ compose: {
1951
+ ...stack.compose,
1952
+ services
1953
+ }
1954
+ };
1955
+ }
1956
+ /**
1957
+ * A dry run: the stack's files, written so they can be read, and what a real
1958
+ * run would do — no image is built or pulled, no container touched, nothing
1959
+ * recorded and no secret kept.
1960
+ */
1961
+ async function planCompose(ctx, run) {
1962
+ if (run.generated.length > 0) ctx.logger.info(`🔑 "${ctx.stage}" has no ${run.generated.join(", ")} yet; this dry run used values it did not keep.`);
1963
+ run.files = await writeStack(ctx.cwd, run.dir, run.stack);
1964
+ printPlan(ctx, run);
1965
+ if (!run.push && run.stack.infra.includes("postgres")) reportPlannedSeeds((await plannedSeeds({
1966
+ root: ctx.workspace.root,
1967
+ manifest: ctx.manifest
1968
+ })).map(({ target, seeds }) => ({
1969
+ folder: target.seeds,
1970
+ seeds
1971
+ })), (line) => ctx.logger.info(line));
1972
+ const planned = (change) => {
1973
+ run.changes.push(change);
1974
+ ctx.emit({
1975
+ type: "resource.planned",
1976
+ ...change
1977
+ });
1978
+ };
1979
+ for (const service of run.push ? [] : run.stack.infra) planned({
1980
+ key: `service:${service}`,
1981
+ resourceType: "service",
1982
+ action: "ensure"
1983
+ });
1984
+ for (const app of run.stack.apps) {
1985
+ planned({
1986
+ key: `image:${app.name}`,
1987
+ resourceType: "image",
1988
+ action: run.mode === "build" ? "build" : "reuse",
1989
+ id: app.ref
1990
+ });
1991
+ if (run.push) continue;
1992
+ planned({
1993
+ key: `service:${app.name}`,
1994
+ resourceType: "service",
1995
+ action: run.previous[app.name] ? "reuse" : "create"
1996
+ });
1997
+ }
1998
+ for (const worker of [...run.stack.workers, ...run.stack.processors]) {
1999
+ planned({
2000
+ key: `image:${worker.name}`,
2001
+ resourceType: "image",
2002
+ action: run.mode === "build" ? "build" : "reuse",
2003
+ id: worker.ref
2004
+ });
2005
+ if (run.push) continue;
2006
+ planned({
2007
+ key: `service:${worker.name}`,
2008
+ resourceType: "service",
2009
+ action: run.previous[worker.name] ? "reuse" : "create"
2010
+ });
2011
+ }
2012
+ for (const push of run.topicSubscriptions?.pushes ?? []) {
2013
+ const key = subscriptionKey(push.worker, push.name);
2014
+ const id = run.topicSubscriptions?.recorded?.[key]?.id;
2015
+ planned({
2016
+ key,
2017
+ resourceType: SUBSCRIPTION_TYPE,
2018
+ action: id ? "reuse" : "create",
2019
+ ...id ? { id } : {}
2020
+ });
2021
+ }
2022
+ if (!run.push) planned({
2023
+ key: run.stack.proxy === "caddy" ? "service:caddy" : `service:${EDGE_PROJECT}/${EDGE_SERVICE}`,
2024
+ resourceType: "service",
2025
+ action: "ensure"
2026
+ });
2027
+ }
2028
+ /**
2029
+ * The shared edge's key for the stage's wildcard certificate: the DNS
2030
+ * challenge's record, and nothing else, through the stage domain's Route 53
2031
+ * entry — before the stack's routes ask for the certificate.
2032
+ */
2033
+ async function edgeAcmeKey(ctx, run, deps) {
2034
+ const { workspace, stage } = ctx;
2035
+ const domain = workspace.domains?.[stage];
2036
+ const dns = workspace.dns ? normalizeDnsConfig(workspace.dns) : {};
2037
+ const zone = domain ? findRootDomain(domain, dns) : null;
2038
+ const config = zone ? dns[zone] : void 0;
2039
+ if (!domain || !zone || config?.provider !== "route53") return;
2040
+ const route53 = config;
2041
+ const server = requiredServerAddress(stage, domain, run.secrets?.custom)?.ipv4 ?? run.server?.host ?? "local";
2042
+ const clients = await (deps.acmeClients ?? defaultAcmeClients)(route53);
2043
+ await ensureEdgeAcmeKey(deps.docker, {
2044
+ clients,
2045
+ edge: edgeRef(edgeFile(run), {
2046
+ ...run.ref.host ? { host: run.ref.host } : {},
2047
+ ...run.ref.output ? { output: run.ref.output } : {}
2048
+ }),
2049
+ server,
2050
+ project: run.stack.project,
2051
+ domain,
2052
+ zone,
2053
+ ...route53.hostedZoneId ? { hostedZoneId: route53.hostedZoneId } : {},
2054
+ ...ctx.dryRun ? { dryRun: true } : {},
2055
+ log: (line) => ctx.logger.info(line)
2056
+ });
2057
+ }
2058
+ /** IAM and Route 53 through the AWS SDK's own chain, or the entry's profile. */
2059
+ async function defaultAcmeClients(config) {
2060
+ const [{ IAMClient }, { Route53Client }, { fromIni }] = await Promise.all([
2061
+ import("@aws-sdk/client-iam"),
2062
+ import("@aws-sdk/client-route-53"),
2063
+ import("@aws-sdk/credential-providers")
2064
+ ]);
2065
+ const options = {
2066
+ region: config.region ?? "us-east-1",
2067
+ ...config.profile ? { credentials: fromIni({ profile: config.profile }) } : {}
2068
+ };
2069
+ return {
2070
+ iam: new IAMClient(options),
2071
+ route53: new Route53Client(options)
2072
+ };
2073
+ }
2074
+ /**
2075
+ * What the apps run on: the stage's generated secrets kept, the stack's files
2076
+ * written, its infrastructure started, and its databases, roles, grants and
2077
+ * migrations applied.
2078
+ */
2079
+ async function provisionCompose(ctx, run, deps) {
2080
+ const { ref } = run;
2081
+ if (run.push) return;
2082
+ if (run.generated.length > 0 && run.secrets) {
2083
+ await ctx.secrets.write(run.secrets);
2084
+ ctx.logger.info(`🔑 Generated for "${ctx.stage}" (${ctx.secrets.store}): ${run.generated.join(", ")}`);
2085
+ }
2086
+ const ports = edgePorts(deps.env);
2087
+ await assertEdgePorts(deps.docker, {
2088
+ project: run.stack.project,
2089
+ proxy: run.stack.proxy,
2090
+ ports,
2091
+ engine: ref
2092
+ });
2093
+ if (run.topicSubscriptions) await subscribeTopics(ctx, run, deps);
2094
+ const { stack } = run;
2095
+ run.files = await writeStack(ctx.cwd, run.dir, stack);
2096
+ printPlan(ctx, run);
2097
+ if (stack.proxy === "traefik") {
2098
+ ctx.logger.info(`\n🌐 Starting the shared edge (${EDGE_PROJECT})…`);
2099
+ run.files.push(...await ensureEdge(deps.docker, {
2100
+ file: edgeFile(run),
2101
+ ports,
2102
+ ...ref.host ? { host: ref.host } : {},
2103
+ logging: {
2104
+ driver: LOG_ROTATION.driver,
2105
+ options: { ...LOG_ROTATION.options }
2106
+ },
2107
+ ...ref.output ? { output: ref.output } : {},
2108
+ ...ref.signal ? { signal: ref.signal } : {}
2109
+ }));
2110
+ applied(ctx, run, {
2111
+ key: `service:${EDGE_PROJECT}/${EDGE_SERVICE}`,
2112
+ resourceType: "service",
2113
+ action: "ensure",
2114
+ id: `${EDGE_PROJECT}/${EDGE_SERVICE}`,
2115
+ via: "found"
2116
+ });
2117
+ if (stack.wildcard?.tls === "dns") await edgeAcmeKey(ctx, run, deps);
2118
+ }
2119
+ if (stack.infra.length > 0) {
2120
+ ctx.logger.info(`\n🗄️ Starting ${stack.infra.join(", ")}…`);
2121
+ await deps.docker.up(ref, stack.infra);
2122
+ for (const service of stack.infra) applied(ctx, run, {
2123
+ key: `service:${service}`,
2124
+ resourceType: "service",
2125
+ action: "ensure",
2126
+ id: `${stack.project}/${service}`,
2127
+ via: Object.keys(run.previous).length > 0 ? "recorded" : "created"
2128
+ });
2129
+ }
2130
+ if (stack.storage && stack.infra.includes("minio")) await prepareBuckets(ctx, run, deps);
2131
+ if (stack.infra.includes("postgres")) await prepareDatabases(ctx, run, deps);
2132
+ }
2133
+ /**
2134
+ * Subscribe each topic subscriber SNS pushes to — for a topic the stage set
2135
+ * to SNS by hand — record the ARNs, and compose the stack again with them,
2136
+ * before a file is written.
2137
+ */
2138
+ async function subscribeTopics(ctx, run, deps) {
2139
+ const { pushes, recorded, restack } = run.topicSubscriptions;
2140
+ if (pushes.length > 0) ctx.logger.info("\n📮 SNS subscriptions for the stage's own topics");
2141
+ run.stack = restack(await ensureTopicSubscriptions({
2142
+ pushes,
2143
+ recorded,
2144
+ secrets: run.secrets?.custom ?? {},
2145
+ stage: ctx.stage,
2146
+ env: deps.env,
2147
+ record: async (resource) => {
2148
+ await ctx.state.putResource(ctx.stage, resource);
2149
+ applied(ctx, run, {
2150
+ key: resource.key,
2151
+ resourceType: SUBSCRIPTION_TYPE,
2152
+ action: "ensure",
2153
+ id: resource.id,
2154
+ via: recorded?.[resource.key]?.id === resource.id ? "recorded" : "created"
2155
+ });
2156
+ },
2157
+ forget: async (key) => {
2158
+ await ctx.state.deleteResource(ctx.stage, key);
2159
+ },
2160
+ log: (line) => ctx.logger.info(line)
2161
+ }));
2162
+ }
2163
+ /**
2164
+ * Create each bucket the stack's MinIO serves, and the open paths of each
2165
+ * one a file server fronts — from this machine, on MinIO's loopback port,
2166
+ * the way `gkm dev` creates them.
2167
+ */
2168
+ async function prepareBuckets(ctx, run, deps) {
2169
+ const storage = run.stack.storage;
2170
+ if (storage.buckets.length === 0) return;
2171
+ const tunnel = await reach(run, deps, "minio", LOOPBACK_SERVICES.minio);
2172
+ let done;
2173
+ try {
2174
+ const client = deps.buckets(tunnel.port, storage);
2175
+ done = [
2176
+ ...await applyBuckets(client, storage.buckets),
2177
+ ...await applyPolicies(client, storage.policies),
2178
+ ...await applyLifecycles(client, storage.lifecycles),
2179
+ ...await applyNotifications(client, storage.objects, new Set(storage.webhooks.map((hook) => hook.bucket)))
2180
+ ];
2181
+ } finally {
2182
+ await tunnel.close();
2183
+ }
2184
+ const created = done.filter((entry) => entry.created).length;
2185
+ ctx.logger.info(`🪣 Buckets: ${storage.buckets.join(", ")}${created ? ` (${created} change(s))` : ""}`);
2186
+ }
2187
+ /**
2188
+ * Create what the plan names in the stack's Postgres, migrate it, then run
2189
+ * its seeds — every one, every time, as each construct's owner.
2190
+ *
2191
+ * Every service the stack publishes on its loopback is reached, not only
2192
+ * Postgres: a seed runs project code, and project code that uploads a file
2193
+ * reaches the stack's MinIO as well.
2194
+ */
2195
+ async function prepareDatabases(ctx, run, deps) {
2196
+ const tunnels = [];
2197
+ try {
2198
+ const ports = {};
2199
+ for (const [service, inside] of Object.entries(LOOPBACK_SERVICES)) {
2200
+ if (!run.stack.infra.includes(service)) continue;
2201
+ const tunnel = await reach(run, deps, service, inside);
2202
+ tunnels.push(tunnel);
2203
+ ports[primaryPortKey(service)] = tunnel.port;
2204
+ }
2205
+ await migrateAndSeed(ctx, run, deps, ports);
2206
+ } finally {
2207
+ await Promise.all(tunnels.map((tunnel) => tunnel.close()));
2208
+ }
2209
+ }
2210
+ /**
2211
+ * A port a stack's service publishes on its engine's loopback, reachable
2212
+ * from this machine: the published port itself on the local stage, and on a
2213
+ * server an SSH tunnel to it over the deploy's own login — so Postgres and
2214
+ * MinIO are never published beyond the server's loopback.
2215
+ *
2216
+ * Migrations and seeds run here, in this process, with the project's own
2217
+ * Kysely — the way `gkm migrate` runs them — through that port. No image
2218
+ * needs to carry them, and nothing is run on the server but the stack.
2219
+ */
2220
+ async function reach(run, deps, service, inside) {
2221
+ const published = await deps.docker.port(run.ref, service, inside);
2222
+ if (!run.server) return {
2223
+ port: published,
2224
+ close: async () => {}
2225
+ };
2226
+ return deps.server.tunnel(run.server, published);
2227
+ }
2228
+ async function migrateAndSeed(ctx, run, deps, ports) {
2229
+ const { stack } = run;
2230
+ const workspace = ctx.workspace;
2231
+ const port = ports[primaryPortKey("postgres")];
2232
+ const superuser = stack.credential.containers.postgres;
2233
+ const login = await deps.logins({
2234
+ port,
2235
+ login: superuser,
2236
+ legacy: [{
2237
+ user: LEGACY_LOCAL_LOGIN.user,
2238
+ password: superuser.password
2239
+ }, ...stack.local ? [LEGACY_LOCAL_LOGIN] : []]
2240
+ });
2241
+ if (login.notice) ctx.logger.info(login.notice);
2242
+ const statements = [...postgresStatements(stack.plan, workspace.name, stack.credential.seed), ...stack.backups ? backupsRoleStatements(stack.backups) : []];
2243
+ if (statements.length > 0) {
2244
+ ctx.logger.info("🗄️ Creating databases, roles and grants…");
2245
+ await applyPostgres(deps.sql(port, login.login), statements);
2246
+ }
2247
+ const sources = {};
2248
+ const manifest = await discover({
2249
+ patterns: constructGlobs(workspace),
2250
+ cwd: workspace.root,
2251
+ sources
2252
+ });
2253
+ const custom = run.secrets?.custom ?? {};
2254
+ const resolved = envFor(stack.plan, {
2255
+ ...stack.resolution,
2256
+ ports,
2257
+ credentials: {
2258
+ ...stack.resolution.credentials,
2259
+ postgres: login.login
2260
+ }
2261
+ });
2262
+ const env = stageEnv(run.secrets ? toEmbeddableSecrets(run.secrets) : {}, Object.fromEntries(Object.entries(resolved).filter(([key]) => custom[key] === void 0)));
2263
+ const options = {
2264
+ root: workspace.root,
2265
+ manifest,
2266
+ sources,
2267
+ env
2268
+ };
2269
+ const runs = await deps.migrate(options);
2270
+ const seeds = await deps.seed({
2271
+ ...options,
2272
+ stage: ctx.stage,
2273
+ backends: backendsOf(workspace)
2274
+ }).catch((error) => {
2275
+ if (error instanceof SeedFailed) throw new DeploySeedsFailed(ctx.stage, error.construct, error.seed, error);
2276
+ throw error;
2277
+ });
2278
+ reportDatabaseRuns(runs.map(({ target, applied }) => ({
2279
+ construct: target.id,
2280
+ folder: target.migrations,
2281
+ applied
2282
+ })), seeds.map(({ target, seeded }) => ({
2283
+ construct: target.id,
2284
+ folder: target.seeds,
2285
+ seeded
2286
+ })), (line) => ctx.logger.info(line), (event) => ctx.emit(event));
2287
+ }
2288
+ /**
2289
+ * Each app's image: built from this checkout — inside Docker, every one of
2290
+ * them, from a pruned slice of the build root — or pulled at the tag. Nothing
2291
+ * live changes.
2292
+ */
2293
+ async function buildCompose(ctx, run, deps) {
2294
+ const { stack, ref } = run;
2295
+ const images = [
2296
+ ...stack.apps,
2297
+ ...stack.workers,
2298
+ ...stack.processors
2299
+ ];
2300
+ const apps = images.map((image) => image.app);
2301
+ if (run.push) {
2302
+ run.files = await writeStack(ctx.cwd, run.dir, stack);
2303
+ printPlan(ctx, run);
2304
+ }
2305
+ if (run.mode === "build") {
2306
+ ctx.logger.info("\n🐳 Building images…");
2307
+ await deps.docker.build(ref, apps);
2308
+ } else {
2309
+ ctx.logger.info(`\n🐳 Pulling ${ctx.tag}…`);
2310
+ await deps.docker.pull(ref, apps);
2311
+ }
2312
+ if (run.push) ctx.logger.info("\n📤 Pushing images…");
2313
+ for (const image of images) {
2314
+ const digest = run.push ? await deps.docker.push(ref, image.ref) : await deps.docker.digest(ref, image.ref);
2315
+ if (run.push) ctx.logger.info(` ${image.app.padEnd(12)} ${image.ref}@${digest}`);
2316
+ run.images[image.app] = {
2317
+ ref: image.ref,
2318
+ tag: image.tag,
2319
+ ...digest ? { digest } : {}
2320
+ };
2321
+ ctx.emit({
2322
+ type: "artifact.built",
2323
+ app: image.app,
2324
+ imageRef: image.ref,
2325
+ ...digest ? { digest } : {}
2326
+ });
2327
+ run.changes.push({
2328
+ key: `image:${image.app}`,
2329
+ resourceType: "image",
2330
+ action: run.mode === "build" ? "build" : "reuse",
2331
+ id: digest ?? image.ref
2332
+ });
2333
+ }
2334
+ }
2335
+ /**
2336
+ * The whole stack up — anything it no longer defines removed — and each
2337
+ * app's image recorded in the stage's state.
2338
+ */
2339
+ async function releaseCompose(ctx, run, deps) {
2340
+ const { stack, ref } = run;
2341
+ if (run.push) return;
2342
+ const edge = edgeRef(edgeFile(run), {
2343
+ ...ref.host ? { host: ref.host } : {},
2344
+ ...ref.output ? { output: ref.output } : {}
2345
+ });
2346
+ if (stack.proxy === "traefik" && stack.traefik) {
2347
+ const written = await writeEdgeRoutes(deps.docker, edge, stack.project, stack.traefik, await edgeCertificate(run.dir, stack));
2348
+ ctx.logger.info(`🌐 Registered ${stack.routes.length} route(s) with ${EDGE_PROJECT}: ${written.at(-1)}`);
2349
+ } else if (!stack.local || existsSync(edgeFile(run))) await removeEdgeRoutes(deps.docker, edge, stack.project);
2350
+ ctx.logger.info("\n🚀 Starting the stack…");
2351
+ const down = await deps.docker.up(ref).then(() => [], (error) => unhealthyAfterUp(ctx, run, deps, error));
2352
+ const isDown = new Set(down.map(({ app }) => app));
2353
+ await recordImages(ctx, run);
2354
+ for (const app of stack.apps) {
2355
+ if (isDown.has(app.name)) continue;
2356
+ const image = run.images[app.name];
2357
+ applied(ctx, run, {
2358
+ key: `service:${app.name}`,
2359
+ resourceType: "service",
2360
+ action: "ensure",
2361
+ id: `${stack.project}/${app.name}`,
2362
+ via: run.previous[app.name] ? "recorded" : "created"
2363
+ });
2364
+ ctx.emit({
2365
+ type: "app.deployed",
2366
+ app: app.name,
2367
+ applicationId: `${stack.project}/${app.name}`,
2368
+ imageRef: image?.ref ?? app.ref,
2369
+ url: app.url
2370
+ });
2371
+ }
2372
+ for (const worker of [...stack.workers, ...stack.processors]) {
2373
+ if (isDown.has(worker.name)) continue;
2374
+ applied(ctx, run, {
2375
+ key: `service:${worker.name}`,
2376
+ resourceType: "service",
2377
+ action: "ensure",
2378
+ id: `${stack.project}/${worker.name}`,
2379
+ via: run.previous[worker.name] ? "recorded" : "created"
2380
+ });
2381
+ }
2382
+ ctx.logger.info(`\n✅ ${stack.project} is running:`);
2383
+ for (const app of stack.apps) {
2384
+ if (isDown.has(app.name)) continue;
2385
+ ctx.logger.info(` ${app.name.padEnd(12)} ${app.url}${app.auth ? ` 🔒 user: ${app.auth.user}, password: gkm secrets:show --stage ${ctx.stage} --reveal → ${app.auth.key}` : ""}`);
2386
+ }
2387
+ if (stack.mailInbox) ctx.logger.info(` ${"mail".padEnd(12)} ${stack.mailInbox.url} (inbox)`);
2388
+ for (const worker of stack.workers) {
2389
+ if (isDown.has(worker.name)) continue;
2390
+ ctx.logger.info(` ${worker.name.padEnd(12)} worker ${worker.id}`);
2391
+ }
2392
+ for (const unit of stack.processors) {
2393
+ if (isDown.has(unit.name)) continue;
2394
+ ctx.logger.info(` ${unit.name.padEnd(12)} processors of ${unit.id}`);
2395
+ }
2396
+ if (stack.authority) ctx.logger.info(`\n🔐 Certificates are from gkm's local CA ("gkm trust" trusts it here): NODE_EXTRA_CA_CERTS=${stack.authority.certFile}`);
2397
+ else if (stack.local && stack.tls) ctx.logger.info(stack.tls.caFile ? `\n🔐 Certificates are the stage's own (deploy.compose.tls), from the CA in ${stack.tls.caFile}: NODE_EXTRA_CA_CERTS=${stack.tls.caFile}` : `\n🔐 Certificates are the stage's own (deploy.compose.tls): ${stack.tls.certFile}. Set its caFile to the root it chains to, so gkm and the stack's containers trust it.`);
2398
+ if (down.length > 0) throw new ComposeAppsUnhealthy(stack.project, down, stack.proxy, "docker");
2399
+ }
2400
+ /**
2401
+ * After `up --wait` failed: each app's and worker's service that is not
2402
+ * healthy once its health settles — waited on while it is still starting,
2403
+ * as `--wait` stops at the first that is not. None means the failure was not
2404
+ * health's, and it is thrown as it was.
2405
+ */
2406
+ async function unhealthyAfterUp(ctx, run, deps, error) {
2407
+ const { stack } = run;
2408
+ const services = [
2409
+ ...stack.apps.flatMap(replicaNames),
2410
+ ...stack.workers.map((worker) => worker.name),
2411
+ ...stack.processors.map((unit) => unit.name)
2412
+ ];
2413
+ const down = (await Promise.all(services.map(async (service) => {
2414
+ let last;
2415
+ for (let attempt = 1; attempt <= deps.healthAttempts; attempt++) {
2416
+ ctx.signal.throwIfAborted();
2417
+ last = await deps.docker.health(run.ref, service).catch(() => void 0);
2418
+ if (last !== "starting") break;
2419
+ if (attempt < deps.healthAttempts) await new Promise((resolve) => setTimeout(resolve, deps.healthIntervalMs));
2420
+ }
2421
+ return {
2422
+ app: service,
2423
+ url: `docker:${service}`,
2424
+ last
2425
+ };
2426
+ }))).filter(({ last }) => last !== "healthy").map(({ app, url, last }) => ({
2427
+ app,
2428
+ url,
2429
+ last: last ?? "not running"
2430
+ }));
2431
+ if (down.length === 0) throw error;
2432
+ ctx.logger.warn(`\n⚠️ ${down.map(({ app }) => app).join(", ")} did not turn healthy; the rest of the stack is released and routed.`);
2433
+ return down;
2434
+ }
2435
+ /**
2436
+ * Each app's image in the stage's state — the same `images` a Dokploy deploy
2437
+ * records: the ref it runs, the tag it was released under, and the digest
2438
+ * the tag resolved to.
2439
+ */
2440
+ async function recordImages(ctx, run) {
2441
+ const journal = await DeployJournal.open(ctx.state, ctx.stage, () => createComposeState(ctx.stage));
2442
+ const by = currentActor();
2443
+ for (const app of [
2444
+ ...run.stack.apps,
2445
+ ...run.stack.workers,
2446
+ ...run.stack.processors
2447
+ ]) {
2448
+ const image = run.images[app.app] ?? {
2449
+ ref: app.ref,
2450
+ tag: app.tag
2451
+ };
2452
+ recordRelease(journal.state, app.app, image, by);
2453
+ }
2454
+ journal.state.identity = ctx.identity.key;
2455
+ await journal.save();
2456
+ }
2457
+ /**
2458
+ * Each app asked through Caddy, over HTTPS, the certificate verified: an API
2459
+ * at `/health`, a site at `/`. On the local stage the edge is this machine and
2460
+ * its certificate is from gkm's local CA, so that root is the one trusted and
2461
+ * the request goes to 127.0.0.1.
2462
+ */
2463
+ async function verifyCompose(ctx, run, deps) {
2464
+ const { stack } = run;
2465
+ if (run.push) return;
2466
+ const ca = await verifyAuthority(stack);
2467
+ ctx.logger.info(`\n🩺 Checking each app through ${stack.proxy === "caddy" ? "Caddy" : "the shared edge"}…`);
2468
+ const edge = stack.local ? { connectTo: "127.0.0.1" } : run.server ? {
2469
+ connectTo: run.server.host,
2470
+ connectPort: edgePorts(deps.env).https
2471
+ } : void 0;
2472
+ const results = await Promise.all(stack.apps.map((app) => app.id === stack.wildcard?.site ? checkContainer(ctx, run, deps, app.name) : checkApp(ctx, app, deps, {
2473
+ ca,
2474
+ edge
2475
+ })));
2476
+ results.push(...await Promise.all([...stack.workers, ...stack.processors].map((worker) => checkContainer(ctx, run, deps, worker.name))));
2477
+ if (stack.logs) results.push(await checkContainer(ctx, run, deps, LOGS_SERVICE));
2478
+ const down = results.filter((result) => !result.healthy).map(({ app, url, last }) => ({
2479
+ app,
2480
+ url,
2481
+ last
2482
+ }));
2483
+ if (down.length > 0) throw new ComposeAppsUnhealthy(stack.project, down, stack.proxy);
2484
+ await pruneStageDns(ctx, run);
2485
+ await confirmSubscriptions(ctx, run, deps);
2486
+ await warmSites(ctx, run, deps, {
2487
+ ca,
2488
+ edge
2489
+ });
2490
+ if (stack.logs) reportLogs(ctx, stack.logs, stack.local, run.server);
2491
+ }
2492
+ /**
2493
+ * Each server-rendered site (`next`, `tanstack`) asked for its sitemap, its
2494
+ * `/` and every page the sitemap lists, through the edge as `checkApp` asks
2495
+ * it — see `warmup.ts`. Its image was built with no API reachable, so each
2496
+ * page renders on its first request: this is that request, made before any
2497
+ * visitor's. A Next site is asked with its on-demand revalidation header,
2498
+ * so a page its build cached (empty) is rendered again from the deployed
2499
+ * APIs rather than served from that cache.
2500
+ *
2501
+ * Only ever said, never failed on: every app is already healthy, and a page
2502
+ * that did not warm renders on the next request for it.
2503
+ */
2504
+ async function warmSites(ctx, run, deps, options) {
2505
+ const { stack } = run;
2506
+ const sites = stack.apps.filter((app) => app.kind === "site" && (app.variant === "next" || app.variant === "tanstack"));
2507
+ if (sites.length === 0) return;
2508
+ ctx.logger.info("\n🔥 Warming each server-rendered site…");
2509
+ for (const app of sites) {
2510
+ if (app.id === stack.wildcard?.site) {
2511
+ ctx.logger.info(` – ${app.name.padEnd(12)} serves every subdomain: there is no one host to warm, so it is skipped`);
2512
+ continue;
2513
+ }
2514
+ try {
2515
+ const headers = app.variant === "next" ? await forceRender(ctx, run, deps, app) : {};
2516
+ const { warmed, cached, warnings, leftOut } = await warmSite({
2517
+ url: app.url,
2518
+ fetch: deps.fetch ?? edgeFetch,
2519
+ headers,
2520
+ edge: {
2521
+ ...options.ca ? { ca: options.ca } : {},
2522
+ ...app.auth ? { authorization: basicAuthorization(app.auth) } : {},
2523
+ ...options.edge,
2524
+ signal: ctx.signal
2525
+ }
2526
+ });
2527
+ const said = `warmed ${warmed.length} page(s)${cached.length > 0 ? `, ${cached.length} served from the build cache` : ""}${warnings.length > 0 ? `, ${warnings.length} warning(s)` : ""}`;
2528
+ if (warnings.length === 0) ctx.logger.info(` ✓ ${app.name.padEnd(12)} ${said}${cached.length > 0 ? `: ${listed(cached)}` : ""}`);
2529
+ else {
2530
+ ctx.logger.warn(` ⚠ ${app.name.padEnd(12)} ${said}:`);
2531
+ for (const { url, reason } of warnings) ctx.logger.warn(` ${url}: ${reason}`);
2532
+ if (cached.length > 0) ctx.logger.warn(` served from the build cache: ${listed(cached)}`);
2533
+ }
2534
+ if (leftOut > 0) ctx.logger.info(` ${leftOut} more page(s) in its sitemap were not asked: at most 500 are warmed per site`);
2535
+ } catch (error) {
2536
+ if (ctx.signal.aborted) throw error;
2537
+ ctx.logger.warn(` ⚠ ${app.name.padEnd(12)} not warmed: ${error instanceof Error ? error.message : String(error)}`);
2538
+ }
2539
+ }
2540
+ }
2541
+ /** The most pages a summary names; the rest are counted. */
2542
+ const LISTED_PAGES = 10;
2543
+ /** Pages by path, `/about`, the first {@link LISTED_PAGES} of them. */
2544
+ function listed(urls) {
2545
+ const paths = urls.map((url) => {
2546
+ const { pathname, search } = new URL(url);
2547
+ return `${pathname}${search}`;
2548
+ });
2549
+ const shown = paths.slice(0, LISTED_PAGES).join(", ");
2550
+ return paths.length > LISTED_PAGES ? `${shown} and ${paths.length - LISTED_PAGES} more` : shown;
2551
+ }
2552
+ /**
2553
+ * The header that has a Next site render each warmed page whatever its
2554
+ * cache says: `x-prerender-revalidate` with the preview-mode id of the image
2555
+ * running — read in its container, through the stack's Docker engine, at
2556
+ * the path the generated Dockerfile puts the standalone output at. Where the
2557
+ * id cannot be read, said once, and the site is warmed with plain requests.
2558
+ */
2559
+ async function forceRender(ctx, run, deps, app) {
2560
+ let reason;
2561
+ try {
2562
+ const root = ctx.workspace.root;
2563
+ const gkmRoot = relative(findBuildRoot(root), root) || ".";
2564
+ const lookup = await nextPreviewModeId((command) => deps.docker.exec(run.ref, app.name, command), nextPrerenderManifest(fromBuildRoot({ gkmRoot }, app.path)));
2565
+ if ("id" in lookup) return { [NEXT_REVALIDATE_HEADER]: lookup.id };
2566
+ reason = lookup.reason;
2567
+ } catch (error) {
2568
+ if (ctx.signal.aborted) throw error;
2569
+ reason = error instanceof Error ? error.message : String(error);
2570
+ }
2571
+ ctx.logger.warn(` ⚠ ${app.name.padEnd(12)} can't force ISR pages to render: ${reason}. Warming with plain requests; a page Next cached at build is served from that cache until its revalidate window ends.`);
2572
+ return {};
2573
+ }
2574
+ /**
2575
+ * The SNS subscriptions the stage's providers made before the workers were
2576
+ * up: each still pending is subscribed again, so SNS resends its
2577
+ * confirmation to a worker now there to answer it, and waited on. One still
2578
+ * pending after that is said, not failed on — every app is healthy, and
2579
+ * rolling them back would not confirm it.
2580
+ */
2581
+ async function confirmSubscriptions(ctx, run, deps) {
2582
+ const stored = await ctx.state.read(ctx.stage);
2583
+ const left = await (deps.confirmSubscriptions ?? confirmPendingSubscriptions)({
2584
+ workspace: ctx.workspace,
2585
+ stage: ctx.stage,
2586
+ secrets: run.secrets?.custom ?? {},
2587
+ resources: stored?.resources,
2588
+ env: deps.env,
2589
+ log: (line) => ctx.logger.info(line),
2590
+ record: async (record) => {
2591
+ const { updatedAt: _, updatedBy: __, ...input } = record;
2592
+ await ctx.state.putResource(ctx.stage, input);
2593
+ }
2594
+ });
2595
+ if (left.length > 0) ctx.logger.warn(`⚠ ${new PushSubscriptionsUnconfirmed(left).message}`);
2596
+ }
2597
+ /**
2598
+ * A container's own health check, as Docker reports it: a worker's, which
2599
+ * has no route through the edge, and OpenObserve's — by default it has no
2600
+ * host there, and served publicly it answers only the addresses it allows,
2601
+ * which need not include this one.
2602
+ */
2603
+ async function checkContainer(ctx, run, deps, service) {
2604
+ const url = `docker:${service}`;
2605
+ let last = "not asked";
2606
+ for (let attempt = 1; attempt <= deps.healthAttempts; attempt++) {
2607
+ ctx.signal.throwIfAborted();
2608
+ const health = await deps.docker.health(run.ref, service).catch((error) => error instanceof Error ? error.message : String(error));
2609
+ last = health ?? "not running";
2610
+ const healthy = health === "healthy";
2611
+ ctx.emit({
2612
+ type: "health.checked",
2613
+ app: service,
2614
+ url,
2615
+ healthy,
2616
+ attempt
2617
+ });
2618
+ if (healthy) {
2619
+ ctx.logger.info(` ✓ ${service.padEnd(12)} ${url} (${last})`);
2620
+ return {
2621
+ app: service,
2622
+ url,
2623
+ last,
2624
+ healthy
2625
+ };
2626
+ }
2627
+ if (attempt < deps.healthAttempts) await new Promise((resolve) => setTimeout(resolve, deps.healthIntervalMs));
2628
+ }
2629
+ ctx.logger.warn(` ✗ ${service.padEnd(12)} ${url} (${last})`);
2630
+ return {
2631
+ app: service,
2632
+ url,
2633
+ last,
2634
+ healthy: false
2635
+ };
2636
+ }
2637
+ /** A login, as the `Authorization` header that carries it. */
2638
+ function basicAuthorization(auth) {
2639
+ return `Basic ${Buffer.from(`${auth.user}:${auth.password}`).toString("base64")}`;
2640
+ }
2641
+ /** How to open the logs, printed — and as an event, for a headless caller. */
2642
+ function reportLogs(ctx, logs, local, server) {
2643
+ const lines = logsAccess(logs, {
2644
+ stage: ctx.stage,
2645
+ local,
2646
+ user: server?.user ?? currentUser(),
2647
+ hostname: server?.host ?? hostname(),
2648
+ ...server ? { known: true } : {}
2649
+ });
2650
+ ctx.logger.info(`\n${lines.join("\n")}`);
2651
+ ctx.emit({
2652
+ type: "logs.ready",
2653
+ service: LOGS_SERVICE,
2654
+ access: logs.public ? "public" : "tunnel",
2655
+ url: logs.url,
2656
+ ...logs.public ? { allow: [...logs.public.allow] } : { port: logs.port },
2657
+ email: logs.email
2658
+ });
2659
+ }
2660
+ /** This machine's user — a guess at who would SSH in. */
2661
+ function currentUser() {
2662
+ try {
2663
+ return userInfo().username;
2664
+ } catch {
2665
+ return process.env.USER ?? "user";
2666
+ }
2667
+ }
2668
+ async function checkApp(ctx, app, deps, options) {
2669
+ const url = `${app.url.replace(/^ws/, "http")}${app.kind === "site" ? "/" : "/health"}`;
2670
+ let last = "not asked";
2671
+ for (let attempt = 1; attempt <= deps.healthAttempts; attempt++) {
2672
+ ctx.signal.throwIfAborted();
2673
+ let status;
2674
+ try {
2675
+ status = await deps.probe({
2676
+ url,
2677
+ ...options.ca ? { ca: options.ca } : {},
2678
+ ...app.auth ? { authorization: basicAuthorization(app.auth) } : {},
2679
+ ...options.edge,
2680
+ timeoutMs: 1e4,
2681
+ signal: ctx.signal
2682
+ });
2683
+ last = `HTTP ${status}`;
2684
+ } catch (error) {
2685
+ last = error instanceof Error ? error.message : String(error);
2686
+ }
2687
+ const healthy = status !== void 0 && isHealthy(status);
2688
+ ctx.emit({
2689
+ type: "health.checked",
2690
+ app: app.name,
2691
+ url,
2692
+ healthy,
2693
+ ...status !== void 0 ? { status } : {},
2694
+ attempt
2695
+ });
2696
+ if (healthy) {
2697
+ ctx.logger.info(` ✓ ${app.name.padEnd(12)} ${url} (${last})`);
2698
+ return {
2699
+ app: app.name,
2700
+ url,
2701
+ last,
2702
+ healthy
2703
+ };
2704
+ }
2705
+ if (attempt < deps.healthAttempts) await new Promise((resolve) => setTimeout(resolve, deps.healthIntervalMs));
2706
+ }
2707
+ ctx.logger.warn(` ✗ ${app.name.padEnd(12)} ${url} (${last})`);
2708
+ return {
2709
+ app: app.name,
2710
+ url,
2711
+ last,
2712
+ healthy: false
2713
+ };
2714
+ }
2715
+ function composeResult(ctx, run) {
2716
+ const apps = [...run.stack.apps.map((app) => {
2717
+ const image = run.images[app.name];
2718
+ return {
2719
+ appName: app.name,
2720
+ type: app.kind === "site" ? "web" : "backend",
2721
+ success: !ctx.dryRun,
2722
+ applicationId: `${run.stack.project}/${app.name}`,
2723
+ imageRef: image?.ref ?? app.ref,
2724
+ ...image?.digest ? { digest: image.digest } : {},
2725
+ url: app.url
2726
+ };
2727
+ }), ...[...run.stack.workers, ...run.stack.processors].map((worker) => {
2728
+ const image = run.images[worker.name];
2729
+ return {
2730
+ appName: worker.name,
2731
+ type: "backend",
2732
+ success: !ctx.dryRun,
2733
+ applicationId: `${run.stack.project}/${worker.name}`,
2734
+ imageRef: image?.ref ?? worker.ref,
2735
+ ...image?.digest ? { digest: image.digest } : {}
2736
+ };
2737
+ })];
2738
+ return {
2739
+ apps,
2740
+ projectId: run.stack.project,
2741
+ successCount: ctx.dryRun ? 0 : apps.length,
2742
+ failedCount: 0,
2743
+ stage: ctx.stage,
2744
+ identity: ctx.identity.key,
2745
+ tag: ctx.tag,
2746
+ dryRun: ctx.dryRun,
2747
+ environmentId: "",
2748
+ skipped: [...ctx.skipped],
2749
+ urls: Object.fromEntries(run.stack.apps.map((app) => [app.name, app.url])),
2750
+ changes: run.changes
2751
+ };
2752
+ }
2753
+ /** The shared edge's compose file, as this machine hands it to compose. */
2754
+ function edgeFile(run) {
2755
+ return join(run.dir, `${EDGE_PROJECT}.yml`);
2756
+ }
2757
+ function applied(ctx, run, change) {
2758
+ const { via, ...rest } = change;
2759
+ run.changes.push(rest);
2760
+ ctx.emit({
2761
+ type: "resource.applied",
2762
+ ...rest,
2763
+ via
2764
+ });
2765
+ }
2766
+ /**
2767
+ * The root the check after a start trusts, PEM: gkm's on the local stage that
2768
+ * set no certificate of its own, and otherwise the one the stage's own names
2769
+ * (`deploy.compose.tls.<stage>.caFile`). Undefined — Node's own roots, and
2770
+ * `NODE_EXTRA_CA_CERTS` — for a certificate that names none: a public CA's.
2771
+ */
2772
+ async function verifyAuthority(stack) {
2773
+ if (stack.authority) return (await readLocalAuthority(stack.authority)).cert;
2774
+ if (stack.tls?.caFile) return readFile(stack.tls.caFile, "utf-8");
2775
+ }
2776
+ /**
2777
+ * The certificate the stack's proxy serves, PEM: the stage's own where it set
2778
+ * one, the one gkm's local CA issued the local stage otherwise, and none — a
2779
+ * deployed stage's comes from Let's Encrypt — when neither.
2780
+ */
2781
+ async function edgeCertificate(dir, stack) {
2782
+ if (stack.tls) return {
2783
+ cert: await readFile(stack.tls.certFile, "utf-8"),
2784
+ key: await readFile(stack.tls.keyFile, "utf-8")
2785
+ };
2786
+ if (stack.authority) return localEdgeCertificate(dir, stack, stack.authority);
2787
+ }
2788
+ /** Where the local stage's edge certificate is kept, between runs. */
2789
+ const LOCAL_EDGE_CERTIFICATE = "local-edge.pem";
2790
+ /**
2791
+ * The local stage's edge certificate, from gkm's local CA: the one kept in
2792
+ * the stack's directory while it still covers every host, so an unchanged
2793
+ * stack does not recreate its Caddy for a new certificate each run.
2794
+ */
2795
+ async function localEdgeCertificate(dir, stack, authority) {
2796
+ const { cert, key } = await ensureLocalCertificate(await ensureLocalAuthority(authority), localCertificateNames(stack.resolution.project ?? "", stackHosts(stack)), join(dir, LOCAL_EDGE_CERTIFICATE));
2797
+ return {
2798
+ cert,
2799
+ key
2800
+ };
2801
+ }
2802
+ /** Write the stack's files: owner-only, since the env files hold secrets. */
2803
+ async function writeStack(root, dir, stack) {
2804
+ await mkdir(dir, {
2805
+ recursive: true,
2806
+ mode: 448
2807
+ });
2808
+ await chmod(dir, 448);
2809
+ const files = [];
2810
+ const write = async (path, content, mode = 420) => {
2811
+ await mkdir(dirname(path), { recursive: true });
2812
+ await writeFile(path, content, { mode });
2813
+ await chmod(path, mode);
2814
+ files.push(path);
2815
+ };
2816
+ const issued = await edgeCertificate(dir, stack);
2817
+ const tls = stack.proxy === "caddy" ? issued : void 0;
2818
+ const compose = stack.caddyfile !== void 0 ? withCaddyConfigs(stack.compose, {
2819
+ caddyfile: stack.caddyfile,
2820
+ ...tls ? { tls } : {}
2821
+ }) : stack.compose;
2822
+ await write(join(dir, "docker-compose.yml"), composeYaml(stack, compose), tls ? 384 : 420);
2823
+ if (stack.caddyfile !== void 0) {
2824
+ await write(join(dir, "Caddyfile"), stack.caddyfile);
2825
+ await rm(join(dir, "traefik.yml"), { force: true });
2826
+ } else await rm(join(dir, "Caddyfile"), { force: true });
2827
+ if (stack.traefik !== void 0) await write(join(dir, "traefik.yml"), stack.traefik);
2828
+ await rm(join(dir, "tls"), {
2829
+ recursive: true,
2830
+ force: true
2831
+ });
2832
+ for (const app of [
2833
+ ...stack.apps,
2834
+ ...stack.workers,
2835
+ ...stack.processors
2836
+ ]) if (app.env) await write(join(dir, `${app.name}.env`), envFile(app.env), 384);
2837
+ if (stack.logs && stack.compose.services["openobserve"]?.env_file) await write(join(dir, `${LOGS_SERVICE}.env`), envFile(stack.logs.env), 384);
2838
+ if (stack.redis && stack.compose.services["redis"]?.env_file) await write(join(dir, `${REDIS_SERVICE}.env`), envFile(stack.redis.env), 384);
2839
+ if (stack.backups) {
2840
+ for (const [name, content] of Object.entries(stack.backups.files)) await write(join(dir, BACKUPS_SERVICE, name), content);
2841
+ if (stack.compose.services["backups"]?.env_file) await write(join(dir, `${BACKUPS_SERVICE}.env`), envFile(stack.backups.env), 384);
2842
+ } else {
2843
+ await rm(join(dir, BACKUPS_SERVICE), {
2844
+ recursive: true,
2845
+ force: true
2846
+ });
2847
+ await rm(join(dir, `${BACKUPS_SERVICE}.env`), { force: true });
2848
+ }
2849
+ for (const entry of await readdir(dir)) if (entry.endsWith(".credentials")) await rm(join(dir, entry));
2850
+ for (const [path, content] of Object.entries(stack.dockerfiles)) await write(join(root, path), content);
2851
+ await ensureDockerignore(findBuildRoot(root));
2852
+ return files;
2853
+ }
2854
+ function composeYaml(stack, compose) {
2855
+ const stage = ` --stage ${stack.stage}`;
2856
+ return `# Generated by gkm compose from the construct manifest — do not edit.
2857
+ # The ${stack.stage} stage's APIs and sites behind ${stack.proxy === "caddy" ? "one Caddy" : `the shared Traefik edge (${EDGE_PROJECT})`}, and its workers.
2858
+ # Each backend and worker reads exactly the keys in its own env file beside
2859
+ # this one.
2860
+ #
2861
+ # gkm compose${stage} start or update it
2862
+ # gkm compose${stage} --down stop it
2863
+ ${stringify(compose, {
2864
+ lineWidth: 0,
2865
+ aliasDuplicateObjects: false
2866
+ })}`;
2867
+ }
2868
+ function printPlan(ctx, run) {
2869
+ const { stack, mode, files } = run;
2870
+ ctx.logger.info(`\n🧱 ${stack.project} — stage ${stack.stage}`);
2871
+ if (run.server) ctx.logger.info(` → ${sshTarget(run.server)} (docker over ssh)`);
2872
+ for (const app of stack.apps) ctx.logger.info(` ${app.name.padEnd(12)} ${app.url.padEnd(40)} ${mode} ${app.ref}`);
2873
+ for (const worker of stack.workers) ctx.logger.info(` ${worker.name.padEnd(12)} ${`worker ${worker.id} (no route)`.padEnd(40)} ${mode} ${worker.ref}`);
2874
+ for (const unit of stack.processors) ctx.logger.info(` ${unit.name.padEnd(12)} ${`processors of ${unit.id}`.padEnd(40)} ${mode} ${unit.ref}`);
2875
+ if (stack.infra.length > 0) ctx.logger.info(` infrastructure: ${stack.infra.join(", ")}`);
2876
+ if (stack.backups) ctx.logger.info(` ${BACKUPS_SERVICE.padEnd(12)} ${stack.backups.databases.map((d) => d.file).join(", ")} — ${stack.backups.backups.describe}, to ${stack.backups.prefix}/`);
2877
+ ctx.logger.info(`\n📝 Wrote ${files.length} file(s) under ${stackDir(stack.stage)}/`);
2878
+ }
2879
+ //#endregion
2880
+ //#region src/target/compose/index.ts
2881
+ /**
2882
+ * The built-in `compose` target: a stage as one Docker Compose stack behind
2883
+ * Caddy — the local stage on the machine that runs the deploy, a deployed
2884
+ * stage on its server's Docker engine, over SSH (`deploy.compose.server`).
2885
+ *
2886
+ * ```bash
2887
+ * gkm deploy --target compose --stage production --tag v1.4.0 # what CI pushed
2888
+ * gkm deploy --target compose --stage production # built from here
2889
+ * gkm deploy --target compose --stage development # the local stage
2890
+ * ```
2891
+ *
2892
+ * - `validate`: the stack (`composeStack`, pure) and, for a tag, every image
2893
+ * looked up in the registry — `ImageTagNotFound` before anything changes
2894
+ * - `ready`: a deploy's own checks, after the stage's keys are — the stage's
2895
+ * certificate, its server's address, and every public host's DNS
2896
+ * - `plan`: the stack's files and what a run would create or reuse
2897
+ * - `provision`: generated secrets kept, files written, infrastructure up,
2898
+ * databases, roles, grants and migrations
2899
+ * - `build`: each backend bundled on the host and every image built — or, for
2900
+ * a tag, pulled
2901
+ * - `release`: the stack's routes on the shared edge, then `up --wait
2902
+ * --remove-orphans`, and each app's image, tag and digest in the stage's
2903
+ * state — an app that never turns healthy fails it only once the rest is
2904
+ * released and routed
2905
+ * - `verify`: each app asked through Caddy over HTTPS, then each
2906
+ * server-rendered site warmed — its `/` and its sitemap's pages asked, a
2907
+ * failure only warned of
2908
+ *
2909
+ * With `push` (`gkm compose --build --push`), run with `buildOnly`, it builds
2910
+ * every image and pushes it to `deploy.registry`, and does nothing else: no
2911
+ * readiness check runs, so a stage whose keys the deploy has yet to write
2912
+ * still builds.
2913
+ *
2914
+ * `gkm compose` is this target with a few more switches (`--build`, `--pull`,
2915
+ * `--down`), run through the same `deploy()`.
2916
+ */
2917
+ var compose_exports = /* @__PURE__ */ __exportAll({ composeTarget: () => composeTarget });
2918
+ /** The compose target, with its Docker, bundler and probe as given. */
2919
+ function composeTarget(options = {}) {
2920
+ const { mode, push, pin, report, ...given } = options;
2921
+ const deps = {
2922
+ ...defaultDeps,
2923
+ ...given
2924
+ };
2925
+ return defineTarget({
2926
+ name: "compose",
2927
+ runtime: "server",
2928
+ capabilities: {
2929
+ rollback: false,
2930
+ migrations: "target",
2931
+ images: true,
2932
+ localStage: true,
2933
+ selfHostedTelemetry: true
2934
+ },
2935
+ credentials: [],
2936
+ tag: ({ cwd }) => deps.revision(cwd),
2937
+ validate: (ctx) => validateCompose(ctx, deps, {
2938
+ ...mode ? { mode } : {},
2939
+ ...push ? { push } : {},
2940
+ ...pin ? { pin } : {}
2941
+ }),
2942
+ ready: (ctx, run) => readyCompose(ctx, run, deps),
2943
+ plan: (ctx, run) => planCompose(ctx, run),
2944
+ provision: (ctx, run) => provisionCompose(ctx, run, deps),
2945
+ build: (ctx, run) => buildCompose(ctx, run, deps),
2946
+ release: (ctx, run) => releaseCompose(ctx, run, deps),
2947
+ verify: (ctx, run) => verifyCompose(ctx, run, deps),
2948
+ result: (ctx, run) => {
2949
+ report?.(run);
2950
+ return composeResult(ctx, run);
2951
+ }
2952
+ });
2953
+ }
2954
+ //#endregion
2955
+ export { edgePorts as a, edgeRef as c, dockerHost as d, sshAccess as f, projectNetwork as h, ComposePushNeedsBuild as i, removeEdgeRoutes as l, dockerCompose as m, compose_exports as n, stackOverrideFile as o, sshTarget as p, ComposePinNeedsPull as r, deployedStackSecrets as s, composeTarget as t, composeServer as u };
2956
+
2957
+ //# sourceMappingURL=compose-BIXEPrGA.mjs.map