2020-06-17 13:46:25 +00:00
|
|
|
/**
|
|
|
|
* @license
|
|
|
|
* Copyright 2016-2020 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.
|
|
|
|
*/
|
|
|
|
|
|
|
|
import { flags } from '@oclif/command';
|
2020-02-27 12:38:50 +00:00
|
|
|
import type { IArg } from '@oclif/parser/lib/args';
|
|
|
|
import type { Application, Device } from 'balena-sdk';
|
2020-06-17 13:46:25 +00:00
|
|
|
import Command from '../../command';
|
|
|
|
import * as cf from '../../utils/common-flags';
|
|
|
|
import { expandForAppName } from '../../utils/helpers';
|
2020-05-04 14:57:23 +00:00
|
|
|
import { getBalenaSdk, stripIndent } from '../../utils/lazy';
|
2020-06-17 13:46:25 +00:00
|
|
|
import { tryAsInteger } from '../../utils/validation';
|
2020-07-15 14:26:31 +00:00
|
|
|
import { ExpectedError } from '../../errors';
|
2020-06-17 13:46:25 +00:00
|
|
|
|
|
|
|
interface ExtendedDevice extends Device {
|
|
|
|
application_name?: string;
|
|
|
|
}
|
|
|
|
|
|
|
|
interface FlagsDef {
|
|
|
|
application?: string;
|
|
|
|
app?: string;
|
|
|
|
help: void;
|
|
|
|
}
|
|
|
|
|
|
|
|
interface ArgsDef {
|
|
|
|
uuid: string;
|
|
|
|
}
|
|
|
|
|
|
|
|
export default class DeviceMoveCmd extends Command {
|
|
|
|
public static description = stripIndent`
|
2020-07-15 14:26:31 +00:00
|
|
|
Move one or more devices to another application.
|
2020-06-17 13:46:25 +00:00
|
|
|
|
2020-07-15 14:26:31 +00:00
|
|
|
Move one or more devices to another application.
|
2020-06-17 13:46:25 +00:00
|
|
|
|
|
|
|
Note, if the application option is omitted it will be prompted
|
|
|
|
for interactively.
|
|
|
|
`;
|
|
|
|
public static examples = [
|
|
|
|
'$ balena device move 7cf02a6',
|
2020-07-15 14:26:31 +00:00
|
|
|
'$ balena device move 7cf02a6,dc39e52',
|
2020-06-17 13:46:25 +00:00
|
|
|
'$ balena device move 7cf02a6 --application MyNewApp',
|
|
|
|
];
|
|
|
|
|
|
|
|
public static args: Array<IArg<any>> = [
|
|
|
|
{
|
|
|
|
name: 'uuid',
|
2020-07-15 14:26:31 +00:00
|
|
|
description:
|
|
|
|
'comma-separated list (no blank spaces) of device UUIDs to be moved',
|
2020-06-17 13:46:25 +00:00
|
|
|
parse: (dev) => tryAsInteger(dev),
|
|
|
|
required: true,
|
|
|
|
},
|
|
|
|
];
|
|
|
|
|
2020-07-15 14:26:31 +00:00
|
|
|
public static usage = 'device move <uuid(s)>';
|
2020-06-17 13:46:25 +00:00
|
|
|
|
|
|
|
public static flags: flags.Input<FlagsDef> = {
|
|
|
|
application: cf.application,
|
|
|
|
app: cf.app,
|
|
|
|
help: cf.help,
|
|
|
|
};
|
|
|
|
|
|
|
|
public static authenticated = true;
|
|
|
|
|
|
|
|
public async run() {
|
|
|
|
const { args: params, flags: options } = this.parse<FlagsDef, ArgsDef>(
|
|
|
|
DeviceMoveCmd,
|
|
|
|
);
|
|
|
|
|
|
|
|
const balena = getBalenaSdk();
|
|
|
|
|
|
|
|
// Consolidate application options
|
|
|
|
options.application = options.application || options.app;
|
|
|
|
delete options.app;
|
|
|
|
|
2020-07-15 14:26:31 +00:00
|
|
|
const devices: ExtendedDevice[] = await Promise.all(
|
|
|
|
params.uuid
|
|
|
|
.split(',')
|
|
|
|
.map((uuid) => balena.models.device.get(uuid, expandForAppName)),
|
2020-06-17 13:46:25 +00:00
|
|
|
);
|
|
|
|
|
2020-07-15 14:26:31 +00:00
|
|
|
for (const device of devices) {
|
|
|
|
const belongsToApplication = device.belongs_to__application as Application[];
|
|
|
|
device.application_name = belongsToApplication?.[0]
|
|
|
|
? belongsToApplication[0].app_name
|
|
|
|
: 'N/a';
|
|
|
|
}
|
2020-06-17 13:46:25 +00:00
|
|
|
|
|
|
|
// Get destination application
|
|
|
|
let application;
|
|
|
|
if (options.application) {
|
|
|
|
application = options.application;
|
|
|
|
} else {
|
2020-07-15 14:26:31 +00:00
|
|
|
const [deviceDeviceTypes, deviceTypes] = await Promise.all([
|
|
|
|
Promise.all(
|
|
|
|
devices.map((device) =>
|
|
|
|
balena.models.device.getManifestBySlug(device.device_type),
|
|
|
|
),
|
|
|
|
),
|
2020-06-17 13:46:25 +00:00
|
|
|
balena.models.config.getDeviceTypes(),
|
|
|
|
]);
|
|
|
|
|
2020-07-15 14:26:31 +00:00
|
|
|
const compatibleDeviceTypes = deviceTypes.filter((dt) =>
|
|
|
|
deviceDeviceTypes.every(
|
|
|
|
(deviceDeviceType) =>
|
|
|
|
balena.models.os.isArchitectureCompatibleWith(
|
|
|
|
deviceDeviceType.arch,
|
|
|
|
dt.arch,
|
|
|
|
) &&
|
|
|
|
!!dt.isDependent === !!deviceDeviceType.isDependent &&
|
|
|
|
dt.state !== 'DISCONTINUED',
|
|
|
|
),
|
2020-06-17 13:46:25 +00:00
|
|
|
);
|
|
|
|
|
2020-07-08 17:21:37 +00:00
|
|
|
const patterns = await import('../../utils/patterns');
|
2020-07-15 14:26:31 +00:00
|
|
|
try {
|
|
|
|
application = await patterns.selectApplication(
|
|
|
|
(app: Application) =>
|
|
|
|
compatibleDeviceTypes.some((dt) => dt.slug === app.device_type) &&
|
|
|
|
// @ts-ignore using the extended device object prop
|
|
|
|
devices.some((device) => device.application_name !== app.app_name),
|
|
|
|
true,
|
|
|
|
);
|
|
|
|
} catch (err) {
|
|
|
|
if (deviceDeviceTypes.length) {
|
|
|
|
throw new ExpectedError(
|
|
|
|
`${err.message}\nDo all devices have a compatible architecture?`,
|
|
|
|
);
|
|
|
|
}
|
|
|
|
throw err;
|
|
|
|
}
|
2020-06-17 13:46:25 +00:00
|
|
|
}
|
|
|
|
|
2020-07-15 14:26:31 +00:00
|
|
|
for (const uuid of params.uuid.split(',')) {
|
|
|
|
try {
|
|
|
|
await balena.models.device.move(uuid, tryAsInteger(application));
|
|
|
|
console.info(`${uuid} was moved to ${application}`);
|
|
|
|
} catch (err) {
|
|
|
|
console.info(`${err.message}, uuid: ${uuid}`);
|
|
|
|
process.exitCode = 1;
|
|
|
|
}
|
|
|
|
}
|
2020-06-17 13:46:25 +00:00
|
|
|
}
|
|
|
|
}
|