Skip to content

Commit

Permalink
Allowing custom folder name for plugin installation (opensearch-proje…
Browse files Browse the repository at this point in the history
…ct#848)

Signed-off-by: Vacha Shah <vachshah@amazon.com>
  • Loading branch information
VachaShah authored and nknize committed Aug 25, 2021
1 parent 1d59eaf commit 497bf27
Show file tree
Hide file tree
Showing 15 changed files with 269 additions and 75 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,7 @@ class PluginBuildPlugin implements Plugin<Project> {
'opensearchVersion' : Version.fromString(VersionProperties.getOpenSearch()).toString(),
'javaVersion' : project.targetCompatibility as String,
'classname' : extension1.classname,
'customFolderName' : extension1.customFolderName,
'extendedPlugins' : extension1.extendedPlugins.join(','),
'hasNativeController' : extension1.hasNativeController,
'requiresKeystore' : extension1.requiresKeystore
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,8 @@ public class PluginPropertiesExtension {

private String classname;

private String customFolderName = "";

/** Other plugins this plugin extends through SPI */
private List<String> extendedPlugins = new ArrayList<>();

Expand All @@ -76,6 +78,14 @@ public PluginPropertiesExtension(Project project) {
this.project = project;
}

public String getCustomFolderName() {
return customFolderName;
}

public void setCustomFolderName(String customFolderName) {
this.customFolderName = customFolderName;
}

public String getName() {
return name == null ? project.getName() : name;
}
Expand Down
3 changes: 3 additions & 0 deletions buildSrc/src/main/resources/plugin-descriptor.properties
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,9 @@ java.version=${javaVersion}
opensearch.version=${opensearchVersion}
### optional elements for plugins:
#
# 'custom.foldername': the custom name of the folder in which the plugin is installed.
custom.foldername=${customFolderName}
#
# 'extended.plugins': other plugins this plugin extends through SPI
extended.plugins=${extendedPlugins}
#
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -261,7 +261,7 @@ void execute(Terminal terminal, List<String> pluginIds, boolean isBatch, Environ
final Path extractedZip = unzip(pluginZip, env.pluginsFile());
deleteOnFailure.add(extractedZip);
final PluginInfo pluginInfo = installPlugin(terminal, isBatch, extractedZip, env, deleteOnFailure);
terminal.println("-> Installed " + pluginInfo.getName());
terminal.println("-> Installed " + pluginInfo.getName() + " with folder name " + pluginInfo.getTargetFolderName());
// swap the entry by plugin id for one with the installed plugin name, it gives a cleaner error message for URL installs
deleteOnFailures.remove(pluginId);
deleteOnFailures.put(pluginInfo.getName(), deleteOnFailure);
Expand Down Expand Up @@ -780,7 +780,9 @@ private void verifyPluginName(Path pluginPath, String pluginName) throws UserExc
throw new UserException(ExitCodes.USAGE, "plugin '" + pluginName + "' cannot be installed as a plugin, it is a system module");
}

final Path destination = pluginPath.resolve(pluginName);
// scan all the installed plugins to see if the plugin being installed already exists
// either with the plugin name or a custom folder name
Path destination = PluginHelper.verifyIfPluginExists(pluginPath, pluginName);
if (Files.exists(destination)) {
final String message = String.format(
Locale.ROOT,
Expand Down Expand Up @@ -864,14 +866,15 @@ private PluginInfo installPlugin(Terminal terminal, boolean isBatch, Path tmpRoo
}
PluginSecurity.confirmPolicyExceptions(terminal, permissions, isBatch);

final Path destination = env.pluginsFile().resolve(info.getName());
String targetFolderName = info.getTargetFolderName();
final Path destination = env.pluginsFile().resolve(targetFolderName);
deleteOnFailure.add(destination);

installPluginSupportFiles(
info,
tmpRoot,
env.binFile().resolve(info.getName()),
env.configFile().resolve(info.getName()),
env.binFile().resolve(targetFolderName),
env.configFile().resolve(targetFolderName),
deleteOnFailure
);
movePlugin(tmpRoot, destination);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -75,8 +75,8 @@ protected void execute(Terminal terminal, OptionSet options, Environment env) th
}

private void printPlugin(Environment env, Terminal terminal, Path plugin, String prefix) throws IOException {
terminal.println(Terminal.Verbosity.SILENT, prefix + plugin.getFileName().toString());
PluginInfo info = PluginInfo.readFromProperties(env.pluginsFile().resolve(plugin));
terminal.println(Terminal.Verbosity.SILENT, prefix + info.getName());
terminal.println(Terminal.Verbosity.VERBOSE, info.toString(prefix));
if (info.getOpenSearchVersion().equals(Version.CURRENT) == false) {
terminal.errorPrintln(
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
/*
* SPDX-License-Identifier: Apache-2.0
*
* The OpenSearch Contributors require contributions made to
* this file be licensed under the Apache-2.0 license or a
* compatible open source license.
*/

package org.opensearch.plugins;

import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.List;
import java.util.stream.Collectors;

/**
* A helper class for the plugin-cli tasks.
*/
public class PluginHelper {

/**
* Verify if a plugin exists with any folder name.
* @param pluginPath the path for the plugins directory.
* @param pluginName the name of the concerned plugin.
* @return the path of the folder if the plugin exists.
* @throws IOException if any I/O exception occurs while performing a file operation
*/
public static Path verifyIfPluginExists(Path pluginPath, String pluginName) throws IOException {
List<Path> pluginSubFolders = Files.walk(pluginPath, 1).filter(Files::isDirectory).collect(Collectors.toList());
for (Path customPluginFolderPath : pluginSubFolders) {
if (customPluginFolderPath != pluginPath
&& !((customPluginFolderPath.getFileName().toString()).contains(".installing"))
&& !((customPluginFolderPath.getFileName().toString()).contains(".removing"))) {
final PluginInfo info = PluginInfo.readFromProperties(customPluginFolderPath);
if (info.getName().equals(pluginName)) {
return customPluginFolderPath;
}
}
}
return pluginPath.resolve(pluginName);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -114,9 +114,20 @@ void execute(Terminal terminal, Environment env, String pluginName, boolean purg
);
}

final Path pluginDir = env.pluginsFile().resolve(pluginName);
final Path pluginConfigDir = env.configFile().resolve(pluginName);
final Path removing = env.pluginsFile().resolve(".removing-" + pluginName);
Path pluginDir = env.pluginsFile().resolve(pluginName);
Path pluginConfigDir = env.configFile().resolve(pluginName);
Path removing = env.pluginsFile().resolve(".removing-" + pluginName);

/*
* If the plugin directory is not found with the plugin name, scan the list of all installed plugins
* to find if the concerned plugin is installed with a custom folder name.
*/
if (!Files.exists(pluginDir)) {
terminal.println("searching in other folders to find if plugin exists with custom folder name");
pluginDir = PluginHelper.verifyIfPluginExists(env.pluginsFile(), pluginName);
pluginConfigDir = env.configFile().resolve(pluginDir.getFileName());
removing = env.pluginsFile().resolve(".removing-" + pluginDir.getFileName());
}

terminal.println("-> removing [" + pluginName + "]...");
/*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -576,6 +576,17 @@ public void testExistingPlugin() throws Exception {
assertInstallCleaned(env.v2());
}

public void testExistingPluginWithCustomFolderName() throws Exception {
Tuple<Path, Environment> env = createEnv(fs, temp);
Path pluginDir = createPluginDir(temp);
String pluginZip = createPluginUrl("fake", pluginDir, "custom.foldername", "fake-folder");
installPlugin(pluginZip, env.v1());
assertPlugin("fake-folder", pluginDir, env.v2());
UserException e = expectThrows(UserException.class, () -> installPlugin(pluginZip, env.v1()));
assertTrue(e.getMessage(), e.getMessage().contains("already exists"));
assertInstallCleaned(env.v2());
}

public void testBin() throws Exception {
Tuple<Path, Environment> env = createEnv(fs, temp);
Path pluginDir = createPluginDir(temp);
Expand Down Expand Up @@ -873,6 +884,14 @@ public void testPluginAlreadyInstalled() throws Exception {
);
}

public void testPluginInstallationWithCustomFolderName() throws Exception {
Tuple<Path, Environment> env = createEnv(fs, temp);
Path pluginDir = createPluginDir(temp);
String pluginZip = createPluginUrl("fake", pluginDir, "custom.foldername", "fake-folder");
installPlugin(pluginZip, env.v1());
assertPlugin("fake-folder", pluginDir, env.v2());
}

private void installPlugin(MockTerminal terminal, boolean isBatch) throws Exception {
Tuple<Path, Environment> env = createEnv(fs, temp);
Path pluginDir = createPluginDir(temp);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,8 @@ private static void buildFakePlugin(
"1.8",
"classname",
classname,
"custom.foldername",
"custom-folder",
"has.native.controller",
Boolean.toString(hasNativeController)
);
Expand Down Expand Up @@ -169,7 +171,8 @@ public void testPluginWithVerbose() throws Exception {
"Java Version: 1.8",
"Native Controller: false",
"Extended Plugins: []",
" * Classname: org.fake"
" * Classname: org.fake",
"Folder name: custom-folder"
),
terminal.getOutput()
);
Expand All @@ -191,7 +194,8 @@ public void testPluginWithNativeController() throws Exception {
"Java Version: 1.8",
"Native Controller: true",
"Extended Plugins: []",
" * Classname: org.fake"
" * Classname: org.fake",
"Folder name: custom-folder"
),
terminal.getOutput()
);
Expand All @@ -215,6 +219,7 @@ public void testPluginWithVerboseMultiplePlugins() throws Exception {
"Native Controller: false",
"Extended Plugins: []",
" * Classname: org.fake",
"Folder name: custom-folder",
"fake_plugin2",
"- Plugin information:",
"Name: fake_plugin2",
Expand All @@ -224,7 +229,8 @@ public void testPluginWithVerboseMultiplePlugins() throws Exception {
"Java Version: 1.8",
"Native Controller: false",
"Extended Plugins: []",
" * Classname: org.fake2"
" * Classname: org.fake2",
"Folder name: custom-folder"
),
terminal.getOutput()
);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,9 @@
import java.nio.file.DirectoryStream;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.Arrays;
import java.util.Map;
import java.util.stream.Stream;

import static org.hamcrest.CoreMatchers.containsString;
import static org.hamcrest.CoreMatchers.not;
Expand Down Expand Up @@ -90,34 +92,34 @@ public void setUp() throws Exception {
env = TestEnvironment.newEnvironment(settings);
}

void createPlugin(String name) throws IOException {
createPlugin(env.pluginsFile(), name);
void createPlugin(String name, String... additionalProps) throws IOException {
createPlugin(env.pluginsFile(), name, Version.CURRENT, additionalProps);
}

void createPlugin(String name, Version version) throws IOException {
createPlugin(env.pluginsFile(), name, version);
}

void createPlugin(Path path, String name) throws IOException {
createPlugin(path, name, Version.CURRENT);
}

void createPlugin(Path path, String name, Version version) throws IOException {
PluginTestUtil.writePluginProperties(
path.resolve(name),
"description",
"dummy",
"name",
name,
"version",
"1.0",
"opensearch.version",
version.toString(),
"java.version",
System.getProperty("java.specification.version"),
"classname",
"SomeClass"
);
void createPlugin(Path path, String name, Version version, String... additionalProps) throws IOException {
String[] properties = Stream.concat(
Stream.of(
"description",
"dummy",
"name",
name,
"version",
"1.0",
"opensearch.version",
version.toString(),
"java.version",
System.getProperty("java.specification.version"),
"classname",
"SomeClass"
),
Arrays.stream(additionalProps)
).toArray(String[]::new);
String pluginFolderName = additionalProps.length == 0 ? name : additionalProps[1];
PluginTestUtil.writePluginProperties(path.resolve(pluginFolderName), properties);
}

static MockTerminal removePlugin(String name, Path home, boolean purge) throws Exception {
Expand Down Expand Up @@ -154,6 +156,17 @@ public void testBasic() throws Exception {
assertRemoveCleaned(env);
}

public void testRemovePluginWithCustomFolderName() throws Exception {
createPlugin("fake", "custom.foldername", "custom-folder");
Files.createFile(env.pluginsFile().resolve("custom-folder").resolve("plugin.jar"));
Files.createDirectory(env.pluginsFile().resolve("custom-folder").resolve("subdir"));
createPlugin("other");
removePlugin("fake", home, randomBoolean());
assertFalse(Files.exists(env.pluginsFile().resolve("custom-folder")));
assertTrue(Files.exists(env.pluginsFile().resolve("other")));
assertRemoveCleaned(env);
}

public void testRemoveOldVersion() throws Exception {
createPlugin(
"fake",
Expand Down Expand Up @@ -261,6 +274,7 @@ protected boolean addShutdownHook() {
BufferedReader reader = new BufferedReader(new StringReader(terminal.getOutput()));
BufferedReader errorReader = new BufferedReader(new StringReader(terminal.getErrorOutput()))
) {
assertEquals("searching in other folders to find if plugin exists with custom folder name", reader.readLine());
assertEquals("-> removing [fake]...", reader.readLine());
assertEquals(
"ERROR: plugin [fake] not found; run 'opensearch-plugin list' to get list of installed plugins",
Expand Down
Loading

0 comments on commit 497bf27

Please sign in to comment.