Skip to content

Commit b3b1e6d

Browse files
authored
feat: add cluster list command (#118)
* feat: add cluster list commmand * chore(deps): upgrade to @dhis2/cli-helpers-engine@1.4.0 * fix: correct regex for Up status, factor list command internals
1 parent 0f2cb56 commit b3b1e6d

10 files changed

Lines changed: 111 additions & 23 deletions

File tree

packages/app/package.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,9 +12,9 @@
1212
"license": "BSD-3-Clause",
1313
"private": false,
1414
"dependencies": {
15-
"@dhis2/cli-helpers-engine": "1.3.0"
15+
"@dhis2/cli-helpers-engine": "1.4.0"
1616
},
1717
"publishConfig": {
1818
"access": "public"
1919
}
20-
}
20+
}

packages/cluster/package.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
"author": "Austin McGee <austin@dhis2.org>",
88
"license": "BSD-3-Clause",
99
"dependencies": {
10-
"@dhis2/cli-helpers-engine": "1.3.0",
10+
"@dhis2/cli-helpers-engine": "1.4.0",
1111
"chalk": "^2.4.2"
1212
},
1313
"bin": {
@@ -16,4 +16,4 @@
1616
"publishConfig": {
1717
"access": "public"
1818
}
19-
}
19+
}
Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
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+
}

packages/cluster/src/common.js

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -164,7 +164,15 @@ module.exports.makeEnvironment = cfg => {
164164

165165
module.exports.makeComposeProject = name => `d2-cluster-${name}`
166166

167-
module.exports.getLocalClusters = async () => {}
167+
module.exports.listClusters = async argv => {
168+
const cache = argv.getCache()
169+
170+
const stat = await cache.stat(clusterDir)
171+
const promises = Object.keys(stat.children)
172+
.filter(name => cache.exists(path.join(clusterDir, name)))
173+
.map(name => resolveConfiguration({ name, getCache: argv.getCache }))
174+
return await Promise.all(promises)
175+
}
168176

169177
module.exports.makeDockerImage = makeDockerImage
170178
module.exports.resolveConfiguration = resolveConfiguration

packages/create-app/package.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,10 @@
88
"license": "BSD-3-Clause",
99
"dependencies": {
1010
"@dhis2/cli-create": "2.0.0",
11-
"@dhis2/cli-helpers-engine": "1.3.0"
11+
"@dhis2/cli-helpers-engine": "1.4.0"
1212
},
1313
"bin": "./bin/d2-create-app",
1414
"publishConfig": {
1515
"access": "public"
1616
}
17-
}
17+
}

packages/create/package.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
"license": "BSD-3-Clause",
1010
"private": false,
1111
"dependencies": {
12-
"@dhis2/cli-helpers-engine": "1.3.0",
12+
"@dhis2/cli-helpers-engine": "1.4.0",
1313
"chalk": "^2.4.2",
1414
"fs-extra": "^8.1.0",
1515
"handlebars": "^4.1.0",
@@ -18,4 +18,4 @@
1818
"publishConfig": {
1919
"access": "public"
2020
}
21-
}
21+
}

packages/create/templates/cli/package.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,9 @@
99
"license": "BSD-3-Clause",
1010
"private": false,
1111
"dependencies": {
12-
"@dhis2/cli-helpers-engine": "1.3.0"
12+
"@dhis2/cli-helpers-engine": "1.4.0"
1313
},
1414
"publishConfig": {
1515
"access": "public"
1616
}
17-
}
17+
}

packages/main/package.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
"@dhis2/cli-app": "2.0.0",
1616
"@dhis2/cli-cluster": "2.0.0",
1717
"@dhis2/cli-create": "2.0.0",
18-
"@dhis2/cli-helpers-engine": "1.3.0",
18+
"@dhis2/cli-helpers-engine": "1.4.0",
1919
"@dhis2/cli-style": "4.1.1",
2020
"@dhis2/cli-utils": "2.0.0",
2121
"cli-table3": "^0.5.1",
@@ -28,4 +28,4 @@
2828
"publishConfig": {
2929
"access": "public"
3030
}
31-
}
31+
}

packages/utils/package.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
"license": "BSD-3-Clause",
1313
"private": false,
1414
"dependencies": {
15-
"@dhis2/cli-helpers-engine": "1.3.0",
15+
"@dhis2/cli-helpers-engine": "1.4.0",
1616
"@semantic-release/changelog": "^3.0.4",
1717
"@semantic-release/commit-analyzer": "^6.3.0",
1818
"@semantic-release/git": "^7.0.16",
@@ -25,4 +25,4 @@
2525
"publishConfig": {
2626
"access": "public"
2727
}
28-
}
28+
}

yarn.lock

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -141,6 +141,18 @@
141141
update-notifier "^3.0.0"
142142
yargs "^13.1.0"
143143

144+
"@dhis2/cli-helpers-engine@1.4.0":
145+
version "1.4.0"
146+
resolved "https://registry.yarnpkg.com/@dhis2/cli-helpers-engine/-/cli-helpers-engine-1.4.0.tgz#6371bce3017a5bc0c2b8ea84b77f253d75b8771e"
147+
integrity sha512-2WLWJ5DcIiECmUlX2ChU2Wbqe/iJ42nDPQGjQXK1YzryNdnXyftUjJRwrLYWwaKGmWzfnPvpu8tvp+Yjysl78A==
148+
dependencies:
149+
chalk "^2.4.2"
150+
fs-extra "^8.0.1"
151+
request "^2.88.0"
152+
tar "^4.4.8"
153+
update-notifier "^3.0.0"
154+
yargs "^13.1.0"
155+
144156
"@dhis2/cli-style@4.1.1":
145157
version "4.1.1"
146158
resolved "https://registry.yarnpkg.com/@dhis2/cli-style/-/cli-style-4.1.1.tgz#0d7da6684bb1b506a741c6ce020b5030c3132322"
@@ -3505,7 +3517,6 @@ npm@^6.10.3:
35053517
cmd-shim "^3.0.3"
35063518
columnify "~1.5.4"
35073519
config-chain "^1.1.12"
3508-
debuglog "*"
35093520
detect-indent "~5.0.0"
35103521
detect-newline "^2.1.0"
35113522
dezalgo "~1.0.3"
@@ -3520,7 +3531,6 @@ npm@^6.10.3:
35203531
has-unicode "~2.0.1"
35213532
hosted-git-info "^2.8.2"
35223533
iferr "^1.0.2"
3523-
imurmurhash "*"
35243534
infer-owner "^1.0.4"
35253535
inflight "~1.0.6"
35263536
inherits "^2.0.4"
@@ -3539,14 +3549,8 @@ npm@^6.10.3:
35393549
libnpx "^10.2.0"
35403550
lock-verify "^2.1.0"
35413551
lockfile "^1.0.4"
3542-
lodash._baseindexof "*"
35433552
lodash._baseuniq "~4.6.0"
3544-
lodash._bindcallback "*"
3545-
lodash._cacheindexof "*"
3546-
lodash._createcache "*"
3547-
lodash._getnative "*"
35483553
lodash.clonedeep "~4.5.0"
3549-
lodash.restparam "*"
35503554
lodash.union "~4.6.0"
35513555
lodash.uniq "~4.5.0"
35523556
lodash.without "~4.4.0"

0 commit comments

Comments
 (0)