From 4e8ce2b00e4e8f5069ae37c2f685bef31733f0cf Mon Sep 17 00:00:00 2001 From: teleivo Date: Wed, 21 Dec 2022 14:31:57 +0100 Subject: [PATCH 1/3] fix: set DHIS2_HOME env var to /DHIS2_home for images pre-Jib Docker images created for releases after 2.39.0, 2.38.2, 2.37.8.2 or 2.37.9 (not yet released) do not create directory /DHIS2_home anymore. See https://github.com/dhis2/dhis2-core/pull/11981 Instead /opt/dhis2 is created which is the default location used by DHIS2. Set DHIS2_HOME to /DHIS2_home for images created before such releases and to /opt/dhis2 otherwise. --- packages/cluster/src/common.js | 66 +++++++++++++++++++ packages/cluster/src/defaults.js | 1 + packages/cluster/tests/docker-image-jib.js | 53 +++++++++++++++ packages/cluster/tests/setup-environment.js | 35 +++++----- .../cluster/tests/substitute-string-keys.js | 6 +- 5 files changed, 143 insertions(+), 18 deletions(-) create mode 100644 packages/cluster/tests/docker-image-jib.js diff --git a/packages/cluster/src/common.js b/packages/cluster/src/common.js index faeff880..21cbf340 100644 --- a/packages/cluster/src/common.js +++ b/packages/cluster/src/common.js @@ -55,6 +55,12 @@ module.exports.substituteVersion = (string, version) => function makeDockerImage(string = '', substitutes = {}, variant = '') { let res = string + // master is published as latest on Dockerhub repo core-dev + // https://github.com/dhis2/operations-handbook/blob/7e3dd185c5c0992e625725724fd81468d95de259/releases/war_docker_schemes.md?plain=1#L70 + if (substitutes.version == 'master') { + substitutes.version = 'latest' + } + // the stable channel is just dhis2/core, so if the channel is // unspecified or 'stable', we should just strip it out from the // image tag @@ -82,6 +88,57 @@ function replacer(string, token, value) { return string.replace(regexp, value) } +function dhis2Home(version) { + if (!dockerImageUsingJib(version)) { + return '/DHIS2_home' + } + + return '/opt/dhis2' +} + +/* + * Docker images for releases starting from 2.39.0, 2.38.2, 2.37.8.2 or 2.37.9 + * (not released at the time this was written) do not create directory + * /DHIS2_home anymore. See https://github.com/dhis2/dhis2-core/pull/11981 + * Instead /opt/dhis2 is created which is the default location used by DHIS2. + */ +function dockerImageUsingJib(version) { + version = version.trim() || '' + + if (version == 'master') { + return true + } + + const segments = version + .split('.') + .map(s => s.trim()) + .filter(s => s.length > 0) // to remove empty segments we get with versions like "2." + .map(n => parseInt(n, 10)) + .filter(n => !Number.isNaN(n)) + if (segments.length < 2) { + throw new Error( + `Invalid version format: '${version}'. Must be 'master' or '2.major.minor.patch'. 'patch' being optional.` + ) + } + + // omit the "2." as we are not using semver; the second segment is our actual major version + // eslint-disable-next-line no-unused-vars + const [_, major, minor = 0, patch = 0] = segments + + if (major >= 39) { + return true + } else if (major == 38) { + return minor >= 2 + } else if (major == 37) { + if (minor >= 9 || (minor == 8 && patch >= 2)) { + return true + } + return false + } else { + return false + } +} + async function resolveConfiguration(argv = {}) { const file = path.join(clusterDir, argv.name, cacheFile) @@ -108,7 +165,13 @@ async function resolveConfiguration(argv = {}) { // resolve specials... resolved.dhis2Version = resolved.dhis2Version || resolved.name + resolved.dhis2Home = dhis2Home(resolved.dhis2Version) resolved.dbVersion = resolved.dbVersion || resolved.dhis2Version + if (resolved.dbVersion == 'master') { + // https://github.com/dhis2/operations-handbook/blob/7e3dd185c5c0992e625725724fd81468d95de259/README.md?plain=1#L325 + // DBs follow a different naming scheme than docker images + resolved.dbVersion = 'dev' + } resolved.contextPath = resolved.customContext ? `/${resolved.name}` : '' resolved.dockerImage = makeDockerImage( @@ -130,6 +193,7 @@ async function resolveConfiguration(argv = {}) { dbVersion: resolved.dbVersion, dhis2Version: resolved.dhis2Version, dhis2Config: resolved.dhis2Config, + dhis2Home: resolved.dhis2Home, customContext: resolved.customContext, image: resolved.image, port: resolved.port, @@ -147,6 +211,7 @@ module.exports.cleanCache = async ({ cache, name }) => module.exports.makeEnvironment = cfg => { const env = { + DHIS2_HOME: cfg.dhis2Home, DHIS2_CORE_NAME: cfg.name, DHIS2_CORE_IMAGE: cfg.dockerImage, DHIS2_CORE_CONTEXT_PATH: cfg.contextPath, @@ -184,3 +249,4 @@ module.exports.listClusters = async argv => { module.exports.makeDockerImage = makeDockerImage module.exports.resolveConfiguration = resolveConfiguration +module.exports.dockerImageUsingJib = dockerImageUsingJib diff --git a/packages/cluster/src/defaults.js b/packages/cluster/src/defaults.js index 2dbceeca..6997a978 100644 --- a/packages/cluster/src/defaults.js +++ b/packages/cluster/src/defaults.js @@ -1,4 +1,5 @@ module.exports = { + dhis2Home: '/opt/dhis2', dhis2Version: '', dbVersion: '', channel: 'stable', diff --git a/packages/cluster/tests/docker-image-jib.js b/packages/cluster/tests/docker-image-jib.js new file mode 100644 index 00000000..86df27ce --- /dev/null +++ b/packages/cluster/tests/docker-image-jib.js @@ -0,0 +1,53 @@ +const test = require('tape-await') +const { dockerImageUsingJib } = require('../src/common.js') + +test(`DHIS2 versions with Docker image containing /opt/dhis2`, async function (t) { + const versions = [ + 'master', + '2.39', + '2.39.0', + '2.39.0.1', + '2.38.2', + '2.38.2.0', + '2.38.2.1', + '2.37.9', + '2.37.8.2', + ] + t.plan(versions.length) + + versions.forEach(version => t.ok(dockerImageUsingJib(version), version)) +}) + +test('DHIS2 versions with Docker image built containing /DHIS2_home', async function (t) { + const versions = [ + '2.38', + '2.38.1.1', + '2.37.8.1', + '2.37.8', + '2.37', + '2.37.7.1', + '2.36.13.1', + '2.36.3', + '2.35.14', + '2.34.0', + '2.33', + ] + t.plan(versions.length) + + versions.forEach(version => t.notOk(dockerImageUsingJib(version), version)) +}) + +test('throws given invalid version', async function (t) { + const versions = ['', ' ', '2', '2.'] + t.plan(versions.length) + + versions.forEach(version => + t.throws( + function () { + dockerImageUsingJib(version) + }, + /^Error: invalid version format/i, + version + ) + ) +}) diff --git a/packages/cluster/tests/setup-environment.js b/packages/cluster/tests/setup-environment.js index fed00499..8d99a2f3 100644 --- a/packages/cluster/tests/setup-environment.js +++ b/packages/cluster/tests/setup-environment.js @@ -11,7 +11,7 @@ test('build runtime environment based on defaults', async function (t) { t.plan(1) const argv = { - name: 'dev', + name: '2.39.0', getCache: () => cache(null), } @@ -19,11 +19,12 @@ test('build runtime environment based on defaults', async function (t) { const actual = makeEnvironment(cfg) const expected = { - DHIS2_CORE_NAME: 'dev', + DHIS2_HOME: '/opt/dhis2', + DHIS2_CORE_NAME: '2.39.0', DHIS2_CORE_CONTEXT_PATH: '', - DHIS2_CORE_IMAGE: 'dhis2/core:dev', - DHIS2_CORE_VERSION: 'dev', - DHIS2_CORE_DB_VERSION: 'dev', + DHIS2_CORE_IMAGE: 'dhis2/core:2.39.0', + DHIS2_CORE_VERSION: '2.39.0', + DHIS2_CORE_DB_VERSION: '2.39.0', DHIS2_CORE_PORT: defaults.port, } @@ -48,6 +49,7 @@ test('build runtime environment based on args', async function (t) { const actual = makeEnvironment(cfg) const expected = { + DHIS2_HOME: '/DHIS2_home', DHIS2_CORE_NAME: 'dev', DHIS2_CORE_CONTEXT_PATH: '/dev', DHIS2_CORE_IMAGE: 'dhis2/core-canary:2.33-jetty-slackware', @@ -80,9 +82,10 @@ test('build runtime environment based on mixed args and config', async function const actual = makeEnvironment(cfg) const expected = { + DHIS2_HOME: '/opt/dhis2', DHIS2_CORE_NAME: 'mydev', DHIS2_CORE_CONTEXT_PATH: '/mydev', - DHIS2_CORE_IMAGE: 'dhis2/core-dev:master', + DHIS2_CORE_IMAGE: 'dhis2/core-dev:latest', DHIS2_CORE_VERSION: 'master', DHIS2_CORE_DB_VERSION: 'dev', DHIS2_CORE_PORT: 8233, @@ -96,7 +99,7 @@ test('build runtime environment based on mixed args, cache, config and defaults' const config = { port: 8233, - dhis2Version: 'dev', + dhis2Version: 'master', } const argv = { @@ -105,7 +108,7 @@ test('build runtime environment based on mixed args, cache, config and defaults' getCache: () => cache({ customContext: true, - image: 'dhis2/core-canary:master-20190523-alpine', + image: 'dhis2/core-canary:master-20190523', }), } @@ -113,10 +116,11 @@ test('build runtime environment based on mixed args, cache, config and defaults' const actual = makeEnvironment(cfg) const expected = { + DHIS2_HOME: '/opt/dhis2', DHIS2_CORE_NAME: 'mydev', DHIS2_CORE_CONTEXT_PATH: '/mydev', - DHIS2_CORE_IMAGE: 'dhis2/core-canary:master-20190523-alpine', - DHIS2_CORE_VERSION: 'dev', + DHIS2_CORE_IMAGE: 'dhis2/core-canary:master-20190523', + DHIS2_CORE_VERSION: 'master', DHIS2_CORE_DB_VERSION: 'dev', DHIS2_CORE_PORT: 8233, } @@ -129,12 +133,12 @@ test('build runtime environment based on mixed args, cache, config, custom per-c const config = { port: 8233, - dhis2Version: 'dev', + dhis2Version: 'master', dbVersion: 'dev', clusters: { 2330: { port: 9999, - dhis2Version: 'apa', + dhis2Version: '2.38.2', }, }, } @@ -145,7 +149,7 @@ test('build runtime environment based on mixed args, cache, config, custom per-c getCache: () => cache({ customContext: true, - image: 'dhis2/core-canary:master-20190523-alpine', + image: 'dhis2/core-canary:master-20190523', }), } @@ -153,10 +157,11 @@ test('build runtime environment based on mixed args, cache, config, custom per-c const actual = makeEnvironment(cfg) const expected = { + DHIS2_HOME: '/opt/dhis2', DHIS2_CORE_NAME: '2330', DHIS2_CORE_CONTEXT_PATH: '/2330', - DHIS2_CORE_IMAGE: 'dhis2/core-canary:master-20190523-alpine', - DHIS2_CORE_VERSION: 'apa', + DHIS2_CORE_IMAGE: 'dhis2/core-canary:master-20190523', + DHIS2_CORE_VERSION: '2.38.2', DHIS2_CORE_DB_VERSION: 'dev', DHIS2_CORE_PORT: 9999, } diff --git a/packages/cluster/tests/substitute-string-keys.js b/packages/cluster/tests/substitute-string-keys.js index fc9f4b56..e3f6278e 100644 --- a/packages/cluster/tests/substitute-string-keys.js +++ b/packages/cluster/tests/substitute-string-keys.js @@ -59,7 +59,7 @@ test('stable and 2.32.0 with alpine variant', function (t) { test('stable and master with tomcat9 and debian-slim variant', function (t) { t.plan(1) - const expected = 'dhis2/core:master-tomcat9-debian-slim' + const expected = 'dhis2/core:latest-tomcat9-debian-slim' t.equal( makeDockerImage( @@ -100,7 +100,7 @@ test('stable and 2.32.0', function (t) { test('dev and master', function (t) { t.plan(1) - const expected = 'dhis2/core-dev:master' + const expected = 'dhis2/core-dev:latest' t.equal( makeDockerImage(template, { @@ -130,7 +130,7 @@ test('dev and 2.32', function (t) { test('canary and master', function (t) { t.plan(1) - const expected = 'dhis2/core-canary:master' + const expected = 'dhis2/core-canary:latest' t.equal( makeDockerImage(template, { From b18129eb3494f27c47c7f68de79dfd03a0501852 Mon Sep 17 00:00:00 2001 From: teleivo Date: Fri, 23 Dec 2022 06:15:14 +0100 Subject: [PATCH 2/3] chore: 2.37.9 was released so we can simplify our logic that future proofed the next 2.37 release --- packages/cluster/src/common.js | 11 ++++------- packages/cluster/tests/docker-image-jib.js | 1 - 2 files changed, 4 insertions(+), 8 deletions(-) diff --git a/packages/cluster/src/common.js b/packages/cluster/src/common.js index 21cbf340..23869b74 100644 --- a/packages/cluster/src/common.js +++ b/packages/cluster/src/common.js @@ -97,9 +97,9 @@ function dhis2Home(version) { } /* - * Docker images for releases starting from 2.39.0, 2.38.2, 2.37.8.2 or 2.37.9 - * (not released at the time this was written) do not create directory - * /DHIS2_home anymore. See https://github.com/dhis2/dhis2-core/pull/11981 + * Docker images for releases starting from 2.39.0, 2.38.2, 2.37.9 + * do not create directory /DHIS2_home anymore. See + * https://github.com/dhis2/dhis2-core/pull/11981 * Instead /opt/dhis2 is created which is the default location used by DHIS2. */ function dockerImageUsingJib(version) { @@ -130,10 +130,7 @@ function dockerImageUsingJib(version) { } else if (major == 38) { return minor >= 2 } else if (major == 37) { - if (minor >= 9 || (minor == 8 && patch >= 2)) { - return true - } - return false + return minor >= 9 } else { return false } diff --git a/packages/cluster/tests/docker-image-jib.js b/packages/cluster/tests/docker-image-jib.js index 86df27ce..6fdf9e2d 100644 --- a/packages/cluster/tests/docker-image-jib.js +++ b/packages/cluster/tests/docker-image-jib.js @@ -11,7 +11,6 @@ test(`DHIS2 versions with Docker image containing /opt/dhis2`, async function (t '2.38.2.0', '2.38.2.1', '2.37.9', - '2.37.8.2', ] t.plan(versions.length) From 27cf4dd1f7d7b336438fe5b6d16f4922adfb6d5a Mon Sep 17 00:00:00 2001 From: "@dhis2-bot" Date: Fri, 13 Jan 2023 15:46:15 +0000 Subject: [PATCH 3/3] chore(release): cut 4.2.3 [skip ci] ## [4.2.3](https://github.com/dhis2/cli/compare/v4.2.2...v4.2.3) (2023-01-13) ### Bug Fixes * set DHIS2_HOME env var to /DHIS2_home for images pre-Jib ([4e8ce2b](https://github.com/dhis2/cli/commit/4e8ce2b00e4e8f5069ae37c2f685bef31733f0cf)) --- CHANGELOG.md | 7 +++++++ package.json | 2 +- packages/app/package.json | 2 +- packages/cluster/package.json | 2 +- packages/create-app/package.json | 4 ++-- packages/create/package.json | 2 +- packages/main/package.json | 10 +++++----- packages/utils/package.json | 2 +- 8 files changed, 19 insertions(+), 12 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 9682449e..07a7a996 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,3 +1,10 @@ +## [4.2.3](https://github.com/dhis2/cli/compare/v4.2.2...v4.2.3) (2023-01-13) + + +### Bug Fixes + +* set DHIS2_HOME env var to /DHIS2_home for images pre-Jib ([4e8ce2b](https://github.com/dhis2/cli/commit/4e8ce2b00e4e8f5069ae37c2f685bef31733f0cf)) + ## [4.2.2](https://github.com/dhis2/cli/compare/v4.2.1...v4.2.2) (2022-01-10) diff --git a/package.json b/package.json index 727f6b0f..a9b8731a 100644 --- a/package.json +++ b/package.json @@ -1,5 +1,5 @@ { - "version": "4.2.2", + "version": "4.2.3", "private": true, "workspaces": [ "packages/*" diff --git a/packages/app/package.json b/packages/app/package.json index 8106d413..42309887 100644 --- a/packages/app/package.json +++ b/packages/app/package.json @@ -6,7 +6,7 @@ "engines": { "node": ">=12" }, - "version": "4.2.2", + "version": "4.2.3", "main": "src/index.js", "author": "Austin McGee ", "contributors": [ diff --git a/packages/cluster/package.json b/packages/cluster/package.json index a2d6a0cc..b1fd5e1b 100644 --- a/packages/cluster/package.json +++ b/packages/cluster/package.json @@ -1,6 +1,6 @@ { "name": "@dhis2/cli-cluster", - "version": "4.2.2", + "version": "4.2.3", "description": "D2 CLI module for managing DHIS2 development clusters", "main": "src/index.js", "repository": "https://github.com/dhis2/cli", diff --git a/packages/create-app/package.json b/packages/create-app/package.json index f70b9cc2..83e20a00 100644 --- a/packages/create-app/package.json +++ b/packages/create-app/package.json @@ -1,13 +1,13 @@ { "name": "@dhis2/create-app", - "version": "4.2.2", + "version": "4.2.3", "description": "D2 frontend application bootstrapping inspired by create-react-app", "main": "index.js", "repository": "https://github.com/dhis2/cli", "author": "Austin McGee ", "license": "BSD-3-Clause", "dependencies": { - "@dhis2/cli-create": "4.2.2", + "@dhis2/cli-create": "4.2.3", "@dhis2/cli-helpers-engine": "^3.2.1" }, "bin": "./bin/d2-create-app", diff --git a/packages/create/package.json b/packages/create/package.json index 5b452a0a..03461520 100644 --- a/packages/create/package.json +++ b/packages/create/package.json @@ -6,7 +6,7 @@ "engines": { "node": ">=12" }, - "version": "4.2.2", + "version": "4.2.3", "main": "src/index.js", "author": "Austin McGee ", "license": "BSD-3-Clause", diff --git a/packages/main/package.json b/packages/main/package.json index eb6426f5..7f9a68fd 100644 --- a/packages/main/package.json +++ b/packages/main/package.json @@ -1,6 +1,6 @@ { "name": "@dhis2/cli", - "version": "4.2.2", + "version": "4.2.3", "description": "A command line interface for DHIS2 development workflows", "main": "src/index.js", "author": "Austin McGee ", @@ -12,12 +12,12 @@ "node": ">=12" }, "dependencies": { - "@dhis2/cli-app": "4.2.2", - "@dhis2/cli-cluster": "4.2.2", - "@dhis2/cli-create": "4.2.2", + "@dhis2/cli-app": "4.2.3", + "@dhis2/cli-cluster": "4.2.3", + "@dhis2/cli-create": "4.2.3", "@dhis2/cli-helpers-engine": "^3.2.1", "@dhis2/cli-style": "^9.0.1", - "@dhis2/cli-utils": "4.2.2", + "@dhis2/cli-utils": "4.2.3", "cli-table3": "^0.6.0", "envinfo": "^7.5.0", "inquirer": "^7.1.0" diff --git a/packages/utils/package.json b/packages/utils/package.json index 2ac8a2b1..7961cd51 100644 --- a/packages/utils/package.json +++ b/packages/utils/package.json @@ -6,7 +6,7 @@ "engines": { "node": ">=12" }, - "version": "4.2.2", + "version": "4.2.3", "main": "src/index.js", "author": "Viktor Varland ", "contributors": [