Skip to content

Commit

Permalink
Bat scripts to work with JAVA_HOME with parentheses backport(#39712) (#…
Browse files Browse the repository at this point in the history
…40768)

the elasticsearch.bat and elasticsearch-env.bat won't work if JAVA
contains parentheses. This seems to be the limitation of FOR /F IN
(command) DO syntax.
The JAVA variable present in a command contains a path to a binary to
start elasticsearch (with spaces & parens). We can workaround the
problem of spaces and parentheses in this path by referring this
variable with a CALL command.
Note that executing binaries with CALL is an undocumented behaviour (but works)
closes #38578
closes #38624
closes #33405
closes #30606
backports:
* Bat scripts to work with JAVA_HOME with parentheses(#39712)
* Link to SYSTEM_JAVA_HOME on windows (#40806)
  • Loading branch information
pgomulka authored Apr 5, 2019
1 parent 3c3d94f commit da2f6be
Show file tree
Hide file tree
Showing 10 changed files with 107 additions and 55 deletions.
2 changes: 1 addition & 1 deletion distribution/src/bin/elasticsearch-env.bat
Original file line number Diff line number Diff line change
Expand Up @@ -58,5 +58,5 @@ set ES_DISTRIBUTION_FLAVOR=${es.distribution.flavor}
set ES_DISTRIBUTION_TYPE=${es.distribution.type}

if not defined ES_TMPDIR (
for /f "tokens=* usebackq" %%a in (`"%JAVA% -cp "!ES_CLASSPATH!" "org.elasticsearch.tools.launchers.TempDirectory""`) do set ES_TMPDIR=%%a
for /f "tokens=* usebackq" %%a in (`CALL %JAVA% -cp "!ES_CLASSPATH!" "org.elasticsearch.tools.launchers.TempDirectory"`) do set ES_TMPDIR=%%a
)
4 changes: 2 additions & 2 deletions distribution/src/bin/elasticsearch.bat
Original file line number Diff line number Diff line change
Expand Up @@ -41,9 +41,9 @@ IF ERRORLEVEL 1 (
EXIT /B %ERRORLEVEL%
)

set "ES_JVM_OPTIONS=%ES_PATH_CONF%\jvm.options"
set ES_JVM_OPTIONS=%ES_PATH_CONF%\jvm.options
@setlocal
for /F "usebackq delims=" %%a in (`"%JAVA% -cp "!ES_CLASSPATH!" "org.elasticsearch.tools.launchers.JvmOptionsParser" "!ES_JVM_OPTIONS!" || echo jvm_options_parser_failed"`) do set JVM_OPTIONS=%%a
for /F "usebackq delims=" %%a in (`CALL %JAVA% -cp "!ES_CLASSPATH!" "org.elasticsearch.tools.launchers.JvmOptionsParser" "!ES_JVM_OPTIONS!" ^|^| echo jvm_options_parser_failed`) do set JVM_OPTIONS=%%a
@endlocal & set "MAYBE_JVM_OPTIONS_PARSER_FAILED=%JVM_OPTIONS%" & set ES_JAVA_OPTS=%JVM_OPTIONS:${ES_TMPDIR}=!ES_TMPDIR!% %ES_JAVA_OPTS%

if "%MAYBE_JVM_OPTIONS_PARSER_FAILED%" == "jvm_options_parser_failed" (
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,29 +20,31 @@
package org.elasticsearch.packaging.test;

import com.carrotsearch.randomizedtesting.annotations.TestCaseOrdering;
import com.carrotsearch.randomizedtesting.generators.RandomStrings;
import org.apache.http.client.fluent.Request;
import org.elasticsearch.packaging.util.Archives;
import org.elasticsearch.packaging.util.Distribution;
import org.elasticsearch.packaging.util.FileUtils;
import org.elasticsearch.packaging.util.Installation;
import org.elasticsearch.packaging.util.Platforms;
import org.elasticsearch.packaging.util.ServerUtils;
import org.elasticsearch.packaging.util.Shell;
import org.elasticsearch.packaging.util.Shell.Result;
import org.junit.Before;
import org.junit.BeforeClass;

import org.elasticsearch.packaging.util.Distribution;
import org.elasticsearch.packaging.util.Installation;

import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.Arrays;
import java.util.stream.Stream;

import static com.carrotsearch.randomizedtesting.RandomizedTest.getRandom;
import static java.util.stream.Collectors.joining;
import static org.elasticsearch.packaging.util.Archives.ARCHIVE_OWNER;
import static org.elasticsearch.packaging.util.Cleanup.cleanEverything;
import static org.elasticsearch.packaging.util.Archives.installArchive;
import static org.elasticsearch.packaging.util.Archives.verifyArchiveInstallation;
import static org.elasticsearch.packaging.util.Cleanup.cleanEverything;
import static org.elasticsearch.packaging.util.FileMatcher.Fileness.File;
import static org.elasticsearch.packaging.util.FileMatcher.file;
import static org.elasticsearch.packaging.util.FileMatcher.p660;
Expand All @@ -53,6 +55,7 @@
import static org.elasticsearch.packaging.util.FileUtils.rm;
import static org.elasticsearch.packaging.util.ServerUtils.makeRequest;
import static org.hamcrest.CoreMatchers.containsString;
import static org.hamcrest.CoreMatchers.equalTo;
import static org.hamcrest.CoreMatchers.is;
import static org.hamcrest.CoreMatchers.not;
import static org.hamcrest.CoreMatchers.notNullValue;
Expand All @@ -76,7 +79,7 @@ public abstract class ArchiveTestCase extends PackagingTestCase {
protected abstract Distribution distribution();

@BeforeClass
public static void cleanup() {
public static void cleanup() throws Exception {
installation = null;
cleanEverything();
}
Expand All @@ -86,12 +89,12 @@ public void onlyCompatibleDistributions() {
assumeTrue("only compatible distributions", distribution().packaging.compatible);
}

public void test10Install() {
public void test10Install() throws Exception {
installation = installArchive(distribution());
verifyArchiveInstallation(installation, distribution());
}

public void test20PluginsListWithNoPlugins() {
public void test20PluginsListWithNoPlugins() throws Exception {
assumeThat(installation, is(notNullValue()));

final Installation.Executables bin = installation.executables();
Expand All @@ -101,7 +104,7 @@ public void test20PluginsListWithNoPlugins() {
assertThat(r.stdout, isEmptyString());
}

public void test30AbortWhenJavaMissing() {
public void test30AbortWhenJavaMissing() throws Exception {
assumeThat(installation, is(notNullValue()));

final Installation.Executables bin = installation.executables();
Expand Down Expand Up @@ -143,7 +146,7 @@ public void test30AbortWhenJavaMissing() {
});
}

public void test40CreateKeystoreManually() {
public void test40CreateKeystoreManually() throws Exception {
assumeThat(installation, is(notNullValue()));

final Installation.Executables bin = installation.executables();
Expand Down Expand Up @@ -176,7 +179,7 @@ public void test40CreateKeystoreManually() {
});
}

public void test50StartAndStop() throws IOException {
public void test50StartAndStop() throws Exception {
assumeThat(installation, is(notNullValue()));

// cleanup from previous test
Expand All @@ -192,8 +195,63 @@ public void test50StartAndStop() throws IOException {

Archives.stopElasticsearch(installation);
}
public void test51JavaHomeWithSpecialCharacters() throws Exception {
assumeThat(installation, is(notNullValue()));

Platforms.onWindows(() -> {
final Shell sh = new Shell();
try {
// once windows 2012 is no longer supported and powershell 5.0 is always available we can change this command
sh.run("cmd /c mklink /D 'C:\\Program Files (x86)\\java' $Env:JAVA_HOME");

sh.getEnv().put("JAVA_HOME", "C:\\Program Files (x86)\\java");

//verify ES can start, stop and run plugin list
Archives.runElasticsearch(installation, sh);

Archives.stopElasticsearch(installation);

String pluginListCommand = installation.bin + "/elasticsearch-plugin list";
Result result = sh.run(pluginListCommand);
assertThat(result.exitCode, equalTo(0));

} finally {
//clean up sym link
sh.run("cmd /c rmdir 'C:\\Program Files (x86)\\java' ");
}
});

Platforms.onLinux(() -> {
final Shell sh = new Shell();
// Create temporary directory with a space and link to java binary.
// Use it as java_home
String nameWithSpace = RandomStrings.randomAsciiAlphanumOfLength(getRandom(), 10) + "java home";
String test_java_home = FileUtils.mkdir(Paths.get("/home",ARCHIVE_OWNER, nameWithSpace)).toAbsolutePath().toString();
try {
final String systemJavaHome = sh.run("echo $JAVA_HOME").stdout.trim();
final String java = systemJavaHome + "/bin/java";

sh.run("mkdir -p \"" + test_java_home + "/bin\"");
sh.run("ln -s \"" + java + "\" \"" + test_java_home + "/bin/java\"");
sh.run("chown -R " + ARCHIVE_OWNER + ":" + ARCHIVE_OWNER + " \"" + test_java_home + "\"");

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

//verify ES can start, stop and run plugin list
Archives.runElasticsearch(installation, sh);

Archives.stopElasticsearch(installation);

String pluginListCommand = installation.bin + "/elasticsearch-plugin list";
Result result = sh.run(pluginListCommand);
assertThat(result.exitCode, equalTo(0));
} finally {
FileUtils.rm(Paths.get("\"" + test_java_home + "\""));
}
});
}

public void test60AutoCreateKeystore() {
public void test60AutoCreateKeystore() throws Exception {
assumeThat(installation, is(notNullValue()));

assertThat(installation.config("elasticsearch.keystore"), file(File, ARCHIVE_OWNER, ARCHIVE_OWNER, p660));
Expand All @@ -212,7 +270,7 @@ public void test60AutoCreateKeystore() {
});
}

public void test70CustomPathConfAndJvmOptions() throws IOException {
public void test70CustomPathConfAndJvmOptions() throws Exception {
assumeThat(installation, is(notNullValue()));

final Path tempConf = getTempDir().resolve("esconf-alternate");
Expand Down Expand Up @@ -261,7 +319,7 @@ public void test70CustomPathConfAndJvmOptions() throws IOException {
}
}

public void test80RelativePathConf() throws IOException {
public void test80RelativePathConf() throws Exception {
assumeThat(installation, is(notNullValue()));

final Path temp = getTempDir().resolve("esconf-alternate");
Expand Down Expand Up @@ -304,7 +362,7 @@ public void test80RelativePathConf() throws IOException {
}
}

public void test90SecurityCliPackaging() {
public void test90SecurityCliPackaging() throws Exception {
assumeThat(installation, is(notNullValue()));

final Installation.Executables bin = installation.executables();
Expand All @@ -328,7 +386,7 @@ public void test90SecurityCliPackaging() {
}
}

public void test100RepairIndexCliPackaging() {
public void test100RepairIndexCliPackaging() throws Exception {
assumeThat(installation, is(notNullValue()));

final Installation.Executables bin = installation.executables();
Expand All @@ -344,5 +402,4 @@ public void test100RepairIndexCliPackaging() {
Platforms.onWindows(action);
}
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ public abstract class DebPreservationTestCase extends PackagingTestCase {
protected abstract Distribution distribution();

@BeforeClass
public static void cleanup() {
public static void cleanup() throws Exception {
installation = null;
cleanEverything();
}
Expand All @@ -66,14 +66,14 @@ public void onlyCompatibleDistributions() {
assumeTrue("only compatible distributions", distribution().packaging.compatible);
}

public void test10Install() {
public void test10Install() throws Exception {
assertRemoved(distribution());
installation = install(distribution());
assertInstalled(distribution());
verifyPackageInstallation(installation, distribution());
}

public void test20Remove() {
public void test20Remove() throws Exception {
assumeThat(installation, is(notNullValue()));

remove(distribution());
Expand Down Expand Up @@ -118,7 +118,7 @@ public void test20Remove() {
assertTrue(Files.exists(installation.envFile));
}

public void test30Purge() {
public void test30Purge() throws Exception {
assumeThat(installation, is(notNullValue()));

final Shell sh = new Shell();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@
import org.elasticsearch.packaging.util.Installation;
import org.elasticsearch.packaging.util.Platforms;
import org.elasticsearch.packaging.util.Shell;

import org.elasticsearch.packaging.util.Shell.Result;
import org.junit.Before;
import org.junit.BeforeClass;
Expand Down Expand Up @@ -52,13 +51,11 @@
import static org.elasticsearch.packaging.util.Platforms.getOsRelease;
import static org.elasticsearch.packaging.util.Platforms.isSystemd;
import static org.elasticsearch.packaging.util.ServerUtils.runElasticsearchTests;

import static org.hamcrest.CoreMatchers.not;
import static org.hamcrest.CoreMatchers.notNullValue;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.containsString;
import static org.hamcrest.Matchers.isEmptyString;

import static org.hamcrest.core.Is.is;
import static org.junit.Assume.assumeThat;
import static org.junit.Assume.assumeTrue;
Expand All @@ -71,13 +68,13 @@ public abstract class PackageTestCase extends PackagingTestCase {
protected abstract Distribution distribution();

@BeforeClass
public static void cleanup() {
public static void cleanup() throws Exception {
installation = null;
cleanEverything();
}

@Before
public void onlyCompatibleDistributions() {
public void onlyCompatibleDistributions() throws Exception {
assumeTrue("only compatible distributions", distribution().packaging.compatible);
}

Expand All @@ -98,14 +95,14 @@ public void test05InstallFailsWhenJavaMissing() {
}
}

public void test10InstallPackage() {
public void test10InstallPackage() throws Exception {
assertRemoved(distribution());
installation = install(distribution());
assertInstalled(distribution());
verifyPackageInstallation(installation, distribution());
}

public void test20PluginsCommandWhenNoPlugins() {
public void test20PluginsCommandWhenNoPlugins() throws Exception {
assumeThat(installation, is(notNullValue()));

final Shell sh = new Shell();
Expand All @@ -127,7 +124,7 @@ public void test40StartServer() throws IOException {
verifyPackageInstallation(installation, distribution()); // check startup script didn't change permissions
}

public void test50Remove() {
public void test50Remove() throws Exception {
assumeThat(installation, is(notNullValue()));

remove(distribution());
Expand Down Expand Up @@ -185,7 +182,7 @@ public void test50Remove() {
Platforms.onRPM(() -> assertFalse(Files.exists(SYSTEMD_SERVICE)));
}

public void test60Reinstall() {
public void test60Reinstall() throws Exception {
assumeThat(installation, is(notNullValue()));

installation = install(distribution());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ public abstract class RpmPreservationTestCase extends PackagingTestCase {
protected abstract Distribution distribution();

@BeforeClass
public static void cleanup() {
public static void cleanup() throws Exception {
installation = null;
cleanEverything();
}
Expand All @@ -69,14 +69,14 @@ public void onlyCompatibleDistributions() {
assumeTrue("only compatible distributions", distribution().packaging.compatible);
}

public void test10Install() {
public void test10Install() throws Exception {
assertRemoved(distribution());
installation = install(distribution());
assertInstalled(distribution());
verifyPackageInstallation(installation, distribution());
}

public void test20Remove() {
public void test20Remove() throws Exception {
assumeThat(installation, is(notNullValue()));

remove(distribution());
Expand All @@ -91,7 +91,7 @@ public void test20Remove() {
assertFalse(Files.exists(installation.envFile));
}

public void test30PreserveConfig() {
public void test30PreserveConfig() throws Exception {
final Shell sh = new Shell();

installation = install(distribution());
Expand Down
Loading

0 comments on commit da2f6be

Please sign in to comment.