From 1a71bad8bbcd231b886fa6f406ca29ee42deae42 Mon Sep 17 00:00:00 2001 From: Pagan Gazzard Date: Wed, 25 Mar 2020 13:16:32 +0000 Subject: [PATCH] Convert lib/actions/local/common.coffee to javascript Change-type: patch --- lib/actions/local/common.coffee | 60 -------------------- lib/actions/local/common.js | 97 +++++++++++++++++++++++++++++++++ lib/utils/docker-coffee.d.ts | 5 ++ 3 files changed, 102 insertions(+), 60 deletions(-) delete mode 100644 lib/actions/local/common.coffee create mode 100644 lib/actions/local/common.js diff --git a/lib/actions/local/common.coffee b/lib/actions/local/common.coffee deleted file mode 100644 index 210ccb34..00000000 --- a/lib/actions/local/common.coffee +++ /dev/null @@ -1,60 +0,0 @@ -Promise = require('bluebird') -_ = require('lodash') - -dockerUtils = require('../../utils/docker') -{ exitWithExpectedError } = require('../../utils/patterns') -{ getChalk } = require('../../utils/lazy') - -exports.dockerPort = dockerPort = 2375 -exports.dockerTimeout = dockerTimeout = 2000 - -exports.filterOutSupervisorContainer = filterOutSupervisorContainer = (container) -> - for name in container.Names - return false if (name.includes('resin_supervisor') or name.includes('balena_supervisor')) - return true - -exports.selectContainerFromDevice = Promise.method (deviceIp, filterSupervisor = false) -> - form = require('resin-cli-form') - docker = dockerUtils.createClient(host: deviceIp, port: dockerPort, timeout: dockerTimeout) - - # 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) - exitWithExpectedError("No containers found in #{deviceIp}") - - 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) - - return { - name: "#{containerName} (#{shortContainerId})" - value: container.Id - } - -exports.pipeContainerStream = Promise.method ({ deviceIp, name, outStream, follow = false }) -> - docker = dockerUtils.createClient(host: deviceIp, port: dockerPort) - - 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(getChalk().red.bold("Container '#{name}' not found.")) - throw err diff --git a/lib/actions/local/common.js b/lib/actions/local/common.js new file mode 100644 index 00000000..b534cdd1 --- /dev/null +++ b/lib/actions/local/common.js @@ -0,0 +1,97 @@ +import * as Promise from 'bluebird'; +import * as _ from 'lodash'; +import * as dockerUtils from '../../utils/docker'; +import { exitWithExpectedError } from '../../utils/patterns'; +import { getChalk } from '../../utils/lazy'; + +export const dockerPort = 2375; +export const dockerTimeout = 2000; + +export const filterOutSupervisorContainer = function(container) { + for (const name of container.Names) { + if ( + name.includes('resin_supervisor') || + name.includes('balena_supervisor') + ) { + return false; + } + } + return true; +}; + +export const selectContainerFromDevice = Promise.method(function( + deviceIp, + filterSupervisor, +) { + if (filterSupervisor == null) { + filterSupervisor = false; + } + const form = require('resin-cli-form'); + const docker = dockerUtils.createClient({ + host: deviceIp, + port: dockerPort, + timeout: dockerTimeout, + }); + + // List all containers, including those not running + return docker.listContainers({ all: true }).then(function(containers) { + containers = containers.filter(function(container) { + if (!filterSupervisor) { + return true; + } + return filterOutSupervisorContainer(container); + }); + if (_.isEmpty(containers)) { + exitWithExpectedError(`No containers found in ${deviceIp}`); + } + + return form.ask({ + message: 'Select a container', + type: 'list', + choices: _.map(containers, function(container) { + const containerName = container.Names?.[0] || 'Untitled'; + const shortContainerId = ('' + container.Id).substr(0, 11); + + return { + name: `${containerName} (${shortContainerId})`, + value: container.Id, + }; + }), + }); + }); +}); + +export const pipeContainerStream = Promise.method(function({ + deviceIp, + name, + outStream, + follow, +}) { + if (follow == null) { + follow = false; + } + const docker = dockerUtils.createClient({ host: deviceIp, port: dockerPort }); + + const container = docker.getContainer(name); + return container + .inspect() + .then(containerInfo => containerInfo?.State?.Running) + .then(isRunning => + container.attach({ + logs: !follow || !isRunning, + stream: follow && isRunning, + stdout: true, + stderr: true, + }), + ) + .then(containerStream => containerStream.pipe(outStream)) + .catch(function(err) { + err = '' + err.statusCode; + if (err === '404') { + return console.log( + getChalk().red.bold(`Container '${name}' not found.`), + ); + } + throw err; + }); +}); diff --git a/lib/utils/docker-coffee.d.ts b/lib/utils/docker-coffee.d.ts index 096ba97c..e46f8f89 100644 --- a/lib/utils/docker-coffee.d.ts +++ b/lib/utils/docker-coffee.d.ts @@ -25,8 +25,13 @@ export interface BuildDockerOptions { docker?: string; // dockerode DockerOptions.socketPath dockerHost?: string; // dockerode DockerOptions.host dockerPort?: number; // dockerode DockerOptions.port + host?: string; + port?: number; + timeout?: number; } export function getDocker( options: BuildDockerOptions, ): Bluebird; + +export function createClient(options: BuildDockerOptions): DockerToolbelt;