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.
|
|
|
|
###
|
|
|
|
|
2016-05-06 17:11:57 +00:00
|
|
|
# TODO: A function to reliably execute a command
|
|
|
|
# in all supported operating systems, including
|
|
|
|
# different Windows environments like `cmd.exe`
|
|
|
|
# and `Cygwin` should be encapsulated in a
|
|
|
|
# re-usable package.
|
|
|
|
# This is literally copy-pasted from the `resin-sync`
|
|
|
|
# module.
|
2016-04-24 19:52:41 +00:00
|
|
|
getSubShellCommand = (command) ->
|
|
|
|
os = require('os')
|
|
|
|
|
|
|
|
if os.platform() is '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-07 17:58:12 +00:00
|
|
|
signature: 'ssh [destination]'
|
2016-04-24 19:52:41 +00:00
|
|
|
description: '(beta) get a shell into the running app container of a device'
|
|
|
|
help: '''
|
2016-05-19 14:10:45 +00:00
|
|
|
WARNING: If you're running Windows, this command only supports `cmd.exe`.
|
|
|
|
|
2016-04-24 19:52:41 +00:00
|
|
|
Use this command to get a shell into the running application container of
|
|
|
|
your device.
|
|
|
|
|
2016-07-07 17:58:12 +00:00
|
|
|
The `destination` argument can be either a device uuid or an application name.
|
|
|
|
|
2016-04-24 19:52:41 +00:00
|
|
|
Examples:
|
|
|
|
|
2016-07-07 17:58:12 +00:00
|
|
|
$ resin ssh MyApp
|
2016-04-24 19:52:41 +00:00
|
|
|
$ resin ssh 7cf02a6
|
|
|
|
$ resin ssh 7cf02a6 --port 8080
|
2016-06-22 11:53:24 +00:00
|
|
|
$ 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'
|
|
|
|
alias: 't'
|
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: (params, options, done) ->
|
|
|
|
child_process = require('child_process')
|
|
|
|
Promise = require 'bluebird'
|
|
|
|
resin = require('resin-sdk')
|
|
|
|
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 not options.port?
|
|
|
|
options.port = 22
|
|
|
|
|
2016-06-22 11:53:24 +00:00
|
|
|
verbose = if options.verbose then '-vvv' else ''
|
|
|
|
|
2016-07-07 17:58:12 +00:00
|
|
|
resin.models.device.has(params.destination).then (isValidUUID) ->
|
|
|
|
if isValidUUID
|
|
|
|
return params.destination
|
|
|
|
|
|
|
|
return patterns.inferOrSelectDevice(params.destination)
|
|
|
|
.then (uuid) ->
|
|
|
|
console.info("Connecting with: #{uuid}")
|
|
|
|
resin.models.device.get(uuid)
|
|
|
|
.then (device) ->
|
|
|
|
throw new Error('Device is not online') if not device.is_online
|
|
|
|
|
|
|
|
Promise.props
|
|
|
|
username: resin.auth.whoami()
|
|
|
|
uuid: device.uuid # get full uuid
|
|
|
|
containerId: resin.models.device.getApplicationInfo(device.uuid).get('containerId')
|
|
|
|
.then ({ username, uuid, containerId }) ->
|
|
|
|
throw new Error('Did not find running application container') if not containerId?
|
|
|
|
Promise.try ->
|
|
|
|
command = "ssh #{verbose} -t -o LogLevel=ERROR -o StrictHostKeyChecking=no -o UserKnownHostsFile=/dev/null \
|
|
|
|
-p #{options.port} #{username}@ssh.#{settings.get('proxyUrl')} enter #{uuid} #{containerId}"
|
|
|
|
|
|
|
|
subShellCommand = getSubShellCommand(command)
|
|
|
|
spawn = child_process.spawn subShellCommand.program, subShellCommand.args,
|
|
|
|
stdio: 'inherit'
|
2016-04-26 16:37:39 +00:00
|
|
|
.nodeify(done)
|