Skip to content

Commit

Permalink
Allow selecting the Node.js binary OSD uses
Browse files Browse the repository at this point in the history
* 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 the build scripts to copy `use_node` into releases
* Make tests that execute node internally, honor the binary being used.

Signed-off-by: Miki <miki@amazon.com>
  • Loading branch information
AMoo-Miki committed Feb 28, 2023
1 parent 69bcbfe commit a149fad
Show file tree
Hide file tree
Showing 13 changed files with 275 additions and 213 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -138,6 +138,7 @@ Inspired from [Keep a Changelog](https://keepachangelog.com/en/1.0.0/)
- Allow relaxing the Node.js runtime version requirement ([3402](https://github.com/opensearch-project/OpenSearch-Dashboards/pull/3402))
- 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))
- Allow selecting the Node.js binary using `NODE_HOME` and `OSD_NODE_HOME` ([3508](https://github.com/opensearch-project/OpenSearch-Dashboards/pull/3508))

### 🪛 Refactoring

Expand Down
58 changes: 29 additions & 29 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -39,41 +39,41 @@
},
"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: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 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",
"debug": "node --nolazy --inspect scripts/opensearch_dashboards --dev",
"debug-break": "node --nolazy --inspect-brk scripts/opensearch_dashboards --dev",
"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",
"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: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": "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)\""
"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",
Expand Down
90 changes: 90 additions & 0 deletions scripts/use_node
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
#!/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"

OS=`uname -s | awk '{print tolower}'`

# 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
101 changes: 101 additions & 0 deletions scripts/use_node.bat
Original file line number Diff line number Diff line change
@@ -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
3 changes: 3 additions & 0 deletions src/dev/build/tasks/bin/copy_bin_scripts_task.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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*'],
});
},
};
34 changes: 1 addition & 33 deletions src/dev/build/tasks/bin/scripts/opensearch-dashboards
Original file line number Diff line number Diff line change
Expand Up @@ -11,36 +11,4 @@
# GitHub history for details.
#

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")
# Drop everything prior to ->
link=$(expr "$ls" : '.*-> \(.*\)$')
if expr "$link" : '/.*' > /dev/null; then
SCRIPT="$link"
else
SCRIPT=$(dirname "$SCRIPT")/"$link"
fi
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)"
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 ./use_node "${@}"
29 changes: 1 addition & 28 deletions src/dev/build/tasks/bin/scripts/opensearch-dashboards-keystore
Original file line number Diff line number Diff line change
Expand Up @@ -11,31 +11,4 @@
# GitHub history for details.
#

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")
# Drop everything prior to ->
link=$(expr "$ls" : '.*-> \(.*\)$')
if expr "$link" : '/.*' > /dev/null; then
SCRIPT="$link"
else
SCRIPT=$(dirname "$SCRIPT")/"$link"
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

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 ./use_node "${@}"
Loading

0 comments on commit a149fad

Please sign in to comment.