Skip to content

Commit

Permalink
#18 first cut of 'triton profiles'
Browse files Browse the repository at this point in the history
  • Loading branch information
trentm committed Sep 9, 2015
1 parent 3699dd3 commit d2e9999
Show file tree
Hide file tree
Showing 10 changed files with 622 additions and 266 deletions.
13 changes: 13 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -212,6 +212,18 @@ as a single `DOCKER_HOST`. See the [Triton Docker
documentation](https://apidocs.joyent.com/docker) for more information.)


## Configuration

This section defines all the vars in a TritonApi config. The baked in defaults
are in "etc/defaults.json" and can be overriden for the CLI in
"~/.triton/config.json".

| Name | Description |
| ---- | ----------- |
| profile | The name of the triton profile to use. The default with the CLI is "env", i.e. take config from `SDC_*` envvars. |
| cacheDir | The path (relative to the config dir, "~/.triton") where cache data is stored. The default is "cache", i.e. the `triton` CLI caches at "~/.triton/cache". |


## node-triton differences with node-smartdc

- There is a single `triton` command instead of a number of `sdc-*` commands.
Expand Down Expand Up @@ -251,3 +263,4 @@ clone via:
## License

MPL 2.0

22 changes: 0 additions & 22 deletions TODO.txt
Original file line number Diff line number Diff line change
Expand Up @@ -24,28 +24,6 @@ triton images
for 'linux' ones. That might hit that stupid naming problem.


# profiles

triton profile # list all profiles
triton profile NAME # show NAME profile
triton profile -a NAME # sets it as active
triton profile -n|--new # ???

For today: only the implicit 'env' profile.



# config

~/.triton/
config.json
{"currProfile": "east3b"}
east3b/ # profile
PROFILE2/
...



# another day

triton config get|set|list # see 'npm config'
Expand Down
2 changes: 1 addition & 1 deletion etc/defaults.json
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
{
"defaultProfile": "env"
"cacheDir": "cache"
}
106 changes: 49 additions & 57 deletions lib/cli.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,23 +25,17 @@ var path = require('path');
var vasync = require('vasync');

var common = require('./common');
var mod_config = require('./config');
var errors = require('./errors');
var TritonApi = require('./tritonapi');



//---- globals

var p = console.log;

var pkg = require('../package.json');
var name = 'triton';
var log = bunyan.createLogger({
name: name,
serializers: bunyan.stdSerializers,
stream: process.stderr,
level: 'warn'
});

var CONFIG_DIR = path.resolve(process.env.HOME, '.triton');

var OPTIONS = [
{
Expand All @@ -60,9 +54,13 @@ var OPTIONS = [
help: 'Verbose/debug output.'
},

// XXX disable profile selection for now
//{names: ['profile', 'p'], type: 'string', env: 'TRITON_PROFILE',
// helpArg: 'NAME', help: 'TritonApi client profile to use.'}
{
names: ['profile', 'p'],
type: 'string',
env: 'TRITON_PROFILE',
helpArg: 'NAME',
help: 'Triton client profile to use.'
},

{
group: 'CloudApi Options'
Expand Down Expand Up @@ -127,7 +125,7 @@ var OPTIONS = [

function CLI() {
Cmdln.call(this, {
name: pkg.name,
name: 'triton',
desc: pkg.description,
options: OPTIONS,
helpOpts: {
Expand All @@ -136,6 +134,8 @@ function CLI() {
},
helpSubcmds: [
'help',
// TODO: hide until the command is fully implemented:
// 'profiles',
{ group: 'Other Commands' },
'info',
'account',
Expand Down Expand Up @@ -171,59 +171,52 @@ CLI.prototype.init = function (opts, args, callback) {
var self = this;

if (opts.version) {
p(this.name, pkg.version);
console.log(this.name, pkg.version);
callback(false);
return;
}
this.opts = opts;

this.log = bunyan.createLogger({
name: this.name,
serializers: bunyan.stdSerializers,
stream: process.stderr,
level: 'warn'
});
if (opts.verbose) {
log.level('trace');
log.src = true;
this.log.level('trace');
this.log.src = true;
this.showErrStack = true;
}

this.__defineGetter__('tritonapi', function () {
if (self._tritonapi === undefined) {
var userConfigPath = require('./config').DEFAULT_USER_CONFIG_PATH;
var dir = path.dirname(userConfigPath);
var cacheDir = path.join(dir, 'cache');

if (!fs.existsSync(cacheDir)) {
try {
mkdirp.sync(cacheDir);
} catch (e) {
log.info({err: e}, 'failed to make dir %s', cacheDir);
}
}
if (!opts.url && opts.J) {
opts.url = format('https://%s.api.joyent.com', opts.J);
}
this.envProfile = mod_config.loadEnvProfile(opts);
this.configDir = CONFIG_DIR;

// XXX support keyId being a priv or pub key path, a la imgapi-cli
// XXX Add TRITON_* envvars.
var envProfile = {
name: 'env',
account: opts.account,
url: opts.url,
keyId: opts.keyId,
insecure: opts.insecure
};
// If --insecure not given, look at envvar(s) for that.
var specifiedInsecureOpt = opts._order.filter(
function (opt) { return opt.key === 'insecure'; }).length > 0;
if (!specifiedInsecureOpt && process.env.SDC_TESTING) {
envProfile.insecure = common.boolFromString(
process.env.SDC_TESTING,
false, '"SDC_TESTING" envvar');
}
if (opts.J) {
envProfile.url = format('https://%s.api.joyent.com', opts.J);
this.__defineGetter__('tritonapi', function () {
if (self._triton === undefined) {
var config = mod_config.loadConfig({
configDir: self.configDir
});
self.log.trace({config: config}, 'loaded config');
var profileName = opts.profile || config.profile || 'env';
var profile;
if (profileName === 'env') {
profile = self.envProfile;
} else {
profile = mod_config.loadProfile({
configDir: self.configDir,
name: profileName
});
}
log.trace({envProfile: envProfile}, 'envProfile');
self.log.trace({profile: profile}, 'loaded profile');

self._tritonapi = new TritonApi({
log: log,
profileName: opts.profile,
envProfile: envProfile,
configPath: userConfigPath,
cacheDir: cacheDir
log: self.log,
profile: profile,
config: config
});
}
return self._tritonapi;
Expand All @@ -237,7 +230,7 @@ CLI.prototype.init = function (opts, args, callback) {

// Meta
CLI.prototype.do_completion = require('./do_completion');
//CLI.prototype.do_profile = require('./do_profile');
CLI.prototype.do_profiles = require('./do_profiles');

// Other
CLI.prototype.do_account = require('./do_account');
Expand Down Expand Up @@ -276,8 +269,6 @@ CLI.prototype.do_badger = require('./do_badger');





//---- mainline

function main(argv) {
Expand Down Expand Up @@ -324,6 +315,7 @@ function main(argv) {
//---- exports

module.exports = {
CONFIG_DIR: CONFIG_DIR,
CLI: CLI,
main: main
};
Loading

0 comments on commit d2e9999

Please sign in to comment.