2019-09-11 13:35:06 +00:00
|
|
|
/**
|
|
|
|
* @license
|
|
|
|
* Copyright 2019 Balena Ltd.
|
|
|
|
*
|
|
|
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
* you may not use this file except in compliance with the License.
|
|
|
|
* You may obtain a copy of the License at
|
|
|
|
*
|
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
*
|
|
|
|
* Unless required by applicable law or agreed to in writing, software
|
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
* See the License for the specific language governing permissions and
|
|
|
|
* limitations under the License.
|
|
|
|
*/
|
|
|
|
|
2020-02-27 12:38:50 +00:00
|
|
|
import type * as SDK from 'balena-sdk';
|
2019-12-04 18:04:57 +00:00
|
|
|
import * as _ from 'lodash';
|
2020-07-13 13:07:51 +00:00
|
|
|
import { getBalenaSdk, getCliForm, getVisuals, stripIndent } from './lazy';
|
2019-04-24 10:31:45 +00:00
|
|
|
|
2019-12-04 18:04:57 +00:00
|
|
|
import { ExpectedError } from '../errors';
|
|
|
|
|
|
|
|
export const serviceIdToName = _.memoize(
|
|
|
|
async (
|
|
|
|
sdk: SDK.BalenaSDK,
|
|
|
|
serviceId: number,
|
|
|
|
): Promise<string | undefined> => {
|
|
|
|
const serviceName = await sdk.pine.get<SDK.Service>({
|
2019-04-24 10:31:45 +00:00
|
|
|
resource: 'service',
|
|
|
|
id: serviceId,
|
|
|
|
options: {
|
|
|
|
$select: 'service_name',
|
|
|
|
},
|
|
|
|
});
|
|
|
|
|
|
|
|
if (serviceName != null) {
|
|
|
|
return serviceName.service_name;
|
|
|
|
}
|
|
|
|
return;
|
|
|
|
},
|
|
|
|
// Memoize the call based on service id
|
|
|
|
(_sdk, id) => id.toString(),
|
|
|
|
);
|
2019-12-04 18:04:57 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Return Device and Application objects for the given device UUID (short UUID
|
|
|
|
* or full UUID). An error is thrown if the application is not accessible, e.g.
|
|
|
|
* if the application owner removed the current user as a collaborator (but the
|
|
|
|
* device still belongs to the current user).
|
|
|
|
*/
|
|
|
|
export const getDeviceAndAppFromUUID = _.memoize(
|
|
|
|
async (
|
|
|
|
sdk: SDK.BalenaSDK,
|
|
|
|
deviceUUID: string,
|
|
|
|
selectDeviceFields?: Array<keyof SDK.Device>,
|
|
|
|
selectAppFields?: Array<keyof SDK.Application>,
|
|
|
|
): Promise<[SDK.Device, SDK.Application]> => {
|
|
|
|
const [device, app] = await getDeviceAndMaybeAppFromUUID(
|
|
|
|
sdk,
|
|
|
|
deviceUUID,
|
|
|
|
selectDeviceFields,
|
|
|
|
selectAppFields,
|
|
|
|
);
|
|
|
|
if (app == null) {
|
|
|
|
throw new ExpectedError(stripIndent`
|
2021-07-15 13:41:38 +00:00
|
|
|
Unable to access the fleet that device ${deviceUUID} belongs to.
|
|
|
|
Hint: check whether the fleet owner withdrew access to it.
|
2019-12-04 18:04:57 +00:00
|
|
|
`);
|
|
|
|
}
|
|
|
|
return [device, app];
|
|
|
|
},
|
|
|
|
// Memoize the call based on UUID
|
|
|
|
(_sdk, deviceUUID) => deviceUUID,
|
|
|
|
);
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Return a Device object and maybe an Application object for the given device
|
|
|
|
* UUID (short UUID or full UUID). The Application object may be undefined if
|
|
|
|
* the user / device lost access to the application, e.g. if the application
|
|
|
|
* owner removed the user as a collaborator (but the device still belongs to
|
|
|
|
* the current user).
|
|
|
|
*/
|
|
|
|
export const getDeviceAndMaybeAppFromUUID = _.memoize(
|
|
|
|
async (
|
|
|
|
sdk: SDK.BalenaSDK,
|
|
|
|
deviceUUID: string,
|
|
|
|
selectDeviceFields?: Array<keyof SDK.Device>,
|
|
|
|
selectAppFields?: Array<keyof SDK.Application>,
|
|
|
|
): Promise<[SDK.Device, SDK.Application | undefined]> => {
|
|
|
|
const pineOpts = {
|
|
|
|
$expand: selectAppFields
|
|
|
|
? { belongs_to__application: { $select: selectAppFields } }
|
|
|
|
: 'belongs_to__application',
|
2020-07-31 14:35:20 +00:00
|
|
|
} as SDK.PineOptions<SDK.Device>;
|
2019-12-04 18:04:57 +00:00
|
|
|
if (selectDeviceFields) {
|
|
|
|
pineOpts.$select = selectDeviceFields as any;
|
|
|
|
}
|
|
|
|
const device = await sdk.models.device.get(deviceUUID, pineOpts);
|
|
|
|
const apps = device.belongs_to__application as SDK.Application[];
|
|
|
|
if (_.isEmpty(apps) || _.isEmpty(apps[0])) {
|
|
|
|
return [device, undefined];
|
|
|
|
}
|
|
|
|
return [device, apps[0]];
|
|
|
|
},
|
|
|
|
// Memoize the call based on UUID
|
|
|
|
(_sdk, deviceUUID) => deviceUUID,
|
|
|
|
);
|
2020-07-13 13:07:51 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Download balenaOS image for the specified `deviceType`.
|
|
|
|
* `OSVersion` may be one of:
|
|
|
|
* - exact version number,
|
|
|
|
* - valid semver range,
|
|
|
|
* - `latest` (includes pre-releases),
|
|
|
|
* - `default` (excludes pre-releases if at least one stable version is available),
|
|
|
|
* - `recommended` (excludes pre-releases, will fail if only pre-release versions are available),
|
|
|
|
* - `menu` (will show the interactive menu )
|
|
|
|
* If not provided, OSVersion will be set to `default`
|
|
|
|
*
|
|
|
|
* @param deviceType
|
|
|
|
* @param outputPath
|
|
|
|
* @param OSVersion
|
|
|
|
*/
|
|
|
|
export async function downloadOSImage(
|
|
|
|
deviceType: string,
|
|
|
|
outputPath: string,
|
|
|
|
OSVersion?: string,
|
|
|
|
) {
|
|
|
|
console.info(`Getting device operating system for ${deviceType}`);
|
|
|
|
|
|
|
|
if (!OSVersion) {
|
2021-09-13 17:26:08 +00:00
|
|
|
console.warn('OS version not specified: using latest released version');
|
2020-07-13 13:07:51 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
OSVersion = OSVersion
|
|
|
|
? await resolveOSVersion(deviceType, OSVersion)
|
|
|
|
: 'default';
|
|
|
|
|
2021-09-20 17:28:14 +00:00
|
|
|
// Override the default zlib flush value as we've seen cases of
|
|
|
|
// incomplete files being identified as successful downloads when using Z_SYNC_FLUSH.
|
|
|
|
// Using Z_NO_FLUSH results in a Z_BUF_ERROR instead of a corrupt image file.
|
|
|
|
// https://github.com/nodejs/node/blob/master/doc/api/zlib.md#zlib-constants
|
|
|
|
// Hopefully this is a temporary workaround until we can resolve
|
|
|
|
// some ongoing issues with the os download stream.
|
|
|
|
process.env.ZLIB_FLUSH = 'Z_NO_FLUSH';
|
|
|
|
|
2020-07-13 13:07:51 +00:00
|
|
|
const manager = await import('balena-image-manager');
|
|
|
|
const stream = await manager.get(deviceType, OSVersion);
|
|
|
|
|
2021-09-22 23:59:53 +00:00
|
|
|
const displayVersion = await new Promise((resolve, reject) => {
|
|
|
|
stream.on('error', reject);
|
|
|
|
stream.on('balena-image-manager:resolved-version', resolve);
|
|
|
|
});
|
|
|
|
|
2020-07-13 13:07:51 +00:00
|
|
|
const visuals = getVisuals();
|
2021-09-22 23:59:53 +00:00
|
|
|
const bar = new visuals.Progress(
|
|
|
|
`Downloading balenaOS version ${displayVersion}`,
|
|
|
|
);
|
2020-07-13 13:07:51 +00:00
|
|
|
const spinner = new visuals.Spinner(
|
2021-09-22 23:59:53 +00:00
|
|
|
`Downloading balenaOS version ${displayVersion} (size unknown)`,
|
2020-07-13 13:07:51 +00:00
|
|
|
);
|
|
|
|
|
|
|
|
stream.on('progress', (state: any) => {
|
|
|
|
if (state != null) {
|
|
|
|
return bar.update(state);
|
|
|
|
} else {
|
|
|
|
return spinner.start();
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
stream.on('end', () => {
|
|
|
|
spinner.stop();
|
|
|
|
});
|
|
|
|
|
|
|
|
// We completely rely on the `mime` custom property
|
|
|
|
// to make this decision.
|
|
|
|
// The actual stream should be checked instead.
|
|
|
|
let output;
|
|
|
|
if (stream.mime === 'application/zip') {
|
|
|
|
const unzip = await import('node-unzip-2');
|
|
|
|
output = unzip.Extract({ path: outputPath });
|
|
|
|
} else {
|
|
|
|
const fs = await import('fs');
|
|
|
|
output = fs.createWriteStream(outputPath);
|
|
|
|
}
|
|
|
|
|
|
|
|
const streamToPromise = await import('stream-to-promise');
|
|
|
|
await streamToPromise(stream.pipe(output));
|
|
|
|
|
2021-09-13 17:26:08 +00:00
|
|
|
console.info(
|
|
|
|
`balenaOS image version ${displayVersion} downloaded successfully`,
|
|
|
|
);
|
2020-07-13 13:07:51 +00:00
|
|
|
|
|
|
|
return outputPath;
|
|
|
|
}
|
|
|
|
|
2021-09-13 17:26:08 +00:00
|
|
|
async function resolveOSVersion(
|
|
|
|
deviceType: string,
|
|
|
|
version: string,
|
|
|
|
): Promise<string> {
|
2021-12-09 17:34:52 +00:00
|
|
|
if (['menu', 'menu-esr'].includes(version)) {
|
|
|
|
return await selectOSVersionFromMenu(deviceType, version === 'menu-esr');
|
|
|
|
}
|
2021-12-16 02:11:53 +00:00
|
|
|
// Note that `version` may also be 'latest', 'recommended', 'default'
|
2021-12-10 00:38:56 +00:00
|
|
|
if (/^v?\d+\.\d+\.\d+/.test(version)) {
|
|
|
|
if (version[0] === 'v') {
|
|
|
|
version = version.slice(1);
|
|
|
|
}
|
2020-07-13 13:07:51 +00:00
|
|
|
}
|
2021-12-09 17:34:52 +00:00
|
|
|
return version;
|
|
|
|
}
|
2020-07-13 13:07:51 +00:00
|
|
|
|
2021-12-09 17:34:52 +00:00
|
|
|
async function selectOSVersionFromMenu(
|
|
|
|
deviceType: string,
|
|
|
|
esr: boolean,
|
|
|
|
): Promise<string> {
|
2021-12-24 15:02:59 +00:00
|
|
|
const vs = await getOsVersions(deviceType, esr);
|
2020-07-13 13:07:51 +00:00
|
|
|
|
|
|
|
const choices = vs.map((v) => ({
|
2021-12-24 15:02:59 +00:00
|
|
|
value: v.raw_version,
|
|
|
|
name: formatOsVersion(v),
|
2020-07-13 13:07:51 +00:00
|
|
|
}));
|
|
|
|
|
|
|
|
return getCliForm().ask({
|
|
|
|
message: 'Select the OS version:',
|
|
|
|
type: 'list',
|
|
|
|
choices,
|
2021-12-24 15:02:59 +00:00
|
|
|
default: vs[0]?.raw_version,
|
2020-07-13 13:07:51 +00:00
|
|
|
});
|
|
|
|
}
|
2021-09-13 17:26:08 +00:00
|
|
|
|
|
|
|
/**
|
2021-12-24 15:02:59 +00:00
|
|
|
* Return the output of sdk.models.os.getAvailableOsVersions(), resolving
|
|
|
|
* device type aliases and filtering with regard to ESR versions.
|
2021-09-13 17:26:08 +00:00
|
|
|
*/
|
2021-12-24 15:02:59 +00:00
|
|
|
export async function getOsVersions(
|
2021-09-13 17:26:08 +00:00
|
|
|
deviceType: string,
|
|
|
|
esr: boolean,
|
|
|
|
): Promise<SDK.OsVersion[]> {
|
2021-12-10 00:45:14 +00:00
|
|
|
const sdk = getBalenaSdk();
|
|
|
|
let slug = deviceType;
|
2021-12-24 15:02:59 +00:00
|
|
|
let versions: SDK.OsVersion[] = await sdk.models.os.getAvailableOsVersions(
|
|
|
|
slug,
|
|
|
|
);
|
2021-12-10 00:45:14 +00:00
|
|
|
// if slug is an alias, fetch the real slug
|
2021-12-24 15:02:59 +00:00
|
|
|
if (!versions.length) {
|
2022-10-17 14:01:33 +00:00
|
|
|
// unalias device type slug
|
|
|
|
slug = (await sdk.models.deviceType.get(slug, { $select: 'slug' })).slug;
|
2021-12-10 00:45:14 +00:00
|
|
|
if (slug !== deviceType) {
|
2021-12-24 15:02:59 +00:00
|
|
|
versions = await sdk.models.os.getAvailableOsVersions(slug);
|
2021-12-10 00:45:14 +00:00
|
|
|
}
|
|
|
|
}
|
2021-12-24 15:02:59 +00:00
|
|
|
versions = versions.filter(
|
|
|
|
(v: SDK.OsVersion) => v.osType === (esr ? 'esr' : 'default'),
|
|
|
|
);
|
2021-09-13 17:26:08 +00:00
|
|
|
if (!versions.length) {
|
2021-12-10 00:45:14 +00:00
|
|
|
const vType = esr ? 'ESR versions' : 'versions';
|
|
|
|
throw new ExpectedError(
|
|
|
|
`Error: No balenaOS ${vType} found for device type '${deviceType}'.`,
|
|
|
|
);
|
2021-09-13 17:26:08 +00:00
|
|
|
}
|
|
|
|
return versions;
|
|
|
|
}
|
2021-12-24 15:02:59 +00:00
|
|
|
|
|
|
|
export function formatOsVersion(osVersion: SDK.OsVersion): string {
|
|
|
|
return osVersion.line
|
|
|
|
? `v${osVersion.raw_version} (${osVersion.line})`
|
|
|
|
: `v${osVersion.raw_version}`;
|
|
|
|
}
|