From 75b3ca8271556f9b40950ac53cd58f4e4783e733 Mon Sep 17 00:00:00 2001 From: AbhishekReddy1127 Date: Mon, 29 Aug 2022 19:25:07 +0000 Subject: [PATCH] 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) {