Skip to content

Commit b50a278

Browse files
authored
allow to specify version for 'iob upgrade self' (#2979)
1 parent d297a9d commit b50a278

File tree

4 files changed

+61
-71
lines changed

4 files changed

+61
-71
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
* (@Apollon77) Fixes async usage of extendObject
99
* (@Apollon77) Makes setObject async save
1010
* (@foxriver76) deprecated `set(Foreign)ObjectAsync` as the non async methods are now working correctly with promises
11+
* (@foxriver76) allow to specify a version on `iob upgrade self` command
1112

1213
## 7.0.3 (2024-11-13) - Lucy
1314
* (@foxriver76) Introduce "Vendor Packages Workflow" (only relevant for vendors - see README.md)

packages/cli/src/lib/setup.ts

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -214,7 +214,7 @@ function initYargs(): ReturnType<typeof yargs> {
214214
{},
215215
)
216216
.command(
217-
'self [<repositoryUrl>]',
217+
'self[@<version>] [<repositoryUrl>]',
218218
'Upgrade js-controller, optionally you can specify the repository url',
219219
{},
220220
)
@@ -1196,9 +1196,15 @@ async function processCommand(
11961196

11971197
if (adapter) {
11981198
try {
1199-
if (adapter === 'self') {
1199+
if (adapter.split('@')[0] === 'self') {
12001200
const hostAlive = await states.getStateAsync(`system.host.${tools.getHostName()}.alive`);
1201-
await upgrade.upgradeController('', params.force || params.f, !!hostAlive?.val);
1201+
const version = adapter.split('@')[1];
1202+
1203+
await upgrade.upgradeController({
1204+
forceDowngrade: params.force || params.f,
1205+
controllerRunning: !!hostAlive?.val,
1206+
version,
1207+
});
12021208
} else {
12031209
await upgrade.upgradeAdapter(
12041210
'',

packages/cli/src/lib/setup/setupInstall.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -154,7 +154,7 @@ export class Install {
154154
* @param stoppedList list of stopped instances (as instance objects)
155155
*/
156156
async downloadPacket(
157-
repoUrlOrRepo: string | undefined | Record<string, any>,
157+
repoUrlOrRepo: string | undefined | Record<string, ioBroker.RepositoryJsonAdapterContent>,
158158
packetName: string,
159159
options?: CLIDownloadPacketOptions,
160160
stoppedList?: ioBroker.InstanceObject[],

packages/cli/src/lib/setup/setupUpgrade.ts

Lines changed: 50 additions & 67 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,17 @@ interface CLIUpgradeOptions {
2424
params: Record<string, any>;
2525
}
2626

27+
interface UpgradeControllerOptions {
28+
/** The repo or url */
29+
repoUrl?: string;
30+
/** If downgrades are allowed */
31+
forceDowngrade: boolean;
32+
/** If controller is currently running */
33+
controllerRunning: boolean;
34+
/** Version to upgrade controller too */
35+
version?: string;
36+
}
37+
2738
export class Upgrade {
2839
private readonly hostname = tools.getHostName();
2940
private readonly upload: Upload;
@@ -652,23 +663,11 @@ export class Upgrade {
652663
/**
653664
* Upgrade the js-controller
654665
*
655-
* @param repoUrl the repo or url
656-
* @param forceDowngrade if downgrades are allowed
657-
* @param controllerRunning if controller is currently running
666+
* @param options additional information like if controller running, optional url, version
658667
*/
659-
async upgradeController(repoUrl: string, forceDowngrade: boolean, controllerRunning: boolean): Promise<void> {
660-
let sources: Record<string, any>;
661-
662-
try {
663-
const result = await getRepository({ repoName: repoUrl, objects: this.objects });
664-
if (!result) {
665-
return console.warn(`Cannot get repository under "${repoUrl}"`);
666-
}
667-
sources = result;
668-
} catch (e) {
669-
console.error(e.message);
670-
return this.processExit(e instanceof IoBrokerError ? e.code : e);
671-
}
668+
async upgradeController(options: UpgradeControllerOptions): Promise<void> {
669+
const { repoUrl, forceDowngrade, controllerRunning, version } = options;
670+
let targetVersion = version;
672671

673672
const installed = fs.readJSONSync(`${tools.getControllerDir()}/io-package.json`);
674673
if (!installed || !installed.common || !installed.common.version) {
@@ -680,63 +679,47 @@ export class Upgrade {
680679
}
681680

682681
const controllerName = installed.common.name;
683-
/** Repository entry of the controller */
684-
const repoController = sources[controllerName];
682+
let sources: Record<string, ioBroker.RepositoryJsonAdapterContent> | undefined;
685683

686-
if (!repoController) {
684+
if (targetVersion === undefined) {
685+
try {
686+
const result = await getRepository({ repoName: repoUrl, objects: this.objects });
687+
if (!result) {
688+
return console.warn(`Cannot get repository under "${repoUrl}"`);
689+
}
690+
sources = result;
691+
} catch (e) {
692+
console.error(e.message);
693+
return this.processExit(e instanceof IoBrokerError ? e.code : e);
694+
}
695+
696+
/** Repository entry of the controller */
697+
const repoController = sources[controllerName];
698+
targetVersion = version ?? repoController.version;
699+
}
700+
701+
if (!targetVersion) {
687702
// no info for controller
688703
return console.error(`Cannot find this controller "${controllerName}" in repository.`);
689704
}
690705

691-
if (repoController.version) {
692-
if (
693-
!forceDowngrade &&
694-
(repoController.version === installed.common.version ||
695-
tools.upToDate(repoController.version, installed.common.version))
696-
) {
697-
console.log(
698-
`Host "${this.hostname}"${
699-
this.hostname.length < 15 ? new Array(15 - this.hostname.length).join(' ') : ''
700-
} is up to date.`,
701-
);
702-
} else if (controllerRunning) {
703-
console.warn(`Controller is running. Please stop ioBroker first.`);
704-
} else {
705-
console.log(`Update ${controllerName} from @${installed.common.version} to @${repoController.version}`);
706-
// Get the controller from website
707-
await this.install.downloadPacket(sources, `${controllerName}@${repoController.version}`, {
708-
stopDb: true,
709-
});
710-
}
706+
if (
707+
!forceDowngrade &&
708+
(targetVersion === installed.common.version || tools.upToDate(targetVersion, installed.common.version))
709+
) {
710+
console.log(
711+
`Host "${this.hostname}"${
712+
this.hostname.length < 15 ? new Array(15 - this.hostname.length).join(' ') : ''
713+
} is up to date.`,
714+
);
715+
} else if (controllerRunning) {
716+
console.warn(`Controller is running. Please stop ioBroker first.`);
711717
} else {
712-
const ioPack = await tools.getJsonAsync(repoController.meta);
713-
if ((!ioPack || !ioPack.common) && !forceDowngrade) {
714-
return console.warn(
715-
`Cannot read version. Write "${tools.appName} upgrade self --force" to upgrade controller anyway.`,
716-
);
717-
}
718-
let version = ioPack?.common ? ioPack.common.version : '';
719-
if (version) {
720-
version = `@${version}`;
721-
}
722-
723-
if (
724-
(ioPack?.common && ioPack.common.version === installed.common.version) ||
725-
(!forceDowngrade && ioPack?.common && tools.upToDate(ioPack.common.version, installed.common.version))
726-
) {
727-
console.log(
728-
`Host "${this.hostname}"${
729-
this.hostname.length < 15 ? new Array(15 - this.hostname.length).join(' ') : ''
730-
} is up to date.`,
731-
);
732-
} else if (controllerRunning) {
733-
console.warn(`Controller is running. Please stop ioBroker first.`);
734-
} else {
735-
const name = ioPack?.common?.name || controllerName;
736-
console.log(`Update ${name} from @${installed.common.version} to ${version}`);
737-
// Get the controller from website
738-
await this.install.downloadPacket(sources, name + version, { stopDb: true });
739-
}
718+
console.log(`Update ${controllerName} from @${installed.common.version} to @${targetVersion}`);
719+
// Get the controller from website
720+
await this.install.downloadPacket(sources, `${controllerName}@${targetVersion}`, {
721+
stopDb: true,
722+
});
740723
}
741724
}
742725
}

0 commit comments

Comments
 (0)