Skip to content

Commit

Permalink
Plugin Manager: add silent mode.
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
dadoonet committed Sep 10, 2013
1 parent 226ec08 commit eab5547
Show file tree
Hide file tree
Showing 3 changed files with 84 additions and 42 deletions.
20 changes: 20 additions & 0 deletions docs/reference/modules/plugins.asciidoc
Expand Up @@ -102,6 +102,26 @@ Removing plugins typically take the following form:
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]
=== Known Plugins

Expand Down
98 changes: 60 additions & 38 deletions src/main/java/org/elasticsearch/plugins/PluginManager.java
Expand Up @@ -54,13 +54,19 @@ public static final class ACTION {
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() {
Expand Down Expand Up @@ -88,11 +94,18 @@ public void checkServerTrusted(
}
}

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");
}

Expand All @@ -105,34 +118,28 @@ public void downloadAndExtract(String name, boolean verbose) throws IOException
}

// 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));
}
}
}
Expand Down Expand Up @@ -161,9 +168,9 @@ public void downloadAndExtract(String name, boolean verbose) throws IOException
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) {
Expand All @@ -177,31 +184,31 @@ public void downloadAndExtract(String name, boolean verbose) throws IOException
}

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;
}

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());
}
}
}
Expand All @@ -212,24 +219,27 @@ public void removePlugin(String name) throws IOException {

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() {
Expand All @@ -239,12 +249,12 @@ public File[] getListInstalledPlugins() {

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());
}
}
}
Expand Down Expand Up @@ -286,7 +296,7 @@ public static void main(String[] args) {
}

String url = null;
boolean verbose = false;
OutputMode outputMode = OutputMode.DEFAULT;
String pluginName = null;
int action = ACTION.NONE;

Expand All @@ -301,9 +311,12 @@ public static void main(String[] args) {
// 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")) {
Expand Down Expand Up @@ -333,16 +346,16 @@ public static void main(String[] args) {

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() +
Expand All @@ -351,12 +364,12 @@ public static void main(String[] args) {
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() +
Expand All @@ -374,7 +387,7 @@ public static void main(String[] args) {
break;

default:
System.out.println("Unknown Action [" + action + "]");
pluginManager.log("Unknown Action [" + action + "]");
exitCode = EXIT_CODE_ERROR;

}
Expand All @@ -389,6 +402,7 @@ private static void displayHelp(String message) {
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:");
Expand All @@ -403,6 +417,14 @@ private static void displayHelp(String message) {
}
}

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.
Expand Down
Expand Up @@ -41,7 +41,7 @@
import java.io.IOException;
import java.net.URL;

import static org.hamcrest.Matchers.*;
import static org.hamcrest.CoreMatchers.*;

public class PluginManagerTests extends AbstractNodesTests {

Expand Down Expand Up @@ -107,11 +107,11 @@ private static PluginManager pluginManager(String pluginUrl) {
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() {
Expand Down Expand Up @@ -157,7 +157,7 @@ private void deletePluginsFolder() {

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));
Expand Down

0 comments on commit eab5547

Please sign in to comment.