mirror of
https://github.com/balena-io/balena-cli.git
synced 2025-04-08 03:44:13 +00:00
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:
commit
8babf4c908
@ -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;
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user