Free, Open Source, Self-Hosted WhatsApp API Gateway
TypeScript apibotgatewayself-hostedwhatsappwhatsapp-apiwhatsapp-automationwhatsapp-botwhatsapp-gatewaywhatsapp-gateway-apiwhatsapp-webwhatsapp-web-api
fix(docker): warn when a built-in container runs an outdated image
Yudhi Armyndharis2aced1e06482
diff --git a/src/modules/docker/docker.service.spec.ts b/src/modules/docker/docker.service.spec.tsindex b8e882f56..f218a30b6 100644--- a/src/modules/docker/docker.service.spec.ts+++ b/src/modules/docker/docker.service.spec.ts@@ -581,6 +581,98 @@ describe('DockerService.startService', () => {});});+describe('DockerService image drift warning', () => {+ const OLD_IMAGE = 'minio/minio:RELEASE.2024-01-01T00-00-00Z';+ const pinOf = (service: DockerService, profile: string): string =>+ (service as unknown as { getContainerSpec(p: string): { image: string } }).getContainerSpec(profile).image;+ const retained = (running: boolean, image?: string) => ({+ inspect: jest.fn().mockResolvedValue({+ Name: '/openwa-minio',+ State: { Running: running },+ ...(image ? { Config: { Image: image } } : {}),+ }),+ start: jest.fn().mockResolvedValue(undefined),+ });+ const setup = (container: unknown) => {+ const service = new DockerService();+ Object.assign(service as unknown as Record<string, unknown>, { docker: {}, isAvailable: true });+ jest.spyOn(service, 'getContainerByService').mockResolvedValue(container as never);+ const warn = jest.spyOn((service as unknown as { logger: { warn: () => void } }).logger, 'warn');+ warn.mockImplementation(() => undefined);+ return { service, warn };+ };++ it('warns when a stopped retained container runs an older image, and still starts it', async () => {+ const container = retained(false, OLD_IMAGE);+ const { service, warn } = setup(container);++ await expect(service.startService('storage')).resolves.toBe(true);++ expect(container.start).toHaveBeenCalledTimes(1);+ expect(warn).toHaveBeenCalledTimes(1);+ const message = String((warn.mock.calls[0] as unknown[])[0]);+ expect(message).toContain('openwa-minio');+ expect(message).toContain(OLD_IMAGE);+ expect(message).toContain(pinOf(service, 'minio'));+ expect(message).toContain('docker rm -f openwa-minio');+ expect(message).not.toContain('data migration');+ });++ it('adds the data-migration caveat for a drifted PostgreSQL container', async () => {+ const container = {+ inspect: jest.fn().mockResolvedValue({+ Name: '/openwa-postgres',+ State: { Running: false },+ Config: { Image: 'postgres:15-alpine' },+ }),+ start: jest.fn().mockResolvedValue(undefined),+ };+ const { service, warn } = setup(container);++ await expect(service.startService('database')).resolves.toBe(true);+ expect(warn).toHaveBeenCalledTimes(1);+ const message = String((warn.mock.calls[0] as unknown[])[0]);+ expect(message).toContain('docker rm -f openwa-postgres');+ expect(message).toContain('major version change needs a data migration');+ });++ it('warns for a running drifted container without starting it', async () => {+ const container = retained(true, OLD_IMAGE);+ const { service, warn } = setup(container);++ await expect(service.startService('storage')).resolves.toBe(true);+ expect(container.start).not.toHaveBeenCalled();+ expect(warn).toHaveBeenCalledTimes(1);+ });++ it('stays quiet when the container runs the pinned image or reports no image', async () => {+ const pinned = setup(null);+ const onPin = retained(false, pinOf(pinned.service, 'minio'));+ jest.spyOn(pinned.service, 'getContainerByService').mockResolvedValue(onPin as never);+ await expect(pinned.service.startService('storage')).resolves.toBe(true);+ expect(pinned.warn).not.toHaveBeenCalled();++ const bare = setup(retained(false));+ await expect(bare.service.startService('storage')).resolves.toBe(true);+ expect(bare.warn).not.toHaveBeenCalled();+ });++ it('warns on the createService name-fallback branch too', async () => {+ const { service, warn } = setup(retained(false, OLD_IMAGE));++ await expect(service.createService('minio')).resolves.toBe(true);+ expect(warn).toHaveBeenCalledWith(expect.stringContaining(OLD_IMAGE));+ });++ it('does not turn a drifted retained container into an orchestration failure', async () => {+ const { service } = setup(retained(false, OLD_IMAGE));++ const result = await service.orchestrateProfiles(['minio']);+ expect(result.success).toBe(true);+ expect(result.containersStarted).toEqual(['minio']);+ });+});+describe('DockerService.stopService', () => {const makeService = (container: unknown) => {const service = new DockerService();diff --git a/src/modules/docker/docker.service.ts b/src/modules/docker/docker.service.tsindex 26700d051..ccc4a4cb5 100644--- a/src/modules/docker/docker.service.ts+++ b/src/modules/docker/docker.service.ts@@ -350,6 +350,7 @@ export class DockerService implements OnModuleInit {const existing = await this.getContainerByService(profile);if (existing) {const info = await existing.inspect();+ this.warnOnImageDrift(profile, spec.name, info);if (info.State.Running) {this.logger.log(`Container ${spec.name} already running`);return true;@@ -444,26 +445,26 @@ export class DockerService implements OnModuleInit {async startService(service: string): Promise<boolean> {const container = await this.getContainerByService(service);+ // Map service names to docker-compose profiles+ const serviceToProfile: Record<string, string> = {+ database: 'postgres',+ cache: 'redis',+ storage: 'minio',+ postgres: 'postgres',+ redis: 'redis',+ minio: 'minio',+ };+ const profile = serviceToProfile[service] || service;+if (!container) {// Container doesn't exist - create it using docker-composethis.logger.log(`Container for service '${service}' not found, creating...`);-- // Map service names to docker-compose profiles- const serviceToProfile: Record<string, string> = {- database: 'postgres',- cache: 'redis',- storage: 'minio',- postgres: 'postgres',- redis: 'redis',- minio: 'minio',- };-- const profile = serviceToProfile[service] || service;return this.createService(profile);}try {const info = await container.inspect();+ this.warnOnImageDrift(profile, info.Name?.replace(/^\//, '') || service, info);if (info.State.Running) {this.logger.log(`Service '${service}' is already running`);return true;@@ -478,6 +479,23 @@ export class DockerService implements OnModuleInit {}}+ /**+ * A retained container is only ever restarted, never recreated (see stopManagedService), so it+ * keeps the image it was created from after the pin moves. Warn with the way out; start and stop+ * behaviour stay as they are.+ */+ private warnOnImageDrift(profile: string, name: string, info: Docker.ContainerInspectInfo): void {+ const pinned = this.getContainerSpec(profile)?.image;+ const running = info.Config?.Image;+ if (!pinned || !running || running === pinned) return;+ this.logger.warn(+ `Container ${name} runs image ${running}, but this release pins ${pinned}. Remove it with ` ++ `\`docker rm -f ${name}\` (its named data volume is kept) and restart OpenWA or re-enable the ` ++ `service so it is recreated from the pin.` ++ (profile === 'postgres' ? ' A PostgreSQL major version change needs a data migration first.' : ''),+ );+ }+/*** Stop a managed profile's container and RETAIN it for a later re-enable.*