Skip to content

Commit

Permalink
Rename PluginInfo to PluginDescriptor (#86950) (#86987)
Browse files Browse the repository at this point in the history
The class PluginInfo represents the plugin-descriptor.properties file
that each plugin must have. This commit renames the class to more
closely match what it represents: the plugin descriptor.
  • Loading branch information
rjernst authored May 20, 2022
1 parent 8f7daf3 commit 412a80c
Show file tree
Hide file tree
Showing 31 changed files with 365 additions and 336 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@
import org.elasticsearch.env.Environment;
import org.elasticsearch.jdk.JarHell;
import org.elasticsearch.plugins.Platforms;
import org.elasticsearch.plugins.PluginInfo;
import org.elasticsearch.plugins.PluginDescriptor;
import org.elasticsearch.plugins.PluginsService;

import java.io.BufferedReader;
Expand Down Expand Up @@ -99,7 +99,7 @@
* </ul>
* <p>
* Plugins are packaged as zip files. Each packaged plugin must contain a plugin properties file.
* See {@link PluginInfo}.
* See {@link PluginDescriptor}.
* <p>
* The installation process first extracts the plugin files into a temporary
* directory in order to verify the plugin satisfies the following requirements:
Expand Down Expand Up @@ -205,13 +205,13 @@ public void setProxy(Proxy proxy) {
}

// pkg private for testing
public void execute(List<PluginDescriptor> plugins) throws Exception {
public void execute(List<InstallablePlugin> plugins) throws Exception {
if (plugins.isEmpty()) {
throw new UserException(ExitCodes.USAGE, "at least one plugin id is required");
}

final Set<String> uniquePluginIds = new HashSet<>();
for (final PluginDescriptor plugin : plugins) {
for (final InstallablePlugin plugin : plugins) {
if (uniquePluginIds.add(plugin.getId()) == false) {
throw new UserException(ExitCodes.USAGE, "duplicate plugin id [" + plugin.getId() + "]");
}
Expand All @@ -220,7 +220,7 @@ public void execute(List<PluginDescriptor> plugins) throws Exception {
final String logPrefix = terminal.isHeadless() ? "" : "-> ";

final Map<String, List<Path>> deleteOnFailures = new LinkedHashMap<>();
for (final PluginDescriptor plugin : plugins) {
for (final InstallablePlugin plugin : plugins) {
final String pluginId = plugin.getId();
terminal.println(logPrefix + "Installing " + pluginId);
try {
Expand All @@ -243,11 +243,11 @@ public void execute(List<PluginDescriptor> plugins) throws Exception {
final Path pluginZip = download(plugin, env.tmpFile());
final Path extractedZip = unzip(pluginZip, env.pluginsFile());
deleteOnFailure.add(extractedZip);
final PluginInfo pluginInfo = installPlugin(plugin, extractedZip, deleteOnFailure);
terminal.println(logPrefix + "Installed " + pluginInfo.getName());
final PluginDescriptor pluginDescriptor = installPlugin(plugin, extractedZip, deleteOnFailure);
terminal.println(logPrefix + "Installed " + pluginDescriptor.getName());
// 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);
deleteOnFailures.put(pluginDescriptor.getName(), deleteOnFailure);
} catch (final Exception installProblem) {
terminal.println(logPrefix + "Failed installing " + pluginId);
for (final Map.Entry<String, List<Path>> deleteOnFailureEntry : deleteOnFailures.entrySet()) {
Expand Down Expand Up @@ -294,7 +294,7 @@ private static void handleInstallXPack(final Build.Flavor flavor) throws UserExc
/**
* Downloads the plugin and returns the file it was downloaded to.
*/
private Path download(PluginDescriptor plugin, Path tmpDir) throws Exception {
private Path download(InstallablePlugin plugin, Path tmpDir) throws Exception {
final String pluginId = plugin.getId();

final String logPrefix = terminal.isHeadless() ? "" : "-> ";
Expand Down Expand Up @@ -861,8 +861,8 @@ private void verifyPluginName(Path pluginPath, String pluginName) throws UserExc
/**
* Load information about the plugin, and verify it can be installed with no errors.
*/
private PluginInfo loadPluginInfo(Path pluginRoot) throws Exception {
final PluginInfo info = PluginInfo.readFromProperties(pluginRoot);
private PluginDescriptor loadPluginInfo(Path pluginRoot) throws Exception {
final PluginDescriptor info = PluginDescriptor.readFromProperties(pluginRoot);
if (info.hasNativeController()) {
throw new IllegalStateException("plugins can not have native controllers");
}
Expand Down Expand Up @@ -890,7 +890,7 @@ private PluginInfo loadPluginInfo(Path pluginRoot) throws Exception {
/**
* check a candidate plugin for jar hell before installing it
*/
void jarHellCheck(PluginInfo candidateInfo, Path candidateDir, Path pluginsDir, Path modulesDir) throws Exception {
void jarHellCheck(PluginDescriptor candidateInfo, Path candidateDir, Path pluginsDir, Path modulesDir) throws Exception {
// create list of current jars in classpath
final Set<URL> classpath = JarHell.parseClassPath().stream().filter(url -> {
try {
Expand Down Expand Up @@ -921,8 +921,8 @@ void jarHellCheck(PluginInfo candidateInfo, Path candidateDir, Path pluginsDir,
* Installs the plugin from {@code tmpRoot} into the plugins dir.
* If the plugin has a bin dir and/or a config dir, those are moved.
*/
private PluginInfo installPlugin(PluginDescriptor descriptor, Path tmpRoot, List<Path> deleteOnFailure) throws Exception {
final PluginInfo info = loadPluginInfo(tmpRoot);
private PluginDescriptor installPlugin(InstallablePlugin descriptor, Path tmpRoot, List<Path> deleteOnFailure) throws Exception {
final PluginDescriptor info = loadPluginInfo(tmpRoot);
checkCanInstallationProceed(terminal, Build.CURRENT.flavor(), info);
PluginPolicyInfo pluginPolicy = PolicyUtil.getPluginPolicyInfo(tmpRoot, env.tmpFile());
if (pluginPolicy != null) {
Expand Down Expand Up @@ -957,8 +957,13 @@ private PluginInfo installPlugin(PluginDescriptor descriptor, Path tmpRoot, List
/**
* Moves bin and config directories from the plugin if they exist
*/
private void installPluginSupportFiles(PluginInfo info, Path tmpRoot, Path destBinDir, Path destConfigDir, List<Path> deleteOnFailure)
throws Exception {
private void installPluginSupportFiles(
PluginDescriptor info,
Path tmpRoot,
Path destBinDir,
Path destConfigDir,
List<Path> deleteOnFailure
) throws Exception {
Path tmpBinDir = tmpRoot.resolve("bin");
if (Files.exists(tmpBinDir)) {
deleteOnFailure.add(destBinDir);
Expand Down Expand Up @@ -1003,7 +1008,7 @@ public FileVisitResult postVisitDirectory(final Path dir, final IOException exc)
/**
* Copies the files from {@code tmpBinDir} into {@code destBinDir}, along with permissions from dest dirs parent.
*/
private void installBin(PluginInfo info, Path tmpBinDir, Path destBinDir) throws Exception {
private void installBin(PluginDescriptor info, Path tmpBinDir, Path destBinDir) throws Exception {
if (Files.isDirectory(tmpBinDir) == false) {
throw new UserException(PLUGIN_MALFORMED, "bin in plugin " + info.getName() + " is not a directory");
}
Expand Down Expand Up @@ -1031,7 +1036,7 @@ private void installBin(PluginInfo info, Path tmpBinDir, Path destBinDir) throws
* Copies the files from {@code tmpConfigDir} into {@code destConfigDir}.
* Any files existing in both the source and destination will be skipped.
*/
private void installConfig(PluginInfo info, Path tmpConfigDir, Path destConfigDir) throws Exception {
private void installConfig(PluginDescriptor info, Path tmpConfigDir, Path destConfigDir) throws Exception {
if (Files.isDirectory(tmpConfigDir) == false) {
throw new UserException(PLUGIN_MALFORMED, "config in plugin " + info.getName() + " is not a directory");
}
Expand Down Expand Up @@ -1093,7 +1098,7 @@ public void close() throws IOException {
IOUtils.rm(pathsToDeleteOnShutdown.toArray(new Path[0]));
}

public static void checkCanInstallationProceed(Terminal terminal, Build.Flavor flavor, PluginInfo info) throws Exception {
public static void checkCanInstallationProceed(Terminal terminal, Build.Flavor flavor, PluginDescriptor info) throws Exception {
if (info.isLicensed() == false) {
return;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
import org.elasticsearch.cli.Terminal;
import org.elasticsearch.common.cli.EnvironmentAwareCommand;
import org.elasticsearch.env.Environment;
import org.elasticsearch.plugins.PluginInfo;
import org.elasticsearch.plugins.PluginDescriptor;

import java.util.Arrays;
import java.util.List;
Expand All @@ -31,7 +31,7 @@
* </ul>
* <p>
* Plugins are packaged as zip files. Each packaged plugin must contain a plugin properties file.
* See {@link PluginInfo}.
* See {@link PluginDescriptor}.
* <p>
* The installation process first extracts the plugin files into a temporary
* directory in order to verify the plugin satisfies the following requirements:
Expand Down Expand Up @@ -77,10 +77,10 @@ protected void printAdditionalHelp(Terminal terminal) {
protected void execute(Terminal terminal, OptionSet options, Environment env) throws Exception {
SyncPluginsAction.ensureNoConfigFile(env);

List<PluginDescriptor> plugins = arguments.values(options)
List<InstallablePlugin> plugins = arguments.values(options)
.stream()
// We only have one piece of data, which could be an ID or could be a location, so we use it for both
.map(idOrLocation -> new PluginDescriptor(idOrLocation, idOrLocation))
.map(idOrLocation -> new InstallablePlugin(idOrLocation, idOrLocation))
.collect(Collectors.toList());
final boolean isBatch = options.has(batchOption);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,11 @@
/**
* Models a single plugin that can be installed.
*/
public class PluginDescriptor {
public class InstallablePlugin {
private String id;
private String location;

public PluginDescriptor() {}
public InstallablePlugin() {}

/**
* Creates a new descriptor instance.
Expand All @@ -28,12 +28,12 @@ public PluginDescriptor() {}
* @param location the location from which to fetch the plugin, e.g. a URL or Maven
* coordinates. Can be null for official plugins.
*/
public PluginDescriptor(String id, String location) {
public InstallablePlugin(String id, String location) {
this.id = Strings.requireNonBlank(id, "plugin id cannot be null or blank");
this.location = location;
}

public PluginDescriptor(String id) {
public InstallablePlugin(String id) {
this(id, null);
}

Expand All @@ -57,7 +57,7 @@ public void setLocation(String location) {
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
PluginDescriptor that = (PluginDescriptor) o;
InstallablePlugin that = (InstallablePlugin) o;
return id.equals(that.id) && Objects.equals(location, that.location);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
import org.elasticsearch.cli.Terminal;
import org.elasticsearch.common.cli.EnvironmentAwareCommand;
import org.elasticsearch.env.Environment;
import org.elasticsearch.plugins.PluginInfo;
import org.elasticsearch.plugins.PluginDescriptor;

import java.io.IOException;
import java.nio.file.DirectoryStream;
Expand Down Expand Up @@ -58,7 +58,7 @@ 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));
PluginDescriptor info = PluginDescriptor.readFromProperties(env.pluginsFile().resolve(plugin));
terminal.println(Terminal.Verbosity.VERBOSE, info.toString(prefix));
if (info.getElasticsearchVersion().equals(Version.CURRENT) == false) {
terminal.errorPrintln(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,15 +33,15 @@
* Elasticsearch plugin.
*/
public class PluginsConfig {
private List<PluginDescriptor> plugins;
private List<InstallablePlugin> plugins;
private String proxy;

public PluginsConfig() {
plugins = List.of();
proxy = null;
}

public void setPlugins(List<PluginDescriptor> plugins) {
public void setPlugins(List<InstallablePlugin> plugins) {
this.plugins = plugins == null ? List.of() : plugins;
}

Expand All @@ -52,7 +52,7 @@ public void setProxy(String proxy) {
/**
* Validate this instance. For example:
* <ul>
* <li>All {@link PluginDescriptor}s must have IDs</li>
* <li>All {@link InstallablePlugin}s must have IDs</li>
* <li>Any proxy must be well-formed.</li>
* <li>Unofficial plugins must have locations</li>
* </ul>
Expand All @@ -68,13 +68,13 @@ public void validate(Set<String> officialPlugins, Set<String> migratedPlugins) t
}

final Set<String> uniquePluginIds = new HashSet<>();
for (final PluginDescriptor plugin : plugins) {
for (final InstallablePlugin plugin : plugins) {
if (uniquePluginIds.add(plugin.getId()) == false) {
throw new PluginSyncException("Duplicate plugin ID [" + plugin.getId() + "] found in [elasticsearch-plugins.yml]");
}
}

for (PluginDescriptor plugin : this.plugins) {
for (InstallablePlugin plugin : this.plugins) {
if (officialPlugins.contains(plugin.getId()) == false
&& migratedPlugins.contains(plugin.getId()) == false
&& plugin.getLocation() == null) {
Expand All @@ -95,7 +95,7 @@ public void validate(Set<String> officialPlugins, Set<String> migratedPlugins) t
}
}

for (PluginDescriptor p : plugins) {
for (InstallablePlugin p : plugins) {
if (p.getLocation() != null) {
if (p.getLocation().isBlank()) {
throw new PluginSyncException("Empty location for plugin [" + p.getId() + "]");
Expand All @@ -111,7 +111,7 @@ public void validate(Set<String> officialPlugins, Set<String> migratedPlugins) t
}
}

public List<PluginDescriptor> getPlugins() {
public List<InstallablePlugin> getPlugins() {
return plugins;
}

Expand Down Expand Up @@ -152,9 +152,9 @@ static PluginsConfig parseConfig(Path configPath, XContent xContent) throws IOEx
// Normally a parser is declared and built statically in the class, but we'll only
// use this when starting up Elasticsearch, so there's no point keeping one around.

final ObjectParser<PluginDescriptor, Void> descriptorParser = new ObjectParser<>("descriptor parser", PluginDescriptor::new);
descriptorParser.declareString(PluginDescriptor::setId, new ParseField("id"));
descriptorParser.declareStringOrNull(PluginDescriptor::setLocation, new ParseField("location"));
final ObjectParser<InstallablePlugin, Void> descriptorParser = new ObjectParser<>("descriptor parser", InstallablePlugin::new);
descriptorParser.declareString(InstallablePlugin::setId, new ParseField("id"));
descriptorParser.declareStringOrNull(InstallablePlugin::setLocation, new ParseField("location"));

final ObjectParser<PluginsConfig, Void> parser = new ObjectParser<>("plugins parser", PluginsConfig::new);
parser.declareStringOrNull(PluginsConfig::setProxy, new ParseField("proxy"));
Expand All @@ -181,7 +181,7 @@ static void writeConfig(XContent xContent, PluginsConfig config, Path configPath

builder.startObject();
builder.startArray("plugins");
for (PluginDescriptor p : config.getPlugins()) {
for (InstallablePlugin p : config.getPlugins()) {
builder.startObject();
{
builder.field("id", p.getId());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -70,29 +70,29 @@ public void setPurge(boolean purge) {
* @throws UserException if plugin directory does not exist
* @throws UserException if the plugin bin directory is not a directory
*/
public void execute(List<PluginDescriptor> plugins) throws IOException, UserException {
public void execute(List<InstallablePlugin> plugins) throws IOException, UserException {
if (plugins == null || plugins.isEmpty()) {
throw new UserException(ExitCodes.USAGE, "At least one plugin ID is required");
}

ensurePluginsNotUsedByOtherPlugins(plugins);

for (PluginDescriptor plugin : plugins) {
for (InstallablePlugin plugin : plugins) {
checkCanRemove(plugin);
}

for (PluginDescriptor plugin : plugins) {
for (InstallablePlugin plugin : plugins) {
removePlugin(plugin);
}
}

private void ensurePluginsNotUsedByOtherPlugins(List<PluginDescriptor> plugins) throws IOException, UserException {
private void ensurePluginsNotUsedByOtherPlugins(List<InstallablePlugin> plugins) throws IOException, UserException {
// First make sure nothing extends this plugin
final Map<String, List<String>> usedBy = new HashMap<>();
Set<PluginsService.Bundle> bundles = PluginsService.getPluginBundles(env.pluginsFile());
for (PluginsService.Bundle bundle : bundles) {
for (String extendedPlugin : bundle.plugin.getExtendedPlugins()) {
for (PluginDescriptor plugin : plugins) {
for (InstallablePlugin plugin : plugins) {
String pluginId = plugin.getId();
if (extendedPlugin.equals(pluginId)) {
usedBy.computeIfAbsent(bundle.plugin.getName(), (_key -> new ArrayList<>())).add(pluginId);
Expand All @@ -114,7 +114,7 @@ private void ensurePluginsNotUsedByOtherPlugins(List<PluginDescriptor> plugins)
throw new UserException(PLUGIN_STILL_USED, message.toString());
}

private void checkCanRemove(PluginDescriptor plugin) throws UserException {
private void checkCanRemove(InstallablePlugin plugin) throws UserException {
String pluginId = plugin.getId();

final Path pluginDir = env.pluginsFile().resolve(pluginId);
Expand Down Expand Up @@ -151,7 +151,7 @@ private void checkCanRemove(PluginDescriptor plugin) throws UserException {
}
}

private void removePlugin(PluginDescriptor plugin) throws IOException {
private void removePlugin(InstallablePlugin plugin) throws IOException {
final String pluginId = plugin.getId();
final Path pluginDir = env.pluginsFile().resolve(pluginId);
final Path pluginConfigDir = env.configFile().resolve(pluginId);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ class RemovePluginCommand extends EnvironmentAwareCommand {
protected void execute(final Terminal terminal, final OptionSet options, final Environment env) throws Exception {
SyncPluginsAction.ensureNoConfigFile(env);

final List<PluginDescriptor> plugins = arguments.values(options).stream().map(PluginDescriptor::new).collect(Collectors.toList());
final List<InstallablePlugin> plugins = arguments.values(options).stream().map(InstallablePlugin::new).collect(Collectors.toList());

final RemovePluginAction action = new RemovePluginAction(terminal, env, options.has(purgeOption));
action.execute(plugins);
Expand Down
Loading

0 comments on commit 412a80c

Please sign in to comment.