mirror of
https://github.com/balena-io/balena-cli.git
synced 2025-02-20 17:33:18 +00:00
Merge pull request #1677 from balena-io/js-lib-action-local-common
Convert lib/actions/local/common.coffee to javascript
This commit is contained in:
commit
45e7e9cb32
@ -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
|
97
lib/actions/local/common.js
Normal file
97
lib/actions/local/common.js
Normal file
@ -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;
|
||||
});
|
||||
});
|
5
lib/utils/docker-coffee.d.ts
vendored
5
lib/utils/docker-coffee.d.ts
vendored
@ -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<DockerToolbelt>;
|
||||
|
||||
export function createClient(options: BuildDockerOptions): DockerToolbelt;
|
||||
|
Loading…
x
Reference in New Issue
Block a user