Merge pull request #439 from resin-io/resin-local-stop

Resin local stop
This commit is contained in:
Kostas Lekkas 2017-03-09 23:50:58 +00:00 committed by GitHub
commit e5a7fa5617
8 changed files with 198 additions and 3 deletions

View File

@ -1,6 +1,6 @@
// Generated by CoffeeScript 1.12.4
(function() {
var Docker, Promise, _, chalk, form;
var Docker, Promise, _, chalk, filterOutSupervisorContainer, form;
Promise = require('bluebird');
@ -12,15 +12,36 @@
chalk = require('chalk');
filterOutSupervisorContainer = function(container) {
var i, len, name, ref;
ref = container.Names;
for (i = 0, len = ref.length; i < len; i++) {
name = ref[i];
if (name.includes('resin_supervisor')) {
return false;
}
}
return true;
};
module.exports = {
selectContainerFromDevice: Promise.method(function(deviceIp) {
filterOutSupervisorContainer: filterOutSupervisorContainer,
selectContainerFromDevice: Promise.method(function(deviceIp, filterSupervisor) {
var docker;
if (filterSupervisor == null) {
filterSupervisor = false;
}
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);

View File

@ -31,4 +31,6 @@ limitations under the License.
exports.push = require('./push');
exports.stop = require('./stop');
}).call(this);

View File

@ -0,0 +1,80 @@
// Generated by CoffeeScript 1.12.4
/*
Copyright 2017 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.
*/
(function() {
module.exports = {
signature: 'local stop [deviceIp]',
description: 'Stop a running container on a resinOS device',
help: '\nExamples:\n\n $ resin local stop\n $ resin local stop --app-name myapp\n $ resin local stop --all\n $ resin local stop 192.168.1.10\n $ resin local stop 192.168.1.10 --app-name myapp',
options: [
{
signature: 'all',
boolean: true,
description: 'stop all containers'
}, {
signature: 'app-name',
parameter: 'name',
description: 'name of container to stop',
alias: 'a'
}
],
action: function(params, options, done) {
var Promise, ResinLocalDockerUtils, chalk, config, filterOutSupervisorContainer, forms, ref, ref1, selectContainerFromDevice;
Promise = require('bluebird');
chalk = require('chalk');
ref = require('resin-sync'), forms = ref.forms, config = ref.config, ResinLocalDockerUtils = ref.ResinLocalDockerUtils;
ref1 = require('./common'), selectContainerFromDevice = ref1.selectContainerFromDevice, filterOutSupervisorContainer = ref1.filterOutSupervisorContainer;
return Promise["try"](function() {
if (params.deviceIp == null) {
return forms.selectLocalResinOsDevice();
}
return params.deviceIp;
}).then((function(_this) {
return function(deviceIp) {
var ref2, ref3, ymlConfig;
_this.deviceIp = deviceIp;
_this.docker = new ResinLocalDockerUtils(_this.deviceIp);
if (options.all) {
return _this.docker.docker.listContainersAsync({
all: false
}).filter(filterOutSupervisorContainer).then(function(containers) {
return Promise.map(containers, function(arg) {
var Id, Names;
Names = arg.Names, Id = arg.Id;
console.log(chalk.yellow.bold("* Stopping container " + Names[0]));
return _this.docker.stopContainer(Id);
});
});
}
ymlConfig = config.load();
_this.appName = (ref2 = options['app-name']) != null ? ref2 : (ref3 = ymlConfig['local_resinos']) != null ? ref3['app-name'] : void 0;
return _this.docker.checkForRunningContainer(_this.appName).then(function(isRunning) {
if (!isRunning) {
return selectContainerFromDevice(_this.deviceIp, true);
}
console.log(chalk.yellow.bold("* Stopping container " + _this.appName));
return _this.appName;
}).then(function(runningContainerName) {
return _this.docker.stopContainer(runningContainerName);
});
};
})(this));
}
};
}).call(this);

View File

@ -170,6 +170,8 @@ limitations under the License.
capitano.command(actions.local.scan);
capitano.command(actions.local.stop);
update.notify();
plugins.register(/^resin-plugin-(.+)$/).then(function() {

View File

@ -4,13 +4,23 @@ Docker = require('docker-toolbelt')
form = require('resin-cli-form')
chalk = require('chalk')
filterOutSupervisorContainer = (container) ->
for name in container.Names
return false if name.includes('resin_supervisor')
return true
module.exports =
selectContainerFromDevice: Promise.method (deviceIp) ->
filterOutSupervisorContainer: filterOutSupervisorContainer
selectContainerFromDevice: Promise.method (deviceIp, filterSupervisor = false) ->
docker = new Docker(host: deviceIp, port: 2375)
# 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}")

View File

@ -21,3 +21,4 @@ exports.promote = require('./promote')
exports.scan = require('./scan')
exports.ssh = require('./ssh')
exports.push = require('./push')
exports.stop = require('./stop')

View File

@ -0,0 +1,78 @@
###
Copyright 2017 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.
###
# 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.
#
module.exports =
signature: 'local stop [deviceIp]'
description: 'Stop a running container on a resinOS device'
help: '''
Examples:
$ resin local stop
$ resin local stop --app-name myapp
$ resin local stop --all
$ resin local stop 192.168.1.10
$ resin local stop 192.168.1.10 --app-name myapp
'''
options: [
signature: 'all'
boolean: true
description: 'stop all containers'
,
signature: 'app-name'
parameter: 'name'
description: 'name of container to stop'
alias: 'a'
]
action: (params, options, done) ->
Promise = require('bluebird')
chalk = require('chalk')
{ forms, config, ResinLocalDockerUtils } = require('resin-sync')
{ selectContainerFromDevice, filterOutSupervisorContainer } = require('./common')
Promise.try ->
if not params.deviceIp?
return forms.selectLocalResinOsDevice()
return params.deviceIp
.then (@deviceIp) =>
@docker = new ResinLocalDockerUtils(@deviceIp)
if options.all
# Only list running containers
return @docker.docker.listContainersAsync(all: false)
.filter(filterOutSupervisorContainer)
.then (containers) =>
Promise.map containers, ({ Names, Id }) =>
console.log(chalk.yellow.bold("* Stopping container #{Names[0]}"))
@docker.stopContainer(Id)
ymlConfig = config.load()
@appName = options['app-name'] ? ymlConfig['local_resinos']?['app-name']
@docker.checkForRunningContainer(@appName)
.then (isRunning) =>
if not isRunning
return selectContainerFromDevice(@deviceIp, true)
console.log(chalk.yellow.bold("* Stopping container #{@appName}"))
return @appName
.then (runningContainerName) =>
@docker.stopContainer(runningContainerName)

View File

@ -132,6 +132,7 @@ capitano.command(actions.local.promote)
capitano.command(actions.local.push)
capitano.command(actions.local.ssh)
capitano.command(actions.local.scan)
capitano.command(actions.local.stop)
update.notify()