Change env var commands to set app-wide env vars, using the new SDK

Change-type: major
This commit is contained in:
Tim Perry 2018-10-25 20:03:12 +02:00
parent c6eca9f895
commit dc6cde2cf1
2 changed files with 132 additions and 107 deletions

View File

@ -564,18 +564,19 @@ path to the config JSON file, see `balena os build-config`
Use this command to list all environment variables for
a particular application or device.
This command lists all custom environment variables.
If you want to see all environment variables, including private
ones used by balena, use the verbose option.
This command lists all application/device environment variables.
At the moment the CLI doesn't fully support multi-container applications,
so the following commands will only show service variables,
without showing which service they belong to.
If you want to see config variables, used to configure
balena features, use the --config option.
At the moment the CLI does not support per-service variables,
so the following commands will only show service-wide
environment variables.
Example:
$ balena envs --application MyApp
$ balena envs --application MyApp --verbose
$ balena envs --application MyApp --config
$ balena envs --device 7cf02a6
### Options
@ -588,16 +589,14 @@ application name
device uuid
#### --verbose, -v
#### --config, -c, -v, --verbose
show private environment variables
show config variables
## env rm <id>
Use this command to remove an environment variable from an application.
Don't remove balena specific variables, as things might not work as expected.
Notice this command asks for confirmation interactively.
You can avoid this by passing the `--yes` boolean option.
@ -621,11 +620,10 @@ device
## env add <key> [value]
Use this command to add an enviroment variable to an application.
Use this command to add an enviroment or config variable to an application.
At the moment the CLI doesn't fully support multi-container applications,
so the following commands will only set service variables for the first
service in your application.
so the following commands will set service-wide environment variables.
If value is omitted, the tool will attempt to use the variable's value
as defined in your host machine.
@ -654,7 +652,7 @@ device uuid
## env rename <id> <value>
Use this command to rename an enviroment variable from an application.
Use this command to change the value of an enviroment variable.
Pass the `--device` boolean option if you want to rename a device environment variable.

View File

@ -18,44 +18,59 @@ import { CommandDefinition } from 'capitano';
import * as commandOptions from './command-options';
import { normalizeUuidProp } from '../utils/normalization';
import { DeviceVariable, ApplicationVariable } from 'balena-sdk';
import { stripIndent } from 'common-tags';
const getReservedPrefixes = async (): Promise<string[]> => {
const balena = (await import('balena-sdk')).fromSharedOptions();
const settings = await balena.settings.getAll();
const response = await balena.request.send({
baseUrl: settings.apiUrl,
url: '/config/vars',
});
return response.body.reservedNamespaces;
};
export const list: CommandDefinition<
{},
{
application?: string;
device?: string;
verbose: boolean;
config: boolean;
}
> = {
signature: 'envs',
description: 'list all environment variables',
help: `\
Use this command to list all environment variables for
a particular application or device.
help: stripIndent`
Use this command to list all environment variables for
a particular application or device.
This command lists all custom environment variables.
If you want to see all environment variables, including private
ones used by balena, use the verbose option.
This command lists all application/device environment variables.
At the moment the CLI doesn't fully support multi-container applications,
so the following commands will only show service variables,
without showing which service they belong to.
If you want to see config variables, used to configure
balena features, use the --config option.
Example:
At the moment the CLI does not support per-service variables,
so the following commands will only show service-wide
environment variables.
$ balena envs --application MyApp
$ balena envs --application MyApp --verbose
$ balena envs --device 7cf02a6\
`,
Example:
$ balena envs --application MyApp
$ balena envs --application MyApp --config
$ balena envs --device 7cf02a6
`,
options: [
commandOptions.optionalApplication,
commandOptions.optionalDevice,
{
signature: 'verbose',
description: 'show private environment variables',
signature: 'config',
description: 'show config variables',
boolean: true,
alias: 'v',
alias: ['c', 'v', 'verbose'],
},
],
permission: 'user',
@ -63,33 +78,32 @@ Example:
normalizeUuidProp(options, 'device');
const Bluebird = await import('bluebird');
const _ = await import('lodash');
const balena = require('resin-sdk-preconfigured');
const balena = (await import('balena-sdk')).fromSharedOptions();
const visuals = await import('resin-cli-visuals');
const { exitWithExpectedError } = await import('../utils/patterns');
return Bluebird.try(function() {
if (options.application != null) {
return balena.models.environmentVariables.getAllByApplication(
options.application,
);
} else if (options.device != null) {
return balena.models.environmentVariables.device.getAll(options.device);
return Bluebird.try(function(): Promise<
DeviceVariable[] | ApplicationVariable[]
> {
if (options.application) {
return balena.models.application[
options.config ? 'configVar' : 'envVar'
].getAllByApplication(options.application);
} else if (options.device) {
return balena.models.device[
options.config ? 'configVar' : 'envVar'
].getAllByDevice(options.device);
} else {
exitWithExpectedError('You must specify an application or device');
return exitWithExpectedError(
'You must specify an application or device',
);
}
})
.tap(function(environmentVariables) {
if (_.isEmpty(environmentVariables)) {
exitWithExpectedError('No environment variables found');
}
if (!options.verbose) {
const { isSystemVariable } = balena.models.environmentVariables;
environmentVariables = _.reject(
environmentVariables,
isSystemVariable,
);
}
console.log(
visuals.table.horizontal(environmentVariables, [
@ -114,26 +128,24 @@ export const remove: CommandDefinition<
> = {
signature: 'env rm <id>',
description: 'remove an environment variable',
help: `\
Use this command to remove an environment variable from an application.
help: stripIndent`
Use this command to remove an environment variable from an application.
Don't remove balena specific variables, as things might not work as expected.
Notice this command asks for confirmation interactively.
You can avoid this by passing the \`--yes\` boolean option.
Notice this command asks for confirmation interactively.
You can avoid this by passing the \`--yes\` boolean option.
If you want to eliminate a device environment variable, pass the \`--device\` boolean option.
If you want to eliminate a device environment variable, pass the \`--device\` boolean option.
Examples:
Examples:
$ balena env rm 215
$ balena env rm 215 --yes
$ balena env rm 215 --device\
`,
$ balena env rm 215
$ balena env rm 215 --yes
$ balena env rm 215 --device
`,
options: [commandOptions.yes, commandOptions.booleanDevice],
permission: 'user',
async action(params, options, done) {
const balena = require('resin-sdk-preconfigured');
const balena = (await import('balena-sdk')).fromSharedOptions();
const patterns = await import('../utils/patterns');
return patterns
@ -143,9 +155,15 @@ Examples:
)
.then(function() {
if (options.device) {
return balena.models.environmentVariables.device.remove(params.id);
return balena.pine.delete({
resource: 'device_environment_variable',
id: params.id,
});
} else {
return balena.models.environmentVariables.remove(params.id);
return balena.pine.delete({
resource: 'application_environment_variable',
id: params.id,
});
}
})
.nodeify(done);
@ -163,39 +181,39 @@ export const add: CommandDefinition<
}
> = {
signature: 'env add <key> [value]',
description: 'add an environment variable',
help: `\
Use this command to add an enviroment variable to an application.
description: 'add an environment or config variable',
help: stripIndent`
Use this command to add an enviroment or config variable to an application.
At the moment the CLI doesn't fully support multi-container applications,
so the following commands will only set service variables for the first
service in your application.
At the moment the CLI doesn't fully support multi-container applications,
so the following commands will set service-wide environment variables.
If value is omitted, the tool will attempt to use the variable's value
as defined in your host machine.
If value is omitted, the tool will attempt to use the variable's value
as defined in your host machine.
Use the \`--device\` option if you want to assign the environment variable
to a specific device.
Use the \`--device\` option if you want to assign the environment variable
to a specific device.
If the value is grabbed from the environment, a warning message will be printed.
Use \`--quiet\` to remove it.
If the value is grabbed from the environment, a warning message will be printed.
Use \`--quiet\` to remove it.
Examples:
Examples:
$ balena env add EDITOR vim --application MyApp
$ balena env add TERM --application MyApp
$ balena env add EDITOR vim --device 7cf02a6\
`,
$ balena env add EDITOR vim --application MyApp
$ balena env add TERM --application MyApp
$ balena env add EDITOR vim --device 7cf02a6
`,
options: [commandOptions.optionalApplication, commandOptions.optionalDevice],
permission: 'user',
async action(params, options, done) {
normalizeUuidProp(options, 'device');
const Bluebird = await import('bluebird');
const balena = require('resin-sdk-preconfigured');
const _ = await import('lodash');
const balena = (await import('balena-sdk')).fromSharedOptions();
const { exitWithExpectedError } = await import('../utils/patterns');
return Bluebird.try(function() {
return Bluebird.try(async function() {
if (params.value == null) {
params.value = process.env[params.key];
@ -210,14 +228,17 @@ Examples:
}
}
if (options.application != null) {
return balena.models.environmentVariables.create(
options.application,
params.key,
params.value,
);
} else if (options.device != null) {
return balena.models.environmentVariables.device.create(
const reservedPrefixes = await getReservedPrefixes();
const isConfigVar = _.some(reservedPrefixes, prefix =>
_.startsWith(params.key, prefix),
);
if (options.application) {
return balena.models.application[
isConfigVar ? 'configVar' : 'envVar'
].set(options.application, params.key, params.value);
} else if (options.device) {
return balena.models.device[isConfigVar ? 'configVar' : 'envVar'].set(
options.device,
params.key,
params.value,
@ -240,33 +261,39 @@ export const rename: CommandDefinition<
> = {
signature: 'env rename <id> <value>',
description: 'rename an environment variable',
help: `\
Use this command to rename an enviroment variable from an application.
help: stripIndent`
Use this command to change the value of an enviroment variable.
Pass the \`--device\` boolean option if you want to rename a device environment variable.
Pass the \`--device\` boolean option if you want to rename a device environment variable.
Examples:
Examples:
$ balena env rename 376 emacs
$ balena env rename 376 emacs --device\
`,
$ balena env rename 376 emacs
$ balena env rename 376 emacs --device
`,
permission: 'user',
options: [commandOptions.booleanDevice],
async action(params, options, done) {
const Bluebird = await import('bluebird');
const balena = require('resin-sdk-preconfigured');
const balena = (await import('balena-sdk')).fromSharedOptions();
return Bluebird.try(function() {
if (options.device) {
return balena.models.environmentVariables.device.update(
params.id,
params.value,
);
return balena.pine.patch({
resource: 'device_environment_variable',
id: params.id,
body: {
value: params.value,
},
});
} else {
return balena.models.environmentVariables.update(
params.id,
params.value,
);
return balena.pine.patch({
resource: 'application_environment_variable',
id: params.id,
body: {
value: params.value,
},
});
}
}).nodeify(done);
},