balena-cli/lib/actions/local/ssh.coffee

115 lines
3.4 KiB
CoffeeScript
Raw Normal View History

2017-03-08 18:49:20 +00:00
###
Copyright 2017 Balena
2017-03-08 18:49:20 +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
{ hostOSAccess } = require('../command-options')
_ = require('lodash')
localHostOSAccessOption = _.cloneDeep(hostOSAccess)
localHostOSAccessOption.description = 'get a shell into the host OS'
2017-03-08 18:49:20 +00:00
module.exports =
signature: 'local ssh [deviceIp]'
description: 'Get a shell into a balenaOS device'
2017-03-08 18:49:20 +00:00
help: '''
Warning: 'balena local ssh' requires an openssh-compatible client to be correctly
2017-03-08 18:49:20 +00:00
installed in your shell environment. For more information (including Windows
support) please check the README here: https://github.com/balena-io/balena-cli
2017-03-08 18:49:20 +00:00
Use this command to get a shell into the running application container of
your device.
The '--host' option will get you a shell into the Host OS of the balenaOS device.
2017-03-08 18:49:20 +00:00
No option will return a list of containers to enter or you can explicitly select
one by passing its name to the --container option
Examples:
$ balena local ssh
$ balena local ssh --host
$ balena local ssh --container chaotic_water
$ balena local ssh --container chaotic_water --port 22222
$ balena local ssh --verbose
2017-03-08 18:49:20 +00:00
'''
options: [
signature: 'verbose'
boolean: true
description: 'increase verbosity'
alias: 'v'
,
2017-12-18 13:31:07 +00:00
localHostOSAccessOption,
2017-03-08 18:49:20 +00:00
signature: 'container'
parameter: 'container'
default: null
description: 'name of container to access'
alias: 'c'
,
signature: 'port'
parameter: 'port'
description: 'ssh port number (default: 22222)'
alias: 'p'
]
root: true
2017-03-08 18:49:20 +00:00
action: (params, options, done) ->
child_process = require('child_process')
Promise = require 'bluebird'
_ = require('lodash')
{ forms } = require('balena-sync')
2017-03-08 18:49:20 +00:00
{ selectContainerFromDevice, getSubShellCommand } = require('./common')
{ exitWithExpectedError } = require('../../utils/patterns')
2017-03-08 18:49:20 +00:00
if (options.host is true and options.container?)
exitWithExpectedError('Please pass either --host or --container option')
2017-03-08 18:49:20 +00:00
if not options.port?
options.port = 22222
verbose = if options.verbose then '-vvv' else ''
Promise.try ->
if not params.deviceIp?
return forms.selectLocalBalenaOsDevice()
2017-03-08 18:49:20 +00:00
return params.deviceIp
.then (deviceIp) ->
_.assign(options, { deviceIp })
return if options.host
if not options.container?
return selectContainerFromDevice(deviceIp)
return options.container
.then (container) ->
command = "ssh \
#{verbose} \
-t \
-p #{options.port} \
-o LogLevel=ERROR \
-o StrictHostKeyChecking=no \
-o UserKnownHostsFile=/dev/null \
root@#{options.deviceIp}"
if not options.host
shellCmd = '''/bin/sh -c $"'if [ -e /bin/bash ]; then exec /bin/bash; else exec /bin/sh; fi'"'''
dockerCmd = "'$(if [ -f /usr/bin/balena ]; then echo \"balena\"; else echo \"docker\"; fi)'"
command += " #{dockerCmd} exec -ti #{container} #{shellCmd}"
2017-03-08 18:49:20 +00:00
subShellCommand = getSubShellCommand(command)
child_process.spawn subShellCommand.program, subShellCommand.args,
stdio: 'inherit'
.nodeify(done)