This commit is contained in:
Dave Eddy 2015-08-25 23:25:00 -04:00
parent 463be10e2d
commit a8d8d638ec
2 changed files with 71 additions and 0 deletions

View File

@ -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');

69
lib/do_ssh.js Normal file
View File

@ -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 <alias|id> [arguments]\n'
+ '\n'
+ '{{options}}'
);
do_ssh.interspersedOptions = false;
module.exports = do_ssh;