diff --git a/core/src/main/java/org/elasticsearch/plugins/InstallPluginCommand.java b/core/src/main/java/org/elasticsearch/plugins/InstallPluginCommand.java index 403816aafa8..dea6fcba312 100644 --- a/core/src/main/java/org/elasticsearch/plugins/InstallPluginCommand.java +++ b/core/src/main/java/org/elasticsearch/plugins/InstallPluginCommand.java @@ -418,6 +418,18 @@ class InstallPluginCommand extends EnvironmentAwareCommand { private PluginInfo verify(Terminal terminal, Path pluginRoot, boolean isBatch, Environment env) throws Exception { // read and validate the plugin descriptor PluginInfo info = PluginInfo.readFromProperties(pluginRoot); + + // checking for existing version of the plugin + final Path destination = env.pluginsFile().resolve(info.getName()); + if (Files.exists(destination)) { + final String message = String.format( + Locale.ROOT, + "plugin directory [%s] already exists; if you need to update the plugin, uninstall it first using command 'remove %s'", + destination.toAbsolutePath(), + info.getName()); + throw new UserException(ExitCodes.CONFIG, message); + } + terminal.println(VERBOSE, info.toString()); // don't let user install plugin as a module... @@ -471,14 +483,7 @@ class InstallPluginCommand extends EnvironmentAwareCommand { try { PluginInfo info = verify(terminal, tmpRoot, isBatch, env); - final Path destination = env.pluginsFile().resolve(info.getName()); - if (Files.exists(destination)) { - throw new UserException( - ExitCodes.USAGE, - "plugin directory " + destination.toAbsolutePath() + - " already exists. To update the plugin, uninstall it first using 'remove " + info.getName() + "' command"); - } Path tmpBinDir = tmpRoot.resolve("bin"); if (Files.exists(tmpBinDir)) { diff --git a/qa/evil-tests/src/test/java/org/elasticsearch/plugins/InstallPluginCommandTests.java b/qa/evil-tests/src/test/java/org/elasticsearch/plugins/InstallPluginCommandTests.java index 96e64e5c888..5ea91eae394 100644 --- a/qa/evil-tests/src/test/java/org/elasticsearch/plugins/InstallPluginCommandTests.java +++ b/qa/evil-tests/src/test/java/org/elasticsearch/plugins/InstallPluginCommandTests.java @@ -60,19 +60,17 @@ import java.nio.file.attribute.PosixFileAttributes; import java.nio.file.attribute.PosixFilePermission; import java.nio.file.attribute.UserPrincipal; import java.util.ArrayList; -import java.util.HashMap; import java.util.HashSet; import java.util.List; -import java.util.Map; import java.util.Set; import java.util.function.Function; import java.util.stream.Collectors; import java.util.zip.ZipEntry; import java.util.zip.ZipOutputStream; -import static org.hamcrest.CoreMatchers.containsString; import static org.hamcrest.CoreMatchers.equalTo; import static org.hamcrest.Matchers.containsInAnyOrder; +import static org.hamcrest.Matchers.containsString; import static org.hamcrest.Matchers.not; @LuceneTestCase.SuppressFileSystems("*") @@ -673,6 +671,18 @@ public class InstallPluginCommandTests extends ESTestCase { assertThat(terminal.getOutput(), not(containsString("100%"))); } + public void testPluginAlreadyInstalled() throws Exception { + Tuple env = createEnv(fs, temp); + Path pluginDir = createPluginDir(temp); + String pluginZip = createPlugin("fake", pluginDir); + installPlugin(pluginZip, env.v1()); + final UserException e = expectThrows(UserException.class, () -> installPlugin(pluginZip, env.v1(), randomBoolean())); + assertThat( + e.getMessage(), + equalTo("plugin directory [" + env.v2().pluginsFile().resolve("fake") + "] already exists; " + + "if you need to update the plugin, uninstall it first using command 'remove fake'")); + } + private void installPlugin(MockTerminal terminal, boolean isBatch) throws Exception { Tuple env = createEnv(fs, temp); Path pluginDir = createPluginDir(temp);