-
Notifications
You must be signed in to change notification settings - Fork 24.3k
/
PluginManager.java
143 lines (127 loc) · 5.79 KB
/
PluginManager.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
package org.elasticsearch.plugins;
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.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.*;
/**
* @author kimchy (shay.banon)
*/
public class PluginManager {
private final Environment environment;
private final String url;
public PluginManager(Environment environment, String url) {
this.environment = environment;
this.url = url;
}
public void downloadPlugin(String name) throws IOException {
HttpDownloadHelper downloadHelper = new HttpDownloadHelper();
File pluginFile = new File(url + "/" + name + "/elasticsearch-" + name + "-" + Version.number() + ".zip");
if (!pluginFile.exists()) {
pluginFile = new File(url + "/elasticsearch-" + name + "-" + Version.number() + ".zip");
if (!pluginFile.exists()) {
URL pluginUrl = new URL(url + "/" + name + "/elasticsearch-" + name + "-" + Version.number() + ".zip");
System.out.println("Downloading plugin from " + pluginUrl.toExternalForm());
pluginFile = new File(environment.pluginsFile(), name + ".zip");
downloadHelper.download(pluginUrl, pluginFile, new HttpDownloadHelper.VerboseProgress(System.out));
} else {
System.out.println("Using plugin from local fs: " + pluginFile.getAbsolutePath());
}
} else {
System.out.println("Using plugin from local fs: " + pluginFile.getAbsolutePath());
}
// 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);
FileSystemUtils.mkdirs(target.getParentFile());
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);
if (pluginToDelete.exists()) {
FileSystemUtils.deleteRecursively(pluginToDelete, true);
}
pluginToDelete = new File(environment.pluginsFile(), name + ".zip");
if (pluginToDelete.exists()) {
pluginToDelete.delete();
}
}
public static void main(String[] args) {
Tuple<Settings, Environment> initialSettings = InternalSettingsPerparer.prepareSettings(EMPTY_SETTINGS, true);
if (!initialSettings.v2().pluginsFile().exists()) {
FileSystemUtils.mkdirs(initialSettings.v2().pluginsFile());
}
String url = "http://elasticsearch.googlecode.com/svn/plugins";
for (int i = 0; i < args.length; i++) {
if ("url".equals(args[i]) || "-url".equals(args[i])) {
url = args[i + 1];
break;
}
}
PluginManager pluginManager = new PluginManager(initialSettings.v2(), url);
if (args.length < 1) {
System.out.println("Usage:");
System.out.println(" -url [plugins location] : Set URL to download plugins from");
System.out.println(" -install [plugin name] : Downloads and installs listed plugins");
System.out.println(" -remove [plugin name] : Removes listed plugins");
}
for (int c = 0; c < args.length; c++) {
String command = args[c];
if (command.equals("install") || command.equals("-install")) {
String pluginName = args[++c];
System.out.print("-> Installing " + pluginName + " ");
try {
pluginManager.downloadPlugin(pluginName);
System.out.println(" DONE");
} catch (IOException e) {
System.out.println("Failed to install " + pluginName + ", reason: " + e.getMessage());
}
} else if (command.equals("remove") || command.equals("-remove")) {
String pluginName = args[++c];
System.out.println("-> Removing " + pluginName + " ");
try {
pluginManager.removePlugin(pluginName);
} catch (IOException e) {
System.out.println("Failed to remove " + pluginName + ", reason: " + e.getMessage());
}
} else {
// not install or remove, continue
c++;
}
}
}
}