Change prepareSettingsAndEnvironment to prepareEnvironment

This commit is contained in:
Ryan Ernst 2015-09-08 08:54:18 -07:00
parent 1ff49eb8de
commit d19e04fbb6
7 changed files with 54 additions and 63 deletions

View File

@ -195,9 +195,9 @@ final class Bootstrap {
} }
} }
private static Tuple<Settings, Environment> initialSettings(boolean foreground) { private static Environment initialSettings(boolean foreground) {
Terminal terminal = foreground ? Terminal.DEFAULT : null; Terminal terminal = foreground ? Terminal.DEFAULT : null;
return InternalSettingsPreparer.prepareSettingsAndEnvironment(EMPTY_SETTINGS, terminal); return InternalSettingsPreparer.prepareEnvironment(EMPTY_SETTINGS, terminal);
} }
private void start() { private void start() {
@ -234,9 +234,8 @@ final class Bootstrap {
foreground = false; foreground = false;
} }
Tuple<Settings, Environment> tuple = initialSettings(foreground); Environment environment = initialSettings(foreground);
Settings settings = tuple.v1(); Settings settings = environment.settings();
Environment environment = tuple.v2();
if (environment.pidFile() != null) { if (environment.pidFile() != null) {
PidFile.create(environment.pidFile(), true); PidFile.create(environment.pidFile(), true);

View File

@ -104,9 +104,8 @@ public abstract class CliTool {
Preconditions.checkArgument(config.cmds().size() != 0, "At least one command must be configured"); Preconditions.checkArgument(config.cmds().size() != 0, "At least one command must be configured");
this.config = config; this.config = config;
this.terminal = terminal; this.terminal = terminal;
Tuple<Settings, Environment> tuple = InternalSettingsPreparer.prepareSettingsAndEnvironment(EMPTY_SETTINGS, terminal); env = InternalSettingsPreparer.prepareEnvironment(EMPTY_SETTINGS, terminal);
settings = tuple.v1(); settings = env.settings();
env = tuple.v2();
} }
public final ExitStatus execute(String... args) { public final ExitStatus execute(String... args) {

View File

@ -133,21 +133,20 @@ public class Node implements Releasable {
Node(Settings preparedSettings, boolean loadConfigSettings, Version version, Collection<Class<? extends Plugin>> classpathPlugins) { Node(Settings preparedSettings, boolean loadConfigSettings, Version version, Collection<Class<? extends Plugin>> classpathPlugins) {
final Settings pSettings = settingsBuilder().put(preparedSettings) final Settings pSettings = settingsBuilder().put(preparedSettings)
.put(Client.CLIENT_TYPE_SETTING, CLIENT_TYPE).build(); .put(Client.CLIENT_TYPE_SETTING, CLIENT_TYPE).build();
Tuple<Settings, Environment> tuple = InternalSettingsPreparer.prepareSettingsAndEnvironment(pSettings, null); Environment env = InternalSettingsPreparer.prepareEnvironment(pSettings, null);
tuple = new Tuple<>(TribeService.processSettings(tuple.v1()), tuple.v2()); Settings settings = TribeService.processSettings(env.settings());
ESLogger logger = Loggers.getLogger(Node.class, tuple.v1().get("name")); ESLogger logger = Loggers.getLogger(Node.class, settings.get("name"));
logger.info("version[{}], pid[{}], build[{}/{}]", version, JvmInfo.jvmInfo().pid(), Build.CURRENT.hashShort(), Build.CURRENT.timestamp()); logger.info("version[{}], pid[{}], build[{}/{}]", version, JvmInfo.jvmInfo().pid(), Build.CURRENT.hashShort(), Build.CURRENT.timestamp());
logger.info("initializing ..."); logger.info("initializing ...");
if (logger.isDebugEnabled()) { if (logger.isDebugEnabled()) {
Environment env = tuple.v2();
logger.debug("using config [{}], data [{}], logs [{}], plugins [{}]", logger.debug("using config [{}], data [{}], logs [{}], plugins [{}]",
env.configFile(), Arrays.toString(env.dataFiles()), env.logsFile(), env.pluginsFile()); env.configFile(), Arrays.toString(env.dataFiles()), env.logsFile(), env.pluginsFile());
} }
this.pluginsService = new PluginsService(tuple.v1(), tuple.v2().pluginsFile(), classpathPlugins); this.pluginsService = new PluginsService(settings, env.pluginsFile(), classpathPlugins);
this.settings = pluginsService.updatedSettings(); this.settings = pluginsService.updatedSettings();
// create the environment based on the finalized (processed) view of the settings // create the environment based on the finalized (processed) view of the settings
this.environment = new Environment(this.settings()); this.environment = new Environment(this.settings());

View File

@ -80,7 +80,7 @@ public class InternalSettingsPreparer {
* @param terminal the Terminal to use for input/output * @param terminal the Terminal to use for input/output
* @return the {@link Settings} and {@link Environment} as a {@link Tuple} * @return the {@link Settings} and {@link Environment} as a {@link Tuple}
*/ */
public static Tuple<Settings, Environment> prepareSettingsAndEnvironment(Settings input, Terminal terminal) { public static Environment prepareEnvironment(Settings input, Terminal terminal) {
// just create enough settings to build the environment // just create enough settings to build the environment
Settings.Builder output = settingsBuilder(); Settings.Builder output = settingsBuilder();
initializeSettings(output, input, true); initializeSettings(output, input, true);
@ -134,7 +134,7 @@ public class InternalSettingsPreparer {
// we put back the path.logs so we can use it in the logging configuration file // we put back the path.logs so we can use it in the logging configuration file
output.put("path.logs", cleanPath(environment.logsFile().toAbsolutePath().toString())); output.put("path.logs", cleanPath(environment.logsFile().toAbsolutePath().toString()));
return new Tuple<>(output.build(), environment); return new Environment(output.build());
} }
private static boolean useSystemProperties(Settings input) { private static boolean useSystemProperties(Settings input) {

View File

@ -50,8 +50,8 @@ public class PluginManagerCliParser extends CliTool {
.build(); .build();
public static void main(String[] args) { public static void main(String[] args) {
Tuple<Settings, Environment> initialSettings = InternalSettingsPreparer.prepareSettingsAndEnvironment(EMPTY, Terminal.DEFAULT); Environment env = InternalSettingsPreparer.prepareEnvironment(EMPTY, Terminal.DEFAULT);
LogConfigurator.configure(initialSettings.v1()); LogConfigurator.configure(env.settings());
int status = new PluginManagerCliParser().execute(args).status(); int status = new PluginManagerCliParser().execute(args).status();
System.exit(status); System.exit(status);
} }

View File

@ -29,9 +29,6 @@ import org.elasticsearch.env.Environment;
import org.elasticsearch.test.ESTestCase; import org.elasticsearch.test.ESTestCase;
import org.junit.After; import org.junit.After;
import org.junit.Before; import org.junit.Before;
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.ExpectedException;
import java.io.IOException; import java.io.IOException;
import java.io.InputStream; import java.io.InputStream;
@ -98,20 +95,20 @@ public class InternalSettingsPreparerTests extends ESTestCase {
assertNotNull(settings.get(ClusterName.SETTING)); // a cluster name was set assertNotNull(settings.get(ClusterName.SETTING)); // a cluster name was set
assertEquals(settings.toString(), 2, settings.names().size()); assertEquals(settings.toString(), 2, settings.names().size());
Tuple<Settings, Environment> settingsAndEnv = InternalSettingsPreparer.prepareSettingsAndEnvironment(baseEnvSettings, null); Environment env = InternalSettingsPreparer.prepareEnvironment(baseEnvSettings, null);
settings = settingsAndEnv.v1(); settings = env.settings();
assertNotNull(settings.get("name")); // a name was set assertNotNull(settings.get("name")); // a name was set
assertNotNull(settings.get(ClusterName.SETTING)); // a cluster name was set assertNotNull(settings.get(ClusterName.SETTING)); // a cluster name was set
assertEquals(settings.toString(), 3 /* path.home is in the base settings */, settings.names().size()); assertEquals(settings.toString(), 3 /* path.home is in the base settings */, settings.names().size());
String home = baseEnvSettings.get("path.home"); String home = baseEnvSettings.get("path.home");
String configDir = settingsAndEnv.v2().configFile().toString(); String configDir = env.configFile().toString();
assertTrue(configDir, configDir.startsWith(home)); assertTrue(configDir, configDir.startsWith(home));
} }
public void testClusterNameDefault() { public void testClusterNameDefault() {
Settings settings = InternalSettingsPreparer.prepareSettings(Settings.EMPTY); Settings settings = InternalSettingsPreparer.prepareSettings(Settings.EMPTY);
assertEquals(ClusterName.DEFAULT.value(), settings.get(ClusterName.SETTING)); assertEquals(ClusterName.DEFAULT.value(), settings.get(ClusterName.SETTING));
settings = InternalSettingsPreparer.prepareSettingsAndEnvironment(baseEnvSettings, null).v1(); settings = InternalSettingsPreparer.prepareEnvironment(baseEnvSettings, null).settings();
assertEquals(ClusterName.DEFAULT.value(), settings.get(ClusterName.SETTING)); assertEquals(ClusterName.DEFAULT.value(), settings.get(ClusterName.SETTING));
} }
@ -122,18 +119,18 @@ public class InternalSettingsPreparerTests extends ESTestCase {
.put("node.zone", "bar") .put("node.zone", "bar")
.put(baseEnvSettings) .put(baseEnvSettings)
.build(); .build();
Tuple<Settings, Environment> tuple = InternalSettingsPreparer.prepareSettingsAndEnvironment(settings, null); Environment env = InternalSettingsPreparer.prepareEnvironment(settings, null);
// Should use setting from the system property // Should use setting from the system property
assertThat(tuple.v1().get("node.zone"), equalTo("foo")); assertThat(env.settings().get("node.zone"), equalTo("foo"));
settings = settingsBuilder() settings = settingsBuilder()
.put(InternalSettingsPreparer.IGNORE_SYSTEM_PROPERTIES_SETTING, true) .put(InternalSettingsPreparer.IGNORE_SYSTEM_PROPERTIES_SETTING, true)
.put("node.zone", "bar") .put("node.zone", "bar")
.put(baseEnvSettings) .put(baseEnvSettings)
.build(); .build();
tuple = InternalSettingsPreparer.prepareSettingsAndEnvironment(settings, null); env = InternalSettingsPreparer.prepareEnvironment(settings, null);
// Should use setting from the system property // Should use setting from the system property
assertThat(tuple.v1().get("node.zone"), equalTo("bar")); assertThat(env.settings().get("node.zone"), equalTo("bar"));
} finally { } finally {
System.clearProperty("es.node.zone"); System.clearProperty("es.node.zone");
} }
@ -169,7 +166,7 @@ public class InternalSettingsPreparerTests extends ESTestCase {
.put("dont.replace4", "__prompt:text_") .put("dont.replace4", "__prompt:text_")
.put("dont.replace5", "prompt:secret__") .put("dont.replace5", "prompt:secret__")
.put("replace_me", InternalSettingsPreparer.TEXT_PROMPT_VALUE); .put("replace_me", InternalSettingsPreparer.TEXT_PROMPT_VALUE);
Settings settings = InternalSettingsPreparer.prepareSettingsAndEnvironment(builder.build(), terminal).v1(); Settings settings = InternalSettingsPreparer.prepareEnvironment(builder.build(), terminal).settings();
assertThat(replacedSecretProperties.size(), is(1)); assertThat(replacedSecretProperties.size(), is(1));
assertThat(replacedTextProperties.size(), is(1)); assertThat(replacedTextProperties.size(), is(1));
@ -189,7 +186,7 @@ public class InternalSettingsPreparerTests extends ESTestCase {
.put(baseEnvSettings) .put(baseEnvSettings)
.put("replace_me1", InternalSettingsPreparer.SECRET_PROMPT_VALUE); .put("replace_me1", InternalSettingsPreparer.SECRET_PROMPT_VALUE);
try { try {
InternalSettingsPreparer.prepareSettingsAndEnvironment(builder.build(), null); InternalSettingsPreparer.prepareEnvironment(builder.build(), null);
fail("an exception should have been thrown since no terminal was provided!"); fail("an exception should have been thrown since no terminal was provided!");
} catch (UnsupportedOperationException e) { } catch (UnsupportedOperationException e) {
assertThat(e.getMessage(), containsString("with value [" + InternalSettingsPreparer.SECRET_PROMPT_VALUE + "]")); assertThat(e.getMessage(), containsString("with value [" + InternalSettingsPreparer.SECRET_PROMPT_VALUE + "]"));
@ -201,7 +198,7 @@ public class InternalSettingsPreparerTests extends ESTestCase {
.put(baseEnvSettings) .put(baseEnvSettings)
.put("replace_me1", InternalSettingsPreparer.TEXT_PROMPT_VALUE); .put("replace_me1", InternalSettingsPreparer.TEXT_PROMPT_VALUE);
try { try {
InternalSettingsPreparer.prepareSettingsAndEnvironment(builder.build(), null); InternalSettingsPreparer.prepareEnvironment(builder.build(), null);
fail("an exception should have been thrown since no terminal was provided!"); fail("an exception should have been thrown since no terminal was provided!");
} catch (UnsupportedOperationException e) { } catch (UnsupportedOperationException e) {
assertThat(e.getMessage(), containsString("with value [" + InternalSettingsPreparer.TEXT_PROMPT_VALUE + "]")); assertThat(e.getMessage(), containsString("with value [" + InternalSettingsPreparer.TEXT_PROMPT_VALUE + "]"));
@ -216,8 +213,8 @@ public class InternalSettingsPreparerTests extends ESTestCase {
.put("node.name", "node-name") .put("node.name", "node-name")
.put(baseEnvSettings) .put(baseEnvSettings)
.build(); .build();
Tuple<Settings, Environment> tuple = InternalSettingsPreparer.prepareSettingsAndEnvironment(settings, null); Environment env = InternalSettingsPreparer.prepareEnvironment(settings, null);
assertThat(tuple.v1().get("name"), equalTo("sys-prop-name")); assertThat(env.settings().get("name"), equalTo("sys-prop-name"));
// test name in settings overrides sys prop and node.name // test name in settings overrides sys prop and node.name
settings = settingsBuilder() settings = settingsBuilder()
@ -225,8 +222,8 @@ public class InternalSettingsPreparerTests extends ESTestCase {
.put("node.name", "node-name") .put("node.name", "node-name")
.put(baseEnvSettings) .put(baseEnvSettings)
.build(); .build();
tuple = InternalSettingsPreparer.prepareSettingsAndEnvironment(settings, null); env = InternalSettingsPreparer.prepareEnvironment(settings, null);
assertThat(tuple.v1().get("name"), equalTo("name-in-settings")); assertThat(env.settings().get("name"), equalTo("name-in-settings"));
// test only node.name in settings // test only node.name in settings
System.clearProperty("name"); System.clearProperty("name");
@ -234,15 +231,15 @@ public class InternalSettingsPreparerTests extends ESTestCase {
.put("node.name", "node-name") .put("node.name", "node-name")
.put(baseEnvSettings) .put(baseEnvSettings)
.build(); .build();
tuple = InternalSettingsPreparer.prepareSettingsAndEnvironment(settings, null); env = InternalSettingsPreparer.prepareEnvironment(settings, null);
assertThat(tuple.v1().get("name"), equalTo("node-name")); assertThat(env.settings().get("name"), equalTo("node-name"));
// test no name at all results in name being set // test no name at all results in name being set
tuple = InternalSettingsPreparer.prepareSettingsAndEnvironment(baseEnvSettings, null); env = InternalSettingsPreparer.prepareEnvironment(baseEnvSettings, null);
assertThat(tuple.v1().get("name"), not("name-in-settings")); assertThat(env.settings().get("name"), not("name-in-settings"));
assertThat(tuple.v1().get("name"), not("sys-prop-name")); assertThat(env.settings().get("name"), not("sys-prop-name"));
assertThat(tuple.v1().get("name"), not("node-name")); assertThat(env.settings().get("name"), not("node-name"));
assertThat(tuple.v1().get("name"), notNullValue()); assertThat(env.settings().get("name"), notNullValue());
} finally { } finally {
System.clearProperty("name"); System.clearProperty("name");
} }
@ -269,8 +266,8 @@ public class InternalSettingsPreparerTests extends ESTestCase {
.put(baseEnvSettings) .put(baseEnvSettings)
.put("node.name", InternalSettingsPreparer.TEXT_PROMPT_VALUE) .put("node.name", InternalSettingsPreparer.TEXT_PROMPT_VALUE)
.build(); .build();
Tuple<Settings, Environment> tuple = InternalSettingsPreparer.prepareSettingsAndEnvironment(settings, terminal); Environment env = InternalSettingsPreparer.prepareEnvironment(settings, terminal);
settings = tuple.v1(); settings = env.settings();
assertThat(counter.intValue(), is(1)); assertThat(counter.intValue(), is(1));
assertThat(settings.get("name"), is("prompted name 0")); assertThat(settings.get("name"), is("prompted name 0"));
assertThat(settings.get("node.name"), is("prompted name 0")); assertThat(settings.get("node.name"), is("prompted name 0"));
@ -283,7 +280,7 @@ public class InternalSettingsPreparerTests extends ESTestCase {
Path config = home.resolve("config"); Path config = home.resolve("config");
Files.createDirectory(config); Files.createDirectory(config);
Files.copy(garbage, config.resolve("elasticsearch.yml")); Files.copy(garbage, config.resolve("elasticsearch.yml"));
InternalSettingsPreparer.prepareSettingsAndEnvironment(settingsBuilder() InternalSettingsPreparer.prepareEnvironment(settingsBuilder()
.put("config.ignore_system_properties", true) .put("config.ignore_system_properties", true)
.put(baseEnvSettings) .put(baseEnvSettings)
.build(), null); .build(), null);
@ -302,7 +299,7 @@ public class InternalSettingsPreparerTests extends ESTestCase {
Files.copy(properties, config.resolve("elasticsearch.properties")); Files.copy(properties, config.resolve("elasticsearch.properties"));
try { try {
InternalSettingsPreparer.prepareSettingsAndEnvironment(settingsBuilder() InternalSettingsPreparer.prepareEnvironment(settingsBuilder()
.put("config.ignore_system_properties", true) .put("config.ignore_system_properties", true)
.put(baseEnvSettings) .put(baseEnvSettings)
.build(), null); .build(), null);

View File

@ -88,18 +88,18 @@ import static org.jboss.netty.handler.codec.http.HttpVersion.HTTP_1_1;
// if its in your classpath, then do not use plugins!!!!!! // if its in your classpath, then do not use plugins!!!!!!
public class PluginManagerIT extends ESIntegTestCase { public class PluginManagerIT extends ESIntegTestCase {
private Tuple<Settings, Environment> initialSettings; private Environment environment;
private CaptureOutputTerminal terminal = new CaptureOutputTerminal(); private CaptureOutputTerminal terminal = new CaptureOutputTerminal();
@Before @Before
public void setup() throws Exception { public void setup() throws Exception {
initialSettings = buildInitialSettings(); environment = buildInitialSettings();
System.setProperty("es.default.path.home", initialSettings.v1().get("path.home")); System.setProperty("es.default.path.home", environment.settings().get("path.home"));
Path binDir = initialSettings.v2().binFile(); Path binDir = environment.binFile();
if (!Files.exists(binDir)) { if (!Files.exists(binDir)) {
Files.createDirectories(binDir); Files.createDirectories(binDir);
} }
Path configDir = initialSettings.v2().configFile(); Path configDir = environment.configFile();
if (!Files.exists(configDir)) { if (!Files.exists(configDir)) {
Files.createDirectories(configDir); Files.createDirectories(configDir);
} }
@ -206,11 +206,10 @@ public class PluginManagerIT extends ESIntegTestCase {
"jvm", "true", "jvm", "true",
"classname", "FakePlugin"); "classname", "FakePlugin");
Environment env = initialSettings.v2(); Path binDir = environment.binFile();
Path binDir = env.binFile();
Path pluginBinDir = binDir.resolve(pluginName); Path pluginBinDir = binDir.resolve(pluginName);
Path pluginConfigDir = env.configFile().resolve(pluginName); Path pluginConfigDir = environment.configFile().resolve(pluginName);
assertStatusOk("install " + pluginUrl + " --verbose"); assertStatusOk("install " + pluginUrl + " --verbose");
terminal.getTerminalOutput().clear(); terminal.getTerminalOutput().clear();
@ -252,8 +251,7 @@ public class PluginManagerIT extends ESIntegTestCase {
"jvm", "true", "jvm", "true",
"classname", "FakePlugin"); "classname", "FakePlugin");
Environment env = initialSettings.v2(); Path pluginConfigDir = environment.configFile().resolve(pluginName);
Path pluginConfigDir = env.configFile().resolve(pluginName);
assertStatusOk(String.format(Locale.ROOT, "install %s --verbose", pluginUrl)); assertStatusOk(String.format(Locale.ROOT, "install %s --verbose", pluginUrl));
@ -355,8 +353,7 @@ public class PluginManagerIT extends ESIntegTestCase {
"jvm", "true", "jvm", "true",
"classname", "FakePlugin"); "classname", "FakePlugin");
Environment env = initialSettings.v2(); Path binDir = environment.binFile();
Path binDir = env.binFile();
Path pluginBinDir = binDir.resolve(pluginName); Path pluginBinDir = binDir.resolve(pluginName);
assertStatusOk(String.format(Locale.ROOT, "install %s --verbose", pluginUrl)); assertStatusOk(String.format(Locale.ROOT, "install %s --verbose", pluginUrl));
@ -372,7 +369,7 @@ public class PluginManagerIT extends ESIntegTestCase {
@Test @Test
public void testListInstalledEmptyWithExistingPluginDirectory() throws IOException { public void testListInstalledEmptyWithExistingPluginDirectory() throws IOException {
Files.createDirectory(initialSettings.v2().pluginsFile()); Files.createDirectory(environment.pluginsFile());
assertStatusOk("list"); assertStatusOk("list");
assertThat(terminal.getTerminalOutput(), hasItem(containsString("No plugin detected"))); assertThat(terminal.getTerminalOutput(), hasItem(containsString("No plugin detected")));
} }
@ -407,7 +404,7 @@ public class PluginManagerIT extends ESIntegTestCase {
assertStatusOk(String.format(Locale.ROOT, "install %s --verbose", pluginUrl)); assertStatusOk(String.format(Locale.ROOT, "install %s --verbose", pluginUrl));
assertThatPluginIsListed(pluginName); assertThatPluginIsListed(pluginName);
// We want to check that Plugin Manager moves content to _site // We want to check that Plugin Manager moves content to _site
assertFileExists(initialSettings.v2().pluginsFile().resolve(pluginName).resolve("_site")); assertFileExists(environment.pluginsFile().resolve(pluginName).resolve("_site"));
} }
@Test @Test
@ -423,7 +420,7 @@ public class PluginManagerIT extends ESIntegTestCase {
assertStatus(String.format(Locale.ROOT, "install %s --verbose", pluginUrl), assertStatus(String.format(Locale.ROOT, "install %s --verbose", pluginUrl),
ExitStatus.IO_ERROR); ExitStatus.IO_ERROR);
assertThatPluginIsNotListed(pluginName); assertThatPluginIsNotListed(pluginName);
assertFileNotExists(initialSettings.v2().pluginsFile().resolve(pluginName).resolve("_site")); assertFileNotExists(environment.pluginsFile().resolve(pluginName).resolve("_site"));
} }
private void singlePluginInstallAndRemove(String pluginDescriptor, String pluginName, String pluginCoordinates) throws IOException { private void singlePluginInstallAndRemove(String pluginDescriptor, String pluginName, String pluginCoordinates) throws IOException {
@ -648,11 +645,11 @@ public class PluginManagerIT extends ESIntegTestCase {
private Tuple<Settings, Environment> buildInitialSettings() throws IOException { private Environment buildInitialSettings() throws IOException {
Settings settings = settingsBuilder() Settings settings = settingsBuilder()
.put("http.enabled", true) .put("http.enabled", true)
.put("path.home", createTempDir()).build(); .put("path.home", createTempDir()).build();
return InternalSettingsPreparer.prepareSettingsAndEnvironment(settings, null); return InternalSettingsPreparer.prepareEnvironment(settings, null);
} }
private void assertStatusOk(String command) { private void assertStatusOk(String command) {