OpenPanel is an open-source web and product analytics platform, an open-source alternative to Mixpanel with optional self-hosting.
self-hosting: supporters follow each build's CLI, Linux binaries only
Carl-Gerhard Lindesvärd2b5a8a01deda
diff --git a/self-hosting/cli/src/commands/upgrade.ts b/self-hosting/cli/src/commands/upgrade.tsindex c5e64ac1a..20fadcbf6 100644--- a/self-hosting/cli/src/commands/upgrade.ts+++ b/self-hosting/cli/src/commands/upgrade.ts@@ -53,12 +53,15 @@ type SelfUpdateOutcome = { kind: 'continue' } | { kind: 'exit'; code: number };// A newer CLI carries the checks and image tag for the newer stack, so when one// exists but cannot be installed, stopping beats upgrading with stale knowledge.-const updateSelf = async (): Promise<SelfUpdateOutcome> => {+const updateSelf = async (+ includePrereleases: boolean+): Promise<SelfUpdateOutcome> => {try {const result = await selfUpdate({currentVersion: VERSION,execPath: process.execPath,fetcher: fetchWithTimeout,+ includePrereleases,});if (result.status === 'current') {return { kind: 'continue' };@@ -128,7 +131,14 @@ const resolveChannel = (flags: UpgradeFlags, install: Install): Channel => {export const upgrade = async (flags: UpgradeFlags): Promise<number> => {if (!flags.noSelfUpdate && isCompiledBinary()) {- const outcome = await updateSelf();+ // A supporter install runs the newest build's images, so it takes the CLI+ // built alongside them (a pre-release) instead of the last public release.+ const followsBuilds =+ flags.supporter ||+ (!flags.public &&+ isInstallDir(flags.dir) &&+ channelOf(loadInstall(flags.dir)) === 'supporter');+ const outcome = await updateSelf(followsBuilds);if (outcome.kind === 'exit') {return outcome.code;}diff --git a/self-hosting/cli/src/self-update.ts b/self-hosting/cli/src/self-update.tsindex b60e25694..1638902bf 100644--- a/self-hosting/cli/src/self-update.ts+++ b/self-hosting/cli/src/self-update.ts@@ -60,26 +60,46 @@ interface ReleaseJson {assets: { name: string; browser_download_url: string }[];}+// Every build on main is a pre-release carrying the CLI; a public self-hosting+// release is one of those promoted. Supporters follow the builds, everyone+// else only the public releases.+export interface ReleaseFilter {+ includePrereleases?: boolean;+}++const versionOfTag = (tag: string) => tag.slice(RELEASE_TAG_PREFIX.length);+export const fetchLatestRelease = async (- fetcher: Fetcher+ fetcher: Fetcher,+ { includePrereleases = false }: ReleaseFilter = {}): Promise<Release | null> => {const response = await fetcher(RELEASES_API);if (!response.ok) {throw new Error(`Could not list releases (HTTP ${response.status})`);}const releases = (await response.json()) as ReleaseJson[];- const latest = releases.find(+ const candidates = releases.filter((release) =>release.tag_name.startsWith(RELEASE_TAG_PREFIX) &&!release.draft &&- !release.prerelease &&+ (includePrereleases || !release.prerelease) &&release.assets.some((asset) => asset.name === CHECKSUMS_ASSET));+ // Highest version, not first in the list: a promoted build keeps the date it+ // was built, so list order and version order can differ.+ const latest = candidates.reduce<ReleaseJson | null>(+ (highest, release) =>+ highest === null ||+ isNewer(versionOfTag(release.tag_name), versionOfTag(highest.tag_name))+ ? release+ : highest,+ null+ );if (!latest) {return null;}return {- version: latest.tag_name.slice(RELEASE_TAG_PREFIX.length),+ version: versionOfTag(latest.tag_name),assets: Object.fromEntries(latest.assets.map((asset) => [asset.name, asset.browser_download_url])),@@ -90,7 +110,7 @@ export type UpdateResult =| { status: 'current'; version: string }| { status: 'updated'; from: string; to: string };-interface UpdateOptions {+interface UpdateOptions extends ReleaseFilter {currentVersion: string;execPath: string;fetcher: Fetcher;@@ -106,8 +126,9 @@ export const selfUpdate = async ({fetcher,platform = process.platform,arch = process.arch,+ includePrereleases = false,}: UpdateOptions): Promise<UpdateResult> => {- const release = await fetchLatestRelease(fetcher);+ const release = await fetchLatestRelease(fetcher, { includePrereleases });if (!(release && isNewer(release.version, currentVersion))) {return { status: 'current', version: currentVersion };}diff --git a/self-hosting/cli/test/self-update.test.ts b/self-hosting/cli/test/self-update.test.tsindex 91e6dcae8..2fff99225 100644--- a/self-hosting/cli/test/self-update.test.ts+++ b/self-hosting/cli/test/self-update.test.ts@@ -12,6 +12,7 @@ import { join } from 'node:path';import {assetName,type Fetcher,+ fetchLatestRelease,isCompiledBinary,isNewer,parseChecksums,@@ -151,3 +152,51 @@ describe('selfUpdate', () => {expect(result.status).toBe('current');});});++describe('fetchLatestRelease: builds vs public releases', () => {+ const cliAssets = [+ {+ name: 'openpanel-linux-x64',+ browser_download_url: 'https://example.test/bin',+ },+ {+ name: 'checksums.txt',+ browser_download_url: 'https://example.test/sums',+ },+ ];+ const release = (tag: string, prerelease: boolean) => ({+ tag_name: tag,+ draft: false,+ prerelease,+ assets: cliAssets,+ });+ // GitHub lists by creation date; a promoted build keeps its build date, so an+ // older build can sit above a newer public release.+ const feed: Fetcher = async () =>+ Response.json([+ release('v3.1.9', true),+ release('v3.1.8', true),+ release('v3.1.4', false),+ release('v3.0.0', false),+ ]);++ test('public installs take the highest public release, never a build', async () => {+ expect((await fetchLatestRelease(feed))?.version).toBe('3.1.4');+ });++ test('supporters take the newest build', async () => {+ expect(+ (await fetchLatestRelease(feed, { includePrereleases: true }))?.version+ ).toBe('3.1.9');+ });++ test('the highest version wins, whatever the list order', async () => {+ const unordered: Fetcher = async () =>+ Response.json([+ release('v3.0.0', false),+ release('v3.2.0', false),+ release('v3.1.4', false),+ ]);+ expect((await fetchLatestRelease(unordered))?.version).toBe('3.2.0');+ });+});diff --git a/self-hosting/install.sh b/self-hosting/install.shindex 16132e58e..cdfe7e7a6 100755--- a/self-hosting/install.sh+++ b/self-hosting/install.sh@@ -7,6 +7,8 @@# Environment:# OPENPANEL_INSTALL_DIR Where to put the binary (default: /usr/local/bin, else ~/.local/bin)# OPENPANEL_RELEASES_API Release feed to read (default: GitHub)+# OPENPANEL_CHANNEL "supporter" installs the newest build (a pre-release)+# instead of the latest public releaseset -euRELEASES_API="${OPENPANEL_RELEASES_API:-https://api.github.com/repos/Openpanel-dev/openpanel/releases?per_page=30}"@@ -21,8 +23,8 @@ command -v curl >/dev/null 2>&1 || fail "curl is required"case "$(uname -s)" inLinux) os=linux ;;- Darwin) os=darwin ;;- *) fail "unsupported OS: $(uname -s). Download a binary from the releases page instead." ;;+ Darwin) fail "the openpanel CLI runs on the Linux server that hosts OpenPanel. To try it on a Mac, run it from source: cd self-hosting/cli && bun install && bun src/main.ts --help" ;;+ *) fail "unsupported OS: $(uname -s). The openpanel CLI runs on Linux servers." ;;esaccase "$(uname -m)" in@@ -50,8 +52,13 @@ curl -fsSL "$RELEASES_API" -o "$tmp/releases.json" || fail "could not reach $REL# Each release object lists its tag before its assets. The newest release that# carries this machine's binary is the one to install: every public self-hosting# release does, and internal version tags never become releases at all.-tag="$(awk -v prefix="\"$TAG_PREFIX" -v name="/$asset\"" '+# Builds of main are pre-releases; only supporters want those.+allow_prerelease=0+[ "${OPENPANEL_CHANNEL:-}" = "supporter" ] && allow_prerelease=1++tag="$(awk -v prefix="\"$TAG_PREFIX" -v name="/$asset\"" -v pre="$allow_prerelease" '/"tag_name":/ { split($0, parts, "\""); current = (index("\"" parts[4], prefix) == 1) ? parts[4] : "" }+ /"prerelease": *true/ && pre != 1 { current = "" }current != "" && /"browser_download_url":/ && index($0, name) { print current; exit }' "$tmp/releases.json")"[ -n "$tag" ] || fail "no release with $asset found"