UNPKG

1.92 kBJavaScriptView Raw
1import fs from 'fs'
2import Table from 'cli-table'
3import assign from 'object.assign'
4import values from 'object.values'
5import chalk from 'chalk'
6import request from 'request'
7import * as utils from '../utils'
8require('colors')
9
10let cmd = {
11 name: 'ls',
12 usage: `Usage: zombie-swarm ls [OPTIONS]
13
14List swarm nodes.
15
16OPTIONS
17`,
18 options: utils.defaultOptions.concat([
19 {
20 name: 'out-file',
21 help: 'File to dump the discovered swarm nodes (.json - can be used as plan input)'
22 },
23 {
24 name: 'query',
25 default: 1000,
26 help: 'How long to query (ms)'
27 }
28 ]),
29 command: function(args) {
30 utils.assignZombieRC(args)
31 utils.initCmd(args)
32 utils.validateArgs(args)
33 utils.querySwarmNodes((nodes) => {
34 if (args['out-file']) fs.writeFileSync(args['out-file'], JSON.stringify(nodes, null, 2))
35 let table = makeTable(nodes, args)
36 console.log(table.toString())
37 if (args['out-file']) console.log(`# Wrote config to file: ${args['out-file']}`)
38 process.exit()
39 }, args, args.query)
40 }
41}
42
43function makeTable(nodes, args) {
44 let formatted = nodes.map((node) => {
45 return {
46 node: node.hostname,
47 swarm: node.swarm,
48 tags: node.tags.join(','),
49 ip: node.ip,
50 engines: node.engines.join(','),
51 memory: node.memory,
52 cpus: `${node.cpus.length} x ${node.cpus[0].speed}`
53 }
54 })
55
56 let table = new Table({
57 head: Object.keys(formatted[0]).map(h => h.green),
58 chars: { 'top': '' , 'top-mid': '' , 'top-left': '' , 'top-right': ''
59 , 'bottom': '' , 'bottom-mid': '' , 'bottom-left': '' , 'bottom-right': ''
60 , 'left': '' , 'left-mid': '' , 'mid': '' , 'mid-mid': ''
61 , 'right': '' , 'right-mid': '' , 'middle': ' ' },
62 style: { 'padding-left': 0, 'padding-right': 0 }
63 })
64 formatted.forEach(f => { table.push(values(f)) })
65 return table
66}
67
68export { cmd as default }