Skip to content

Commit

Permalink
Simplify logic using minTakeoverVersion as a flag
Browse files Browse the repository at this point in the history
This commit adds `minTakeoverVersion` to indicate a version jump that requires a takeover instead of
`balenahup`. This is used by `getHupActionType` to identify the need
for a takeover update.
  • Loading branch information
pipex committed Oct 23, 2024
1 parent f522a6b commit 8f8a770
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 35 deletions.
4 changes: 2 additions & 2 deletions lib/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -83,10 +83,10 @@ export const actionsConfig: ActionsConfig = {
},
},
'jetson-xavier-nx-devkit-emmc': {
takeover: {
balenahup: {
// NOTE: this version is here as a placeholder for
// testing. Replace with the correct version before merging
minTargetVersion: '5.1.45+rev1',
minTakeoverVersion: '5.1.45+rev1',
},
},
qemux86: {
Expand Down
39 changes: 14 additions & 25 deletions lib/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ export class HUPActionHelper {
deviceType: string,
currentVersion: string,
targetVersion: string,
) {
): ActionName | 'takeover' {
const currentVersionParsed = bSemver.parse(currentVersion);
if (currentVersionParsed == null) {
throw new HUPActionError('Invalid current balenaOS version');
Expand Down Expand Up @@ -120,10 +120,10 @@ export class HUPActionHelper {
);
}
} else {
// Takeover overrides the checks below for the device type
if (this.isTakeoverRequired(deviceType, currentVersion, targetVersion)) {
return 'takeover';
}
// // Takeover overrides the checks below for the device type
// if (this.isTakeoverRequired(deviceType, currentVersion, targetVersion)) {
// return 'takeover';
// }
actionName = 'balenahup';
}

Expand All @@ -144,6 +144,7 @@ export class HUPActionHelper {
minSourceVersion,
targetMajorVersion,
minTargetVersion,
minTakeoverVersion,
maxTargetVersion,
} = {
...actionsConfig.actions[actionName],
Expand Down Expand Up @@ -179,28 +180,16 @@ export class HUPActionHelper {
);
}

return actionName;
}

private isTakeoverRequired(
deviceType: string,
currentVersion: string,
targetVersion: string,
) {
const { actionsConfig } = this;
const deviceActions = actionsConfig.deviceTypes[deviceType] || {};

if (deviceActions.takeover == null) {
return false;
if (actionName === 'balenahup' && minTakeoverVersion != null) {
if (
bSemver.lt(currentVersion, minTakeoverVersion) &&
bSemver.gte(targetVersion, minTakeoverVersion)
) {
return 'takeover';
}
}

const { minTargetVersion } = deviceActions.takeover;
if (
bSemver.lt(currentVersion, minTargetVersion) &&
bSemver.gte(targetVersion, minTargetVersion)
) {
return true;
}
return actionName;
}

/**
Expand Down
14 changes: 6 additions & 8 deletions lib/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,19 +25,17 @@ export interface ActionConfig {
minTargetVersion: string;
// first resinOS version within the major version, that the updater can no longer target (update only to strictly lower versions than this)
maxTargetVersion?: string;
// first balenaOS version that requires a takeover rather than a balenahup. An update with a target larger or equal to this version
// coming from a source version before it will require a takeover rather than a HUP
minTakeoverVersion?: string;
}

// The per device configuration can override the version configuration of the
// action, or define a 'takeover' version when a jump between two versions
// cannot be done with balenahup, but needs a full re-flash
type DeviceTypeConfig = {
[K in ActionName]?: Partial<ActionConfig>;
} & { takeover?: Pick<ActionConfig, 'minTargetVersion'> };

export interface ActionsConfig {
actions: { [K in ActionName]: ActionConfig };
deviceTypesDefaults: { [K in ActionName]?: Partial<ActionConfig> };
deviceTypes: Partial<{
[deviceTypeSlug: string]: DeviceTypeConfig;
[deviceTypeSlug: string]: {
[K in ActionName]?: Partial<ActionConfig>;
};
}>;
}

0 comments on commit 8f8a770

Please sign in to comment.