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.
|
|
|
|
*/
|
|
|
|
|
2023-09-06 14:39:31 +00:00
|
|
|
import { Args } from '@oclif/core';
|
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-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';
|
2021-12-19 20:50:03 +00:00
|
|
|
import { applicationIdInfo } from '../../utils/messages';
|
2019-04-02 11:26:21 +00:00
|
|
|
|
|
|
|
interface FlagsDef {
|
2021-07-15 13:41:38 +00:00
|
|
|
fleet?: string;
|
2019-12-10 00:07:16 +00:00
|
|
|
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`
|
2021-07-15 13:41:38 +00:00
|
|
|
Add env or config variable to fleets, devices or services.
|
2020-08-21 10:43:12 +00:00
|
|
|
|
2021-07-15 13:41:38 +00:00
|
|
|
Add an environment or config variable to one or more fleets, devices or
|
|
|
|
services, as selected by the respective command-line options. Either the
|
|
|
|
--fleet or the --device option must be provided, and either may be be
|
2020-08-21 10:43:12 +00:00
|
|
|
used alongside the --service option to define a service-specific variable.
|
2021-07-15 13:41:38 +00:00
|
|
|
(A service corresponds to a Docker image/container in a microservices fleet.)
|
2020-08-21 10:43:12 +00:00
|
|
|
When the --service option is used in conjunction with the --device option,
|
2021-07-15 13:41:38 +00:00
|
|
|
the service variable applies to the selected device only. Otherwise, it
|
|
|
|
applies to all devices of the selected 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
|
2021-07-15 13:41:38 +00:00
|
|
|
with a reserved prefix. When defining custom fleet variables, please avoid
|
|
|
|
these reserved prefixes.
|
2020-12-10 12:30:17 +00:00
|
|
|
|
|
|
|
${applicationIdInfo.split('\n').join('\n\t\t')}
|
|
|
|
`;
|
|
|
|
|
2019-04-02 11:26:21 +00:00
|
|
|
public static examples = [
|
2021-07-15 13:41:38 +00:00
|
|
|
'$ balena env add TERM --fleet MyFleet',
|
|
|
|
'$ balena env add EDITOR vim -f myorg/myfleet',
|
|
|
|
'$ balena env add EDITOR vim --fleet MyFleet,MyFleet2',
|
|
|
|
'$ balena env add EDITOR vim --fleet MyFleet --service MyService',
|
|
|
|
'$ balena env add EDITOR vim --fleet MyFleet,MyFleet2 --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
|
|
|
];
|
|
|
|
|
2023-09-06 14:39:31 +00:00
|
|
|
public static args = {
|
|
|
|
name: Args.string({
|
2019-04-02 11:26:21 +00:00
|
|
|
required: true,
|
|
|
|
description: 'environment or config variable name',
|
2023-09-06 14:39:31 +00:00
|
|
|
}),
|
|
|
|
value: Args.string({
|
2019-04-02 11:26:21 +00:00
|
|
|
required: false,
|
|
|
|
description:
|
2019-12-10 00:07:16 +00:00
|
|
|
"variable value; if omitted, use value from this process' environment",
|
2023-09-06 14:39:31 +00:00
|
|
|
}),
|
|
|
|
};
|
2019-04-02 11:26:21 +00:00
|
|
|
|
2023-10-20 08:32:49 +00:00
|
|
|
// Required for supporting empty string ('') `value` args.
|
|
|
|
public static strict = false;
|
2020-09-04 14:34:34 +00:00
|
|
|
public static usage = 'env add <name> [value]';
|
2019-04-02 11:26:21 +00:00
|
|
|
|
2023-09-06 14:39:31 +00:00
|
|
|
public static flags = {
|
2021-12-19 20:50:03 +00:00
|
|
|
fleet: { ...cf.fleet, exclusive: ['device'] },
|
|
|
|
device: { ...cf.device, exclusive: ['fleet'] },
|
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() {
|
2023-09-06 14:39:31 +00:00
|
|
|
const { args: params, flags: options } = await this.parse(EnvAddCmd);
|
2019-09-06 13:42:54 +00:00
|
|
|
const cmd = this;
|
2019-12-05 15:34:35 +00:00
|
|
|
|
2021-12-19 20:50:03 +00:00
|
|
|
if (!options.fleet && !options.device) {
|
2019-12-10 00:07:16 +00:00
|
|
|
throw new ExpectedError(
|
2021-07-15 13:41:38 +00:00
|
|
|
'Either the --fleet or the --device option must be specified',
|
2019-12-10 00:07:16 +00:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
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';
|
2021-12-19 20:50:03 +00:00
|
|
|
if (options.fleet) {
|
2023-05-23 12:59:41 +00:00
|
|
|
for (const appSlug of await resolveFleetSlugs(balena, options.fleet)) {
|
2020-08-03 23:09:40 +00:00
|
|
|
try {
|
|
|
|
await balena.models.application[varType].set(
|
2023-05-23 12:59:41 +00:00
|
|
|
appSlug,
|
2020-08-03 23:09:40 +00:00
|
|
|
params.name,
|
|
|
|
params.value,
|
|
|
|
);
|
|
|
|
} catch (err) {
|
2023-05-23 12:59:41 +00:00
|
|
|
console.error(`${err.message}, fleet: ${appSlug}`);
|
2020-08-03 23:09:40 +00:00
|
|
|
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
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-05-23 12:59:41 +00:00
|
|
|
// TODO: Stop accepting application names in the next major
|
|
|
|
// and just drop this in favor of doing the .split(',') directly.
|
|
|
|
async function resolveFleetSlugs(
|
|
|
|
balena: BalenaSdk.BalenaSDK,
|
|
|
|
fleetOption: string,
|
|
|
|
) {
|
|
|
|
const fleetSlugs: string[] = [];
|
|
|
|
const { getFleetSlug } = await import('../../utils/sdk');
|
|
|
|
for (const appNameOrSlug of fleetOption.split(',')) {
|
|
|
|
try {
|
|
|
|
fleetSlugs.push(await getFleetSlug(balena, appNameOrSlug));
|
|
|
|
} catch (err) {
|
|
|
|
console.error(`${err.message}, fleet: ${appNameOrSlug}`);
|
|
|
|
process.exitCode = 1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return fleetSlugs;
|
|
|
|
}
|
|
|
|
|
2019-12-10 00:07:16 +00:00
|
|
|
/**
|
2021-07-15 13:41:38 +00:00
|
|
|
* Add service variables for a device or fleet.
|
2019-12-10 00:07:16 +00:00
|
|
|
*/
|
|
|
|
async function setServiceVars(
|
|
|
|
sdk: BalenaSdk.BalenaSDK,
|
|
|
|
params: ArgsDef,
|
|
|
|
options: FlagsDef,
|
|
|
|
) {
|
2021-12-19 20:50:03 +00:00
|
|
|
if (options.fleet) {
|
2023-05-23 12:59:41 +00:00
|
|
|
for (const appSlug of await resolveFleetSlugs(sdk, options.fleet)) {
|
2020-08-03 23:09:40 +00:00
|
|
|
for (const service of options.service!.split(',')) {
|
|
|
|
try {
|
2023-05-23 12:59:41 +00:00
|
|
|
const serviceId = await getServiceIdForApp(sdk, appSlug, service);
|
2020-08-03 23:09:40 +00:00
|
|
|
await sdk.models.service.var.set(
|
|
|
|
serviceId,
|
|
|
|
params.name,
|
|
|
|
params.value!,
|
|
|
|
);
|
|
|
|
} catch (err) {
|
2023-05-23 12:59:41 +00:00
|
|
|
console.error(`${err.message}, fleet: ${appSlug}`);
|
2020-08-03 23:09:40 +00:00
|
|
|
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'],
|
2021-08-11 00:19:49 +00:00
|
|
|
['slug'],
|
2020-08-03 23:09:40 +00:00
|
|
|
);
|
|
|
|
} catch (err) {
|
|
|
|
console.error(`${err.message}, device: ${uuid}`);
|
|
|
|
process.exitCode = 1;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
for (const service of options.service!.split(',')) {
|
|
|
|
try {
|
2021-08-11 00:19:49 +00:00
|
|
|
const serviceId = await getServiceIdForApp(sdk, app.slug, service);
|
2020-08-03 23:09:40 +00:00
|
|
|
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,
|
2023-05-23 12:59:41 +00:00
|
|
|
appSlug: string,
|
2019-12-10 00:07:16 +00:00
|
|
|
serviceName: string,
|
|
|
|
): Promise<number> {
|
|
|
|
let serviceId: number | undefined;
|
2023-05-23 12:59:41 +00:00
|
|
|
const services = await sdk.models.service.getAllByApplication(appSlug, {
|
2023-08-16 13:37:58 +00:00
|
|
|
$select: 'id',
|
2019-12-10 00:07:16 +00:00
|
|
|
$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(
|
2023-05-23 12:59:41 +00:00
|
|
|
`Cannot find service ${serviceName} for fleet ${appSlug}`,
|
2019-12-10 00:07:16 +00:00
|
|
|
);
|
|
|
|
}
|
|
|
|
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;
|
|
|
|
}
|