2016-01-04 23:08:16 +02: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 2016 Joyent, Inc.
|
|
|
|
*
|
|
|
|
* `triton instance ssh ...`
|
|
|
|
*/
|
|
|
|
|
2016-03-12 00:58:27 +02:00
|
|
|
var path = require('path');
|
2016-01-04 23:08:16 +02:00
|
|
|
var spawn = require('child_process').spawn;
|
2016-12-22 23:27:13 +02:00
|
|
|
var vasync = require('vasync');
|
2016-01-04 23:08:16 +02:00
|
|
|
|
|
|
|
var common = require('../common');
|
2016-06-09 00:13:16 +03:00
|
|
|
var errors = require('../errors');
|
2016-01-04 23:08:16 +02:00
|
|
|
|
|
|
|
|
|
|
|
function do_ssh(subcmd, opts, args, callback) {
|
|
|
|
if (opts.help) {
|
|
|
|
this.do_help('help', {}, [subcmd], callback);
|
|
|
|
return;
|
|
|
|
} else if (args.length === 0) {
|
2016-06-09 00:13:16 +03:00
|
|
|
callback(new errors.UsageError('missing INST arg'));
|
2016-01-04 23:08:16 +02:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
var id = args.shift();
|
|
|
|
|
2016-12-22 23:27:13 +02:00
|
|
|
var user;
|
2016-01-04 23:08:16 +02:00
|
|
|
var i = id.indexOf('@');
|
|
|
|
if (i >= 0) {
|
|
|
|
user = id.substr(0, i);
|
|
|
|
id = id.substr(i + 1);
|
|
|
|
}
|
|
|
|
|
2016-12-22 23:27:13 +02:00
|
|
|
vasync.pipeline({arg: {cli: this.top}, funcs: [
|
|
|
|
common.cliSetupTritonApi,
|
|
|
|
|
|
|
|
function getInstanceIp(ctx, next) {
|
|
|
|
ctx.cli.tritonapi.getInstance(id, function (err, inst) {
|
|
|
|
if (err) {
|
|
|
|
next(err);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
ctx.inst = inst;
|
|
|
|
|
|
|
|
ctx.ip = inst.primaryIp;
|
|
|
|
if (!ctx.ip) {
|
|
|
|
next(new Error('primaryIp not found for instance'));
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
next();
|
|
|
|
});
|
|
|
|
},
|
2016-01-04 23:08:16 +02:00
|
|
|
|
2016-12-22 23:27:13 +02:00
|
|
|
function getUser(ctx, next) {
|
|
|
|
if (user) {
|
|
|
|
next();
|
2016-12-13 20:04:41 +02:00
|
|
|
return;
|
|
|
|
}
|
2016-01-04 23:08:16 +02:00
|
|
|
|
2016-12-22 23:27:13 +02:00
|
|
|
ctx.cli.tritonapi.getImage({
|
|
|
|
name: ctx.inst.image,
|
|
|
|
useCache: true
|
|
|
|
}, function (getImageErr, image) {
|
|
|
|
if (getImageErr) {
|
|
|
|
next(getImageErr);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* This is a convention as seen on Joyent's
|
|
|
|
* "ubuntu-certified" KVM images.
|
|
|
|
*/
|
|
|
|
if (image.tags.default_user) {
|
|
|
|
user = image.tags.default_user;
|
|
|
|
} else {
|
|
|
|
user = 'root';
|
|
|
|
}
|
|
|
|
|
|
|
|
next();
|
|
|
|
});
|
|
|
|
},
|
|
|
|
|
|
|
|
function doSsh(ctx, next) {
|
|
|
|
args = ['-l', user, ctx.ip].concat(args);
|
2016-03-12 00:58:27 +02:00
|
|
|
|
|
|
|
/*
|
2016-12-22 23:27:13 +02:00
|
|
|
* By default we disable ControlMaster (aka mux, aka SSH
|
|
|
|
* connection multiplexing) because of
|
2016-12-13 20:04:41 +02:00
|
|
|
* https://github.com/joyent/node-triton/issues/52
|
2016-03-12 00:58:27 +02:00
|
|
|
*/
|
2016-12-13 20:04:41 +02:00
|
|
|
if (!opts.no_disable_mux) {
|
|
|
|
/*
|
|
|
|
* A simple `-o ControlMaster=no` doesn't work. With
|
|
|
|
* just that option, a `ControlPath` option (from
|
|
|
|
* ~/.ssh/config) will still be used if it exists. Our
|
|
|
|
* hack is to set a ControlPath we know should not
|
|
|
|
* exist. Using '/dev/null' wasn't a good alternative
|
|
|
|
* because `ssh` tries "$ControlPath.$somerandomnum"
|
|
|
|
* and also because Windows.
|
|
|
|
*/
|
|
|
|
var nullSshControlPath = path.resolve(
|
2016-12-22 23:27:13 +02:00
|
|
|
ctx.cli.tritonapi.config._configDir, 'tmp',
|
2016-12-13 20:04:41 +02:00
|
|
|
'nullSshControlPath');
|
|
|
|
args = [
|
|
|
|
'-o', 'ControlMaster=no',
|
|
|
|
'-o', 'ControlPath='+nullSshControlPath
|
|
|
|
].concat(args);
|
|
|
|
}
|
2016-01-04 23:08:16 +02:00
|
|
|
|
2016-12-22 23:27:13 +02:00
|
|
|
ctx.cli.log.info({args: args}, 'forking ssh');
|
2016-12-13 20:04:41 +02:00
|
|
|
var child = spawn('ssh', args, {stdio: 'inherit'});
|
|
|
|
child.on('close', function (code) {
|
|
|
|
/*
|
|
|
|
* Once node 0.10 support is dropped we could instead:
|
|
|
|
* process.exitCode = code;
|
|
|
|
* callback();
|
|
|
|
*/
|
|
|
|
process.exit(code);
|
2016-12-22 23:27:13 +02:00
|
|
|
/* process.exit does not return so no need to call next(). */
|
2016-12-13 20:04:41 +02:00
|
|
|
});
|
2016-12-22 23:27:13 +02:00
|
|
|
}
|
|
|
|
]}, callback);
|
2016-01-04 23:08:16 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
do_ssh.options = [
|
|
|
|
{
|
|
|
|
names: ['help', 'h'],
|
|
|
|
type: 'bool',
|
|
|
|
help: 'Show this help.'
|
|
|
|
}
|
|
|
|
];
|
2016-12-22 23:27:13 +02:00
|
|
|
do_ssh.synopses = ['{{name}} ssh [-h] [USER@]INST [SSH-ARGUMENTS]'];
|
2016-03-12 00:58:27 +02:00
|
|
|
do_ssh.help = [
|
|
|
|
/* BEGIN JSSTYLED */
|
|
|
|
'SSH to the primary IP of an instance',
|
|
|
|
'',
|
2016-06-09 00:13:16 +03:00
|
|
|
'{{usage}}',
|
2016-03-12 00:58:27 +02:00
|
|
|
'',
|
|
|
|
'{{options}}',
|
2016-06-09 00:13:16 +03:00
|
|
|
'Where INST is the name, id, or short id of an instance. Note that',
|
|
|
|
'the INST argument must come before any `ssh` options or arguments.',
|
2016-12-22 23:27:13 +02:00
|
|
|
'Where USER is the username to use on the instance. If not specified,',
|
|
|
|
'the instance\'s image is inspected for the default_user tag.',
|
|
|
|
'If USER is not specified and the default_user tag is not set, the user',
|
|
|
|
'is assumed to be \"root\".',
|
2016-03-12 00:58:27 +02:00
|
|
|
'',
|
|
|
|
'There is a known issue with SSH connection multiplexing (a.k.a. ',
|
|
|
|
'ControlMaster, mux) where stdout/stderr is lost. As a workaround, `ssh`',
|
2016-03-12 01:24:33 +02:00
|
|
|
'is spawned with options disabling ControlMaster. See ',
|
|
|
|
'<https://github.com/joyent/node-triton/issues/52> for details. If you ',
|
|
|
|
'want to use ControlMaster, an alternative is:',
|
2016-06-09 00:13:16 +03:00
|
|
|
' ssh root@$(triton ip INST)'
|
2016-03-12 00:58:27 +02:00
|
|
|
/* END JSSTYLED */
|
|
|
|
].join('\n');
|
2016-01-04 23:08:16 +02:00
|
|
|
|
|
|
|
do_ssh.interspersedOptions = false;
|
|
|
|
|
2016-03-09 19:19:44 +02:00
|
|
|
// Use 'file' to fallback to the default bash completion... even though 'file'
|
|
|
|
// isn't quite right.
|
|
|
|
do_ssh.completionArgtypes = ['tritoninstance', 'file'];
|
|
|
|
|
2016-01-04 23:08:16 +02:00
|
|
|
module.exports = do_ssh;
|