2014-02-07 23:21:24 +02:00
|
|
|
/*
|
2015-09-04 21:12:20 +03:00
|
|
|
* This Source Code Form is subject to the terms of the Mozilla Public
|
|
|
|
* License, v. 2.0. If a copy of the MPL was not distributed with this
|
|
|
|
* file, You can obtain one at http://mozilla.org/MPL/2.0/.
|
|
|
|
*/
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Copyright 2015 Joyent, Inc.
|
2014-02-07 23:21:24 +02:00
|
|
|
*
|
2015-08-25 22:14:16 +03:00
|
|
|
* The `triton` CLI class.
|
2014-02-07 23:21:24 +02:00
|
|
|
*/
|
|
|
|
|
2015-08-25 22:14:16 +03:00
|
|
|
var assert = require('assert-plus');
|
|
|
|
var bunyan = require('bunyan');
|
2014-02-07 23:21:24 +02:00
|
|
|
var child_process = require('child_process'),
|
|
|
|
spawn = child_process.spawn,
|
|
|
|
exec = child_process.exec;
|
|
|
|
var cmdln = require('cmdln'),
|
|
|
|
Cmdln = cmdln.Cmdln;
|
2015-08-25 22:14:16 +03:00
|
|
|
var fs = require('fs');
|
2015-09-04 01:12:08 +03:00
|
|
|
var mkdirp = require('mkdirp');
|
2015-08-25 22:14:16 +03:00
|
|
|
var util = require('util'),
|
|
|
|
format = util.format;
|
2015-08-26 19:59:12 +03:00
|
|
|
var path = require('path');
|
2015-08-25 22:14:16 +03:00
|
|
|
var vasync = require('vasync');
|
2014-02-07 23:21:24 +02:00
|
|
|
|
|
|
|
var common = require('./common');
|
2015-09-08 19:55:48 +03:00
|
|
|
var mod_config = require('./config');
|
2014-02-08 10:15:26 +02:00
|
|
|
var errors = require('./errors');
|
2015-09-04 21:04:45 +03:00
|
|
|
var TritonApi = require('./tritonapi');
|
2014-02-07 23:21:24 +02:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
//---- globals
|
|
|
|
|
|
|
|
var pkg = require('../package.json');
|
2015-09-08 19:55:48 +03:00
|
|
|
|
|
|
|
var CONFIG_DIR = path.resolve(process.env.HOME, '.triton');
|
2014-02-07 23:21:24 +02:00
|
|
|
|
2015-08-27 03:21:27 +03:00
|
|
|
var OPTIONS = [
|
|
|
|
{
|
|
|
|
names: ['help', 'h'],
|
|
|
|
type: 'bool',
|
|
|
|
help: 'Print this help and exit.'
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: 'version',
|
|
|
|
type: 'bool',
|
|
|
|
help: 'Print version and exit.'
|
|
|
|
},
|
|
|
|
{
|
|
|
|
names: ['verbose', 'v'],
|
|
|
|
type: 'bool',
|
|
|
|
help: 'Verbose/debug output.'
|
|
|
|
},
|
|
|
|
|
2015-09-08 19:55:48 +03:00
|
|
|
{
|
|
|
|
names: ['profile', 'p'],
|
|
|
|
type: 'string',
|
|
|
|
env: 'TRITON_PROFILE',
|
|
|
|
helpArg: 'NAME',
|
2015-09-10 02:14:34 +03:00
|
|
|
help: 'Triton client profile to use.',
|
|
|
|
hidden: true // TODO: hidden until profiles impl is complete
|
2015-09-08 19:55:48 +03:00
|
|
|
},
|
2015-08-27 03:21:27 +03:00
|
|
|
|
|
|
|
{
|
2015-09-21 22:34:37 +03:00
|
|
|
group: 'CloudAPI Options'
|
2015-08-27 03:21:27 +03:00
|
|
|
},
|
2015-09-21 22:34:37 +03:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Environment variable integration.
|
|
|
|
*
|
|
|
|
* While dashdash supports integrated envvar parsing with options
|
|
|
|
* we don't use that with `triton` because (a) we want to apply *option*
|
|
|
|
* usage (but not envvars) to profiles other than the default 'env'
|
|
|
|
* profile, and (b) we want to support `TRITON_*` *and* `SDC_*` envvars,
|
|
|
|
* which dashdash doesn't support.
|
|
|
|
*
|
|
|
|
* See <https://github.com/joyent/node-triton/issues/28> for some details.
|
|
|
|
*/
|
2015-08-27 03:21:27 +03:00
|
|
|
{
|
|
|
|
names: ['account', 'a'],
|
|
|
|
type: 'string',
|
2015-09-21 22:34:37 +03:00
|
|
|
help: 'Account (login name). Environment: TRITON_ACCOUNT=ACCOUNT ' +
|
|
|
|
'or SDC_ACCOUNT=ACCOUNT.',
|
2015-08-27 03:21:27 +03:00
|
|
|
helpArg: 'ACCOUNT'
|
|
|
|
},
|
2015-09-21 22:34:37 +03:00
|
|
|
// TODO: subuser/RBAC support
|
2015-08-27 03:21:27 +03:00
|
|
|
//{
|
|
|
|
// names: ['subuser', 'user'],
|
|
|
|
// type: 'string',
|
|
|
|
// env: 'MANTA_SUBUSER',
|
|
|
|
// help: 'Manta User (login name)',
|
|
|
|
// helpArg: 'USER'
|
|
|
|
//},
|
|
|
|
//{
|
|
|
|
// names: ['role'],
|
|
|
|
// type: 'arrayOfString',
|
|
|
|
// env: 'MANTA_ROLE',
|
|
|
|
// help: 'Assume a role. Use multiple times or once with a list',
|
|
|
|
// helpArg: 'ROLE,ROLE,...'
|
|
|
|
//},
|
|
|
|
{
|
|
|
|
names: ['keyId', 'k'],
|
|
|
|
type: 'string',
|
2015-09-21 22:34:37 +03:00
|
|
|
help: 'SSH key fingerprint. Environment: SDC_KEY_ID=FINGERPRINT.',
|
2015-08-27 03:21:27 +03:00
|
|
|
helpArg: 'FINGERPRINT'
|
|
|
|
},
|
|
|
|
{
|
|
|
|
names: ['url', 'u'],
|
|
|
|
type: 'string',
|
2015-09-21 22:34:37 +03:00
|
|
|
help: 'CloudAPI URL. Environment: TRITON_URL=URL or SDC_URL=URL.',
|
2015-08-27 03:21:27 +03:00
|
|
|
helpArg: 'URL'
|
|
|
|
},
|
|
|
|
{
|
|
|
|
names: ['J'],
|
|
|
|
type: 'string',
|
|
|
|
hidden: true,
|
|
|
|
help: 'Joyent Public Cloud (JPC) datacenter name. This is ' +
|
|
|
|
'a shortcut to the "https://$dc.api.joyent.com" ' +
|
|
|
|
'cloudapi URL.'
|
|
|
|
},
|
|
|
|
{
|
|
|
|
names: ['insecure', 'i'],
|
|
|
|
type: 'bool',
|
2015-09-21 22:34:37 +03:00
|
|
|
help: 'Do not validate the CloudAPI SSL certificate. Environment: ' +
|
|
|
|
'TRITON_TLS_INSECURE=1, SDC_TLS_INSECURE=1 (or the deprecated ' +
|
|
|
|
'SDC_TESTING=1).',
|
2015-09-21 22:37:59 +03:00
|
|
|
'default': false
|
2015-08-27 03:21:27 +03:00
|
|
|
}
|
|
|
|
];
|
2014-02-07 23:21:24 +02:00
|
|
|
|
|
|
|
|
|
|
|
//---- CLI class
|
|
|
|
|
|
|
|
function CLI() {
|
|
|
|
Cmdln.call(this, {
|
2015-09-08 19:55:48 +03:00
|
|
|
name: 'triton',
|
2014-02-07 23:21:24 +02:00
|
|
|
desc: pkg.description,
|
2015-08-27 03:21:27 +03:00
|
|
|
options: OPTIONS,
|
2014-02-07 23:21:24 +02:00
|
|
|
helpOpts: {
|
|
|
|
includeEnv: true,
|
2015-08-26 07:34:47 +03:00
|
|
|
minHelpCol: 30
|
2015-08-26 01:10:13 +03:00
|
|
|
},
|
|
|
|
helpSubcmds: [
|
|
|
|
'help',
|
2015-09-08 19:55:48 +03:00
|
|
|
// TODO: hide until the command is fully implemented:
|
|
|
|
// 'profiles',
|
2015-08-31 21:14:07 +03:00
|
|
|
{ group: 'Other Commands' },
|
|
|
|
'info',
|
2015-08-26 06:44:08 +03:00
|
|
|
'account',
|
2015-08-31 21:14:07 +03:00
|
|
|
'keys',
|
2015-08-27 02:56:18 +03:00
|
|
|
'services',
|
2015-08-27 02:59:28 +03:00
|
|
|
'datacenters',
|
2015-08-26 01:15:02 +03:00
|
|
|
{ group: 'Instances (aka VMs/Machines/Containers)' },
|
2015-08-26 06:53:48 +03:00
|
|
|
'create-instance',
|
2015-08-26 01:15:02 +03:00
|
|
|
'instances',
|
2015-08-26 03:27:46 +03:00
|
|
|
'instance',
|
2015-08-26 01:15:02 +03:00
|
|
|
'instance-audit',
|
2015-08-26 04:09:32 +03:00
|
|
|
'start-instance',
|
|
|
|
'stop-instance',
|
|
|
|
'reboot-instance',
|
2015-08-26 08:25:26 +03:00
|
|
|
'delete-instance',
|
2015-08-26 22:16:01 +03:00
|
|
|
'wait-instance',
|
2015-08-26 06:25:00 +03:00
|
|
|
'ssh',
|
2015-08-26 01:10:13 +03:00
|
|
|
{ group: 'Images' },
|
|
|
|
'images',
|
2015-08-26 01:47:29 +03:00
|
|
|
'image',
|
2015-08-26 02:12:35 +03:00
|
|
|
{ group: 'Packages' },
|
|
|
|
'packages',
|
2015-08-26 23:40:50 +03:00
|
|
|
'package',
|
|
|
|
{ group: 'Networks' },
|
2015-08-27 00:09:50 +03:00
|
|
|
'networks',
|
|
|
|
'network'
|
2015-08-26 01:10:13 +03:00
|
|
|
]
|
2014-02-07 23:21:24 +02:00
|
|
|
});
|
|
|
|
}
|
|
|
|
util.inherits(CLI, Cmdln);
|
|
|
|
|
|
|
|
CLI.prototype.init = function (opts, args, callback) {
|
|
|
|
var self = this;
|
2015-09-21 22:34:37 +03:00
|
|
|
this.opts = opts;
|
2014-02-07 23:21:24 +02:00
|
|
|
|
|
|
|
if (opts.version) {
|
2015-09-08 19:55:48 +03:00
|
|
|
console.log(this.name, pkg.version);
|
2014-02-07 23:21:24 +02:00
|
|
|
callback(false);
|
|
|
|
return;
|
|
|
|
}
|
2015-09-08 19:55:48 +03:00
|
|
|
|
|
|
|
this.log = bunyan.createLogger({
|
|
|
|
name: this.name,
|
|
|
|
serializers: bunyan.stdSerializers,
|
|
|
|
stream: process.stderr,
|
|
|
|
level: 'warn'
|
|
|
|
});
|
2014-02-07 23:21:24 +02:00
|
|
|
if (opts.verbose) {
|
2015-09-08 19:55:48 +03:00
|
|
|
this.log.level('trace');
|
|
|
|
this.log.src = true;
|
2015-08-27 03:21:27 +03:00
|
|
|
this.showErrStack = true;
|
2014-02-07 23:21:24 +02:00
|
|
|
}
|
|
|
|
|
2015-09-21 22:34:37 +03:00
|
|
|
if (opts.url && opts.J) {
|
|
|
|
callback(new errors.UsageError(
|
|
|
|
'cannot use both "--url" and "-J" options'));
|
|
|
|
} else if (opts.J) {
|
2015-09-08 19:55:48 +03:00
|
|
|
opts.url = format('https://%s.api.joyent.com', opts.J);
|
|
|
|
}
|
2015-09-21 22:34:37 +03:00
|
|
|
|
2015-09-08 19:55:48 +03:00
|
|
|
this.configDir = CONFIG_DIR;
|
2015-08-26 19:59:12 +03:00
|
|
|
|
2015-09-08 19:55:48 +03:00
|
|
|
this.__defineGetter__('tritonapi', function () {
|
|
|
|
if (self._triton === undefined) {
|
|
|
|
var config = mod_config.loadConfig({
|
|
|
|
configDir: self.configDir
|
|
|
|
});
|
|
|
|
self.log.trace({config: config}, 'loaded config');
|
2015-09-21 22:34:37 +03:00
|
|
|
|
2015-09-08 19:55:48 +03:00
|
|
|
var profileName = opts.profile || config.profile || 'env';
|
2015-09-21 22:34:37 +03:00
|
|
|
var profile = mod_config.loadProfile({
|
|
|
|
configDir: self.configDir,
|
|
|
|
name: profileName
|
|
|
|
});
|
|
|
|
self._applyProfileOverrides(profile);
|
2015-09-08 19:55:48 +03:00
|
|
|
self.log.trace({profile: profile}, 'loaded profile');
|
2015-08-27 03:21:27 +03:00
|
|
|
|
2015-09-04 21:04:45 +03:00
|
|
|
self._tritonapi = new TritonApi({
|
2015-09-08 19:55:48 +03:00
|
|
|
log: self.log,
|
|
|
|
profile: profile,
|
|
|
|
config: config
|
2015-08-26 19:59:12 +03:00
|
|
|
});
|
2015-08-25 23:11:40 +03:00
|
|
|
}
|
2015-09-04 21:04:45 +03:00
|
|
|
return self._tritonapi;
|
2015-08-25 23:11:40 +03:00
|
|
|
});
|
2014-02-07 23:21:24 +02:00
|
|
|
|
|
|
|
// Cmdln class handles `opts.help`.
|
|
|
|
Cmdln.prototype.init.apply(this, arguments);
|
|
|
|
};
|
|
|
|
|
2015-08-25 23:11:40 +03:00
|
|
|
|
2015-09-21 22:34:37 +03:00
|
|
|
/*
|
|
|
|
* Apply overrides from CLI options to the given profile object *in place*.
|
|
|
|
*/
|
|
|
|
CLI.prototype._applyProfileOverrides =
|
|
|
|
function _applyProfileOverrides(profile) {
|
|
|
|
var self = this;
|
|
|
|
['account', 'url', 'keyId', 'insecure'].forEach(function (field) {
|
2015-09-21 23:57:10 +03:00
|
|
|
// We need to check `opts._order` to know if boolean opts
|
|
|
|
// were specified.
|
|
|
|
var specified = self.opts._order.filter(
|
|
|
|
function (opt) { return opt.key === field; }).length > 0;
|
|
|
|
if (specified) {
|
2015-09-21 22:34:37 +03:00
|
|
|
profile[field] = self.opts[field];
|
|
|
|
}
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
2015-08-25 23:11:40 +03:00
|
|
|
|
2015-09-04 10:09:19 +03:00
|
|
|
// Meta
|
|
|
|
CLI.prototype.do_completion = require('./do_completion');
|
2015-09-08 19:55:48 +03:00
|
|
|
CLI.prototype.do_profiles = require('./do_profiles');
|
2014-02-07 23:21:24 +02:00
|
|
|
|
2015-09-04 10:09:19 +03:00
|
|
|
// Other
|
2015-08-26 06:44:08 +03:00
|
|
|
CLI.prototype.do_account = require('./do_account');
|
2015-08-27 02:56:18 +03:00
|
|
|
CLI.prototype.do_services = require('./do_services');
|
2015-08-27 02:59:28 +03:00
|
|
|
CLI.prototype.do_datacenters = require('./do_datacenters');
|
2015-08-26 07:16:41 +03:00
|
|
|
CLI.prototype.do_info = require('./do_info');
|
2015-08-26 07:40:32 +03:00
|
|
|
CLI.prototype.do_keys = require('./do_keys');
|
2015-08-26 06:44:08 +03:00
|
|
|
|
2015-08-26 01:10:13 +03:00
|
|
|
// Images
|
2015-08-26 00:25:30 +03:00
|
|
|
CLI.prototype.do_images = require('./do_images');
|
2015-08-26 01:47:29 +03:00
|
|
|
CLI.prototype.do_image = require('./do_image');
|
2015-08-26 00:25:30 +03:00
|
|
|
|
2015-08-26 01:15:02 +03:00
|
|
|
// Instances (aka VMs/containers/machines)
|
2015-08-26 03:27:46 +03:00
|
|
|
CLI.prototype.do_instance = require('./do_instance');
|
2015-08-26 01:15:02 +03:00
|
|
|
CLI.prototype.do_instances = require('./do_instances');
|
2015-08-26 06:53:48 +03:00
|
|
|
CLI.prototype.do_create_instance = require('./do_create_instance');
|
2015-08-26 01:15:02 +03:00
|
|
|
CLI.prototype.do_instance_audit = require('./do_instance_audit');
|
2015-08-26 04:46:14 +03:00
|
|
|
CLI.prototype.do_stop_instance = require('./do_startstop_instance')('stop');
|
|
|
|
CLI.prototype.do_start_instance = require('./do_startstop_instance')('start');
|
|
|
|
CLI.prototype.do_reboot_instance = require('./do_startstop_instance')('reboot');
|
2015-08-26 19:18:40 +03:00
|
|
|
CLI.prototype.do_delete_instance = require('./do_startstop_instance')('delete');
|
2015-08-26 22:16:01 +03:00
|
|
|
CLI.prototype.do_wait_instance = require('./do_wait_instance');
|
2015-08-26 06:25:00 +03:00
|
|
|
CLI.prototype.do_ssh = require('./do_ssh');
|
2014-02-07 23:21:24 +02:00
|
|
|
|
2015-08-26 01:30:25 +03:00
|
|
|
// Packages
|
|
|
|
CLI.prototype.do_packages = require('./do_packages');
|
2015-08-26 02:12:35 +03:00
|
|
|
CLI.prototype.do_package = require('./do_package');
|
2015-08-26 01:30:25 +03:00
|
|
|
|
2015-08-26 23:40:50 +03:00
|
|
|
// Networks
|
|
|
|
CLI.prototype.do_networks = require('./do_networks');
|
2015-08-27 00:09:50 +03:00
|
|
|
CLI.prototype.do_network = require('./do_network');
|
2015-08-26 23:40:50 +03:00
|
|
|
|
2015-08-26 02:12:35 +03:00
|
|
|
// Hidden commands
|
2015-08-26 01:30:25 +03:00
|
|
|
CLI.prototype.do_cloudapi = require('./do_cloudapi');
|
2015-08-26 02:12:35 +03:00
|
|
|
CLI.prototype.do_badger = require('./do_badger');
|
2014-02-07 23:21:24 +02:00
|
|
|
|
|
|
|
|
2015-07-26 08:45:20 +03:00
|
|
|
|
2015-08-25 22:14:16 +03:00
|
|
|
//---- mainline
|
|
|
|
|
2015-09-02 11:04:20 +03:00
|
|
|
function main(argv) {
|
|
|
|
if (!argv) {
|
|
|
|
argv = process.argv;
|
|
|
|
}
|
|
|
|
|
2015-08-25 22:14:16 +03:00
|
|
|
var cli = new CLI();
|
2015-09-02 11:04:20 +03:00
|
|
|
cli.main(argv, function (err, subcmd) {
|
|
|
|
var exitStatus = (err ? err.exitStatus || 1 : 0);
|
|
|
|
var showErr = (cli.showErr !== undefined ? cli.showErr : true);
|
|
|
|
|
|
|
|
if (err && showErr) {
|
2015-09-24 07:08:26 +03:00
|
|
|
var code = (err.body ? err.body.code : err.code) || err.restCode;
|
2015-09-02 11:04:20 +03:00
|
|
|
if (code === 'NoCommand') {
|
|
|
|
/* jsl:pass */
|
|
|
|
} else if (err.message !== undefined) {
|
|
|
|
console.error('%s%s: error%s: %s',
|
|
|
|
cli.name,
|
|
|
|
(subcmd ? ' ' + subcmd : ''),
|
|
|
|
(code ? format(' (%s)', code) : ''),
|
|
|
|
(cli.showErrStack ? err.stack : err.message));
|
|
|
|
|
|
|
|
// If this is a usage error, attempt to show some usage info.
|
|
|
|
if (['Usage', 'Option'].indexOf(code) !== -1 && subcmd) {
|
|
|
|
var help = cli.helpFromSubcmd(subcmd);
|
|
|
|
if (help) {
|
|
|
|
// Would like a shorter synopsis. Attempt to
|
|
|
|
// parse it down, somewhat generally.
|
|
|
|
var usageIdx = help.indexOf('\nUsage:');
|
|
|
|
if (usageIdx !== -1) {
|
|
|
|
help = help.slice(usageIdx);
|
|
|
|
}
|
|
|
|
console.error(help);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
process.exit(exitStatus);
|
|
|
|
});
|
2015-08-25 22:14:16 +03:00
|
|
|
}
|
2014-02-07 23:21:24 +02:00
|
|
|
|
|
|
|
//---- exports
|
|
|
|
|
2015-09-02 11:04:20 +03:00
|
|
|
module.exports = {
|
2015-09-08 19:55:48 +03:00
|
|
|
CONFIG_DIR: CONFIG_DIR,
|
2015-09-02 11:04:20 +03:00
|
|
|
CLI: CLI,
|
|
|
|
main: main
|
|
|
|
};
|