Search in sources :

Example 16 with UserException

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

the class CrateDB method execute.

@Override
protected void execute(Terminal terminal, OptionSet options, Environment env) throws Exception {
    if (options.nonOptionArguments().isEmpty() == false) {
        throw new UserException(ExitCodes.USAGE, "Positional arguments not allowed, found " + options.nonOptionArguments());
    }
    if (options.has(versionOption)) {
        if (options.has(daemonizeOption) || options.has(pidfileOption)) {
            throw new UserException(ExitCodes.USAGE, "CrateDB version option is mutually exclusive with any other option");
        }
        terminal.println("Version: " + Version.CURRENT + ", Build: " + Build.CURRENT.hashShort() + "/" + Build.CURRENT.timestamp() + ", JVM: " + JvmInfo.jvmInfo().version());
        return;
    }
    final boolean daemonize = options.has(daemonizeOption);
    final Path pidFile = pidfileOption.value(options);
    env = addPidFileSetting(pidFile, env);
    final boolean quiet = options.has(quietOption);
    try {
        init(daemonize, quiet, env);
    } catch (NodeValidationException e) {
        throw new UserException(ExitCodes.CONFIG, e.getMessage());
    }
}
Also used : Path(java.nio.file.Path) NodeValidationException(org.elasticsearch.node.NodeValidationException) UserException(org.elasticsearch.cli.UserException)

Example 17 with UserException

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

the class Elasticsearch method execute.

@Override
protected void execute(Terminal terminal, OptionSet options, Environment env) throws UserException {
    if (options.nonOptionArguments().isEmpty() == false) {
        throw new UserException(ExitCodes.USAGE, "Positional arguments not allowed, found " + options.nonOptionArguments());
    }
    if (options.has(versionOption)) {
        if (options.has(daemonizeOption) || options.has(pidfileOption)) {
            throw new UserException(ExitCodes.USAGE, "Elasticsearch version option is mutually exclusive with any other option");
        }
        terminal.println("Version: " + org.elasticsearch.Version.CURRENT + ", Build: " + Build.CURRENT.shortHash() + "/" + Build.CURRENT.date() + ", JVM: " + JvmInfo.jvmInfo().version());
        return;
    }
    final boolean daemonize = options.has(daemonizeOption);
    final Path pidFile = pidfileOption.value(options);
    final boolean quiet = options.has(quietOption);
    try {
        init(daemonize, pidFile, quiet, env);
    } catch (NodeValidationException e) {
        throw new UserException(ExitCodes.CONFIG, e.getMessage());
    }
}
Also used : Path(java.nio.file.Path) NodeValidationException(org.elasticsearch.node.NodeValidationException) UserException(org.elasticsearch.cli.UserException)

Example 18 with UserException

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

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);
    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 List<AbstractConfiguration> configurations = new ArrayList<>();
    final PropertiesConfigurationFactory factory = new PropertiesConfigurationFactory();
    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);
}
Also used : Path(java.nio.file.Path) FileVisitOption(java.nio.file.FileVisitOption) CompositeConfiguration(org.apache.logging.log4j.core.config.composite.CompositeConfiguration) ArrayList(java.util.ArrayList) FileVisitResult(java.nio.file.FileVisitResult) IOException(java.io.IOException) LoggerContext(org.apache.logging.log4j.core.LoggerContext) 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) UserException(org.elasticsearch.cli.UserException) BasicFileAttributes(java.nio.file.attribute.BasicFileAttributes)

Example 19 with UserException

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

the class InstallPluginCommand method installBin.

/** Copies the files from {@code tmpBinDir} into {@code destBinDir}, along with permissions from dest dirs parent. */
private void installBin(PluginInfo info, Path tmpBinDir, Path destBinDir) throws Exception {
    if (Files.isDirectory(tmpBinDir) == false) {
        throw new UserException(ExitCodes.IO_ERROR, "bin in plugin " + info.getName() + " is not a directory");
    }
    Files.createDirectory(destBinDir);
    setFileAttributes(destBinDir, BIN_DIR_PERMS);
    try (DirectoryStream<Path> stream = Files.newDirectoryStream(tmpBinDir)) {
        for (Path srcFile : stream) {
            if (Files.isDirectory(srcFile)) {
                throw new UserException(ExitCodes.DATA_ERROR, "Directories not allowed in bin dir for plugin " + info.getName() + ", found " + srcFile.getFileName());
            }
            Path destFile = destBinDir.resolve(tmpBinDir.relativize(srcFile));
            Files.copy(srcFile, destFile);
            setFileAttributes(destFile, BIN_FILES_PERMS);
        }
    }
    // clean up what we just copied
    IOUtils.rm(tmpBinDir);
}
Also used : Path(java.nio.file.Path) UserException(org.elasticsearch.cli.UserException)

Example 20 with UserException

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

the class InstallPluginCommand method verify.

/** Load information about the plugin, and verify it can be installed with no errors. */
private PluginInfo verify(Terminal terminal, Path pluginRoot, boolean isBatch, Environment env) throws Exception {
    // read and validate the plugin descriptor
    PluginInfo info = PluginInfo.readFromProperties(pluginRoot);
    // checking for existing version of the plugin
    final Path destination = env.pluginsFile().resolve(info.getName());
    if (Files.exists(destination)) {
        final String message = String.format(Locale.ROOT, "plugin directory [%s] already exists; if you need to update the plugin, uninstall it first using command 'remove %s'", destination.toAbsolutePath(), info.getName());
        throw new UserException(ExitCodes.CONFIG, message);
    }
    terminal.println(VERBOSE, info.toString());
    // they might be unavoidably in maven central and are packaged up the same way)
    if (MODULES.contains(info.getName())) {
        throw new UserException(ExitCodes.USAGE, "plugin '" + info.getName() + "' cannot be installed like this, it is a system module");
    }
    // check for jar hell before any copying
    jarHellCheck(pluginRoot, env.pluginsFile());
    // read optional security policy (extra permissions)
    // if it exists, confirm or warn the user
    Path policy = pluginRoot.resolve(PluginInfo.ES_PLUGIN_POLICY);
    if (Files.exists(policy)) {
        PluginSecurity.readPolicy(policy, terminal, env, isBatch);
    }
    return info;
}
Also used : Path(java.nio.file.Path) UserException(org.elasticsearch.cli.UserException)

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