swarmerode
Advanced tools
Comparing version 3.0.0 to 3.1.0
67
index.js
@@ -62,2 +62,3 @@ 'use strict' | ||
* Host: '10.0.0.1:4242, | ||
* Status: 'Healthy', | ||
* Containers: 5, | ||
@@ -75,2 +76,3 @@ * ReservedCpus: '0 / 1', | ||
* } | ||
* nodeHostname should be in format similar to ip-10.20.10.12 or `(unknown)` | ||
* @param {Array} - array data response from swarm info | ||
@@ -89,20 +91,51 @@ * @return array of json objects with swarm data for each node | ||
} | ||
for (var i = 0; i < formatted.Nodes; i++) { | ||
try { | ||
formatted.ParsedNodes[systemStatus[0][0].trim()] = { | ||
Host: systemStatus.shift()[1], | ||
ID: systemStatus.shift()[1], | ||
Status: systemStatus.shift()[1], | ||
Containers: parseInt(systemStatus.shift()[1], 10), | ||
ReservedCpus: systemStatus.shift()[1], | ||
ReservedMem: systemStatus.shift()[1], | ||
Labels: parseLabels(systemStatus.shift()[1]), | ||
UpdatedAt: systemStatus.shift()[1], | ||
ServerVersion: systemStatus.shift()[1] | ||
var parsedNodes = systemStatus.reduce( | ||
function (nodesArray, itemPair) { | ||
var key = itemPair[0].toLowerCase().trim() | ||
var value = itemPair[1] | ||
// when status is pending node will have no IP but will have `(unknown)` key | ||
if (key.indexOf('ip-') === 0 || key === '(unknown)') { | ||
nodesArray.push({ | ||
Host: value | ||
}) | ||
} else { | ||
// updated the last node | ||
var normalizedKey = null | ||
if (key.indexOf('id') > -1) { | ||
normalizedKey = 'ID' | ||
} else if (key.indexOf('status') > -1) { | ||
normalizedKey = 'Status' | ||
} else if (key.indexOf('containers') > -1) { | ||
normalizedKey = 'Containers' | ||
} else if (key.indexOf('reserved cpus') > -1) { | ||
normalizedKey = 'ReservedCpus' | ||
} else if (key.indexOf('reserved memory') > -1) { | ||
normalizedKey = 'ReservedMem' | ||
} else if (key.indexOf('error') > -1) { | ||
normalizedKey = 'Error' | ||
} else if (key.indexOf('updatedat') > -1) { | ||
normalizedKey = 'UpdatedAt' | ||
} else if (key.indexOf('labels') > -1) { | ||
normalizedKey = 'Labels' | ||
} else if (key.indexOf('ServerVersion') > -1) { | ||
normalizedKey = 'ServerVersion' | ||
} | ||
if (normalizedKey && nodesArray.length > 0 && !nodesArray[nodesArray.length - 1][normalizedKey]) { | ||
if (normalizedKey === 'Labels') { | ||
nodesArray[nodesArray.length - 1][normalizedKey] = parseLabels(value) | ||
} else { | ||
nodesArray[nodesArray.length - 1][normalizedKey] = value | ||
} | ||
} | ||
} | ||
} catch (err) { | ||
debug('ERROR - invalid node', err.message) | ||
} | ||
} | ||
return nodesArray | ||
}, | ||
[]) | ||
// convert array to object indexed by host | ||
formatted.ParsedNodes = parsedNodes.reduce( | ||
function (obj, item) { | ||
obj[item.Host] = item | ||
return obj | ||
}, | ||
{}) | ||
@@ -109,0 +142,0 @@ function parseLabels (labelString) { |
{ | ||
"name": "swarmerode", | ||
"version": "3.0.0", | ||
"version": "3.1.0", | ||
"description": "Swarm Client Extension for Dockerode", | ||
@@ -39,3 +39,3 @@ "main": "index.js", | ||
"dependencies": { | ||
"101": "^1.2.0", | ||
"101": "^1.6.1", | ||
"bluebird": "^3.3.5", | ||
@@ -42,0 +42,0 @@ "debug": "^2.2.0", |
@@ -5,3 +5,3 @@ 'use strict' | ||
function hostEntry (hostInfo) { | ||
function hostEntry (hostInfo, isError) { | ||
var hostIndex = index++ | ||
@@ -23,6 +23,6 @@ var labelString = [ | ||
return [ | ||
var node = [ | ||
[ nodeName, host ], | ||
[ ' └ ID', '' + Math.random() ], | ||
[ ' └ Status', 'Healthy' ], | ||
[ ' └ Status', isError ? 'Pending' : 'Healthy' ], | ||
[ ' └ Containers', numContainers ], | ||
@@ -35,5 +35,9 @@ [ ' └ Reserved CPUs', '0 / 1' ], | ||
] | ||
if (isError) { | ||
node.push([ ' └ Error', 'Docker daemon is unavailable' ]) | ||
} | ||
return node | ||
} | ||
module.exports = function (testHosts) { | ||
module.exports = function (testHosts, isError) { | ||
if (!Array.isArray(testHosts) || testHosts.length <= 0) { | ||
@@ -49,3 +53,3 @@ throw new Error('swarm-info requires a list of hosts') | ||
testHosts.forEach(function (hostInfo) { | ||
SystemStatus.push.apply(SystemStatus, hostEntry(hostInfo)) | ||
SystemStatus.push.apply(SystemStatus, hostEntry(hostInfo, isError)) | ||
}) | ||
@@ -52,0 +56,0 @@ |
@@ -160,3 +160,3 @@ 'use strict' | ||
describe('_parseSwarmSystemStatus', function () { | ||
it('should format SystemStatus correctly', function (done) { | ||
it('should not parse node that has incorrect nodename', function (done) { | ||
var coolNode = { | ||
@@ -182,41 +182,64 @@ Labels: 'env=test, hd=ssd', | ||
assert.equal(out.Nodes, 2) | ||
assert.equal(Object.keys(out.ParsedNodes).length, 0) | ||
done() | ||
}) | ||
assert.equal(out.ParsedNodes['cool.node'].Host, coolNode.host) | ||
assert.isNumber(out.ParsedNodes['cool.node'].Containers) | ||
assert.isString(out.ParsedNodes['cool.node'].ID) | ||
assert.equal(out.ParsedNodes['cool.node'].Containers, 100) | ||
assert.equal(out.ParsedNodes['cool.node'].Status, 'Healthy') | ||
assert.equal(out.ParsedNodes['cool.node'].ReservedCpus, '0 / 1') | ||
assert.equal(out.ParsedNodes['cool.node'].ReservedMem, '10 GiB / 1.021 GiB') | ||
assert.equal(out.ParsedNodes['cool.node'].UpdatedAt, '2016-03-08T19:02:41Z') | ||
assert.equal(out.ParsedNodes['cool.node'].Labels.env, 'test') | ||
assert.equal(out.ParsedNodes['cool.node'].Labels.hd, 'ssd') | ||
it('should parse node with `(unknown)` nodename', function (done) { | ||
var firstNode = { | ||
Labels: 'env=test, hd=ssd', | ||
Containers: 100, | ||
nodeName: '(unknown)', | ||
host: '10.42.42.42:4242' | ||
} | ||
var secondNode = { | ||
Containers: 4, | ||
nodeName: '(unknown)', | ||
host: '10.7.7.7:4242' | ||
} | ||
var testHosts = swarmInfoMock([firstNode, secondNode]) | ||
delete testHosts.SystemStatus[18] | ||
var out = Swarmerode._Swarmerode._parseSwarmSystemStatus(testHosts.SystemStatus) | ||
assert.equal(out.Role, 'primary') | ||
assert.equal(out.Strategy, 'spread') | ||
assert.equal(out.Filters, 'health, port, dependency, affinity, constraint') | ||
assert.isNumber(out.Nodes) | ||
assert.equal(out.Nodes, 2) | ||
assert.equal(Object.keys(out.ParsedNodes).length, 2) | ||
assert.equal(out.ParsedNodes['10.42.42.42:4242'].Host, firstNode.host) | ||
assert.isNumber(out.ParsedNodes['10.42.42.42:4242'].Containers) | ||
assert.isString(out.ParsedNodes['10.42.42.42:4242'].ID) | ||
assert.equal(out.ParsedNodes['10.42.42.42:4242'].Containers, 100) | ||
assert.equal(out.ParsedNodes['10.42.42.42:4242'].Status, 'Healthy') | ||
assert.equal(out.ParsedNodes['10.42.42.42:4242'].ReservedCpus, '0 / 1') | ||
assert.equal(out.ParsedNodes['10.42.42.42:4242'].ReservedMem, '10 GiB / 1.021 GiB') | ||
assert.equal(out.ParsedNodes['10.42.42.42:4242'].UpdatedAt, '2016-03-08T19:02:41Z') | ||
assert.equal(out.ParsedNodes['10.42.42.42:4242'].Labels.env, 'test') | ||
assert.equal(out.ParsedNodes['10.42.42.42:4242'].Labels.hd, 'ssd') | ||
assert.equal(out.ParsedNodes['un.cool.node'].Host, uncoolNode.host) | ||
assert.isNumber(out.ParsedNodes['un.cool.node'].Containers) | ||
assert.isString(out.ParsedNodes['un.cool.node'].ID) | ||
assert.equal(out.ParsedNodes['un.cool.node'].Status, 'Healthy') | ||
assert.equal(out.ParsedNodes['un.cool.node'].Containers, 4) | ||
assert.equal(out.ParsedNodes['un.cool.node'].ReservedCpus, '0 / 1') | ||
assert.equal(out.ParsedNodes['un.cool.node'].ReservedMem, '10 GiB / 1.021 GiB') | ||
assert.equal(out.ParsedNodes['un.cool.node'].UpdatedAt, '2016-03-08T19:02:41Z') | ||
assert.equal(out.ParsedNodes['un.cool.node'].Labels.env, 'prod') | ||
assert.equal(out.ParsedNodes['un.cool.node'].Labels.hd, 'disk') | ||
assert.equal(out.ParsedNodes['10.7.7.7:4242'].Host, secondNode.host) | ||
assert.isNumber(out.ParsedNodes['10.7.7.7:4242'].Containers) | ||
assert.isString(out.ParsedNodes['10.7.7.7:4242'].ID) | ||
assert.equal(out.ParsedNodes['10.7.7.7:4242'].Containers, 4) | ||
assert.equal(out.ParsedNodes['10.7.7.7:4242'].Status, 'Healthy') | ||
assert.equal(out.ParsedNodes['10.7.7.7:4242'].ReservedCpus, '0 / 1') | ||
assert.isUndefined(out.ParsedNodes['10.7.7.7:4242'].ReservedMem) | ||
assert.equal(out.ParsedNodes['10.7.7.7:4242'].UpdatedAt, '2016-03-08T19:02:41Z') | ||
done() | ||
}) | ||
it('should only return correctly formated nodes', function (done) { | ||
var coolNode = { | ||
it('should parse node with `(unknown)` nodename and error', function (done) { | ||
var firstNode = { | ||
Labels: 'env=test, hd=ssd', | ||
Containers: 100, | ||
nodeName: ' cool.node', | ||
nodeName: '(unknown)', | ||
host: '10.42.42.42:4242' | ||
} | ||
var uncoolNode = { | ||
var secondNode = { | ||
Containers: 4, | ||
nodeName: ' un.cool.node', | ||
nodeName: '(unknown)', | ||
host: '10.7.7.7:4242' | ||
} | ||
var testHosts = swarmInfoMock([coolNode, uncoolNode]) | ||
delete testHosts.SystemStatus[18] | ||
var isError = true | ||
var testHosts = swarmInfoMock([firstNode, secondNode], isError) | ||
var out = Swarmerode._Swarmerode._parseSwarmSystemStatus(testHosts.SystemStatus) | ||
@@ -228,16 +251,24 @@ assert.equal(out.Role, 'primary') | ||
assert.equal(out.Nodes, 2) | ||
assert.equal(Object.keys(out.ParsedNodes).length, 2) | ||
assert.equal(out.ParsedNodes['10.42.42.42:4242'].Host, firstNode.host) | ||
assert.isNumber(out.ParsedNodes['10.42.42.42:4242'].Containers) | ||
assert.isString(out.ParsedNodes['10.42.42.42:4242'].ID) | ||
assert.equal(out.ParsedNodes['10.42.42.42:4242'].Containers, 100) | ||
assert.equal(out.ParsedNodes['10.42.42.42:4242'].Status, 'Pending') | ||
assert.equal(out.ParsedNodes['10.42.42.42:4242'].Error, 'Docker daemon is unavailable') | ||
assert.equal(out.ParsedNodes['10.42.42.42:4242'].ReservedCpus, '0 / 1') | ||
assert.equal(out.ParsedNodes['10.42.42.42:4242'].ReservedMem, '10 GiB / 1.021 GiB') | ||
assert.equal(out.ParsedNodes['10.42.42.42:4242'].UpdatedAt, '2016-03-08T19:02:41Z') | ||
assert.equal(out.ParsedNodes['10.42.42.42:4242'].Labels.env, 'test') | ||
assert.equal(out.ParsedNodes['10.42.42.42:4242'].Labels.hd, 'ssd') | ||
assert.equal(out.ParsedNodes['cool.node'].Host, coolNode.host) | ||
assert.isNumber(out.ParsedNodes['cool.node'].Containers) | ||
assert.isString(out.ParsedNodes['cool.node'].ID) | ||
assert.equal(out.ParsedNodes['cool.node'].Containers, 100) | ||
assert.equal(out.ParsedNodes['cool.node'].Status, 'Healthy') | ||
assert.equal(out.ParsedNodes['cool.node'].ReservedCpus, '0 / 1') | ||
assert.equal(out.ParsedNodes['cool.node'].ReservedMem, '10 GiB / 1.021 GiB') | ||
assert.equal(out.ParsedNodes['cool.node'].UpdatedAt, '2016-03-08T19:02:41Z') | ||
assert.equal(out.ParsedNodes['cool.node'].Labels.env, 'test') | ||
assert.equal(out.ParsedNodes['cool.node'].Labels.hd, 'ssd') | ||
assert.equal(out.ParsedNodes['10.7.7.7:4242'].Host, secondNode.host) | ||
assert.isNumber(out.ParsedNodes['10.7.7.7:4242'].Containers) | ||
assert.isString(out.ParsedNodes['10.7.7.7:4242'].ID) | ||
assert.equal(out.ParsedNodes['10.7.7.7:4242'].Containers, 4) | ||
assert.equal(out.ParsedNodes['10.7.7.7:4242'].Status, 'Pending') | ||
assert.equal(out.ParsedNodes['10.7.7.7:4242'].Error, 'Docker daemon is unavailable') | ||
assert.equal(out.ParsedNodes['10.7.7.7:4242'].ReservedCpus, '0 / 1') | ||
assert.equal(out.ParsedNodes['10.7.7.7:4242'].UpdatedAt, '2016-03-08T19:02:41Z') | ||
assert.isUndefined(out.ParsedNodes['un.cool.node']) | ||
done() | ||
@@ -244,0 +275,0 @@ }) |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
27243
671
Updated101@^1.6.1