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

83 lines
2.5 KiB
CoffeeScript
Raw Normal View History

2017-03-09 19:14:25 +00:00
###
Copyright 2017 Balena
2017-03-09 19:14:25 +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.
###
# 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.
#
2020-02-28 15:25:12 +00:00
{ getChalk } = require('../../utils/lazy')
2017-03-09 19:14:25 +00:00
module.exports =
signature: 'local stop [deviceIp]'
description: 'Stop a running container on a balenaOS device'
2017-03-09 19:14:25 +00:00
help: '''
Examples:
$ balena local stop
$ balena local stop --app-name myapp
$ balena local stop --all
$ balena local stop 192.168.1.10
$ balena local stop 192.168.1.10 --app-name myapp
2017-03-09 19:14:25 +00:00
'''
options: [
signature: 'all'
boolean: true
description: 'stop all containers'
,
signature: 'app-name'
parameter: 'name'
description: 'name of container to stop'
alias: 'a'
]
root: true
action: (params, options) ->
2017-03-09 19:14:25 +00:00
Promise = require('bluebird')
2020-02-28 15:25:12 +00:00
chalk = getChalk()
{ forms, config, BalenaLocalDockerUtils } = require('balena-sync')
2017-03-09 19:14:25 +00:00
{ selectContainerFromDevice, filterOutSupervisorContainer } = require('./common')
Promise.try ->
if not params.deviceIp?
return forms.selectLocalBalenaOsDevice()
2017-03-09 19:14:25 +00:00
return params.deviceIp
.then (@deviceIp) =>
@docker = new BalenaLocalDockerUtils(@deviceIp)
2017-03-09 19:14:25 +00:00
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_balenaos']?['app-name']
2017-03-09 19:14:25 +00:00
@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)