From 7ad468dc545b837554f86ced020e42b7f8f55665 Mon Sep 17 00:00:00 2001 From: Kostas Lekkas Date: Thu, 9 Mar 2017 19:14:25 +0000 Subject: [PATCH] Implement 'resin local stop' --- lib/actions/local/common.coffee | 12 ++++- lib/actions/local/index.coffee | 1 + lib/actions/local/stop.coffee | 78 +++++++++++++++++++++++++++++++++ lib/app.coffee | 1 + 4 files changed, 91 insertions(+), 1 deletion(-) create mode 100644 lib/actions/local/stop.coffee diff --git a/lib/actions/local/common.coffee b/lib/actions/local/common.coffee index 897e7a26..3691d58e 100644 --- a/lib/actions/local/common.coffee +++ b/lib/actions/local/common.coffee @@ -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}") diff --git a/lib/actions/local/index.coffee b/lib/actions/local/index.coffee index 628dbb1e..01c37969 100644 --- a/lib/actions/local/index.coffee +++ b/lib/actions/local/index.coffee @@ -21,3 +21,4 @@ exports.promote = require('./promote') exports.scan = require('./scan') exports.ssh = require('./ssh') exports.push = require('./push') +exports.stop = require('./stop') diff --git a/lib/actions/local/stop.coffee b/lib/actions/local/stop.coffee new file mode 100644 index 00000000..0122b1cf --- /dev/null +++ b/lib/actions/local/stop.coffee @@ -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) diff --git a/lib/app.coffee b/lib/app.coffee index ac98d490..5cb1b164 100644 --- a/lib/app.coffee +++ b/lib/app.coffee @@ -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()