2017-03-08 18:45:36 +00:00
|
|
|
Promise = require('bluebird')
|
|
|
|
_ = require('lodash')
|
|
|
|
chalk = require('chalk')
|
|
|
|
|
2017-12-06 21:16:41 +00:00
|
|
|
dockerUtils = require('../../utils/docker')
|
2018-04-17 13:17:48 +00:00
|
|
|
{ exitWithExpectedError } = require('../../utils/patterns')
|
2017-12-06 21:16:41 +00:00
|
|
|
|
2017-03-10 15:11:27 +00:00
|
|
|
exports.dockerPort = dockerPort = 2375
|
|
|
|
exports.dockerTimeout = dockerTimeout = 2000
|
|
|
|
|
|
|
|
exports.filterOutSupervisorContainer = filterOutSupervisorContainer = (container) ->
|
2017-03-09 19:14:25 +00:00
|
|
|
for name in container.Names
|
2018-10-19 14:38:50 +00:00
|
|
|
return false if (name.includes('resin_supervisor') or name.includes('balena_supervisor'))
|
2017-03-09 19:14:25 +00:00
|
|
|
return true
|
|
|
|
|
2017-03-10 15:11:27 +00:00
|
|
|
exports.selectContainerFromDevice = Promise.method (deviceIp, filterSupervisor = false) ->
|
2019-01-11 17:52:06 +00:00
|
|
|
form = require('resin-cli-form')
|
2017-12-06 21:16:41 +00:00
|
|
|
docker = dockerUtils.createClient(host: deviceIp, port: dockerPort, timeout: dockerTimeout)
|
2017-03-08 18:45:36 +00:00
|
|
|
|
2017-03-10 15:11:27 +00:00
|
|
|
# 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)
|
2018-04-17 13:17:48 +00:00
|
|
|
exitWithExpectedError("No containers found in #{deviceIp}")
|
2017-03-08 18:45:36 +00:00
|
|
|
|
2017-03-10 15:11:27 +00:00
|
|
|
return form.ask
|
|
|
|
message: 'Select a container'
|
|
|
|
type: 'list'
|
|
|
|
choices: _.map containers, (container) ->
|
2018-06-28 10:03:44 +00:00
|
|
|
containerName = container.Names?[0] or 'Untitled'
|
2017-03-10 15:11:27 +00:00
|
|
|
shortContainerId = ('' + container.Id).substr(0, 11)
|
2017-03-08 18:45:36 +00:00
|
|
|
|
2017-03-10 15:11:27 +00:00
|
|
|
return {
|
2018-04-13 16:18:56 +00:00
|
|
|
name: "#{containerName} (#{shortContainerId})"
|
2017-03-10 15:11:27 +00:00
|
|
|
value: container.Id
|
|
|
|
}
|
2017-03-08 18:45:36 +00:00
|
|
|
|
2017-03-10 15:11:27 +00:00
|
|
|
exports.pipeContainerStream = Promise.method ({ deviceIp, name, outStream, follow = false }) ->
|
2017-12-06 21:16:41 +00:00
|
|
|
docker = dockerUtils.createClient(host: deviceIp, port: dockerPort)
|
2017-03-08 18:45:36 +00:00
|
|
|
|
2017-03-10 15:11:27 +00:00
|
|
|
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
|
2017-03-08 18:45:36 +00:00
|
|
|
|
2017-05-22 18:49:34 +00:00
|
|
|
exports.getSubShellCommand = require('../../utils/helpers').getSubShellCommand
|