Skip to content

Support ES_JAVA_HOME when version > 7.12.0 #35

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Feb 23, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -178,16 +178,16 @@ private static void ExecuteBinaryInternal(EphemeralClusterConfiguration config,

if (result.ExitCode != 0)
throw new Exception(
$"Expected exit code 0 but recieved ({result.ExitCode}) while executing {description}: {command}");
$"Expected exit code 0 but received ({result.ExitCode}) while executing {description}: {command}");

var errorOut = result.ConsoleOut.Where(c => c.Error).ToList();
// this manifested when calling certgen on versions smaller then 5.2.0
if (errorOut.Any() && config.Version < "5.2.0")
errorOut = errorOut.Where(e => !e.Line.Contains("No log4j2 configuration file found")).ToList();

if (errorOut.Any(e => !string.IsNullOrWhiteSpace(e.Line)) && (!binary.Contains("plugin") && !binary.Contains("cert")))
if (errorOut.Any(e => !string.IsNullOrWhiteSpace(e.Line) && !e.Line.Contains("usage of JAVA_HOME is deprecated")) && !binary.Contains("plugin") && !binary.Contains("cert"))
throw new Exception(
$"Recieved error out with exitCode ({result.ExitCode}) while executing {description}: {command}");
$"Received error out with exitCode ({result.ExitCode}) while executing {description}: {command}");

writer?.WriteDiagnostic(
$"{{{nameof(ExecuteBinary)}}} finished process [{description}] {{{result.ExitCode}}}");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,6 @@ public class EnsureJavaHomeEnvironmentVariableIsSet : ClusterComposeTask
public override void Run(IEphemeralCluster<EphemeralClusterConfiguration> cluster)
{
var fs = cluster.FileSystem;
var v = cluster.ClusterConfiguration.Version;
var a = cluster.ClusterConfiguration.Artifact;

var java8Home = Environment.GetEnvironmentVariable("JAVA8_HOME");
if (cluster.ClusterConfiguration.Version < "6.0.0" && !string.IsNullOrWhiteSpace(java8Home))
Expand All @@ -23,30 +21,32 @@ public override void Run(IEphemeralCluster<EphemeralClusterConfiguration> cluste
Environment.SetEnvironmentVariable("JAVA_HOME", java8Home);
cluster.Writer?.WriteDiagnostic($"{{{nameof(EnsureJavaHomeEnvironmentVariableIsSet)}}} Forcing [JAVA8_HOME] as [JAVA_HOME] since we are on Elasticsearch <6.0.0");
}
var javaHome = Environment.GetEnvironmentVariable("JAVA_HOME");

var envVarName = cluster.ClusterConfiguration.JavaHomeEnvironmentVariable;
var javaHome = Environment.GetEnvironmentVariable(envVarName);

//7.0.0 ships with its own JDK
if (cluster.ClusterConfiguration.Version < "7.0.0" && string.IsNullOrWhiteSpace(javaHome))
{
cluster.Writer?.WriteDiagnostic($"{{{nameof(EnsureJavaHomeEnvironmentVariableIsSet)}}} [JAVA_HOME] is not SET exiting..");
throw new Exception("The elasticsearch bat files are resillient to JAVA_HOME not being set, however the shield tooling is not");
cluster.Writer?.WriteDiagnostic($"{{{nameof(EnsureJavaHomeEnvironmentVariableIsSet)}}} [{envVarName}] is not SET exiting..");
throw new Exception("The elasticsearch bat files are resilient to JAVA_HOME not being set, however the shield tooling is not");
}

var cachedEsHomeFolder = Path.Combine(fs.LocalFolder, cluster.GetCacheFolderName());
var jdkFolder = Path.Combine(cachedEsHomeFolder, "jdk");
if (Directory.Exists(jdkFolder))
{
//prefer bundled jdk
cluster.Writer?.WriteDiagnostic($"{{{nameof(EnsureJavaHomeEnvironmentVariableIsSet)}}} [JAVA_HOME] is set to bundled jdk: {{{jdkFolder}}} ");
cluster.Writer?.WriteDiagnostic($"{{{nameof(EnsureJavaHomeEnvironmentVariableIsSet)}}} [{envVarName}] is set to bundled jdk: {{{jdkFolder}}} ");
Environment.SetEnvironmentVariable("JAVA_HOME", jdkFolder);

}
else if (cluster.ClusterConfiguration.Version >= "7.0.0" && !string.IsNullOrWhiteSpace(javaHome))
{
cluster.Writer?.WriteDiagnostic($"{{{nameof(EnsureJavaHomeEnvironmentVariableIsSet)}}} [JAVA_HOME] is set, unsetting for process to prefer bundled jdk..");
Environment.SetEnvironmentVariable("JAVA_HOME", null);
cluster.Writer?.WriteDiagnostic($"{{{nameof(EnsureJavaHomeEnvironmentVariableIsSet)}}} [{envVarName}] is set; clearing value for process to prefer bundled jdk...");
Environment.SetEnvironmentVariable(envVarName, null);
}

else cluster.Writer?.WriteDiagnostic($"{{{nameof(EnsureJavaHomeEnvironmentVariableIsSet)}}} JAVA_HOME is set proceeding or using default JDK");

else cluster.Writer?.WriteDiagnostic($"{{{nameof(EnsureJavaHomeEnvironmentVariableIsSet)}}} {envVarName} is not set proceeding or using default JDK");
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,16 +12,16 @@ public class SetElasticsearchBundledJdkJavaHome : ClusterComposeTask
{
public override void Run(IEphemeralCluster<EphemeralClusterConfiguration> cluster)
{

var fs = cluster.FileSystem;
var jdkFolder = Path.Combine(fs.ElasticsearchHome, "jdk");
if (Directory.Exists(jdkFolder))
{
cluster.Writer?.WriteDiagnostic($"{{{nameof(SetElasticsearchBundledJdkJavaHome)}}} [JAVA_HOME] is set to bundled jdk: {{{jdkFolder}}} ");
Environment.SetEnvironmentVariable("JAVA_HOME", jdkFolder);
var envVarName = cluster.ClusterConfiguration.JavaHomeEnvironmentVariable;
cluster.Writer?.WriteDiagnostic($"{{{nameof(SetElasticsearchBundledJdkJavaHome)}}} [{envVarName}] is set to bundled jdk: {{{jdkFolder}}} ");
Environment.SetEnvironmentVariable(envVarName, jdkFolder);
}
else
cluster.Writer?.WriteDiagnostic($"{{{nameof(SetElasticsearchBundledJdkJavaHome)}}} [No bundled jdk found] looked in: {{{jdkFolder}}} ");
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,7 @@ public ClusterConfiguration(ElasticVersion version, Func<ElasticVersion, string,
public int StartingPortNumber { get; set; } = 9200;
public bool NoCleanupAfterNodeStopped { get; set; }

public string JavaHomeEnvironmentVariable => Version.InRange("<7.12.0") ? "JAVA_HOME" : "ES_JAVA_HOME";

/// <summary> Will print the contents of all the yaml files when starting the cluster up, great for debugging purposes</summary>
public bool PrintYamlFilesInConfigFolder { get; set; }
Expand Down
7 changes: 4 additions & 3 deletions src/Elastic.Elasticsearch.Managed/ElasticsearchNode.cs
Original file line number Diff line number Diff line change
Expand Up @@ -107,9 +107,10 @@ public IDisposable SubscribeLines(IConsoleLineHandler writer, Action<LineOut> on
writer?.WriteDiagnostic($"Elasticsearch location: [{Binary}]", node);
writer?.WriteDiagnostic($"Settings: {{{string.Join(" ", NodeConfiguration.CommandLineArguments)}}}", node);

var javaHome = Environment.GetEnvironmentVariable("JAVA_HOME");
writer?.WriteDiagnostic($"JAVA_HOME: {{{javaHome}}}", node);
Process.StartInfo.Environment["JAVA_HOME"] = javaHome;
var envVarName = NodeConfiguration.Version.InRange("<7.12.0") ? "JAVA_HOME" : "ES_JAVA_HOME";
var javaHome = Environment.GetEnvironmentVariable(envVarName);
writer?.WriteDiagnostic($"{envVarName}: {{{javaHome}}}", node);
Process.StartInfo.Environment[envVarName] = javaHome;

return SubscribeLines(
l => {
Expand Down