Merge pull request #365 from resin-io/feature/sync-ssh-device-autochoose

Feature/sync ssh device autochoose
This commit is contained in:
Kostas Lekkas 2016-07-07 22:21:37 +03:00 committed by GitHub
commit f68364b695
4 changed files with 68 additions and 48 deletions

View File

@ -35,9 +35,9 @@ limitations under the License.
};
module.exports = {
signature: 'ssh <uuid>',
signature: 'ssh [destination]',
description: '(beta) get a shell into the running app container of a device',
help: 'WARNING: If you\'re running Windows, this command only supports `cmd.exe`.\n\nUse this command to get a shell into the running application container of\nyour device.\n\nExamples:\n\n $ resin ssh 7cf02a6\n $ resin ssh 7cf02a6 --port 8080\n $ resin ssh 7cf02a6 -v',
help: 'WARNING: If you\'re running Windows, this command only supports `cmd.exe`.\n\nUse this command to get a shell into the running application container of\nyour device.\n\nThe `destination` argument can be either a device uuid or an application name.\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: [
@ -54,36 +54,45 @@ limitations under the License.
}
],
action: function(params, options, done) {
var Promise, child_process, resin, settings, verbose;
var Promise, child_process, patterns, resin, settings, verbose;
child_process = require('child_process');
Promise = require('bluebird');
resin = require('resin-sdk');
settings = require('resin-settings-client');
patterns = require('../utils/patterns');
if (options.port == null) {
options.port = 22;
}
verbose = options.verbose ? '-vvv' : '';
console.info("Connecting with: " + params.uuid);
return Promise.props({
isOnline: resin.models.device.isOnline(params.uuid),
username: resin.auth.whoami(),
uuid: resin.models.device.get(params.uuid).get('uuid'),
containerId: resin.models.device.getApplicationInfo(params.uuid).get('containerId')
}).then(function(arg) {
var containerId, isOnline, username, uuid;
isOnline = arg.isOnline, username = arg.username, uuid = arg.uuid, containerId = arg.containerId;
if (!isOnline) {
return resin.models.device.has(params.destination).then(function(isValidUUID) {
if (isValidUUID) {
return params.destination;
}
return patterns.inferOrSelectDevice(params.destination);
}).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');
}
if (containerId == null) {
throw new Error('Did not find running application container');
}
return Promise["try"](function() {
var command, spawn, subShellCommand;
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);
return spawn = child_process.spawn(subShellCommand.program, subShellCommand.args, {
stdio: 'inherit'
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() {
var command, spawn, subShellCommand;
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);
return spawn = child_process.spawn(subShellCommand.program, subShellCommand.args, {
stdio: 'inherit'
});
});
});
}).nodeify(done);

View File

@ -17,9 +17,9 @@ limitations under the License.
(function() {
module.exports = {
signature: 'sync [source]',
signature: 'sync [destination]',
description: '(beta) sync your changes with a device',
help: 'WARNING: If you\'re running Windows, this command only supports `cmd.exe`.\n\nUse this command to sync your local changes to a certain device on the fly.\n\nThe `source` argument can be either a device uuid or an application name.\n\nYou can save all the options mentioned below in a `resin-sync.yml` file,\nby using the same option names as keys. For example:\n\n $ cat $PWD/resin-sync.yml\n source: src/\n before: \'echo Hello\'\n ignore:\n - .git\n - node_modules/\n progress: true\n verbose: false\n\nNotice that explicitly passed command options override the ones set in the configuration file.\n\nExamples:\n\n $ resin sync MyApp\n $ resin sync 7cf02a6\n $ resin sync 7cf02a6 --port 8080\n $ resin sync 7cf02a6 --ignore foo,bar\n $ resin sync 7cf02a6 -v',
help: 'WARNING: If you\'re running Windows, this command only supports `cmd.exe`.\n\nUse this command to sync your local changes to a certain device on the fly.\n\nThe `destination` argument can be either a device uuid or an application name.\n\nYou can save all the options mentioned below in a `resin-sync.yml` file,\nby using the same option names as keys. For example:\n\n $ cat $PWD/resin-sync.yml\n source: src/\n before: \'echo Hello\'\n ignore:\n - .git\n - node_modules/\n progress: true\n verbose: false\n\nNotice that explicitly passed command options override the ones set in the configuration file.\n\nExamples:\n\n $ resin sync MyApp\n $ resin sync 7cf02a6\n $ resin sync 7cf02a6 --port 8080\n $ resin sync 7cf02a6 --ignore foo,bar\n $ resin sync 7cf02a6 -v',
permission: 'user',
primary: true,
options: [
@ -63,11 +63,11 @@ limitations under the License.
if (options.ignore != null) {
options.ignore = options.ignore.split(',');
}
return resin.models.device.has(params.source).then(function(isValidUUID) {
return resin.models.device.has(params.destination).then(function(isValidUUID) {
if (isValidUUID) {
return params.source;
return params.destination;
}
return patterns.inferOrSelectDevice(params.source);
return patterns.inferOrSelectDevice(params.destination);
}).then(function(uuid) {
return resinSync.sync(uuid, options);
}).nodeify(done);

View File

@ -36,7 +36,7 @@ getSubShellCommand = (command) ->
}
module.exports =
signature: 'ssh <uuid>'
signature: 'ssh [destination]'
description: '(beta) get a shell into the running app container of a device'
help: '''
WARNING: If you're running Windows, this command only supports `cmd.exe`.
@ -44,8 +44,11 @@ module.exports =
Use this command to get a shell into the running application container of
your device.
The `destination` argument can be either a device uuid or an application name.
Examples:
$ resin ssh MyApp
$ resin ssh 7cf02a6
$ resin ssh 7cf02a6 --port 8080
$ resin ssh 7cf02a6 -v
@ -68,27 +71,35 @@ module.exports =
Promise = require 'bluebird'
resin = require('resin-sdk')
settings = require('resin-settings-client')
patterns = require('../utils/patterns')
if not options.port?
options.port = 22
verbose = if options.verbose then '-vvv' else ''
console.info("Connecting with: #{params.uuid}")
resin.models.device.has(params.destination).then (isValidUUID) ->
if isValidUUID
return params.destination
Promise.props
isOnline: resin.models.device.isOnline(params.uuid)
username: resin.auth.whoami()
uuid: resin.models.device.get(params.uuid).get('uuid') # get full uuid
containerId: resin.models.device.getApplicationInfo(params.uuid).get('containerId')
.then ({ isOnline, username, uuid, containerId }) ->
throw new Error('Device is not online') if not isOnline
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}"
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
subShellCommand = getSubShellCommand(command)
spawn = child_process.spawn subShellCommand.program, subShellCommand.args,
stdio: 'inherit'
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'
.nodeify(done)

View File

@ -15,14 +15,14 @@ limitations under the License.
###
module.exports =
signature: 'sync [source]'
signature: 'sync [destination]'
description: '(beta) sync your changes with a device'
help: '''
WARNING: If you're running Windows, this command only supports `cmd.exe`.
Use this command to sync your local changes to a certain device on the fly.
The `source` argument can be either a device uuid or an application name.
The `destination` argument can be either a device uuid or an application name.
You can save all the options mentioned below in a `resin-sync.yml` file,
by using the same option names as keys. For example:
@ -89,11 +89,11 @@ module.exports =
if options.ignore?
options.ignore = options.ignore.split(',')
resin.models.device.has(params.source).then (isValidUUID) ->
resin.models.device.has(params.destination).then (isValidUUID) ->
if isValidUUID
return params.source
return params.destination
return patterns.inferOrSelectDevice(params.source)
return patterns.inferOrSelectDevice(params.destination)
.then (uuid) ->
resinSync.sync(uuid, options)
.nodeify(done)