Stop relying on device-type.json for resolving the cpu architecture

Resolves: #2542
Change-type: patch
Signed-off-by: Thodoris Greasidis <thodoris@balena.io>
This commit is contained in:
Thodoris Greasidis 2022-10-17 17:00:29 +03:00
parent a2823fd3ec
commit 57b0dccc7d
4 changed files with 71 additions and 45 deletions

View File

@ -175,19 +175,14 @@ export default class ConfigGenerateCmd extends Command {
const deviceType = options.deviceType || resourceDeviceType; const deviceType = options.deviceType || resourceDeviceType;
const deviceManifest = await balena.models.device.getManifestBySlug(
deviceType,
);
// Check compatibility if application and deviceType provided // Check compatibility if application and deviceType provided
if (options.fleet && options.deviceType) { if (options.fleet && options.deviceType) {
const appDeviceManifest = await balena.models.device.getManifestBySlug(
resourceDeviceType,
);
const helpers = await import('../../utils/helpers'); const helpers = await import('../../utils/helpers');
if ( if (
!helpers.areDeviceTypesCompatible(appDeviceManifest, deviceManifest) !(await helpers.areDeviceTypesCompatible(
resourceDeviceType,
deviceType,
))
) { ) {
throw new balena.errors.BalenaInvalidDeviceType( throw new balena.errors.BalenaInvalidDeviceType(
`Device type ${options.deviceType} is incompatible with fleet ${options.fleet}`, `Device type ${options.deviceType} is incompatible with fleet ${options.fleet}`,
@ -195,6 +190,10 @@ export default class ConfigGenerateCmd extends Command {
} }
} }
const deviceManifest = await balena.models.device.getManifestBySlug(
deviceType,
);
// Prompt for values // Prompt for values
// Pass params as an override: if there is any param with exactly the same name as a // Pass params as an override: if there is any param with exactly the same name as a
// required option, that value is used (and the corresponding question is not asked) // required option, that value is used (and the corresponding question is not asked)

View File

@ -215,7 +215,7 @@ export default class OsConfigureCmd extends Command {
is_for__device_type: { $select: 'slug' }, is_for__device_type: { $select: 'slug' },
}, },
})) as ApplicationWithDeviceType; })) as ApplicationWithDeviceType;
await checkDeviceTypeCompatibility(balena, options, app); await checkDeviceTypeCompatibility(options, app);
deviceTypeSlug = deviceTypeSlug =
options['device-type'] || app.is_for__device_type[0].slug; options['device-type'] || app.is_for__device_type[0].slug;
} }
@ -361,17 +361,17 @@ async function getOsVersionFromImage(
* @param app Balena SDK Application model object * @param app Balena SDK Application model object
*/ */
async function checkDeviceTypeCompatibility( async function checkDeviceTypeCompatibility(
sdk: BalenaSdk.BalenaSDK,
options: FlagsDef, options: FlagsDef,
app: ApplicationWithDeviceType, app: ApplicationWithDeviceType,
) { ) {
if (options['device-type']) { if (options['device-type']) {
const [appDeviceType, optionDeviceType] = await Promise.all([
sdk.models.device.getManifestBySlug(app.is_for__device_type[0].slug),
sdk.models.device.getManifestBySlug(options['device-type']),
]);
const helpers = await import('../../utils/helpers'); const helpers = await import('../../utils/helpers');
if (!helpers.areDeviceTypesCompatible(appDeviceType, optionDeviceType)) { if (
!(await helpers.areDeviceTypesCompatible(
app.is_for__device_type[0].slug,
options['device-type'],
))
) {
throw new ExpectedError( throw new ExpectedError(
`Device type ${options['device-type']} is incompatible with fleet ${options.fleet}`, `Device type ${options['device-type']} is incompatible with fleet ${options.fleet}`,
); );

View File

@ -114,14 +114,35 @@ export async function getManifest(
return getBalenaSdk().models.device.getManifestBySlug(deviceType); return getBalenaSdk().models.device.getManifestBySlug(deviceType);
} }
export const areDeviceTypesCompatible = ( export const areDeviceTypesCompatible = async (
appDeviceType: BalenaSdk.DeviceTypeJson.DeviceType, appDeviceTypeSlug: string,
osDeviceType: BalenaSdk.DeviceTypeJson.DeviceType, osDeviceTypeSlug: string,
) => ) => {
getBalenaSdk().models.os.isArchitectureCompatibleWith( if (appDeviceTypeSlug === osDeviceTypeSlug) {
osDeviceType.arch, return true;
appDeviceType.arch, }
) && !!appDeviceType.isDependent === !!osDeviceType.isDependent; const sdk = getBalenaSdk();
const pineOptions = {
$select: 'is_of__cpu_architecture',
$expand: {
is_of__cpu_architecture: {
$select: 'slug',
},
},
} as const;
const [appDeviceType, osDeviceType] = await Promise.all(
[appDeviceTypeSlug, osDeviceTypeSlug].map(
(dtSlug) =>
sdk.models.deviceType.get(dtSlug, pineOptions) as Promise<
BalenaSdk.PineTypedResult<BalenaSdk.DeviceType, typeof pineOptions>
>,
),
);
return sdk.models.os.isArchitectureCompatibleWith(
osDeviceType.is_of__cpu_architecture[0].slug,
appDeviceType.is_of__cpu_architecture[0].slug,
);
};
export async function osProgressHandler(step: InitializeEmitter) { export async function osProgressHandler(step: InitializeEmitter) {
step.on('stdout', process.stdout.write.bind(process.stdout)); step.on('stdout', process.stdout.write.bind(process.stdout));

View File

@ -198,31 +198,37 @@ async function selectAppFromList(
async function getOrSelectApplication( async function getOrSelectApplication(
sdk: BalenaSdk.BalenaSDK, sdk: BalenaSdk.BalenaSDK,
deviceType: string, deviceTypeSlug: string,
appName?: string, appName?: string,
): Promise<ApplicationWithDeviceType> { ): Promise<ApplicationWithDeviceType> {
const _ = await import('lodash'); const pineOptions = {
$select: 'slug',
$expand: {
is_of__cpu_architecture: {
$select: 'slug',
},
},
} as const;
const [deviceType, allDeviceTypes] = await Promise.all([
sdk.models.deviceType.get(deviceTypeSlug, pineOptions) as Promise<
BalenaSdk.PineTypedResult<BalenaSdk.DeviceType, typeof pineOptions>
>,
sdk.models.deviceType.getAllSupported(pineOptions) as Promise<
Array<BalenaSdk.PineTypedResult<BalenaSdk.DeviceType, typeof pineOptions>>
>,
]);
const allDeviceTypes = await sdk.models.config.getDeviceTypes(); const compatibleDeviceTypes = allDeviceTypes
const deviceTypeManifest = _.find(allDeviceTypes, { slug: deviceType }); .filter((dt) =>
if (!deviceTypeManifest) { sdk.models.os.isArchitectureCompatibleWith(
throw new ExpectedError(`"${deviceType}" is not a valid device type`); deviceType.is_of__cpu_architecture[0].slug,
} dt.is_of__cpu_architecture[0].slug,
const compatibleDeviceTypes = _(allDeviceTypes) ),
.filter(
(dt) =>
sdk.models.os.isArchitectureCompatibleWith(
deviceTypeManifest.arch,
dt.arch,
) &&
!!dt.isDependent === !!deviceTypeManifest.isDependent &&
dt.state !== 'DISCONTINUED',
) )
.map((type) => type.slug) .map((type) => type.slug);
.value();
if (!appName) { if (!appName) {
return createOrSelectApp(sdk, compatibleDeviceTypes, deviceType); return createOrSelectApp(sdk, compatibleDeviceTypes, deviceTypeSlug);
} }
const options: BalenaSdk.PineOptions<BalenaSdk.Application> = { const options: BalenaSdk.PineOptions<BalenaSdk.Application> = {
@ -257,13 +263,13 @@ async function getOrSelectApplication(
undefined, undefined,
true, true,
); );
return await createApplication(sdk, deviceType, name); return await createApplication(sdk, deviceTypeSlug, name);
} }
// We've found at least one fleet with the given name. // We've found at least one fleet with the given name.
// Filter out fleets for non-matching device types and see what we're left with. // Filter out fleets for non-matching device types and see what we're left with.
const validApplications = applications.filter((app) => const validApplications = applications.filter((app) =>
_.includes(compatibleDeviceTypes, app.is_for__device_type[0].slug), compatibleDeviceTypes.includes(app.is_for__device_type[0].slug),
); );
if (validApplications.length === 0) { if (validApplications.length === 0) {