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-12-18 13:31:07 +00:00
|
|
|
commandOptions = require('./command-options')
|
|
|
|
|
2016-04-24 19:52:41 +00:00
|
|
|
module.exports =
|
2016-07-19 16:10:26 +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'
|
|
|
|
help: '''
|
2017-01-18 15:57:52 +00:00
|
|
|
Warning: 'resin ssh' requires an openssh-compatible client to be correctly
|
|
|
|
installed in your shell environment. For more information (including Windows
|
|
|
|
support) please check the README here: https://github.com/resin-io/resin-cli
|
2016-05-19 14:10:45 +00:00
|
|
|
|
2016-04-24 19:52:41 +00:00
|
|
|
Use this command to get a shell into the running application container of
|
|
|
|
your device.
|
|
|
|
|
|
|
|
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
|
2017-12-18 10:36:58 +00:00
|
|
|
$ resin ssh 7cf02a6 -H
|
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:10:26 +00:00
|
|
|
alias: 'p'
|
2016-06-22 11:53:24 +00:00
|
|
|
,
|
|
|
|
signature: 'verbose'
|
|
|
|
boolean: true
|
|
|
|
description: 'increase verbosity'
|
|
|
|
alias: 'v'
|
2017-12-18 13:31:07 +00:00
|
|
|
commandOptions.hostOSAccess,
|
2017-05-18 12:12:52 +00:00
|
|
|
signature: 'noproxy'
|
|
|
|
boolean: true
|
2017-05-18 22:25:01 +00:00
|
|
|
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
|
|
|
]
|
|
|
|
action: (params, options, done) ->
|
|
|
|
child_process = require('child_process')
|
2017-05-11 11:34:22 +00:00
|
|
|
Promise = require('bluebird')
|
2017-01-25 18:32:07 +00:00
|
|
|
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')
|
2016-07-07 17:58:12 +00:00
|
|
|
patterns = require('../utils/patterns')
|
2016-04-24 19:52:41 +00:00
|
|
|
|
2017-05-11 11:34:22 +00:00
|
|
|
options.port ?= 22
|
2016-04-24 19:52:41 +00:00
|
|
|
|
2016-06-22 11:53:24 +00:00
|
|
|
verbose = if options.verbose then '-vvv' else ''
|
|
|
|
|
2017-05-18 23:10:14 +00:00
|
|
|
proxyConfig = global.PROXY_CONFIG
|
|
|
|
useProxy = !!proxyConfig and not options.noproxy
|
|
|
|
|
|
|
|
getSshProxyCommand = (hasTunnelBin) ->
|
|
|
|
return '' if not useProxy
|
|
|
|
|
|
|
|
if not hasTunnelBin
|
|
|
|
console.warn('''
|
|
|
|
Proxy is enabled but the `proxytunnel` binary cannot be found.
|
|
|
|
Please install it if you want to route the `resin ssh` requests through the proxy.
|
|
|
|
Alternatively you can pass `--noproxy` param to the `resin ssh` command to ignore the proxy config
|
|
|
|
for the `ssh` requests.
|
|
|
|
|
|
|
|
Attemmpting the unproxied request for now.
|
|
|
|
''')
|
|
|
|
return ''
|
|
|
|
|
|
|
|
tunnelOptions =
|
|
|
|
proxy: "#{proxyConfig.host}:#{proxyConfig.port}"
|
|
|
|
dest: '%h:%p'
|
|
|
|
{ proxyAuth } = proxyConfig
|
|
|
|
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-03 12:02:06 +00:00
|
|
|
Promise.try ->
|
|
|
|
return false if not params.uuid
|
|
|
|
return resin.models.device.has(params.uuid)
|
|
|
|
.then (uuidExists) ->
|
|
|
|
return params.uuid if uuidExists
|
2016-07-19 16:10:26 +00:00
|
|
|
return patterns.inferOrSelectDevice()
|
2016-07-07 17:58:12 +00:00
|
|
|
.then (uuid) ->
|
2017-05-11 11:34:22 +00:00
|
|
|
console.info("Connecting to: #{uuid}")
|
2016-07-07 17:58:12 +00:00
|
|
|
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()
|
2017-04-21 10:10:30 +00:00
|
|
|
uuid: device.uuid
|
|
|
|
# get full uuid
|
2017-12-18 10:36:58 +00:00
|
|
|
containerId: if options.host then '' else resin.models.device.getApplicationInfo(device.uuid).get('containerId')
|
2017-04-27 13:18:39 +00:00
|
|
|
proxyUrl: resin.settings.get('proxyUrl')
|
2017-05-18 23:10:14 +00:00
|
|
|
|
|
|
|
hasTunnelBin: if useProxy then hasbin('proxytunnel') else null
|
|
|
|
.then ({ username, uuid, containerId, proxyUrl, hasTunnelBin }) ->
|
2016-07-07 17:58:12 +00:00
|
|
|
throw new Error('Did not find running application container') if not containerId?
|
|
|
|
Promise.try ->
|
2017-05-18 23:10:14 +00:00
|
|
|
sshProxyCommand = getSshProxyCommand(hasTunnelBin)
|
2017-12-18 10:36:58 +00:00
|
|
|
|
|
|
|
if options.host
|
|
|
|
accessCommand = "host #{uuid}"
|
2017-12-18 13:31:07 +00:00
|
|
|
else
|
|
|
|
accessCommand = "enter #{uuid} #{containerId}"
|
2017-12-18 10:36:58 +00:00
|
|
|
|
2016-07-20 14:24:42 +00:00
|
|
|
command = "ssh #{verbose} -t \
|
|
|
|
-o LogLevel=ERROR \
|
|
|
|
-o StrictHostKeyChecking=no \
|
|
|
|
-o UserKnownHostsFile=/dev/null \
|
2017-05-11 11:34:22 +00:00
|
|
|
#{sshProxyCommand} \
|
2017-12-18 10:36:58 +00:00
|
|
|
-p #{options.port} #{username}@ssh.#{proxyUrl} #{accessCommand}"
|
2016-07-07 17:58:12 +00:00
|
|
|
|
|
|
|
subShellCommand = getSubShellCommand(command)
|
2017-03-03 12:02:06 +00:00
|
|
|
child_process.spawn subShellCommand.program, subShellCommand.args,
|
2016-07-07 17:58:12 +00:00
|
|
|
stdio: 'inherit'
|
2016-04-26 16:37:39 +00:00
|
|
|
.nodeify(done)
|