2017-07-27 11:07:05 +00:00
// Generated by CoffeeScript 1.12.7
2016-04-24 19:52:41 +00:00
/ *
2017-06-14 21:20:15 +00:00
Copyright 2016 - 2017 Resin . io
2016-04-24 19:52:41 +00:00
Licensed under the Apache License , Version 2.0 ( the "License" ) ;
you may not use this file except in compliance with the License .
You may obtain a copy of the License at
http : //www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing , software
distributed under the License is distributed on an "AS IS" BASIS ,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND , either express or implied .
See the License for the specific language governing permissions and
limitations under the License .
* /
2017-03-22 09:46:06 +00:00
module . exports = {
signature : 'ssh [uuid]' ,
description : '(beta) get a shell into the running app container of a device' ,
help : 'Warning: \'resin ssh\' requires an openssh-compatible client to be correctly\ninstalled in your shell environment. For more information (including Windows\nsupport) please check the README here: https://github.com/resin-io/resin-cli\n\nUse this command to get a shell into the running application container of\nyour device.\n\nExamples:\n\n $ resin ssh MyApp\n $ resin ssh 7cf02a6\n $ resin ssh 7cf02a6 --port 8080\n $ resin ssh 7cf02a6 -v' ,
permission : 'user' ,
primary : true ,
options : [
{
signature : 'port' ,
parameter : 'port' ,
description : 'ssh gateway port' ,
alias : 'p'
} , {
signature : 'verbose' ,
boolean : true ,
description : 'increase verbosity' ,
alias : 'v'
2017-05-18 22:25:01 +00:00
} , {
signature : 'noproxy' ,
boolean : true ,
description : "don't use the proxy configuration for this connection. Only makes sense if you've configured proxy globally."
2016-04-24 19:52:41 +00:00
}
2017-03-22 09:46:06 +00:00
] ,
action : function ( params , options , done ) {
2017-05-18 23:10:14 +00:00
var Promise , _ , bash , child _process , getSshProxyCommand , getSubShellCommand , hasbin , patterns , proxyConfig , resin , useProxy , verbose ;
2017-03-22 09:46:06 +00:00
child _process = require ( 'child_process' ) ;
Promise = require ( 'bluebird' ) ;
resin = require ( 'resin-sdk-preconfigured' ) ;
2017-05-18 23:10:14 +00:00
_ = require ( 'lodash' ) ;
bash = require ( 'bash' ) ;
hasbin = require ( 'hasbin' ) ;
getSubShellCommand = require ( '../utils/helpers' ) . getSubShellCommand ;
2017-03-22 09:46:06 +00:00
patterns = require ( '../utils/patterns' ) ;
if ( options . port == null ) {
options . port = 22 ;
}
verbose = options . verbose ? '-vvv' : '' ;
2017-05-18 23:10:14 +00:00
proxyConfig = global . PROXY _CONFIG ;
useProxy = ! ! proxyConfig && ! options . noproxy ;
getSshProxyCommand = function ( hasTunnelBin ) {
var i , proxyAuth , proxyCommand , tunnelOptions ;
if ( ! useProxy ) {
return '' ;
}
if ( ! hasTunnelBin ) {
console . warn ( 'Proxy is enabled but the `proxytunnel` binary cannot be found.\nPlease install it if you want to route the `resin ssh` requests through the proxy.\nAlternatively you can pass `--noproxy` param to the `resin ssh` command to ignore the proxy config\nfor the `ssh` requests.\n\nAttemmpting the unproxied request for now.' ) ;
return '' ;
}
tunnelOptions = {
proxy : proxyConfig . host + ":" + proxyConfig . port ,
dest : '%h:%p'
} ;
proxyAuth = proxyConfig . proxyAuth ;
if ( proxyAuth ) {
i = proxyAuth . indexOf ( ':' ) ;
_ . assign ( tunnelOptions , {
user : proxyAuth . substring ( 0 , i ) ,
pass : proxyAuth . substring ( i + 1 )
} ) ;
}
proxyCommand = "proxytunnel " + ( bash . args ( tunnelOptions , '--' , '=' ) ) ;
return "-o " + ( bash . args ( {
ProxyCommand : proxyCommand
} , '' , '=' ) ) ;
} ;
2017-03-22 09:46:06 +00:00
return Promise [ "try" ] ( function ( ) {
if ( ! params . uuid ) {
return false ;
2016-04-24 19:52:41 +00:00
}
2017-03-22 09:46:06 +00:00
return resin . models . device . has ( params . uuid ) ;
} ) . then ( function ( uuidExists ) {
if ( uuidExists ) {
return params . uuid ;
2016-04-24 19:52:41 +00:00
}
2017-03-22 09:46:06 +00:00
return patterns . inferOrSelectDevice ( ) ;
} ) . then ( function ( uuid ) {
2017-05-11 11:34:22 +00:00
console . info ( "Connecting to: " + uuid ) ;
2017-03-22 09:46:06 +00:00
return resin . models . device . get ( uuid ) ;
} ) . then ( function ( device ) {
if ( ! device . is _online ) {
throw new Error ( 'Device is not online' ) ;
}
return Promise . props ( {
username : resin . auth . whoami ( ) ,
uuid : device . uuid ,
2017-04-27 13:18:39 +00:00
containerId : resin . models . device . getApplicationInfo ( device . uuid ) . get ( 'containerId' ) ,
2017-05-18 23:10:14 +00:00
proxyUrl : resin . settings . get ( 'proxyUrl' ) ,
hasTunnelBin : useProxy ? hasbin ( 'proxytunnel' ) : null
2017-03-22 09:46:06 +00:00
} ) . then ( function ( arg ) {
2017-05-18 23:10:14 +00:00
var containerId , hasTunnelBin , proxyUrl , username , uuid ;
username = arg . username , uuid = arg . uuid , containerId = arg . containerId , proxyUrl = arg . proxyUrl , hasTunnelBin = arg . hasTunnelBin ;
2017-03-22 09:46:06 +00:00
if ( containerId == null ) {
throw new Error ( 'Did not find running application container' ) ;
2016-04-24 19:52:41 +00:00
}
2017-03-22 09:46:06 +00:00
return Promise [ "try" ] ( function ( ) {
2017-05-18 23:10:14 +00:00
var command , sshProxyCommand , subShellCommand ;
sshProxyCommand = getSshProxyCommand ( hasTunnelBin ) ;
2017-06-29 08:51:29 +00:00
command = "ssh " + verbose + " -t -o LogLevel=ERROR -o StrictHostKeyChecking=no -o UserKnownHostsFile=/dev/null " + sshProxyCommand + " -p " + options . port + " " + username + "@ssh." + proxyUrl + " enter " + uuid + " " + containerId ;
2017-03-22 09:46:06 +00:00
subShellCommand = getSubShellCommand ( command ) ;
return child _process . spawn ( subShellCommand . program , subShellCommand . args , {
stdio : 'inherit'
2016-04-24 19:52:41 +00:00
} ) ;
} ) ;
2017-03-22 09:46:06 +00:00
} ) ;
} ) . nodeify ( done ) ;
}
} ;