diff --git a/CHANGES.md b/CHANGES.md index 4528e99..d0b44f6 100644 --- a/CHANGES.md +++ b/CHANGES.md @@ -7,6 +7,7 @@ Known issues: ## 4.7.1 (not yet released) +- #103 `triton ip ` to output the instance's primaryIp - #52 Workaround for `triton ssh ...`. In version 4.6.0, `triton ssh ...` interactive sessions were broken. This version reverts that change and adds a workaround for #52 (by disabling ControlMaster when spawning `ssh`). diff --git a/lib/cli.js b/lib/cli.js index 3e8800c..91b2f17 100644 --- a/lib/cli.js +++ b/lib/cli.js @@ -218,6 +218,7 @@ function CLI() { 'stop', 'reboot', 'ssh', + 'ip', { group: 'Images, Packages, Networks, Firewall Rules' }, 'image', 'package', @@ -569,6 +570,7 @@ CLI.prototype.do_start = require('./do_start'); CLI.prototype.do_stop = require('./do_stop'); CLI.prototype.do_reboot = require('./do_reboot'); CLI.prototype.do_ssh = require('./do_ssh'); +CLI.prototype.do_ip = require('./do_ip'); // Packages CLI.prototype.do_packages = require('./do_packages'); diff --git a/lib/do_instance/do_ip.js b/lib/do_instance/do_ip.js new file mode 100644 index 0000000..ff6ee39 --- /dev/null +++ b/lib/do_instance/do_ip.js @@ -0,0 +1,71 @@ +/* + * 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 2016 Joyent, Inc. + * + * `triton instance ip ...` + */ + +var format = require('util').format; + +var errors = require('../errors'); + + +function do_ip(subcmd, opts, args, cb) { + if (opts.help) { + this.do_help('help', {}, [subcmd], cb); + return; + } else if (args.length === 0) { + cb(new errors.UsageError('missing argument')); + return; + } else if (args.length > 1) { + cb(new errors.UsageError('too many arguments: ' + args)); + return; + } + + var cli = this.top; + + cli.tritonapi.getInstance(args[0], function (err, inst) { + if (err) { + cb(err); + return; + } + + if (!inst.primaryIp) { + cb(new errors.TritonError(format( + 'primaryIp not found for instance "%s"', args[0]))); + return; + } + + console.log(inst.primaryIp); + cb(); + }); +} + +do_ip.options = [ + { + names: ['help', 'h'], + type: 'bool', + help: 'Show this help.' + } +]; + +do_ip.help = [ + /* BEGIN JSSTYLED */ + 'Print the primaryIp of the given instance.', + '', + 'Usage:', + ' {{name}} ip ', + '', + '{{options}}', + 'For example: ssh root@$(triton ip my-instance)' +].join('\n'); + + +do_ip.completionArgtypes = ['tritoninstance', 'none']; + +module.exports = do_ip; diff --git a/lib/do_instance/index.js b/lib/do_instance/index.js index 199f566..9b0abbe 100644 --- a/lib/do_instance/index.js +++ b/lib/do_instance/index.js @@ -44,6 +44,7 @@ function InstanceCLI(top) { 'disable-firewall', { group: '' }, 'ssh', + 'ip', 'wait', 'audit', 'snapshot', @@ -72,6 +73,7 @@ InstanceCLI.prototype.do_enable_firewall = require('./do_enable_firewall'); InstanceCLI.prototype.do_disable_firewall = require('./do_disable_firewall'); InstanceCLI.prototype.do_ssh = require('./do_ssh'); +InstanceCLI.prototype.do_ip = require('./do_ip'); InstanceCLI.prototype.do_wait = require('./do_wait'); InstanceCLI.prototype.do_audit = require('./do_audit'); InstanceCLI.prototype.do_snapshot = require('./do_snapshot'); diff --git a/lib/do_ip.js b/lib/do_ip.js new file mode 100644 index 0000000..b974ac0 --- /dev/null +++ b/lib/do_ip.js @@ -0,0 +1,27 @@ +/* + * 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 2016 Joyent, Inc. + * + * `triton ip ...` shortcut for `triton instance ip ...`. + */ + +var targ = require('./do_instance/do_ip'); + +function do_ip(subcmd, opts, args, callback) { + this.handlerFromSubcmd('instance').dispatch({ + subcmd: 'ip', + opts: opts, + args: args + }, callback); +} + +do_ip.help = 'A shortcut for "triton instance ip".'; +do_ip.options = targ.options; +do_ip.completionArgtypes = targ.completionArgtypes; + +module.exports = do_ip; diff --git a/test/integration/cli-subcommands.test.js b/test/integration/cli-subcommands.test.js index f9aab5f..1086341 100644 --- a/test/integration/cli-subcommands.test.js +++ b/test/integration/cli-subcommands.test.js @@ -43,6 +43,8 @@ var subs = [ ['instance delete', 'instance rm', 'delete', 'rm'], ['instance enable-firewall'], ['instance disable-firewall'], + ['instance ssh'], + ['instance ip'], ['instance wait'], ['instance audit'], ['instance fwrules'], @@ -51,6 +53,7 @@ var subs = [ ['instance snapshot list', 'instance snapshot ls', 'instance snapshots'], ['instance snapshot get'], ['instance snapshot delete', 'instance snapshot rm'], + ['ip'], ['ssh'], ['network'], ['network list', 'networks'],