Skip to content

Commit

Permalink
[FEATURE] Add OPENSEARCH_JAVA_HOME env to override JAVA_HOME (#2001)
Browse files Browse the repository at this point in the history
Signed-off-by: Andriy Redko <[email protected]>
  • Loading branch information
reta authored Feb 2, 2022
1 parent 6da253b commit c8ac037
Show file tree
Hide file tree
Showing 9 changed files with 125 additions and 13 deletions.
1 change: 1 addition & 0 deletions Vagrantfile
Original file line number Diff line number Diff line change
Expand Up @@ -484,6 +484,7 @@ def sh_install_deps(config,
cat \<\<SUDOERS_VARS > /etc/sudoers.d/opensearch_vars
Defaults env_keep += "JAVA_HOME"
Defaults env_keep += "SYSTEM_JAVA_HOME"
Defaults env_keep += "OPENSEARCH_JAVA_HOME"
SUDOERS_VARS
chmod 0440 /etc/sudoers.d/opensearch_vars
SHELL
Expand Down
2 changes: 1 addition & 1 deletion distribution/packages/src/common/env/opensearch
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
#OPENSEARCH_HOME=/usr/share/opensearch

# OpenSearch Java path
#JAVA_HOME=
#OPENSEARCH_JAVA_HOME=

# OpenSearch configuration directory
# Note: this setting will be shared with command-line tools
Expand Down
3 changes: 2 additions & 1 deletion distribution/packages/src/deb/init.d/opensearch
Original file line number Diff line number Diff line change
Expand Up @@ -66,8 +66,9 @@ DAEMON=$OPENSEARCH_HOME/bin/opensearch
DAEMON_OPTS="-d -p $PID_FILE"

export OPENSEARCH_JAVA_OPTS
export JAVA_HOME
export OPENSEARCH_PATH_CONF
export JAVA_HOME
export OPENSEARCH_JAVA_HOME

if [ ! -x "$DAEMON" ]; then
echo "The opensearch startup script does not exists or it is not executable, tried: $DAEMON"
Expand Down
1 change: 1 addition & 0 deletions distribution/packages/src/rpm/init.d/opensearch
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ export OPENSEARCH_JAVA_OPTS
export JAVA_HOME
export OPENSEARCH_PATH_CONF
export OPENSEARCH_STARTUP_SLEEP_TIME
export OPENSEARCH_JAVA_HOME

lockfile=/var/lock/subsys/$prog

Expand Down
7 changes: 5 additions & 2 deletions distribution/src/bin/opensearch-env
Original file line number Diff line number Diff line change
Expand Up @@ -44,8 +44,11 @@ OPENSEARCH_HOME=`dirname "$OPENSEARCH_HOME"`
# now set the classpath
OPENSEARCH_CLASSPATH="$OPENSEARCH_HOME/lib/*"

# now set the path to java
if [ ! -z "$JAVA_HOME" ]; then
# now set the path to java: OPENSEARCH_JAVA_HOME -> JAVA_HOME -> bundled JDK
if [ ! -z "$OPENSEARCH_JAVA_HOME" ]; then
JAVA="$OPENSEARCH_JAVA_HOME/bin/java"
JAVA_TYPE="OPENSEARCH_JAVA_HOME"
elif [ ! -z "$JAVA_HOME" ]; then
JAVA="$JAVA_HOME/bin/java"
JAVA_TYPE="JAVA_HOME"
else
Expand Down
15 changes: 9 additions & 6 deletions distribution/src/bin/opensearch-env.bat
Original file line number Diff line number Diff line change
Expand Up @@ -39,16 +39,19 @@ if "%1" == "nojava" (
exit /b
)

rem compariing to empty string makes this equivalent to bash -v check on env var
rem comparing to empty string makes this equivalent to bash -v check on env var
rem and allows to effectively force use of the bundled jdk when launching OpenSearch
rem by setting JAVA_HOME=
if "%JAVA_HOME%" == "" (
rem by setting OPENSEARCH_JAVA_HOME= and JAVA_HOME=
if not "%OPENSEARCH_JAVA_HOME%" == "" (
set JAVA="%OPENSEARCH_JAVA_HOME%\bin\java.exe"
set JAVA_TYPE=OPENSEARCH_JAVA_HOME
) else if not "%JAVA_HOME%" == "" (
set JAVA="%JAVA_HOME%\bin\java.exe"
set JAVA_TYPE=JAVA_HOME
) else (
set JAVA="%OPENSEARCH_HOME%\jdk\bin\java.exe"
set JAVA_HOME="%OPENSEARCH_HOME%\jdk"
set JAVA_TYPE=bundled jdk
) else (
set JAVA="%JAVA_HOME%\bin\java.exe"
set JAVA_TYPE=JAVA_HOME
)

if not exist !JAVA! (
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,7 @@ public void test20PluginsListWithNoPlugins() throws Exception {
public void test30MissingBundledJdk() throws Exception {
final Installation.Executables bin = installation.executables();
sh.getEnv().remove("JAVA_HOME");
sh.getEnv().remove("OPENSEARCH_JAVA_HOME");

final Path relocatedJdk = installation.bundledJdk.getParent().resolve("jdk.relocated");

Expand All @@ -105,6 +106,7 @@ public void test30MissingBundledJdk() throws Exception {

public void test31BadJavaHome() throws Exception {
final Installation.Executables bin = installation.executables();
sh.getEnv().remove("OPENSEARCH_JAVA_HOME");
sh.getEnv().put("JAVA_HOME", "doesnotexist");

// ask for opensearch version to quickly exit if java is actually found (ie test failure)
Expand All @@ -114,11 +116,23 @@ public void test31BadJavaHome() throws Exception {

}

public void test31BadOpensearchJavaHome() throws Exception {
final Installation.Executables bin = installation.executables();
sh.getEnv().put("OPENSEARCH_JAVA_HOME", "doesnotexist");

// ask for opensearch version to quickly exit if java is actually found (ie test failure)
final Result runResult = sh.runIgnoreExitCode(bin.opensearch.toString() + " -V");
assertThat(runResult.exitCode, is(1));
assertThat(runResult.stderr, containsString("could not find java in OPENSEARCH_JAVA_HOME"));

}

public void test32SpecialCharactersInJdkPath() throws Exception {
final Installation.Executables bin = installation.executables();
assumeTrue("Only run this test when we know where the JDK is.", distribution().hasJdk);

final Path relocatedJdk = installation.bundledJdk.getParent().resolve("a (special) path");
sh.getEnv().remove("OPENSEARCH_JAVA_HOME");
sh.getEnv().put("JAVA_HOME", relocatedJdk.toString());

try {
Expand Down Expand Up @@ -154,6 +168,8 @@ public void test50StartAndStop() throws Exception {
}

public void test51JavaHomeOverride() throws Exception {
sh.getEnv().remove("OPENSEARCH_JAVA_HOME");

Platforms.onLinux(() -> {
String systemJavaHome1 = sh.run("echo $SYSTEM_JAVA_HOME").stdout.trim();
sh.getEnv().put("JAVA_HOME", systemJavaHome1);
Expand All @@ -171,8 +187,29 @@ public void test51JavaHomeOverride() throws Exception {
assertThat(FileUtils.slurpAllLogs(installation.logs, "opensearch.log", "*.log.gz"), containsString(systemJavaHome1));
}

public void test52BundledJdkRemoved() throws Exception {
public void test51OpensearchJavaHomeOverride() throws Exception {
Platforms.onLinux(() -> {
String systemJavaHome1 = sh.run("echo $SYSTEM_JAVA_HOME").stdout.trim();
sh.getEnv().put("OPENSEARCH_JAVA_HOME", systemJavaHome1);
sh.getEnv().put("JAVA_HOME", "doesnotexist");
});
Platforms.onWindows(() -> {
final String systemJavaHome1 = sh.run("$Env:SYSTEM_JAVA_HOME").stdout.trim();
sh.getEnv().put("OPENSEARCH_JAVA_HOME", systemJavaHome1);
sh.getEnv().put("JAVA_HOME", "doesnotexist");
});

startOpenSearch();
ServerUtils.runOpenSearchTests();
stopOpenSearch();

String systemJavaHome1 = sh.getEnv().get("OPENSEARCH_JAVA_HOME");
assertThat(FileUtils.slurpAllLogs(installation.logs, "opensearch.log", "*.log.gz"), containsString(systemJavaHome1));
}

public void test52JavaHomeBundledJdkRemoved() throws Exception {
assumeThat(distribution().hasJdk, is(true));
sh.getEnv().remove("OPENSEARCH_JAVA_HOME");

Path relocatedJdk = installation.bundledJdk.getParent().resolve("jdk.relocated");
try {
Expand All @@ -197,7 +234,37 @@ public void test52BundledJdkRemoved() throws Exception {
}
}

public void test52OpensearchJavaHomeBundledJdkRemoved() throws Exception {
assumeThat(distribution().hasJdk, is(true));

Path relocatedJdk = installation.bundledJdk.getParent().resolve("jdk.relocated");
try {
mv(installation.bundledJdk, relocatedJdk);
Platforms.onLinux(() -> {
String systemJavaHome1 = sh.run("echo $SYSTEM_JAVA_HOME").stdout.trim();
sh.getEnv().put("OPENSEARCH_JAVA_HOME", systemJavaHome1);
sh.getEnv().put("JAVA_HOME", "doesnotexist");
});
Platforms.onWindows(() -> {
final String systemJavaHome1 = sh.run("$Env:SYSTEM_JAVA_HOME").stdout.trim();
sh.getEnv().put("OPENSEARCH_JAVA_HOME", systemJavaHome1);
sh.getEnv().put("JAVA_HOME", "doesnotexist");
});

startOpenSearch();
ServerUtils.runOpenSearchTests();
stopOpenSearch();

String systemJavaHome1 = sh.getEnv().get("OPENSEARCH_JAVA_HOME");
assertThat(FileUtils.slurpAllLogs(installation.logs, "opensearch.log", "*.log.gz"), containsString(systemJavaHome1));
} finally {
mv(relocatedJdk, installation.bundledJdk);
}
}

public void test53JavaHomeWithSpecialCharacters() throws Exception {
sh.getEnv().remove("OPENSEARCH_JAVA_HOME");

Platforms.onWindows(() -> {
String javaPath = "C:\\Program Files (x86)\\java";
try {
Expand Down Expand Up @@ -250,6 +317,7 @@ public void test54ForceBundledJdkEmptyJavaHome() throws Exception {
// cleanup from previous test
rm(installation.config("opensearch.keystore"));

sh.getEnv().put("OPENSEARCH_JAVA_HOME", "");
sh.getEnv().put("JAVA_HOME", "");

startOpenSearch();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
package org.opensearch.packaging.test;

import com.carrotsearch.randomizedtesting.JUnit3MethodProvider;
import com.carrotsearch.randomizedtesting.RandomizedContext;
import com.carrotsearch.randomizedtesting.RandomizedRunner;
import com.carrotsearch.randomizedtesting.annotations.TestCaseOrdering;
import com.carrotsearch.randomizedtesting.annotations.TestGroup;
Expand Down Expand Up @@ -182,11 +183,19 @@ public void setup() throws Exception {

sh.reset();
if (distribution().hasJdk == false) {
Platforms.onLinux(() -> sh.getEnv().put("JAVA_HOME", systemJavaHome));
Platforms.onWindows(() -> sh.getEnv().put("JAVA_HOME", systemJavaHome));
// Randomly switch between JAVA_HOME and OPENSEARCH_JAVA_HOME
final String javaHomeEnv = randomBoolean() ? "JAVA_HOME" : "OPENSEARCH_JAVA_HOME";
logger.info("Using " + javaHomeEnv);

Platforms.onLinux(() -> sh.getEnv().put(javaHomeEnv, systemJavaHome));
Platforms.onWindows(() -> sh.getEnv().put(javaHomeEnv, systemJavaHome));
}
}

private boolean randomBoolean() {
return RandomizedContext.current().getRandom().nextBoolean();
}

@After
public void teardown() throws Exception {
if (installation != null && failed == false) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -149,12 +149,20 @@ public void test13InstallMissingBundledJdk() throws IOException {
}

public void test14InstallBadJavaHome() throws IOException {
sh.getEnv().put("OPENSEARCH_JAVA_HOME", "");
sh.getEnv().put("JAVA_HOME", "doesnotexist");
Result result = sh.runIgnoreExitCode(serviceScript + " install");
assertThat(result.exitCode, equalTo(1));
assertThat(result.stderr, containsString("could not find java in JAVA_HOME"));
}

public void test14InstallBadOpensearchJavaHome() throws IOException {
sh.getEnv().put("OPENSEARCH_JAVA_HOME", "doesnotexist");
Result result = sh.runIgnoreExitCode(serviceScript + " install");
assertThat(result.exitCode, equalTo(1));
assertThat(result.stderr, containsString("could not find java in OPENSEARCH_JAVA_HOME"));
}

public void test15RemoveNotInstalled() {
Result result = assertFailure(serviceScript + " remove", 1);
assertThat(result.stdout, containsString("Failed removing '" + DEFAULT_ID + "' service"));
Expand All @@ -163,6 +171,7 @@ public void test15RemoveNotInstalled() {
public void test16InstallSpecialCharactersInJdkPath() throws IOException {
assumeTrue("Only run this test when we know where the JDK is.", distribution().hasJdk);
final Path relocatedJdk = installation.bundledJdk.getParent().resolve("a (special) jdk");
sh.getEnv().put("OPENSEARCH_JAVA_HOME", "");
sh.getEnv().put("JAVA_HOME", relocatedJdk.toString());

try {
Expand Down Expand Up @@ -248,6 +257,7 @@ public void test32StopNotStarted() throws IOException {

public void test33JavaChanged() throws Exception {
final Path relocatedJdk = installation.bundledJdk.getParent().resolve("jdk.relocated");
sh.getEnv().put("OPENSEARCH_JAVA_HOME", "");

try {
mv(installation.bundledJdk, relocatedJdk);
Expand All @@ -261,6 +271,22 @@ public void test33JavaChanged() throws Exception {
}
}

public void test33OpensearchJavaChanged() throws Exception {
final Path relocatedJdk = installation.bundledJdk.getParent().resolve("jdk.relocated");
sh.getEnv().put("JAVA_HOME", "");

try {
mv(installation.bundledJdk, relocatedJdk);
sh.getEnv().put("OPENSEARCH_JAVA_HOME", relocatedJdk.toString());
assertCommand(serviceScript + " install");
sh.getEnv().remove("OPENSEARCH_JAVA_HOME");
assertCommand(serviceScript + " start");
assertStartedAndStop();
} finally {
mv(relocatedJdk, installation.bundledJdk);
}
}

public void test60Manager() throws IOException {
Path serviceMgr = installation.bin("opensearch-service-mgr.exe");
Path tmpServiceMgr = serviceMgr.getParent().resolve(serviceMgr.getFileName() + ".tmp");
Expand Down

0 comments on commit c8ac037

Please sign in to comment.