@rocket.chat/houston 0.27.0-next.3 → 2.1.8

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 (293) hide show
  1. package/LICENSE +1 -1
  2. package/README.md +108 -15
  3. package/bin/houston +62 -0
  4. package/data/docs/ARCHITECTURE.md +387 -0
  5. package/data/docs/automation.md +107 -0
  6. package/data/docs/e2ee.md +76 -0
  7. package/data/docs/eks.md +258 -0
  8. package/data/docs/gui.md +282 -0
  9. package/data/docs/guide.md +669 -0
  10. package/data/docs/kubernetes.md +138 -0
  11. package/data/docs/troubleshooting.md +193 -0
  12. package/data/docs/upgrading.md +84 -0
  13. package/data/docs/voice.md +253 -0
  14. package/data/iam/engineer-policy.json +11 -0
  15. package/data/iam/engineer-rbac.yaml +42 -0
  16. package/data/iam/operator-policy.json +211 -0
  17. package/data/loadtest/badbot.js +22 -0
  18. package/data/loadtest/common.js +168 -0
  19. package/data/loadtest/custom.js +20 -0
  20. package/data/loadtest/journey.js +58 -0
  21. package/data/loadtest/login.js +18 -0
  22. package/data/loadtest/messages.js +13 -0
  23. package/data/loadtest/mixed.js +39 -0
  24. package/data/loadtest/read.js +12 -0
  25. package/data/loadtest/webhook.js +20 -0
  26. package/data/monitoring/houston-logs.json +98 -0
  27. package/data/monitoring/k6-loadtest.json +610 -0
  28. package/data/monitoring/mongodb-exporter.json +3007 -0
  29. package/data/monitoring/node-exporter-full.json +16114 -0
  30. package/data/monitoring/rocketchat-metrics.json +6471 -0
  31. package/data/presets/airgapped.yaml +16 -0
  32. package/data/presets/default.yaml +9 -0
  33. package/data/seed/avatar-0.png +0 -0
  34. package/data/seed/avatar-1.png +0 -0
  35. package/data/seed/avatar-2.png +0 -0
  36. package/data/seed/avatar-3.png +0 -0
  37. package/data/seed/avatar-4.png +0 -0
  38. package/data/seed/avatar-5.png +0 -0
  39. package/data/seed/avatar-6.png +0 -0
  40. package/data/seed/avatar-7.png +0 -0
  41. package/data/seed/seed-metrics.csv +3 -0
  42. package/data/seed/seed-preview.png +0 -0
  43. package/data/seed/seed-report.pdf +32 -0
  44. package/data/seed/seed-runbook.txt +3 -0
  45. package/data/skill/SKILL.md +399 -0
  46. package/data/versions.yaml +55 -0
  47. package/data/voice/freeswitch/drachtio.xml +52 -0
  48. package/data/voice/freeswitch/public.xml +87 -0
  49. package/data/webui/app.css +2003 -0
  50. package/data/webui/index.html +678 -0
  51. package/data/webui/js/api.js +561 -0
  52. package/data/webui/js/boot.js +44 -0
  53. package/data/webui/js/clusters-tail.js +1377 -0
  54. package/data/webui/js/clusters.js +896 -0
  55. package/data/webui/js/create.js +276 -0
  56. package/data/webui/js/detail.js +507 -0
  57. package/data/webui/js/jobs.js +636 -0
  58. package/data/webui/js/logs.js +634 -0
  59. package/data/webui/js/main.js +14 -0
  60. package/data/webui/js/options.js +408 -0
  61. package/data/webui/js/people.js +438 -0
  62. package/data/webui/js/perf.js +303 -0
  63. package/data/webui/js/render.js +438 -0
  64. package/data/webui/js/shared.js +3 -0
  65. package/data/webui/js/state.js +583 -0
  66. package/data/webui/js/theme.js +123 -0
  67. package/data/webui/js/util.js +584 -0
  68. package/dist/cli/app.js +593 -0
  69. package/dist/cli/capabilities.js +76 -0
  70. package/dist/cli/cell-widths.js +495 -0
  71. package/dist/cli/entry.js +10 -0
  72. package/dist/cli/error-panel.js +492 -0
  73. package/dist/cli/handlers/actor.js +84 -0
  74. package/dist/cli/handlers/https.js +614 -0
  75. package/dist/cli/handlers/prompt.js +299 -0
  76. package/dist/cli/handlers/users.js +226 -0
  77. package/dist/cli/handlers.js +3678 -0
  78. package/dist/cli/help.js +183 -0
  79. package/dist/cli/index.js +15 -0
  80. package/dist/cli/perf.js +810 -0
  81. package/dist/cli/render.js +254 -0
  82. package/dist/cli/serve.js +858 -0
  83. package/dist/cli/spec-data.js +2593 -0
  84. package/dist/cli/spec-types.js +8 -0
  85. package/dist/cli/usage-error.js +520 -0
  86. package/dist/cli/wire.js +70 -0
  87. package/dist/core/audit.js +346 -0
  88. package/dist/core/backup-compat.js +85 -0
  89. package/dist/core/backup.js +1639 -0
  90. package/dist/core/cluster.js +1398 -0
  91. package/dist/core/configimport.js +344 -0
  92. package/dist/core/data.js +267 -0
  93. package/dist/core/database-of.js +82 -0
  94. package/dist/core/diagnose.js +159 -0
  95. package/dist/core/doctor.js +1518 -0
  96. package/dist/core/edge/acme.js +338 -0
  97. package/dist/core/edge/engine.js +500 -0
  98. package/dist/core/edge/networks.js +174 -0
  99. package/dist/core/edge/paths.js +107 -0
  100. package/dist/core/edge/routes.js +251 -0
  101. package/dist/core/edge/routeyml.js +195 -0
  102. package/dist/core/edge/runtime.js +493 -0
  103. package/dist/core/edge/spec.js +305 -0
  104. package/dist/core/edge/types.js +202 -0
  105. package/dist/core/eks-docs.js +135 -0
  106. package/dist/core/eks.js +2240 -0
  107. package/dist/core/envvars.js +562 -0
  108. package/dist/core/eventlog.js +541 -0
  109. package/dist/core/firstrun.js +153 -0
  110. package/dist/core/journal.js +691 -0
  111. package/dist/core/kube/ask.js +231 -0
  112. package/dist/core/kube/capabilities.js +269 -0
  113. package/dist/core/kube/certmanager.js +452 -0
  114. package/dist/core/kube/chart.js +413 -0
  115. package/dist/core/kube/cluster.js +196 -0
  116. package/dist/core/kube/context.js +545 -0
  117. package/dist/core/kube/endpoints.js +448 -0
  118. package/dist/core/kube/exec.js +195 -0
  119. package/dist/core/kube/forward.js +732 -0
  120. package/dist/core/kube/grafana-ingress.js +110 -0
  121. package/dist/core/kube/ingress.js +218 -0
  122. package/dist/core/kube/manifests.js +361 -0
  123. package/dist/core/kube/monitoring.js +768 -0
  124. package/dist/core/kube/names.js +237 -0
  125. package/dist/core/kube/namespace.js +678 -0
  126. package/dist/core/kube/operator.js +941 -0
  127. package/dist/core/kube/probe.js +202 -0
  128. package/dist/core/kube/reachability.js +163 -0
  129. package/dist/core/kube/release.js +439 -0
  130. package/dist/core/kube/replicaset.js +180 -0
  131. package/dist/core/kube/scale.js +398 -0
  132. package/dist/core/kube/status.js +633 -0
  133. package/dist/core/kube/tools.js +238 -0
  134. package/dist/core/kube/transfer.js +173 -0
  135. package/dist/core/kube/workspace.js +423 -0
  136. package/dist/core/lifecycle/admin.js +64 -0
  137. package/dist/core/lifecycle/create-docker.js +1533 -0
  138. package/dist/core/lifecycle/create-k8s.js +939 -0
  139. package/dist/core/lifecycle/destroy.js +339 -0
  140. package/dist/core/lifecycle/inspect.js +635 -0
  141. package/dist/core/lifecycle/naming.js +298 -0
  142. package/dist/core/lifecycle/notes.js +726 -0
  143. package/dist/core/lifecycle/ownership.js +313 -0
  144. package/dist/core/lifecycle/payload.js +401 -0
  145. package/dist/core/lifecycle/ports.js +388 -0
  146. package/dist/core/lifecycle/preconditions.js +367 -0
  147. package/dist/core/lifecycle/pruning.js +551 -0
  148. package/dist/core/lifecycle/ready.js +753 -0
  149. package/dist/core/lifecycle/request.js +525 -0
  150. package/dist/core/lifecycle/seeding.js +281 -0
  151. package/dist/core/lifecycle/sizing.js +392 -0
  152. package/dist/core/lifecycle/state.js +286 -0
  153. package/dist/core/lifecycle/tlsplan.js +704 -0
  154. package/dist/core/lifecycle/types.js +232 -0
  155. package/dist/core/loadtest.js +995 -0
  156. package/dist/core/migrate.js +612 -0
  157. package/dist/core/monitor.js +494 -0
  158. package/dist/core/postready.js +969 -0
  159. package/dist/core/promote.js +804 -0
  160. package/dist/core/rcapi.js +1285 -0
  161. package/dist/core/recovery.js +75 -0
  162. package/dist/core/runner/engine.js +394 -0
  163. package/dist/core/runner/host.js +298 -0
  164. package/dist/core/runner/locks.js +129 -0
  165. package/dist/core/runner/paths.js +173 -0
  166. package/dist/core/runner/ports.js +172 -0
  167. package/dist/core/runner/queries.js +612 -0
  168. package/dist/core/runner/record.js +563 -0
  169. package/dist/core/scaleseed.js +271 -0
  170. package/dist/core/seed-db.js +208 -0
  171. package/dist/core/sessions.js +472 -0
  172. package/dist/core/skill.js +326 -0
  173. package/dist/core/sso-identities.js +172 -0
  174. package/dist/core/sso.js +703 -0
  175. package/dist/core/tls-local.js +311 -0
  176. package/dist/core/tls.js +878 -0
  177. package/dist/core/topology.js +482 -0
  178. package/dist/core/upgrade.js +868 -0
  179. package/dist/core/users.js +739 -0
  180. package/dist/lib/audit.js +100 -0
  181. package/dist/lib/close-matches.js +238 -0
  182. package/dist/lib/compose.js +672 -0
  183. package/dist/lib/config.js +1398 -0
  184. package/dist/lib/context.js +18 -0
  185. package/dist/lib/errors.js +305 -0
  186. package/dist/lib/events.js +208 -0
  187. package/dist/lib/float-text.js +126 -0
  188. package/dist/lib/format.js +1320 -0
  189. package/dist/lib/home.js +325 -0
  190. package/dist/lib/journal.js +122 -0
  191. package/dist/lib/json-text.js +733 -0
  192. package/dist/lib/jsonout.js +382 -0
  193. package/dist/lib/lock.js +516 -0
  194. package/dist/lib/login-name.js +202 -0
  195. package/dist/lib/map-limit.js +29 -0
  196. package/dist/lib/mkdir-parents.js +90 -0
  197. package/dist/lib/mongo-kernel.js +67 -0
  198. package/dist/lib/monitoring.js +492 -0
  199. package/dist/lib/platform-name.js +59 -0
  200. package/dist/lib/presets/common.js +390 -0
  201. package/dist/lib/presets/composite.js +326 -0
  202. package/dist/lib/presets/e2ee.js +143 -0
  203. package/dist/lib/presets/email.js +172 -0
  204. package/dist/lib/presets/guides.js +27 -0
  205. package/dist/lib/presets/index.js +490 -0
  206. package/dist/lib/presets/keycloak.js +369 -0
  207. package/dist/lib/presets/ldap.js +1285 -0
  208. package/dist/lib/presets/livechat.js +220 -0
  209. package/dist/lib/presets/multi-instance.js +124 -0
  210. package/dist/lib/presets/oidc.js +161 -0
  211. package/dist/lib/presets/public.js +496 -0
  212. package/dist/lib/presets/registry.js +64 -0
  213. package/dist/lib/presets/s3-minio.js +300 -0
  214. package/dist/lib/presets/saml.js +149 -0
  215. package/dist/lib/presets/scenario.js +51 -0
  216. package/dist/lib/presets/types.js +86 -0
  217. package/dist/lib/presets/voice-pbx.js +320 -0
  218. package/dist/lib/presets/voice.js +345 -0
  219. package/dist/lib/presets/webhooks.js +182 -0
  220. package/dist/lib/redact.js +106 -0
  221. package/dist/lib/seed-options.js +68 -0
  222. package/dist/lib/seed-profiles.js +59 -0
  223. package/dist/lib/targz.js +1383 -0
  224. package/dist/lib/teammode.js +25 -0
  225. package/dist/lib/tempdir.js +162 -0
  226. package/dist/lib/unicode-tables.js +2335 -0
  227. package/dist/lib/version-spec.js +357 -0
  228. package/dist/lib/versions.js +463 -0
  229. package/dist/lib/yaml-dump.js +1361 -0
  230. package/dist/perf/baseline.js +168 -0
  231. package/dist/perf/constrain.js +296 -0
  232. package/dist/perf/k6.js +235 -0
  233. package/dist/perf/mongoprof.js +171 -0
  234. package/dist/perf/rcmetrics.js +178 -0
  235. package/dist/perf/report.js +386 -0
  236. package/dist/perf/resources.js +224 -0
  237. package/dist/perf/slo.js +111 -0
  238. package/dist/perf/timeline.js +187 -0
  239. package/dist/perf/timings.js +85 -0
  240. package/dist/perf/verdict.js +201 -0
  241. package/dist/seams/aws-cli.js +126 -0
  242. package/dist/seams/docker-cli.js +280 -0
  243. package/dist/seams/exec.js +861 -0
  244. package/dist/seams/fs/lock.js +528 -0
  245. package/dist/seams/kubectl.js +83 -0
  246. package/dist/seams/rc-http.js +921 -0
  247. package/dist/seed/apply.js +190 -0
  248. package/dist/seed/assets.js +76 -0
  249. package/dist/seed/check.js +391 -0
  250. package/dist/seed/client.js +172 -0
  251. package/dist/seed/executor.js +726 -0
  252. package/dist/seed/history.js +281 -0
  253. package/dist/seed/kinds.js +91 -0
  254. package/dist/seed/plan.js +432 -0
  255. package/dist/seed/receipt.js +339 -0
  256. package/dist/seed/repairs.js +75 -0
  257. package/dist/seed/verification.js +363 -0
  258. package/dist/seed/wording.js +248 -0
  259. package/dist/web/app.js +326 -0
  260. package/dist/web/jobs.js +510 -0
  261. package/dist/web/route-deps.js +211 -0
  262. package/dist/web/route-table.js +213 -0
  263. package/dist/web/routes/backup.js +244 -0
  264. package/dist/web/routes/clusters.js +303 -0
  265. package/dist/web/routes/data.js +187 -0
  266. package/dist/web/routes/edge.js +82 -0
  267. package/dist/web/routes/index.js +63 -0
  268. package/dist/web/routes/jobs.js +95 -0
  269. package/dist/web/routes/logs.js +297 -0
  270. package/dist/web/routes/meta.js +191 -0
  271. package/dist/web/routes/people.js +151 -0
  272. package/dist/web/routes/perf.js +95 -0
  273. package/dist/web/routes/repros.js +550 -0
  274. package/dist/web/routes/session.js +361 -0
  275. package/dist/web/routes/spa.js +198 -0
  276. package/dist/web/security.js +475 -0
  277. package/dist/web/signin.js +246 -0
  278. package/package.json +67 -32
  279. package/.eslintrc +0 -94
  280. package/.swcrc +0 -18
  281. package/dist/bin.js +0 -3
  282. package/dist/cli.js +0 -184
  283. package/dist/commands/createBranch.js +0 -194
  284. package/dist/outputCI.js +0 -59
  285. package/src/bin.js +0 -4
  286. package/src/cli.ts +0 -25
  287. package/src/commands/createBranch.ts +0 -28
  288. package/src/outputCI.ts +0 -12
  289. package/src-old/logs.js +0 -323
  290. package/src-old/md.js +0 -191
  291. package/src-old/release-table.js +0 -60
  292. package/src-old/set-version.js +0 -614
  293. package/src-old/utils.js +0 -15
@@ -0,0 +1,493 @@
1
+ "use strict";
2
+ var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
3
+ if (k2 === undefined) k2 = k;
4
+ var desc = Object.getOwnPropertyDescriptor(m, k);
5
+ if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
6
+ desc = { enumerable: true, get: function() { return m[k]; } };
7
+ }
8
+ Object.defineProperty(o, k2, desc);
9
+ }) : (function(o, m, k, k2) {
10
+ if (k2 === undefined) k2 = k;
11
+ o[k2] = m[k];
12
+ }));
13
+ var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
14
+ Object.defineProperty(o, "default", { enumerable: true, value: v });
15
+ }) : function(o, v) {
16
+ o["default"] = v;
17
+ });
18
+ var __importStar = (this && this.__importStar) || (function () {
19
+ var ownKeys = function(o) {
20
+ ownKeys = Object.getOwnPropertyNames || function (o) {
21
+ var ar = [];
22
+ for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k;
23
+ return ar;
24
+ };
25
+ return ownKeys(o);
26
+ };
27
+ return function (mod) {
28
+ if (mod && mod.__esModule) return mod;
29
+ var result = {};
30
+ if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]);
31
+ __setModuleDefault(result, mod);
32
+ return result;
33
+ };
34
+ })();
35
+ Object.defineProperty(exports, "__esModule", { value: true });
36
+ exports.adopt = adopt;
37
+ exports.systemdUnit = systemdUnit;
38
+ exports.challengeStale = challengeStale;
39
+ exports.up = up;
40
+ exports.ensureRunning = ensureRunning;
41
+ exports.deleteHost = deleteHost;
42
+ exports.logs = logs;
43
+ exports.status = status;
44
+ /**
45
+ * Starting, stopping, adopting and forgetting. Tier 6 of `src/core/edge/`.
46
+ *
47
+ * Nothing in the package imports this, which is what being the top of the tiers means.
48
+ */
49
+ const fs = __importStar(require("node:fs"));
50
+ const node_os_1 = require("node:os");
51
+ const path = __importStar(require("node:path"));
52
+ const compose_1 = require("../../lib/compose");
53
+ const errors_1 = require("../../lib/errors");
54
+ const home_1 = require("../../lib/home");
55
+ const format_1 = require("../../lib/format");
56
+ const json_text_1 = require("../../lib/json-text");
57
+ const docker_cli_1 = require("../../seams/docker-cli");
58
+ const exec_1 = require("../../seams/exec");
59
+ const paths_1 = require("../runner/paths");
60
+ const record_1 = require("../runner/record");
61
+ const tls_1 = require("../tls");
62
+ const acme_1 = require("./acme");
63
+ const engine_1 = require("./engine");
64
+ const networks_1 = require("./networks");
65
+ const paths = __importStar(require("./paths"));
66
+ const edgeRoutes = __importStar(require("./routes"));
67
+ const edgeSpec = __importStar(require("./spec"));
68
+ const types_1 = require("./types");
69
+ /**
70
+ * Move a workspace that runs its OWN Traefik onto the edge, without recreating it.
71
+ *
72
+ * Four steps, none of which stops Rocket.Chat or MongoDB:
73
+ *
74
+ * 1. remove ONLY the workspace's traefik container -- this is what frees 443
75
+ * 2. attach the edge to the workspace's network, live
76
+ * 3. write the route
77
+ * 4. rewrite the compose file so a future `up` matches reality
78
+ *
79
+ * Step 4 is a file write and is NOT applied: compose files take effect on the next `up`,
80
+ * so nothing restarts now. That is the difference from the first version, which called
81
+ * `up` here and therefore recreated every container -- minutes of downtime per workspace,
82
+ * and a rebuild is where an incompatibility or a lost volume would come from. Twenty
83
+ * workspaces is now twenty seconds.
84
+ */
85
+ async function adopt(name) {
86
+ return await paths.edgeLock(async () => await adoptLocked(name));
87
+ }
88
+ async function adoptLocked(name) {
89
+ const meta = await (0, record_1.readMeta)(name);
90
+ // `orderedDict(extra)`: a copy that writes back in the record's key order and spelling, where a
91
+ // spread is a new object in JavaScript's order.
92
+ const extra = (0, json_text_1.orderedDict)(meta.extra);
93
+ const host = (meta.publicUrl || '').split('://').slice(-1)[0]?.split('/')[0] ?? '';
94
+ if (extra.tls !== tls_1.MODE_ACME || host === '') {
95
+ throw new errors_1.ValidationError(`'${name}' is not a --domain workspace, so there is nothing to move onto the edge`);
96
+ }
97
+ // 1. Its Traefik, and only its Traefik. `docker rm -f` on one container leaves every
98
+ // other service in the project running.
99
+ await (0, engine_1.dockerRun)(['rm', '-f', await (0, networks_1.backendContainer)(name, tls_1.SERVICE)]);
100
+ // 2. Live attach -- no restart of anything. Only if the edge exists yet: adoption
101
+ // usually runs BEFORE it starts, because freeing 443 is the whole reason it could
102
+ // not. `reattachAll()` picks these up after the start.
103
+ if (await (0, engine_1.running)())
104
+ await (0, networks_1.attach)(name);
105
+ // 3 + 4.
106
+ const instances = typeof extra.instances === 'number' ? extra.instances : 1;
107
+ const doc = await (0, record_1.readCompose)(name);
108
+ const services = doc.services;
109
+ if (services !== null && typeof services === 'object') {
110
+ delete services[tls_1.SERVICE];
111
+ }
112
+ extra.edge = true;
113
+ // It no longer publishes 443, and leaving the claim would make every later workspace
114
+ // allocate around a port this one does not hold.
115
+ extra.tls_ports = [];
116
+ await (0, record_1.write)(name, (0, compose_1.toYaml)(doc), { ...meta, extra });
117
+ await edgeRoutes.register(name, host, { instances });
118
+ return { name, host };
119
+ }
120
+ /** What `systemdUnit` copies from the shell that printed it, in this order. */
121
+ const SERVICE_ENVIRONMENT = [
122
+ 'PATH',
123
+ 'HOUSTON_HOME',
124
+ 'AWS_PROFILE',
125
+ 'AWS_REGION',
126
+ 'AWS_DEFAULT_REGION',
127
+ ];
128
+ /**
129
+ * The unit that keeps `houston serve` up.
130
+ *
131
+ * Printed for the operator to install, never written by houston: writing it needs root,
132
+ * and a tool that edits /etc/systemd on your behalf is a bigger thing to trust than one
133
+ * that shows you the file. It also keeps this testable as a pure string, with no
134
+ * privileged CI.
135
+ *
136
+ * `Restart=always` is only safe BECAUSE named accounts exist: with the old session token
137
+ * every restart minted a new one and killed every bookmark.
138
+ *
139
+ * It carries the PATH of the shell that printed it. systemd starts a unit with its own
140
+ * bare PATH, so under nvm `bin/houston`'s `exec node` failed with "node: not found"
141
+ * (status 127) and the unit restarted forever; docker, aws and kubectl are found the same
142
+ * way. AWS_PROFILE and the region go with it when set, or the GUI's EKS pages ask AWS
143
+ * with the default profile rather than the one the operator signed in with.
144
+ */
145
+ function systemdUnit(execStart, user, environment = process.env) {
146
+ const lines = SERVICE_ENVIRONMENT.flatMap((key) => {
147
+ // HOUSTON_HOME too, ABSOLUTE: a unit printed from a non-default home served `User=`'s
148
+ // `~/.houston` -- other accounts, other workspaces -- and systemd starts it from `/`.
149
+ const given = environment[key] ?? '';
150
+ const value = key === 'HOUSTON_HOME' && given !== '' ? path.resolve(given) : given;
151
+ // Inside quotes systemd reads C escapes, and `%` starts a specifier.
152
+ const quoted = value.replace(/[\\"]/gu, '\\$&').replaceAll('%', '%%');
153
+ return value === '' ? [] : [`Environment="${key}=${quoted}"\n`];
154
+ });
155
+ return ('[Unit]\n' +
156
+ 'Description=houston GUI\n' +
157
+ 'After=docker.service\n' +
158
+ 'Requires=docker.service\n' +
159
+ '\n' +
160
+ '[Service]\n' +
161
+ `User=${user}\n` +
162
+ lines.join('') +
163
+ `ExecStart=${execStart}\n` +
164
+ 'Restart=always\n' +
165
+ // Without a pause systemd restarts within 100ms, and five starts in ten seconds stop it
166
+ // for good ("Start request repeated too quickly"). `serve --domain` exits while the edge
167
+ // cannot start, so one broken edge left the production GUI down after the edge was fixed.
168
+ 'RestartSec=10\n' +
169
+ '\n' +
170
+ '[Install]\n' +
171
+ 'WantedBy=multi-user.target\n');
172
+ }
173
+ /**
174
+ * Repair stale GUI state and materialise the requested edge configuration.
175
+ *
176
+ * `acmeEmail` MATTERS. A bare edge declares no resolver -- right for `up --https`, which
177
+ * only ever serves local-CA names -- and a `--domain` workspace registered against it
178
+ * produced routes Traefik rejected with "Router uses a nonexistent certificate resolver:
179
+ * le", so the name 404'd while looking perfectly configured from the outside. The first
180
+ * workspace that needs Let's Encrypt therefore teaches the edge how to ask.
181
+ */
182
+ /** Whether the edge asks Let's Encrypt with a challenge `acme.challenge` no longer names. */
183
+ function challengeStale() {
184
+ const now = edgeSpec.current();
185
+ if (now === null || !(0, acme_1.hasAcme)())
186
+ return false;
187
+ return (0, types_1.resolveEdge)(now.domain, now.acmeEmail).acmeChallenge !== now.acmeChallenge;
188
+ }
189
+ async function prepareEdge(acmeEmail, acmeStaging) {
190
+ // Repair a GUI route left by an older houston. That version wrote one even with no
191
+ // domain, producing ``Host(``)`` -- which Traefik rejects at every reload with "empty
192
+ // args for matcher Host", plus a certResolver a bare edge never declares. Not writing it
193
+ // any more does nothing for the edges that already have it, and nobody would think to
194
+ // look in a generated directory.
195
+ const gui = paths.routePath('_gui');
196
+ if (edgeSpec.servedDomain() === '' && fs.existsSync(gui))
197
+ fs.rmSync(gui, { force: true });
198
+ const wantAcme = acmeEmail !== '';
199
+ if (!edgeSpec.installed()) {
200
+ // Through `resolveEdge`, as the upgrade below and `serve` go: a bare `edge(...)` took the
201
+ // default challenge, so the first edge `up` wrote ignored `acme.challenge` and the dns-01
202
+ // credentials, and behind a tunnel asked for TLS-ALPN, which never validated.
203
+ await edgeSpec.write(wantAcme ? (0, types_1.resolveEdge)('', acmeEmail, { acmeStaging }) : (0, types_1.edge)());
204
+ }
205
+ else if (wantAcme && !(0, acme_1.hasAcme)()) {
206
+ // Upgrade in place: keep whatever domain `serve` gave it, add the resolver.
207
+ await edgeSpec.write((0, types_1.resolveEdge)(edgeSpec.servedDomain(), acmeEmail, { acmeStaging }));
208
+ await (0, engine_1.down)(); // so the new flags are actually applied
209
+ }
210
+ else if (wantAcme && challengeStale()) {
211
+ // A CHANGED `acme.challenge` reached the container only through `edge restart`: `up
212
+ // --domain` rebuilt the edge from the spec it was made with, so behind a tunnel (where
213
+ // only http-01 can validate) the next certificate failed on TLS-ALPN again, measured.
214
+ // Re-resolved as `edge start` does; staging as this request asks (config included), as
215
+ // the two branches above build it.
216
+ const now = edgeSpec.current();
217
+ if (now !== null) {
218
+ await edgeSpec.write((0, types_1.resolveEdge)(now.domain, now.acmeEmail, {
219
+ acmeStaging,
220
+ guiHost: now.guiHost,
221
+ guiPort: now.guiPort,
222
+ httpPort: now.httpPort,
223
+ httpsPort: now.httpsPort,
224
+ }));
225
+ await (0, engine_1.down)();
226
+ }
227
+ }
228
+ // A container of ours that is UP but holds no ports cannot be repaired by the `compose
229
+ // up -d` below: its resolved configuration has not changed, so compose leaves it exactly
230
+ // as it is and reports success. Remove it first, so what follows is a real attempt that
231
+ // either takes the ports or fails naming whoever has them.
232
+ const stalled = await (0, engine_1.ourContainer)();
233
+ if (stalled !== '' && !(await (0, engine_1.publishesPorts)(stalled)))
234
+ await (0, engine_1.down)();
235
+ }
236
+ /** Move every compatible pre-edge workspace off the shared HTTPS port. */
237
+ async function adoptLegacyPortHolders() {
238
+ // Workspaces from before the edge run their own Traefik on 443, which is exactly why the
239
+ // edge cannot start. Moving them across is now instant and touches no data, so it
240
+ // happens automatically rather than behind a flag -- the flag existed when doing this
241
+ // meant recreating every container, and asking permission for something expensive is
242
+ // very different from asking permission for removing one proxy container.
243
+ for (const held of await (0, engine_1.holdersOf443)()) {
244
+ try {
245
+ await adopt(held);
246
+ }
247
+ catch (error) {
248
+ // One workspace that will not move must not stop the others, or the edge. Whatever
249
+ // still holds the port is named by `portHolder()`.
250
+ if (!(error instanceof errors_1.ReproError) && !(error instanceof Error))
251
+ throw error;
252
+ continue;
253
+ }
254
+ }
255
+ }
256
+ /**
257
+ * Start the edge, and return what docker said. No network to create: it joins each
258
+ * workspace's own.
259
+ *
260
+ * Returns the whole result rather than its return code, because the return code was all
261
+ * the callers ever got and a failed start is precisely the moment the output matters.
262
+ */
263
+ async function up(options = {}) {
264
+ return await paths.edgeLock(async () => await upLocked(options));
265
+ }
266
+ async function upLocked({ pull = true } = {}) {
267
+ const other = await (0, engine_1.foreignEdge)();
268
+ if (other !== '') {
269
+ // Refuse rather than replace. `compose up` would happily take the other home's
270
+ // container, and the first anyone would know is every one of its names going dark.
271
+ throw new errors_1.ConflictError('another houston edge is already running on this machine, from a different home:\n' +
272
+ ` ${other}\n` +
273
+ ' Starting this one would REPLACE it and take down every https name it serves.\n' +
274
+ ' houston is designed around one shared home per box — set HOUSTON_HOME to the ' +
275
+ 'same\n directory as the other user, or stop that edge first:\n' +
276
+ ` HOUSTON_HOME=${path.dirname(other)} houston edge stop`);
277
+ }
278
+ if (pull)
279
+ await (0, engine_1.composeRun)(['pull']); // non-fatal, exactly as `runner.up` does it
280
+ let res = await (0, engine_1.composeRun)(['up', '-d', '--remove-orphans']);
281
+ // A stopped edge restarts as the same container, so on the same systemd unit, and a unit
282
+ // its last run left loaded fails every start (`STALE_UNIT`). Its certificates and routes
283
+ // are bind mounts, so a recreated edge loses nothing.
284
+ if (res.returncode !== 0) {
285
+ const state = await (0, engine_1.dockerRun)(['inspect', paths.CONTAINER, '--format', '{{.State.Error}}']);
286
+ if (state.returncode === 0 && docker_cli_1.STALE_UNIT.test(state.stdout)) {
287
+ res = await (0, engine_1.composeRun)(['up', '-d', '--remove-orphans', '--force-recreate']);
288
+ }
289
+ }
290
+ if (res.returncode === 0) {
291
+ // Every start, not just the first: a recreated container has none of its previous
292
+ // attachments, and the route files that outlived it would then all answer 502.
293
+ await edgeRoutes.reattachAll();
294
+ }
295
+ return res;
296
+ }
297
+ /**
298
+ * Start the edge lazily, on the first route that needs it.
299
+ *
300
+ * Never at install and never on plain `serve`: a laptop that never asks for a name never
301
+ * learns this exists. Workspace and GUI HTTPS remain unrelated.
302
+ */
303
+ async function ensureRunning(options = {}) {
304
+ // Preparation and compose must see one generation. Locking only the final `up`
305
+ // still lets a concurrent bare start replace a newly written ACME resolver.
306
+ return await paths.edgeLock(async () => await ensureRunningLocked(options));
307
+ }
308
+ async function ensureRunningLocked({ acmeEmail = '', acmeStaging = false, } = {}) {
309
+ await prepareEdge(acmeEmail, acmeStaging);
310
+ if (await (0, engine_1.running)()) {
311
+ // RUNNING IS NOT UP TO DATE. This returned true here, and the caller had already
312
+ // rewritten the compose file -- `serve --domain X --email Y` calls `write(door)` first
313
+ // -- so a changed resolver email, a changed domain or any new flag was written to disk
314
+ // and never applied. The container kept the command line it was created with, `serve`
315
+ // reported success, and nothing said the two disagreed. Found on a box whose
316
+ // `acme.json` held an account for a MIStyped address while the corrected one was being
317
+ // passed on every restart.
318
+ //
319
+ // `docker compose up -d` is the right instrument: it is idempotent and recreates the
320
+ // container ONLY if the resolved configuration changed, so this costs one compose call
321
+ // when nothing has and does the necessary thing when something has. The adoption loop
322
+ // below is skipped -- nothing else can be holding :443 while this edge has it.
323
+ const res = await up({ pull: false });
324
+ return res.returncode === 0 ? (0, types_1.startResult)(true) : (0, types_1.startResult)(false, (0, engine_1.why)(res));
325
+ }
326
+ await adoptLegacyPortHolders();
327
+ const res = await up({ pull: false });
328
+ return res.returncode === 0 ? (0, types_1.startResult)(true) : (0, types_1.startResult)(false, (0, engine_1.why)(res));
329
+ }
330
+ /** Validate a destructive hostname request and return its claimed routes. */
331
+ async function validateHostDeletion(host, confirm) {
332
+ const wanted = (host || '').trim().replace(/\.+$/u, '');
333
+ if (wanted === '') {
334
+ throw new errors_1.ValidationError('name a hostname to delete: `houston edge delete <host>`. `houston edge status` ' +
335
+ 'lists them.');
336
+ }
337
+ const gui = edgeSpec.servedDomain();
338
+ if (gui !== '' && (0, format_1.foldCase)(gui) === (0, format_1.foldCase)(wanted)) {
339
+ throw new errors_1.ConflictError(`${wanted} is the GUI's own name, so deleting it would take \`houston serve\` off ` +
340
+ 'HTTPS while you are probably using it. Stop the edge (`houston edge stop`) if ' +
341
+ 'that is really what you want.');
342
+ }
343
+ const routes = edgeRoutes.routesFor(wanted);
344
+ const live = [];
345
+ for (const name of routes) {
346
+ if (await (0, paths_1.exists)(name))
347
+ live.push(name);
348
+ }
349
+ if (live.length > 0) {
350
+ throw new errors_1.ConflictError(`${wanted} still belongs to workspace '${live.join(', ')}' — deleting its ` +
351
+ 'certificate would leave a workspace that cannot serve its own name. `houston ' +
352
+ `down --name ${String(live[0])} --volumes\` first, then delete the name.`);
353
+ }
354
+ if (!confirm) {
355
+ throw new errors_1.ValidationError(`deleting ${wanted} removes its certificate and PRIVATE KEY and cannot be undone - ` +
356
+ 'pass confirm=true');
357
+ }
358
+ return [wanted, routes];
359
+ }
360
+ /** Delete route, certificate, declaration, and ACME state for one hostname. */
361
+ async function deleteHostFiles(host, routes) {
362
+ // Passthrough first: the answer for a Kubernetes name is genuinely different, and saying
363
+ // "certificate removed" about one that was never here would be a lie in the shape this
364
+ // codebase keeps finding -- reporting the attempt as the outcome.
365
+ const through = routes.filter((name) => edgeRoutes.isPassthrough(name));
366
+ for (const name of routes)
367
+ edgeRoutes.deregister(name);
368
+ const crt = (0, home_1.joinPath)(paths.certsDir(), `${host}.crt`);
369
+ const key = (0, home_1.joinPath)(paths.certsDir(), `${host}.key`);
370
+ const hadCert = fs.existsSync(crt) || fs.existsSync(key);
371
+ fs.rmSync(crt, { force: true });
372
+ fs.rmSync(key, { force: true });
373
+ const decl = (0, home_1.joinPath)(paths.dynamicDir(), `_cert-${host}.yml`);
374
+ const hadDecl = fs.existsSync(decl);
375
+ fs.rmSync(decl, { force: true });
376
+ return [through, hadCert, hadDecl, await (0, acme_1.forgetAcme)(host)];
377
+ }
378
+ /** Refresh our edge's in-memory ACME store, or identify a foreign edge. */
379
+ async function restartAfterAcmeDelete(stores, restart) {
380
+ let foreign = '';
381
+ let restarted = null;
382
+ if (stores.length > 0 && restart) {
383
+ if (await (0, engine_1.running)()) {
384
+ restarted = (await (0, engine_1.down)()) === 0 && (await up({ pull: false })).returncode === 0;
385
+ }
386
+ else {
387
+ foreign = await (0, engine_1.foreignEdge)();
388
+ }
389
+ }
390
+ return [foreign, restarted];
391
+ }
392
+ /**
393
+ * Forget `host` entirely: its routes, certificate, key, and ACME entry.
394
+ *
395
+ * Returns what actually went, per kind, so the caller reports the deletion it made instead
396
+ * of the one it attempted.
397
+ *
398
+ * Three refusals, and each one is a case where deleting would break something the caller
399
+ * can still see:
400
+ *
401
+ * * a LIVE workspace still claims the name -- `down` it first, named in the error. A
402
+ * route whose workspace record is gone is the orphan this command exists for, so that
403
+ * one proceeds.
404
+ * * the name is the GUI's own -- deleting it takes `serve` off the network the operator
405
+ * is most likely using to run this.
406
+ * * `confirm` is not set. A private key is not recoverable and this is the only gate.
407
+ *
408
+ * `restart` is the part that is easy to get wrong. Traefik holds its ACME store in memory
409
+ * and persists the whole file when a certificate changes, so an edit made underneath a
410
+ * running Traefik can be written back from memory later -- the entry reappears, hours
411
+ * after a command said it was deleted. Restarting rebuilds that memory from the edited
412
+ * file. Routes are files, so every other name comes back with no re-registration; that
413
+ * property is why a restart is affordable here.
414
+ */
415
+ async function deleteHost(host, options = {}) {
416
+ return await paths.edgeLock(async () => await deleteHostLocked(host, options));
417
+ }
418
+ async function deleteHostLocked(host, { confirm = false, restart = true } = {}) {
419
+ const [wanted, routes] = await validateHostDeletion(host, confirm);
420
+ const [through, hadCert, hadDecl, stores] = await deleteHostFiles(wanted, routes);
421
+ // Docker is asked ONLY for an ACME deletion. A route or a local certificate is read from
422
+ // disk every time, so removing the file is already in effect; making a file deletion
423
+ // depend on a daemon to decide it need not restart would be a cost with nothing bought.
424
+ //
425
+ // `running()` is HOME-AWARE -- it matches the compose project label AND the working
426
+ // directory -- so `down()` here cannot reach an edge belonging to a different
427
+ // HOUSTON_HOME, which every home naming its project `houston_edge` would otherwise make
428
+ // trivially possible. That is not hypothetical: doing it by hand cost this box's owner
429
+ // their edge once, following an error message that did not know about it either.
430
+ //
431
+ // Which leaves the case worth REPORTING rather than guarding: ours is not running and
432
+ // somebody else's is. A foreign edge mounts its own home's ACME store, so the
433
+ // certificate just deleted was this home's copy and NOT the one being served -- and a
434
+ // plain "removed" would read as a name that stopped resolving.
435
+ const [foreign, restarted] = await restartAfterAcmeDelete(stores, restart);
436
+ return {
437
+ host: wanted,
438
+ routes: [...routes],
439
+ passthrough: through,
440
+ certificate: hadCert,
441
+ declaration: hadDecl,
442
+ acme: stores,
443
+ restarted,
444
+ foreign,
445
+ kept: fs.existsSync((0, tls_1.issuedPath)()) ? [`acme/${tls_1.ISSUED_FILE}`] : [],
446
+ };
447
+ }
448
+ /**
449
+ * Stream or dump the edge's own log, and return the exit code.
450
+ *
451
+ * The edge is where HTTPS actually succeeds or fails, and until now there was no way to
452
+ * read it from houston: `edge` offered status/start/stop/restart, so the answer to "my
453
+ * name serves nothing" lived in a container whose name you had to know.
454
+ *
455
+ * It matters most for ACME. Traefik requests certificates in the BACKGROUND after it
456
+ * starts, so a name can be routed, the edge can be healthy, and issuance can still be
457
+ * failing -- and the only record of why is here.
458
+ */
459
+ async function logs({ tail = 200, follow = false } = {}) {
460
+ const args = ['logs', `--tail=${String(tail)}`];
461
+ if (follow)
462
+ args.push('-f');
463
+ // NOT CAPTURED: the child inherits the terminal, so `-f` behaves like `docker compose
464
+ // logs -f` and compose's own errors reach the reader in its words. This went through
465
+ // composeRun, which captures, and dropped what it caught: `edge logs` printed nothing of
466
+ // the log and `-f` nothing at all.
467
+ //
468
+ // Same contract otherwise: an absent `edge/` or docker must not be a stack trace. Not
469
+ // reachable from the CLI, which guards on `installed()` first, and hardened anyway -- a
470
+ // seam that raises for one caller raises for the next one. A Ctrl-C is not a launch
471
+ // failure; it unwinds as the Interrupted it is.
472
+ try {
473
+ const res = await (0, docker_cli_1.compose)(['-p', paths.PROJECT, ...args], { cwd: paths.edgeDir() });
474
+ // `returncode`: a child a signal ended is minus that signal, the established convention.
475
+ return res.exitCode ?? -node_os_1.constants.signals[res.signal];
476
+ }
477
+ catch (error) {
478
+ if (error instanceof exec_1.Interrupted)
479
+ throw error;
480
+ return engine_1.LAUNCH_FAILURE_CODE;
481
+ }
482
+ }
483
+ /** What `doctor` and the GUI report. */
484
+ async function status() {
485
+ return {
486
+ installed: edgeSpec.installed(),
487
+ running: await (0, engine_1.running)(),
488
+ routes: edgeRoutes.registered(),
489
+ // Which workspace networks it is currently joined to. A route with no attachment is a
490
+ // 502 rather than an error, so the two belong side by side.
491
+ attached: [...(await (0, networks_1.attachedNetworks)())].sort(),
492
+ };
493
+ }