2018-10-25 12:57:45 +00:00
|
|
|
/*
|
|
|
|
Copyright 2016-2017 Balena
|
|
|
|
|
|
|
|
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.
|
|
|
|
*/
|
|
|
|
|
|
|
|
import { CommandDefinition } from 'capitano';
|
|
|
|
|
|
|
|
import * as commandOptions from './command-options';
|
|
|
|
import { normalizeUuidProp } from '../utils/normalization';
|
2018-10-25 18:03:12 +00:00
|
|
|
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;
|
|
|
|
};
|
2018-10-25 12:57:45 +00:00
|
|
|
|
|
|
|
export const list: CommandDefinition<
|
|
|
|
{},
|
|
|
|
{
|
|
|
|
application?: string;
|
|
|
|
device?: string;
|
2018-10-25 18:03:12 +00:00
|
|
|
config: boolean;
|
2018-10-25 12:57:45 +00:00
|
|
|
}
|
|
|
|
> = {
|
|
|
|
signature: 'envs',
|
|
|
|
description: 'list all environment variables',
|
2018-10-25 18:03:12 +00:00
|
|
|
help: stripIndent`
|
|
|
|
Use this command to list all environment variables for
|
|
|
|
a particular application or device.
|
|
|
|
|
|
|
|
This command lists all application/device environment variables.
|
2018-10-25 12:57:45 +00:00
|
|
|
|
2018-10-25 18:03:12 +00:00
|
|
|
If you want to see config variables, used to configure
|
|
|
|
balena features, use the --config option.
|
2018-10-25 12:57:45 +00:00
|
|
|
|
2018-10-25 18:03:12 +00:00
|
|
|
At the moment the CLI does not support per-service variables,
|
|
|
|
so the following commands will only show service-wide
|
|
|
|
environment variables.
|
2018-10-25 12:57:45 +00:00
|
|
|
|
2018-10-25 18:03:12 +00:00
|
|
|
Example:
|
2018-10-25 12:57:45 +00:00
|
|
|
|
2018-10-25 18:03:12 +00:00
|
|
|
$ balena envs --application MyApp
|
|
|
|
$ balena envs --application MyApp --config
|
|
|
|
$ balena envs --device 7cf02a6
|
|
|
|
`,
|
2018-10-25 12:57:45 +00:00
|
|
|
options: [
|
|
|
|
commandOptions.optionalApplication,
|
|
|
|
commandOptions.optionalDevice,
|
|
|
|
|
|
|
|
{
|
2018-10-25 18:03:12 +00:00
|
|
|
signature: 'config',
|
|
|
|
description: 'show config variables',
|
2018-10-25 12:57:45 +00:00
|
|
|
boolean: true,
|
2018-10-25 18:03:12 +00:00
|
|
|
alias: ['c', 'v', 'verbose'],
|
2018-10-25 12:57:45 +00:00
|
|
|
},
|
|
|
|
],
|
|
|
|
permission: 'user',
|
|
|
|
async action(_params, options, done) {
|
|
|
|
normalizeUuidProp(options, 'device');
|
|
|
|
const Bluebird = await import('bluebird');
|
|
|
|
const _ = await import('lodash');
|
2018-10-25 18:03:12 +00:00
|
|
|
const balena = (await import('balena-sdk')).fromSharedOptions();
|
2018-10-25 12:57:45 +00:00
|
|
|
const visuals = await import('resin-cli-visuals');
|
|
|
|
|
|
|
|
const { exitWithExpectedError } = await import('../utils/patterns');
|
|
|
|
|
2018-10-25 18:03:12 +00:00
|
|
|
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);
|
2018-10-25 12:57:45 +00:00
|
|
|
} else {
|
2018-10-25 18:03:12 +00:00
|
|
|
return exitWithExpectedError(
|
|
|
|
'You must specify an application or device',
|
|
|
|
);
|
2018-10-25 12:57:45 +00:00
|
|
|
}
|
|
|
|
})
|
|
|
|
.tap(function(environmentVariables) {
|
|
|
|
if (_.isEmpty(environmentVariables)) {
|
|
|
|
exitWithExpectedError('No environment variables found');
|
|
|
|
}
|
|
|
|
|
|
|
|
console.log(
|
|
|
|
visuals.table.horizontal(environmentVariables, [
|
|
|
|
'id',
|
|
|
|
'name',
|
|
|
|
'value',
|
|
|
|
]),
|
|
|
|
);
|
|
|
|
})
|
|
|
|
.nodeify(done);
|
|
|
|
},
|
|
|
|
};
|
|
|
|
|
|
|
|
export const remove: CommandDefinition<
|
|
|
|
{
|
|
|
|
id: number;
|
|
|
|
},
|
|
|
|
{
|
|
|
|
yes: boolean;
|
|
|
|
device: boolean;
|
|
|
|
}
|
|
|
|
> = {
|
|
|
|
signature: 'env rm <id>',
|
|
|
|
description: 'remove an environment variable',
|
2018-10-25 18:03:12 +00:00
|
|
|
help: stripIndent`
|
|
|
|
Use this command to remove an environment variable from an application.
|
2018-10-25 12:57:45 +00:00
|
|
|
|
2018-10-25 18:03:12 +00:00
|
|
|
Notice this command asks for confirmation interactively.
|
|
|
|
You can avoid this by passing the \`--yes\` boolean option.
|
2018-10-25 12:57:45 +00:00
|
|
|
|
2018-10-25 18:03:12 +00:00
|
|
|
If you want to eliminate a device environment variable, pass the \`--device\` boolean option.
|
2018-10-25 12:57:45 +00:00
|
|
|
|
2018-10-25 18:03:12 +00:00
|
|
|
Examples:
|
2018-10-25 12:57:45 +00:00
|
|
|
|
2018-10-25 18:03:12 +00:00
|
|
|
$ balena env rm 215
|
|
|
|
$ balena env rm 215 --yes
|
|
|
|
$ balena env rm 215 --device
|
|
|
|
`,
|
2018-10-25 12:57:45 +00:00
|
|
|
options: [commandOptions.yes, commandOptions.booleanDevice],
|
|
|
|
permission: 'user',
|
|
|
|
async action(params, options, done) {
|
2018-10-25 18:03:12 +00:00
|
|
|
const balena = (await import('balena-sdk')).fromSharedOptions();
|
2018-10-25 12:57:45 +00:00
|
|
|
const patterns = await import('../utils/patterns');
|
|
|
|
|
|
|
|
return patterns
|
|
|
|
.confirm(
|
|
|
|
options.yes,
|
|
|
|
'Are you sure you want to delete the environment variable?',
|
|
|
|
)
|
|
|
|
.then(function() {
|
|
|
|
if (options.device) {
|
2018-10-25 18:03:12 +00:00
|
|
|
return balena.pine.delete({
|
|
|
|
resource: 'device_environment_variable',
|
|
|
|
id: params.id,
|
|
|
|
});
|
2018-10-25 12:57:45 +00:00
|
|
|
} else {
|
2018-10-25 18:03:12 +00:00
|
|
|
return balena.pine.delete({
|
|
|
|
resource: 'application_environment_variable',
|
|
|
|
id: params.id,
|
|
|
|
});
|
2018-10-25 12:57:45 +00:00
|
|
|
}
|
|
|
|
})
|
|
|
|
.nodeify(done);
|
|
|
|
},
|
|
|
|
};
|
|
|
|
|
|
|
|
export const add: CommandDefinition<
|
|
|
|
{
|
|
|
|
key: string;
|
|
|
|
value?: string;
|
|
|
|
},
|
|
|
|
{
|
|
|
|
application?: string;
|
|
|
|
device?: string;
|
|
|
|
}
|
|
|
|
> = {
|
|
|
|
signature: 'env add <key> [value]',
|
2018-10-25 18:03:12 +00:00
|
|
|
description: 'add an environment or config variable',
|
|
|
|
help: stripIndent`
|
|
|
|
Use this command to add an enviroment or config variable to an application.
|
2018-10-25 12:57:45 +00:00
|
|
|
|
2018-10-25 18:03:12 +00:00
|
|
|
At the moment the CLI doesn't fully support multi-container applications,
|
|
|
|
so the following commands will set service-wide environment variables.
|
2018-10-25 12:57:45 +00:00
|
|
|
|
2018-10-25 18:03:12 +00:00
|
|
|
If value is omitted, the tool will attempt to use the variable's value
|
|
|
|
as defined in your host machine.
|
2018-10-25 12:57:45 +00:00
|
|
|
|
2018-10-25 18:03:12 +00:00
|
|
|
Use the \`--device\` option if you want to assign the environment variable
|
|
|
|
to a specific device.
|
2018-10-25 12:57:45 +00:00
|
|
|
|
2018-10-25 18:03:12 +00:00
|
|
|
If the value is grabbed from the environment, a warning message will be printed.
|
|
|
|
Use \`--quiet\` to remove it.
|
2018-10-25 12:57:45 +00:00
|
|
|
|
2018-10-25 18:03:12 +00:00
|
|
|
Examples:
|
2018-10-25 12:57:45 +00:00
|
|
|
|
2018-10-25 18:03:12 +00:00
|
|
|
$ balena env add EDITOR vim --application MyApp
|
|
|
|
$ balena env add TERM --application MyApp
|
|
|
|
$ balena env add EDITOR vim --device 7cf02a6
|
|
|
|
`,
|
2018-10-25 12:57:45 +00:00
|
|
|
options: [commandOptions.optionalApplication, commandOptions.optionalDevice],
|
|
|
|
permission: 'user',
|
|
|
|
async action(params, options, done) {
|
|
|
|
normalizeUuidProp(options, 'device');
|
|
|
|
const Bluebird = await import('bluebird');
|
2018-10-25 18:03:12 +00:00
|
|
|
const _ = await import('lodash');
|
|
|
|
const balena = (await import('balena-sdk')).fromSharedOptions();
|
2018-10-25 12:57:45 +00:00
|
|
|
|
|
|
|
const { exitWithExpectedError } = await import('../utils/patterns');
|
|
|
|
|
2018-10-25 18:03:12 +00:00
|
|
|
return Bluebird.try(async function() {
|
2018-10-25 12:57:45 +00:00
|
|
|
if (params.value == null) {
|
|
|
|
params.value = process.env[params.key];
|
|
|
|
|
|
|
|
if (params.value == null) {
|
|
|
|
throw new Error(`Environment value not found for key: ${params.key}`);
|
|
|
|
} else {
|
|
|
|
console.info(
|
|
|
|
`Warning: using ${params.key}=${
|
|
|
|
params.value
|
|
|
|
} from host environment`,
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-10-25 18:03:12 +00:00
|
|
|
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(
|
2018-10-25 12:57:45 +00:00
|
|
|
options.device,
|
|
|
|
params.key,
|
|
|
|
params.value,
|
|
|
|
);
|
|
|
|
} else {
|
|
|
|
exitWithExpectedError('You must specify an application or device');
|
|
|
|
}
|
|
|
|
}).nodeify(done);
|
|
|
|
},
|
|
|
|
};
|
|
|
|
|
|
|
|
export const rename: CommandDefinition<
|
|
|
|
{
|
|
|
|
id: number;
|
|
|
|
value: string;
|
|
|
|
},
|
|
|
|
{
|
|
|
|
device: boolean;
|
|
|
|
}
|
|
|
|
> = {
|
|
|
|
signature: 'env rename <id> <value>',
|
|
|
|
description: 'rename an environment variable',
|
2018-10-25 18:03:12 +00:00
|
|
|
help: stripIndent`
|
|
|
|
Use this command to change the value of an enviroment variable.
|
2018-10-25 12:57:45 +00:00
|
|
|
|
2018-10-25 18:03:12 +00:00
|
|
|
Pass the \`--device\` boolean option if you want to rename a device environment variable.
|
2018-10-25 12:57:45 +00:00
|
|
|
|
2018-10-25 18:03:12 +00:00
|
|
|
Examples:
|
2018-10-25 12:57:45 +00:00
|
|
|
|
2018-10-25 18:03:12 +00:00
|
|
|
$ balena env rename 376 emacs
|
|
|
|
$ balena env rename 376 emacs --device
|
|
|
|
`,
|
2018-10-25 12:57:45 +00:00
|
|
|
permission: 'user',
|
|
|
|
options: [commandOptions.booleanDevice],
|
|
|
|
async action(params, options, done) {
|
|
|
|
const Bluebird = await import('bluebird');
|
2018-10-25 18:03:12 +00:00
|
|
|
const balena = (await import('balena-sdk')).fromSharedOptions();
|
2018-10-25 12:57:45 +00:00
|
|
|
|
|
|
|
return Bluebird.try(function() {
|
|
|
|
if (options.device) {
|
2018-10-25 18:03:12 +00:00
|
|
|
return balena.pine.patch({
|
|
|
|
resource: 'device_environment_variable',
|
|
|
|
id: params.id,
|
|
|
|
body: {
|
|
|
|
value: params.value,
|
|
|
|
},
|
|
|
|
});
|
2018-10-25 12:57:45 +00:00
|
|
|
} else {
|
2018-10-25 18:03:12 +00:00
|
|
|
return balena.pine.patch({
|
|
|
|
resource: 'application_environment_variable',
|
|
|
|
id: params.id,
|
|
|
|
body: {
|
|
|
|
value: params.value,
|
|
|
|
},
|
|
|
|
});
|
2018-10-25 12:57:45 +00:00
|
|
|
}
|
|
|
|
}).nodeify(done);
|
|
|
|
},
|
|
|
|
};
|