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;
return InternalSettingsPreparer.prepareSettingsAndEnvironment(EMPTY_SETTINGS, terminal);
return InternalSettingsPreparer.prepareEnvironment(EMPTY_SETTINGS, terminal);
}
private void start() {
@ -234,9 +234,8 @@ final class Bootstrap {
foreground = false;
}
Tuple<Settings, Environment> tuple = initialSettings(foreground);
Settings settings = tuple.v1();
Environment environment = tuple.v2();
Environment environment = initialSettings(foreground);
Settings settings = environment.settings();
if (environment.pidFile() != null) {
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");
this.config = config;
this.terminal = terminal;
Tuple<Settings, Environment> tuple = InternalSettingsPreparer.prepareSettingsAndEnvironment(EMPTY_SETTINGS, terminal);
settings = tuple.v1();
env = tuple.v2();
env = InternalSettingsPreparer.prepareEnvironment(EMPTY_SETTINGS, terminal);
settings = env.settings();
}
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) {
final Settings pSettings = settingsBuilder().put(preparedSettings)
.put(Client.CLIENT_TYPE_SETTING, CLIENT_TYPE).build();
Tuple<Settings, Environment> tuple = InternalSettingsPreparer.prepareSettingsAndEnvironment(pSettings, null);
tuple = new Tuple<>(TribeService.processSettings(tuple.v1()), tuple.v2());
Environment env = InternalSettingsPreparer.prepareEnvironment(pSettings, null);
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("initializing ...");
if (logger.isDebugEnabled()) {
Environment env = tuple.v2();
logger.debug("using config [{}], data [{}], logs [{}], plugins [{}]",
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();
// create the environment based on the finalized (processed) view of the 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
* @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
Settings.Builder output = settingsBuilder();
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
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) {

View File

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

View File

@ -29,9 +29,6 @@ import org.elasticsearch.env.Environment;
import org.elasticsearch.test.ESTestCase;
import org.junit.After;
import org.junit.Before;
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.ExpectedException;
import java.io.IOException;
import java.io.InputStream;
@ -98,20 +95,20 @@ public class InternalSettingsPreparerTests extends ESTestCase {
assertNotNull(settings.get(ClusterName.SETTING)); // a cluster name was set
assertEquals(settings.toString(), 2, settings.names().size());
Tuple<Settings, Environment> settingsAndEnv = InternalSettingsPreparer.prepareSettingsAndEnvironment(baseEnvSettings, null);
settings = settingsAndEnv.v1();
Environment env = InternalSettingsPreparer.prepareEnvironment(baseEnvSettings, null);
settings = env.settings();
assertNotNull(settings.get("name")); // a 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());
String home = baseEnvSettings.get("path.home");
String configDir = settingsAndEnv.v2().configFile().toString();
String configDir = env.configFile().toString();
assertTrue(configDir, configDir.startsWith(home));
}
public void testClusterNameDefault() {
Settings settings = InternalSettingsPreparer.prepareSettings(Settings.EMPTY);
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));
}
@ -122,18 +119,18 @@ public class InternalSettingsPreparerTests extends ESTestCase {
.put("node.zone", "bar")
.put(baseEnvSettings)
.build();
Tuple<Settings, Environment> tuple = InternalSettingsPreparer.prepareSettingsAndEnvironment(settings, null);
Environment env = InternalSettingsPreparer.prepareEnvironment(settings, null);
// 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()
.put(InternalSettingsPreparer.IGNORE_SYSTEM_PROPERTIES_SETTING, true)
.put("node.zone", "bar")
.put(baseEnvSettings)
.build();
tuple = InternalSettingsPreparer.prepareSettingsAndEnvironment(settings, null);
env = InternalSettingsPreparer.prepareEnvironment(settings, null);
// Should use setting from the system property
assertThat(tuple.v1().get("node.zone"), equalTo("bar"));
assertThat(env.settings().get("node.zone"), equalTo("bar"));
} finally {
System.clearProperty("es.node.zone");
}
@ -169,7 +166,7 @@ public class InternalSettingsPreparerTests extends ESTestCase {
.put("dont.replace4", "__prompt:text_")
.put("dont.replace5", "prompt:secret__")
.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(replacedTextProperties.size(), is(1));
@ -189,7 +186,7 @@ public class InternalSettingsPreparerTests extends ESTestCase {
.put(baseEnvSettings)
.put("replace_me1", InternalSettingsPreparer.SECRET_PROMPT_VALUE);
try {
InternalSettingsPreparer.prepareSettingsAndEnvironment(builder.build(), null);
InternalSettingsPreparer.prepareEnvironment(builder.build(), null);
fail("an exception should have been thrown since no terminal was provided!");
} catch (UnsupportedOperationException e) {
assertThat(e.getMessage(), containsString("with value [" + InternalSettingsPreparer.SECRET_PROMPT_VALUE + "]"));
@ -201,7 +198,7 @@ public class InternalSettingsPreparerTests extends ESTestCase {
.put(baseEnvSettings)
.put("replace_me1", InternalSettingsPreparer.TEXT_PROMPT_VALUE);
try {
InternalSettingsPreparer.prepareSettingsAndEnvironment(builder.build(), null);
InternalSettingsPreparer.prepareEnvironment(builder.build(), null);
fail("an exception should have been thrown since no terminal was provided!");
} catch (UnsupportedOperationException e) {
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(baseEnvSettings)
.build();
Tuple<Settings, Environment> tuple = InternalSettingsPreparer.prepareSettingsAndEnvironment(settings, null);
assertThat(tuple.v1().get("name"), equalTo("sys-prop-name"));
Environment env = InternalSettingsPreparer.prepareEnvironment(settings, null);
assertThat(env.settings().get("name"), equalTo("sys-prop-name"));
// test name in settings overrides sys prop and node.name
settings = settingsBuilder()
@ -225,8 +222,8 @@ public class InternalSettingsPreparerTests extends ESTestCase {
.put("node.name", "node-name")
.put(baseEnvSettings)
.build();
tuple = InternalSettingsPreparer.prepareSettingsAndEnvironment(settings, null);
assertThat(tuple.v1().get("name"), equalTo("name-in-settings"));
env = InternalSettingsPreparer.prepareEnvironment(settings, null);
assertThat(env.settings().get("name"), equalTo("name-in-settings"));
// test only node.name in settings
System.clearProperty("name");
@ -234,15 +231,15 @@ public class InternalSettingsPreparerTests extends ESTestCase {
.put("node.name", "node-name")
.put(baseEnvSettings)
.build();
tuple = InternalSettingsPreparer.prepareSettingsAndEnvironment(settings, null);
assertThat(tuple.v1().get("name"), equalTo("node-name"));
env = InternalSettingsPreparer.prepareEnvironment(settings, null);
assertThat(env.settings().get("name"), equalTo("node-name"));
// test no name at all results in name being set
tuple = InternalSettingsPreparer.prepareSettingsAndEnvironment(baseEnvSettings, null);
assertThat(tuple.v1().get("name"), not("name-in-settings"));
assertThat(tuple.v1().get("name"), not("sys-prop-name"));
assertThat(tuple.v1().get("name"), not("node-name"));
assertThat(tuple.v1().get("name"), notNullValue());
env = InternalSettingsPreparer.prepareEnvironment(baseEnvSettings, null);
assertThat(env.settings().get("name"), not("name-in-settings"));
assertThat(env.settings().get("name"), not("sys-prop-name"));
assertThat(env.settings().get("name"), not("node-name"));
assertThat(env.settings().get("name"), notNullValue());
} finally {
System.clearProperty("name");
}
@ -269,8 +266,8 @@ public class InternalSettingsPreparerTests extends ESTestCase {
.put(baseEnvSettings)
.put("node.name", InternalSettingsPreparer.TEXT_PROMPT_VALUE)
.build();
Tuple<Settings, Environment> tuple = InternalSettingsPreparer.prepareSettingsAndEnvironment(settings, terminal);
settings = tuple.v1();
Environment env = InternalSettingsPreparer.prepareEnvironment(settings, terminal);
settings = env.settings();
assertThat(counter.intValue(), is(1));
assertThat(settings.get("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");
Files.createDirectory(config);
Files.copy(garbage, config.resolve("elasticsearch.yml"));
InternalSettingsPreparer.prepareSettingsAndEnvironment(settingsBuilder()
InternalSettingsPreparer.prepareEnvironment(settingsBuilder()
.put("config.ignore_system_properties", true)
.put(baseEnvSettings)
.build(), null);
@ -302,7 +299,7 @@ public class InternalSettingsPreparerTests extends ESTestCase {
Files.copy(properties, config.resolve("elasticsearch.properties"));
try {
InternalSettingsPreparer.prepareSettingsAndEnvironment(settingsBuilder()
InternalSettingsPreparer.prepareEnvironment(settingsBuilder()
.put("config.ignore_system_properties", true)
.put(baseEnvSettings)
.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!!!!!!
public class PluginManagerIT extends ESIntegTestCase {
private Tuple<Settings, Environment> initialSettings;
private Environment environment;
private CaptureOutputTerminal terminal = new CaptureOutputTerminal();
@Before
public void setup() throws Exception {
initialSettings = buildInitialSettings();
System.setProperty("es.default.path.home", initialSettings.v1().get("path.home"));
Path binDir = initialSettings.v2().binFile();
environment = buildInitialSettings();
System.setProperty("es.default.path.home", environment.settings().get("path.home"));
Path binDir = environment.binFile();
if (!Files.exists(binDir)) {
Files.createDirectories(binDir);
}
Path configDir = initialSettings.v2().configFile();
Path configDir = environment.configFile();
if (!Files.exists(configDir)) {
Files.createDirectories(configDir);
}
@ -206,11 +206,10 @@ public class PluginManagerIT extends ESIntegTestCase {
"jvm", "true",
"classname", "FakePlugin");
Environment env = initialSettings.v2();
Path binDir = env.binFile();
Path binDir = environment.binFile();
Path pluginBinDir = binDir.resolve(pluginName);
Path pluginConfigDir = env.configFile().resolve(pluginName);
Path pluginConfigDir = environment.configFile().resolve(pluginName);
assertStatusOk("install " + pluginUrl + " --verbose");
terminal.getTerminalOutput().clear();
@ -252,8 +251,7 @@ public class PluginManagerIT extends ESIntegTestCase {
"jvm", "true",
"classname", "FakePlugin");
Environment env = initialSettings.v2();
Path pluginConfigDir = env.configFile().resolve(pluginName);
Path pluginConfigDir = environment.configFile().resolve(pluginName);
assertStatusOk(String.format(Locale.ROOT, "install %s --verbose", pluginUrl));
@ -355,8 +353,7 @@ public class PluginManagerIT extends ESIntegTestCase {
"jvm", "true",
"classname", "FakePlugin");
Environment env = initialSettings.v2();
Path binDir = env.binFile();
Path binDir = environment.binFile();
Path pluginBinDir = binDir.resolve(pluginName);
assertStatusOk(String.format(Locale.ROOT, "install %s --verbose", pluginUrl));
@ -372,7 +369,7 @@ public class PluginManagerIT extends ESIntegTestCase {
@Test
public void testListInstalledEmptyWithExistingPluginDirectory() throws IOException {
Files.createDirectory(initialSettings.v2().pluginsFile());
Files.createDirectory(environment.pluginsFile());
assertStatusOk("list");
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));
assertThatPluginIsListed(pluginName);
// 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
@ -423,7 +420,7 @@ public class PluginManagerIT extends ESIntegTestCase {
assertStatus(String.format(Locale.ROOT, "install %s --verbose", pluginUrl),
ExitStatus.IO_ERROR);
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 {
@ -648,11 +645,11 @@ public class PluginManagerIT extends ESIntegTestCase {
private Tuple<Settings, Environment> buildInitialSettings() throws IOException {
private Environment buildInitialSettings() throws IOException {
Settings settings = settingsBuilder()
.put("http.enabled", true)
.put("path.home", createTempDir()).build();
return InternalSettingsPreparer.prepareSettingsAndEnvironment(settings, null);
return InternalSettingsPreparer.prepareEnvironment(settings, null);
}
private void assertStatusOk(String command) {