From a75794b8f880af854a820660877d4aabcee8cc7a Mon Sep 17 00:00:00 2001 From: Miki Date: Mon, 20 Mar 2023 16:19:26 -0700 Subject: [PATCH] Allow selecting the Node.js binary OSD uses (#3508) New reusable use_node scripts take care of selecting the correct Node.js binary based OSD_NODE_HOME, OSD_HOME, and whatever is in the PATH. It also takes care of finding the binary on FreeBSD. * The startup scripts check `OSD_NODE_HOME` and `NODE_HOME` before falling back to use the bundled or system-wide Node.js binary. * Update `package.json` to replace `node` with `use_node`. * Update file templates and shell scripts to internally use `use_node`. * Update the build scripts to copy `use_node` into releases * Make tests that execute node internally, honor the binary being used. Signed-off-by: Miki --- CHANGELOG.md | 1 + package.json | 64 +++++------ packages/osd-ace/package.json | 2 +- packages/osd-analytics/package.json | 6 +- .../src/precommit_hook/script_source.ts | 4 +- packages/osd-i18n/package.json | 6 +- packages/osd-interpreter/package.json | 6 +- packages/osd-monaco/package.json | 2 +- packages/osd-opensearch-archiver/package.json | 4 +- packages/osd-opensearch/package.json | 4 +- packages/osd-plugin-generator/package.json | 4 +- .../template/package.json.ejs | 6 +- packages/osd-plugin-helpers/package.json | 2 +- packages/osd-ui-shared-deps/package.json | 6 +- scripts/use_node | 97 +++++++++++++++++ scripts/use_node.bat | 101 ++++++++++++++++++ .../build/tasks/bin/copy_bin_scripts_task.ts | 3 + .../tasks/bin/scripts/opensearch-dashboards | 27 ++--- .../scripts/opensearch-dashboards-keystore | 22 ++-- .../opensearch-dashboards-keystore.bat | 43 +++----- .../bin/scripts/opensearch-dashboards-plugin | 22 ++-- .../scripts/opensearch-dashboards-plugin.bat | 47 +++----- .../bin/scripts/opensearch-dashboards.bat | 49 +++------ src/dev/ci_setup/setup.sh | 6 +- src/dev/ci_setup/setup_percy.sh | 2 +- .../node_version_validator.test.js | 2 +- tasks/config/run.js | 2 +- 27 files changed, 330 insertions(+), 210 deletions(-) create mode 100755 scripts/use_node create mode 100644 scripts/use_node.bat diff --git a/CHANGELOG.md b/CHANGELOG.md index a09c82ffc202..aa1f22ddead0 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -148,6 +148,7 @@ Inspired from [Keep a Changelog](https://keepachangelog.com/en/1.0.0/) - Relax the Node.js requirement to `^14.20.1` ([3463](https://github.com/opensearch-project/OpenSearch-Dashboards/pull/3463)) - Bump the version of Node.js installed by `nvm` to `14.21.3` ([3463](https://github.com/opensearch-project/OpenSearch-Dashboards/pull/3463)) - Remove the unused `renovate.json5` file ([3489](https://github.com/opensearch-project/OpenSearch-Dashboards/pull/3489)) +- Allow selecting the Node.js binary using `NODE_HOME` and `OSD_NODE_HOME` ([3508](https://github.com/opensearch-project/OpenSearch-Dashboards/pull/3508)) ### 🪛 Refactoring diff --git a/package.json b/package.json index e791a29ca5d7..1e96cc9bdda5 100644 --- a/package.json +++ b/package.json @@ -39,42 +39,42 @@ }, "author": "opensearch-project", "scripts": { - "preinstall": "node ./preinstall_check", - "osd": "node scripts/osd", - "opensearch": "node scripts/opensearch", + "preinstall": "scripts/use_node ./preinstall_check", + "osd": "scripts/use_node scripts/osd", + "opensearch": "scripts/use_node scripts/opensearch", "test": "grunt test", "test:bwc": "./scripts/bwctest_osd.sh", - "test:jest": "node scripts/jest", - "test:jest:coverage": "node scripts/jest --coverage", - "test:jest:ci": "node scripts/jest --ci --colors --runInBand", - "test:jest:ci:coverage": "node scripts/jest --ci --colors --runInBand --coverage", - "test:jest_integration": "node scripts/jest_integration", - "test:jest_integration:ci": "node scripts/jest_integration --ci --colors --max-old-space-size=5120", - "test:mocha": "node scripts/mocha", - "test:mocha:coverage": "yarn nyc --reporter=text-summary --reporter=lcov --report-dir=./target/opensearch-dashboards-coverage/mocha node scripts/mocha", - "test:ftr": "node scripts/functional_tests", - "test:ftr:server": "node scripts/functional_tests_server", - "test:ftr:runner": "node scripts/functional_test_runner", - "checkLicenses": "node scripts/check_licenses --dev", - "notice:validate": "node scripts/notice --validate", - "notice:generate": "node scripts/notice", - "build-platform": "node scripts/build", - "build": "node scripts/build --all-platforms", - "start": "node scripts/opensearch_dashboards --dev", - "start:docker": "node scripts/opensearch_dashboards --dev --opensearch.hosts=$OPENSEARCH_HOSTS --opensearch.ignoreVersionMismatch=true --server.host=$SERVER_HOST", - "debug": "node --nolazy --inspect scripts/opensearch_dashboards --dev", - "debug-break": "node --nolazy --inspect-brk scripts/opensearch_dashboards --dev", + "test:jest": "scripts/use_node scripts/jest", + "test:jest:coverage": "scripts/use_node scripts/jest --coverage", + "test:jest:ci": "scripts/use_node scripts/jest --ci --colors --runInBand", + "test:jest:ci:coverage": "scripts/use_node scripts/jest --ci --colors --runInBand --coverage", + "test:jest_integration": "scripts/use_node scripts/jest_integration", + "test:jest_integration:ci": "scripts/use_node scripts/jest_integration --ci --colors --max-old-space-size=5120", + "test:mocha": "scripts/use_node scripts/mocha", + "test:mocha:coverage": "yarn nyc --reporter=text-summary --reporter=lcov --report-dir=./target/opensearch-dashboards-coverage/mocha scripts/use_node scripts/mocha", + "test:ftr": "scripts/use_node scripts/functional_tests", + "test:ftr:server": "scripts/use_node scripts/functional_tests_server", + "test:ftr:runner": "scripts/use_node scripts/functional_test_runner", + "checkLicenses": "scripts/use_node scripts/check_licenses --dev", + "notice:validate": "scripts/use_node scripts/notice --validate", + "notice:generate": "scripts/use_node scripts/notice", + "build-platform": "scripts/use_node scripts/build", + "build": "scripts/use_node scripts/build --all-platforms", + "start": "scripts/use_node scripts/opensearch_dashboards --dev", + "start:docker": "scripts/use_node scripts/opensearch_dashboards --dev --opensearch.hosts=$OPENSEARCH_HOSTS --opensearch.ignoreVersionMismatch=true --server.host=$SERVER_HOST", + "debug": "scripts/use_node --nolazy --inspect scripts/opensearch_dashboards --dev", + "debug-break": "scripts/use_node --nolazy --inspect-brk scripts/opensearch_dashboards --dev", "lint": "yarn run lint:es && yarn run lint:style", - "lint:es": "node scripts/eslint", - "lint:style": "node scripts/stylelint", - "makelogs": "node scripts/makelogs", + "lint:es": "scripts/use_node scripts/eslint", + "lint:style": "scripts/use_node scripts/stylelint", + "makelogs": "scripts/use_node scripts/makelogs", "uiFramework:compileCss": "cd packages/osd-ui-framework && yarn compileCss", - "osd:watch": "node scripts/opensearch_dashboards --dev --logging.json=false", - "build:types": "node scripts/remove.js ./target/types && tsc --p tsconfig.types.json", - "docs:acceptApiChanges": "node --max-old-space-size=6144 scripts/check_published_api_changes.js --accept", - "osd:bootstrap": "node scripts/build_ts_refs && node scripts/register_git_hook", - "spec_to_console": "node scripts/spec_to_console", - "pkg-version": "node -e \"console.log(require('./package.json').version)\"" + "osd:watch": "scripts/use_node scripts/opensearch_dashboards --dev --logging.json=false", + "build:types": "scripts/use_node scripts/remove.js ./target/types && tsc --p tsconfig.types.json", + "docs:acceptApiChanges": "scripts/use_node --max-old-space-size=6144 scripts/check_published_api_changes.js --accept", + "osd:bootstrap": "scripts/use_node scripts/build_ts_refs && scripts/use_node scripts/register_git_hook", + "spec_to_console": "scripts/use_node scripts/spec_to_console", + "pkg-version": "scripts/use_node -e \"console.log(require('./package.json').version)\"" }, "repository": { "type": "git", diff --git a/packages/osd-ace/package.json b/packages/osd-ace/package.json index 5a2f3141ddd5..a93467058179 100644 --- a/packages/osd-ace/package.json +++ b/packages/osd-ace/package.json @@ -5,7 +5,7 @@ "main": "./target/index.js", "license": "Apache-2.0", "scripts": { - "build": "node ./scripts/build.js", + "build": "../../scripts/use_node ./scripts/build.js", "osd:bootstrap": "yarn build --dev" }, "dependencies": { diff --git a/packages/osd-analytics/package.json b/packages/osd-analytics/package.json index d460ab335a2d..954f6cc51ba4 100644 --- a/packages/osd-analytics/package.json +++ b/packages/osd-analytics/package.json @@ -9,9 +9,9 @@ "author": "Ahmad Bamieh ", "license": "Apache-2.0", "scripts": { - "build": "node scripts/build", - "osd:bootstrap": "node scripts/build --source-maps", - "osd:watch": "node scripts/build --source-maps --watch" + "build": "../../scripts/use_node scripts/build", + "osd:bootstrap": "../../scripts/use_node scripts/build --source-maps", + "osd:watch": "../../scripts/use_node scripts/build --source-maps --watch" }, "devDependencies": { "@babel/cli": "^7.16.0", diff --git a/packages/osd-dev-utils/src/precommit_hook/script_source.ts b/packages/osd-dev-utils/src/precommit_hook/script_source.ts index 64915df17d9f..3adfacf238de 100644 --- a/packages/osd-dev-utils/src/precommit_hook/script_source.ts +++ b/packages/osd-dev-utils/src/precommit_hook/script_source.ts @@ -57,7 +57,7 @@ set -euo pipefail trap "exit 130" INT has_node() { - command -v node >/dev/null 2>&1 + command -v scripts/use_node >/dev/null 2>&1 } has_nvm() { @@ -109,7 +109,7 @@ has_node || { } execute_precommit_hook() { - node scripts/precommit_hook || return 1 + scripts/use_node scripts/precommit_hook || return 1 PRECOMMIT_FILE="./.git/hooks/pre-commit.local" if [ -x "\${PRECOMMIT_FILE}" ]; then diff --git a/packages/osd-i18n/package.json b/packages/osd-i18n/package.json index 98b3f9bbde1d..94a5e0ff1aa9 100644 --- a/packages/osd-i18n/package.json +++ b/packages/osd-i18n/package.json @@ -7,9 +7,9 @@ "license": "Apache-2.0", "private": true, "scripts": { - "build": "node scripts/build", - "osd:bootstrap": "node scripts/build --source-maps", - "osd:watch": "node scripts/build --watch --source-maps" + "build": "../../scripts/use_node scripts/build", + "osd:bootstrap": "../../scripts/use_node scripts/build --source-maps", + "osd:watch": "../../scripts/use_node scripts/build --watch --source-maps" }, "devDependencies": { "@babel/cli": "^7.16.0", diff --git a/packages/osd-interpreter/package.json b/packages/osd-interpreter/package.json index 4654b73b186e..77ec29c839dd 100644 --- a/packages/osd-interpreter/package.json +++ b/packages/osd-interpreter/package.json @@ -4,9 +4,9 @@ "license": "Apache-2.0", "scripts": { "interpreter:peg": "pegjs src/common/lib/grammar.peg", - "build": "node scripts/build", - "osd:bootstrap": "node scripts/build --dev", - "osd:watch": "node scripts/build --dev --watch" + "build": "../../scripts/use_node scripts/build", + "osd:bootstrap": "../../scripts/use_node scripts/build --dev", + "osd:watch": "../../scripts/use_node scripts/build --dev --watch" }, "dependencies": { "@babel/runtime": "^7.16.5", diff --git a/packages/osd-monaco/package.json b/packages/osd-monaco/package.json index e937b1e8f60b..a3f973d11fd0 100644 --- a/packages/osd-monaco/package.json +++ b/packages/osd-monaco/package.json @@ -5,7 +5,7 @@ "main": "./target/index.js", "license": "Apache-2.0", "scripts": { - "build": "node ./scripts/build.js", + "build": "../../scripts/use_node ./scripts/build.js", "osd:bootstrap": "yarn build --dev" }, "dependencies": { diff --git a/packages/osd-opensearch-archiver/package.json b/packages/osd-opensearch-archiver/package.json index f130ae44138a..8f88fa879525 100644 --- a/packages/osd-opensearch-archiver/package.json +++ b/packages/osd-opensearch-archiver/package.json @@ -7,8 +7,8 @@ "devOnly": true }, "scripts": { - "osd:bootstrap": "node ../../scripts/remove.js target && tsc", - "osd:watch": "node ../../scripts/remove.js target && tsc --watch" + "osd:bootstrap": "../../scripts/use_node ../../scripts/remove.js target && tsc", + "osd:watch": "../../scripts/use_node ../../scripts/remove.js target && tsc --watch" }, "dependencies": { "@osd/dev-utils": "1.0.0", diff --git a/packages/osd-opensearch/package.json b/packages/osd-opensearch/package.json index 4a9aafa875a5..02fcddb36e6f 100644 --- a/packages/osd-opensearch/package.json +++ b/packages/osd-opensearch/package.json @@ -8,8 +8,8 @@ "devOnly": true }, "scripts": { - "osd:bootstrap": "node scripts/build", - "osd:watch": "node scripts/build --watch" + "osd:bootstrap": "../../scripts/use_node scripts/build", + "osd:watch": "../../scripts/use_node scripts/build --watch" }, "dependencies": { "@opensearch-project/opensearch": "^2.1.0", diff --git a/packages/osd-plugin-generator/package.json b/packages/osd-plugin-generator/package.json index 66028c53875f..bab54de44149 100644 --- a/packages/osd-plugin-generator/package.json +++ b/packages/osd-plugin-generator/package.json @@ -5,8 +5,8 @@ "license": "Apache-2.0", "main": "target/index.js", "scripts": { - "osd:bootstrap": "node scripts/build", - "osd:watch": "node scripts/build --watch" + "osd:bootstrap": "../../scripts/use_node scripts/build", + "osd:watch": "../../scripts/use_node scripts/build --watch" }, "dependencies": { "@osd/cross-platform": "1.0.0", diff --git a/packages/osd-plugin-generator/template/package.json.ejs b/packages/osd-plugin-generator/template/package.json.ejs index 649fe19073c8..e49a20bbd8b2 100644 --- a/packages/osd-plugin-generator/template/package.json.ejs +++ b/packages/osd-plugin-generator/template/package.json.ejs @@ -4,7 +4,7 @@ "private": true, "scripts": { "build": "yarn plugin-helpers build", - "plugin-helpers": "node ../../scripts/plugin_helpers", - "osd": "node ../../scripts/osd" + "plugin-helpers": "../../scripts/use_node ../../scripts/plugin_helpers", + "osd": "../../scripts/use_node ../../scripts/osd" + } } - } \ No newline at end of file diff --git a/packages/osd-plugin-helpers/package.json b/packages/osd-plugin-helpers/package.json index 7ee45578a63b..595065322aec 100644 --- a/packages/osd-plugin-helpers/package.json +++ b/packages/osd-plugin-helpers/package.json @@ -12,7 +12,7 @@ "plugin-helpers": "bin/plugin-helpers.js" }, "scripts": { - "osd:bootstrap": "node ../../scripts/remove.js target && tsc", + "osd:bootstrap": "../../scripts/use_node ../../scripts/remove.js target && tsc", "osd:watch": "tsc --watch" }, "dependencies": { diff --git a/packages/osd-ui-shared-deps/package.json b/packages/osd-ui-shared-deps/package.json index 3df0f549e67a..2b9371401063 100644 --- a/packages/osd-ui-shared-deps/package.json +++ b/packages/osd-ui-shared-deps/package.json @@ -4,9 +4,9 @@ "private": true, "license": "Apache-2.0", "scripts": { - "build": "node scripts/build", - "osd:bootstrap": "node scripts/build --dev", - "osd:watch": "node scripts/build --dev --watch" + "build": "../../scripts/use_node scripts/build", + "osd:bootstrap": "../../scripts/use_node scripts/build --dev", + "osd:watch": "../../scripts/use_node scripts/build --dev --watch" }, "dependencies": { "@elastic/charts": "31.1.0", diff --git a/scripts/use_node b/scripts/use_node new file mode 100755 index 000000000000..f39a39434db1 --- /dev/null +++ b/scripts/use_node @@ -0,0 +1,97 @@ +#!/bin/sh + +# +# SPDX-License-Identifier: Apache-2.0 +# +# The OpenSearch Contributors require contributions made to +# this file be licensed under the Apache-2.0 license or a +# compatible open source license. +# +# Any modifications Copyright OpenSearch Contributors. See +# GitHub history for details. +# + +# This script will find the appropriate Node.js runtime binary and execute it with any +# parameters passed in. +# +# Set a variable named OSD_USE_NODE_JS_FILE_PATH to have it prefixed with OSD_HOME and executed +# Example: SET OSD_USE_NODE_JS_FILE_PATH=\src\cli\dist +# +# NODE_OPTIONS is built using config/node.options and overridden by any previously set NODE_OPTIONS. +# To pass in any specific defaults that can be overridden by both of them, use OSD_NODE_OPTS_PREFIX. + +SCRIPT="$0" + +UNAME=$(uname -s) +if [ $UNAME = "FreeBSD" ]; then + OS="freebsd" +elif [ $UNAME = "Darwin" ]; then + OS="darwin" +else + OS="other" +fi + +# SCRIPT may be an arbitrarily deep series of symlinks. Loop until we have the concrete path. +while [ -h "$SCRIPT" ] ; do + loc=$(ls -ld "$SCRIPT") + # Drop everything prior to -> + link=$(expr "$loc" : '.*-> \(.*\)$') + if expr "$link" : '/.*' > /dev/null; then + SCRIPT="$link" + else + SCRIPT=$(dirname "$SCRIPT")/"$link" + fi +done + +# Get an absolute path for OSD_HOME +OSD_HOME="$(cd "$(dirname "${SCRIPT}")/.."; pwd)" +CONFIG_DIR=${OSD_PATH_CONF:-"OSD_HOME/config"} + +# Places to look for the Node.js binary in order: OSD_NODE_HOME > NODE_HOME > bundled with OSD > system-wide +if [ ! -z "$OSD_NODE_HOME" ]; then + NODE="$OSD_NODE_HOME/bin/node" + NODE_ERROR_MSG="in OSD_NODE_HOME" + NODE_ERROR_SHOW=true +elif [ ! -z "$NODE_HOME" ]; then + NODE="$NODE_HOME/bin/node" + NODE_ERROR_MSG="in NODE_HOME" + NODE_ERROR_SHOW=true +else + NODE="$OSD_HOME/node/bin/node" + NODE_ERROR_MSG="bundled with OpenSearch Dashboards" + # A bin folder at the root is only present in release builds that have a bundled Node.js binary + if [ -x "OSD_HOME/bin" ]; then + NODE_ERROR_SHOW=true + fi +fi + +if [ -x "$NODE" ]; then + # Node.js binary was found where it was expected; no need to show an error + NODE_ERROR_SHOW= +elif [ $OS = "freebsd" ]; then + NODE="${LOCALBASE}/bin/node" +else + NODE="$(command -v node)" +fi + +if [ ! -x "$NODE" ]; then + # Irrespective of NODE_ERROR_SHOW, show the error + echo "Could not find a Node.js runtime binary $NODE_ERROR_MSG or on the system" >&2 + exit 1 +fi + +# Node.js binary was found but not where it was told to be, so show a warning +if [ ! -z "$NODE_ERROR_SHOW" ]; then + echo "Could not find a Node.js runtime binary $NODE_ERROR_MSG but found one at $NODE" >&2 +fi + +if [ -f "${CONFIG_DIR}/node.options" ]; then + OSD_NODE_OPTS="$(grep -v ^# < ${CONFIG_DIR}/node.options | xargs)" +fi + +# If a file path was provided for execution, prefix with OSD_HOME; use relative paths to avoid the need for this. +if [ ! -z "$OSD_USE_NODE_JS_FILE_PATH" ]; then + NODE_OPTIONS="$OSD_NODE_OPTS_PREFIX $OSD_NODE_OPTS $NODE_OPTIONS" "${NODE}" "${OSD_HOME}${OSD_USE_NODE_JS_FILE_PATH}" "${@}" +elif [ $# -ne 0 ]; then + NODE_OPTIONS="$OSD_NODE_OPTS_PREFIX $OSD_NODE_OPTS $NODE_OPTIONS" "${NODE}" "${@}" +fi diff --git a/scripts/use_node.bat b/scripts/use_node.bat new file mode 100644 index 000000000000..f90508f0026c --- /dev/null +++ b/scripts/use_node.bat @@ -0,0 +1,101 @@ +@ECHO OFF + +:: +:: SPDX-License-Identifier: Apache-2.0 +:: +:: The OpenSearch Contributors require contributions made to +:: this file be licensed under the Apache-2.0 license or a +:: compatible open source license. +:: +:: Any modifications Copyright OpenSearch Contributors. See +:: GitHub history for details. +:: + +:: This script will find the appropriate Node.js runtime binary and execute it with any +:: parameters passed in. +:: +:: Set a variable named OSD_USE_NODE_JS_FILE_PATH to have it prefixed with OSD_HOME and executed +:: Example: SET OSD_USE_NODE_JS_FILE_PATH=\src\cli\dist +:: +:: NODE_OPTIONS is built using config/node.options and overridden by any previously set NODE_OPTIONS. +:: To pass in any specific defaults that can be overridden by both of them, use OSD_NODE_OPTS_PREFIX. + +SETLOCAL ENABLEDELAYEDEXPANSION + +SET SCRIPT_DIR=%~dp0 +FOR %%I IN ("%SCRIPT_DIR%..") DO SET OSD_HOME=%%~dpfI + +SET CONFIG_DIR=%OSD_PATH_CONF% +IF NOT DEFINED OSD_PATH_CONF ( + SET "CONFIG_DIR=%OSD_HOME%\config" +) + +:: Places to look for the Node.js binary in order: OSD_NODE_HOME > NODE_HOME > bundled with OSD > system-wide +IF DEFINED OSD_NODE_HOME ( + SET "NODE=%OSD_NODE_HOME%\node.exe" + SET NODE_ERROR_MSG=in OSD_NODE_HOME + SET NODE_ERROR_SHOW=true + GOTO CheckNode +) +IF DEFINED NODE_HOME ( + SET "NODE=%NODE_HOME%\node.exe" + SET NODE_ERROR_MSG=in NODE_HOME + SET NODE_ERROR_SHOW=true + GOTO CheckNode +) + +SET NODE=%OSD_HOME%\node\node.exe +SET NODE_ERROR_MSG=bundled with OpenSearch Dashboards +:: A bin folder at the root is only present in release builds that have a bundled Node.js binary +IF EXIST "%OSD_HOME%\bin" ( + SET NODE_ERROR_SHOW=true +) + +:CheckNode + +IF EXIST "%NODE%" ( + :: Node.js binary was found where it was expected; no need to show an error + SET "NODE_ERROR_SHOW=" +) ELSE ( + :: Try finding the system-wide Node.js binary + FOR /F "tokens=* USEBACKQ" %%F IN (`where node.exe`) DO ( + SET "NODE=%%F" + :: Bail out after finding the first one + GOTO CheckNodeAgain + ) +) + +:CheckNodeAgain + +IF NOT EXIST "%NODE%" ( + :: Irrespective of NODE_ERROR_SHOW, show the error; NODE_ERROR_MSG is guaranteed to be set + ECHO Could not find a Node.js runtime binary %NODE_ERROR_MSG% or on the system >&2 + EXIT /B 1 +) + +:: Node.js binary was found but not where it was told to be, so show a warning +IF DEFINED NODE_ERROR_SHOW ( + ECHO Could not find a Node.js runtime binary %NODE_ERROR_MSG% but found one at %NODE% >&2 +) + +IF EXIST "%CONFIG_DIR%\node.options" ( + FOR /F "eol=# tokens=*" %%i IN (%CONFIG_DIR%\node.options) DO ( + :: This cannot accept spaces within a line of node.options + IF [!OSD_NODE_OPTS!] == [] ( + SET "OSD_NODE_OPTS=%%i" + ) ELSE ( + SET "OSD_NODE_OPTS=!OSD_NODE_OPTS! %%i" + ) + ) +) + +SET "NODE_OPTIONS=%OSD_NODE_OPTS_PREFIX% %OSD_NODE_OPTS% %NODE_OPTIONS%" + +:: If a file path was provided for execution, prefix with OSD_HOME; use relative paths to avoid the need for this. +IF DEFINED OSD_USE_NODE_JS_FILE_PATH ( + "%NODE%" "%OSD_HOME%%OSD_USE_NODE_JS_FILE_PATH%" %* +) ELSE IF NOT "%~1" == "" ( + "%NODE%" %* +) + +ENDLOCAL diff --git a/src/dev/build/tasks/bin/copy_bin_scripts_task.ts b/src/dev/build/tasks/bin/copy_bin_scripts_task.ts index 5e9a2c546b79..cde7f4a0cc2f 100644 --- a/src/dev/build/tasks/bin/copy_bin_scripts_task.ts +++ b/src/dev/build/tasks/bin/copy_bin_scripts_task.ts @@ -38,5 +38,8 @@ export const CopyBinScripts: Task = { config.resolveFromRepo('src/dev/build/tasks/bin/scripts'), build.resolvePath('bin') ); + await copyAll(config.resolveFromRepo('scripts'), build.resolvePath('bin'), { + select: ['use_node*'], + }); }, }; diff --git a/src/dev/build/tasks/bin/scripts/opensearch-dashboards b/src/dev/build/tasks/bin/scripts/opensearch-dashboards index 645dc3638b4a..49c41011da7d 100755 --- a/src/dev/build/tasks/bin/scripts/opensearch-dashboards +++ b/src/dev/build/tasks/bin/scripts/opensearch-dashboards @@ -11,13 +11,13 @@ # GitHub history for details. # -SCRIPT=$0 +SCRIPT="$0" # SCRIPT may be an arbitrarily deep series of symlinks. Loop until we have the concrete path. while [ -h "$SCRIPT" ] ; do - ls=$(ls -ld "$SCRIPT") + loc=$(ls -ld "$SCRIPT") # Drop everything prior to -> - link=$(expr "$ls" : '.*-> \(.*\)$') + link=$(expr "$loc" : '.*-> \(.*\)$') if expr "$link" : '/.*' > /dev/null; then SCRIPT="$link" else @@ -25,22 +25,7 @@ while [ -h "$SCRIPT" ] ; do fi done -DIR="$(dirname "${SCRIPT}")/.." -CONFIG_DIR=${OSD_PATH_CONF:-"$DIR/config"} +# Get an absolute path for OSD_HOME +OSD_HOME="$(cd "$(dirname "${SCRIPT}")/.."; pwd)" -if [ -x "${DIR}/node/bin/node" ]; then - NODE="${DIR}/node/bin/node" -else - NODE="$(which node)" -fi - -if [ ! -x "$NODE" ]; then - echo "unable to find usable node.js executable." - exit 1 -fi - -if [ -f "${CONFIG_DIR}/node.options" ]; then - OSD_NODE_OPTS="$(grep -v ^# < ${CONFIG_DIR}/node.options | xargs)" -fi - -NODE_OPTIONS="--no-warnings --max-http-header-size=65536 $OSD_NODE_OPTS $NODE_OPTIONS" NODE_ENV=production exec "${NODE}" "${DIR}/src/cli/dist" ${@} +OSD_NODE_OPTS_PREFIX="--no-warnings --max-http-header-size=65536" OSD_USE_NODE_JS_FILE_PATH=/src/cli/dist NODE_ENV=production ${OSD_HOME}/bin/use_node "${@}" diff --git a/src/dev/build/tasks/bin/scripts/opensearch-dashboards-keystore b/src/dev/build/tasks/bin/scripts/opensearch-dashboards-keystore index 7453ce995039..8cbf451ba4bd 100755 --- a/src/dev/build/tasks/bin/scripts/opensearch-dashboards-keystore +++ b/src/dev/build/tasks/bin/scripts/opensearch-dashboards-keystore @@ -11,13 +11,13 @@ # GitHub history for details. # -SCRIPT=$0 +SCRIPT="$0" # SCRIPT may be an arbitrarily deep series of symlinks. Loop until we have the concrete path. while [ -h "$SCRIPT" ] ; do - ls=$(ls -ld "$SCRIPT") + loc=$(ls -ld "$SCRIPT") # Drop everything prior to -> - link=$(expr "$ls" : '.*-> \(.*\)$') + link=$(expr "$loc" : '.*-> \(.*\)$') if expr "$link" : '/.*' > /dev/null; then SCRIPT="$link" else @@ -25,17 +25,7 @@ while [ -h "$SCRIPT" ] ; do fi done -DIR="$(dirname "${SCRIPT}")/.." -CONFIG_DIR=${OSD_PATH_CONF:-"$DIR/config"} -NODE="${DIR}/node/bin/node" -test -x "$NODE" -if [ ! -x "$NODE" ]; then - echo "unable to find usable node.js executable." - exit 1 -fi +# Get an absolute path for OSD_HOME +OSD_HOME="$(cd "$(dirname "${SCRIPT}")/.."; pwd)" -if [ -f "${CONFIG_DIR}/node.options" ]; then - OSD_NODE_OPTS="$(grep -v ^# < ${CONFIG_DIR}/node.options | xargs)" -fi - -NODE_OPTIONS="$OSD_NODE_OPTS $NODE_OPTIONS" "${NODE}" "${DIR}/src/cli_keystore/dist" "$@" +OSD_USE_NODE_JS_FILE_PATH=/src/cli_keystore/dist NODE_ENV=production ${OSD_HOME}/bin/use_node "${@}" diff --git a/src/dev/build/tasks/bin/scripts/opensearch-dashboards-keystore.bat b/src/dev/build/tasks/bin/scripts/opensearch-dashboards-keystore.bat index 365379a8b9bc..42c617701f54 100755 --- a/src/dev/build/tasks/bin/scripts/opensearch-dashboards-keystore.bat +++ b/src/dev/build/tasks/bin/scripts/opensearch-dashboards-keystore.bat @@ -1,35 +1,20 @@ @echo off -SETLOCAL ENABLEDELAYEDEXPANSION - -set SCRIPT_DIR=%~dp0 -for %%I in ("%SCRIPT_DIR%..") do set DIR=%%~dpfI - -set NODE=%DIR%\node\node.exe - -If Not Exist "%NODE%" ( - Echo unable to find usable node.js executable. - Exit /B 1 -) +:: +:: SPDX-License-Identifier: Apache-2.0 +:: +:: The OpenSearch Contributors require contributions made to +:: this file be licensed under the Apache-2.0 license or a +:: compatible open source license. +:: +:: Any modifications Copyright OpenSearch Contributors. See +:: GitHub history for details. +:: -set CONFIG_DIR=%OSD_PATH_CONF% -If [%OSD_PATH_CONF%] == [] ( - set CONFIG_DIR=%DIR%\config -) - -IF EXIST "%CONFIG_DIR%\node.options" ( - for /F "eol=# tokens=*" %%i in (%CONFIG_DIR%\node.options) do ( - If [!NODE_OPTIONS!] == [] ( - set "NODE_OPTIONS=%%i" - ) Else ( - set "NODE_OPTIONS=!NODE_OPTIONS! %%i" - ) - ) -) +SETLOCAL ENABLEDELAYEDEXPANSION TITLE OpenSearch Dashboards Keystore -"%NODE%" "%DIR%\src\cli_keystore\dist" %* - -:finally +SET OSD_USE_NODE_JS_FILE_PATH=src\cli_keystore\dist +call %~dp0\use_node.bat %* -ENDLOCAL +ENDLOCAL \ No newline at end of file diff --git a/src/dev/build/tasks/bin/scripts/opensearch-dashboards-plugin b/src/dev/build/tasks/bin/scripts/opensearch-dashboards-plugin index c489d02e89b9..6af40438d13b 100755 --- a/src/dev/build/tasks/bin/scripts/opensearch-dashboards-plugin +++ b/src/dev/build/tasks/bin/scripts/opensearch-dashboards-plugin @@ -11,13 +11,13 @@ # GitHub history for details. # -SCRIPT=$0 +SCRIPT="$0" # SCRIPT may be an arbitrarily deep series of symlinks. Loop until we have the concrete path. while [ -h "$SCRIPT" ] ; do - ls=$(ls -ld "$SCRIPT") + loc=$(ls -ld "$SCRIPT") # Drop everything prior to -> - link=$(expr "$ls" : '.*-> \(.*\)$') + link=$(expr "$loc" : '.*-> \(.*\)$') if expr "$link" : '/.*' > /dev/null; then SCRIPT="$link" else @@ -25,17 +25,7 @@ while [ -h "$SCRIPT" ] ; do fi done -DIR="$(dirname "${SCRIPT}")/.." -CONFIG_DIR=${OSD_PATH_CONF:-"$DIR/config"} -NODE="${DIR}/node/bin/node" -test -x "$NODE" -if [ ! -x "$NODE" ]; then - echo "unable to find usable node.js executable." - exit 1 -fi +# Get an absolute path for OSD_HOME +OSD_HOME="$(cd "$(dirname "${SCRIPT}")/.."; pwd)" -if [ -f "${CONFIG_DIR}/node.options" ]; then - OSD_NODE_OPTS="$(grep -v ^# < ${CONFIG_DIR}/node.options | xargs)" -fi - -NODE_OPTIONS="--no-warnings $OSD_NODE_OPTS $NODE_OPTIONS" NODE_ENV=production exec "${NODE}" "${DIR}/src/cli_plugin/dist" "$@" +OSD_NODE_OPTS_PREFIX="--no-warnings" OSD_USE_NODE_JS_FILE_PATH=/src/cli_plugin/dist NODE_ENV=production ${OSD_HOME}/bin/use_node "${@}" diff --git a/src/dev/build/tasks/bin/scripts/opensearch-dashboards-plugin.bat b/src/dev/build/tasks/bin/scripts/opensearch-dashboards-plugin.bat index b9bbea2ec0ed..f6a5e1c626d7 100755 --- a/src/dev/build/tasks/bin/scripts/opensearch-dashboards-plugin.bat +++ b/src/dev/build/tasks/bin/scripts/opensearch-dashboards-plugin.bat @@ -1,39 +1,24 @@ @echo off -SETLOCAL ENABLEDELAYEDEXPANSION - -set SCRIPT_DIR=%~dp0 -for %%I in ("%SCRIPT_DIR%..") do set DIR=%%~dpfI - -set NODE=%DIR%\node\node.exe -set NODE_ENV="production" - -If Not Exist "%NODE%" ( - Echo unable to find usable node.js executable. - Exit /B 1 -) +:: +:: SPDX-License-Identifier: Apache-2.0 +:: +:: The OpenSearch Contributors require contributions made to +:: this file be licensed under the Apache-2.0 license or a +:: compatible open source license. +:: +:: Any modifications Copyright OpenSearch Contributors. See +:: GitHub history for details. +:: -set CONFIG_DIR=%OSD_PATH_CONF% -If [%OSD_PATH_CONF%] == [] ( - set CONFIG_DIR=%DIR%\config -) - -IF EXIST "%CONFIG_DIR%\node.options" ( - for /F "eol=# tokens=*" %%i in (%CONFIG_DIR%\node.options) do ( - If [!NODE_OPTIONS!] == [] ( - set "NODE_OPTIONS=%%i" - ) Else ( - set "NODE_OPTIONS=!NODE_OPTIONS! %%i" - ) - ) -) +SETLOCAL ENABLEDELAYEDEXPANSION :: Include pre-defined node option -set "NODE_OPTIONS=--no-warnings %NODE_OPTIONS%" - -TITLE OpenSearch Dashboards Server -"%NODE%" "%DIR%\src\cli_plugin\dist" %* +SET "OSD_NODE_OPTS_PREFIX=--no-warnings" +SET NODE_ENV="production" -:finally +TITLE OpenSearch Dashboards +SET OSD_USE_NODE_JS_FILE_PATH=\src\cli_plugin\dist +call %~dp0\use_node.bat %* ENDLOCAL diff --git a/src/dev/build/tasks/bin/scripts/opensearch-dashboards.bat b/src/dev/build/tasks/bin/scripts/opensearch-dashboards.bat index 895f8923bd90..c6316e02d134 100755 --- a/src/dev/build/tasks/bin/scripts/opensearch-dashboards.bat +++ b/src/dev/build/tasks/bin/scripts/opensearch-dashboards.bat @@ -1,41 +1,24 @@ @echo off -SETLOCAL ENABLEDELAYEDEXPANSION - -set SCRIPT_DIR=%~dp0 -for %%I in ("%SCRIPT_DIR%..") do set DIR=%%~dpfI - -set NODE=%DIR%\node\node.exe - -set NODE_ENV="production" +:: +:: SPDX-License-Identifier: Apache-2.0 +:: +:: The OpenSearch Contributors require contributions made to +:: this file be licensed under the Apache-2.0 license or a +:: compatible open source license. +:: +:: Any modifications Copyright OpenSearch Contributors. See +:: GitHub history for details. +:: -If Not Exist "%NODE%" ( - Echo unable to find usable node.js executable. - Exit /B 1 -) - -set CONFIG_DIR=%OSD_PATH_CONF% -If [%OSD_PATH_CONF%] == [] ( - set CONFIG_DIR=%DIR%\config -) - -IF EXIST "%CONFIG_DIR%\node.options" ( - for /F "eol=# tokens=*" %%i in (%CONFIG_DIR%\node.options) do ( - If [!NODE_OPTIONS!] == [] ( - set "NODE_OPTIONS=%%i" - ) Else ( - set "NODE_OPTIONS=!NODE_OPTIONS! %%i" - ) - ) -) +SETLOCAL ENABLEDELAYEDEXPANSION :: Include pre-defined node option -set "NODE_OPTIONS=--no-warnings --max-http-header-size=65536 %NODE_OPTIONS%" - -:: This should run independently as the last instruction -:: as we need NODE_OPTIONS previously set to expand -"%NODE%" "%DIR%\src\cli\dist" %* +SET "OSD_NODE_OPTS_PREFIX=--no-warnings --max-http-header-size=65536" +SET NODE_ENV="production" -:finally +TITLE OpenSearch Dashboards +SET OSD_USE_NODE_JS_FILE_PATH=\src\cli\dist +call %~dp0\use_node.bat %* ENDLOCAL diff --git a/src/dev/ci_setup/setup.sh b/src/dev/ci_setup/setup.sh index 14548e0f0d4c..99cd2aa5e686 100755 --- a/src/dev/ci_setup/setup.sh +++ b/src/dev/ci_setup/setup.sh @@ -30,8 +30,8 @@ yarn osd bootstrap --prefer-offline ### Download opensearch snapshots ### echo " -- downloading opensearch snapshot" -node scripts/opensearch snapshot --download-only; -node scripts/opensearch snapshot --license=oss --download-only; +scripts/use_node scripts/opensearch snapshot --download-only; +scripts/use_node scripts/opensearch snapshot --license=oss --download-only; ### @@ -64,7 +64,7 @@ fi ### rebuild plugin list to ensure it's not out of date ### echo " -- building plugin list docs" -node scripts/build_plugin_list_docs +scripts/use_node scripts/build_plugin_list_docs ### ### verify no git modifications diff --git a/src/dev/ci_setup/setup_percy.sh b/src/dev/ci_setup/setup_percy.sh index c20c50eeb49e..10abb3c5044d 100755 --- a/src/dev/ci_setup/setup_percy.sh +++ b/src/dev/ci_setup/setup_percy.sh @@ -23,7 +23,7 @@ export PUPPETEER_EXECUTABLE_PATH ### ### Set Percy parallel build support environment vars ### -eval "$(node ./src/dev/ci_setup/get_percy_env)" +eval "$(./scripts/use_node ./src/dev/ci_setup/get_percy_env)" echo " -- PERCY_PARALLEL_NONCE='$PERCY_PARALLEL_NONCE'" echo " -- PERCY_PARALLEL_TOTAL='$PERCY_PARALLEL_TOTAL'" echo " -- PERCY_BRANCH='$PERCY_BRANCH'" diff --git a/src/setup_node_env/node_version_validator.test.js b/src/setup_node_env/node_version_validator.test.js index 5d3a70cd7b92..fa53b76508d9 100644 --- a/src/setup_node_env/node_version_validator.test.js +++ b/src/setup_node_env/node_version_validator.test.js @@ -155,7 +155,7 @@ const checkNodeVersionValidation = async (nodeVersion, requiredRange) => { * Note: src/dev/jest/config.js should be checked for any other config that we would like to pass to nyc */ await exec( - `node -e "${mockedProcessVersion}${mockedRequire}require('./node_version_validator.js')"`, + `"${process.execPath}" -e "${mockedProcessVersion}${mockedRequire}require('./node_version_validator.js')"`, { cwd: __dirname } ); } catch (ex) { diff --git a/tasks/config/run.js b/tasks/config/run.js index 474c6c535f13..f0e4fb7fcd66 100644 --- a/tasks/config/run.js +++ b/tasks/config/run.js @@ -36,7 +36,7 @@ const OPENSEARCH_DASHBOARDS_INSTALL_DIR = `./build/oss/opensearch-dashboards-${version}-SNAPSHOT-${process.platform}-x64`; module.exports = function () { - const NODE = 'node'; + const NODE = process.execPath; const YARN = 'yarn'; return {