Plugin Manager: add silent mode.

Now with have proper exit codes for elasticsearch plugin manager (see #3463), we can add a silent mode to plugin manager.

```sh
bin/plugin --install karmi/elasticsearch-paramedic --silent
```

Closes #3628.
This commit is contained in:
David Pilato 2013-09-05 16:20:37 +02:00
parent a91653cf1a
commit fafc4eef98
3 changed files with 84 additions and 42 deletions

View File

@ -102,6 +102,26 @@ Removing plugins typically take the following form:
plugin --remove <pluginname> plugin --remove <pluginname>
----------------------------------- -----------------------------------
[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] [float]
=== Known Plugins === Known Plugins

View File

@ -54,13 +54,19 @@ public class PluginManager {
public static final int LIST = 3; public static final int LIST = 3;
} }
public enum OutputMode {
DEFAULT, SILENT, VERBOSE
}
private final Environment environment; private final Environment environment;
private String url; private String url;
private OutputMode outputMode;
public PluginManager(Environment environment, String url) { public PluginManager(Environment environment, String url, OutputMode outputMode) {
this.environment = environment; this.environment = environment;
this.url = url; this.url = url;
this.outputMode = outputMode;
TrustManager[] trustAllCerts = new TrustManager[]{ TrustManager[] trustAllCerts = new TrustManager[]{
new X509TrustManager() { 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(); 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()) { if (!environment.pluginsFile().canWrite()) {
System.out.println(); System.err.println();
throw new IOException("plugin directory " + environment.pluginsFile() + " is read only"); throw new IOException("plugin directory " + environment.pluginsFile() + " is read only");
} }
@ -105,34 +118,28 @@ public class PluginManager {
} }
// first, try directly from the URL provided // first, try directly from the URL provided
boolean downloaded = false;
if (url != null) { if (url != null) {
URL pluginUrl = new URL(url); URL pluginUrl = new URL(url);
System.out.println("Trying " + pluginUrl.toExternalForm() + "..."); log("Trying " + pluginUrl.toExternalForm() + "...");
try { try {
downloadHelper.download(pluginUrl, pluginFile, new HttpDownloadHelper.VerboseProgress(System.out)); downloadHelper.download(pluginUrl, pluginFile, progress);
downloaded = true; downloaded = true;
} catch (IOException e) { } catch (IOException e) {
// ignore // ignore
if (verbose) { log("Failed: " + ExceptionsHelper.detailedMessage(e));
System.out.println("Failed: " + ExceptionsHelper.detailedMessage(e));
}
} }
} }
if (!downloaded) { if (!downloaded) {
// We try all possible locations // We try all possible locations
for (URL url: pluginHandle.urls()) { for (URL url: pluginHandle.urls()) {
System.out.println("Trying " + url.toExternalForm() + "..."); log("Trying " + url.toExternalForm() + "...");
try { try {
downloadHelper.download(url, pluginFile, new HttpDownloadHelper.VerboseProgress(System.out)); downloadHelper.download(url, pluginFile, progress);
downloaded = true; downloaded = true;
break; break;
} catch (Exception e) { } catch (Exception e) {
if (verbose) { debug("Failed: " + ExceptionsHelper.detailedMessage(e));
System.out.println("Failed: " + ExceptionsHelper.detailedMessage(e));
}
} }
} }
} }
@ -161,9 +168,9 @@ public class PluginManager {
FileSystemUtils.mkdirs(target.getParentFile()); FileSystemUtils.mkdirs(target.getParentFile());
Streams.copy(zipFile.getInputStream(zipEntry), new FileOutputStream(target)); 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) { } catch (Exception e) {
System.err.println("failed to extract plugin [" + pluginFile + "]: " + ExceptionsHelper.detailedMessage(e)); log("failed to extract plugin [" + pluginFile + "]: " + ExceptionsHelper.detailedMessage(e));
return; return;
} finally { } finally {
if (zipFile != null) { if (zipFile != null) {
@ -177,7 +184,7 @@ public class PluginManager {
} }
if (FileSystemUtils.hasExtensions(extractLocation, ".java")) { 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); FileSystemUtils.deleteRecursively(extractLocation);
return; return;
} }
@ -185,23 +192,23 @@ public class PluginManager {
File binFile = new File(extractLocation, "bin"); File binFile = new File(extractLocation, "bin");
if (binFile.exists() && binFile.isDirectory()) { if (binFile.exists() && binFile.isDirectory()) {
File toLocation = pluginHandle.binDir(environment); File toLocation = pluginHandle.binDir(environment);
System.out.println("Found bin, moving to " + toLocation.getAbsolutePath()); debug("Found bin, moving to " + toLocation.getAbsolutePath());
FileSystemUtils.deleteRecursively(toLocation); FileSystemUtils.deleteRecursively(toLocation);
binFile.renameTo(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 // 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 // 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 (!new File(extractLocation, "_site").exists()) {
if (!FileSystemUtils.hasExtensions(extractLocation, ".class", ".jar")) { 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 site = new File(extractLocation, "_site");
File tmpLocation = new File(environment.pluginsFile(), name + ".tmp"); File tmpLocation = new File(environment.pluginsFile(), name + ".tmp");
extractLocation.renameTo(tmpLocation); extractLocation.renameTo(tmpLocation);
FileSystemUtils.mkdirs(extractLocation); FileSystemUtils.mkdirs(extractLocation);
tmpLocation.renameTo(site); tmpLocation.renameTo(site);
System.out.println("Installed " + name + " into " + site.getAbsolutePath()); debug("Installed " + name + " into " + site.getAbsolutePath());
} }
} }
} }
@ -212,23 +219,26 @@ public class PluginManager {
File pluginToDelete = pluginHandle.extractedDir(environment); File pluginToDelete = pluginHandle.extractedDir(environment);
if (pluginToDelete.exists()) { if (pluginToDelete.exists()) {
debug("Removing: " + pluginToDelete.getPath());
FileSystemUtils.deleteRecursively(pluginToDelete, true); FileSystemUtils.deleteRecursively(pluginToDelete, true);
removed = true; removed = true;
} }
pluginToDelete = pluginHandle.distroFile(environment); pluginToDelete = pluginHandle.distroFile(environment);
if (pluginToDelete.exists()) { if (pluginToDelete.exists()) {
debug("Removing: " + pluginToDelete.getPath());
pluginToDelete.delete(); pluginToDelete.delete();
removed = true; removed = true;
} }
File binLocation = pluginHandle.binDir(environment); File binLocation = pluginHandle.binDir(environment);
if (binLocation.exists()) { if (binLocation.exists()) {
debug("Removing: " + binLocation.getPath());
FileSystemUtils.deleteRecursively(binLocation); FileSystemUtils.deleteRecursively(binLocation);
removed = true; removed = true;
} }
if (removed) { if (removed) {
System.out.println("Removed " + name); log("Removed " + name);
} else { } 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.");
} }
} }
@ -239,12 +249,12 @@ public class PluginManager {
public void listInstalledPlugins() { public void listInstalledPlugins() {
File[] plugins = getListInstalledPlugins(); File[] plugins = getListInstalledPlugins();
System.out.println("Installed plugins:"); log("Installed plugins:");
if (plugins == null || plugins.length == 0) { 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 { } else {
for (int i = 0; i < plugins.length; i++) { 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; String url = null;
boolean verbose = false; OutputMode outputMode = OutputMode.DEFAULT;
String pluginName = null; String pluginName = null;
int action = ACTION.NONE; int action = ACTION.NONE;
@ -301,9 +311,12 @@ public class PluginManager {
// Deprecated commands // Deprecated commands
|| "url".equals(command) || "-url".equals(command)) { || "url".equals(command) || "-url".equals(command)) {
url = args[++c]; 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".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") } else if (command.equals("-i") || command.equals("--install")
// Deprecated commands // Deprecated commands
|| command.equals("install") || command.equals("-install")) { || command.equals("install") || command.equals("-install")) {
@ -333,16 +346,16 @@ public class PluginManager {
if (action > ACTION.NONE) { if (action > ACTION.NONE) {
int exitCode = EXIT_CODE_ERROR; // we fail unless it's reset 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) { switch (action) {
case ACTION.INSTALL: case ACTION.INSTALL:
try { try {
System.out.println("-> Installing " + pluginName + "..."); pluginManager.log("-> Installing " + pluginName + "...");
pluginManager.downloadAndExtract(pluginName, verbose); pluginManager.downloadAndExtract(pluginName);
exitCode = EXIT_CODE_OK; exitCode = EXIT_CODE_OK;
} catch (IOException e) { } catch (IOException e) {
exitCode = EXIT_CODE_IO_ERROR; 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) { } catch (Throwable e) {
exitCode = EXIT_CODE_ERROR; exitCode = EXIT_CODE_ERROR;
displayHelp("Error while installing plugin, reason: " + e.getClass().getSimpleName() + displayHelp("Error while installing plugin, reason: " + e.getClass().getSimpleName() +
@ -351,12 +364,12 @@ public class PluginManager {
break; break;
case ACTION.REMOVE: case ACTION.REMOVE:
try { try {
System.out.println("-> Removing " + pluginName + " "); pluginManager.log("-> Removing " + pluginName + " ");
pluginManager.removePlugin(pluginName); pluginManager.removePlugin(pluginName);
exitCode = EXIT_CODE_OK; exitCode = EXIT_CODE_OK;
} catch (IOException e) { } catch (IOException e) {
exitCode = EXIT_CODE_IO_ERROR; 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) { } catch (Throwable e) {
exitCode = EXIT_CODE_ERROR; exitCode = EXIT_CODE_ERROR;
displayHelp("Error while removing plugin, reason: " + e.getClass().getSimpleName() + displayHelp("Error while removing plugin, reason: " + e.getClass().getSimpleName() +
@ -374,7 +387,7 @@ public class PluginManager {
break; break;
default: default:
System.out.println("Unknown Action [" + action + "]"); pluginManager.log("Unknown Action [" + action + "]");
exitCode = EXIT_CODE_ERROR; exitCode = EXIT_CODE_ERROR;
} }
@ -389,6 +402,7 @@ public class PluginManager {
System.out.println(" -r, --remove [plugin name] : Removes listed plugins"); System.out.println(" -r, --remove [plugin name] : Removes listed plugins");
System.out.println(" -l, --list : List installed plugins"); System.out.println(" -l, --list : List installed plugins");
System.out.println(" -v, --verbose : Prints verbose messages"); 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(" -h, --help : Prints this help message");
System.out.println(); System.out.println();
System.out.println(" [*] Plugin name could be:"); 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 * Helper class to extract properly user name, repository name, version and plugin name
* from plugin name given by a user. * from plugin name given by a user.

View File

@ -41,7 +41,7 @@ import java.io.File;
import java.io.IOException; import java.io.IOException;
import java.net.URL; import java.net.URL;
import static org.hamcrest.Matchers.*; import static org.hamcrest.CoreMatchers.*;
public class PluginManagerTests extends AbstractNodesTests { public class PluginManagerTests extends AbstractNodesTests {
@ -107,11 +107,11 @@ public class PluginManagerTests extends AbstractNodesTests {
if (!initialSettings.v2().pluginsFile().exists()) { if (!initialSettings.v2().pluginsFile().exists()) {
FileSystemUtils.mkdirs(initialSettings.v2().pluginsFile()); 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 { private static void downloadAndExtract(String pluginName, String pluginUrl) throws IOException {
pluginManager(pluginUrl).downloadAndExtract(pluginName, false); pluginManager(pluginUrl).downloadAndExtract(pluginName);
} }
private Node startNode() { private Node startNode() {
@ -157,7 +157,7 @@ public class PluginManagerTests extends AbstractNodesTests {
private void singlePluginInstallAndRemove(String pluginName, String pluginCoordinates) throws IOException { private void singlePluginInstallAndRemove(String pluginName, String pluginCoordinates) throws IOException {
PluginManager pluginManager = pluginManager(pluginCoordinates); PluginManager pluginManager = pluginManager(pluginCoordinates);
pluginManager.downloadAndExtract("plugin", false); pluginManager.downloadAndExtract("plugin");
File[] plugins = pluginManager.getListInstalledPlugins(); File[] plugins = pluginManager.getListInstalledPlugins();
assertThat(plugins, notNullValue()); assertThat(plugins, notNullValue());
assertThat(plugins.length, is(1)); assertThat(plugins.length, is(1));