2020-04-22 16:08:11 +00:00
|
|
|
/**
|
|
|
|
* @license
|
2021-07-15 13:41:38 +00:00
|
|
|
* Copyright 2016-2021 Balena Ltd.
|
2020-04-22 16:08:11 +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 { flags } from '@oclif/command';
|
2021-07-15 13:41:38 +00:00
|
|
|
import type { Output as ParserOutput } from '@oclif/parser';
|
|
|
|
import type { Application } from 'balena-sdk';
|
|
|
|
|
2020-04-22 16:08:11 +00:00
|
|
|
import Command from '../../command';
|
|
|
|
import { ExpectedError } from '../../errors';
|
|
|
|
import * as cf from '../../utils/common-flags';
|
2020-05-04 14:57:23 +00:00
|
|
|
import { getBalenaSdk, stripIndent } from '../../utils/lazy';
|
2021-07-15 13:41:38 +00:00
|
|
|
import { appToFleetCmdMsg, warnify } from '../../utils/messages';
|
2020-04-22 16:08:11 +00:00
|
|
|
|
|
|
|
interface FlagsDef {
|
2020-12-09 15:05:12 +00:00
|
|
|
organization?: string;
|
2020-04-22 16:08:11 +00:00
|
|
|
type?: string; // application device type
|
|
|
|
help: void;
|
|
|
|
}
|
|
|
|
|
|
|
|
interface ArgsDef {
|
|
|
|
name: string;
|
|
|
|
}
|
|
|
|
|
2021-07-15 13:41:38 +00:00
|
|
|
export class FleetCreateCmd extends Command {
|
2020-04-22 16:08:11 +00:00
|
|
|
public static description = stripIndent`
|
2021-07-15 13:41:38 +00:00
|
|
|
Create a fleet.
|
2020-04-22 16:08:11 +00:00
|
|
|
|
2021-07-15 13:41:38 +00:00
|
|
|
Create a new balena fleet.
|
2020-04-22 16:08:11 +00:00
|
|
|
|
2021-07-15 13:41:38 +00:00
|
|
|
You can specify the organization the fleet should belong to using
|
2020-12-09 15:05:12 +00:00
|
|
|
the \`--organization\` option. The organization's handle, not its name,
|
|
|
|
should be provided. Organization handles can be listed with the
|
|
|
|
\`balena orgs\` command.
|
2020-04-22 16:08:11 +00:00
|
|
|
|
2021-07-15 13:41:38 +00:00
|
|
|
The fleet's default device type is specified with the \`--type\` option.
|
2020-12-09 15:05:12 +00:00
|
|
|
The \`balena devices supported\` command can be used to list the available
|
|
|
|
device types.
|
|
|
|
|
|
|
|
Interactive dropdowns will be shown for selection if no device type or
|
|
|
|
organization is specified and there are multiple options to choose from.
|
|
|
|
If there is a single option to choose from, it will be chosen automatically.
|
|
|
|
This interactive behavior can be disabled by explicitly specifying a device
|
|
|
|
type and organization.
|
|
|
|
`;
|
2020-04-22 16:08:11 +00:00
|
|
|
|
|
|
|
public static examples = [
|
2021-07-15 13:41:38 +00:00
|
|
|
'$ balena fleet create MyFleet',
|
|
|
|
'$ balena fleet create MyFleet --organization mmyorg',
|
|
|
|
'$ balena fleet create MyFleet -o myorg --type raspberry-pi',
|
2020-04-22 16:08:11 +00:00
|
|
|
];
|
|
|
|
|
|
|
|
public static args = [
|
|
|
|
{
|
|
|
|
name: 'name',
|
2021-07-15 13:41:38 +00:00
|
|
|
description: 'fleet name',
|
2020-04-22 16:08:11 +00:00
|
|
|
required: true,
|
|
|
|
},
|
|
|
|
];
|
|
|
|
|
2021-07-15 13:41:38 +00:00
|
|
|
public static usage = 'fleet create <name>';
|
2020-04-22 16:08:11 +00:00
|
|
|
|
|
|
|
public static flags: flags.Input<FlagsDef> = {
|
2020-12-09 15:05:12 +00:00
|
|
|
organization: flags.string({
|
|
|
|
char: 'o',
|
2021-07-15 13:41:38 +00:00
|
|
|
description: 'handle of the organization the fleet should belong to',
|
2020-12-09 15:05:12 +00:00
|
|
|
}),
|
2020-04-22 16:08:11 +00:00
|
|
|
type: flags.string({
|
|
|
|
char: 't',
|
|
|
|
description:
|
2021-07-15 13:41:38 +00:00
|
|
|
'fleet device type (Check available types with `balena devices supported`)',
|
2020-04-22 16:08:11 +00:00
|
|
|
}),
|
|
|
|
help: cf.help,
|
|
|
|
};
|
|
|
|
|
|
|
|
public static authenticated = true;
|
|
|
|
|
2021-07-15 13:41:38 +00:00
|
|
|
public async run(parserOutput?: ParserOutput<FlagsDef, ArgsDef>) {
|
|
|
|
const { args: params, flags: options } =
|
|
|
|
parserOutput || this.parse<FlagsDef, ArgsDef>(FleetCreateCmd);
|
2020-04-22 16:08:11 +00:00
|
|
|
|
2020-12-09 15:05:12 +00:00
|
|
|
// Ascertain device type
|
2020-07-08 17:21:37 +00:00
|
|
|
const deviceType =
|
|
|
|
options.type ||
|
|
|
|
(await (await import('../../utils/patterns')).selectDeviceType());
|
2020-12-09 15:05:12 +00:00
|
|
|
|
|
|
|
// Ascertain organization
|
|
|
|
const organization =
|
|
|
|
options.organization?.toLowerCase() || (await this.getOrganization());
|
|
|
|
|
|
|
|
// Create application
|
|
|
|
let application: Application;
|
2020-05-13 14:44:57 +00:00
|
|
|
try {
|
2020-12-09 15:05:12 +00:00
|
|
|
application = await getBalenaSdk().models.application.create({
|
2020-05-13 14:44:57 +00:00
|
|
|
name: params.name,
|
|
|
|
deviceType,
|
2020-12-09 15:05:12 +00:00
|
|
|
organization,
|
2020-05-13 14:44:57 +00:00
|
|
|
});
|
|
|
|
} catch (err) {
|
|
|
|
if ((err.message || '').toLowerCase().includes('unique')) {
|
2020-12-09 15:05:12 +00:00
|
|
|
// BalenaRequestError: Request error: "organization" and "app_name" must be unique.
|
2020-05-13 14:44:57 +00:00
|
|
|
throw new ExpectedError(
|
2021-07-15 13:41:38 +00:00
|
|
|
`Error: fleet "${params.name}" already exists in organization "${organization}".`,
|
2020-12-09 15:05:12 +00:00
|
|
|
);
|
|
|
|
} else if ((err.message || '').toLowerCase().includes('unauthorized')) {
|
|
|
|
// BalenaRequestError: Request error: Unauthorized
|
|
|
|
throw new ExpectedError(
|
2021-07-15 13:41:38 +00:00
|
|
|
`Error: You are not authorized to create fleets in organization "${organization}".`,
|
2020-05-13 14:44:57 +00:00
|
|
|
);
|
|
|
|
}
|
2020-12-09 15:05:12 +00:00
|
|
|
|
2020-05-13 14:44:57 +00:00
|
|
|
throw err;
|
|
|
|
}
|
2020-12-09 15:05:12 +00:00
|
|
|
|
|
|
|
// Output
|
|
|
|
const { isV13 } = await import('../../utils/version');
|
|
|
|
console.log(
|
|
|
|
isV13()
|
2021-07-15 13:41:38 +00:00
|
|
|
? `Fleet created: slug "${application.slug}", device type "${deviceType}"`
|
|
|
|
: `Fleet created: ${application.slug} (${deviceType}, id ${application.id})`,
|
2020-04-22 16:08:11 +00:00
|
|
|
);
|
|
|
|
}
|
2020-12-09 15:05:12 +00:00
|
|
|
|
|
|
|
async getOrganization() {
|
|
|
|
const { getOwnOrganizations } = await import('../../utils/sdk');
|
|
|
|
const organizations = await getOwnOrganizations(getBalenaSdk());
|
|
|
|
|
|
|
|
if (organizations.length === 0) {
|
|
|
|
// User is not a member of any organizations (should not happen).
|
|
|
|
throw new Error('This account is not a member of any organizations');
|
|
|
|
} else if (organizations.length === 1) {
|
|
|
|
// User is a member of only one organization - use this.
|
|
|
|
return organizations[0].handle;
|
|
|
|
} else {
|
|
|
|
// User is a member of multiple organizations -
|
|
|
|
const { selectOrganization } = await import('../../utils/patterns');
|
|
|
|
return selectOrganization(organizations);
|
|
|
|
}
|
|
|
|
}
|
2020-04-22 16:08:11 +00:00
|
|
|
}
|
2021-07-15 13:41:38 +00:00
|
|
|
|
|
|
|
export default class AppCreateCmd extends FleetCreateCmd {
|
|
|
|
public static description = stripIndent`
|
|
|
|
DEPRECATED alias for the 'fleet create' command
|
|
|
|
|
|
|
|
${appToFleetCmdMsg
|
|
|
|
.split('\n')
|
|
|
|
.map((l) => `\t\t${l}`)
|
|
|
|
.join('\n')}
|
|
|
|
|
|
|
|
For command usage, see 'balena help fleet create'
|
|
|
|
`;
|
|
|
|
public static examples = [];
|
|
|
|
public static usage = 'app create <name>';
|
|
|
|
public static args = FleetCreateCmd.args;
|
|
|
|
public static flags = FleetCreateCmd.flags;
|
|
|
|
public static authenticated = FleetCreateCmd.authenticated;
|
|
|
|
public static primary = FleetCreateCmd.primary;
|
|
|
|
|
|
|
|
public async run() {
|
|
|
|
// call this.parse() before deprecation message to parse '-h'
|
|
|
|
const parserOutput = this.parse<FlagsDef, ArgsDef>(AppCreateCmd);
|
|
|
|
if (process.stderr.isTTY) {
|
|
|
|
console.error(warnify(appToFleetCmdMsg));
|
|
|
|
}
|
|
|
|
await super.run(parserOutput);
|
|
|
|
}
|
|
|
|
}
|