Merge pull request #1962 from nwneisen/1956-bulk-add-env-vars

Add ability to add env var to multiple locations in one command
This commit is contained in:
bulldozer-balena[bot] 2020-08-05 00:25:32 +00:00 committed by GitHub
commit 9f98529e56
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 104 additions and 54 deletions

View File

@ -961,15 +961,16 @@ do not prompt for confirmation before deleting the variable
## env add NAME [VALUE]
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.
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
services.
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
@ -988,9 +989,13 @@ Examples:
$ balena env add TERM --application MyApp
$ balena env add EDITOR vim --application MyApp
$ balena env add EDITOR vim --application MyApp,MyApp2
$ balena env add EDITOR vim --application MyApp --service MyService
$ balena env add EDITOR vim --application MyApp,MyApp2 --service MyService,MyService2
$ balena env add EDITOR vim --device 7cf02a6
$ balena env add EDITOR vim --device 7cf02a6,d6f1433
$ balena env add EDITOR vim --device 7cf02a6 --service MyService
$ balena env add EDITOR vim --device 7cf02a6,d6f1433 --service MyService,MyService2
### Arguments

View File

@ -39,17 +39,18 @@ interface ArgsDef {
export default class EnvAddCmd extends Command {
public static description = stripIndent`
Add an environment or config variable to an application, device or service.
Add an environment or config variable to one or more applications, devices or services.
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.
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
services.
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
@ -67,9 +68,13 @@ export default class EnvAddCmd extends Command {
public static examples = [
'$ balena env add TERM --application MyApp',
'$ balena env add EDITOR vim --application MyApp',
'$ balena env add EDITOR vim --application MyApp,MyApp2',
'$ balena env add EDITOR vim --application MyApp --service MyService',
'$ balena env add EDITOR vim --application MyApp,MyApp2 --service MyService,MyService2',
'$ balena env add EDITOR vim --device 7cf02a6',
'$ balena env add EDITOR vim --device 7cf02a6,d6f1433',
'$ balena env add EDITOR vim --device 7cf02a6 --service MyService',
'$ balena env add EDITOR vim --device 7cf02a6,d6f1433 --service MyService,MyService2',
];
public static args = [
@ -147,17 +152,31 @@ export default class EnvAddCmd extends Command {
const varType = isConfigVar ? 'configVar' : 'envVar';
if (options.application) {
for (const app of options.application.split(',')) {
try {
await balena.models.application[varType].set(
options.application,
app,
params.name,
params.value,
);
} catch (err) {
console.error(`${err.message}, app: ${app}`);
process.exitCode = 1;
}
}
} else if (options.device) {
for (const device of options.device.split(',')) {
try {
await balena.models.device[varType].set(
options.device,
device,
params.name,
params.value,
);
} catch (err) {
console.error(`${err.message}, device: ${device}`);
process.exitCode = 1;
}
}
}
}
}
@ -171,24 +190,44 @@ async function setServiceVars(
options: FlagsDef,
) {
if (options.application) {
const serviceId = await getServiceIdForApp(
sdk,
options.application,
options.service!,
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!,
);
await sdk.models.service.var.set(serviceId, params.name, params.value!);
} else {
} catch (err) {
console.error(`${err.message}, application: ${app}`);
process.exitCode = 1;
}
}
}
} else if (options.device) {
const { getDeviceAndAppFromUUID } = await import('../../utils/cloud');
const [device, app] = await getDeviceAndAppFromUUID(
for (const uuid of options.device.split(',')) {
let device;
let app;
try {
[device, app] = await getDeviceAndAppFromUUID(
sdk,
options.device!,
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,
options.service!,
service,
);
await sdk.models.device.serviceVar.set(
device.id,
@ -196,6 +235,12 @@ async function setServiceVars(
params.name,
params.value!,
);
} catch (err) {
console.error(`${err.message}, service: ${service}`);
process.exitCode = 1;
}
}
}
}
}

View File

@ -67,7 +67,7 @@ Additional commands:
device rm <uuid(s)> remove one or more devices
device shutdown <uuid> shutdown a device
devices supported list the supported device types (like 'raspberrypi3' or 'intel-nuc')
env add <name> [value] add an environment or config variable to an application, device or service
env add <name> [value] add an environment or config variable to one or more applications, devices or services
env rename <id> <value> change the value of a config or env var for an app, device or service
env rm <id> remove a config or env var from an application, device or service
envs list the environment or config variables of an application, device or service