Search in sources :

Example 21 with UserException

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

the class InstallPluginCommand method execute.

// pkg private for testing
void execute(Terminal terminal, String pluginId, boolean isBatch, Environment env) throws Exception {
    if (pluginId == null) {
        throw new UserException(ExitCodes.USAGE, "plugin id is required");
    }
    // TODO: remove this leniency!! is it needed anymore?
    if (Files.exists(env.pluginsFile()) == false) {
        terminal.println("Plugins directory [" + env.pluginsFile() + "] does not exist. Creating...");
        Files.createDirectory(env.pluginsFile());
    }
    Path pluginZip = download(terminal, pluginId, env.tmpFile());
    Path extractedZip = unzip(pluginZip, env.pluginsFile());
    install(terminal, isBatch, extractedZip, env);
}
Also used : Path(java.nio.file.Path) UserException(org.elasticsearch.cli.UserException)

Example 22 with UserException

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

the class InstallPluginCommand method unzip.

private Path unzip(Path zip, Path pluginsDir) throws IOException, UserException {
    // unzip plugin to a staging temp dir
    final Path target = stagingDirectory(pluginsDir);
    pathsToDeleteOnShutdown.add(target);
    boolean hasEsDir = false;
    try (ZipInputStream zipInput = new ZipInputStream(Files.newInputStream(zip))) {
        ZipEntry entry;
        byte[] buffer = new byte[8192];
        while ((entry = zipInput.getNextEntry()) != null) {
            if (entry.getName().startsWith("elasticsearch/") == false) {
                // only extract the elasticsearch directory
                continue;
            }
            hasEsDir = true;
            Path targetFile = target.resolve(entry.getName().substring("elasticsearch/".length()));
            // and ensuring the normalized entry is still rooted with the target plugin directory.
            if (targetFile.normalize().startsWith(target) == false) {
                throw new IOException("Zip contains entry name '" + entry.getName() + "' resolving outside of plugin directory");
            }
            // before their children (although this makes sense, but is it guaranteed?)
            if (!Files.isSymbolicLink(targetFile.getParent())) {
                Files.createDirectories(targetFile.getParent());
            }
            if (entry.isDirectory() == false) {
                try (OutputStream out = Files.newOutputStream(targetFile)) {
                    int len;
                    while ((len = zipInput.read(buffer)) >= 0) {
                        out.write(buffer, 0, len);
                    }
                }
            }
            zipInput.closeEntry();
        }
    }
    Files.delete(zip);
    if (hasEsDir == false) {
        IOUtils.rm(target);
        throw new UserException(ExitCodes.DATA_ERROR, "`elasticsearch` directory is missing in the plugin zip");
    }
    return target;
}
Also used : Path(java.nio.file.Path) ZipInputStream(java.util.zip.ZipInputStream) ZipEntry(java.util.zip.ZipEntry) OutputStream(java.io.OutputStream) IOException(java.io.IOException) UserException(org.elasticsearch.cli.UserException)

Example 23 with UserException

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

the class RemovePluginCommand method execute.

// pkg private for testing
void execute(Terminal terminal, String pluginName, Environment env) throws Exception {
    if (pluginName == null) {
        throw new UserException(ExitCodes.USAGE, "plugin name is required");
    }
    terminal.println("-> Removing " + Strings.coalesceToEmpty(pluginName) + "...");
    final Path pluginDir = env.pluginsFile().resolve(pluginName);
    if (Files.exists(pluginDir) == false) {
        throw new UserException(ExitCodes.CONFIG, "plugin " + pluginName + " not found; run 'elasticsearch-plugin list' to get list of installed plugins");
    }
    final List<Path> pluginPaths = new ArrayList<>();
    final Path pluginBinDir = env.binFile().resolve(pluginName);
    if (Files.exists(pluginBinDir)) {
        if (Files.isDirectory(pluginBinDir) == false) {
            throw new UserException(ExitCodes.IO_ERROR, "Bin dir for " + pluginName + " is not a directory");
        }
        pluginPaths.add(pluginBinDir);
        terminal.println(VERBOSE, "Removing: " + pluginBinDir);
    }
    terminal.println(VERBOSE, "Removing: " + pluginDir);
    final Path tmpPluginDir = env.pluginsFile().resolve(".removing-" + pluginName);
    try {
        Files.move(pluginDir, tmpPluginDir, StandardCopyOption.ATOMIC_MOVE);
    } catch (final AtomicMoveNotSupportedException e) {
        // this can happen on a union filesystem when a plugin is not installed on the top layer; we fall back to a non-atomic move
        Files.move(pluginDir, tmpPluginDir);
    }
    pluginPaths.add(tmpPluginDir);
    IOUtils.rm(pluginPaths.toArray(new Path[pluginPaths.size()]));
    // we preserve the config files in case the user is upgrading the plugin, but we print
    // a message so the user knows in case they want to remove manually
    final Path pluginConfigDir = env.configFile().resolve(pluginName);
    if (Files.exists(pluginConfigDir)) {
        terminal.println("-> Preserving plugin config files [" + pluginConfigDir + "] in case of upgrade, delete manually if not needed");
    }
}
Also used : Path(java.nio.file.Path) ArrayList(java.util.ArrayList) UserException(org.elasticsearch.cli.UserException) AtomicMoveNotSupportedException(java.nio.file.AtomicMoveNotSupportedException)

Example 24 with UserException

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

the class RemoveSettingKeyStoreCommandTests method testNoSettings.

public void testNoSettings() throws Exception {
    createKeystore("");
    UserException e = expectThrows(UserException.class, this::execute);
    assertEquals(ExitCodes.USAGE, e.exitCode);
    assertThat(e.getMessage(), containsString("Must supply at least one setting"));
}
Also used : UserException(org.elasticsearch.cli.UserException)

Example 25 with UserException

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

the class RemoveSettingKeyStoreCommandTests method testMissing.

public void testMissing() throws Exception {
    UserException e = expectThrows(UserException.class, () -> execute("foo"));
    assertEquals(ExitCodes.DATA_ERROR, e.exitCode);
    assertThat(e.getMessage(), containsString("keystore not found"));
}
Also used : 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