2019-04-02 11:26:21 +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.
|
|
|
|
*/
|
|
|
|
|
|
|
|
import { Command, flags } from '@oclif/command';
|
2019-12-10 00:07:16 +00:00
|
|
|
import * as BalenaSdk from 'balena-sdk';
|
2019-04-02 11:26:21 +00:00
|
|
|
import { stripIndent } from 'common-tags';
|
2019-08-28 18:51:56 +00:00
|
|
|
import * as _ from 'lodash';
|
2019-04-02 11:26:21 +00:00
|
|
|
|
2019-12-05 15:34:35 +00:00
|
|
|
import { ExpectedError } from '../../errors';
|
2019-08-28 18:51:56 +00:00
|
|
|
import * as cf from '../../utils/common-flags';
|
2020-02-27 14:55:30 +00:00
|
|
|
import { getBalenaSdk } from '../../utils/lazy';
|
2019-04-02 11:26:21 +00:00
|
|
|
import { CommandHelp } from '../../utils/oclif-utils';
|
|
|
|
|
|
|
|
interface FlagsDef {
|
2019-12-10 00:07:16 +00:00
|
|
|
application?: string; // application name
|
|
|
|
device?: string; // device UUID
|
2019-04-02 11:26:21 +00:00
|
|
|
help: void;
|
|
|
|
quiet: boolean;
|
2019-12-10 00:07:16 +00:00
|
|
|
service?: string; // service name
|
2019-04-02 11:26:21 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
interface ArgsDef {
|
|
|
|
name: string;
|
|
|
|
value?: string;
|
|
|
|
}
|
|
|
|
|
|
|
|
export default class EnvAddCmd extends Command {
|
|
|
|
public static description = stripIndent`
|
2019-12-10 00:07:16 +00:00
|
|
|
Add an environment or config variable to an application, device or service.
|
|
|
|
|
|
|
|
Add an environment or config variable to an application, device or service,
|
|
|
|
as selected by the respective command-line options. Either the --application
|
|
|
|
or the --device option must be provided, and either may be be used alongside
|
|
|
|
the --service option to define a service-specific variable. (A service is an
|
|
|
|
application container in a "microservices" application.) When the --service
|
|
|
|
option is used in conjunction with the --device option, the service variable
|
|
|
|
applies to the selected device only. Otherwise, it applies to all devices of
|
|
|
|
the selected application (i.e., the application's fleet). If the --service
|
|
|
|
option is omitted, the variable applies to all services.
|
2019-04-02 11:26:21 +00:00
|
|
|
|
|
|
|
If VALUE is omitted, the CLI will attempt to use the value of the environment
|
|
|
|
variable of same name in the CLI process' environment. In this case, a warning
|
|
|
|
message will be printed. Use \`--quiet\` to suppress it.
|
|
|
|
|
2019-12-10 00:07:16 +00:00
|
|
|
'BALENA_' or 'RESIN_' are reserved variable name prefixes used to identify
|
|
|
|
"configuration variables". Configuration variables control balena platform
|
|
|
|
features and are treated specially by balenaOS and the balena supervisor
|
|
|
|
running on devices. They are also stored differently in the balenaCloud API
|
|
|
|
database. Configuration variables cannot be set for specific services,
|
|
|
|
therefore the --service option cannot be used when the variable name starts
|
|
|
|
with a reserved prefix. When defining custom application variables, please
|
|
|
|
avoid the reserved prefixes.
|
2019-04-02 11:26:21 +00:00
|
|
|
`;
|
|
|
|
public static examples = [
|
|
|
|
'$ balena env add TERM --application MyApp',
|
|
|
|
'$ balena env add EDITOR vim --application MyApp',
|
2019-12-10 00:07:16 +00:00
|
|
|
'$ balena env add EDITOR vim --application MyApp --service MyService',
|
2019-04-02 11:26:21 +00:00
|
|
|
'$ balena env add EDITOR vim --device 7cf02a6',
|
2019-12-10 00:07:16 +00:00
|
|
|
'$ balena env add EDITOR vim --device 7cf02a6 --service MyService',
|
2019-04-02 11:26:21 +00:00
|
|
|
];
|
|
|
|
|
|
|
|
public static args = [
|
|
|
|
{
|
|
|
|
name: 'name',
|
|
|
|
required: true,
|
|
|
|
description: 'environment or config variable name',
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: 'value',
|
|
|
|
required: false,
|
|
|
|
description:
|
2019-12-10 00:07:16 +00:00
|
|
|
"variable value; if omitted, use value from this process' environment",
|
2019-04-02 11:26:21 +00:00
|
|
|
},
|
|
|
|
];
|
|
|
|
|
|
|
|
// hardcoded 'env add' to avoid oclif's 'env:add' topic syntax
|
|
|
|
public static usage =
|
|
|
|
'env add ' + new CommandHelp({ args: EnvAddCmd.args }).defaultUsage();
|
|
|
|
|
2019-08-09 08:06:17 +00:00
|
|
|
public static flags: flags.Input<FlagsDef> = {
|
2019-08-28 18:51:56 +00:00
|
|
|
application: _.assign({ exclusive: ['device'] }, cf.application),
|
|
|
|
device: _.assign({ exclusive: ['application'] }, cf.device),
|
|
|
|
help: cf.help,
|
|
|
|
quiet: cf.quiet,
|
2019-12-10 00:07:16 +00:00
|
|
|
service: cf.service,
|
2019-04-02 11:26:21 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
public async run() {
|
|
|
|
const { args: params, flags: options } = this.parse<FlagsDef, ArgsDef>(
|
|
|
|
EnvAddCmd,
|
|
|
|
);
|
2019-09-06 13:42:54 +00:00
|
|
|
const cmd = this;
|
2019-12-05 15:34:35 +00:00
|
|
|
const { checkLoggedIn } = await import('../../utils/patterns');
|
|
|
|
|
2019-12-10 00:07:16 +00:00
|
|
|
if (!options.application && !options.device) {
|
|
|
|
throw new ExpectedError(
|
|
|
|
'Either the --application or the --device option must always be used',
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2019-12-05 15:34:35 +00:00
|
|
|
await checkLoggedIn();
|
2019-04-02 11:26:21 +00:00
|
|
|
|
2019-09-06 13:42:54 +00:00
|
|
|
if (params.value == null) {
|
|
|
|
params.value = process.env[params.name];
|
2019-04-02 11:26:21 +00:00
|
|
|
|
|
|
|
if (params.value == null) {
|
2019-09-06 13:42:54 +00:00
|
|
|
throw new Error(
|
2019-12-10 00:07:16 +00:00
|
|
|
`Value not found for environment variable: ${params.name}`,
|
2019-09-06 13:42:54 +00:00
|
|
|
);
|
|
|
|
} else if (!options.quiet) {
|
|
|
|
cmd.warn(
|
|
|
|
`Using ${params.name}=${params.value} from CLI process environment`,
|
|
|
|
);
|
2019-04-02 11:26:21 +00:00
|
|
|
}
|
2019-09-06 13:42:54 +00:00
|
|
|
}
|
2019-04-02 11:26:21 +00:00
|
|
|
|
2020-02-27 14:55:30 +00:00
|
|
|
const balena = getBalenaSdk();
|
2019-12-10 00:07:16 +00:00
|
|
|
const reservedPrefixes = await getReservedPrefixes(balena);
|
2019-09-06 13:42:54 +00:00
|
|
|
const isConfigVar = _.some(reservedPrefixes, prefix =>
|
|
|
|
_.startsWith(params.name, prefix),
|
|
|
|
);
|
2019-04-02 11:26:21 +00:00
|
|
|
|
2019-12-10 00:07:16 +00:00
|
|
|
if (options.service) {
|
|
|
|
if (isConfigVar) {
|
|
|
|
throw new ExpectedError(stripIndent`
|
|
|
|
Configuration variables prefixed with "${reservedPrefixes.join(
|
|
|
|
'" or "',
|
|
|
|
)}" cannot be set per service.
|
|
|
|
Hint: remove the --service option or rename the variable.
|
|
|
|
`);
|
|
|
|
}
|
|
|
|
await setServiceVars(balena, params, options);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
const varType = isConfigVar ? 'configVar' : 'envVar';
|
2019-09-06 13:42:54 +00:00
|
|
|
if (options.application) {
|
|
|
|
await balena.models.application[varType].set(
|
|
|
|
options.application,
|
|
|
|
params.name,
|
|
|
|
params.value,
|
|
|
|
);
|
|
|
|
} else if (options.device) {
|
|
|
|
await balena.models.device[varType].set(
|
|
|
|
options.device,
|
|
|
|
params.name,
|
|
|
|
params.value,
|
|
|
|
);
|
|
|
|
}
|
2019-04-02 11:26:21 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-12-10 00:07:16 +00:00
|
|
|
/**
|
|
|
|
* Add service variables for a device or application.
|
|
|
|
*/
|
|
|
|
async function setServiceVars(
|
|
|
|
sdk: BalenaSdk.BalenaSDK,
|
|
|
|
params: ArgsDef,
|
|
|
|
options: FlagsDef,
|
|
|
|
) {
|
|
|
|
if (options.application) {
|
|
|
|
const serviceId = await getServiceIdForApp(
|
|
|
|
sdk,
|
|
|
|
options.application,
|
|
|
|
options.service!,
|
|
|
|
);
|
|
|
|
await sdk.models.service.var.set(serviceId, params.name, params.value!);
|
|
|
|
} else {
|
|
|
|
const { getDeviceAndAppFromUUID } = await import('../../utils/cloud');
|
|
|
|
const [device, app] = await getDeviceAndAppFromUUID(
|
|
|
|
sdk,
|
|
|
|
options.device!,
|
|
|
|
['id'],
|
|
|
|
['app_name'],
|
|
|
|
);
|
|
|
|
const serviceId = await getServiceIdForApp(
|
|
|
|
sdk,
|
|
|
|
app.app_name,
|
|
|
|
options.service!,
|
|
|
|
);
|
|
|
|
await sdk.models.device.serviceVar.set(
|
|
|
|
device.id,
|
|
|
|
serviceId,
|
|
|
|
params.name,
|
|
|
|
params.value!,
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Return a sevice ID for the given app name and service name.
|
|
|
|
*/
|
|
|
|
async function getServiceIdForApp(
|
|
|
|
sdk: BalenaSdk.BalenaSDK,
|
|
|
|
appName: string,
|
|
|
|
serviceName: string,
|
|
|
|
): Promise<number> {
|
|
|
|
let serviceId: number | undefined;
|
|
|
|
const services = await sdk.models.service.getAllByApplication(appName, {
|
|
|
|
$filter: { service_name: serviceName },
|
|
|
|
});
|
|
|
|
if (!_.isEmpty(services)) {
|
|
|
|
serviceId = services[0].id;
|
|
|
|
}
|
|
|
|
if (serviceId === undefined) {
|
|
|
|
throw new ExpectedError(
|
|
|
|
`Cannot find service ${serviceName} for application ${appName}`,
|
|
|
|
);
|
|
|
|
}
|
|
|
|
return serviceId;
|
|
|
|
}
|
2019-04-02 11:26:21 +00:00
|
|
|
|
2019-12-10 00:07:16 +00:00
|
|
|
/**
|
|
|
|
* Return an array of variable name prefixes like: [ 'RESIN_', 'BALENA_' ].
|
|
|
|
* These prefixes can be used to identify "configuration variables".
|
|
|
|
*/
|
|
|
|
async function getReservedPrefixes(
|
|
|
|
balena: BalenaSdk.BalenaSDK,
|
|
|
|
): Promise<string[]> {
|
|
|
|
const settings = await balena.settings.getAll();
|
2019-04-02 11:26:21 +00:00
|
|
|
const response = await balena.request.send({
|
|
|
|
baseUrl: settings.apiUrl,
|
|
|
|
url: '/config/vars',
|
|
|
|
});
|
|
|
|
|
|
|
|
return response.body.reservedNamespaces;
|
|
|
|
}
|