|
| 1 | +const chalk = require('chalk') |
| 2 | +const path = require('path') |
| 3 | +const { reporter, exec, tryCatchAsync } = require('@dhis2/cli-helpers-engine') |
| 4 | +const { makeComposeProject, listClusters } = require('../common') |
| 5 | +const Table = require('cli-table3') |
| 6 | + |
| 7 | +const getStatus = async cluster => |
| 8 | + // TODO: check the status of the other services, not just `core` |
| 9 | + await exec({ |
| 10 | + cmd: 'docker', |
| 11 | + args: [ |
| 12 | + 'ps', |
| 13 | + '--filter', |
| 14 | + `name=${makeComposeProject(cluster.name)}_core`, |
| 15 | + '--format', |
| 16 | + '{{.Status}}', |
| 17 | + ], |
| 18 | + pipe: false, |
| 19 | + captureOut: true, |
| 20 | + }) |
| 21 | + |
| 22 | +const formatStatus = status => { |
| 23 | + status = status.trim() |
| 24 | + |
| 25 | + if (status.length === 0) { |
| 26 | + return chalk.grey('Down') |
| 27 | + } else if (/\(Paused\)$/.test(status)) { |
| 28 | + return chalk.cyan(status) |
| 29 | + } else if (/^Up \d+/.test(status)) { |
| 30 | + return chalk.green(status) |
| 31 | + } else { |
| 32 | + return chalk.yellow(status) |
| 33 | + } |
| 34 | +} |
| 35 | + |
| 36 | +const run = async function(argv) { |
| 37 | + const clusters = await listClusters(argv) |
| 38 | + |
| 39 | + const table = new Table({ |
| 40 | + head: [ |
| 41 | + 'Name', |
| 42 | + 'Port', |
| 43 | + 'Channel', |
| 44 | + 'DHIS2 Version', |
| 45 | + 'DB Version', |
| 46 | + 'Status', |
| 47 | + ], |
| 48 | + }) |
| 49 | + |
| 50 | + await Promise.all( |
| 51 | + clusters.map(async cluster => { |
| 52 | + const status = await getStatus(cluster) |
| 53 | + cluster.status = formatStatus(status) |
| 54 | + }) |
| 55 | + ) |
| 56 | + |
| 57 | + clusters.forEach(cluster => |
| 58 | + table.push([ |
| 59 | + chalk.blue(cluster.name), |
| 60 | + cluster.port, |
| 61 | + cluster.channel, |
| 62 | + cluster.dhis2Version, |
| 63 | + cluster.dbVersion, |
| 64 | + cluster.status, |
| 65 | + ]) |
| 66 | + ) |
| 67 | + |
| 68 | + reporter.print(table) |
| 69 | +} |
| 70 | + |
| 71 | +module.exports = { |
| 72 | + command: 'list', |
| 73 | + desc: 'List all active cluster configurations', |
| 74 | + aliases: 'ls', |
| 75 | + handler: run, |
| 76 | +} |
0 commit comments