2019-09-06 13:43:53 +00:00
|
|
|
/**
|
|
|
|
* @license
|
2021-07-15 13:41:38 +00:00
|
|
|
* Copyright 2016-2021 Balena Ltd.
|
2019-09-06 13:43:53 +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.
|
|
|
|
*/
|
2020-03-19 08:45:57 +00:00
|
|
|
import { flags } from '@oclif/command';
|
2020-02-27 12:38:50 +00:00
|
|
|
import type * as SDK from 'balena-sdk';
|
2019-09-06 13:43:53 +00:00
|
|
|
import * as _ from 'lodash';
|
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-09-06 13:43:53 +00:00
|
|
|
import * as cf from '../utils/common-flags';
|
2020-05-04 14:57:23 +00:00
|
|
|
import { getBalenaSdk, getVisuals, stripIndent } from '../utils/lazy';
|
2021-12-19 20:50:03 +00:00
|
|
|
import { applicationIdInfo } from '../utils/messages';
|
2019-09-06 13:43:53 +00:00
|
|
|
|
|
|
|
interface FlagsDef {
|
2021-07-15 13:41:38 +00:00
|
|
|
fleet?: string;
|
2019-09-06 13:43:53 +00:00
|
|
|
config: boolean;
|
2019-12-04 18:04:57 +00:00
|
|
|
device?: string; // device UUID
|
2019-12-05 17:09:02 +00:00
|
|
|
json: boolean;
|
2019-09-06 13:43:53 +00:00
|
|
|
help: void;
|
2019-12-04 18:04:57 +00:00
|
|
|
service?: string; // service name
|
2019-09-06 13:43:53 +00:00
|
|
|
}
|
|
|
|
|
2019-12-04 18:04:57 +00:00
|
|
|
interface EnvironmentVariableInfo extends SDK.EnvironmentVariableBase {
|
|
|
|
appName?: string | null; // application name
|
|
|
|
deviceUUID?: string; // device UUID
|
|
|
|
serviceName?: string; // service name
|
|
|
|
}
|
|
|
|
|
|
|
|
interface DeviceServiceEnvironmentVariableInfo
|
|
|
|
extends SDK.DeviceServiceEnvironmentVariable {
|
|
|
|
appName?: string; // application name
|
|
|
|
deviceUUID?: string; // device UUID
|
|
|
|
serviceName?: string; // service name
|
|
|
|
}
|
|
|
|
|
|
|
|
interface ServiceEnvironmentVariableInfo
|
|
|
|
extends SDK.ServiceEnvironmentVariable {
|
|
|
|
appName?: string; // application name
|
|
|
|
deviceUUID?: string; // device UUID
|
|
|
|
serviceName?: string; // service name
|
|
|
|
}
|
|
|
|
|
2019-09-06 13:43:53 +00:00
|
|
|
export default class EnvsCmd extends Command {
|
2020-12-07 13:58:23 +00:00
|
|
|
public static description = stripIndent`
|
2021-07-15 13:41:38 +00:00
|
|
|
List the environment or config variables of a fleet, device or service.
|
2020-05-29 07:27:11 +00:00
|
|
|
|
2021-07-15 13:41:38 +00:00
|
|
|
List the environment or configuration variables of a fleet, device or
|
|
|
|
service, as selected by the respective command-line options. (A service
|
|
|
|
corresponds to a Docker image/container in a microservices fleet.)
|
2020-05-29 07:27:11 +00:00
|
|
|
|
2021-07-15 13:41:38 +00:00
|
|
|
The results include fleet-wide (multiple devices), device-specific (multiple
|
|
|
|
services on a specific device) and service-specific variables that apply to the
|
|
|
|
selected fleet, device or service. It can be thought of as including inherited
|
|
|
|
variables; for example, a service inherits device-wide variables, and a device
|
|
|
|
inherits fleet-wide variables.
|
2020-05-29 07:27:11 +00:00
|
|
|
|
|
|
|
The printed output may include DEVICE and/or SERVICE columns to distinguish
|
2021-07-15 13:41:38 +00:00
|
|
|
between fleet-wide, device-specific and service-specific variables.
|
2020-05-29 07:27:11 +00:00
|
|
|
An asterisk in these columns indicates that the variable applies to
|
|
|
|
"all devices" or "all services".
|
|
|
|
|
|
|
|
The --config option is used to list "configuration variables" that control
|
|
|
|
balena platform features, as opposed to custom environment variables defined
|
|
|
|
by the user. The --config and the --service options are mutually exclusive
|
|
|
|
because configuration variables cannot be set for specific services.
|
|
|
|
|
2020-01-03 23:13:20 +00:00
|
|
|
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
|
|
|
|
types like lists and empty strings. The 'jq' utility may be helpful in shell
|
|
|
|
scripts (https://stedolan.github.io/jq/manual/). When --json is used, an empty
|
|
|
|
JSON array ([]) is printed instead of an error message when no variables exist
|
2021-07-15 13:41:38 +00:00
|
|
|
for the given query. When querying variables for a device, note that the fleet
|
|
|
|
name may be null in JSON output (or 'N/A' in tabular output) if the fleet that
|
|
|
|
the device belonged to is no longer accessible by the current user (for example,
|
|
|
|
in case the current user was removed from the fleet by the fleet's owner).
|
2020-12-10 12:30:17 +00:00
|
|
|
|
|
|
|
${applicationIdInfo.split('\n').join('\n\t\t')}
|
|
|
|
`;
|
|
|
|
|
2020-12-07 13:58:23 +00:00
|
|
|
public static examples = [
|
2021-07-15 13:41:38 +00:00
|
|
|
'$ balena envs --fleet myorg/myfleet',
|
|
|
|
'$ balena envs --fleet MyFleet --json',
|
|
|
|
'$ balena envs --fleet MyFleet --service MyService',
|
|
|
|
'$ balena envs --fleet MyFleet --service MyService',
|
|
|
|
'$ balena envs --fleet MyFleet --config',
|
2020-12-07 13:58:23 +00:00
|
|
|
'$ balena envs --device 7cf02a6',
|
|
|
|
'$ balena envs --device 7cf02a6 --json',
|
|
|
|
'$ balena envs --device 7cf02a6 --config --json',
|
|
|
|
'$ balena envs --device 7cf02a6 --service MyService',
|
|
|
|
];
|
|
|
|
|
|
|
|
public static usage = 'envs';
|
2019-09-06 13:43:53 +00:00
|
|
|
|
|
|
|
public static flags: flags.Input<FlagsDef> = {
|
2021-12-19 20:50:03 +00:00
|
|
|
fleet: { ...cf.fleet, exclusive: ['device'] },
|
2019-09-06 13:43:53 +00:00
|
|
|
config: flags.boolean({
|
2020-12-07 13:35:57 +00:00
|
|
|
default: false,
|
2019-09-06 13:43:53 +00:00
|
|
|
char: 'c',
|
2019-12-04 18:04:57 +00:00
|
|
|
description: 'show configuration variables only',
|
|
|
|
exclusive: ['service'],
|
2019-09-06 13:43:53 +00:00
|
|
|
}),
|
2021-12-19 20:50:03 +00:00
|
|
|
device: { ...cf.device, exclusive: ['fleet'] },
|
2019-09-06 13:43:53 +00:00
|
|
|
help: cf.help,
|
2020-12-10 12:30:17 +00:00
|
|
|
json: cf.json,
|
2021-12-19 20:50:03 +00:00
|
|
|
service: { ...cf.service, exclusive: ['config'] },
|
2019-09-06 13:43:53 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
public async run() {
|
|
|
|
const { flags: options } = this.parse<FlagsDef, {}>(EnvsCmd);
|
2021-07-15 13:41:38 +00:00
|
|
|
|
2019-12-04 18:04:57 +00:00
|
|
|
const variables: EnvironmentVariableInfo[] = [];
|
2019-12-05 15:34:35 +00:00
|
|
|
|
2020-03-24 08:51:19 +00:00
|
|
|
await Command.checkLoggedIn();
|
2019-12-05 15:34:35 +00:00
|
|
|
|
2021-12-19 20:50:03 +00:00
|
|
|
if (!options.fleet && !options.device) {
|
2021-07-15 13:41:38 +00:00
|
|
|
throw new ExpectedError('Missing --fleet or --device option');
|
2019-09-06 13:43:53 +00:00
|
|
|
}
|
|
|
|
|
2020-02-27 14:55:30 +00:00
|
|
|
const balena = getBalenaSdk();
|
|
|
|
|
2021-12-19 20:50:03 +00:00
|
|
|
let fleetSlug: string | undefined = options.fleet
|
|
|
|
? await (await import('../utils/sdk')).getFleetSlug(balena, options.fleet)
|
|
|
|
: undefined;
|
2019-12-04 18:04:57 +00:00
|
|
|
let fullUUID: string | undefined; // as oppposed to the short, 7-char UUID
|
|
|
|
|
|
|
|
if (options.device) {
|
2020-07-08 17:21:37 +00:00
|
|
|
const { getDeviceAndMaybeAppFromUUID } = await import('../utils/cloud');
|
2019-12-04 18:04:57 +00:00
|
|
|
const [device, app] = await getDeviceAndMaybeAppFromUUID(
|
|
|
|
balena,
|
|
|
|
options.device,
|
|
|
|
['uuid'],
|
2021-08-11 00:19:49 +00:00
|
|
|
['slug'],
|
2019-12-04 18:04:57 +00:00
|
|
|
);
|
|
|
|
fullUUID = device.uuid;
|
|
|
|
if (app) {
|
2021-12-19 20:50:03 +00:00
|
|
|
fleetSlug = app.slug;
|
2019-12-04 18:04:57 +00:00
|
|
|
}
|
|
|
|
}
|
2021-12-19 20:50:03 +00:00
|
|
|
if (fleetSlug && options.service) {
|
|
|
|
await validateServiceName(balena, options.service, fleetSlug);
|
2019-12-04 18:04:57 +00:00
|
|
|
}
|
2021-12-19 20:50:03 +00:00
|
|
|
variables.push(...(await getAppVars(balena, fleetSlug, options)));
|
2019-12-04 18:04:57 +00:00
|
|
|
if (fullUUID) {
|
|
|
|
variables.push(
|
2021-12-19 20:50:03 +00:00
|
|
|
...(await getDeviceVars(balena, fullUUID, fleetSlug, options)),
|
2019-12-04 18:04:57 +00:00
|
|
|
);
|
|
|
|
}
|
2020-07-08 16:34:53 +00:00
|
|
|
if (!options.json && variables.length === 0) {
|
2019-12-04 18:04:57 +00:00
|
|
|
const target =
|
|
|
|
(options.service ? `service "${options.service}" of ` : '') +
|
2021-12-19 20:50:03 +00:00
|
|
|
(options.fleet
|
|
|
|
? `fleet "${options.fleet}"`
|
2019-12-04 18:04:57 +00:00
|
|
|
: `device "${options.device}"`);
|
|
|
|
throw new ExpectedError(`No environment variables found for ${target}`);
|
2019-09-06 13:43:53 +00:00
|
|
|
}
|
|
|
|
|
2019-12-04 18:04:57 +00:00
|
|
|
await this.printVariables(variables, options);
|
|
|
|
}
|
|
|
|
|
|
|
|
protected async printVariables(
|
|
|
|
varArray: EnvironmentVariableInfo[],
|
|
|
|
options: FlagsDef,
|
|
|
|
) {
|
2019-12-05 17:09:02 +00:00
|
|
|
const fields = ['id', 'name', 'value'];
|
|
|
|
|
2020-12-07 13:58:23 +00:00
|
|
|
// Replace undefined app names with 'N/A' or null
|
|
|
|
varArray = varArray.map((i: EnvironmentVariableInfo) => {
|
2021-12-19 20:50:03 +00:00
|
|
|
i.appName ||= options.json ? null : 'N/A';
|
2020-12-07 13:58:23 +00:00
|
|
|
return i;
|
|
|
|
});
|
2019-12-04 18:04:57 +00:00
|
|
|
|
2021-12-19 20:50:03 +00:00
|
|
|
fields.push(options.json ? `appName => fleetName` : `appName => FLEET`);
|
2020-12-07 13:58:23 +00:00
|
|
|
if (options.device) {
|
|
|
|
fields.push(options.json ? 'deviceUUID' : 'deviceUUID => DEVICE');
|
|
|
|
}
|
|
|
|
if (!options.config) {
|
|
|
|
fields.push(options.json ? 'serviceName' : 'serviceName => SERVICE');
|
2019-12-04 18:04:57 +00:00
|
|
|
}
|
|
|
|
|
2019-12-05 17:09:02 +00:00
|
|
|
if (options.json) {
|
2021-07-15 13:41:38 +00:00
|
|
|
const { pickAndRename } = await import('../utils/helpers');
|
|
|
|
const mapped = varArray.map((o) => pickAndRename(o, fields));
|
|
|
|
this.log(JSON.stringify(mapped, null, 4));
|
2019-12-04 18:04:57 +00:00
|
|
|
} else {
|
|
|
|
this.log(
|
2020-02-28 13:06:14 +00:00
|
|
|
getVisuals().table.horizontal(
|
2019-12-04 18:04:57 +00:00
|
|
|
_.sortBy(varArray, (v: SDK.EnvironmentVariableBase) => v.name),
|
2019-12-05 17:09:02 +00:00
|
|
|
fields,
|
|
|
|
),
|
|
|
|
);
|
|
|
|
}
|
2019-09-06 13:43:53 +00:00
|
|
|
}
|
|
|
|
}
|
2019-12-05 17:09:02 +00:00
|
|
|
|
2019-12-04 18:04:57 +00:00
|
|
|
async function validateServiceName(
|
|
|
|
sdk: SDK.BalenaSDK,
|
|
|
|
serviceName: string,
|
2021-12-19 20:50:03 +00:00
|
|
|
fleetSlug: string,
|
2019-12-04 18:04:57 +00:00
|
|
|
) {
|
2021-12-19 20:50:03 +00:00
|
|
|
const services = await sdk.models.service.getAllByApplication(fleetSlug, {
|
2019-12-04 18:04:57 +00:00
|
|
|
$filter: { service_name: serviceName },
|
|
|
|
});
|
2020-07-08 16:34:53 +00:00
|
|
|
if (services.length === 0) {
|
2019-12-04 18:04:57 +00:00
|
|
|
throw new ExpectedError(
|
2021-12-19 20:50:03 +00:00
|
|
|
`Service "${serviceName}" not found for fleet "${fleetSlug}"`,
|
2019-12-04 18:04:57 +00:00
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Fetch application-wide config / env / service vars.
|
|
|
|
* If options.application is undefined, an attempt is made to obtain the
|
|
|
|
* application name from the device UUID (options.device). If this attempt
|
|
|
|
* fails because the device does not belong to any application, an emtpy
|
|
|
|
* array is returned.
|
|
|
|
*/
|
|
|
|
async function getAppVars(
|
|
|
|
sdk: SDK.BalenaSDK,
|
2021-12-19 20:50:03 +00:00
|
|
|
fleetSlug: string | undefined,
|
2019-12-04 18:04:57 +00:00
|
|
|
options: FlagsDef,
|
|
|
|
): Promise<EnvironmentVariableInfo[]> {
|
|
|
|
const appVars: EnvironmentVariableInfo[] = [];
|
2021-12-19 20:50:03 +00:00
|
|
|
if (!fleetSlug) {
|
2019-12-04 18:04:57 +00:00
|
|
|
return appVars;
|
|
|
|
}
|
2020-12-07 13:58:23 +00:00
|
|
|
const vars = await sdk.models.application[
|
|
|
|
options.config ? 'configVar' : 'envVar'
|
2021-12-19 20:50:03 +00:00
|
|
|
].getAllByApplication(fleetSlug);
|
|
|
|
fillInInfoFields(vars, fleetSlug);
|
2020-12-07 13:58:23 +00:00
|
|
|
appVars.push(...vars);
|
|
|
|
if (!options.config) {
|
2020-07-31 14:35:20 +00:00
|
|
|
const pineOpts: SDK.PineOptions<SDK.ServiceEnvironmentVariable> = {
|
2019-12-04 18:04:57 +00:00
|
|
|
$expand: {
|
|
|
|
service: {},
|
|
|
|
},
|
|
|
|
};
|
|
|
|
if (options.service) {
|
|
|
|
pineOpts.$filter = {
|
|
|
|
service: {
|
|
|
|
service_name: options.service,
|
|
|
|
},
|
|
|
|
};
|
|
|
|
}
|
|
|
|
const serviceVars = await sdk.models.service.var.getAllByApplication(
|
2021-12-19 20:50:03 +00:00
|
|
|
fleetSlug,
|
2019-12-04 18:04:57 +00:00
|
|
|
pineOpts,
|
|
|
|
);
|
2021-12-19 20:50:03 +00:00
|
|
|
fillInInfoFields(serviceVars, fleetSlug);
|
2019-12-04 18:04:57 +00:00
|
|
|
appVars.push(...serviceVars);
|
|
|
|
}
|
|
|
|
return appVars;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Fetch config / env / service vars when the '--device' option is provided.
|
|
|
|
* Precondition: options.device must be defined.
|
|
|
|
*/
|
|
|
|
async function getDeviceVars(
|
|
|
|
sdk: SDK.BalenaSDK,
|
|
|
|
fullUUID: string,
|
2021-12-19 20:50:03 +00:00
|
|
|
fleetSlug: string | undefined,
|
2019-12-04 18:04:57 +00:00
|
|
|
options: FlagsDef,
|
|
|
|
): Promise<EnvironmentVariableInfo[]> {
|
|
|
|
const printedUUID = options.json ? fullUUID : options.device!;
|
|
|
|
const deviceVars: EnvironmentVariableInfo[] = [];
|
|
|
|
if (options.config) {
|
|
|
|
const deviceConfigVars = await sdk.models.device.configVar.getAllByDevice(
|
|
|
|
fullUUID,
|
|
|
|
);
|
2021-12-19 20:50:03 +00:00
|
|
|
fillInInfoFields(deviceConfigVars, fleetSlug, printedUUID);
|
2019-12-04 18:04:57 +00:00
|
|
|
deviceVars.push(...deviceConfigVars);
|
|
|
|
} else {
|
2020-12-07 13:58:23 +00:00
|
|
|
const pineOpts: SDK.PineOptions<SDK.DeviceServiceEnvironmentVariable> = {
|
|
|
|
$expand: {
|
|
|
|
service_install: {
|
|
|
|
$expand: 'installs__service',
|
|
|
|
},
|
|
|
|
},
|
|
|
|
};
|
|
|
|
if (options.service) {
|
|
|
|
pineOpts.$filter = {
|
|
|
|
service_install: {
|
|
|
|
installs__service: { service_name: options.service },
|
2019-12-04 18:04:57 +00:00
|
|
|
},
|
|
|
|
};
|
|
|
|
}
|
2020-12-07 13:58:23 +00:00
|
|
|
const deviceServiceVars = await sdk.models.device.serviceVar.getAllByDevice(
|
|
|
|
fullUUID,
|
|
|
|
pineOpts,
|
|
|
|
);
|
2021-12-19 20:50:03 +00:00
|
|
|
fillInInfoFields(deviceServiceVars, fleetSlug, printedUUID);
|
2020-12-07 13:58:23 +00:00
|
|
|
deviceVars.push(...deviceServiceVars);
|
|
|
|
|
|
|
|
const deviceEnvVars = await sdk.models.device.envVar.getAllByDevice(
|
|
|
|
fullUUID,
|
|
|
|
);
|
2021-12-19 20:50:03 +00:00
|
|
|
fillInInfoFields(deviceEnvVars, fleetSlug, printedUUID);
|
2020-12-07 13:58:23 +00:00
|
|
|
deviceVars.push(...deviceEnvVars);
|
2019-12-04 18:04:57 +00:00
|
|
|
}
|
|
|
|
return deviceVars;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* For each env var object in varArray, fill in its top-level serviceName
|
|
|
|
* and deviceUUID fields. An asterisk is used to indicate that the variable
|
|
|
|
* applies to "all services" or "all devices".
|
|
|
|
*/
|
|
|
|
function fillInInfoFields(
|
|
|
|
varArray:
|
|
|
|
| EnvironmentVariableInfo[]
|
|
|
|
| DeviceServiceEnvironmentVariableInfo[]
|
|
|
|
| ServiceEnvironmentVariableInfo[],
|
2021-12-19 20:50:03 +00:00
|
|
|
fleetSlug?: string,
|
2019-12-04 18:04:57 +00:00
|
|
|
deviceUUID?: string,
|
|
|
|
) {
|
|
|
|
for (const envVar of varArray) {
|
|
|
|
if ('service' in envVar) {
|
|
|
|
// envVar is of type ServiceEnvironmentVariableInfo
|
2020-07-08 16:34:53 +00:00
|
|
|
envVar.serviceName = (envVar.service as SDK.Service[])[0]?.service_name;
|
2019-12-04 18:04:57 +00:00
|
|
|
} else if ('service_install' in envVar) {
|
|
|
|
// envVar is of type DeviceServiceEnvironmentVariableInfo
|
2021-07-20 13:57:00 +00:00
|
|
|
envVar.serviceName = (
|
|
|
|
(envVar.service_install as SDK.ServiceInstall[])[0]
|
|
|
|
?.installs__service as SDK.Service[]
|
|
|
|
)[0]?.service_name;
|
2019-12-04 18:04:57 +00:00
|
|
|
}
|
2021-12-19 20:50:03 +00:00
|
|
|
envVar.appName = fleetSlug;
|
2019-12-04 18:04:57 +00:00
|
|
|
envVar.serviceName = envVar.serviceName || '*';
|
|
|
|
envVar.deviceUUID = deviceUUID || '*';
|
|
|
|
}
|
|
|
|
}
|