mirror of
https://github.com/balena-io/balena-cli.git
synced 2025-06-02 16:00:43 +00:00
devices supported: Use new DeviceType data model as source of truth
Change-type: patch
This commit is contained in:
parent
6e7a0defb7
commit
a254e46118
@ -662,10 +662,11 @@ produce JSON output instead of tabular output
|
|||||||
|
|
||||||
List the supported device types (like 'raspberrypi3' or 'intel-nuc').
|
List the supported device types (like 'raspberrypi3' or 'intel-nuc').
|
||||||
|
|
||||||
The --verbose option adds extra columns/fields to the output, including the
|
The --verbose option may add extra columns/fields to the output. Currently
|
||||||
"STATE" column whose values are one of 'new', 'released' or 'discontinued'.
|
this includes the "STATE" column which is DEPRECATED and whose values are one
|
||||||
However, 'discontinued' device types are only listed if the '--discontinued'
|
of 'new', 'released' or 'discontinued'. However, 'discontinued' device types
|
||||||
option is used.
|
are only listed if the '--discontinued' option is also used, and this option
|
||||||
|
is also DEPRECATED.
|
||||||
|
|
||||||
The --json option is recommended when scripting the output of this command,
|
The --json option is recommended when scripting the output of this command,
|
||||||
because the JSON format is less likely to change and it better represents data
|
because the JSON format is less likely to change and it better represents data
|
||||||
@ -683,7 +684,7 @@ Examples:
|
|||||||
|
|
||||||
#### --discontinued
|
#### --discontinued
|
||||||
|
|
||||||
include "discontinued" device types
|
include "discontinued" device types (DEPRECATED)
|
||||||
|
|
||||||
#### -j, --json
|
#### -j, --json
|
||||||
|
|
||||||
@ -691,7 +692,7 @@ produce JSON output instead of tabular output
|
|||||||
|
|
||||||
#### -v, --verbose
|
#### -v, --verbose
|
||||||
|
|
||||||
add extra columns in the tabular output (ALIASES, ARCH, STATE)
|
add extra columns in the tabular output (DEPRECATED)
|
||||||
|
|
||||||
## device <uuid>
|
## device <uuid>
|
||||||
|
|
||||||
|
@ -1,6 +1,6 @@
|
|||||||
/**
|
/**
|
||||||
* @license
|
* @license
|
||||||
* Copyright 2016-2019 Balena Ltd.
|
* Copyright 2016-2021 Balena Ltd.
|
||||||
*
|
*
|
||||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||||
* you may not use this file except in compliance with the License.
|
* you may not use this file except in compliance with the License.
|
||||||
@ -15,13 +15,13 @@
|
|||||||
* limitations under the License.
|
* limitations under the License.
|
||||||
*/
|
*/
|
||||||
import { flags } from '@oclif/command';
|
import { flags } from '@oclif/command';
|
||||||
import type * as SDK from 'balena-sdk';
|
|
||||||
import * as _ from 'lodash';
|
import * as _ from 'lodash';
|
||||||
import Command from '../../command';
|
import Command from '../../command';
|
||||||
|
|
||||||
import * as cf from '../../utils/common-flags';
|
import * as cf from '../../utils/common-flags';
|
||||||
import { getBalenaSdk, getVisuals, stripIndent } from '../../utils/lazy';
|
import { getBalenaSdk, getVisuals, stripIndent } from '../../utils/lazy';
|
||||||
import { CommandHelp } from '../../utils/oclif-utils';
|
import { CommandHelp } from '../../utils/oclif-utils';
|
||||||
|
import { isV13 } from '../../utils/version';
|
||||||
|
|
||||||
interface FlagsDef {
|
interface FlagsDef {
|
||||||
discontinued: boolean;
|
discontinued: boolean;
|
||||||
@ -30,17 +30,24 @@ interface FlagsDef {
|
|||||||
verbose?: boolean;
|
verbose?: boolean;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
const deprecatedInfo = isV13()
|
||||||
|
? ''
|
||||||
|
: `
|
||||||
|
The --verbose option may add extra columns/fields to the output. Currently
|
||||||
|
this includes the "STATE" column which is DEPRECATED and whose values are one
|
||||||
|
of 'new', 'released' or 'discontinued'. However, 'discontinued' device types
|
||||||
|
are only listed if the '--discontinued' option is also used, and this option
|
||||||
|
is also DEPRECATED.
|
||||||
|
`
|
||||||
|
.split('\n')
|
||||||
|
.join(`\n\t\t`);
|
||||||
|
|
||||||
export default class DevicesSupportedCmd extends Command {
|
export default class DevicesSupportedCmd extends Command {
|
||||||
public static description = stripIndent`
|
public static description = stripIndent`
|
||||||
List the supported device types (like 'raspberrypi3' or 'intel-nuc').
|
List the supported device types (like 'raspberrypi3' or 'intel-nuc').
|
||||||
|
|
||||||
List the supported device types (like 'raspberrypi3' or 'intel-nuc').
|
List the supported device types (like 'raspberrypi3' or 'intel-nuc').
|
||||||
|
${deprecatedInfo}
|
||||||
The --verbose option adds extra columns/fields to the output, including the
|
|
||||||
"STATE" column whose values are one of 'new', 'released' or 'discontinued'.
|
|
||||||
However, 'discontinued' device types are only listed if the '--discontinued'
|
|
||||||
option is used.
|
|
||||||
|
|
||||||
The --json option is recommended when scripting the output of this command,
|
The --json option is recommended when scripting the output of this command,
|
||||||
because the JSON format is less likely to change and it better represents data
|
because the JSON format is less likely to change and it better represents data
|
||||||
types like lists and empty strings (for example, the ALIASES column contains a
|
types like lists and empty strings (for example, the ALIASES column contains a
|
||||||
@ -60,7 +67,9 @@ export default class DevicesSupportedCmd extends Command {
|
|||||||
|
|
||||||
public static flags: flags.Input<FlagsDef> = {
|
public static flags: flags.Input<FlagsDef> = {
|
||||||
discontinued: flags.boolean({
|
discontinued: flags.boolean({
|
||||||
description: 'include "discontinued" device types',
|
description: isV13()
|
||||||
|
? 'No effect (DEPRECATED)'
|
||||||
|
: 'include "discontinued" device types (DEPRECATED)',
|
||||||
}),
|
}),
|
||||||
help: cf.help,
|
help: cf.help,
|
||||||
json: flags.boolean({
|
json: flags.boolean({
|
||||||
@ -69,45 +78,71 @@ export default class DevicesSupportedCmd extends Command {
|
|||||||
}),
|
}),
|
||||||
verbose: flags.boolean({
|
verbose: flags.boolean({
|
||||||
char: 'v',
|
char: 'v',
|
||||||
description:
|
description: isV13()
|
||||||
'add extra columns in the tabular output (ALIASES, ARCH, STATE)',
|
? 'No effect (DEPRECATED)'
|
||||||
|
: 'add extra columns in the tabular output (DEPRECATED)',
|
||||||
}),
|
}),
|
||||||
};
|
};
|
||||||
|
|
||||||
public async run() {
|
public async run() {
|
||||||
const { flags: options } = this.parse<FlagsDef, {}>(DevicesSupportedCmd);
|
const { flags: options } = this.parse<FlagsDef, {}>(DevicesSupportedCmd);
|
||||||
const dts = await getBalenaSdk().models.config.getDeviceTypes();
|
const [dts, configDTs] = await Promise.all([
|
||||||
let deviceTypes: Array<Partial<SDK.DeviceTypeJson.DeviceType>> = dts.map(
|
getBalenaSdk().models.deviceType.getAllSupported({
|
||||||
(d) => {
|
$expand: { is_of__cpu_architecture: { $select: 'slug' } },
|
||||||
if (d.aliases && d.aliases.length) {
|
$select: ['slug', 'name'],
|
||||||
// remove aliases that are equal to the slug
|
}),
|
||||||
d.aliases = d.aliases.filter((alias: string) => alias !== d.slug);
|
getBalenaSdk().models.config.getDeviceTypes(),
|
||||||
if (!options.json) {
|
]);
|
||||||
// stringify the aliases array with commas and spaces
|
const dtsBySlug = _.keyBy(dts, (dt) => dt.slug);
|
||||||
d.aliases = [d.aliases.join(', ')];
|
const configDTsBySlug = _.keyBy(configDTs, (dt) => dt.slug);
|
||||||
|
const discontinuedDTs = isV13()
|
||||||
|
? []
|
||||||
|
: configDTs.filter((dt) => dt.state === 'DISCONTINUED');
|
||||||
|
const discontinuedDTsBySlug = _.keyBy(discontinuedDTs, (dt) => dt.slug);
|
||||||
|
// set of slugs from models.deviceType.getAllSupported() plus slugs of
|
||||||
|
// discontinued device types as per models.config.getDeviceTypes()
|
||||||
|
const slugsOfInterest = new Set([
|
||||||
|
...Object.keys(dtsBySlug),
|
||||||
|
...Object.keys(discontinuedDTsBySlug),
|
||||||
|
]);
|
||||||
|
interface DT {
|
||||||
|
slug: string;
|
||||||
|
aliases: string[];
|
||||||
|
arch: string;
|
||||||
|
state?: string; // to be removed in CLI v13
|
||||||
|
name: string;
|
||||||
}
|
}
|
||||||
} else {
|
let deviceTypes: DT[] = [];
|
||||||
// ensure it is always an array (for the benefit of JSON output)
|
for (const slug of slugsOfInterest) {
|
||||||
d.aliases = [];
|
const configDT: Partial<typeof configDTs[0]> =
|
||||||
|
configDTsBySlug[slug] || {};
|
||||||
|
if (configDT.state === 'DISCONTINUED' && !options.discontinued) {
|
||||||
|
continue;
|
||||||
}
|
}
|
||||||
return d;
|
const dt: Partial<typeof dts[0]> = dtsBySlug[slug] || {};
|
||||||
},
|
const aliases = (configDT.aliases || []).filter(
|
||||||
|
(alias) => alias !== slug,
|
||||||
);
|
);
|
||||||
if (!options.discontinued) {
|
deviceTypes.push({
|
||||||
deviceTypes = deviceTypes.filter((dt) => dt.state !== 'DISCONTINUED');
|
slug,
|
||||||
|
aliases: options.json ? aliases : [aliases.join(', ')],
|
||||||
|
arch:
|
||||||
|
(dt.is_of__cpu_architecture as any)?.[0]?.slug ||
|
||||||
|
configDT.arch ||
|
||||||
|
'n/a',
|
||||||
|
// 'BETA' renamed to 'NEW'
|
||||||
|
// https://www.flowdock.com/app/rulemotion/i-cli/threads/1svvyaf8FAZeSdG4dPJc4kHOvJU
|
||||||
|
state: isV13()
|
||||||
|
? undefined
|
||||||
|
: (configDT.state || 'NEW').replace('BETA', 'NEW'),
|
||||||
|
name: dt.name || configDT.name || 'N/A',
|
||||||
|
});
|
||||||
}
|
}
|
||||||
const fields = options.verbose
|
const fields =
|
||||||
|
options.verbose && !isV13()
|
||||||
? ['slug', 'aliases', 'arch', 'state', 'name']
|
? ['slug', 'aliases', 'arch', 'state', 'name']
|
||||||
: ['slug', 'aliases', 'arch', 'name'];
|
: ['slug', 'aliases', 'arch', 'name'];
|
||||||
deviceTypes = _.sortBy(
|
deviceTypes = _.sortBy(deviceTypes, fields);
|
||||||
deviceTypes.map((d) => {
|
|
||||||
const picked = _.pick(d, fields);
|
|
||||||
// 'BETA' renamed to 'NEW'
|
|
||||||
picked.state = picked.state === 'BETA' ? 'NEW' : picked.state;
|
|
||||||
return picked;
|
|
||||||
}),
|
|
||||||
fields,
|
|
||||||
);
|
|
||||||
if (options.json) {
|
if (options.json) {
|
||||||
console.log(JSON.stringify(deviceTypes, null, 4));
|
console.log(JSON.stringify(deviceTypes, null, 4));
|
||||||
} else {
|
} else {
|
||||||
|
@ -1,6 +1,6 @@
|
|||||||
/**
|
/**
|
||||||
* @license
|
* @license
|
||||||
* Copyright 2019-2020 Balena Ltd.
|
* Copyright 2019-2021 Balena Ltd.
|
||||||
*
|
*
|
||||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||||
* you may not use this file except in compliance with the License.
|
* you may not use this file except in compliance with the License.
|
||||||
@ -311,7 +311,7 @@ export class BalenaAPIMock extends NockMock {
|
|||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
public expectGetDeviceTypes(opts: ScopeOpts = {}) {
|
public expectGetConfigDeviceTypes(opts: ScopeOpts = {}) {
|
||||||
this.optGet('/device-types/v1', opts).replyWithFile(
|
this.optGet('/device-types/v1', opts).replyWithFile(
|
||||||
200,
|
200,
|
||||||
path.join(apiResponsePath, 'device-types-GET-v1.json'),
|
path.join(apiResponsePath, 'device-types-GET-v1.json'),
|
||||||
@ -319,6 +319,14 @@ export class BalenaAPIMock extends NockMock {
|
|||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public expectGetDeviceTypes(opts: ScopeOpts = {}) {
|
||||||
|
this.optGet(/^\/v\d+\/device_type($|\?)/, opts).replyWithFile(
|
||||||
|
200,
|
||||||
|
path.join(apiResponsePath, 'device-type-GET-v6.json'),
|
||||||
|
jHeader,
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
public expectGetConfigVars(opts: ScopeOpts = {}) {
|
public expectGetConfigVars(opts: ScopeOpts = {}) {
|
||||||
this.optGet('/config/vars', opts).reply(200, {
|
this.optGet('/config/vars', opts).reply(200, {
|
||||||
reservedNames: [],
|
reservedNames: [],
|
||||||
|
@ -1,6 +1,6 @@
|
|||||||
/**
|
/**
|
||||||
* @license
|
* @license
|
||||||
* Copyright 2020 Balena Ltd.
|
* Copyright 2020-2021 Balena Ltd.
|
||||||
*
|
*
|
||||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||||
* you may not use this file except in compliance with the License.
|
* you may not use this file except in compliance with the License.
|
||||||
@ -76,7 +76,7 @@ describe('balena deploy', function () {
|
|||||||
docker = new DockerMock();
|
docker = new DockerMock();
|
||||||
api.expectGetWhoAmI({ optional: true, persist: true });
|
api.expectGetWhoAmI({ optional: true, persist: true });
|
||||||
api.expectGetMixpanel({ optional: true });
|
api.expectGetMixpanel({ optional: true });
|
||||||
api.expectGetDeviceTypes();
|
api.expectGetConfigDeviceTypes();
|
||||||
api.expectGetApplication();
|
api.expectGetApplication();
|
||||||
api.expectPostRelease();
|
api.expectPostRelease();
|
||||||
api.expectGetRelease();
|
api.expectGetRelease();
|
||||||
|
@ -1,6 +1,6 @@
|
|||||||
/**
|
/**
|
||||||
* @license
|
* @license
|
||||||
* Copyright 2019-2020 Balena Ltd.
|
* Copyright 2019-2021 Balena Ltd.
|
||||||
*
|
*
|
||||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||||
* you may not use this file except in compliance with the License.
|
* you may not use this file except in compliance with the License.
|
||||||
@ -20,6 +20,8 @@ import { expect } from 'chai';
|
|||||||
import { BalenaAPIMock } from '../../balena-api-mock';
|
import { BalenaAPIMock } from '../../balena-api-mock';
|
||||||
import { cleanOutput, runCommand } from '../../helpers';
|
import { cleanOutput, runCommand } from '../../helpers';
|
||||||
|
|
||||||
|
import { isV13 } from '../../../lib/utils/version';
|
||||||
|
|
||||||
describe('balena devices supported', function () {
|
describe('balena devices supported', function () {
|
||||||
let api: BalenaAPIMock;
|
let api: BalenaAPIMock;
|
||||||
|
|
||||||
@ -44,20 +46,35 @@ describe('balena devices supported', function () {
|
|||||||
|
|
||||||
it('should list currently supported devices, with correct filtering', async () => {
|
it('should list currently supported devices, with correct filtering', async () => {
|
||||||
api.expectGetDeviceTypes();
|
api.expectGetDeviceTypes();
|
||||||
|
api.expectGetConfigDeviceTypes();
|
||||||
|
|
||||||
const { out, err } = await runCommand('devices supported');
|
const { out, err } = await runCommand('devices supported -v');
|
||||||
|
|
||||||
const lines = cleanOutput(out);
|
const lines = cleanOutput(out, true);
|
||||||
|
|
||||||
expect(lines[0].replace(/ +/g, ' ')).to.equal('SLUG ALIASES ARCH NAME');
|
expect(lines[0]).to.equal(
|
||||||
|
isV13() ? 'SLUG ALIASES ARCH NAME' : 'SLUG ALIASES ARCH STATE NAME',
|
||||||
|
);
|
||||||
expect(lines).to.have.lengthOf.at.least(2);
|
expect(lines).to.have.lengthOf.at.least(2);
|
||||||
|
expect(lines).to.contain(
|
||||||
|
isV13()
|
||||||
|
? 'intel-nuc nuc amd64 Intel NUC'
|
||||||
|
: 'intel-nuc nuc amd64 RELEASED Intel NUC',
|
||||||
|
);
|
||||||
|
expect(lines).to.contain(
|
||||||
|
isV13()
|
||||||
|
? 'odroid-xu4 odroid-ux3, odroid-u3+ armv7hf ODROID-XU4'
|
||||||
|
: 'odroid-xu4 odroid-ux3, odroid-u3+ armv7hf RELEASED ODROID-XU4',
|
||||||
|
);
|
||||||
|
|
||||||
// Discontinued devices should be filtered out from results
|
// Discontinued devices should be filtered out from results
|
||||||
expect(lines.some((l) => l.includes('DISCONTINUED'))).to.be.false;
|
expect(lines.some((l) => l.includes('DISCONTINUED'))).to.be.false;
|
||||||
|
|
||||||
// Experimental devices should be listed as new
|
// Beta devices should be listed as new
|
||||||
expect(lines.some((l) => l.includes('EXPERIMENTAL'))).to.be.false;
|
expect(lines.some((l) => l.includes('BETA'))).to.be.false;
|
||||||
expect(lines.some((l) => l.includes('NEW'))).to.be.true;
|
expect(lines.some((l) => l.includes('NEW'))).to.equal(
|
||||||
|
isV13() ? false : true,
|
||||||
|
);
|
||||||
|
|
||||||
expect(err).to.eql([]);
|
expect(err).to.eql([]);
|
||||||
});
|
});
|
||||||
|
@ -1,3 +1,20 @@
|
|||||||
|
/**
|
||||||
|
* @license
|
||||||
|
* Copyright 2020-2021 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 { expect } from 'chai';
|
import { expect } from 'chai';
|
||||||
import { promises as fs } from 'fs';
|
import { promises as fs } from 'fs';
|
||||||
import * as process from 'process';
|
import * as process from 'process';
|
||||||
@ -30,7 +47,7 @@ if (process.platform !== 'win32') {
|
|||||||
|
|
||||||
it('should inject a valid config.json file', async () => {
|
it('should inject a valid config.json file', async () => {
|
||||||
api.expectGetApplication();
|
api.expectGetApplication();
|
||||||
api.expectGetDeviceTypes();
|
api.expectGetConfigDeviceTypes();
|
||||||
api.expectDownloadConfig();
|
api.expectDownloadConfig();
|
||||||
api.expectApplicationProvisioning();
|
api.expectApplicationProvisioning();
|
||||||
|
|
||||||
|
1634
tests/test-data/api-response/device-type-GET-v6.json
Normal file
1634
tests/test-data/api-response/device-type-GET-v6.json
Normal file
File diff suppressed because it is too large
Load Diff
Loading…
x
Reference in New Issue
Block a user