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.
|
|
|
|
*/
|
|
|
|
|
2020-03-19 08:45:57 +00:00
|
|
|
import { flags } from '@oclif/command';
|
2020-02-27 12:38:50 +00:00
|
|
|
import type * as BalenaSdk from 'balena-sdk';
|
2020-03-19 08:45:57 +00:00
|
|
|
import Command from '../../command';
|
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-05-04 14:57:23 +00:00
|
|
|
import { getBalenaSdk, stripIndent } from '../../utils/lazy';
|
2019-04-02 11:26:21 +00:00
|
|
|
|
|
|
|
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`
|
2020-08-21 10:43:12 +00:00
|
|
|
Add env or config variable to application(s), device(s) or service(s).
|
|
|
|
|
|
|
|
Add an environment or config variable to one or more applications, devices
|
|
|
|
or services, 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
|
2020-08-03 23:09:40 +00:00
|
|
|
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',
|
2020-08-03 23:09:40 +00:00
|
|
|
'$ balena env add EDITOR vim --application MyApp,MyApp2',
|
2019-12-10 00:07:16 +00:00
|
|
|
'$ balena env add EDITOR vim --application MyApp --service MyService',
|
2020-08-03 23:09:40 +00:00
|
|
|
'$ balena env add EDITOR vim --application MyApp,MyApp2 --service MyService,MyService2',
|
2019-04-02 11:26:21 +00:00
|
|
|
'$ balena env add EDITOR vim --device 7cf02a6',
|
2020-08-03 23:09:40 +00:00
|
|
|
'$ balena env add EDITOR vim --device 7cf02a6,d6f1433',
|
2019-12-10 00:07:16 +00:00
|
|
|
'$ balena env add EDITOR vim --device 7cf02a6 --service MyService',
|
2020-08-03 23:09:40 +00:00
|
|
|
'$ balena env add EDITOR vim --device 7cf02a6,d6f1433 --service MyService,MyService2',
|
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
|
|
|
},
|
|
|
|
];
|
|
|
|
|
2020-09-04 14:34:34 +00:00
|
|
|
public static usage = 'env add <name> [value]';
|
2019-04-02 11:26:21 +00:00
|
|
|
|
2019-08-09 08:06:17 +00:00
|
|
|
public static flags: flags.Input<FlagsDef> = {
|
2020-02-29 22:06:54 +00:00
|
|
|
application: { exclusive: ['device'], ...cf.application },
|
|
|
|
device: { exclusive: ['application'], ...cf.device },
|
2019-08-28 18:51:56 +00:00
|
|
|
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
|
|
|
|
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',
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2020-03-24 08:51:19 +00:00
|
|
|
await Command.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) {
|
2020-06-26 11:46:58 +00:00
|
|
|
throw new ExpectedError(
|
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);
|
2020-07-08 16:34:53 +00:00
|
|
|
const isConfigVar = reservedPrefixes.some((prefix) =>
|
|
|
|
params.name.startsWith(prefix),
|
2019-09-06 13:42:54 +00:00
|
|
|
);
|
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) {
|
2020-08-03 23:09:40 +00:00
|
|
|
for (const app of options.application.split(',')) {
|
|
|
|
try {
|
|
|
|
await balena.models.application[varType].set(
|
|
|
|
app,
|
|
|
|
params.name,
|
|
|
|
params.value,
|
|
|
|
);
|
|
|
|
} catch (err) {
|
|
|
|
console.error(`${err.message}, app: ${app}`);
|
|
|
|
process.exitCode = 1;
|
|
|
|
}
|
|
|
|
}
|
2019-09-06 13:42:54 +00:00
|
|
|
} else if (options.device) {
|
2020-08-03 23:09:40 +00:00
|
|
|
for (const device of options.device.split(',')) {
|
|
|
|
try {
|
|
|
|
await balena.models.device[varType].set(
|
|
|
|
device,
|
|
|
|
params.name,
|
|
|
|
params.value,
|
|
|
|
);
|
|
|
|
} catch (err) {
|
|
|
|
console.error(`${err.message}, device: ${device}`);
|
|
|
|
process.exitCode = 1;
|
|
|
|
}
|
|
|
|
}
|
2019-09-06 13:42:54 +00:00
|
|
|
}
|
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) {
|
2020-08-03 23:09:40 +00:00
|
|
|
for (const app of options.application.split(',')) {
|
|
|
|
for (const service of options.service!.split(',')) {
|
|
|
|
try {
|
|
|
|
const serviceId = await getServiceIdForApp(sdk, app, service);
|
|
|
|
await sdk.models.service.var.set(
|
|
|
|
serviceId,
|
|
|
|
params.name,
|
|
|
|
params.value!,
|
|
|
|
);
|
|
|
|
} catch (err) {
|
|
|
|
console.error(`${err.message}, application: ${app}`);
|
|
|
|
process.exitCode = 1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else if (options.device) {
|
2019-12-10 00:07:16 +00:00
|
|
|
const { getDeviceAndAppFromUUID } = await import('../../utils/cloud');
|
2020-08-03 23:09:40 +00:00
|
|
|
for (const uuid of options.device.split(',')) {
|
|
|
|
let device;
|
|
|
|
let app;
|
|
|
|
try {
|
|
|
|
[device, app] = await getDeviceAndAppFromUUID(
|
|
|
|
sdk,
|
|
|
|
uuid,
|
|
|
|
['id'],
|
|
|
|
['app_name'],
|
|
|
|
);
|
|
|
|
} catch (err) {
|
|
|
|
console.error(`${err.message}, device: ${uuid}`);
|
|
|
|
process.exitCode = 1;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
for (const service of options.service!.split(',')) {
|
|
|
|
try {
|
|
|
|
const serviceId = await getServiceIdForApp(
|
|
|
|
sdk,
|
|
|
|
app.app_name,
|
|
|
|
service,
|
|
|
|
);
|
|
|
|
await sdk.models.device.serviceVar.set(
|
|
|
|
device.id,
|
|
|
|
serviceId,
|
|
|
|
params.name,
|
|
|
|
params.value!,
|
|
|
|
);
|
|
|
|
} catch (err) {
|
|
|
|
console.error(`${err.message}, service: ${service}`);
|
|
|
|
process.exitCode = 1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2019-12-10 00:07:16 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* 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 },
|
|
|
|
});
|
2020-07-08 16:34:53 +00:00
|
|
|
if (services.length > 0) {
|
2019-12-10 00:07:16 +00:00
|
|
|
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;
|
|
|
|
}
|