Merge pull request #20338 from jasontedor/remove-plugin

Print message when removing plugin with config
This commit is contained in:
Jason Tedor 2016-09-06 11:43:51 -04:00 committed by GitHub
commit 0d7dfcd798
3 changed files with 49 additions and 14 deletions

View File

@ -470,7 +470,6 @@
<suppress files="core[/\\]src[/\\]main[/\\]java[/\\]org[/\\]elasticsearch[/\\]node[/\\]Node.java" checks="LineLength" />
<suppress files="core[/\\]src[/\\]main[/\\]java[/\\]org[/\\]elasticsearch[/\\]node[/\\]internal[/\\]InternalSettingsPreparer.java" checks="LineLength" />
<suppress files="core[/\\]src[/\\]main[/\\]java[/\\]org[/\\]elasticsearch[/\\]plugins[/\\]PluginsService.java" checks="LineLength" />
<suppress files="core[/\\]src[/\\]main[/\\]java[/\\]org[/\\]elasticsearch[/\\]plugins[/\\]RemovePluginCommand.java" checks="LineLength" />
<suppress files="core[/\\]src[/\\]main[/\\]java[/\\]org[/\\]elasticsearch[/\\]repositories[/\\]RepositoriesService.java" checks="LineLength" />
<suppress files="core[/\\]src[/\\]main[/\\]java[/\\]org[/\\]elasticsearch[/\\]repositories[/\\]Repository.java" checks="LineLength" />
<suppress files="core[/\\]src[/\\]main[/\\]java[/\\]org[/\\]elasticsearch[/\\]repositories[/\\]RepositoryModule.java" checks="LineLength" />

View File

@ -43,7 +43,7 @@ import static org.elasticsearch.cli.Terminal.Verbosity.VERBOSE;
/**
* A command for the plugin cli to remove a plugin from elasticsearch.
*/
class RemovePluginCommand extends SettingCommand {
final class RemovePluginCommand extends SettingCommand {
private final OptionSpec<String> arguments;
@ -64,14 +64,16 @@ class RemovePluginCommand extends SettingCommand {
terminal.println("-> Removing " + Strings.coalesceToEmpty(pluginName) + "...");
Path pluginDir = env.pluginsFile().resolve(pluginName);
final Path pluginDir = env.pluginsFile().resolve(pluginName);
if (Files.exists(pluginDir) == false) {
throw new UserException(ExitCodes.USAGE, "plugin " + pluginName + " not found; run 'elasticsearch-plugin list' to get list of installed plugins");
throw new UserException(
ExitCodes.USAGE,
"plugin " + pluginName + " not found; run 'elasticsearch-plugin list' to get list of installed plugins");
}
List<Path> pluginPaths = new ArrayList<>();
final List<Path> pluginPaths = new ArrayList<>();
Path pluginBinDir = env.binFile().resolve(pluginName);
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");
@ -81,10 +83,19 @@ class RemovePluginCommand extends SettingCommand {
}
terminal.println(VERBOSE, "Removing: " + pluginDir);
Path tmpPluginDir = env.pluginsFile().resolve(".removing-" + pluginName);
final Path tmpPluginDir = env.pluginsFile().resolve(".removing-" + pluginName);
Files.move(pluginDir, tmpPluginDir, StandardCopyOption.ATOMIC_MOVE);
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");
}
}
}

View File

@ -19,6 +19,14 @@
package org.elasticsearch.plugins;
import org.apache.lucene.util.LuceneTestCase;
import org.elasticsearch.cli.MockTerminal;
import org.elasticsearch.cli.UserException;
import org.elasticsearch.common.settings.Settings;
import org.elasticsearch.env.Environment;
import org.elasticsearch.test.ESTestCase;
import org.junit.Before;
import java.io.IOException;
import java.nio.file.DirectoryStream;
import java.nio.file.Files;
@ -26,13 +34,8 @@ import java.nio.file.Path;
import java.util.HashMap;
import java.util.Map;
import org.apache.lucene.util.LuceneTestCase;
import org.elasticsearch.cli.UserException;
import org.elasticsearch.cli.MockTerminal;
import org.elasticsearch.common.settings.Settings;
import org.elasticsearch.env.Environment;
import org.elasticsearch.test.ESTestCase;
import org.junit.Before;
import static org.hamcrest.CoreMatchers.containsString;
import static org.hamcrest.CoreMatchers.not;
@LuceneTestCase.SuppressFileSystems("*")
public class RemovePluginCommandTests extends ESTestCase {
@ -109,4 +112,26 @@ public class RemovePluginCommandTests extends ESTestCase {
assertRemoveCleaned(env);
}
public void testConfigDirPreserved() throws Exception {
Files.createDirectories(env.pluginsFile().resolve("fake"));
final Path configDir = env.configFile().resolve("fake");
Files.createDirectories(configDir);
Files.createFile(configDir.resolve("fake.yml"));
final MockTerminal terminal = removePlugin("fake", home);
assertTrue(Files.exists(env.configFile().resolve("fake")));
assertThat(terminal.getOutput(), containsString(expectedConfigDirPreservedMessage(configDir)));
assertRemoveCleaned(env);
}
public void testNoConfigDirPreserved() throws Exception {
Files.createDirectories(env.pluginsFile().resolve("fake"));
final Path configDir = env.configFile().resolve("fake");
final MockTerminal terminal = removePlugin("fake", home);
assertThat(terminal.getOutput(), not(containsString(expectedConfigDirPreservedMessage(configDir))));
}
private String expectedConfigDirPreservedMessage(final Path configDir) {
return "-> Preserving plugin config files [" + configDir + "] in case of upgrade, delete manually if not needed";
}
}