From b50a278725d350a15d2e89556fee6afed5154f0b Mon Sep 17 00:00:00 2001 From: Max Hauser Date: Wed, 4 Dec 2024 16:08:00 +0100 Subject: [PATCH] allow to specify version for 'iob upgrade self' (#2979) --- CHANGELOG.md | 1 + packages/cli/src/lib/setup.ts | 12 ++- packages/cli/src/lib/setup/setupInstall.ts | 2 +- packages/cli/src/lib/setup/setupUpgrade.ts | 117 +++++++++------------ 4 files changed, 61 insertions(+), 71 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 963508d32b..a2c46824e3 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -8,6 +8,7 @@ * (@Apollon77) Fixes async usage of extendObject * (@Apollon77) Makes setObject async save * (@foxriver76) deprecated `set(Foreign)ObjectAsync` as the non async methods are now working correctly with promises +* (@foxriver76) allow to specify a version on `iob upgrade self` command ## 7.0.3 (2024-11-13) - Lucy * (@foxriver76) Introduce "Vendor Packages Workflow" (only relevant for vendors - see README.md) diff --git a/packages/cli/src/lib/setup.ts b/packages/cli/src/lib/setup.ts index cbd4bde2e3..24da3cf8b3 100644 --- a/packages/cli/src/lib/setup.ts +++ b/packages/cli/src/lib/setup.ts @@ -214,7 +214,7 @@ function initYargs(): ReturnType { {}, ) .command( - 'self []', + 'self[@] []', 'Upgrade js-controller, optionally you can specify the repository url', {}, ) @@ -1196,9 +1196,15 @@ async function processCommand( if (adapter) { try { - if (adapter === 'self') { + if (adapter.split('@')[0] === 'self') { const hostAlive = await states.getStateAsync(`system.host.${tools.getHostName()}.alive`); - await upgrade.upgradeController('', params.force || params.f, !!hostAlive?.val); + const version = adapter.split('@')[1]; + + await upgrade.upgradeController({ + forceDowngrade: params.force || params.f, + controllerRunning: !!hostAlive?.val, + version, + }); } else { await upgrade.upgradeAdapter( '', diff --git a/packages/cli/src/lib/setup/setupInstall.ts b/packages/cli/src/lib/setup/setupInstall.ts index a8890804a5..94bc638f04 100644 --- a/packages/cli/src/lib/setup/setupInstall.ts +++ b/packages/cli/src/lib/setup/setupInstall.ts @@ -154,7 +154,7 @@ export class Install { * @param stoppedList list of stopped instances (as instance objects) */ async downloadPacket( - repoUrlOrRepo: string | undefined | Record, + repoUrlOrRepo: string | undefined | Record, packetName: string, options?: CLIDownloadPacketOptions, stoppedList?: ioBroker.InstanceObject[], diff --git a/packages/cli/src/lib/setup/setupUpgrade.ts b/packages/cli/src/lib/setup/setupUpgrade.ts index 0fb3b1f059..98cc1785c9 100644 --- a/packages/cli/src/lib/setup/setupUpgrade.ts +++ b/packages/cli/src/lib/setup/setupUpgrade.ts @@ -24,6 +24,17 @@ interface CLIUpgradeOptions { params: Record; } +interface UpgradeControllerOptions { + /** The repo or url */ + repoUrl?: string; + /** If downgrades are allowed */ + forceDowngrade: boolean; + /** If controller is currently running */ + controllerRunning: boolean; + /** Version to upgrade controller too */ + version?: string; +} + export class Upgrade { private readonly hostname = tools.getHostName(); private readonly upload: Upload; @@ -652,23 +663,11 @@ export class Upgrade { /** * Upgrade the js-controller * - * @param repoUrl the repo or url - * @param forceDowngrade if downgrades are allowed - * @param controllerRunning if controller is currently running + * @param options additional information like if controller running, optional url, version */ - async upgradeController(repoUrl: string, forceDowngrade: boolean, controllerRunning: boolean): Promise { - let sources: Record; - - try { - const result = await getRepository({ repoName: repoUrl, objects: this.objects }); - if (!result) { - return console.warn(`Cannot get repository under "${repoUrl}"`); - } - sources = result; - } catch (e) { - console.error(e.message); - return this.processExit(e instanceof IoBrokerError ? e.code : e); - } + async upgradeController(options: UpgradeControllerOptions): Promise { + const { repoUrl, forceDowngrade, controllerRunning, version } = options; + let targetVersion = version; const installed = fs.readJSONSync(`${tools.getControllerDir()}/io-package.json`); if (!installed || !installed.common || !installed.common.version) { @@ -680,63 +679,47 @@ export class Upgrade { } const controllerName = installed.common.name; - /** Repository entry of the controller */ - const repoController = sources[controllerName]; + let sources: Record | undefined; - if (!repoController) { + if (targetVersion === undefined) { + try { + const result = await getRepository({ repoName: repoUrl, objects: this.objects }); + if (!result) { + return console.warn(`Cannot get repository under "${repoUrl}"`); + } + sources = result; + } catch (e) { + console.error(e.message); + return this.processExit(e instanceof IoBrokerError ? e.code : e); + } + + /** Repository entry of the controller */ + const repoController = sources[controllerName]; + targetVersion = version ?? repoController.version; + } + + if (!targetVersion) { // no info for controller return console.error(`Cannot find this controller "${controllerName}" in repository.`); } - if (repoController.version) { - if ( - !forceDowngrade && - (repoController.version === installed.common.version || - tools.upToDate(repoController.version, installed.common.version)) - ) { - console.log( - `Host "${this.hostname}"${ - this.hostname.length < 15 ? new Array(15 - this.hostname.length).join(' ') : '' - } is up to date.`, - ); - } else if (controllerRunning) { - console.warn(`Controller is running. Please stop ioBroker first.`); - } else { - console.log(`Update ${controllerName} from @${installed.common.version} to @${repoController.version}`); - // Get the controller from website - await this.install.downloadPacket(sources, `${controllerName}@${repoController.version}`, { - stopDb: true, - }); - } + if ( + !forceDowngrade && + (targetVersion === installed.common.version || tools.upToDate(targetVersion, installed.common.version)) + ) { + console.log( + `Host "${this.hostname}"${ + this.hostname.length < 15 ? new Array(15 - this.hostname.length).join(' ') : '' + } is up to date.`, + ); + } else if (controllerRunning) { + console.warn(`Controller is running. Please stop ioBroker first.`); } else { - const ioPack = await tools.getJsonAsync(repoController.meta); - if ((!ioPack || !ioPack.common) && !forceDowngrade) { - return console.warn( - `Cannot read version. Write "${tools.appName} upgrade self --force" to upgrade controller anyway.`, - ); - } - let version = ioPack?.common ? ioPack.common.version : ''; - if (version) { - version = `@${version}`; - } - - if ( - (ioPack?.common && ioPack.common.version === installed.common.version) || - (!forceDowngrade && ioPack?.common && tools.upToDate(ioPack.common.version, installed.common.version)) - ) { - console.log( - `Host "${this.hostname}"${ - this.hostname.length < 15 ? new Array(15 - this.hostname.length).join(' ') : '' - } is up to date.`, - ); - } else if (controllerRunning) { - console.warn(`Controller is running. Please stop ioBroker first.`); - } else { - const name = ioPack?.common?.name || controllerName; - console.log(`Update ${name} from @${installed.common.version} to ${version}`); - // Get the controller from website - await this.install.downloadPacket(sources, name + version, { stopDb: true }); - } + console.log(`Update ${controllerName} from @${installed.common.version} to @${targetVersion}`); + // Get the controller from website + await this.install.downloadPacket(sources, `${controllerName}@${targetVersion}`, { + stopDb: true, + }); } } }