Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add cluster list command #118

Merged
merged 3 commits into from
Aug 24, 2019
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions packages/app/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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"
}
}
}
4 changes: 2 additions & 2 deletions packages/cluster/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
"author": "Austin McGee <austin@dhis2.org>",
"license": "BSD-3-Clause",
"dependencies": {
"@dhis2/cli-helpers-engine": "1.3.0",
"@dhis2/cli-helpers-engine": "1.4.0",
"chalk": "^2.4.2"
},
"bin": {
Expand All @@ -16,4 +16,4 @@
"publishConfig": {
"access": "public"
}
}
}
68 changes: 68 additions & 0 deletions packages/cluster/src/commands/list.js
Original file line number Diff line number Diff line change
@@ -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,
}
10 changes: 9 additions & 1 deletion packages/cluster/src/common.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
4 changes: 2 additions & 2 deletions packages/create-app/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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"
}
}
}
4 changes: 2 additions & 2 deletions packages/create/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand All @@ -18,4 +18,4 @@
"publishConfig": {
"access": "public"
}
}
}
4 changes: 2 additions & 2 deletions packages/create/templates/cli/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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"
}
}
}
4 changes: 2 additions & 2 deletions packages/main/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand All @@ -28,4 +28,4 @@
"publishConfig": {
"access": "public"
}
}
}
4 changes: 2 additions & 2 deletions packages/utils/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand All @@ -25,4 +25,4 @@
"publishConfig": {
"access": "public"
}
}
}
20 changes: 12 additions & 8 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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"
Expand All @@ -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"
Expand All @@ -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"
Expand Down