Merge pull request #2405 from balena-io/2386-os-download-dt-aliases

os download, os versions: Accept device type aliases
This commit is contained in:
bulldozer-balena[bot] 2021-12-10 01:26:38 +00:00 committed by GitHub
commit 8babf4c908
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -107,6 +107,16 @@ export const getDeviceAndMaybeAppFromUUID = _.memoize(
(_sdk, deviceUUID) => deviceUUID,
);
/** Given a device type alias like 'nuc', return the actual slug like 'intel-nuc'. */
export const unaliasDeviceType = _.memoize(async function (
sdk: SDK.BalenaSDK,
deviceType: string,
): Promise<string> {
return (
(await sdk.models.device.getManifestBySlug(deviceType)).slug || deviceType
);
});
/**
* Download balenaOS image for the specified `deviceType`.
* `OSVersion` may be one of:
@ -202,15 +212,15 @@ async function resolveOSVersion(
if (['menu', 'menu-esr'].includes(version)) {
return await selectOSVersionFromMenu(deviceType, version === 'menu-esr');
}
if (version[0] === 'v') {
version = version.slice(1);
}
// The version must end with either '.dev' or '.prod', as expected
// by `balena-image-manager` and the balena SDK. Note that something
// like '2.88.4.prod' is not valid semver (https://semver.org/),
// so we don't even attempt semver parsing here.
if (!version.endsWith('.dev') && !version.endsWith('.prod')) {
version += '.prod';
if (/^v?\d+\.\d+\.\d+/.test(version)) {
if (version[0] === 'v') {
version = version.slice(1);
}
// The version must end with either '.dev' or '.prod', as expected
// by `balena-image-manager` and the balena SDK.
if (!version.endsWith('.dev') && !version.endsWith('.prod')) {
version += '.prod';
}
}
return version;
}
@ -244,11 +254,19 @@ export async function getFormattedOsVersions(
deviceType: string,
esr: boolean,
): Promise<SDK.OsVersion[]> {
const versions: SDK.OsVersion[] = (
(await getBalenaSdk().models.hostapp.getAvailableOsVersions([deviceType]))[
deviceType
] ?? []
)
const sdk = getBalenaSdk();
let slug = deviceType;
let versionsByDT: SDK.OsVersionsByDeviceType =
await sdk.models.hostapp.getAvailableOsVersions([slug]);
// if slug is an alias, fetch the real slug
if (!versionsByDT[slug]?.length) {
// unaliasDeviceType() produces a nice error msg if slug is invalid
slug = await unaliasDeviceType(sdk, slug);
if (slug !== deviceType) {
versionsByDT = await sdk.models.hostapp.getAvailableOsVersions([slug]);
}
}
const versions: SDK.OsVersion[] = (versionsByDT[slug] || [])
.filter((v: SDK.OsVersion) => v.osType === (esr ? 'esr' : 'default'))
.map((v: SDK.OsVersion) => {
const i = v.formattedVersion.indexOf(' ');
@ -259,9 +277,10 @@ export async function getFormattedOsVersions(
return v;
});
if (!versions.length) {
throw new ExpectedError(stripIndent`
Error: No balenaOS versions found for device type '${deviceType}'.
Double-check the device type slug with 'balena devices supported'.`);
const vType = esr ? 'ESR versions' : 'versions';
throw new ExpectedError(
`Error: No balenaOS ${vType} found for device type '${deviceType}'.`,
);
}
return versions;
}