[FEATURE] Add OPENSEARCH_JAVA_HOME env to override JAVA_HOME (#2001)

Signed-off-by: Andriy Redko <andriy.redko@aiven.io>
This commit is contained in:
Andriy Redko 2022-02-02 13:55:22 -05:00 committed by GitHub
parent 6da253b8ff
commit c8ac037389
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
9 changed files with 125 additions and 13 deletions

1
Vagrantfile vendored
View File

@ -484,6 +484,7 @@ def sh_install_deps(config,
cat \<\<SUDOERS_VARS > /etc/sudoers.d/opensearch_vars cat \<\<SUDOERS_VARS > /etc/sudoers.d/opensearch_vars
Defaults env_keep += "JAVA_HOME" Defaults env_keep += "JAVA_HOME"
Defaults env_keep += "SYSTEM_JAVA_HOME" Defaults env_keep += "SYSTEM_JAVA_HOME"
Defaults env_keep += "OPENSEARCH_JAVA_HOME"
SUDOERS_VARS SUDOERS_VARS
chmod 0440 /etc/sudoers.d/opensearch_vars chmod 0440 /etc/sudoers.d/opensearch_vars
SHELL SHELL

View File

@ -6,7 +6,7 @@
#OPENSEARCH_HOME=/usr/share/opensearch #OPENSEARCH_HOME=/usr/share/opensearch
# OpenSearch Java path # OpenSearch Java path
#JAVA_HOME= #OPENSEARCH_JAVA_HOME=
# OpenSearch configuration directory # OpenSearch configuration directory
# Note: this setting will be shared with command-line tools # Note: this setting will be shared with command-line tools

View File

@ -66,8 +66,9 @@ DAEMON=$OPENSEARCH_HOME/bin/opensearch
DAEMON_OPTS="-d -p $PID_FILE" DAEMON_OPTS="-d -p $PID_FILE"
export OPENSEARCH_JAVA_OPTS export OPENSEARCH_JAVA_OPTS
export JAVA_HOME
export OPENSEARCH_PATH_CONF export OPENSEARCH_PATH_CONF
export JAVA_HOME
export OPENSEARCH_JAVA_HOME
if [ ! -x "$DAEMON" ]; then if [ ! -x "$DAEMON" ]; then
echo "The opensearch startup script does not exists or it is not executable, tried: $DAEMON" echo "The opensearch startup script does not exists or it is not executable, tried: $DAEMON"

View File

@ -53,6 +53,7 @@ export OPENSEARCH_JAVA_OPTS
export JAVA_HOME export JAVA_HOME
export OPENSEARCH_PATH_CONF export OPENSEARCH_PATH_CONF
export OPENSEARCH_STARTUP_SLEEP_TIME export OPENSEARCH_STARTUP_SLEEP_TIME
export OPENSEARCH_JAVA_HOME
lockfile=/var/lock/subsys/$prog lockfile=/var/lock/subsys/$prog

View File

@ -44,8 +44,11 @@ OPENSEARCH_HOME=`dirname "$OPENSEARCH_HOME"`
# now set the classpath # now set the classpath
OPENSEARCH_CLASSPATH="$OPENSEARCH_HOME/lib/*" OPENSEARCH_CLASSPATH="$OPENSEARCH_HOME/lib/*"
# now set the path to java # now set the path to java: OPENSEARCH_JAVA_HOME -> JAVA_HOME -> bundled JDK
if [ ! -z "$JAVA_HOME" ]; then 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="$JAVA_HOME/bin/java"
JAVA_TYPE="JAVA_HOME" JAVA_TYPE="JAVA_HOME"
else else

View File

@ -39,16 +39,19 @@ if "%1" == "nojava" (
exit /b 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 and allows to effectively force use of the bundled jdk when launching OpenSearch
rem by setting JAVA_HOME= rem by setting OPENSEARCH_JAVA_HOME= and JAVA_HOME=
if "%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="%OPENSEARCH_HOME%\jdk\bin\java.exe"
set JAVA_HOME="%OPENSEARCH_HOME%\jdk" set JAVA_HOME="%OPENSEARCH_HOME%\jdk"
set JAVA_TYPE=bundled jdk set JAVA_TYPE=bundled jdk
) else (
set JAVA="%JAVA_HOME%\bin\java.exe"
set JAVA_TYPE=JAVA_HOME
) )
if not exist !JAVA! ( if not exist !JAVA! (

View File

@ -85,6 +85,7 @@ public class ArchiveTests extends PackagingTestCase {
public void test30MissingBundledJdk() throws Exception { public void test30MissingBundledJdk() throws Exception {
final Installation.Executables bin = installation.executables(); final Installation.Executables bin = installation.executables();
sh.getEnv().remove("JAVA_HOME"); sh.getEnv().remove("JAVA_HOME");
sh.getEnv().remove("OPENSEARCH_JAVA_HOME");
final Path relocatedJdk = installation.bundledJdk.getParent().resolve("jdk.relocated"); final Path relocatedJdk = installation.bundledJdk.getParent().resolve("jdk.relocated");
@ -105,6 +106,7 @@ public class ArchiveTests extends PackagingTestCase {
public void test31BadJavaHome() throws Exception { public void test31BadJavaHome() throws Exception {
final Installation.Executables bin = installation.executables(); final Installation.Executables bin = installation.executables();
sh.getEnv().remove("OPENSEARCH_JAVA_HOME");
sh.getEnv().put("JAVA_HOME", "doesnotexist"); sh.getEnv().put("JAVA_HOME", "doesnotexist");
// ask for opensearch version to quickly exit if java is actually found (ie test failure) // ask for opensearch version to quickly exit if java is actually found (ie test failure)
@ -114,11 +116,23 @@ public class ArchiveTests extends PackagingTestCase {
} }
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 { public void test32SpecialCharactersInJdkPath() throws Exception {
final Installation.Executables bin = installation.executables(); final Installation.Executables bin = installation.executables();
assumeTrue("Only run this test when we know where the JDK is.", distribution().hasJdk); assumeTrue("Only run this test when we know where the JDK is.", distribution().hasJdk);
final Path relocatedJdk = installation.bundledJdk.getParent().resolve("a (special) path"); final Path relocatedJdk = installation.bundledJdk.getParent().resolve("a (special) path");
sh.getEnv().remove("OPENSEARCH_JAVA_HOME");
sh.getEnv().put("JAVA_HOME", relocatedJdk.toString()); sh.getEnv().put("JAVA_HOME", relocatedJdk.toString());
try { try {
@ -154,6 +168,8 @@ public class ArchiveTests extends PackagingTestCase {
} }
public void test51JavaHomeOverride() throws Exception { public void test51JavaHomeOverride() throws Exception {
sh.getEnv().remove("OPENSEARCH_JAVA_HOME");
Platforms.onLinux(() -> { Platforms.onLinux(() -> {
String systemJavaHome1 = sh.run("echo $SYSTEM_JAVA_HOME").stdout.trim(); String systemJavaHome1 = sh.run("echo $SYSTEM_JAVA_HOME").stdout.trim();
sh.getEnv().put("JAVA_HOME", systemJavaHome1); sh.getEnv().put("JAVA_HOME", systemJavaHome1);
@ -171,8 +187,29 @@ public class ArchiveTests extends PackagingTestCase {
assertThat(FileUtils.slurpAllLogs(installation.logs, "opensearch.log", "*.log.gz"), containsString(systemJavaHome1)); 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)); assumeThat(distribution().hasJdk, is(true));
sh.getEnv().remove("OPENSEARCH_JAVA_HOME");
Path relocatedJdk = installation.bundledJdk.getParent().resolve("jdk.relocated"); Path relocatedJdk = installation.bundledJdk.getParent().resolve("jdk.relocated");
try { try {
@ -197,7 +234,37 @@ public class ArchiveTests extends PackagingTestCase {
} }
} }
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 { public void test53JavaHomeWithSpecialCharacters() throws Exception {
sh.getEnv().remove("OPENSEARCH_JAVA_HOME");
Platforms.onWindows(() -> { Platforms.onWindows(() -> {
String javaPath = "C:\\Program Files (x86)\\java"; String javaPath = "C:\\Program Files (x86)\\java";
try { try {
@ -250,6 +317,7 @@ public class ArchiveTests extends PackagingTestCase {
// cleanup from previous test // cleanup from previous test
rm(installation.config("opensearch.keystore")); rm(installation.config("opensearch.keystore"));
sh.getEnv().put("OPENSEARCH_JAVA_HOME", "");
sh.getEnv().put("JAVA_HOME", ""); sh.getEnv().put("JAVA_HOME", "");
startOpenSearch(); startOpenSearch();

View File

@ -33,6 +33,7 @@
package org.opensearch.packaging.test; package org.opensearch.packaging.test;
import com.carrotsearch.randomizedtesting.JUnit3MethodProvider; import com.carrotsearch.randomizedtesting.JUnit3MethodProvider;
import com.carrotsearch.randomizedtesting.RandomizedContext;
import com.carrotsearch.randomizedtesting.RandomizedRunner; import com.carrotsearch.randomizedtesting.RandomizedRunner;
import com.carrotsearch.randomizedtesting.annotations.TestCaseOrdering; import com.carrotsearch.randomizedtesting.annotations.TestCaseOrdering;
import com.carrotsearch.randomizedtesting.annotations.TestGroup; import com.carrotsearch.randomizedtesting.annotations.TestGroup;
@ -182,11 +183,19 @@ public abstract class PackagingTestCase extends Assert {
sh.reset(); sh.reset();
if (distribution().hasJdk == false) { if (distribution().hasJdk == false) {
Platforms.onLinux(() -> sh.getEnv().put("JAVA_HOME", systemJavaHome)); // Randomly switch between JAVA_HOME and OPENSEARCH_JAVA_HOME
Platforms.onWindows(() -> sh.getEnv().put("JAVA_HOME", systemJavaHome)); 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 @After
public void teardown() throws Exception { public void teardown() throws Exception {
if (installation != null && failed == false) { if (installation != null && failed == false) {

View File

@ -149,12 +149,20 @@ public class WindowsServiceTests extends PackagingTestCase {
} }
public void test14InstallBadJavaHome() throws IOException { public void test14InstallBadJavaHome() throws IOException {
sh.getEnv().put("OPENSEARCH_JAVA_HOME", "");
sh.getEnv().put("JAVA_HOME", "doesnotexist"); sh.getEnv().put("JAVA_HOME", "doesnotexist");
Result result = sh.runIgnoreExitCode(serviceScript + " install"); Result result = sh.runIgnoreExitCode(serviceScript + " install");
assertThat(result.exitCode, equalTo(1)); assertThat(result.exitCode, equalTo(1));
assertThat(result.stderr, containsString("could not find java in JAVA_HOME")); 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() { public void test15RemoveNotInstalled() {
Result result = assertFailure(serviceScript + " remove", 1); Result result = assertFailure(serviceScript + " remove", 1);
assertThat(result.stdout, containsString("Failed removing '" + DEFAULT_ID + "' service")); assertThat(result.stdout, containsString("Failed removing '" + DEFAULT_ID + "' service"));
@ -163,6 +171,7 @@ public class WindowsServiceTests extends PackagingTestCase {
public void test16InstallSpecialCharactersInJdkPath() throws IOException { public void test16InstallSpecialCharactersInJdkPath() throws IOException {
assumeTrue("Only run this test when we know where the JDK is.", distribution().hasJdk); assumeTrue("Only run this test when we know where the JDK is.", distribution().hasJdk);
final Path relocatedJdk = installation.bundledJdk.getParent().resolve("a (special) jdk"); final Path relocatedJdk = installation.bundledJdk.getParent().resolve("a (special) jdk");
sh.getEnv().put("OPENSEARCH_JAVA_HOME", "");
sh.getEnv().put("JAVA_HOME", relocatedJdk.toString()); sh.getEnv().put("JAVA_HOME", relocatedJdk.toString());
try { try {
@ -248,6 +257,7 @@ public class WindowsServiceTests extends PackagingTestCase {
public void test33JavaChanged() throws Exception { public void test33JavaChanged() throws Exception {
final Path relocatedJdk = installation.bundledJdk.getParent().resolve("jdk.relocated"); final Path relocatedJdk = installation.bundledJdk.getParent().resolve("jdk.relocated");
sh.getEnv().put("OPENSEARCH_JAVA_HOME", "");
try { try {
mv(installation.bundledJdk, relocatedJdk); mv(installation.bundledJdk, relocatedJdk);
@ -261,6 +271,22 @@ public class WindowsServiceTests extends PackagingTestCase {
} }
} }
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 { public void test60Manager() throws IOException {
Path serviceMgr = installation.bin("opensearch-service-mgr.exe"); Path serviceMgr = installation.bin("opensearch-service-mgr.exe");
Path tmpServiceMgr = serviceMgr.getParent().resolve(serviceMgr.getFileName() + ".tmp"); Path tmpServiceMgr = serviceMgr.getParent().resolve(serviceMgr.getFileName() + ".tmp");