From c59dbd392b03a13a5357ed845136df878ff69ec7 Mon Sep 17 00:00:00 2001 From: Austin McGee <947888+amcgee@users.noreply.github.com> Date: Sat, 24 Aug 2019 13:33:35 +0200 Subject: [PATCH 1/3] feat: add cluster list commmand --- packages/cluster/src/commands/list.js | 68 +++++++++++++++++++++++++++ packages/cluster/src/common.js | 10 +++- 2 files changed, 77 insertions(+), 1 deletion(-) create mode 100644 packages/cluster/src/commands/list.js diff --git a/packages/cluster/src/commands/list.js b/packages/cluster/src/commands/list.js new file mode 100644 index 00000000..83a4855b --- /dev/null +++ b/packages/cluster/src/commands/list.js @@ -0,0 +1,68 @@ +const chalk = require('chalk') +const path = require('path') +const { reporter, exec, tryCatchAsync } = require('@dhis2/cli-helpers-engine') +const { makeComposeProject, listClusters } = require('../common') +const Table = require('cli-table3') + +const run = async function(argv) { + const clusters = await listClusters(argv) + + const table = new Table({ + head: [ + 'Name', + 'Port', + 'Channel', + 'DHIS2 Version', + 'DB Version', + 'Status', + ], + }) + + await Promise.all( + clusters.map(async cluster => { + let status = await exec({ + cmd: 'docker', + args: [ + 'ps', + '--filter', + `name=${makeComposeProject(cluster.name)}_core`, + '--format', + '{{.Status}}', + ], + pipe: false, + captureOut: true, + }) + + if (status.length === 0) { + status = chalk.grey('Down') + } else if (/\(Paused\)/.test(status)) { + status = chalk.cyan(status) + } else if (/$Up \d+/.test(status)) { + status = chalk.green(status) + } else { + status = chalk.yellow(status) + } + cluster.status = status + }) + ) + + clusters.forEach(cluster => + table.push([ + chalk.blue(cluster.name), + cluster.port, + cluster.channel, + cluster.dhis2Version, + cluster.dbVersion, + cluster.status.trim(), + ]) + ) + + reporter.print(table) +} + +module.exports = { + command: 'list', + desc: 'List all active cluster configurations', + aliases: 'ls', + handler: run, +} diff --git a/packages/cluster/src/common.js b/packages/cluster/src/common.js index 1bde7090..4e45b34a 100644 --- a/packages/cluster/src/common.js +++ b/packages/cluster/src/common.js @@ -164,7 +164,15 @@ module.exports.makeEnvironment = cfg => { module.exports.makeComposeProject = name => `d2-cluster-${name}` -module.exports.getLocalClusters = async () => {} +module.exports.listClusters = async argv => { + const cache = argv.getCache() + + const stat = await cache.stat(clusterDir) + const promises = Object.keys(stat.children) + .filter(name => cache.exists(path.join(clusterDir, name))) + .map(name => resolveConfiguration({ name, getCache: argv.getCache })) + return await Promise.all(promises) +} module.exports.makeDockerImage = makeDockerImage module.exports.resolveConfiguration = resolveConfiguration From 6905fe956dc8e9b7fc590223873cccb9d83337b7 Mon Sep 17 00:00:00 2001 From: Austin McGee <947888+amcgee@users.noreply.github.com> Date: Sat, 24 Aug 2019 13:39:17 +0200 Subject: [PATCH 2/3] chore(deps): upgrade to @dhis2/cli-helpers-engine@1.4.0 --- packages/app/package.json | 4 ++-- packages/cluster/package.json | 4 ++-- packages/create-app/package.json | 4 ++-- packages/create/package.json | 4 ++-- packages/create/templates/cli/package.json | 4 ++-- packages/main/package.json | 4 ++-- packages/utils/package.json | 4 ++-- yarn.lock | 20 ++++++++++++-------- 8 files changed, 26 insertions(+), 22 deletions(-) diff --git a/packages/app/package.json b/packages/app/package.json index 774b60c2..4ae1ccbd 100644 --- a/packages/app/package.json +++ b/packages/app/package.json @@ -12,9 +12,9 @@ "license": "BSD-3-Clause", "private": false, "dependencies": { - "@dhis2/cli-helpers-engine": "1.3.0" + "@dhis2/cli-helpers-engine": "1.4.0" }, "publishConfig": { "access": "public" } -} +} \ No newline at end of file diff --git a/packages/cluster/package.json b/packages/cluster/package.json index 46c3f140..17f5e77b 100644 --- a/packages/cluster/package.json +++ b/packages/cluster/package.json @@ -7,7 +7,7 @@ "author": "Austin McGee ", "license": "BSD-3-Clause", "dependencies": { - "@dhis2/cli-helpers-engine": "1.3.0", + "@dhis2/cli-helpers-engine": "1.4.0", "chalk": "^2.4.2" }, "bin": { @@ -16,4 +16,4 @@ "publishConfig": { "access": "public" } -} +} \ No newline at end of file diff --git a/packages/create-app/package.json b/packages/create-app/package.json index a16172bd..3080fd88 100644 --- a/packages/create-app/package.json +++ b/packages/create-app/package.json @@ -8,10 +8,10 @@ "license": "BSD-3-Clause", "dependencies": { "@dhis2/cli-create": "2.0.0", - "@dhis2/cli-helpers-engine": "1.3.0" + "@dhis2/cli-helpers-engine": "1.4.0" }, "bin": "./bin/d2-create-app", "publishConfig": { "access": "public" } -} +} \ No newline at end of file diff --git a/packages/create/package.json b/packages/create/package.json index 15bc16ec..0c8e22e5 100644 --- a/packages/create/package.json +++ b/packages/create/package.json @@ -9,7 +9,7 @@ "license": "BSD-3-Clause", "private": false, "dependencies": { - "@dhis2/cli-helpers-engine": "1.3.0", + "@dhis2/cli-helpers-engine": "1.4.0", "chalk": "^2.4.2", "fs-extra": "^8.1.0", "handlebars": "^4.1.0", @@ -18,4 +18,4 @@ "publishConfig": { "access": "public" } -} +} \ No newline at end of file diff --git a/packages/create/templates/cli/package.json b/packages/create/templates/cli/package.json index 4f20864e..9c9033b5 100644 --- a/packages/create/templates/cli/package.json +++ b/packages/create/templates/cli/package.json @@ -9,9 +9,9 @@ "license": "BSD-3-Clause", "private": false, "dependencies": { - "@dhis2/cli-helpers-engine": "1.3.0" + "@dhis2/cli-helpers-engine": "1.4.0" }, "publishConfig": { "access": "public" } -} +} \ No newline at end of file diff --git a/packages/main/package.json b/packages/main/package.json index 85305336..8deccb58 100644 --- a/packages/main/package.json +++ b/packages/main/package.json @@ -15,7 +15,7 @@ "@dhis2/cli-app": "2.0.0", "@dhis2/cli-cluster": "2.0.0", "@dhis2/cli-create": "2.0.0", - "@dhis2/cli-helpers-engine": "1.3.0", + "@dhis2/cli-helpers-engine": "1.4.0", "@dhis2/cli-style": "4.1.1", "@dhis2/cli-utils": "2.0.0", "cli-table3": "^0.5.1", @@ -28,4 +28,4 @@ "publishConfig": { "access": "public" } -} +} \ No newline at end of file diff --git a/packages/utils/package.json b/packages/utils/package.json index d490e569..0e741483 100644 --- a/packages/utils/package.json +++ b/packages/utils/package.json @@ -12,7 +12,7 @@ "license": "BSD-3-Clause", "private": false, "dependencies": { - "@dhis2/cli-helpers-engine": "1.3.0", + "@dhis2/cli-helpers-engine": "1.4.0", "@semantic-release/changelog": "^3.0.4", "@semantic-release/commit-analyzer": "^6.3.0", "@semantic-release/git": "^7.0.16", @@ -25,4 +25,4 @@ "publishConfig": { "access": "public" } -} +} \ No newline at end of file diff --git a/yarn.lock b/yarn.lock index 48914ef0..229cd357 100644 --- a/yarn.lock +++ b/yarn.lock @@ -141,6 +141,18 @@ update-notifier "^3.0.0" yargs "^13.1.0" +"@dhis2/cli-helpers-engine@1.4.0": + version "1.4.0" + resolved "https://registry.yarnpkg.com/@dhis2/cli-helpers-engine/-/cli-helpers-engine-1.4.0.tgz#6371bce3017a5bc0c2b8ea84b77f253d75b8771e" + integrity sha512-2WLWJ5DcIiECmUlX2ChU2Wbqe/iJ42nDPQGjQXK1YzryNdnXyftUjJRwrLYWwaKGmWzfnPvpu8tvp+Yjysl78A== + dependencies: + chalk "^2.4.2" + fs-extra "^8.0.1" + request "^2.88.0" + tar "^4.4.8" + update-notifier "^3.0.0" + yargs "^13.1.0" + "@dhis2/cli-style@4.1.1": version "4.1.1" resolved "https://registry.yarnpkg.com/@dhis2/cli-style/-/cli-style-4.1.1.tgz#0d7da6684bb1b506a741c6ce020b5030c3132322" @@ -3505,7 +3517,6 @@ npm@^6.10.3: cmd-shim "^3.0.3" columnify "~1.5.4" config-chain "^1.1.12" - debuglog "*" detect-indent "~5.0.0" detect-newline "^2.1.0" dezalgo "~1.0.3" @@ -3520,7 +3531,6 @@ npm@^6.10.3: has-unicode "~2.0.1" hosted-git-info "^2.8.2" iferr "^1.0.2" - imurmurhash "*" infer-owner "^1.0.4" inflight "~1.0.6" inherits "^2.0.4" @@ -3539,14 +3549,8 @@ npm@^6.10.3: libnpx "^10.2.0" lock-verify "^2.1.0" lockfile "^1.0.4" - lodash._baseindexof "*" lodash._baseuniq "~4.6.0" - lodash._bindcallback "*" - lodash._cacheindexof "*" - lodash._createcache "*" - lodash._getnative "*" lodash.clonedeep "~4.5.0" - lodash.restparam "*" lodash.union "~4.6.0" lodash.uniq "~4.5.0" lodash.without "~4.4.0" From 7df11f2a090f19e83ed8037851084d07b420c854 Mon Sep 17 00:00:00 2001 From: Austin McGee <947888+amcgee@users.noreply.github.com> Date: Sat, 24 Aug 2019 16:32:56 +0200 Subject: [PATCH 3/3] fix: correct regex for Up status, factor list command internals --- packages/cluster/src/commands/list.js | 56 +++++++++++++++------------ 1 file changed, 32 insertions(+), 24 deletions(-) diff --git a/packages/cluster/src/commands/list.js b/packages/cluster/src/commands/list.js index 83a4855b..84773cb0 100644 --- a/packages/cluster/src/commands/list.js +++ b/packages/cluster/src/commands/list.js @@ -4,6 +4,35 @@ const { reporter, exec, tryCatchAsync } = require('@dhis2/cli-helpers-engine') const { makeComposeProject, listClusters } = require('../common') const Table = require('cli-table3') +const getStatus = async cluster => + // TODO: check the status of the other services, not just `core` + await exec({ + cmd: 'docker', + args: [ + 'ps', + '--filter', + `name=${makeComposeProject(cluster.name)}_core`, + '--format', + '{{.Status}}', + ], + pipe: false, + captureOut: true, + }) + +const formatStatus = status => { + status = status.trim() + + if (status.length === 0) { + return chalk.grey('Down') + } else if (/\(Paused\)$/.test(status)) { + return chalk.cyan(status) + } else if (/^Up \d+/.test(status)) { + return chalk.green(status) + } else { + return chalk.yellow(status) + } +} + const run = async function(argv) { const clusters = await listClusters(argv) @@ -20,29 +49,8 @@ const run = async function(argv) { await Promise.all( clusters.map(async cluster => { - let status = await exec({ - cmd: 'docker', - args: [ - 'ps', - '--filter', - `name=${makeComposeProject(cluster.name)}_core`, - '--format', - '{{.Status}}', - ], - pipe: false, - captureOut: true, - }) - - if (status.length === 0) { - status = chalk.grey('Down') - } else if (/\(Paused\)/.test(status)) { - status = chalk.cyan(status) - } else if (/$Up \d+/.test(status)) { - status = chalk.green(status) - } else { - status = chalk.yellow(status) - } - cluster.status = status + const status = await getStatus(cluster) + cluster.status = formatStatus(status) }) ) @@ -53,7 +61,7 @@ const run = async function(argv) { cluster.channel, cluster.dhis2Version, cluster.dbVersion, - cluster.status.trim(), + cluster.status, ]) )