From c9a507a8805de4c2ca92b970fb41269ac060d628 Mon Sep 17 00:00:00 2001 From: AbhishekReddy1127 Date: Mon, 29 Aug 2022 19:25:07 +0000 Subject: [PATCH 1/6] Adding Support for OSD_NODE_HOME Signed-off-by: AbhishekReddy1127 --- .../tasks/bin/scripts/opensearch-dashboards | 12 +++-- src/setup_node_env/node_version_validator.js | 19 ++++--- .../node_version_validator.test.js | 52 ++++++++++--------- 3 files changed, 47 insertions(+), 36 deletions(-) diff --git a/src/dev/build/tasks/bin/scripts/opensearch-dashboards b/src/dev/build/tasks/bin/scripts/opensearch-dashboards index 645dc3638b4a..adeb3b3f8214 100755 --- a/src/dev/build/tasks/bin/scripts/opensearch-dashboards +++ b/src/dev/build/tasks/bin/scripts/opensearch-dashboards @@ -28,10 +28,14 @@ done DIR="$(dirname "${SCRIPT}")/.." CONFIG_DIR=${OSD_PATH_CONF:-"$DIR/config"} -if [ -x "${DIR}/node/bin/node" ]; then - NODE="${DIR}/node/bin/node" -else - NODE="$(which node)" +NODE="$OSD_NODE_HOME" + +if [ -z "$NODE" ]; then + if [ -x "${DIR}/node/bin/node" ]; then + NODE="${DIR}/node/bin/node" + else + NODE="$(which node)" + fi fi if [ ! -x "$NODE" ]; then diff --git a/src/setup_node_env/node_version_validator.js b/src/setup_node_env/node_version_validator.js index 504d0970ecf6..a3d5afd94efc 100644 --- a/src/setup_node_env/node_version_validator.js +++ b/src/setup_node_env/node_version_validator.js @@ -44,11 +44,16 @@ var isVersionValid = // Validates current the NodeJS version compatibility when OpenSearch Dashboards starts. if (!isVersionValid) { - var errorMessage = - `OpenSearch Dashboards was built with ${requiredVersion} and does not support the current Node.js version ${currentVersion}. ` + - `Please use Node.js ${requiredVersion} or a higher patch version.`; - - // Actions to apply when validation fails: error report + exit. - console.error(errorMessage); - process.exit(1); + var errorMessage = `OpenSearch Dashboards was built with ${requiredVersion} and does not support the current Node.js version ${currentVersion}. `; + if (!process.env.OSD_NODE_HOME) { + // Actions to apply when validation fails: error report + exit. + errorMessage += `Please use Node.js ${requiredVersion} or a higher patch version.`; + console.error(errorMessage); + process.exit(1); + } else { + errorMessage += + '\nOpenSearch Dashboards is running as OSD_NODE_HOME environment variable is set, ' + + 'Ignoring any incpatibilities in node version.'; + console.warn(errorMessage); + } } diff --git a/src/setup_node_env/node_version_validator.test.js b/src/setup_node_env/node_version_validator.test.js index cb3639154c6c..4285d5675aa5 100644 --- a/src/setup_node_env/node_version_validator.test.js +++ b/src/setup_node_env/node_version_validator.test.js @@ -51,31 +51,33 @@ describe('NodeVersionValidator', function () { ); }); - it('should run the script WITH error if the major version is higher', function (done) { - testValidateNodeVersion(done, requiredNodeVersionWithDiff(+1, 0, 0), true); - }); - - it('should run the script WITH error if the major version is lower', function (done) { - var lowerMajorVersion = requiredNodeVersionWithDiff(-1, 0, 0); - testValidateNodeVersion( - done, - lowerMajorVersion, - REQUIRED_NODE_JS_VERSION !== lowerMajorVersion - ); - }); - - it('should run the script WITH error if the minor version is higher', function (done) { - testValidateNodeVersion(done, requiredNodeVersionWithDiff(0, +1, 0), true); - }); - - it('should run the script WITH error if the minor version is lower', function (done) { - var lowerMinorVersion = requiredNodeVersionWithDiff(0, -1, 0); - testValidateNodeVersion( - done, - lowerMinorVersion, - REQUIRED_NODE_JS_VERSION !== lowerMinorVersion - ); - }); + if (!process.env.OSD_NODE_HOME) { + it('should run the script WITH error if the major version is higher', function (done) { + testValidateNodeVersion(done, requiredNodeVersionWithDiff(+1, 0, 0), true); + }); + + it('should run the script WITH error if the major version is lower', function (done) { + var lowerMajorVersion = requiredNodeVersionWithDiff(-1, 0, 0); + testValidateNodeVersion( + done, + lowerMajorVersion, + REQUIRED_NODE_JS_VERSION !== lowerMajorVersion + ); + }); + + it('should run the script WITH error if the minor version is higher', function (done) { + testValidateNodeVersion(done, requiredNodeVersionWithDiff(0, +1, 0), true); + }); + + it('should run the script WITH error if the minor version is lower', function (done) { + var lowerMinorVersion = requiredNodeVersionWithDiff(0, -1, 0); + testValidateNodeVersion( + done, + lowerMinorVersion, + REQUIRED_NODE_JS_VERSION !== lowerMinorVersion + ); + }); + } }); function requiredNodeVersionWithDiff(majorDiff, minorDiff, patchDiff) { From c53b54a34dee6294e430393de15469f0021ae67d Mon Sep 17 00:00:00 2001 From: AbhishekReddy1127 Date: Tue, 30 Aug 2022 05:15:00 +0000 Subject: [PATCH 2/6] Updated text Signed-off-by: AbhishekReddy1127 --- src/setup_node_env/node_version_validator.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/setup_node_env/node_version_validator.js b/src/setup_node_env/node_version_validator.js index a3d5afd94efc..e8b700d1f2a7 100644 --- a/src/setup_node_env/node_version_validator.js +++ b/src/setup_node_env/node_version_validator.js @@ -53,7 +53,7 @@ if (!isVersionValid) { } else { errorMessage += '\nOpenSearch Dashboards is running as OSD_NODE_HOME environment variable is set, ' + - 'Ignoring any incpatibilities in node version.'; + 'So ignoring any incpatibilities in node version.'; console.warn(errorMessage); } } From 04f534750836115e20af0d11ae43d19b99018ba3 Mon Sep 17 00:00:00 2001 From: Abhishek Reddy <62020972+AbhishekReddy1127@users.noreply.github.com> Date: Tue, 6 Sep 2022 10:43:19 -0700 Subject: [PATCH 3/6] Update node_version_validator.js Signed-off-by: AbhishekReddy1127 --- src/setup_node_env/node_version_validator.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/setup_node_env/node_version_validator.js b/src/setup_node_env/node_version_validator.js index e8b700d1f2a7..69653f2e7da2 100644 --- a/src/setup_node_env/node_version_validator.js +++ b/src/setup_node_env/node_version_validator.js @@ -53,7 +53,7 @@ if (!isVersionValid) { } else { errorMessage += '\nOpenSearch Dashboards is running as OSD_NODE_HOME environment variable is set, ' + - 'So ignoring any incpatibilities in node version.'; + 'So ignoring any incapabilities in node version.'; console.warn(errorMessage); } } From 6147ac0da5af84c1aba9f8bbcecd82fbd109f243 Mon Sep 17 00:00:00 2001 From: AbhishekReddy1127 Date: Wed, 11 Jan 2023 19:44:31 +0000 Subject: [PATCH 4/6] Updated test case Signed-off-by: AbhishekReddy1127 --- CHANGELOG.md | 3 +- src/setup_node_env/node_version_validator.js | 3 +- .../node_version_validator.test.js | 84 ++++++++++++++++--- 3 files changed, 74 insertions(+), 16 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index fe9806ba766c..72e7c443fc82 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -55,8 +55,7 @@ Inspired from [Keep a Changelog](https://keepachangelog.com/en/1.0.0/) - [Multi DataSource] Improve test connection ([#3110](https://github.com/opensearch-project/OpenSearch-Dashboards/pull/3110)) - [Vis Builder] Add app filter and query persistence without using state container ([#3100](https://github.com/opensearch-project/OpenSearch-Dashboards/pull/3100)) - [Optimizer] Increase timeout waiting for the exiting of an optimizer worker ([#3193](https://github.com/opensearch-project/OpenSearch-Dashboards/pull/3193)) -- [Data] Update `createAggConfig` so that newly created configs can be added to beginning of `aggConfig` array ([#3160](https://github.com/opensearch-project/OpenSearch-Dashboards/pull/3160)) - +- [Data] Update `createAggConfig` so that newly created configs can be added to beginning of `aggConfig` array ([#3160](https://github.com/opensearch-project/OpenSearch-Dashboards/pull/3160)) -[Feature OSD_NODE_HOME] Added support for Environment variable OSD_NODE_HOME ([#2208](https://github.com/opensearch-project/OpenSearch-Dashboards/pull/2208))) ### 🐛 Bug Fixes diff --git a/src/setup_node_env/node_version_validator.js b/src/setup_node_env/node_version_validator.js index 69653f2e7da2..df607ca3c8d8 100644 --- a/src/setup_node_env/node_version_validator.js +++ b/src/setup_node_env/node_version_validator.js @@ -52,8 +52,7 @@ if (!isVersionValid) { process.exit(1); } else { errorMessage += - '\nOpenSearch Dashboards is running as OSD_NODE_HOME environment variable is set, ' + - 'So ignoring any incapabilities in node version.'; + '\nBecause the OSD_NODE_HOME environment variable is set, any node version incompatibilities will be ignored.'; console.warn(errorMessage); } } diff --git a/src/setup_node_env/node_version_validator.test.js b/src/setup_node_env/node_version_validator.test.js index 4285d5675aa5..420ce82edf2f 100644 --- a/src/setup_node_env/node_version_validator.test.js +++ b/src/setup_node_env/node_version_validator.test.js @@ -33,7 +33,7 @@ var pkg = require('../../package.json'); var REQUIRED_NODE_JS_VERSION = 'v' + pkg.engines.node; -describe('NodeVersionValidator', function () { +describe('NodeVersionValidator without OSD_NODE_HOME defined in the process ', function () { it('should run the script WITHOUT error when the version is the same', function (done) { testValidateNodeVersion(done, REQUIRED_NODE_JS_VERSION); }); @@ -80,6 +80,54 @@ describe('NodeVersionValidator', function () { } }); +describe('NodeVersionValidator with OSD_NODE_HOME defined in the process ', function () { + it('should run the script WITHOUT warning when the version is the same', function (done) { + testValidateNodeVersion(done, REQUIRED_NODE_JS_VERSION, false, 'v14.0.0'); + }); + + it('should run the script WITHOUT warning when only the patch version is higher', function (done) { + testValidateNodeVersion(done, requiredNodeVersionWithDiff(0, 0, +1), false, 'v14.0.0'); + }); + + it('should run the script WITH warning if the patch version is lower', function (done) { + var lowerPatchversion = requiredNodeVersionWithDiff(0, 0, -1); + testValidateNodeVersion( + done, + lowerPatchversion, + REQUIRED_NODE_JS_VERSION !== lowerPatchversion, + 'v14.0.0' + ); + }); + + it('should run the script WITH warning if the major version is higher', function (done) { + testValidateNodeVersion(done, requiredNodeVersionWithDiff(+1, 0, 0), true, 'v14.0.0'); + }); + + it('should run the script WITH warning if the major version is lower', function (done) { + var lowerMajorVersion = requiredNodeVersionWithDiff(-1, 0, 0); + testValidateNodeVersion( + done, + lowerMajorVersion, + REQUIRED_NODE_JS_VERSION !== lowerMajorVersion, + 'v14.0.0' + ); + }); + + it('should run the script WITH warning if the minor version is higher', function (done) { + testValidateNodeVersion(done, requiredNodeVersionWithDiff(0, +1, 0), true, 'v14.0.0'); + }); + + it('should run the script WITH warning if the minor version is lower', function (done) { + var lowerMinorVersion = requiredNodeVersionWithDiff(0, -1, 0); + testValidateNodeVersion( + done, + lowerMinorVersion, + REQUIRED_NODE_JS_VERSION !== lowerMinorVersion, + 'v14.0.0' + ); + }); +}); + function requiredNodeVersionWithDiff(majorDiff, minorDiff, patchDiff) { var matches = REQUIRED_NODE_JS_VERSION.match(/^v(\d+)\.(\d+)\.(\d+)/); var major = Math.max(parseInt(matches[1], 10) + majorDiff, 0); @@ -89,20 +137,32 @@ function requiredNodeVersionWithDiff(majorDiff, minorDiff, patchDiff) { return `v${major}.${minor}.${patch}`; } -function testValidateNodeVersion(done, versionToTest, expectError = false) { - var processVersionOverwrite = `Object.defineProperty(process, 'version', { value: '${versionToTest}', writable: true });`; - var command = `node -e "${processVersionOverwrite}require('./node_version_validator.js')"`; +function testValidateNodeVersion( + done, + versionToTest, + expectErrorOrWarning = false, + osdNodeHome = '' +) { + var processOverwrite = `Object.defineProperty(process, 'version', { value: '${versionToTest}', writable: true });`; + if (osdNodeHome) { + processOverwrite += `process.env.OSD_NODE_HOME = '${osdNodeHome}';`; + } + var command = `node -e "${processOverwrite}require('./node_version_validator.js')"`; exec(command, { cwd: __dirname }, function (error, _stdout, stderr) { expect(stderr).toBeDefined(); - if (expectError) { - expect(error.code).toBe(1); - - var speficicErrorMessage = - `OpenSearch Dashboards was built with ${REQUIRED_NODE_JS_VERSION} and does not support the current Node.js version ${versionToTest}. ` + - `Please use Node.js ${REQUIRED_NODE_JS_VERSION} or a higher patch version.\n`; - - expect(stderr).toStrictEqual(speficicErrorMessage); + var specificErrorOrWarningMessage = `OpenSearch Dashboards was built with ${REQUIRED_NODE_JS_VERSION} and does not support the current Node.js version ${versionToTest}. `; + + if (expectErrorOrWarning) { + if (!osdNodeHome) { + expect(error.code).toBe(1); + // Actions to apply when validation fails: error report + exit. + specificErrorOrWarningMessage += `Please use Node.js ${REQUIRED_NODE_JS_VERSION} or a higher patch version.\n`; + } else { + specificErrorOrWarningMessage += + '\nBecause the OSD_NODE_HOME environment variable is set, any node version incompatibilities will be ignored.\n'; + } + expect(stderr).toStrictEqual(specificErrorOrWarningMessage); } else { expect(error).toBeNull(); expect(stderr).toHaveLength(0); From fe99492fe9ea57fb094ee65d1a9dfb0a4abf6632 Mon Sep 17 00:00:00 2001 From: AbhishekReddy1127 Date: Wed, 11 Jan 2023 19:47:44 +0000 Subject: [PATCH 5/6] Added Changelog Signed-off-by: AbhishekReddy1127 --- CHANGELOG.md | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 72e7c443fc82..4964283c56ec 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -55,7 +55,8 @@ Inspired from [Keep a Changelog](https://keepachangelog.com/en/1.0.0/) - [Multi DataSource] Improve test connection ([#3110](https://github.com/opensearch-project/OpenSearch-Dashboards/pull/3110)) - [Vis Builder] Add app filter and query persistence without using state container ([#3100](https://github.com/opensearch-project/OpenSearch-Dashboards/pull/3100)) - [Optimizer] Increase timeout waiting for the exiting of an optimizer worker ([#3193](https://github.com/opensearch-project/OpenSearch-Dashboards/pull/3193)) -- [Data] Update `createAggConfig` so that newly created configs can be added to beginning of `aggConfig` array ([#3160](https://github.com/opensearch-project/OpenSearch-Dashboards/pull/3160)) -[Feature OSD_NODE_HOME] Added support for Environment variable OSD_NODE_HOME ([#2208](https://github.com/opensearch-project/OpenSearch-Dashboards/pull/2208))) +- [Data] Update `createAggConfig` so that newly created configs can be added to beginning of `aggConfig` array ([#3160](https://github.com/opensearch-project/OpenSearch-Dashboards/pull/3160)) +- [Feature OSD_NODE_HOME] Added support for Environment variable OSD_NODE_HOME ([#2208](https://github.com/opensearch-project/OpenSearch-Dashboards/pull/2208))) ### 🐛 Bug Fixes From 7f7cababb1254736995676536ab879c26d727123 Mon Sep 17 00:00:00 2001 From: Abhishek Reddy <62020972+AbhishekReddy1127@users.noreply.github.com> Date: Thu, 12 Jan 2023 16:42:41 -0800 Subject: [PATCH 6/6] Update CHANGELOG.md Co-authored-by: Josh Romero Signed-off-by: AbhishekReddy1127 --- CHANGELOG.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 4964283c56ec..1cc5e0e02441 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -56,7 +56,7 @@ Inspired from [Keep a Changelog](https://keepachangelog.com/en/1.0.0/) - [Vis Builder] Add app filter and query persistence without using state container ([#3100](https://github.com/opensearch-project/OpenSearch-Dashboards/pull/3100)) - [Optimizer] Increase timeout waiting for the exiting of an optimizer worker ([#3193](https://github.com/opensearch-project/OpenSearch-Dashboards/pull/3193)) - [Data] Update `createAggConfig` so that newly created configs can be added to beginning of `aggConfig` array ([#3160](https://github.com/opensearch-project/OpenSearch-Dashboards/pull/3160)) -- [Feature OSD_NODE_HOME] Added support for Environment variable OSD_NODE_HOME ([#2208](https://github.com/opensearch-project/OpenSearch-Dashboards/pull/2208))) +- [CLI] Add support for environment variable `OSD_NODE_HOME` which allows skipping node compatibility check ([#2208](https://github.com/opensearch-project/OpenSearch-Dashboards/pull/2208)) ### 🐛 Bug Fixes