Search in sources :

Example 36 with UserException

use of org.elasticsearch.cli.UserException in project crate by crate.

the class ElasticsearchNodeCommand method processNodePaths.

protected void processNodePaths(Terminal terminal, OptionSet options, Environment env) throws IOException, UserException {
    terminal.println(Terminal.Verbosity.VERBOSE, "Obtaining lock for node");
    Integer nodeOrdinal = nodeOrdinalOption.value(options);
    if (nodeOrdinal == null) {
        nodeOrdinal = 0;
    }
    try (NodeEnvironment.NodeLock lock = new NodeEnvironment.NodeLock(nodeOrdinal, LOGGER, env, Files::exists)) {
        final Path[] dataPaths = Arrays.stream(lock.getNodePaths()).filter(Objects::nonNull).map(p -> p.path).toArray(Path[]::new);
        if (dataPaths.length == 0) {
            throw new ElasticsearchException(NO_NODE_FOLDER_FOUND_MSG);
        }
        processNodePaths(terminal, dataPaths, options, env);
    } catch (LockObtainFailedException e) {
        throw new ElasticsearchException(FAILED_TO_OBTAIN_NODE_LOCK_MSG, e);
    }
}
Also used : Path(java.nio.file.Path) ElasticsearchException(org.elasticsearch.ElasticsearchException) Arrays(java.util.Arrays) Tuple(io.crate.common.collections.Tuple) BigArrays(org.elasticsearch.common.util.BigArrays) Environment(org.elasticsearch.env.Environment) LockObtainFailedException(org.apache.lucene.store.LockObtainFailedException) Function(java.util.function.Function) ClusterState(org.elasticsearch.cluster.ClusterState) Settings(org.elasticsearch.common.settings.Settings) OptionParser(joptsimple.OptionParser) IndicesModule(org.elasticsearch.indices.IndicesModule) ClusterName(org.elasticsearch.cluster.ClusterName) Path(java.nio.file.Path) NamedXContentRegistry(org.elasticsearch.common.xcontent.NamedXContentRegistry) OptionSet(joptsimple.OptionSet) OptionSpec(joptsimple.OptionSpec) Files(java.nio.file.Files) NodeMetadata(org.elasticsearch.env.NodeMetadata) IOException(java.io.IOException) ClusterModule(org.elasticsearch.cluster.ClusterModule) Collectors(java.util.stream.Collectors) Objects(java.util.Objects) ClusterSettings(org.elasticsearch.common.settings.ClusterSettings) Logger(org.apache.logging.log4j.Logger) NodeEnvironment(org.elasticsearch.env.NodeEnvironment) Stream(java.util.stream.Stream) EnvironmentAwareCommand(org.elasticsearch.cli.EnvironmentAwareCommand) PersistedClusterStateService(org.elasticsearch.gateway.PersistedClusterStateService) LogManager(org.apache.logging.log4j.LogManager) Terminal(org.elasticsearch.cli.Terminal) UserException(org.elasticsearch.cli.UserException) NodeEnvironment(org.elasticsearch.env.NodeEnvironment) LockObtainFailedException(org.apache.lucene.store.LockObtainFailedException) Objects(java.util.Objects) ElasticsearchException(org.elasticsearch.ElasticsearchException) Files(java.nio.file.Files)

Example 37 with UserException

use of org.elasticsearch.cli.UserException in project crate by crate.

the class LogConfigurator method configure.

private static void configure(final Settings settings, final Path configsPath, final Path logsPath) throws IOException, UserException {
    Objects.requireNonNull(settings);
    Objects.requireNonNull(configsPath);
    Objects.requireNonNull(logsPath);
    loadLog4jPlugins();
    setLogConfigurationSystemProperty(logsPath, settings);
    // we initialize the status logger immediately otherwise Log4j will complain when we try to get the context
    configureStatusLogger();
    final LoggerContext context = (LoggerContext) LogManager.getContext(false);
    final Set<String> locationsWithDeprecatedPatterns = Collections.synchronizedSet(new HashSet<>());
    final List<AbstractConfiguration> configurations = new ArrayList<>();
    /*
         * Subclass the properties configurator to hack the new pattern in
         * place so users don't have to change log4j2.properties in
         * a minor release. In 7.0 we'll remove this and force users to
         * change log4j2.properties. If they don't customize log4j2.properties
         * then they won't have to do anything anyway.
         *
         * Everything in this subclass that isn't marked as a hack is copied
         * from log4j2's source.
         */
    final PropertiesConfigurationFactory factory = new PropertiesConfigurationFactory() {

        @Override
        public PropertiesConfiguration getConfiguration(final LoggerContext loggerContext, final ConfigurationSource source) {
            final Properties properties = new Properties();
            try (InputStream configStream = source.getInputStream()) {
                properties.load(configStream);
            } catch (final IOException ioe) {
                throw new ConfigurationException("Unable to load " + source.toString(), ioe);
            }
            // Hack the new pattern into place
            for (String name : properties.stringPropertyNames()) {
                if (false == name.endsWith(".pattern"))
                    continue;
                // Null is weird here but we can't do anything with it so ignore it
                String value = properties.getProperty(name);
                if (value == null)
                    continue;
                // Tests don't need to be changed
                if (value.contains("%test_thread_info"))
                    continue;
                /*
                     * Patterns without a marker are sufficiently customized
                     * that we don't have an opinion about them.
                     */
                if (false == value.contains("%marker"))
                    continue;
                if (false == value.contains("%node_name")) {
                    locationsWithDeprecatedPatterns.add(source.getLocation());
                    properties.setProperty(name, value.replace("%marker", "[%node_name]%marker "));
                }
            }
            // end hack
            return new PropertiesConfigurationBuilder().setConfigurationSource(source).setRootProperties(properties).setLoggerContext(loggerContext).build();
        }
    };
    final Set<FileVisitOption> options = EnumSet.of(FileVisitOption.FOLLOW_LINKS);
    Files.walkFileTree(configsPath, options, Integer.MAX_VALUE, new SimpleFileVisitor<Path>() {

        @Override
        public FileVisitResult visitFile(final Path file, final BasicFileAttributes attrs) throws IOException {
            if (file.getFileName().toString().equals("log4j2.properties")) {
                configurations.add((PropertiesConfiguration) factory.getConfiguration(context, file.toString(), file.toUri()));
            }
            return FileVisitResult.CONTINUE;
        }
    });
    if (configurations.isEmpty()) {
        throw new UserException(ExitCodes.CONFIG, "no log4j2.properties found; tried [" + configsPath + "] and its subdirectories");
    }
    context.start(new CompositeConfiguration(configurations));
    configureLoggerLevels(settings);
    final String deprecatedLocationsString = String.join("\n  ", locationsWithDeprecatedPatterns);
    if (deprecatedLocationsString.length() > 0) {
        LogManager.getLogger(LogConfigurator.class).warn("Some logging configurations have %marker but don't have %node_name. " + "We will automatically add %node_name to the pattern to ease the migration for users who customize " + "log4j2.properties but will stop this behavior in 7.0. You should manually replace `%node_name` with " + "`[%node_name]%marker ` in these locations:\n  {}", deprecatedLocationsString);
    }
}
Also used : FileVisitOption(java.nio.file.FileVisitOption) ArrayList(java.util.ArrayList) Properties(java.util.Properties) PropertiesConfiguration(org.apache.logging.log4j.core.config.properties.PropertiesConfiguration) AbstractConfiguration(org.apache.logging.log4j.core.config.AbstractConfiguration) PropertiesConfigurationFactory(org.apache.logging.log4j.core.config.properties.PropertiesConfigurationFactory) ConfigurationException(org.apache.logging.log4j.core.config.ConfigurationException) UserException(org.elasticsearch.cli.UserException) BasicFileAttributes(java.nio.file.attribute.BasicFileAttributes) Path(java.nio.file.Path) ConfigurationSource(org.apache.logging.log4j.core.config.ConfigurationSource) InputStream(java.io.InputStream) CompositeConfiguration(org.apache.logging.log4j.core.config.composite.CompositeConfiguration) FileVisitResult(java.nio.file.FileVisitResult) IOException(java.io.IOException) LoggerContext(org.apache.logging.log4j.core.LoggerContext) PropertiesConfigurationBuilder(org.apache.logging.log4j.core.config.properties.PropertiesConfigurationBuilder)

Example 38 with UserException

use of org.elasticsearch.cli.UserException in project crate by crate.

the class RemoveSettingsCommand method processNodePaths.

@Override
protected void processNodePaths(Terminal terminal, Path[] dataPaths, OptionSet options, Environment env) throws IOException, UserException {
    final List<String> settingsToRemove = arguments.values(options);
    if (settingsToRemove.isEmpty()) {
        throw new UserException(ExitCodes.USAGE, "Must supply at least one setting to remove");
    }
    final PersistedClusterStateService persistedClusterStateService = createPersistedClusterStateService(env.settings(), dataPaths);
    terminal.println(Terminal.Verbosity.VERBOSE, "Loading cluster state");
    final Tuple<Long, ClusterState> termAndClusterState = loadTermAndClusterState(persistedClusterStateService, env);
    final ClusterState oldClusterState = termAndClusterState.v2();
    final Settings oldPersistentSettings = oldClusterState.metadata().persistentSettings();
    terminal.println(Terminal.Verbosity.VERBOSE, "persistent settings: " + oldPersistentSettings);
    final Settings.Builder newPersistentSettingsBuilder = Settings.builder().put(oldPersistentSettings);
    for (String settingToRemove : settingsToRemove) {
        boolean matched = false;
        for (String settingKey : oldPersistentSettings.keySet()) {
            if (Regex.simpleMatch(settingToRemove, settingKey)) {
                newPersistentSettingsBuilder.remove(settingKey);
                if (matched == false) {
                    terminal.println("The following settings will be removed:");
                }
                matched = true;
                terminal.println(settingKey + ": " + oldPersistentSettings.get(settingKey));
            }
        }
        if (matched == false) {
            throw new UserException(ExitCodes.USAGE, "No persistent cluster settings matching [" + settingToRemove + "] were found on this node");
        }
    }
    final ClusterState newClusterState = ClusterState.builder(oldClusterState).metadata(Metadata.builder(oldClusterState.metadata()).persistentSettings(newPersistentSettingsBuilder.build()).build()).build();
    terminal.println(Terminal.Verbosity.VERBOSE, "[old cluster state = " + oldClusterState + ", new cluster state = " + newClusterState + "]");
    confirm(terminal, CONFIRMATION_MSG);
    try (PersistedClusterStateService.Writer writer = persistedClusterStateService.createWriter()) {
        writer.writeFullStateAndCommit(termAndClusterState.v1(), newClusterState);
    }
    terminal.println(SETTINGS_REMOVED_MSG);
}
Also used : ClusterState(org.elasticsearch.cluster.ClusterState) UserException(org.elasticsearch.cli.UserException) PersistedClusterStateService(org.elasticsearch.gateway.PersistedClusterStateService) Settings(org.elasticsearch.common.settings.Settings)

Example 39 with UserException

use of org.elasticsearch.cli.UserException in project crate by crate.

the class RemoveSettingsCommandIT method testSettingDoesNotMatch.

public void testSettingDoesNotMatch() throws Exception {
    internalCluster().setBootstrapMasterNodeIndex(0);
    String node = internalCluster().startNode();
    client().admin().cluster().prepareUpdateSettings().setPersistentSettings(Settings.builder().put(DiskThresholdSettings.CLUSTER_ROUTING_ALLOCATION_DISK_THRESHOLD_ENABLED_SETTING.getKey(), false).build()).get();
    assertThat(client().admin().cluster().prepareState().get().getState().metadata().persistentSettings().keySet(), contains(DiskThresholdSettings.CLUSTER_ROUTING_ALLOCATION_DISK_THRESHOLD_ENABLED_SETTING.getKey()));
    Settings dataPathSettings = internalCluster().dataPathSettings(node);
    ensureStableCluster(1);
    internalCluster().stopRandomDataNode();
    Environment environment = TestEnvironment.newEnvironment(Settings.builder().put(internalCluster().getDefaultSettings()).put(dataPathSettings).build());
    UserException ex = expectThrows(UserException.class, () -> removeSettings(environment, false, new String[] { "cluster.routing.allocation.disk.bla.*" }));
    assertThat(ex.getMessage(), containsString("No persistent cluster settings matching [cluster.routing.allocation.disk.bla.*] were " + "found on this node"));
}
Also used : TestEnvironment(org.elasticsearch.env.TestEnvironment) Environment(org.elasticsearch.env.Environment) Matchers.containsString(org.hamcrest.Matchers.containsString) UserException(org.elasticsearch.cli.UserException) Settings(org.elasticsearch.common.settings.Settings) DiskThresholdSettings(org.elasticsearch.cluster.routing.allocation.DiskThresholdSettings)

Aggregations

UserException (org.elasticsearch.cli.UserException)39 Path (java.nio.file.Path)24 Environment (org.elasticsearch.env.Environment)14 Matchers.containsString (org.hamcrest.Matchers.containsString)9 IOException (java.io.IOException)5 Settings (org.elasticsearch.common.settings.Settings)4 BufferedReader (java.io.BufferedReader)3 ArrayList (java.util.ArrayList)3 InputStream (java.io.InputStream)2 InputStreamReader (java.io.InputStreamReader)2 FileVisitOption (java.nio.file.FileVisitOption)2 FileVisitResult (java.nio.file.FileVisitResult)2 BasicFileAttributes (java.nio.file.attribute.BasicFileAttributes)2 ZipInputStream (java.util.zip.ZipInputStream)2 Logger (org.apache.logging.log4j.Logger)2 LoggerContext (org.apache.logging.log4j.core.LoggerContext)2 AbstractConfiguration (org.apache.logging.log4j.core.config.AbstractConfiguration)2 CompositeConfiguration (org.apache.logging.log4j.core.config.composite.CompositeConfiguration)2 PropertiesConfiguration (org.apache.logging.log4j.core.config.properties.PropertiesConfiguration)2 PropertiesConfigurationFactory (org.apache.logging.log4j.core.config.properties.PropertiesConfigurationFactory)2