Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adding support for OSD_NODE_HOME #2208

Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
12 changes: 8 additions & 4 deletions src/dev/build/tasks/bin/scripts/opensearch-dashboards
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
18 changes: 11 additions & 7 deletions src/setup_node_env/node_version_validator.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}
102 changes: 82 additions & 20 deletions src/setup_node_env/node_version_validator.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
});
Expand All @@ -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) {
joshuarrrr marked this conversation as resolved.
Show resolved Hide resolved
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'
);
});
});
Expand All @@ -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);
Expand Down