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] ## env add NAME [VALUE]
Add an environment or config variable to an application, device or service, Add an environment or config variable to one or more applications, devices
as selected by the respective command-line options. Either the --application or services, as selected by the respective command-line options. Either the
or the --device option must be provided, and either may be be used alongside --application or the --device option must be provided, and either may be be
the --service option to define a service-specific variable. (A service is an used alongside the --service option to define a service-specific variable.
application container in a "microservices" application.) When the --service (A service is an application container in a "microservices" application.)
option is used in conjunction with the --device option, the service variable When the --service option is used in conjunction with the --device option,
applies to the selected device only. Otherwise, it applies to all devices of the service variable applies to the selected device only. Otherwise, it
the selected application (i.e., the application's fleet). If the --service applies to all devices of the selected application (i.e., the application's
option is omitted, the variable applies to all services. 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 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 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 TERM --application MyApp
$ balena env add EDITOR vim --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 --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
$ balena env add EDITOR vim --device 7cf02a6,d6f1433
$ balena env add EDITOR vim --device 7cf02a6 --service MyService $ balena env add EDITOR vim --device 7cf02a6 --service MyService
$ balena env add EDITOR vim --device 7cf02a6,d6f1433 --service MyService,MyService2
### Arguments ### Arguments

View File

@ -39,17 +39,18 @@ interface ArgsDef {
export default class EnvAddCmd extends Command { export default class EnvAddCmd extends Command {
public static description = stripIndent` 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, Add an environment or config variable to one or more applications, devices
as selected by the respective command-line options. Either the --application or services, as selected by the respective command-line options. Either the
or the --device option must be provided, and either may be be used alongside --application or the --device option must be provided, and either may be be
the --service option to define a service-specific variable. (A service is an used alongside the --service option to define a service-specific variable.
application container in a "microservices" application.) When the --service (A service is an application container in a "microservices" application.)
option is used in conjunction with the --device option, the service variable When the --service option is used in conjunction with the --device option,
applies to the selected device only. Otherwise, it applies to all devices of the service variable applies to the selected device only. Otherwise, it
the selected application (i.e., the application's fleet). If the --service applies to all devices of the selected application (i.e., the application's
option is omitted, the variable applies to all services. 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 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 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 = [ public static examples = [
'$ balena env add TERM --application MyApp', '$ balena env add TERM --application MyApp',
'$ balena env add EDITOR vim --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 --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',
'$ balena env add EDITOR vim --device 7cf02a6,d6f1433',
'$ balena env add EDITOR vim --device 7cf02a6 --service MyService', '$ balena env add EDITOR vim --device 7cf02a6 --service MyService',
'$ balena env add EDITOR vim --device 7cf02a6,d6f1433 --service MyService,MyService2',
]; ];
public static args = [ public static args = [
@ -147,17 +152,31 @@ export default class EnvAddCmd extends Command {
const varType = isConfigVar ? 'configVar' : 'envVar'; const varType = isConfigVar ? 'configVar' : 'envVar';
if (options.application) { if (options.application) {
await balena.models.application[varType].set( for (const app of options.application.split(',')) {
options.application, try {
params.name, await balena.models.application[varType].set(
params.value, app,
); params.name,
params.value,
);
} catch (err) {
console.error(`${err.message}, app: ${app}`);
process.exitCode = 1;
}
}
} else if (options.device) { } else if (options.device) {
await balena.models.device[varType].set( for (const device of options.device.split(',')) {
options.device, try {
params.name, await balena.models.device[varType].set(
params.value, device,
); params.name,
params.value,
);
} catch (err) {
console.error(`${err.message}, device: ${device}`);
process.exitCode = 1;
}
}
} }
} }
} }
@ -171,31 +190,57 @@ async function setServiceVars(
options: FlagsDef, options: FlagsDef,
) { ) {
if (options.application) { if (options.application) {
const serviceId = await getServiceIdForApp( for (const app of options.application.split(',')) {
sdk, for (const service of options.service!.split(',')) {
options.application, try {
options.service!, const serviceId = await getServiceIdForApp(sdk, app, service);
); await sdk.models.service.var.set(
await sdk.models.service.var.set(serviceId, params.name, params.value!); serviceId,
} else { params.name,
params.value!,
);
} catch (err) {
console.error(`${err.message}, application: ${app}`);
process.exitCode = 1;
}
}
}
} else if (options.device) {
const { getDeviceAndAppFromUUID } = await import('../../utils/cloud'); const { getDeviceAndAppFromUUID } = await import('../../utils/cloud');
const [device, app] = await getDeviceAndAppFromUUID( for (const uuid of options.device.split(',')) {
sdk, let device;
options.device!, let app;
['id'], try {
['app_name'], [device, app] = await getDeviceAndAppFromUUID(
); sdk,
const serviceId = await getServiceIdForApp( uuid,
sdk, ['id'],
app.app_name, ['app_name'],
options.service!, );
); } catch (err) {
await sdk.models.device.serviceVar.set( console.error(`${err.message}, device: ${uuid}`);
device.id, process.exitCode = 1;
serviceId, continue;
params.name, }
params.value!, 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;
}
}
}
} }
} }

View File

@ -67,7 +67,7 @@ Additional commands:
device rm <uuid(s)> remove one or more devices device rm <uuid(s)> remove one or more devices
device shutdown <uuid> shutdown a device device shutdown <uuid> shutdown a device
devices supported list the supported device types (like 'raspberrypi3' or 'intel-nuc') 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 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 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 envs list the environment or config variables of an application, device or service