diff --git a/package-lock.json b/package-lock.json index 65d78652a..f9e242217 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1,6 +1,6 @@ { "name": "abstruse", - "version": "1.4.5", + "version": "1.5.0", "lockfileVersion": 1, "requires": true, "dependencies": { diff --git a/src/api/docker-stats.ts b/src/api/docker-stats.ts index 076f3e68d..9fea1c794 100644 --- a/src/api/docker-stats.ts +++ b/src/api/docker-stats.ts @@ -12,7 +12,7 @@ export function getContainersStats(): Observable { return listContainers() .then(containers => Promise.all(containers.map(c => getContainerStats(c)))); }) - .map(stats => observer.next({ type: 'containersStats', data: stats })) + .map(stats => observer.next({ type: 'containersStats', data: stats.filter(Boolean) })) .subscribe(); return () => { @@ -25,14 +25,18 @@ export function getContainersStats(): Observable { function getContainerStats(container: any): Promise { return calculateContainerStats(container, processes).then(stats => { - return { - id: stats.id, - name: stats.name, - cpu: getCpuData(stats.data), - network: getNetworkData(stats.data), - memory: getMemory(stats.data), - debug: stats.debug - }; + if (stats) { + return { + id: stats.id, + name: stats.name, + cpu: getCpuData(stats.data), + network: getNetworkData(stats.data), + memory: getMemory(stats.data), + debug: stats.debug + }; + } else { + return null; + } }); } diff --git a/src/api/docker.ts b/src/api/docker.ts index 8078ec44e..c257b0159 100644 --- a/src/api/docker.ts +++ b/src/api/docker.ts @@ -8,8 +8,10 @@ import { ProcessOutput } from './process'; import * as envVars from './env-variables'; import chalk from 'chalk'; import * as style from 'ansi-styles'; +import { platform } from 'os'; -export let docker = new dockerode(); +export const docker = new dockerode(); +const binds = platform() === 'darwin' ? [] : ['/var/run/docker.sock:/var/run/docker.sock']; export function createContainer( name: string, @@ -24,7 +26,7 @@ export function createContainer( OpenStdin: true, StdinOnce: false, Env: envVars.serialize(envs) || [], - Binds: ['/var/run/docker.sock:/var/run/docker.sock'], + Binds: binds, Privileged: true, ExposedPorts: { '22/tcp': {}, @@ -290,37 +292,23 @@ export function calculateContainerStats( container: dockerode.ContainerInfo, processes: any ): Promise { - return docker.getContainer(container.Id).stats() - .then(stream => { - let json = ''; - return new Promise((resolve, reject) => { - stream.on('data', buf => { - let rawJson = json + buf.toString(); - try { - let data = JSON.parse(rawJson); - - if (data && data.precpu_stats.system_cpu_usage) { - let jobId = container.Names[0].split('_')[2] || -1; - let job = processes.find(p => p.job_id === Number(jobId)); - let debug = false; - if (job) { - debug = job.debug || false; - } - - let stats = { - id: container.Id, - name: container.Names[0].substr(1) || '', - debug: debug, - data: data - }; + return docker.getContainer(container.Id).stats({ stream: false }) + .then(stats => { + const data = stats; + if (data && data.precpu_stats.system_cpu_usage) { + const jobId = container.Names[0].split('_')[2] || -1; + const job = processes.find(p => p.job_id === Number(jobId)); + const debug = job && job.debug || false; + const stats = { + id: container.Id, + name: container.Names[0].substr(1) || '', + debug: debug, + data: data + }; - stream.destroy(); - resolve(stats); - } - } catch (e) { - json = rawJson; - } - }); - }); + return stats; + } else { + return null; + } }); }