diff --git a/docs/reference/modules/plugins.asciidoc b/docs/reference/modules/plugins.asciidoc index 5d0be647848..9ee8bf7c3fc 100644 --- a/docs/reference/modules/plugins.asciidoc +++ b/docs/reference/modules/plugins.asciidoc @@ -102,6 +102,26 @@ Removing plugins typically take the following form: plugin --remove ----------------------------------- +[float] +==== Silent/Verbose mode + +When running the `plugin` script, you can get more information (debug mode) using `--verbose`. +On the opposite, if you want `plugin` script to be silent, use `--silent` option. + +Note that exit codes could be: + +* `0`: everything was OK +* `64`: unknown command or incorrect option parameter +* `74`: IO error +* `70`: other errors + +[source,shell] +----------------------------------- +bin/plugin --install mobz/elasticsearch-head --verbose +plugin --remove head --silent +----------------------------------- + + [float] === Known Plugins diff --git a/src/main/java/org/elasticsearch/plugins/PluginManager.java b/src/main/java/org/elasticsearch/plugins/PluginManager.java index b052a2a1fb0..def9395eafc 100644 --- a/src/main/java/org/elasticsearch/plugins/PluginManager.java +++ b/src/main/java/org/elasticsearch/plugins/PluginManager.java @@ -54,13 +54,19 @@ public class PluginManager { public static final int LIST = 3; } + public enum OutputMode { + DEFAULT, SILENT, VERBOSE + } + private final Environment environment; private String url; + private OutputMode outputMode; - public PluginManager(Environment environment, String url) { + public PluginManager(Environment environment, String url, OutputMode outputMode) { this.environment = environment; this.url = url; + this.outputMode = outputMode; TrustManager[] trustAllCerts = new TrustManager[]{ new X509TrustManager() { @@ -88,11 +94,18 @@ public class PluginManager { } } - public void downloadAndExtract(String name, boolean verbose) throws IOException { + public void downloadAndExtract(String name) throws IOException { HttpDownloadHelper downloadHelper = new HttpDownloadHelper(); + boolean downloaded = false; + HttpDownloadHelper.DownloadProgress progress; + if (outputMode == OutputMode.SILENT) { + progress = new HttpDownloadHelper.NullProgress(); + } else { + progress = new HttpDownloadHelper.VerboseProgress(System.out); + } if (!environment.pluginsFile().canWrite()) { - System.out.println(); + System.err.println(); throw new IOException("plugin directory " + environment.pluginsFile() + " is read only"); } @@ -105,34 +118,28 @@ public class PluginManager { } // first, try directly from the URL provided - boolean downloaded = false; - if (url != null) { URL pluginUrl = new URL(url); - System.out.println("Trying " + pluginUrl.toExternalForm() + "..."); + log("Trying " + pluginUrl.toExternalForm() + "..."); try { - downloadHelper.download(pluginUrl, pluginFile, new HttpDownloadHelper.VerboseProgress(System.out)); + downloadHelper.download(pluginUrl, pluginFile, progress); downloaded = true; } catch (IOException e) { // ignore - if (verbose) { - System.out.println("Failed: " + ExceptionsHelper.detailedMessage(e)); - } + log("Failed: " + ExceptionsHelper.detailedMessage(e)); } } if (!downloaded) { // We try all possible locations for (URL url: pluginHandle.urls()) { - System.out.println("Trying " + url.toExternalForm() + "..."); + log("Trying " + url.toExternalForm() + "..."); try { - downloadHelper.download(url, pluginFile, new HttpDownloadHelper.VerboseProgress(System.out)); + downloadHelper.download(url, pluginFile, progress); downloaded = true; break; } catch (Exception e) { - if (verbose) { - System.out.println("Failed: " + ExceptionsHelper.detailedMessage(e)); - } + debug("Failed: " + ExceptionsHelper.detailedMessage(e)); } } } @@ -161,9 +168,9 @@ public class PluginManager { FileSystemUtils.mkdirs(target.getParentFile()); Streams.copy(zipFile.getInputStream(zipEntry), new FileOutputStream(target)); } - System.out.println("Installed " + name + " into " + extractLocation.getAbsolutePath()); + log("Installed " + name + " into " + extractLocation.getAbsolutePath()); } catch (Exception e) { - System.err.println("failed to extract plugin [" + pluginFile + "]: " + ExceptionsHelper.detailedMessage(e)); + log("failed to extract plugin [" + pluginFile + "]: " + ExceptionsHelper.detailedMessage(e)); return; } finally { if (zipFile != null) { @@ -177,7 +184,7 @@ public class PluginManager { } if (FileSystemUtils.hasExtensions(extractLocation, ".java")) { - System.out.println("Plugin installation assumed to be site plugin, but contains source code, aborting installation..."); + debug("Plugin installation assumed to be site plugin, but contains source code, aborting installation..."); FileSystemUtils.deleteRecursively(extractLocation); return; } @@ -185,23 +192,23 @@ public class PluginManager { File binFile = new File(extractLocation, "bin"); if (binFile.exists() && binFile.isDirectory()) { File toLocation = pluginHandle.binDir(environment); - System.out.println("Found bin, moving to " + toLocation.getAbsolutePath()); + debug("Found bin, moving to " + toLocation.getAbsolutePath()); FileSystemUtils.deleteRecursively(toLocation); binFile.renameTo(toLocation); - System.out.println("Installed " + name + " into " + toLocation.getAbsolutePath()); + debug("Installed " + name + " into " + toLocation.getAbsolutePath()); } // try and identify the plugin type, see if it has no .class or .jar files in it // so its probably a _site, and it it does not have a _site in it, move everything to _site if (!new File(extractLocation, "_site").exists()) { if (!FileSystemUtils.hasExtensions(extractLocation, ".class", ".jar")) { - System.out.println("Identified as a _site plugin, moving to _site structure ..."); + log("Identified as a _site plugin, moving to _site structure ..."); File site = new File(extractLocation, "_site"); File tmpLocation = new File(environment.pluginsFile(), name + ".tmp"); extractLocation.renameTo(tmpLocation); FileSystemUtils.mkdirs(extractLocation); tmpLocation.renameTo(site); - System.out.println("Installed " + name + " into " + site.getAbsolutePath()); + debug("Installed " + name + " into " + site.getAbsolutePath()); } } } @@ -212,24 +219,27 @@ public class PluginManager { File pluginToDelete = pluginHandle.extractedDir(environment); if (pluginToDelete.exists()) { + debug("Removing: " + pluginToDelete.getPath()); FileSystemUtils.deleteRecursively(pluginToDelete, true); removed = true; } pluginToDelete = pluginHandle.distroFile(environment); if (pluginToDelete.exists()) { + debug("Removing: " + pluginToDelete.getPath()); pluginToDelete.delete(); removed = true; } File binLocation = pluginHandle.binDir(environment); if (binLocation.exists()) { + debug("Removing: " + binLocation.getPath()); FileSystemUtils.deleteRecursively(binLocation); removed = true; } if (removed) { - System.out.println("Removed " + name); + log("Removed " + name); } else { - System.out.println("Plugin " + name + " not found. Run plugin --list to get list of installed plugins."); - } + log("Plugin " + name + " not found. Run plugin --list to get list of installed plugins."); + } } public File[] getListInstalledPlugins() { @@ -239,12 +249,12 @@ public class PluginManager { public void listInstalledPlugins() { File[] plugins = getListInstalledPlugins(); - System.out.println("Installed plugins:"); + log("Installed plugins:"); if (plugins == null || plugins.length == 0) { - System.out.println(" - No plugin detected in " + environment.pluginsFile().getAbsolutePath()); + log(" - No plugin detected in " + environment.pluginsFile().getAbsolutePath()); } else { for (int i = 0; i < plugins.length; i++) { - System.out.println(" - " + plugins[i].getName()); + log(" - " + plugins[i].getName()); } } } @@ -286,7 +296,7 @@ public class PluginManager { } String url = null; - boolean verbose = false; + OutputMode outputMode = OutputMode.DEFAULT; String pluginName = null; int action = ACTION.NONE; @@ -301,9 +311,12 @@ public class PluginManager { // Deprecated commands || "url".equals(command) || "-url".equals(command)) { url = args[++c]; - } else if ("-v".equals(command) || "--v".equals(command) + } else if ("-v".equals(command) || "--verbose".equals(command) || "verbose".equals(command) || "-verbose".equals(command)) { - verbose = true; + outputMode = OutputMode.VERBOSE; + } else if ("-s".equals(command) || "--silent".equals(command) + || "silent".equals(command) || "-silent".equals(command)) { + outputMode = OutputMode.SILENT; } else if (command.equals("-i") || command.equals("--install") // Deprecated commands || command.equals("install") || command.equals("-install")) { @@ -333,16 +346,16 @@ public class PluginManager { if (action > ACTION.NONE) { int exitCode = EXIT_CODE_ERROR; // we fail unless it's reset - PluginManager pluginManager = new PluginManager(initialSettings.v2(), url); + PluginManager pluginManager = new PluginManager(initialSettings.v2(), url, outputMode); switch (action) { case ACTION.INSTALL: try { - System.out.println("-> Installing " + pluginName + "..."); - pluginManager.downloadAndExtract(pluginName, verbose); + pluginManager.log("-> Installing " + pluginName + "..."); + pluginManager.downloadAndExtract(pluginName); exitCode = EXIT_CODE_OK; } catch (IOException e) { exitCode = EXIT_CODE_IO_ERROR; - System.out.println("Failed to install " + pluginName + ", reason: " + e.getMessage()); + pluginManager.log("Failed to install " + pluginName + ", reason: " + e.getMessage()); } catch (Throwable e) { exitCode = EXIT_CODE_ERROR; displayHelp("Error while installing plugin, reason: " + e.getClass().getSimpleName() + @@ -351,12 +364,12 @@ public class PluginManager { break; case ACTION.REMOVE: try { - System.out.println("-> Removing " + pluginName + " "); + pluginManager.log("-> Removing " + pluginName + " "); pluginManager.removePlugin(pluginName); exitCode = EXIT_CODE_OK; } catch (IOException e) { exitCode = EXIT_CODE_IO_ERROR; - System.out.println("Failed to remove " + pluginName + ", reason: " + e.getMessage()); + pluginManager.log("Failed to remove " + pluginName + ", reason: " + e.getMessage()); } catch (Throwable e) { exitCode = EXIT_CODE_ERROR; displayHelp("Error while removing plugin, reason: " + e.getClass().getSimpleName() + @@ -374,7 +387,7 @@ public class PluginManager { break; default: - System.out.println("Unknown Action [" + action + "]"); + pluginManager.log("Unknown Action [" + action + "]"); exitCode = EXIT_CODE_ERROR; } @@ -389,6 +402,7 @@ public class PluginManager { System.out.println(" -r, --remove [plugin name] : Removes listed plugins"); System.out.println(" -l, --list : List installed plugins"); System.out.println(" -v, --verbose : Prints verbose messages"); + System.out.println(" -s, --silent : Run in silent mode"); System.out.println(" -h, --help : Prints this help message"); System.out.println(); System.out.println(" [*] Plugin name could be:"); @@ -403,6 +417,14 @@ public class PluginManager { } } + private void debug(String line) { + if (outputMode == OutputMode.VERBOSE) System.out.println(line); + } + + private void log(String line) { + if (outputMode != OutputMode.SILENT) System.out.println(line); + } + /** * Helper class to extract properly user name, repository name, version and plugin name * from plugin name given by a user. diff --git a/src/test/java/org/elasticsearch/test/integration/plugin/PluginManagerTests.java b/src/test/java/org/elasticsearch/test/integration/plugin/PluginManagerTests.java index 791d588f118..67b5ba64d1f 100644 --- a/src/test/java/org/elasticsearch/test/integration/plugin/PluginManagerTests.java +++ b/src/test/java/org/elasticsearch/test/integration/plugin/PluginManagerTests.java @@ -41,7 +41,7 @@ import java.io.File; import java.io.IOException; import java.net.URL; -import static org.hamcrest.Matchers.*; +import static org.hamcrest.CoreMatchers.*; public class PluginManagerTests extends AbstractNodesTests { @@ -107,11 +107,11 @@ public class PluginManagerTests extends AbstractNodesTests { if (!initialSettings.v2().pluginsFile().exists()) { FileSystemUtils.mkdirs(initialSettings.v2().pluginsFile()); } - return new PluginManager(initialSettings.v2(), pluginUrl); + return new PluginManager(initialSettings.v2(), pluginUrl, PluginManager.OutputMode.SILENT); } private static void downloadAndExtract(String pluginName, String pluginUrl) throws IOException { - pluginManager(pluginUrl).downloadAndExtract(pluginName, false); + pluginManager(pluginUrl).downloadAndExtract(pluginName); } private Node startNode() { @@ -157,7 +157,7 @@ public class PluginManagerTests extends AbstractNodesTests { private void singlePluginInstallAndRemove(String pluginName, String pluginCoordinates) throws IOException { PluginManager pluginManager = pluginManager(pluginCoordinates); - pluginManager.downloadAndExtract("plugin", false); + pluginManager.downloadAndExtract("plugin"); File[] plugins = pluginManager.getListInstalledPlugins(); assertThat(plugins, notNullValue()); assertThat(plugins.length, is(1));