Search in sources :

Example 1 with SettingsException

use of org.elasticsearch.common.settings.SettingsException in project elasticsearch by elastic.

the class InternalSettingsPreparer method prepareEnvironment.

/**
     * Prepares the settings by gathering all elasticsearch system properties, optionally loading the configuration settings,
     * and then replacing all property placeholders. If a {@link Terminal} is provided and configuration settings are loaded,
     * settings with a value of <code>${prompt.text}</code> or <code>${prompt.secret}</code> will result in a prompt for
     * the setting to the user.
     * @param input The custom settings to use. These are not overwritten by settings in the configuration file.
     * @param terminal the Terminal to use for input/output
     * @param properties Map of properties key/value pairs (usually from the command-line)
     * @return the {@link Settings} and {@link Environment} as a {@link Tuple}
     */
public static Environment prepareEnvironment(Settings input, Terminal terminal, Map<String, String> properties) {
    // just create enough settings to build the environment, to get the config dir
    Settings.Builder output = Settings.builder();
    initializeSettings(output, input, properties);
    Environment environment = new Environment(output.build());
    // start with a fresh output
    output = Settings.builder();
    boolean settingsFileFound = false;
    Set<String> foundSuffixes = new HashSet<>();
    for (String allowedSuffix : ALLOWED_SUFFIXES) {
        Path path = environment.configFile().resolve("elasticsearch" + allowedSuffix);
        if (Files.exists(path)) {
            if (!settingsFileFound) {
                try {
                    output.loadFromPath(path);
                } catch (IOException e) {
                    throw new SettingsException("Failed to load settings from " + path.toString(), e);
                }
            }
            settingsFileFound = true;
            foundSuffixes.add(allowedSuffix);
        }
    }
    if (foundSuffixes.size() > 1) {
        throw new SettingsException("multiple settings files found with suffixes: " + Strings.collectionToDelimitedString(foundSuffixes, ","));
    }
    // re-initialize settings now that the config file has been loaded
    initializeSettings(output, input, properties);
    finalizeSettings(output, terminal);
    environment = new Environment(output.build());
    // we put back the path.logs so we can use it in the logging configuration file
    output.put(Environment.PATH_LOGS_SETTING.getKey(), cleanPath(environment.logsFile().toAbsolutePath().toString()));
    return new Environment(output.build());
}
Also used : Path(java.nio.file.Path) Strings.cleanPath(org.elasticsearch.common.Strings.cleanPath) Environment(org.elasticsearch.env.Environment) IOException(java.io.IOException) SettingsException(org.elasticsearch.common.settings.SettingsException) Settings(org.elasticsearch.common.settings.Settings) HashSet(java.util.HashSet)

Example 2 with SettingsException

use of org.elasticsearch.common.settings.SettingsException in project elasticsearch by elastic.

the class AzureSettingsParserTests method testParseTwoSettingsNoDefault.

public void testParseTwoSettingsNoDefault() {
    Settings settings = Settings.builder().put("cloud.azure.storage.azure1.account", "myaccount1").put("cloud.azure.storage.azure1.key", "mykey1").put("cloud.azure.storage.azure2.account", "myaccount2").put("cloud.azure.storage.azure2.key", "mykey2").build();
    try {
        AzureStorageSettings.parse(settings);
        fail("Should have failed with a SettingsException (no default data store)");
    } catch (SettingsException ex) {
        assertEquals(ex.getMessage(), "No default Azure data store configured");
    }
}
Also used : SettingsException(org.elasticsearch.common.settings.SettingsException) Settings(org.elasticsearch.common.settings.Settings) AzureStorageSettings(org.elasticsearch.cloud.azure.storage.AzureStorageSettings)

Example 3 with SettingsException

use of org.elasticsearch.common.settings.SettingsException in project elasticsearch by elastic.

the class InternalSettingsPreparerTests method testMultipleSettingsFileNotAllowed.

public void testMultipleSettingsFileNotAllowed() throws IOException {
    InputStream yaml = getClass().getResourceAsStream("/config/elasticsearch.yaml");
    InputStream properties = getClass().getResourceAsStream("/config/elasticsearch.properties");
    Path home = createTempDir();
    Path config = home.resolve("config");
    Files.createDirectory(config);
    Files.copy(yaml, config.resolve("elasticsearch.yaml"));
    Files.copy(properties, config.resolve("elasticsearch.properties"));
    try {
        InternalSettingsPreparer.prepareEnvironment(Settings.builder().put(baseEnvSettings).build(), null);
    } catch (SettingsException e) {
        assertTrue(e.getMessage(), e.getMessage().contains("multiple settings files found with suffixes"));
        assertTrue(e.getMessage(), e.getMessage().contains(".yaml"));
        assertTrue(e.getMessage(), e.getMessage().contains(".properties"));
    }
}
Also used : Path(java.nio.file.Path) InputStream(java.io.InputStream) SettingsException(org.elasticsearch.common.settings.SettingsException)

Example 4 with SettingsException

use of org.elasticsearch.common.settings.SettingsException in project elasticsearch by elastic.

the class YamlSettingsLoaderTests method testDuplicateKeysThrowsException.

public void testDuplicateKeysThrowsException() {
    assumeFalse("Test only makes sense if XContent parser doesn't have strict duplicate checks enabled", XContent.isStrictDuplicateDetectionEnabled());
    String yaml = "foo: bar\nfoo: baz";
    SettingsException e = expectThrows(SettingsException.class, () -> {
        Settings.builder().loadFromSource(yaml, XContentType.YAML);
    });
    assertEquals(e.getCause().getClass(), ElasticsearchParseException.class);
    String msg = e.getCause().getMessage();
    assertTrue(msg, msg.contains("duplicate settings key [foo] found at line number [2], column number [6], " + "previous value [bar], current value [baz]"));
}
Also used : SettingsException(org.elasticsearch.common.settings.SettingsException)

Example 5 with SettingsException

use of org.elasticsearch.common.settings.SettingsException in project play2-elasticsearch by cleverage.

the class IndexClient method loadSettings.

/**
 * Load settings from resource file
 *
 * @return
 * @throws Exception
 */
private Settings.Builder loadSettings() throws Exception {
    Settings.Builder settings = Settings.settingsBuilder();
    // set default settings
    settings.put("client.transport.sniff", config.sniffing);
    if (config.clusterName != null && !config.clusterName.isEmpty()) {
        settings.put("cluster.name", config.clusterName);
    }
    // load settings
    if (config.localConfig != null && !config.localConfig.isEmpty()) {
        Logger.debug("Elasticsearch : Load settings from " + config.localConfig);
        try {
            settings.loadFromPath(Paths.get(this.getClass().getClassLoader().getResource(config.localConfig).toURI()));
        } catch (SettingsException settingsException) {
            Logger.error("Elasticsearch : Error when loading settings from " + config.localConfig);
            throw new Exception(settingsException);
        }
    }
    settings.build();
    Logger.info("Elasticsearch : Settings  " + settings.internalMap().toString());
    return settings;
}
Also used : SettingsException(org.elasticsearch.common.settings.SettingsException) Settings(org.elasticsearch.common.settings.Settings) SettingsException(org.elasticsearch.common.settings.SettingsException)

Aggregations

SettingsException (org.elasticsearch.common.settings.SettingsException)16 Settings (org.elasticsearch.common.settings.Settings)10 Path (java.nio.file.Path)5 IOException (java.io.IOException)2 InputStream (java.io.InputStream)2 AzureStorageSettings (org.elasticsearch.cloud.azure.storage.AzureStorageSettings)2 Environment (org.elasticsearch.env.Environment)2 AWSCredentials (com.amazonaws.auth.AWSCredentials)1 BasicAWSCredentials (com.amazonaws.auth.BasicAWSCredentials)1 BasicSessionCredentials (com.amazonaws.auth.BasicSessionCredentials)1 HttpMethod (io.netty.handler.codec.http.HttpMethod)1 HashSet (java.util.HashSet)1 Pattern (java.util.regex.Pattern)1 PatternSyntaxException (java.util.regex.PatternSyntaxException)1 Strings.cleanPath (org.elasticsearch.common.Strings.cleanPath)1 SecureString (org.elasticsearch.common.settings.SecureString)1 Netty4CorsConfigBuilder (org.elasticsearch.http.netty4.cors.Netty4CorsConfigBuilder)1 CoreMatchers.containsString (org.hamcrest.CoreMatchers.containsString)1