Plugins: plugins should now be in extracted format under `ES_HOME/plugins`, closes #438.

This commit is contained in:
kimchy 2010-10-18 11:14:16 +02:00
parent 0a3d187e6a
commit 019c844dd1
2 changed files with 55 additions and 115 deletions

View File

@ -4,14 +4,18 @@ import org.elasticsearch.Version;
import org.elasticsearch.common.collect.Tuple;
import org.elasticsearch.common.http.client.HttpDownloadHelper;
import org.elasticsearch.common.io.FileSystemUtils;
import org.elasticsearch.common.io.Streams;
import org.elasticsearch.common.settings.Settings;
import org.elasticsearch.env.Environment;
import org.elasticsearch.node.internal.InternalSettingsPerparer;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.net.URL;
import java.util.Enumeration;
import java.util.zip.ZipEntry;
import java.util.zip.ZipFile;
import static org.elasticsearch.common.settings.ImmutableSettings.Builder.*;
@ -33,16 +37,48 @@ public class PluginManager {
HttpDownloadHelper downloadHelper = new HttpDownloadHelper();
URL pluginUrl = new URL(url + "/" + name + "/elasticsearch-" + name + "-" + Version.number() + ".zip");
downloadHelper.download(pluginUrl, new File(environment.pluginsFile(), name + ".zip"), new HttpDownloadHelper.VerboseProgress(System.out));
File pluginFile = new File(environment.pluginsFile(), name + ".zip");
downloadHelper.download(pluginUrl, pluginFile, new HttpDownloadHelper.VerboseProgress(System.out));
// extract the plugin
File extractLocation = new File(environment.pluginsFile(), name);
ZipFile zipFile = null;
try {
zipFile = new ZipFile(pluginFile);
Enumeration<? extends ZipEntry> zipEntries = zipFile.entries();
while (zipEntries.hasMoreElements()) {
ZipEntry zipEntry = zipEntries.nextElement();
if (!(zipEntry.getName().endsWith(".jar") || zipEntry.getName().endsWith(".zip"))) {
continue;
}
String zipName = zipEntry.getName().replace('\\', '/');
File target = new File(extractLocation, zipName);
target.getParentFile().mkdirs();
Streams.copy(zipFile.getInputStream(zipEntry), new FileOutputStream(target));
}
} catch (Exception e) {
System.err.println("failed to extract plugin [" + pluginFile + "]");
} finally {
if (zipFile != null) {
try {
zipFile.close();
} catch (IOException e) {
// ignore
}
}
pluginFile.delete();
}
}
public void removePlugin(String name) throws IOException {
File pluginToDelete = new File(environment.pluginsFile(), name + ".zip");
if (!pluginToDelete.exists()) {
throw new FileNotFoundException("Plugin [" + name + "] does not exists");
File pluginToDelete = new File(environment.pluginsFile(), name);
if (pluginToDelete.exists()) {
FileSystemUtils.deleteRecursively(pluginToDelete, true);
}
pluginToDelete = new File(environment.pluginsFile(), name + ".zip");
if (pluginToDelete.exists()) {
pluginToDelete.delete();
}
pluginToDelete.delete();
FileSystemUtils.deleteRecursively(new File(new File(environment.workFile(), "plugins"), name), true);
}
public static void main(String[] args) {

View File

@ -27,20 +27,18 @@ import org.elasticsearch.common.component.AbstractComponent;
import org.elasticsearch.common.component.LifecycleComponent;
import org.elasticsearch.common.inject.Inject;
import org.elasticsearch.common.inject.Module;
import org.elasticsearch.common.io.Streams;
import org.elasticsearch.common.settings.Settings;
import org.elasticsearch.env.Environment;
import org.elasticsearch.index.CloseableIndexComponent;
import java.io.*;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.lang.reflect.Method;
import java.net.URL;
import java.util.*;
import java.util.zip.ZipEntry;
import java.util.zip.ZipFile;
import static org.elasticsearch.common.collect.Maps.*;
import static org.elasticsearch.common.io.FileSystemUtils.*;
/**
* @author kimchy (shay.banon)
@ -163,115 +161,21 @@ public class PluginsService extends AbstractComponent {
File[] pluginsFiles = pluginsFile.listFiles();
for (File pluginFile : pluginsFiles) {
if (!pluginFile.getName().endsWith(".zip")) {
if (pluginFile.isDirectory()) {
logger.trace("--- adding expanded plugin [" + pluginFile.getAbsolutePath() + "]");
try {
// add the root
addURL.invoke(classLoader, pluginFile.toURI().toURL());
// if there are jars in it, add it as well
for (File jarToAdd : pluginFile.listFiles()) {
if (!(jarToAdd.getName().endsWith(".jar") || jarToAdd.getName().endsWith(".zip"))) {
continue;
}
addURL.invoke(classLoader, jarToAdd.toURI().toURL());
}
} catch (Exception e) {
logger.warn("failed to add plugin [" + pluginFile + "]", e);
}
}
continue;
}
if (logger.isTraceEnabled()) {
logger.trace("processing [{}]", pluginFile);
}
String pluginNameNoExtension = pluginFile.getName().substring(0, pluginFile.getName().lastIndexOf('.'));
File extractedPluginDir = new File(new File(environment.workFile(), "plugins"), pluginNameNoExtension);
extractedPluginDir.mkdirs();
File stampsDir = new File(new File(environment.workFile(), "plugins"), "_stamps");
stampsDir.mkdirs();
boolean extractPlugin = true;
File stampFile = new File(stampsDir, pluginNameNoExtension + ".stamp");
if (stampFile.exists()) {
// read it, and check if its the same size as the pluginFile
RandomAccessFile raf = null;
if (pluginFile.isDirectory()) {
logger.trace("--- adding plugin [" + pluginFile.getAbsolutePath() + "]");
try {
raf = new RandomAccessFile(stampFile, "r");
long size = raf.readLong();
if (size == pluginFile.length()) {
extractPlugin = false;
if (logger.isTraceEnabled()) {
logger.trace("--- no need to extract plugin, same size [" + size + "]");
}
}
} catch (Exception e) {
// ignore and extract the plugin
} finally {
if (raf != null) {
try {
raf.close();
} catch (IOException e) {
// ignore
}
}
}
}
if (extractPlugin) {
if (logger.isTraceEnabled()) {
logger.trace("--- extracting plugin to [" + extractedPluginDir + "]");
}
deleteRecursively(extractedPluginDir, false);
ZipFile zipFile = null;
try {
zipFile = new ZipFile(pluginFile);
Enumeration<? extends ZipEntry> zipEntries = zipFile.entries();
while (zipEntries.hasMoreElements()) {
ZipEntry zipEntry = zipEntries.nextElement();
if (!(zipEntry.getName().endsWith(".jar") || zipEntry.getName().endsWith(".zip"))) {
// add the root
addURL.invoke(classLoader, pluginFile.toURI().toURL());
// if there are jars in it, add it as well
for (File jarToAdd : pluginFile.listFiles()) {
if (!(jarToAdd.getName().endsWith(".jar") || jarToAdd.getName().endsWith(".zip"))) {
continue;
}
String name = zipEntry.getName().replace('\\', '/');
File target = new File(extractedPluginDir, name);
Streams.copy(zipFile.getInputStream(zipEntry), new FileOutputStream(target));
addURL.invoke(classLoader, jarToAdd.toURI().toURL());
}
} catch (Exception e) {
logger.warn("failed to extract plugin [" + pluginFile + "], ignoring...", e);
continue;
} finally {
if (zipFile != null) {
try {
zipFile.close();
} catch (IOException e) {
// ignore
}
}
logger.warn("failed to add plugin [" + pluginFile + "]", e);
}
try {
RandomAccessFile raf = new RandomAccessFile(stampFile, "rw");
raf.writeLong(pluginFile.length());
raf.close();
} catch (Exception e) {
// ignore
}
}
try {
for (File jarToAdd : extractedPluginDir.listFiles()) {
if (!(jarToAdd.getName().endsWith(".jar") || jarToAdd.getName().endsWith(".zip"))) {
continue;
}
addURL.invoke(classLoader, jarToAdd.toURI().toURL());
}
} catch (Exception e) {
logger.warn("failed to add plugin [" + pluginFile + "]", e);
}
}
}