2015-11-25 02:40:17 +02:00
|
|
|
/*
|
2016-03-19 01:51:22 +02:00
|
|
|
* Copyright 2016 Joyent Inc.
|
2015-11-25 02:40:17 +02:00
|
|
|
*
|
|
|
|
* `triton env ...`
|
|
|
|
*/
|
|
|
|
|
|
|
|
var assert = require('assert-plus');
|
|
|
|
var format = require('util').format;
|
|
|
|
var fs = require('fs');
|
2016-03-19 01:51:22 +02:00
|
|
|
var path = require('path');
|
2015-11-25 02:40:17 +02:00
|
|
|
var strsplit = require('strsplit');
|
|
|
|
var sshpk = require('sshpk');
|
|
|
|
var vasync = require('vasync');
|
|
|
|
|
|
|
|
var common = require('./common');
|
|
|
|
var errors = require('./errors');
|
|
|
|
var mod_config = require('./config');
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
function do_env(subcmd, opts, args, cb) {
|
2016-03-19 01:51:22 +02:00
|
|
|
var self = this;
|
2015-11-25 02:40:17 +02:00
|
|
|
if (opts.help) {
|
|
|
|
this.do_help('help', {}, [subcmd], cb);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
if (args.length > 1) {
|
|
|
|
return cb(new errors.UsageError('too many arguments'));
|
|
|
|
}
|
|
|
|
|
|
|
|
var profileName = args[0] || this.tritonapi.profile.name;
|
2016-03-19 01:51:22 +02:00
|
|
|
var allClientTypes = ['triton', 'docker', 'smartdc'];
|
2016-01-12 20:27:46 +02:00
|
|
|
var clientTypes = [];
|
2016-03-19 01:51:22 +02:00
|
|
|
var explicit;
|
|
|
|
var shortOpts = '';
|
2016-01-12 20:27:46 +02:00
|
|
|
if (opts.triton) {
|
2016-03-19 01:51:22 +02:00
|
|
|
shortOpts += 't';
|
2016-01-12 20:27:46 +02:00
|
|
|
clientTypes.push('triton');
|
|
|
|
}
|
2016-03-19 01:51:22 +02:00
|
|
|
if (opts.docker) {
|
|
|
|
shortOpts += 'd';
|
|
|
|
clientTypes.push('docker');
|
|
|
|
}
|
|
|
|
if (opts.smartdc) {
|
|
|
|
shortOpts += 's';
|
|
|
|
clientTypes.push('smartdc');
|
|
|
|
}
|
2016-04-22 22:55:32 +03:00
|
|
|
if (opts.unset) {
|
|
|
|
shortOpts += 'u';
|
|
|
|
}
|
2016-01-12 20:27:46 +02:00
|
|
|
if (clientTypes.length === 0) {
|
2016-03-19 01:51:22 +02:00
|
|
|
explicit = false;
|
2016-01-12 20:27:46 +02:00
|
|
|
clientTypes = allClientTypes;
|
2016-03-19 01:51:22 +02:00
|
|
|
} else {
|
|
|
|
explicit = true;
|
2015-11-25 02:40:17 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
try {
|
|
|
|
var profile = mod_config.loadProfile({
|
|
|
|
configDir: this.configDir,
|
|
|
|
name: profileName
|
|
|
|
});
|
|
|
|
} catch (err) {
|
|
|
|
return cb(err);
|
|
|
|
}
|
|
|
|
if (profile.name === this.tritonapi.profile.name) {
|
|
|
|
this._applyProfileOverrides(profile);
|
|
|
|
}
|
|
|
|
|
|
|
|
var p = console.log;
|
2016-01-12 20:27:46 +02:00
|
|
|
clientTypes.forEach(function (clientType) {
|
|
|
|
switch (clientType) {
|
|
|
|
case 'triton':
|
2016-04-22 22:55:32 +03:00
|
|
|
if (opts.unset) {
|
|
|
|
[
|
|
|
|
'TRITON_PROFILE',
|
|
|
|
'TRITON_URL',
|
|
|
|
'TRITON_ACCOUNT',
|
|
|
|
'TRITON_USER',
|
|
|
|
'TRITON_KEY_ID',
|
|
|
|
'TRITON_TLS_INSECURE'
|
|
|
|
].forEach(function (key) {
|
|
|
|
p('unset %s', key);
|
|
|
|
});
|
|
|
|
} else {
|
|
|
|
p('export TRITON_PROFILE="%s"', profile.name);
|
|
|
|
}
|
2016-01-12 20:27:46 +02:00
|
|
|
break;
|
2016-03-19 01:51:22 +02:00
|
|
|
case 'docker':
|
|
|
|
var setupJson = path.resolve(self.configDir, 'docker',
|
|
|
|
common.profileSlug(profile), 'setup.json');
|
|
|
|
if (fs.existsSync(setupJson)) {
|
|
|
|
var setup;
|
|
|
|
try {
|
|
|
|
setup = JSON.parse(fs.readFileSync(setupJson));
|
|
|
|
} catch (err) {
|
|
|
|
cb(new errors.ConfigError(err, format(
|
|
|
|
'error determining Docker environment from "%s": %s',
|
|
|
|
setupJson, err)));
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
Object.keys(setup.env).forEach(function (key) {
|
|
|
|
var val = setup.env[key];
|
2016-04-22 22:55:32 +03:00
|
|
|
if (opts.unset || val === null) {
|
2016-03-19 01:51:22 +02:00
|
|
|
p('unset %s', key);
|
|
|
|
} else {
|
|
|
|
p('export %s=%s', key, val);
|
|
|
|
}
|
|
|
|
});
|
2016-04-22 22:55:32 +03:00
|
|
|
} else if (opts.unset) {
|
|
|
|
[
|
|
|
|
'DOCKER_HOST',
|
|
|
|
'DOCKER_CERT_PATH',
|
|
|
|
'DOCKER_TLS_VERIFY',
|
|
|
|
'COMPOSE_HTTP_TIMEOUT'
|
|
|
|
].forEach(function (key) {
|
|
|
|
p('unset %s', key);
|
|
|
|
});
|
2016-03-19 01:51:22 +02:00
|
|
|
} else if (explicit) {
|
|
|
|
cb(new errors.ConfigError(format('could not find Docker '
|
|
|
|
+ 'environment setup for profile "%s":\n Run `triton '
|
|
|
|
+ 'profile docker-setup %s` to setup.',
|
|
|
|
profile.name, profile.name)));
|
|
|
|
}
|
|
|
|
break;
|
2016-01-12 20:27:46 +02:00
|
|
|
case 'smartdc':
|
2016-04-22 22:55:32 +03:00
|
|
|
if (opts.unset) {
|
|
|
|
[
|
|
|
|
'SDC_URL',
|
|
|
|
'SDC_ACCOUNT',
|
|
|
|
'SDC_USER',
|
|
|
|
'SDC_KEY_ID',
|
|
|
|
'SDC_TESTING'
|
|
|
|
].forEach(function (key) {
|
|
|
|
p('unset %s', key);
|
|
|
|
});
|
2016-01-12 20:27:46 +02:00
|
|
|
} else {
|
2016-04-22 22:55:32 +03:00
|
|
|
p('export SDC_URL="%s"', profile.url);
|
|
|
|
p('export SDC_ACCOUNT="%s"', profile.account);
|
|
|
|
if (profile.user) {
|
|
|
|
p('export SDC_USER="%s"', profile.user);
|
|
|
|
} else {
|
|
|
|
p('unset SDC_USER');
|
|
|
|
}
|
|
|
|
p('export SDC_KEY_ID="%s"', profile.keyId);
|
|
|
|
if (profile.insecure) {
|
|
|
|
p('export SDC_TESTING="%s"', profile.insecure);
|
|
|
|
} else {
|
|
|
|
p('unset SDC_TESTING');
|
|
|
|
}
|
2016-01-12 20:27:46 +02:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
return cb(new errors.InternalError(
|
|
|
|
'unknown clientType: ' + clientType));
|
2015-11-25 02:40:17 +02:00
|
|
|
}
|
2016-01-12 20:27:46 +02:00
|
|
|
});
|
|
|
|
|
|
|
|
p('# Run this command to configure your shell:');
|
2016-03-19 01:51:22 +02:00
|
|
|
p('# eval "$(triton env%s%s)"',
|
|
|
|
(shortOpts ? ' -'+shortOpts : ''),
|
|
|
|
(profile.name === this.tritonapi.profile.name
|
|
|
|
? '' : ' ' + profile.name));
|
2015-11-25 02:40:17 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
do_env.options = [
|
|
|
|
{
|
|
|
|
names: ['help', 'h'],
|
|
|
|
type: 'bool',
|
|
|
|
help: 'Show this help.'
|
|
|
|
},
|
2016-04-22 22:55:32 +03:00
|
|
|
{
|
|
|
|
group: ''
|
|
|
|
},
|
2016-01-12 20:27:46 +02:00
|
|
|
{
|
|
|
|
names: ['triton', 't'],
|
|
|
|
type: 'bool',
|
|
|
|
help: 'Emit environment commands for node-triton itself (i.e. the ' +
|
|
|
|
'"TRITON_PROFILE" variable).'
|
2016-03-19 01:51:22 +02:00
|
|
|
},
|
|
|
|
{
|
|
|
|
names: ['docker', 'd'],
|
|
|
|
type: 'bool',
|
|
|
|
help: 'Emit environment commands for docker ("DOCKER_HOST" et al).'
|
|
|
|
},
|
|
|
|
{
|
|
|
|
names: ['smartdc', 's'],
|
|
|
|
type: 'bool',
|
|
|
|
help: 'Emit environment for node-smartdc (i.e. the "SDC_*" variables).'
|
2016-04-22 22:55:32 +03:00
|
|
|
},
|
|
|
|
{
|
|
|
|
group: ''
|
|
|
|
},
|
|
|
|
{
|
|
|
|
names: ['unset', 'u'],
|
|
|
|
type: 'bool',
|
|
|
|
help: 'Emit environment to *unset* the relevant environment variables.'
|
2015-11-25 02:40:17 +02:00
|
|
|
}
|
|
|
|
];
|
|
|
|
|
2016-01-12 20:27:46 +02:00
|
|
|
// TODO: support env for docker usage.
|
2015-11-25 02:40:17 +02:00
|
|
|
do_env.help = [
|
|
|
|
/* BEGIN JSSTYLED */
|
2016-04-22 22:55:32 +03:00
|
|
|
'Emit shell commands to setup environment.',
|
2015-11-25 02:40:17 +02:00
|
|
|
'',
|
2016-01-12 20:27:46 +02:00
|
|
|
'Supported "clients" here are: node-smartdc (i.e. the `sdc-*` tools),',
|
|
|
|
'and node-triton itself. By default this emits the environment for all',
|
|
|
|
'supported tools. Use options to be specific.',
|
2015-11-25 02:40:17 +02:00
|
|
|
'',
|
|
|
|
'Usage:',
|
|
|
|
' {{name}} env [PROFILE]',
|
|
|
|
'',
|
2016-04-22 22:55:32 +03:00
|
|
|
'{{options}}',
|
|
|
|
'If no options are given, environment variables are emitted for all clients.',
|
|
|
|
'If PROFILE is not given, the current profile is used.',
|
|
|
|
'',
|
|
|
|
'The following Bash function can be added to one\'s "~/.bashrc" to quickly',
|
|
|
|
'change between Triton profiles:',
|
|
|
|
' triton-select () { eval "$(triton env $1)"; }',
|
|
|
|
'for example:',
|
|
|
|
' $ triton-select west1',
|
|
|
|
' $ triton profile get | grep name',
|
|
|
|
' name: west1',
|
|
|
|
' $ triton-select east1',
|
|
|
|
' $ triton profile get | grep name',
|
|
|
|
' name: east1'
|
2015-11-25 02:40:17 +02:00
|
|
|
/* END JSSTYLED */
|
|
|
|
].join('\n');
|
|
|
|
|
2016-04-22 23:09:35 +03:00
|
|
|
do_env.completionArgtypes = ['tritonprofile', 'none'];
|
|
|
|
|
2015-11-25 02:40:17 +02:00
|
|
|
module.exports = do_env;
|