2017-03-03 12:02:06 +00:00
// Generated by CoffeeScript 1.12.4
2016-04-24 19:52:41 +00:00
/ *
Copyright 2016 Resin . io
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 .
* /
( function ( ) {
var getSubShellCommand ;
getSubShellCommand = function ( command ) {
var os ;
os = require ( 'os' ) ;
if ( os . platform ( ) === 'win32' ) {
return {
2016-05-06 17:11:57 +00:00
program : 'cmd.exe' ,
args : [ '/s' , '/c' , command ]
2016-04-24 19:52:41 +00:00
} ;
} else {
return {
program : '/bin/sh' ,
args : [ '-c' , command ]
} ;
}
} ;
module . exports = {
2016-07-19 16:12:36 +00:00
signature : 'ssh [uuid]' ,
2016-04-24 19:52:41 +00:00
description : '(beta) get a shell into the running app container of a device' ,
2017-01-18 15:58:20 +00:00
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' ,
2016-04-24 19:52:41 +00:00
permission : 'user' ,
primary : true ,
options : [
{
signature : 'port' ,
parameter : 'port' ,
description : 'ssh gateway port' ,
2016-07-19 16:12:36 +00:00
alias : 'p'
2016-06-22 11:53:24 +00:00
} , {
signature : 'verbose' ,
boolean : true ,
description : 'increase verbosity' ,
alias : 'v'
2016-04-24 19:52:41 +00:00
}
] ,
action : function ( params , options , done ) {
2016-07-07 17:58:12 +00:00
var Promise , child _process , patterns , resin , settings , verbose ;
2016-04-24 19:52:41 +00:00
child _process = require ( 'child_process' ) ;
Promise = require ( 'bluebird' ) ;
2017-01-25 18:32:07 +00:00
resin = require ( 'resin-sdk-preconfigured' ) ;
2016-04-24 19:52:41 +00:00
settings = require ( 'resin-settings-client' ) ;
2016-07-07 17:58:12 +00:00
patterns = require ( '../utils/patterns' ) ;
2016-04-24 19:52:41 +00:00
if ( options . port == null ) {
options . port = 22 ;
}
2016-06-22 11:53:24 +00:00
verbose = options . verbose ? '-vvv' : '' ;
2017-03-03 12:02:06 +00:00
return Promise [ "try" ] ( function ( ) {
if ( ! params . uuid ) {
return false ;
}
return resin . models . device . has ( params . uuid ) ;
} ) . then ( function ( uuidExists ) {
if ( uuidExists ) {
2016-07-19 16:12:36 +00:00
return params . uuid ;
2016-04-24 19:52:41 +00:00
}
2016-07-19 16:12:36 +00:00
return patterns . inferOrSelectDevice ( ) ;
2016-07-07 17:58:12 +00:00
} ) . then ( function ( uuid ) {
console . info ( "Connecting with: " + uuid ) ;
return resin . models . device . get ( uuid ) ;
} ) . then ( function ( device ) {
if ( ! device . is _online ) {
throw new Error ( 'Device is not online' ) ;
2016-04-24 19:52:41 +00:00
}
2016-07-07 17:58:12 +00:00
return Promise . props ( {
username : resin . auth . whoami ( ) ,
uuid : device . uuid ,
containerId : resin . models . device . getApplicationInfo ( device . uuid ) . get ( 'containerId' )
} ) . then ( function ( arg ) {
var containerId , username , uuid ;
username = arg . username , uuid = arg . uuid , containerId = arg . containerId ;
if ( containerId == null ) {
throw new Error ( 'Did not find running application container' ) ;
}
return Promise [ "try" ] ( function ( ) {
2017-03-03 12:02:06 +00:00
var command , subShellCommand ;
2016-07-19 16:12:36 +00:00
command = "ssh " + verbose + " -t -o LogLevel=ERROR -o StrictHostKeyChecking=no -o UserKnownHostsFile=/dev/null -o ControlMaster=no -p " + options . port + " " + username + "@ssh." + ( settings . get ( 'proxyUrl' ) ) + " enter " + uuid + " " + containerId ;
2016-07-07 17:58:12 +00:00
subShellCommand = getSubShellCommand ( command ) ;
2017-03-03 12:02:06 +00:00
return child _process . spawn ( subShellCommand . program , subShellCommand . args , {
2016-07-07 17:58:12 +00:00
stdio : 'inherit'
} ) ;
2016-04-24 19:52:41 +00:00
} ) ;
} ) ;
2016-04-26 16:37:39 +00:00
} ) . nodeify ( done ) ;
2016-04-24 19:52:41 +00:00
}
} ;
} ) . call ( this ) ;