aboutsummaryrefslogtreecommitdiffstats
path: root/server
diff options
context:
space:
mode:
authorDaniel Schwarz <daniel.schwarz@sonarsource.com>2017-08-17 16:44:34 +0200
committerSimon Brandhof <simon.brandhof@sonarsource.com>2017-09-05 14:24:13 +0200
commitcac5ac482c98ce8a555b1b6b646b0f547532fb0c (patch)
tree6b37d02106027eea5e9fd19e27ef142d965449bd /server
parentd63783420e7d1789f3dfe15ae1100ad2f4e7a510 (diff)
downloadsonarqube-cac5ac482c98ce8a555b1b6b646b0f547532fb0c.tar.gz
sonarqube-cac5ac482c98ce8a555b1b6b646b0f547532fb0c.zip
SONAR-9714 Pass Elasticsearch settings in a yaml file
instead of command line arguments
Diffstat (limited to 'server')
-rw-r--r--server/sonar-main/src/main/java/org/sonar/application/process/ProcessLauncherImpl.java5
-rw-r--r--server/sonar-process/pom.xml4
-rw-r--r--server/sonar-process/src/main/java/org/sonar/process/command/CommandFactoryImpl.java9
-rw-r--r--server/sonar-process/src/main/java/org/sonar/process/command/EsCommand.java11
-rw-r--r--server/sonar-process/src/main/java/org/sonar/process/es/EsYmlSettings.java56
5 files changed, 77 insertions, 8 deletions
diff --git a/server/sonar-main/src/main/java/org/sonar/application/process/ProcessLauncherImpl.java b/server/sonar-main/src/main/java/org/sonar/application/process/ProcessLauncherImpl.java
index 76576b6d235..3c2816474a7 100644
--- a/server/sonar-main/src/main/java/org/sonar/application/process/ProcessLauncherImpl.java
+++ b/server/sonar-main/src/main/java/org/sonar/application/process/ProcessLauncherImpl.java
@@ -29,7 +29,6 @@ import java.util.List;
import java.util.Map;
import java.util.Properties;
import java.util.function.Supplier;
-import org.apache.commons.io.IOUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.sonar.process.ProcessId;
@@ -90,7 +89,7 @@ public class ProcessLauncherImpl implements ProcessLauncher {
}
}
- private void writeConfFiles(EsCommand esCommand) {
+ private static void writeConfFiles(EsCommand esCommand) {
EsFileSystem esFileSystem = esCommand.getFileSystem();
File confDir = esFileSystem.getConfDirectory();
if (!confDir.exists() && !confDir.mkdirs()) {
@@ -100,7 +99,7 @@ public class ProcessLauncherImpl implements ProcessLauncher {
}
try {
- IOUtils.copy(getClass().getResourceAsStream("elasticsearch.yml"), new FileOutputStream(esFileSystem.getElasticsearchYml()));
+ esCommand.getEsYmlSettings().writeToYmlSettingsFile(esFileSystem.getElasticsearchYml());
esCommand.getEsJvmOptions().writeToJvmOptionFile(esFileSystem.getJvmOptions());
esCommand.getLog4j2Properties().store(new FileOutputStream(esFileSystem.getLog4j2Properties()), "log4j2 properties file for ES bundled in SonarQube");
} catch (IOException e) {
diff --git a/server/sonar-process/pom.xml b/server/sonar-process/pom.xml
index 48ff3f16253..ac07efaa96c 100644
--- a/server/sonar-process/pom.xml
+++ b/server/sonar-process/pom.xml
@@ -41,6 +41,10 @@
<artifactId>commons-lang</artifactId>
</dependency>
<dependency>
+ <groupId>org.yaml</groupId>
+ <artifactId>snakeyaml</artifactId>
+ </dependency>
+ <dependency>
<groupId>com.google.code.findbugs</groupId>
<artifactId>jsr305</artifactId>
<scope>provided</scope>
diff --git a/server/sonar-process/src/main/java/org/sonar/process/command/CommandFactoryImpl.java b/server/sonar-process/src/main/java/org/sonar/process/command/CommandFactoryImpl.java
index 01731f8ee56..ae0ed14b064 100644
--- a/server/sonar-process/src/main/java/org/sonar/process/command/CommandFactoryImpl.java
+++ b/server/sonar-process/src/main/java/org/sonar/process/command/CommandFactoryImpl.java
@@ -28,6 +28,7 @@ import org.sonar.process.Props;
import org.sonar.process.es.EsFileSystem;
import org.sonar.process.es.EsLogging;
import org.sonar.process.es.EsSettings;
+import org.sonar.process.es.EsYmlSettings;
import org.sonar.process.jmvoptions.CeJvmOptions;
import org.sonar.process.jmvoptions.EsJvmOptions;
import org.sonar.process.jmvoptions.JvmOptions;
@@ -68,22 +69,20 @@ public class CommandFactoryImpl implements CommandFactory {
}
Map<String, String> settingsMap = new EsSettings(props, esFileSystem).build();
- EsCommand res = new EsCommand(ProcessId.ELASTICSEARCH, esFileSystem.getHomeDirectory())
+ return new EsCommand(ProcessId.ELASTICSEARCH, esFileSystem.getHomeDirectory())
.setFileSystem(esFileSystem)
.setLog4j2Properties(new EsLogging().createProperties(props, esFileSystem.getLogDirectory()))
.setArguments(props.rawProperties())
.setClusterName(settingsMap.get("cluster.name"))
.setHost(settingsMap.get("network.host"))
.setPort(Integer.valueOf(settingsMap.get("transport.tcp.port")))
+ .addEsOption("-Epath.conf="+esFileSystem.getConfDirectory().getAbsolutePath())
.setEsJvmOptions(new EsJvmOptions()
.addFromMandatoryProperty(props, ProcessProperties.SEARCH_JAVA_OPTS)
.addFromMandatoryProperty(props, ProcessProperties.SEARCH_JAVA_ADDITIONAL_OPTS))
+ .setEsYmlSettings(new EsYmlSettings(settingsMap))
.setEnvVariable("ES_JVM_OPTIONS", esFileSystem.getJvmOptions().getAbsolutePath())
.setEnvVariable("JAVA_HOME", System.getProperties().getProperty("java.home"));
-
- settingsMap.forEach((key, value) -> res.addEsOption("-E" + key + "=" + value));
-
- return res;
}
@Override
diff --git a/server/sonar-process/src/main/java/org/sonar/process/command/EsCommand.java b/server/sonar-process/src/main/java/org/sonar/process/command/EsCommand.java
index 7b2efefcb26..9bfff4627bb 100644
--- a/server/sonar-process/src/main/java/org/sonar/process/command/EsCommand.java
+++ b/server/sonar-process/src/main/java/org/sonar/process/command/EsCommand.java
@@ -25,6 +25,7 @@ import java.util.List;
import java.util.Properties;
import org.sonar.process.ProcessId;
import org.sonar.process.es.EsFileSystem;
+import org.sonar.process.es.EsYmlSettings;
import org.sonar.process.jmvoptions.EsJvmOptions;
public class EsCommand extends AbstractCommand<EsCommand> {
@@ -35,6 +36,7 @@ public class EsCommand extends AbstractCommand<EsCommand> {
private Properties log4j2Properties;
private List<String> esOptions = new ArrayList<>();
private EsJvmOptions esJvmOptions;
+ private EsYmlSettings esYmlSettings;
public EsCommand(ProcessId id, File workDir) {
super(id, workDir);
@@ -104,4 +106,13 @@ public class EsCommand extends AbstractCommand<EsCommand> {
public EsJvmOptions getEsJvmOptions() {
return esJvmOptions;
}
+
+ public EsCommand setEsYmlSettings(EsYmlSettings esYmlSettings) {
+ this.esYmlSettings = esYmlSettings;
+ return this;
+ }
+
+ public EsYmlSettings getEsYmlSettings() {
+ return esYmlSettings;
+ }
}
diff --git a/server/sonar-process/src/main/java/org/sonar/process/es/EsYmlSettings.java b/server/sonar-process/src/main/java/org/sonar/process/es/EsYmlSettings.java
new file mode 100644
index 00000000000..6eae2b14524
--- /dev/null
+++ b/server/sonar-process/src/main/java/org/sonar/process/es/EsYmlSettings.java
@@ -0,0 +1,56 @@
+/*
+ * SonarQube
+ * Copyright (C) 2009-2017 SonarSource SA
+ * mailto:info AT sonarsource DOT com
+ *
+ * This program is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either
+ * version 3 of the License, or (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public License
+ * along with this program; if not, write to the Free Software Foundation,
+ * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
+ */
+package org.sonar.process.es;
+
+import java.io.File;
+import java.io.IOException;
+import java.nio.charset.Charset;
+import java.nio.file.Files;
+import java.util.Map;
+import org.yaml.snakeyaml.DumperOptions;
+import org.yaml.snakeyaml.Yaml;
+
+import static org.yaml.snakeyaml.DumperOptions.FlowStyle.BLOCK;
+
+public class EsYmlSettings {
+ private static final String ELASTICSEARCH_YML_OPTIONS_HEADER = "# This file has been automatically generated by SonarQube during startup.\n" +
+ "\n" +
+ "# DO NOT EDIT THIS FILE\n" +
+ "\n";
+
+ private final Map<String, String> elasticsearchSettings;
+
+ public EsYmlSettings(Map<String, String> elasticsearchSettings) {
+ this.elasticsearchSettings = elasticsearchSettings;
+ }
+
+ public void writeToYmlSettingsFile(File file) {
+ DumperOptions dumperOptions = new DumperOptions();
+ dumperOptions.setPrettyFlow(true);
+ dumperOptions.setDefaultFlowStyle(BLOCK);
+ Yaml yaml = new Yaml(dumperOptions);
+ String output = ELASTICSEARCH_YML_OPTIONS_HEADER + yaml.dump(elasticsearchSettings);
+ try {
+ Files.write(file.toPath(), output.getBytes(Charset.forName("UTF-8")));
+ } catch (IOException e) {
+ throw new IllegalStateException("Cannot write Elasticsearch yml settings file", e);
+ }
+ }
+}