@camunda8/cli 4.7.0-alpha.32 → 4.7.0-alpha.33

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.
@@ -83,6 +83,18 @@ patch releases.
83
83
  If the download server is unreachable, the aliases fall back to the values
84
84
  shipped in the plugin's `package.json`.
85
85
 
86
+ `stable` never launches a prerelease build. If the install for its version
87
+ still holds an alpha build (for example, a `8.11` install made through `alpha`
88
+ before 8.11 went GA), `start` and `install` download the GA build and replace
89
+ it. The GA build is extracted and checked in a staging directory first, so if
90
+ the download or extraction fails, the archive still holds a prerelease, or
91
+ another c8ctl has started that build in the meantime, the alpha build is kept.
92
+ When the download fails, the error says that only a prerelease build is
93
+ installed. A prerelease build that has cluster data, or an install that has
94
+ lost its launcher but still holds data, is never replaced automatically,
95
+ because that would delete the data: keep using a prerelease build with
96
+ `c8ctl cluster start <version>`, or run `c8ctl cluster purge <version>` first.
97
+
86
98
  ### Resilient running-state tracking
87
99
 
88
100
  A running cluster is tracked both by c8run's own `.process` pidfiles (which live
@@ -27,7 +27,7 @@ import {
27
27
  import { once } from 'node:events';
28
28
  import { chmod } from 'node:fs/promises';
29
29
  import { homedir, platform as osPlatform, arch as osArch } from 'node:os';
30
- import { join, dirname, basename, isAbsolute, resolve } from 'node:path';
30
+ import { join, dirname, basename, isAbsolute, relative, resolve } from 'node:path';
31
31
  import { finished } from 'node:stream/promises';
32
32
  import { StringDecoder } from 'node:string_decoder';
33
33
  import { fileURLToPath } from 'node:url';
@@ -232,9 +232,9 @@ function readLocalAliasMapping(cacheDir, alias) {
232
232
  */
233
233
  function findInstalledAliasTarget(cacheDir, value, alias) {
234
234
  const accepts = (version) => {
235
- const binaryPath = findC8RunBinaryPath({ cacheDir, version });
236
- if (!binaryPath) return false;
237
- return alias !== 'stable' || !isPrereleaseBuild(basename(dirname(binaryPath)));
235
+ const build = installedBuildName(cacheDir, version);
236
+ if (!build) return false;
237
+ return alias !== 'stable' || !isPrereleaseBuild(build);
238
238
  };
239
239
 
240
240
  if (accepts(value)) return value;
@@ -257,6 +257,94 @@ function isPrereleaseBuild(buildDirName) {
257
257
  return /^c8run-\d+\.\d+(\.\d+)?-/.test(buildDirName);
258
258
  }
259
259
 
260
+ /**
261
+ * Every cluster data dir in an install: `camunda-data` and the `data` dir
262
+ * inside each `camunda-zeebe-` dir, beside any launcher in the install dir
263
+ * or one of its immediate subdirs (older builds of a rolling install, or an
264
+ * install that lost its launcher). `cluster purge` deletes exactly these, and
265
+ * `stable` refuses to replace an install that still has any.
266
+ */
267
+ function findClusterDataDirs(installDir) {
268
+ const listDirs = (dir) => {
269
+ try {
270
+ return readdirSync(dir, { withFileTypes: true }).filter((entry) => entry.isDirectory());
271
+ } catch {
272
+ return [];
273
+ }
274
+ };
275
+ const roots = [installDir, ...listDirs(installDir).map((entry) => join(installDir, entry.name))];
276
+ const found = [];
277
+ for (const root of roots) {
278
+ const camundaData = join(root, 'camunda-data');
279
+ if (existsSync(camundaData)) found.push(camundaData);
280
+ for (const entry of listDirs(root)) {
281
+ const journal = join(root, entry.name, 'data');
282
+ if (entry.name.startsWith('camunda-zeebe-') && existsSync(journal)) found.push(journal);
283
+ }
284
+ }
285
+ return found;
286
+ }
287
+
288
+ /**
289
+ * Refuse to replace a `stable` install whose cluster data would be deleted:
290
+ * a prerelease build, or an install that lost its launcher. Data may sit
291
+ * beside the launcher or, without one, in the install dir or a subdir of it.
292
+ * A GA build is replaced like any rolling update.
293
+ */
294
+ function refuseIfClusterDataAtRisk(config) {
295
+ const version = config.version;
296
+ const installDir = join(config.cacheDir, `c8run-${version}`);
297
+ if (!existsSync(installDir)) return;
298
+ const build = installedBuildName(config.cacheDir, version);
299
+ if (build && !isPrereleaseBuild(build)) return;
300
+
301
+ if (findClusterDataDirs(installDir).length === 0) return;
302
+
303
+ const state = build
304
+ ? `holds a prerelease build (${build.slice('c8run-'.length)}) with cluster data`
305
+ : 'has cluster data but no c8run launcher';
306
+ const options = build
307
+ ? `Keep using it with "c8ctl cluster start ${version}", or delete its data with`
308
+ : 'Delete its data with';
309
+ throw new Error(
310
+ `c8run ${version} ${state}. "stable" needs a GA build, and replacing this install would delete that data. ` +
311
+ `${options} "c8ctl cluster purge ${version}" and run this command again.`,
312
+ );
313
+ }
314
+
315
+ /** Name of the build dir whose c8run would launch for `version` (e.g. "c8run-8.10.0-alpha1"), or null. */
316
+ function installedBuildName(cacheDir, version) {
317
+ const binaryPath = findC8RunBinaryPath({ cacheDir, version });
318
+ return binaryPath ? basename(dirname(binaryPath)) : null;
319
+ }
320
+
321
+ /** The prerelease build installed for `version`, or null when none is installed or it is GA. */
322
+ function installedPrereleaseBuild(cacheDir, version) {
323
+ const build = installedBuildName(cacheDir, version);
324
+ return build && isPrereleaseBuild(build) ? build : null;
325
+ }
326
+
327
+ /**
328
+ * Explain why `stable` has no usable local install when the version it would
329
+ * resolve to only holds a prerelease build — either `version` itself or the
330
+ * cached `stable` mapping (which resolution skips for that reason). Null otherwise.
331
+ */
332
+ function describePrereleaseOnlyStable(cacheDir, version) {
333
+ let cached = null;
334
+ try {
335
+ cached = readFileSync(getAliasMappingPath(cacheDir, 'stable'), 'utf-8').trim();
336
+ } catch {
337
+ // No cached mapping
338
+ }
339
+ for (const candidate of [version, cached]) {
340
+ const build = candidate && installedPrereleaseBuild(cacheDir, candidate);
341
+ if (build) {
342
+ return `Only a prerelease build of ${candidate} is installed (${build.slice('c8run-'.length)}); "stable" needs a GA build.`;
343
+ }
344
+ }
345
+ return null;
346
+ }
347
+
260
348
  /** Persist an alias mapping. Returns false if it could not be written. */
261
349
  export function storeLocalAliasMapping(cacheDir, alias, resolved) {
262
350
  try {
@@ -1555,7 +1643,18 @@ export async function hasNewerVersionAvailable(config) {
1555
1643
  export async function ensureC8RunInstalled(config) {
1556
1644
  const logger = getLogger();
1557
1645
 
1558
- if (isC8RunInstalled(config)) {
1646
+ // `stable` must never launch a prerelease, e.g. a rolling dir still holding
1647
+ // the alpha build installed through `alpha` before that minor went GA.
1648
+ const prereleaseBuild = config.requireGA ? installedPrereleaseBuild(config.cacheDir, config.version) : null;
1649
+ // Checked before downloading anything, and again right before the swap.
1650
+ if (config.requireGA) refuseIfClusterDataAtRisk(config);
1651
+
1652
+ if (prereleaseBuild) {
1653
+ logger.info(
1654
+ `c8run ${config.version} holds a prerelease build (${prereleaseBuild.slice('c8run-'.length)}); ` +
1655
+ '"stable" needs a GA build, so it will be replaced.',
1656
+ );
1657
+ } else if (isC8RunInstalled(config)) {
1559
1658
  if (config.checkForUpdates) {
1560
1659
  // Explicitly asked to check for updates (e.g. install subcommand with a rolling version)
1561
1660
  const hasUpdate = await hasNewerVersionAvailable(config);
@@ -1573,7 +1672,9 @@ export async function ensureC8RunInstalled(config) {
1573
1672
  'Stop it first with "c8ctl cluster stop", then re-run the install.',
1574
1673
  );
1575
1674
  }
1576
- purgeInstalledVersion(config);
1675
+ // stable is replaced via a staged swap after the new build is validated,
1676
+ // so a rejected archive never leaves it without a usable install.
1677
+ if (!config.requireGA) purgeInstalledVersion(config);
1577
1678
  } else {
1578
1679
  // Use local installation as-is (start subcommand, or pinned version)
1579
1680
  logger.info(`c8run ${config.version} is already installed.`);
@@ -1613,20 +1714,34 @@ export async function ensureC8RunInstalled(config) {
1613
1714
  );
1614
1715
  }
1615
1716
 
1616
- logger.info('No local installation found. Setting up...');
1717
+ if (!isC8RunInstalled(config)) logger.info('No local installation found. Setting up...');
1617
1718
 
1618
- const { archivePath, etag } = await downloadC8Run(config);
1719
+ let download;
1720
+ try {
1721
+ download = await downloadC8Run(config);
1722
+ } catch (error) {
1723
+ const note = config.requireGA && describePrereleaseOnlyStable(config.cacheDir, config.version);
1724
+ if (note) throw new Error(`${note} ${error?.message ?? error}`, { cause: error?.cause });
1725
+ throw error;
1726
+ }
1727
+ const { archivePath, etag } = download;
1619
1728
 
1620
1729
  const installDir = join(config.cacheDir, `c8run-${config.version}`);
1621
- await extractArchive(archivePath, installDir);
1730
+
1731
+ if (config.requireGA) {
1732
+ await installGABuild(config, archivePath, installDir);
1733
+ } else {
1734
+ await extractArchive(archivePath, installDir);
1735
+ }
1622
1736
 
1623
1737
  // Clean up the downloaded archive to save disk space
1624
1738
  if (existsSync(archivePath)) {
1625
1739
  rmSync(archivePath);
1626
1740
  }
1627
1741
 
1628
- const binaryPath = getC8RunBinaryPath(config);
1629
- await chmod(binaryPath, 0o755);
1742
+ if (!config.requireGA) {
1743
+ await chmod(getC8RunBinaryPath(config), 0o755);
1744
+ }
1630
1745
 
1631
1746
  // Store the ETag for rolling versions so future checks can detect new releases
1632
1747
  if (config.isRolling && etag) {
@@ -1636,6 +1751,64 @@ export async function ensureC8RunInstalled(config) {
1636
1751
  logger.info(`c8run ${config.version} installed successfully.`);
1637
1752
  }
1638
1753
 
1754
+ /**
1755
+ * Install the archive for `stable` without ever leaving a prerelease or
1756
+ * nothing in place: extract into a staging dir, require a GA launcher there,
1757
+ * then swap it in. Any failure before the swap leaves the existing install
1758
+ * (e.g. a usable alpha build) untouched.
1759
+ */
1760
+ async function installGABuild(config, archivePath, installDir) {
1761
+ const logger = getLogger();
1762
+ const stagingRoot = join(config.cacheDir, `.staging-c8run-${config.version}-${process.pid}`);
1763
+ const stagedDir = join(stagingRoot, `c8run-${config.version}`);
1764
+ // Dot-prefixed so neither dir is ever listed as an installed version ("c8run-*").
1765
+ const backupDir = join(config.cacheDir, `.replaced-c8run-${config.version}-${process.pid}`);
1766
+ rmSync(stagingRoot, { recursive: true, force: true });
1767
+
1768
+ try {
1769
+ await extractArchive(archivePath, stagedDir);
1770
+ const stagedBinary = findC8RunBinaryPath({ cacheDir: stagingRoot, version: config.version });
1771
+ if (!stagedBinary) {
1772
+ throw new Error(`The downloaded c8run ${config.version} archive does not contain a c8run launcher.`);
1773
+ }
1774
+ const stagedBuild = basename(dirname(stagedBinary));
1775
+ if (isPrereleaseBuild(stagedBuild)) {
1776
+ throw new Error(
1777
+ `The downloaded c8run ${config.version} is a prerelease build (${stagedBuild.slice('c8run-'.length)}); ` +
1778
+ '"stable" needs a GA build. Try again once the GA build is published, or start an explicit GA version.',
1779
+ );
1780
+ }
1781
+ await chmod(stagedBinary, 0o755);
1782
+
1783
+ // Rechecked here: another c8ctl may have started this version during the download.
1784
+ if (isVersionInstanceRunning(config.cacheDir, config.version)) {
1785
+ throw new Error(
1786
+ `Refusing to replace c8run ${config.version}: an instance started from it is still running. ` +
1787
+ 'Stop it first with "c8ctl cluster stop".',
1788
+ );
1789
+ }
1790
+
1791
+ // Rechecked here too: data may have been created during the download.
1792
+ refuseIfClusterDataAtRisk(config);
1793
+
1794
+ const replacing = existsSync(installDir);
1795
+ if (replacing) {
1796
+ logger.info(`Replacing the existing c8run ${config.version} build...`);
1797
+ rmSync(backupDir, { recursive: true, force: true });
1798
+ renameSync(installDir, backupDir);
1799
+ }
1800
+ try {
1801
+ renameSync(stagedDir, installDir);
1802
+ } catch (error) {
1803
+ if (replacing) renameSync(backupDir, installDir);
1804
+ throw error;
1805
+ }
1806
+ if (replacing) rmSync(backupDir, { recursive: true, force: true });
1807
+ } finally {
1808
+ rmSync(stagingRoot, { recursive: true, force: true });
1809
+ }
1810
+ }
1811
+
1639
1812
  // ---------------------------------------------------------------------------
1640
1813
  // Process management
1641
1814
  // ---------------------------------------------------------------------------
@@ -3273,48 +3446,19 @@ export async function purgeClusterData(cacheDir, versionSpec) {
3273
3446
  process.exit(1);
3274
3447
  }
3275
3448
 
3276
- const config = { cacheDir, version: resolvedVersion };
3277
3449
  const installDir = join(cacheDir, `c8run-${resolvedVersion}`);
3278
3450
  if (!existsSync(installDir)) {
3279
3451
  logger.error(`Version ${resolvedVersion} is not installed locally.`);
3280
3452
  process.exit(1);
3281
3453
  }
3282
3454
 
3283
- // Runtime data normally sits next to the c8run binary. If the binary itself
3284
- // is missing but the install dir survives — a partial/corrupt install, or the
3285
- // orphan case where the binary was removed out from under a running cluster
3286
- // (#560) — fall back to scanning the install dir and its immediate subdirs so
3287
- // leftover data is still purged instead of being silently left behind.
3288
- const binaryPath = findC8RunBinaryPath(config);
3289
- const dataRoots = binaryPath
3290
- ? [dirname(binaryPath)]
3291
- : [
3292
- installDir,
3293
- ...readdirSync(installDir, { withFileTypes: true })
3294
- .filter((entry) => entry.isDirectory())
3295
- .map((entry) => join(installDir, entry.name)),
3296
- ];
3297
-
3455
+ // Not only beside the launcher that would run: an older build of a rolling
3456
+ // install, or an install that lost its launcher, can hold data too, and
3457
+ // `stable` refuses to replace an install while any of it is left.
3298
3458
  const deleted = [];
3299
- for (const dataRoot of dataRoots) {
3300
- // Delete camunda-data (history data + application state)
3301
- const camundaDataDir = join(dataRoot, 'camunda-data');
3302
- if (existsSync(camundaDataDir)) {
3303
- rmSync(camundaDataDir, { recursive: true });
3304
- deleted.push('camunda-data');
3305
- }
3306
-
3307
- // Delete Zeebe journal data inside camunda-zeebe-* subdirectory
3308
- if (existsSync(dataRoot)) {
3309
- for (const entry of readdirSync(dataRoot, { withFileTypes: true })) {
3310
- if (!entry.isDirectory() || !entry.name.startsWith('camunda-zeebe-')) continue;
3311
- const dataDir = join(dataRoot, entry.name, 'data');
3312
- if (existsSync(dataDir)) {
3313
- rmSync(dataDir, { recursive: true });
3314
- deleted.push(join(entry.name, 'data'));
3315
- }
3316
- }
3317
- }
3459
+ for (const dataDir of findClusterDataDirs(installDir)) {
3460
+ rmSync(dataDir, { recursive: true });
3461
+ deleted.push(relative(installDir, dataDir));
3318
3462
  }
3319
3463
 
3320
3464
  if (deleted.length > 0) {
@@ -3899,6 +4043,8 @@ export const commands = {
3899
4043
  checkForUpdateHint: parsed.subcommand === 'start' && rolling,
3900
4044
  // --verbose: detailed HTTP diagnostics for the c8run download
3901
4045
  verbose: ctx?.verbose === true,
4046
+ // stable: reject (and replace) an install whose build is a prerelease
4047
+ requireGA: versionSpec === 'stable',
3902
4048
  };
3903
4049
 
3904
4050
  // For start with a named alias, check in the background whether the
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@camunda8/cli",
3
- "version": "4.7.0-alpha.32",
3
+ "version": "4.7.0-alpha.33",
4
4
  "description": "Camunda 8 CLI - minimal-dependency CLI for Camunda 8 operations",
5
5
  "type": "module",
6
6
  "engines": {