From a8d8d638ecd3a9228b129a82f60746bc37f993eb Mon Sep 17 00:00:00 2001 From: Dave Eddy Date: Tue, 25 Aug 2015 23:25:00 -0400 Subject: [PATCH] do_ssh --- lib/cli.js | 2 ++ lib/do_ssh.js | 69 +++++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 71 insertions(+) create mode 100644 lib/do_ssh.js diff --git a/lib/cli.js b/lib/cli.js index 33e0250..977c15a 100644 --- a/lib/cli.js +++ b/lib/cli.js @@ -66,6 +66,7 @@ function CLI() { 'start-instance', 'stop-instance', 'reboot-instance', + 'ssh', { group: 'Images' }, 'images', 'image', @@ -118,6 +119,7 @@ CLI.prototype.do_instance_audit = require('./do_instance_audit'); 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'); +CLI.prototype.do_ssh = require('./do_ssh'); // Packages CLI.prototype.do_packages = require('./do_packages'); diff --git a/lib/do_ssh.js b/lib/do_ssh.js new file mode 100644 index 0000000..62cd623 --- /dev/null +++ b/lib/do_ssh.js @@ -0,0 +1,69 @@ +/* + * Copyright 2015 Joyent Inc. + * + * `triton ssh ...` + */ + +var common = require('./common'); +var spawn = require('child_process').spawn; + +function do_ssh(subcmd, opts, args, callback) { + var self = this; + + if (opts.help) { + this.do_help('help', {}, [subcmd], callback); + return; + } else if (args.length === 0) { + callback(new Error('invalid args: ' + args)); + return; + } + + var id = args.shift(); + + if (common.isUUID(id)) { + this.triton.cloudapi.getMachine(id, cb); + } else { + this.triton.getMachineByAlias(id, cb); + } + + function cb(err, machine) { + if (err) { + callback(err); + return; + } + + var ip = machine.primaryIp; + if (!ip) { + callback(new Error('primaryIp not found for machine')); + return; + } + + args = ['-l', 'root'].concat(ip).concat(args); + + self.triton.log.info({args: args}, 'forking ssh'); + var child = spawn('ssh', args, {stdio: 'inherit'}); + child.on('close', function (code) { + process.exit(code); + }); + } +} + +do_ssh.options = [ + { + names: ['help', 'h'], + type: 'bool', + help: 'Show this help.' + } +]; +do_ssh.help = ( + 'SSH to the primary IP of an instance\n' + + '\n' + + 'Usage:\n' + + ' {{name}} ssh [arguments]\n' + + '\n' + + '{{options}}' +); + +do_ssh.interspersedOptions = false; + +module.exports = do_ssh;