diff --git a/CHANGELOG.md b/CHANGELOG.md index c06ea5f0030c..08b017bd5deb 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -57,6 +57,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)) +- [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)) - Add disablePrototypePoisoningProtection configuration to prevent JS client from erroring when cluster utilizes JS reserved words ([#2992](https://github.com/opensearch-project/OpenSearch-Dashboards/pull/2992)) ### 🐛 Bug Fixes 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..df607ca3c8d8 100644 --- a/src/setup_node_env/node_version_validator.js +++ b/src/setup_node_env/node_version_validator.js @@ -44,11 +44,15 @@ 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 += + '\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 cb3639154c6c..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); }); @@ -51,29 +51,79 @@ 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); + 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 + ); + }); + } +}); + +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 error if the major version is lower', function (done) { + 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 + REQUIRED_NODE_JS_VERSION !== lowerMajorVersion, + 'v14.0.0' ); }); - 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 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 error if the minor version is lower', function (done) { + 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 + REQUIRED_NODE_JS_VERSION !== lowerMinorVersion, + 'v14.0.0' ); }); }); @@ -87,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);