Merge pull request #444 from resin-io/scan-sudo-and-fixed-latency

Scan sudo and fixed latency
This commit is contained in:
Kostas Lekkas 2017-03-10 18:04:56 +00:00 committed by GitHub
commit c95a01e34e
15 changed files with 180 additions and 155 deletions

View File

@ -1,6 +1,6 @@
// Generated by CoffeeScript 1.12.4
(function() {
var Docker, Promise, _, chalk, filterOutSupervisorContainer, form;
var Docker, Promise, _, chalk, dockerPort, dockerTimeout, filterOutSupervisorContainer, form;
Promise = require('bluebird');
@ -12,7 +12,11 @@
chalk = require('chalk');
filterOutSupervisorContainer = function(container) {
exports.dockerPort = dockerPort = 2375;
exports.dockerTimeout = dockerTimeout = 2000;
exports.filterOutSupervisorContainer = filterOutSupervisorContainer = function(container) {
var i, len, name, ref;
ref = container.Names;
for (i = 0, len = ref.length; i < len; i++) {
@ -24,86 +28,86 @@
return true;
};
module.exports = {
filterOutSupervisorContainer: filterOutSupervisorContainer,
selectContainerFromDevice: Promise.method(function(deviceIp, filterSupervisor) {
var docker;
if (filterSupervisor == null) {
filterSupervisor = false;
exports.selectContainerFromDevice = Promise.method(function(deviceIp, filterSupervisor) {
var docker;
if (filterSupervisor == null) {
filterSupervisor = false;
}
docker = new Docker({
host: deviceIp,
port: dockerPort,
timeout: dockerTimeout
});
return docker.listContainersAsync({
all: true
}).filter(function(container) {
if (!filterSupervisor) {
return true;
}
docker = new Docker({
host: deviceIp,
port: 2375
});
return docker.listContainersAsync({
all: true
}).filter(function(container) {
if (!filterSupervisor) {
return true;
}
return filterOutSupervisorContainer(container);
}).then(function(containers) {
if (_.isEmpty(containers)) {
throw new Error("No containers found in " + deviceIp);
}
return form.ask({
message: 'Select a container',
type: 'list',
choices: _.map(containers, function(container) {
var containerName, containerStatus, shortContainerId;
containerName = container.Names[0] || 'Untitled';
shortContainerId = ('' + container.Id).substr(0, 11);
containerStatus = container.Status;
return {
name: containerName + " (" + shortContainerId + ") - " + containerStatus,
value: container.Id
};
})
});
});
}),
pipeContainerStream: Promise.method(function(arg) {
var container, deviceIp, docker, follow, name, outStream, ref;
deviceIp = arg.deviceIp, name = arg.name, outStream = arg.outStream, follow = (ref = arg.follow) != null ? ref : false;
docker = new Docker({
host: deviceIp,
port: 2375
});
container = docker.getContainer(name);
return container.inspectAsync().then(function(containerInfo) {
var ref1;
return containerInfo != null ? (ref1 = containerInfo.State) != null ? ref1.Running : void 0 : void 0;
}).then(function(isRunning) {
return container.attachAsync({
logs: !follow || !isRunning,
stream: follow && isRunning,
stdout: true,
stderr: true
});
}).then(function(containerStream) {
return containerStream.pipe(outStream);
})["catch"](function(err) {
err = '' + err.statusCode;
if (err === '404') {
return console.log(chalk.red.bold("Container '" + name + "' not found."));
}
throw err;
});
}),
getSubShellCommand: function(command) {
var os;
os = require('os');
if (os.platform() === 'win32') {
return {
program: 'cmd.exe',
args: ['/s', '/c', command]
};
} else {
return {
program: '/bin/sh',
args: ['-c', command]
};
return filterOutSupervisorContainer(container);
}).then(function(containers) {
if (_.isEmpty(containers)) {
throw new Error("No containers found in " + deviceIp);
}
return form.ask({
message: 'Select a container',
type: 'list',
choices: _.map(containers, function(container) {
var containerName, containerStatus, shortContainerId;
containerName = container.Names[0] || 'Untitled';
shortContainerId = ('' + container.Id).substr(0, 11);
containerStatus = container.Status;
return {
name: containerName + " (" + shortContainerId + ") - " + containerStatus,
value: container.Id
};
})
});
});
});
exports.pipeContainerStream = Promise.method(function(arg) {
var container, deviceIp, docker, follow, name, outStream, ref;
deviceIp = arg.deviceIp, name = arg.name, outStream = arg.outStream, follow = (ref = arg.follow) != null ? ref : false;
docker = new Docker({
host: deviceIp,
port: dockerPort
});
container = docker.getContainer(name);
return container.inspectAsync().then(function(containerInfo) {
var ref1;
return containerInfo != null ? (ref1 = containerInfo.State) != null ? ref1.Running : void 0 : void 0;
}).then(function(isRunning) {
return container.attachAsync({
logs: !follow || !isRunning,
stream: follow && isRunning,
stdout: true,
stderr: true
});
}).then(function(containerStream) {
return containerStream.pipe(outStream);
})["catch"](function(err) {
err = '' + err.statusCode;
if (err === '404') {
return console.log(chalk.red.bold("Container '" + name + "' not found."));
}
throw err;
});
});
exports.getSubShellCommand = function(command) {
var os;
os = require('os');
if (os.platform() === 'win32') {
return {
program: 'cmd.exe',
args: ['/s', '/c', command]
};
} else {
return {
program: '/bin/sh',
args: ['-c', command]
};
}
};

View File

@ -34,6 +34,7 @@ limitations under the License.
alias: 'a'
}
],
root: true,
action: function(params, options, done) {
var Promise, forms, pipeContainerStream, ref, selectContainerFromDevice;
Promise = require('bluebird');

View File

@ -34,6 +34,7 @@ limitations under the License.
alias: 'p'
}
],
root: true,
action: function(params, options, done) {
var Promise, _, child_process, forms, getSubShellCommand, verbose;
child_process = require('child_process');

View File

@ -29,6 +29,8 @@ limitations under the License.
resinPush.primary = true;
resinPush.root = true;
module.exports = resinPush;
}).call(this);

View File

@ -41,14 +41,16 @@ limitations under the License.
}
],
primary: true,
root: true,
action: function(params, options, done) {
var Docker, Promise, SpinnerPromise, _, discover, prettyjson;
var Docker, Promise, SpinnerPromise, _, discover, dockerPort, dockerTimeout, prettyjson, ref;
Promise = require('bluebird');
_ = require('lodash');
prettyjson = require('prettyjson');
Docker = require('docker-toolbelt');
discover = require('resin-sync').discover;
SpinnerPromise = require('resin-cli-visuals').SpinnerPromise;
ref = require('./common'), dockerPort = ref.dockerPort, dockerTimeout = ref.dockerTimeout;
if (options.timeout != null) {
options.timeout *= 1000;
}
@ -59,13 +61,17 @@ limitations under the License.
stopMessage: 'Reporting scan results'
});
}).filter(function(arg) {
var address, docker;
var address;
address = arg.address;
docker = new Docker({
host: address,
port: 2375
});
return docker.infoAsync()["return"](true).catchReturn(false);
return Promise["try"](function() {
var docker;
docker = new Docker({
host: address,
port: dockerPort,
timeout: dockerTimeout
});
return docker.pingAsync();
})["return"](true).catchReturn(false);
}).tap(function(devices) {
if (_.isEmpty(devices)) {
throw new Error('Could not find any resinOS devices in the local network');
@ -75,7 +81,8 @@ limitations under the License.
host = arg.host, address = arg.address;
docker = new Docker({
host: address,
port: 2375
port: dockerPort,
timeout: dockerTimeout
});
return Promise.props({
dockerInfo: docker.infoAsync().catchReturn('Could not get Docker info'),

View File

@ -45,6 +45,7 @@ limitations under the License.
alias: 'p'
}
],
root: true,
action: function(params, options, done) {
var Promise, _, child_process, forms, getSubShellCommand, ref, selectContainerFromDevice, verbose;
child_process = require('child_process');

View File

@ -33,6 +33,7 @@ limitations under the License.
alias: 'a'
}
],
root: true,
action: function(params, options, done) {
var Promise, ResinLocalDockerUtils, chalk, config, filterOutSupervisorContainer, forms, ref, ref1, selectContainerFromDevice;
Promise = require('bluebird');

View File

@ -4,76 +4,75 @@ Docker = require('docker-toolbelt')
form = require('resin-cli-form')
chalk = require('chalk')
filterOutSupervisorContainer = (container) ->
exports.dockerPort = dockerPort = 2375
exports.dockerTimeout = dockerTimeout = 2000
exports.filterOutSupervisorContainer = filterOutSupervisorContainer = (container) ->
for name in container.Names
return false if name.includes('resin_supervisor')
return true
module.exports =
exports.selectContainerFromDevice = Promise.method (deviceIp, filterSupervisor = false) ->
docker = new Docker(host: deviceIp, port: dockerPort, timeout: dockerTimeout)
filterOutSupervisorContainer: filterOutSupervisorContainer
# List all containers, including those not running
docker.listContainersAsync(all: true)
.filter (container) ->
return true if not filterSupervisor
filterOutSupervisorContainer(container)
.then (containers) ->
if _.isEmpty(containers)
throw new Error("No containers found in #{deviceIp}")
selectContainerFromDevice: Promise.method (deviceIp, filterSupervisor = false) ->
docker = new Docker(host: deviceIp, port: 2375)
return form.ask
message: 'Select a container'
type: 'list'
choices: _.map containers, (container) ->
containerName = container.Names[0] or 'Untitled'
shortContainerId = ('' + container.Id).substr(0, 11)
containerStatus = container.Status
# List all containers, including those not running
docker.listContainersAsync(all: true)
.filter (container) ->
return true if not filterSupervisor
filterOutSupervisorContainer(container)
.then (containers) ->
if _.isEmpty(containers)
throw new Error("No containers found in #{deviceIp}")
return {
name: "#{containerName} (#{shortContainerId}) - #{containerStatus}"
value: container.Id
}
return form.ask
message: 'Select a container'
type: 'list'
choices: _.map containers, (container) ->
containerName = container.Names[0] or 'Untitled'
shortContainerId = ('' + container.Id).substr(0, 11)
containerStatus = container.Status
exports.pipeContainerStream = Promise.method ({ deviceIp, name, outStream, follow = false }) ->
docker = new Docker(host: deviceIp, port: dockerPort)
return {
name: "#{containerName} (#{shortContainerId}) - #{containerStatus}"
value: container.Id
}
container = docker.getContainer(name)
container.inspectAsync()
.then (containerInfo) ->
return containerInfo?.State?.Running
.then (isRunning) ->
container.attachAsync
logs: not follow or not isRunning
stream: follow and isRunning
stdout: true
stderr: true
.then (containerStream) ->
containerStream.pipe(outStream)
.catch (err) ->
err = '' + err.statusCode
if err is '404'
return console.log(chalk.red.bold("Container '#{name}' not found."))
throw err
pipeContainerStream: Promise.method ({ deviceIp, name, outStream, follow = false }) ->
docker = new Docker(host: deviceIp, port: 2375)
# 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.
exports.getSubShellCommand = (command) ->
os = require('os')
container = docker.getContainer(name)
container.inspectAsync()
.then (containerInfo) ->
return containerInfo?.State?.Running
.then (isRunning) ->
container.attachAsync
logs: not follow or not isRunning
stream: follow and isRunning
stdout: true
stderr: true
.then (containerStream) ->
containerStream.pipe(outStream)
.catch (err) ->
err = '' + err.statusCode
if err is '404'
return console.log(chalk.red.bold("Container '#{name}' not found."))
throw err
# 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.
getSubShellCommand: (command) ->
os = require('os')
if os.platform() is 'win32'
return {
program: 'cmd.exe'
args: [ '/s', '/c', command ]
}
else
return {
program: '/bin/sh'
args: [ '-c', command ]
}
if os.platform() is 'win32'
return {
program: 'cmd.exe'
args: [ '/s', '/c', command ]
}
else
return {
program: '/bin/sh'
args: [ '-c', command ]
}

View File

@ -44,6 +44,7 @@ module.exports =
description: 'name of container to get logs from'
alias: 'a'
]
root: true
action: (params, options, done) ->
Promise = require('bluebird')
{ forms } = require('resin-sync')

View File

@ -44,6 +44,7 @@ module.exports =
description: 'ssh port number (default: 22222)'
alias: 'p'
]
root: true
action: (params, options, done) ->
child_process = require('child_process')
Promise = require 'bluebird'

View File

@ -66,4 +66,5 @@ resinPush.help =
$ resin local push 192.168.2.10 -s /home/user/myResinProject -d /usr/src/app --before 'echo Hello' --after 'echo Done'
'''
resinPush.primary = true
resinPush.root = true
module.exports = resinPush

View File

@ -14,7 +14,6 @@ See the License for the specific language governing permissions and
limitations under the License.
###
dockerInfoProperties = [
'Containers'
'ContainersRunning'
@ -56,6 +55,7 @@ module.exports =
alias: 't'
]
primary: true
root: true
action: (params, options, done) ->
Promise = require('bluebird')
_ = require('lodash')
@ -63,6 +63,7 @@ module.exports =
Docker = require('docker-toolbelt')
{ discover } = require('resin-sync')
{ SpinnerPromise } = require('resin-cli-visuals')
{ dockerPort, dockerTimeout } = require('./common')
if options.timeout?
options.timeout *= 1000
@ -73,13 +74,16 @@ module.exports =
startMessage: 'Scanning for local resinOS devices..'
stopMessage: 'Reporting scan results'
.filter ({ address }) ->
docker = new Docker(host: address, port: 2375)
docker.infoAsync().return(true).catchReturn(false)
Promise.try ->
docker = new Docker(host: address, port: dockerPort, timeout: dockerTimeout)
docker.pingAsync()
.return(true)
.catchReturn(false)
.tap (devices) ->
if _.isEmpty(devices)
throw new Error('Could not find any resinOS devices in the local network')
.map ({ host, address }) ->
docker = new Docker(host: address, port: 2375)
docker = new Docker(host: address, port: dockerPort, timeout: dockerTimeout)
Promise.props
dockerInfo: docker.infoAsync().catchReturn('Could not get Docker info')
dockerVersion: docker.versionAsync().catchReturn('Could not get Docker version')

View File

@ -59,6 +59,7 @@ module.exports =
description: 'ssh port number (default: 22222)'
alias: 'p'
]
root: true
action: (params, options, done) ->
child_process = require('child_process')
Promise = require 'bluebird'

View File

@ -43,6 +43,7 @@ module.exports =
description: 'name of container to stop'
alias: 'a'
]
root: true
action: (params, options, done) ->
Promise = require('bluebird')
chalk = require('chalk')

View File

@ -63,7 +63,7 @@
"resin-image-manager": "^4.0.0",
"resin-sdk-preconfigured": "^0.1.0",
"resin-settings-client": "^3.5.0",
"resin-sync": "^6.1.0",
"resin-sync": "^7.0.0",
"rimraf": "^2.4.3",
"rindle": "^1.0.0",
"tmp": "^0.0.31",