Free, Open Source, Self-Hosted WhatsApp API Gateway
TypeScript apibotgatewayself-hostedwhatsappwhatsapp-apiwhatsapp-automationwhatsapp-botwhatsapp-gatewaywhatsapp-gateway-apiwhatsapp-webwhatsapp-web-api
fix(infra): keep a container the next boot reaches by another name
Yudhi Armyndharisc6dc131a6d80
diff --git a/docs/06-api-specification.md b/docs/06-api-specification.mdindex 12387d1e0..7973c00d2 100644--- a/docs/06-api-specification.md+++ b/docs/06-api-specification.md@@ -5794,7 +5794,7 @@ Request a graceful server restart, optionally orchestrating Docker profiles (add}```-`estimatedTime` (seconds, a typical healthy value rather than a bound; teardown is force-exited 67 s after the grace at the default `WEBHOOK_SHUTDOWN_DRAIN_MS`) = base 15 + 20/postgres + 13/redis + 15/minio + 5/removal. The echoed `profilesToRemove`, and the removal count, already omit any bundled service the environment pins the app to (for example `DATABASE_HOST=postgres` set in the host environment or the project `.env`); such a service is never stopped. Likewise the echoed `profiles` omit a bundled service whose host the environment pins to anything else (for example `DATABASE_HOST=db.example.com`): the next boot dials that host, so its container is not started. `orchestration` is present only when Docker is available and at least one managed profile (`postgres`, `redis`, `minio`) remains in `profiles`; `removal` only when Docker is available and at least one managed profile remains in `profilesToRemove` after dropping pinned services and profiles that are also being started. Without Docker, a `data/.orchestration-request.json` signal file is written instead. After responding, `shutdownService.shutdown()` runs (default ~3s grace); readiness returns `503` during drain.+`estimatedTime` (seconds, a typical healthy value rather than a bound; teardown is force-exited 67 s after the grace at the default `WEBHOOK_SHUTDOWN_DRAIN_MS`) = base 15 + 20/postgres + 13/redis + 15/minio + 5/removal. The echoed `profilesToRemove`, and the removal count, already omit any bundled service the environment pins the app to (for example `DATABASE_HOST=postgres` set in the host environment or the project `.env`, or the container name `openwa-postgres`, in any letter case); such a service is never stopped. Likewise the echoed `profiles` omit a bundled service whose host the environment pins to anything else (for example `DATABASE_HOST=db.example.com`): the next boot dials that host, so its container is not started. `orchestration` is present only when Docker is available and at least one managed profile (`postgres`, `redis`, `minio`) remains in `profiles`; `removal` only when Docker is available and at least one managed profile remains in `profilesToRemove` after dropping pinned services and profiles that are also being started. Without Docker, a `data/.orchestration-request.json` signal file is written instead. After responding, `shutdownService.shutdown()` runs (default ~3s grace); readiness returns `503` during drain.**Errors:** `401` · `403`diff --git a/src/modules/docker/docker.service.ts b/src/modules/docker/docker.service.tsindex a8470bacd..7e9c719ca 100644--- a/src/modules/docker/docker.service.ts+++ b/src/modules/docker/docker.service.ts@@ -27,6 +27,25 @@ export const BUNDLED_SERVICE_ENV: Record<string, [string, string]> = {minio: ['S3_ENDPOINT', 'http://minio:9000'],};+/**+ * Whether a host (for minio, the host of an S3 endpoint) reaches the profile's bundled container: its network alias+ * (the profile name) or its container name, in any letter case and with or without the trailing dot of an absolute+ * DNS name.+ */+export function namesBundledContainer(profile: string, value: string | undefined): boolean {+ if (!BUNDLED_SERVICE_ENV[profile] || value === undefined) return false;+ let host = value.trim();+ if (profile === 'minio') {+ try {+ host = new URL(host).hostname;+ } catch {+ return false;+ }+ }+ host = host.toLowerCase().replace(/\.$/, '');+ return host === profile || host === `openwa-${profile}`;+}+/*** Whether the environment (or the project .env) pins the profile's host to anything but its bundled container. The* pin outranks the saved file at every boot, so a container started for that profile would run with nothing using it.diff --git a/src/modules/infra/infra-config.controller.spec.ts b/src/modules/infra/infra-config.controller.spec.tsindex 0fde0fb70..6b3bf8cd3 100644--- a/src/modules/infra/infra-config.controller.spec.ts+++ b/src/modules/infra/infra-config.controller.spec.ts@@ -2031,6 +2031,33 @@ describe('InfraConfigController.requestRestart constrains teardown to managed pr}});+ // The bundled container answers to its network alias and its container name, in any letter case and as an+ // absolute DNS name with a trailing dot, so a host spelled any of these ways still reaches it.+ const SPELLINGS: Record<ServiceName, { reach: string[]; elsewhere: string[] }> = {+ database: {+ reach: ['Postgres', 'postgres.', 'openwa-postgres', ' OPENWA-POSTGRES. '],+ elsewhere: ['localhost', 'postgres-replica', 'postgres.example.com', 'openwa-postgres-2'],+ },+ redis: { reach: ['REDIS', 'redis.', 'openwa-redis'], elsewhere: ['localhost', 'redis-master', 'myredis'] },+ storage: {+ reach: ['http://MINIO:9000', 'http://minio:9000/', 'http://openwa-minio:9000', 'https://minio.:9000'],+ elsewhere: ['http://localhost:9000', 'http://minio.example.com:9000', 'minio:9000', 'not a url'],+ },+ };+ describe.each(SERVICE_NAMES)('%s under another spelling of the bundled host', service => {+ for (const host of SPELLINGS[service].reach) {+ it(`keeps it under ${JSON.stringify(host)}, in the file or pinned`, async () => {+ expect((await stoppedFor(service, host, undefined)).stopped).toBe(false);+ expect((await stoppedFor(service, SERVICES[service].externalHost, host)).stopped).toBe(false);+ });+ }+ for (const host of SPELLINGS[service].elsewhere) {+ it(`still stops it under ${JSON.stringify(host)}`, async () => {+ expect((await stoppedFor(service, host, undefined)).stopped).toBe(true);+ });+ }+ });+it('keeps the container when the flag is pinned off over a stored bundled host', async () => {process.env.REDIS_BUILTIN = 'false';pinnedKeys.push('REDIS_BUILTIN');diff --git a/src/modules/infra/infra-config.controller.ts b/src/modules/infra/infra-config.controller.tsindex 6025c4b8d..017f4ac7a 100644--- a/src/modules/infra/infra-config.controller.ts+++ b/src/modules/infra/infra-config.controller.ts@@ -17,7 +17,13 @@ import { RequireRole, RequireUnscopedKey } from '../auth/decorators/auth.decoratimport { ApiKeyRole } from '../auth/entities/api-key.entity';import { writeSecretFile } from '../../common/utils/secret-file';import { EngineFactory } from '../../engine/engine.factory';-import { BUNDLED_SERVICE_ENV, DockerService, MANAGED_DOCKER_PROFILES, hostPinnedElsewhere } from '../docker';+import {+ BUNDLED_SERVICE_ENV,+ DockerService,+ MANAGED_DOCKER_PROFILES,+ hostPinnedElsewhere,+ namesBundledContainer,+} from '../docker';import { ShutdownService } from '../../common/services/shutdown.service';import { createLogger } from '../../common/services/logger.service';import { AuditService } from '../audit/audit.service';@@ -444,14 +450,14 @@ export class InfraConfigController {// app would point at a stopped container and fail to boot, with the dashboard that could undo it. The// host it dials is the one the environment pins (e.g. the documented manual built-in Postgres with// DATABASE_HOST=postgres in .env, which outranks the saved config), else the one in the saved file,- // which already holds the save that precedes this restart. A host moved away from the bundled name- // still stops its container.+ // which already holds the save that precedes this restart. Any spelling that reaches the container counts (the+ // container name, another letter case). A host moved away from the bundled name still stops its container.let saved: Record<string, string> | undefined;const dialsBundled = (profile: string): boolean => {const bundled = BUNDLED_SERVICE_ENV[profile];if (!bundled) return false;- const [key, value] = bundled;- return (isEnvPinned(key) ? process.env[key] : (saved ??= readGeneratedEnv())[key]) === value;+ const [key] = bundled;+ return namesBundledContainer(profile, isEnvPinned(key) ? process.env[key] : (saved ??= readGeneratedEnv())[key]);};const requestedRemoval = body?.profilesToRemove || [];const keptRunning = requestedRemoval.filter(dialsBundled);