From 5a1045f9498eac4613ed2093b1eab4d4b07a0dfe Mon Sep 17 00:00:00 2001 From: Birk Johansson Date: Fri, 16 Sep 2022 01:27:39 +0200 Subject: [PATCH 1/3] feat(cluster): add support for custom context-path. --- packages/cluster/src/commands/up.js | 4 ++-- packages/cluster/src/common.js | 12 ++++++++++-- 2 files changed, 12 insertions(+), 4 deletions(-) diff --git a/packages/cluster/src/commands/up.js b/packages/cluster/src/commands/up.js index 13c8ccad..db72eeed 100644 --- a/packages/cluster/src/commands/up.js +++ b/packages/cluster/src/commands/up.js @@ -138,8 +138,8 @@ module.exports = { }, customContext: { alias: 'c', - desc: 'Serve on a custom context path', - type: 'boolean', + desc: 'Serve on a custom context path. If used as a flag, the name of the cluster will be used.', + type: 'string', }, variant: { desc: 'Append variant options to the image', diff --git a/packages/cluster/src/common.js b/packages/cluster/src/common.js index faeff880..8060937a 100644 --- a/packages/cluster/src/common.js +++ b/packages/cluster/src/common.js @@ -1,6 +1,7 @@ const path = require('path') const { reporter } = require('@dhis2/cli-helpers-engine') const defaults = require('./defaults') +const { customContext } = require('./defaults') const clusterDir = 'clusters' const dockerComposeCacheName = 'docker-compose' @@ -109,7 +110,7 @@ async function resolveConfiguration(argv = {}) { // resolve specials... resolved.dhis2Version = resolved.dhis2Version || resolved.name resolved.dbVersion = resolved.dbVersion || resolved.dhis2Version - resolved.contextPath = resolved.customContext ? `/${resolved.name}` : '' + resolved.contextPath = resolveCustomContextPath(resolved) resolved.dockerImage = makeDockerImage( resolved.image, @@ -121,7 +122,6 @@ async function resolveConfiguration(argv = {}) { ) reporter.debug('Resolved configuration\n', resolved) - await argv.getCache().write( file, JSON.stringify( @@ -163,6 +163,14 @@ module.exports.makeEnvironment = cfg => { return env } +const resolveCustomContextPath = (resolved) => { + let contextPath = resolved.customContext + if (customContext === '') { + contextPath = resolved.name + } + return contextPath ? `/${contextPath}` : '' +} + // This has to match the normalization done by docker-compose to reliably get container statuses // from https://github.com/docker/compose/blob/c8279bc4db56f49cf2e2b80c8734ced1c418b856/compose/cli/command.py#L154 const normalizeName = name => name.replace(/[^-_a-z0-9]/g, '') From a1cceae081c8500d0659c4a3e1df8dd5d42a2baa Mon Sep 17 00:00:00 2001 From: Birk Johansson Date: Fri, 16 Sep 2022 01:28:11 +0200 Subject: [PATCH 2/3] feat(cluster-list): add context-path in cluster-list --- packages/cluster/src/commands/list.js | 24 ++++++++++++++---------- packages/cluster/src/common.js | 3 ++- 2 files changed, 16 insertions(+), 11 deletions(-) diff --git a/packages/cluster/src/commands/list.js b/packages/cluster/src/commands/list.js index 838c8abd..fadb147e 100644 --- a/packages/cluster/src/commands/list.js +++ b/packages/cluster/src/commands/list.js @@ -36,7 +36,9 @@ const formatStatus = status => { const run = async function (argv) { const clusters = await listClusters(argv) - + const anyCustomContext = clusters.some( + cluster => cluster.contextPath !== '' + ) const table = new Table({ head: [ 'Name', @@ -45,20 +47,22 @@ const run = async function (argv) { 'DHIS2 Version', 'DB Version', 'Status', - ], + ].concat(anyCustomContext ? 'Context Path' : []), }) await Promise.all( clusters.map(async cluster => { const status = await getStatus(cluster) - table.push([ - chalk.blue(cluster.name), - cluster.port, - cluster.channel, - cluster.dhis2Version, - cluster.dbVersion, - formatStatus(status), - ]) + table.push( + [ + chalk.blue(cluster.name), + cluster.port, + cluster.channel, + cluster.dhis2Version, + cluster.dbVersion, + formatStatus(status), + ].concat(anyCustomContext ? cluster.contextPath : []) + ) }) ) diff --git a/packages/cluster/src/common.js b/packages/cluster/src/common.js index 8060937a..e2af4a87 100644 --- a/packages/cluster/src/common.js +++ b/packages/cluster/src/common.js @@ -122,6 +122,7 @@ async function resolveConfiguration(argv = {}) { ) reporter.debug('Resolved configuration\n', resolved) + await argv.getCache().write( file, JSON.stringify( @@ -163,7 +164,7 @@ module.exports.makeEnvironment = cfg => { return env } -const resolveCustomContextPath = (resolved) => { +const resolveCustomContextPath = resolved => { let contextPath = resolved.customContext if (customContext === '') { contextPath = resolved.name From cf461d7a5d7ced895c4846a766b11ee6817ece78 Mon Sep 17 00:00:00 2001 From: Birk Johansson Date: Fri, 16 Sep 2022 01:59:10 +0200 Subject: [PATCH 3/3] test: fix tests --- packages/cluster/src/common.js | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/packages/cluster/src/common.js b/packages/cluster/src/common.js index e2af4a87..434b4b7b 100644 --- a/packages/cluster/src/common.js +++ b/packages/cluster/src/common.js @@ -1,7 +1,6 @@ const path = require('path') const { reporter } = require('@dhis2/cli-helpers-engine') const defaults = require('./defaults') -const { customContext } = require('./defaults') const clusterDir = 'clusters' const dockerComposeCacheName = 'docker-compose' @@ -166,7 +165,7 @@ module.exports.makeEnvironment = cfg => { const resolveCustomContextPath = resolved => { let contextPath = resolved.customContext - if (customContext === '') { + if (contextPath === '' || contextPath === true) { contextPath = resolved.name } return contextPath ? `/${contextPath}` : ''