OpenPanel is an open-source web and product analytics platform, an open-source alternative to Mixpanel with optional self-hosting.
self-hosting: only a stamped CLI manages image tags
Carl-Gerhard Lindesvärd37bb3ab02d2f
diff --git a/self-hosting/cli/src/doctor/checks.ts b/self-hosting/cli/src/doctor/checks.tsindex 4623b3e3c..1baf070d5 100644--- a/self-hosting/cli/src/doctor/checks.ts+++ b/self-hosting/cli/src/doctor/checks.ts@@ -197,61 +197,61 @@ const OPENPANEL_IMAGE = /^(lindesvard\/openpanel-(?:api|dashboard)):(.+)$/;const IMAGE_SERVICES = ['op-api', 'op-dashboard', 'op-worker'] as const;const EXACT_VERSION = /^\d+\.\d+\.\d+$/;-const MAJOR_ONLY = /^\d+$/;--// An image already on the CLI's tag, or newer than it, is left alone: an older-// CLI must never downgrade a stack. A dev build (floating major, e.g. "3") is-// satisfied by any exact version of that major.-export const satisfiesStack = (- tag: string,- stack: string = STACK_IMAGE_TAG-): boolean => {++// An image already on the CLI's version, or newer than it, is left alone: an+// older CLI must never downgrade a stack.+export const satisfiesStack = (tag: string, stack: string): boolean => {if (tag === stack) {return true;}- if (MAJOR_ONLY.test(stack)) {- return tag.startsWith(`${stack}.`) && EXACT_VERSION.test(tag);- }const bothExact = EXACT_VERSION.test(tag) && EXACT_VERSION.test(stack);return bothExact && isNewer(tag, stack);};-const staleImages = (install: Install) =>+const staleImages = (install: Install, stack: string) =>IMAGE_SERVICES.flatMap((service) => {const image = serviceImage(install, service);const match = image ? OPENPANEL_IMAGE.exec(image) : null;const [, repository, tag] = match ?? [];- return repository && tag && !satisfiesStack(tag)+ return repository && tag && !satisfiesStack(tag, stack)? [{ service, repository, tag }]: [];});// The CLI owns the OpenPanel image tag, pinned versions included: the compose// changes it makes only work with the images it was released for. Images from-// other registries (supporter builds, forks) are never touched.-export const imageTag: Check = (install) => {- const stale = staleImages(install);- if (stale.length === 0) {- return null;- }- const changes = stale- .map(({ service, tag }) => `${service} ${tag} → ${STACK_IMAGE_TAG}`)- .join(', ');- return {- id: 'compose/image-tag',- severity: 'error',- title: `OpenPanel images move to :${STACK_IMAGE_TAG} (${changes})`,- detail: `This CLI release targets :${STACK_IMAGE_TAG}; the compose changes it makes only work with those images.`,- fix: (target) => {- for (const { service, repository } of staleImages(target)) {- target.compose.setIn(- ['services', service, 'image'],- `${repository}:${STACK_IMAGE_TAG}`- );- }- },+// other registries (supporter builds, forks) are never touched, and neither is+// anything when there is no version to target (a dev build).+export const imageTagFor =+ (stack: string | null): Check =>+ (install) => {+ if (stack === null) {+ return null;+ }+ const stale = staleImages(install, stack);+ if (stale.length === 0) {+ return null;+ }+ const changes = stale+ .map(({ service, tag }) => `${service} ${tag} → ${stack}`)+ .join(', ');+ return {+ id: 'compose/image-tag',+ severity: 'error',+ title: `OpenPanel images move to :${stack} (${changes})`,+ detail: `This CLI release targets :${stack}; the compose changes it makes only work with those images.`,+ fix: (target) => {+ for (const { service, repository } of staleImages(target, stack)) {+ target.compose.setIn(+ ['services', service, 'image'],+ `${repository}:${stack}`+ );+ }+ },+ };};-};++export const imageTag = imageTagFor(STACK_IMAGE_TAG);export const workerImage: Check = (install) => {if (!isLegacyWorkerImage(install)) {diff --git a/self-hosting/cli/src/init/generate.ts b/self-hosting/cli/src/init/generate.tsindex de82828bf..0be1c917a 100644--- a/self-hosting/cli/src/init/generate.ts+++ b/self-hosting/cli/src/init/generate.ts@@ -133,7 +133,10 @@ const renderCompose = (answers: InitAnswers): string => {if (answers.proxy === 'external') {removeService(compose, 'op-proxy');}- for (const service of OPENPANEL_IMAGE_SERVICES) {+ // A release CLI pins its own version; a dev build keeps the template's tag.+ for (const service of STACK_IMAGE_TAG === null+ ? []+ : OPENPANEL_IMAGE_SERVICES) {const image = String(compose.getIn(['services', service, 'image']) ?? '');const repository = image.split(':')[0];if (compose.hasIn(['services', service]) && repository) {diff --git a/self-hosting/cli/src/supporter.ts b/self-hosting/cli/src/supporter.tsindex b165373a2..97afc1d79 100644--- a/self-hosting/cli/src/supporter.ts+++ b/self-hosting/cli/src/supporter.ts@@ -3,7 +3,7 @@ import { homedir } from 'node:os';import { join } from 'node:path';import type { Install } from './doctor/types';import { type Fetcher, isNewer } from './self-update';-import { STACK_IMAGE_TAG } from './templates';+import { STACK_IMAGE_TAG, TEMPLATE_IMAGE_TAG } from './templates';// Supporters pull every internal build (each push to main) from this registry,// which mirrors ghcr.io/openpanel-dev; the public release images are on Docker Hub.@@ -62,7 +62,8 @@ export const pointAt = (return changes;};-export const publicVersion = (): string => STACK_IMAGE_TAG;+export const publicVersion = (): string =>+ STACK_IMAGE_TAG ?? TEMPLATE_IMAGE_TAG;// Every push to main is tagged vX.Y.Z; the highest one is the newest build.export const latestBuildVersion = async (fetcher: Fetcher): Promise<string> => {diff --git a/self-hosting/cli/src/templates.ts b/self-hosting/cli/src/templates.tsindex 75137435e..8808720ee 100644--- a/self-hosting/cli/src/templates.ts+++ b/self-hosting/cli/src/templates.ts@@ -33,13 +33,16 @@ export const templates = {export const DEFAULT_EVENTS_TOPIC_PARTITIONS = 24;-const TEMPLATE_IMAGE_TAG = /lindesvard\/openpanel-api:(\S+)/;+const TEMPLATE_IMAGE_PATTERN = /lindesvard\/openpanel-api:(\S+)/;-// The OpenPanel image tag this CLI installs: its own version for a release-// build, so CLI and images always match; the template's tag for a dev build.-export const STACK_IMAGE_TAG: string =- RELEASE_VERSION ??- (TEMPLATE_IMAGE_TAG.exec(templates.compose)?.[1] as string);+// The exact version a release CLI installs, so CLI and images always match.+// null for a dev build (run from source), which leaves image tags alone.+export const STACK_IMAGE_TAG: string | null = RELEASE_VERSION;++// Only a default for local testing with a dev build; nothing shipped reads it.+export const TEMPLATE_IMAGE_TAG = TEMPLATE_IMAGE_PATTERN.exec(+ templates.compose+)?.[1] as string;export const renderRedpandaBootstrap = (partitions: number): string =>templates.redpanda.replace(diff --git a/self-hosting/cli/test/doctor.test.ts b/self-hosting/cli/test/doctor.test.tsindex 73fc2323f..ac4125e7b 100644--- a/self-hosting/cli/test/doctor.test.ts+++ b/self-hosting/cli/test/doctor.test.ts@@ -29,7 +29,6 @@ describe('doctor on a v1 install', () => {'upgrade/drain-old-queue','compose/redpanda-service','files/redpanda-bootstrap',- 'compose/image-tag','compose/worker-image','compose/worker-role','compose/worker-replicas',@@ -60,10 +59,6 @@ describe('doctor on a v1 install', () => {expect(compose.services['op-worker'].image).toBe(compose.services['op-api'].image);- expect(compose.services['op-api'].image).toBe('lindesvard/openpanel-api:3');- expect(compose.services['op-dashboard'].image).toBe(- 'lindesvard/openpanel-dashboard:3'- );expect(compose.services['op-worker'].environment).toContain('ROLE=worker');expect(compose.services['op-api'].command).toContain('scripts/migrate-code.ts'diff --git a/self-hosting/cli/test/image-tag.test.ts b/self-hosting/cli/test/image-tag.test.tsindex 7a09f61ec..2d656c261 100644--- a/self-hosting/cli/test/image-tag.test.ts+++ b/self-hosting/cli/test/image-tag.test.ts@@ -1,122 +1,97 @@import { describe, expect, test } from 'bun:test';import { parseDocument } from 'yaml';-import { applyFixes, runDoctor } from '../src/doctor';-import { satisfiesStack } from '../src/doctor/checks';+import { imageTagFor, satisfiesStack } from '../src/doctor/checks';import type { Install } from '../src/doctor/types';import { EnvFile } from '../src/env-file';import { STACK_IMAGE_TAG, templates } from '../src/templates';-// A current install whose image lines are replaced, so only the tag check can fire.+const RELEASE = '3.1.4';+const check = imageTagFor(RELEASE);+const installWithImages = (images: Record<string, string>): Install => {const compose = parseDocument(templates.compose);for (const [service, image] of Object.entries(images)) {compose.setIn(['services', service, 'image'], image);}- const env = new EnvFile(- [- 'SELF_HOSTED="true"',- 'DASHBOARD_URL="https://a.example.com"',- 'KAFKA_BROKERS="op-rp:9092"',- 'KAFKA_EVENTS_TOPIC_PARTITIONS="24"',- `ENCRYPTION_KEY="${'ab'.repeat(32)}"`,- 'COOKIE_SECRET="secret"',- ].join('\n')- );return {compose,- env,+ env: new EnvFile(''),caddyfile: null,files: new Map(),fileKind: () => 'file',};};-const tagFinding = (install: Install) =>- runDoctor(install).find((finding) => finding.id === 'compose/image-tag');--describe('compose/image-tag', () => {- test('the expected tag is read from the template', () => {- expect(STACK_IMAGE_TAG).toMatch(/^[\w.-]+$/);- expect(- String(- parseDocument(templates.compose).getIn(['services', 'op-api', 'image'])- )- ).toEndWith(`:${STACK_IMAGE_TAG}`);- });-- test('an install already on the expected tag is left alone', () => {- expect(tagFinding(installWithImages({}))).toBeUndefined();- });+const image = (install: Install, service: string) =>+ install.compose.getIn(['services', service, 'image']);- test('floating tags from an older line are rewritten', () => {+describe('compose/image-tag for a release CLI', () => {+ test('floating and older tags move to the exact version', () => {const install = installWithImages({- 'op-api': 'lindesvard/openpanel-api:1',+ 'op-api': 'lindesvard/openpanel-api:3','op-dashboard': 'lindesvard/openpanel-dashboard:latest',+ 'op-worker': 'lindesvard/openpanel-api:3.0.0',});- expect(tagFinding(install)?.fix).toBeDefined();+ const finding = check(install);+ expect(finding?.title).toContain('op-api 3 → 3.1.4');- applyFixes(install);- const compose = install.compose.toJS();- expect(compose.services['op-api'].image).toBe(- `lindesvard/openpanel-api:${STACK_IMAGE_TAG}`+ finding?.fix?.(install);+ expect(image(install, 'op-api')).toBe(+ `lindesvard/openpanel-api:${RELEASE}`);- expect(compose.services['op-dashboard'].image).toBe(- `lindesvard/openpanel-dashboard:${STACK_IMAGE_TAG}`+ expect(image(install, 'op-dashboard')).toBe(+ `lindesvard/openpanel-dashboard:${RELEASE}`);- expect(tagFinding(install)).toBeUndefined();- });-- test('exact pins are moved too, and the title says what changes', () => {- const install = installWithImages({- 'op-api': 'lindesvard/openpanel-api:1.2.3',- 'op-dashboard': 'lindesvard/openpanel-dashboard:1',- });- expect(tagFinding(install)?.title).toContain(- `op-api 1.2.3 → ${STACK_IMAGE_TAG}`+ expect(image(install, 'op-worker')).toBe(+ `lindesvard/openpanel-api:${RELEASE}`);+ expect(check(install)).toBeNull();+ });- applyFixes(install);- const compose = install.compose.toJS();- expect(compose.services['op-api'].image).toBe(- `lindesvard/openpanel-api:${STACK_IMAGE_TAG}`- );- expect(compose.services['op-dashboard'].image).toBe(- `lindesvard/openpanel-dashboard:${STACK_IMAGE_TAG}`- );+ test('images already on the version, or newer, are left alone', () => {+ expect(+ check(+ installWithImages({+ 'op-api': `lindesvard/openpanel-api:${RELEASE}`,+ 'op-dashboard': 'lindesvard/openpanel-dashboard:3.2.0',+ 'op-worker': `lindesvard/openpanel-api:${RELEASE}`,+ })+ )+ ).toBeNull();});test('supporter and third-party registries are never touched', () => {- const supporter = 'docker.openpanel.dev/openpanel-dev/api:main-ab12';+ const supporter = 'docker.openpanel.dev/openpanel-dev/api:3.1.9';const install = installWithImages({'op-api': supporter,- 'op-db': 'postgres:14-alpine',+ 'op-dashboard': `lindesvard/openpanel-dashboard:${RELEASE}`,+ 'op-worker': supporter,});+ expect(check(install)).toBeNull();+ });+});- expect(tagFinding(install)).toBeUndefined();- applyFixes(install);- expect(install.compose.getIn(['services', 'op-api', 'image'])).toBe(- supporter- );+describe('a dev build (run from source)', () => {+ test('has no version, so it leaves image tags alone', () => {+ expect(STACK_IMAGE_TAG).toBeNull();+ expect(+ imageTagFor(null)(+ installWithImages({ 'op-api': 'lindesvard/openpanel-api:1' })+ )+ ).toBeNull();});});describe('satisfiesStack', () => {- test('a release CLI moves floating and older tags to its exact version', () => {+ test('older, floating and other tags do not satisfy an exact version', () => {expect(satisfiesStack('3', '3.1.4')).toBe(false);expect(satisfiesStack('latest', '3.1.4')).toBe(false);expect(satisfiesStack('3.1.3', '3.1.4')).toBe(false);expect(satisfiesStack('2.3.0', '3.1.4')).toBe(false);});- test('a release CLI never downgrades newer images', () => {+ test('the same or a newer version does (never downgrade)', () => {expect(satisfiesStack('3.1.4', '3.1.4')).toBe(true);expect(satisfiesStack('3.2.0', '3.1.4')).toBe(true);});-- test('a dev build (floating major) accepts any exact version of that major', () => {- expect(satisfiesStack('3.1.4', '3')).toBe(true);- expect(satisfiesStack('3', '3')).toBe(true);- expect(satisfiesStack('2', '3')).toBe(false);- expect(satisfiesStack('2.3.0', '3')).toBe(false);- });});