2019-06-11 12:08:15 +00:00
|
|
|
/*
|
2020-04-15 10:39:27 +00:00
|
|
|
Copyright 2016-2020 Balena Ltd.
|
2019-06-11 12:08:15 +00:00
|
|
|
|
|
|
|
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.
|
|
|
|
*/
|
|
|
|
import { Device } from 'balena-sdk';
|
|
|
|
import { CommandDefinition } from 'capitano';
|
|
|
|
import { stripIndent } from 'common-tags';
|
2020-04-15 10:39:27 +00:00
|
|
|
import { ExpectedError } from '../errors';
|
2020-02-27 14:55:30 +00:00
|
|
|
import { getBalenaSdk } from '../utils/lazy';
|
2019-06-11 12:08:15 +00:00
|
|
|
import { normalizeUuidProp } from '../utils/normalization';
|
|
|
|
import * as commandOptions from './command-options';
|
|
|
|
|
|
|
|
// tslint:disable-next-line:no-namespace
|
|
|
|
namespace OsUpdate {
|
|
|
|
export interface Args {
|
|
|
|
uuid: string;
|
|
|
|
}
|
|
|
|
|
|
|
|
export type Options = commandOptions.OptionalOsVersionOption &
|
|
|
|
commandOptions.YesOption;
|
|
|
|
}
|
|
|
|
|
|
|
|
export const osUpdate: CommandDefinition<OsUpdate.Args, OsUpdate.Options> = {
|
|
|
|
signature: 'device os-update <uuid>',
|
|
|
|
description: 'Start a Host OS update for a device',
|
|
|
|
help: stripIndent`
|
|
|
|
Use this command to trigger a Host OS update for a device.
|
|
|
|
|
|
|
|
Notice this command will ask for confirmation interactively.
|
|
|
|
You can avoid this by passing the \`--yes\` boolean option.
|
|
|
|
|
2020-03-26 12:19:35 +00:00
|
|
|
Requires balenaCloud; will not work with openBalena or standalone balenaOS.
|
|
|
|
|
2019-06-11 12:08:15 +00:00
|
|
|
Examples:
|
|
|
|
|
|
|
|
$ balena device os-update 23c73a1
|
|
|
|
$ balena device os-update 23c73a1 --version 2.31.0+rev1.prod
|
|
|
|
`,
|
|
|
|
options: [commandOptions.optionalOsVersion, commandOptions.yes],
|
|
|
|
permission: 'user',
|
2020-02-28 13:12:28 +00:00
|
|
|
async action(params, options) {
|
2019-06-11 12:08:15 +00:00
|
|
|
normalizeUuidProp(params);
|
|
|
|
const _ = await import('lodash');
|
2020-02-27 14:55:30 +00:00
|
|
|
const sdk = getBalenaSdk();
|
2019-06-11 12:08:15 +00:00
|
|
|
const patterns = await import('../utils/patterns');
|
|
|
|
const form = await import('resin-cli-form');
|
|
|
|
|
2020-04-15 10:39:27 +00:00
|
|
|
// Get device info
|
|
|
|
const {
|
|
|
|
uuid,
|
|
|
|
device_type,
|
|
|
|
os_version,
|
|
|
|
os_variant,
|
|
|
|
} = await sdk.models.device.get(params.uuid, {
|
|
|
|
$select: ['uuid', 'device_type', 'os_version', 'os_variant'],
|
|
|
|
});
|
|
|
|
|
|
|
|
// Get current device OS version
|
|
|
|
const currentOsVersion = sdk.models.device.getOsVersion({
|
|
|
|
os_version,
|
|
|
|
os_variant,
|
|
|
|
} as Device);
|
|
|
|
if (!currentOsVersion) {
|
|
|
|
throw new ExpectedError(
|
|
|
|
'The current os version of the device is not available',
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Get supported OS update versions
|
|
|
|
const hupVersionInfo = await sdk.models.os.getSupportedOsUpdateVersions(
|
|
|
|
device_type,
|
|
|
|
currentOsVersion,
|
|
|
|
);
|
|
|
|
if (hupVersionInfo.versions.length === 0) {
|
|
|
|
throw new ExpectedError(
|
|
|
|
'There are no available Host OS update targets for this device',
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Get target OS version
|
|
|
|
let targetOsVersion = options.version;
|
|
|
|
if (targetOsVersion != null) {
|
|
|
|
if (!_.includes(hupVersionInfo.versions, targetOsVersion)) {
|
|
|
|
throw new ExpectedError(
|
|
|
|
`The provided version ${targetOsVersion} is not in the Host OS update targets for this device`,
|
|
|
|
);
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
targetOsVersion = await form.ask({
|
|
|
|
message: 'Target OS version',
|
|
|
|
type: 'list',
|
|
|
|
choices: hupVersionInfo.versions.map(version => ({
|
|
|
|
name:
|
|
|
|
hupVersionInfo.recommended === version
|
|
|
|
? `${version} (recommended)`
|
|
|
|
: version,
|
|
|
|
value: version,
|
|
|
|
})),
|
2020-02-28 13:12:28 +00:00
|
|
|
});
|
2020-04-15 10:39:27 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Confirm and start update
|
|
|
|
await patterns.confirm(
|
|
|
|
options.yes || false,
|
|
|
|
'Host OS updates require a device restart when they complete. Are you sure you want to proceed?',
|
|
|
|
);
|
|
|
|
|
|
|
|
await sdk.models.device.startOsUpdate(uuid, targetOsVersion);
|
|
|
|
await patterns.awaitDeviceOsUpdate(uuid, targetOsVersion);
|
2019-06-11 12:08:15 +00:00
|
|
|
},
|
|
|
|
};
|