From cac5ac482c98ce8a555b1b6b646b0f547532fb0c Mon Sep 17 00:00:00 2001 From: Daniel Schwarz Date: Thu, 17 Aug 2017 16:44:34 +0200 Subject: SONAR-9714 Pass Elasticsearch settings in a yaml file instead of command line arguments --- server/sonar-process/pom.xml | 4 ++ .../sonar/process/command/CommandFactoryImpl.java | 9 ++-- .../java/org/sonar/process/command/EsCommand.java | 11 +++++ .../java/org/sonar/process/es/EsYmlSettings.java | 56 ++++++++++++++++++++++ 4 files changed, 75 insertions(+), 5 deletions(-) create mode 100644 server/sonar-process/src/main/java/org/sonar/process/es/EsYmlSettings.java (limited to 'server/sonar-process') 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 @@ -40,6 +40,10 @@ commons-lang commons-lang + + org.yaml + snakeyaml + com.google.code.findbugs jsr305 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 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 { @@ -35,6 +36,7 @@ public class EsCommand extends AbstractCommand { private Properties log4j2Properties; private List 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 { 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 elasticsearchSettings; + + public EsYmlSettings(Map 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); + } + } +} -- cgit v1.2.3