Skip to content

Commit

Permalink
allow to specify version for 'iob upgrade self' (#2979)
Browse files Browse the repository at this point in the history
  • Loading branch information
foxriver76 authored Dec 4, 2024
1 parent d297a9d commit b50a278
Show file tree
Hide file tree
Showing 4 changed files with 61 additions and 71 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
12 changes: 9 additions & 3 deletions packages/cli/src/lib/setup.ts
Original file line number Diff line number Diff line change
Expand Up @@ -214,7 +214,7 @@ function initYargs(): ReturnType<typeof yargs> {
{},
)
.command(
'self [<repositoryUrl>]',
'self[@<version>] [<repositoryUrl>]',
'Upgrade js-controller, optionally you can specify the repository url',
{},
)
Expand Down Expand Up @@ -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(
'',
Expand Down
2 changes: 1 addition & 1 deletion packages/cli/src/lib/setup/setupInstall.ts
Original file line number Diff line number Diff line change
Expand Up @@ -154,7 +154,7 @@ export class Install {
* @param stoppedList list of stopped instances (as instance objects)
*/
async downloadPacket(
repoUrlOrRepo: string | undefined | Record<string, any>,
repoUrlOrRepo: string | undefined | Record<string, ioBroker.RepositoryJsonAdapterContent>,
packetName: string,
options?: CLIDownloadPacketOptions,
stoppedList?: ioBroker.InstanceObject[],
Expand Down
117 changes: 50 additions & 67 deletions packages/cli/src/lib/setup/setupUpgrade.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,17 @@ interface CLIUpgradeOptions {
params: Record<string, any>;
}

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;
Expand Down Expand Up @@ -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<void> {
let sources: Record<string, any>;

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<void> {
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) {
Expand All @@ -680,63 +679,47 @@ export class Upgrade {
}

const controllerName = installed.common.name;
/** Repository entry of the controller */
const repoController = sources[controllerName];
let sources: Record<string, ioBroker.RepositoryJsonAdapterContent> | 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,
});
}
}
}

0 comments on commit b50a278

Please sign in to comment.