PageRenderTime 72ms CodeModel.GetById 47ms app.highlight 22ms RepoModel.GetById 1ms app.codeStats 0ms

/modules/elasticsearch/src/main/java/org/elasticsearch/plugins/PluginManager.java

https://github.com/tatemae/elasticsearch
Java | 285 lines | 240 code | 23 blank | 22 comment | 42 complexity | ac51845d17875141415d070c17c1891e MD5 | raw file
  1package org.elasticsearch.plugins;
  2
  3import org.elasticsearch.Version;
  4import org.elasticsearch.common.collect.Tuple;
  5import org.elasticsearch.common.http.client.HttpDownloadHelper;
  6import org.elasticsearch.common.io.FileSystemUtils;
  7import org.elasticsearch.common.io.Streams;
  8import org.elasticsearch.common.settings.Settings;
  9import org.elasticsearch.env.Environment;
 10import org.elasticsearch.node.internal.InternalSettingsPerparer;
 11
 12import javax.net.ssl.HttpsURLConnection;
 13import javax.net.ssl.SSLContext;
 14import javax.net.ssl.TrustManager;
 15import javax.net.ssl.X509TrustManager;
 16import java.io.File;
 17import java.io.FileOutputStream;
 18import java.io.IOException;
 19import java.net.URL;
 20import java.util.Enumeration;
 21import java.util.zip.ZipEntry;
 22import java.util.zip.ZipFile;
 23
 24import static org.elasticsearch.common.settings.ImmutableSettings.Builder.*;
 25
 26/**
 27 * @author kimchy (shay.banon)
 28 */
 29public class PluginManager {
 30
 31    private final Environment environment;
 32
 33    private String url;
 34
 35    public PluginManager(Environment environment, String url) {
 36        this.environment = environment;
 37        this.url = url;
 38
 39        TrustManager[] trustAllCerts = new TrustManager[]{
 40                new X509TrustManager() {
 41                    public java.security.cert.X509Certificate[] getAcceptedIssuers() {
 42                        return null;
 43                    }
 44
 45                    public void checkClientTrusted(
 46                            java.security.cert.X509Certificate[] certs, String authType) {
 47                    }
 48
 49                    public void checkServerTrusted(
 50                            java.security.cert.X509Certificate[] certs, String authType) {
 51                    }
 52                }
 53        };
 54
 55        // Install the all-trusting trust manager
 56        try {
 57            SSLContext sc = SSLContext.getInstance("SSL");
 58            sc.init(null, trustAllCerts, new java.security.SecureRandom());
 59            HttpsURLConnection.setDefaultSSLSocketFactory(sc.getSocketFactory());
 60        } catch (Exception e) {
 61            e.printStackTrace();
 62        }
 63    }
 64
 65    public void downloadAndExtract(String name) throws IOException {
 66        HttpDownloadHelper downloadHelper = new HttpDownloadHelper();
 67
 68        File pluginFile = new File(url + "/" + name + "/elasticsearch-" + name + "-" + Version.CURRENT.number() + ".zip");
 69        boolean downloaded = false;
 70        String filterZipName = null;
 71        if (!pluginFile.exists()) {
 72            pluginFile = new File(url + "/elasticsearch-" + name + "-" + Version.CURRENT.number() + ".zip");
 73            if (!pluginFile.exists()) {
 74                pluginFile = new File(environment.pluginsFile(), name + ".zip");
 75                if (url != null) {
 76                    URL pluginUrl = new URL(url);
 77                    System.out.println("Trying " + pluginUrl.toExternalForm() + "...");
 78                    try {
 79                        downloadHelper.download(pluginUrl, pluginFile, new HttpDownloadHelper.VerboseProgress(System.out));
 80                        downloaded = true;
 81                    } catch (IOException e) {
 82                        // ignore
 83                    }
 84                } else {
 85                    url = "http://elasticsearch.googlecode.com/svn/plugins";
 86                }
 87                if (!downloaded) {
 88                    if (name.indexOf('/') != -1) {
 89                        // github repo
 90                        String[] elements = name.split("/");
 91                        String userName = elements[0];
 92                        String repoName = elements[1];
 93                        String version = null;
 94                        if (elements.length > 2) {
 95                            version = elements[2];
 96                        }
 97                        filterZipName = userName + "-" + repoName;
 98                        // the installation file should not include the userName, just the repoName
 99                        name = repoName;
100                        if (name.startsWith("elasticsearch-")) {
101                            // remove elasticsearch- prefix
102                            name = name.substring("elasticsearch-".length());
103                        } else if (name.startsWith("es-")) {
104                            // remove es- prefix
105                            name = name.substring("es-".length());
106                        }
107                        pluginFile = new File(environment.pluginsFile(), name + ".zip");
108                        if (version == null) {
109                            // try with ES version from downloads
110                            URL pluginUrl = new URL("https://github.com/downloads/" + userName + "/" + repoName + "/" + repoName + "-" + Version.CURRENT.number() + ".zip");
111                            System.out.println("Trying " + pluginUrl.toExternalForm() + "...");
112                            try {
113                                downloadHelper.download(pluginUrl, pluginFile, new HttpDownloadHelper.VerboseProgress(System.out));
114                                downloaded = true;
115                            } catch (IOException e) {
116                                // try a tag with ES version
117                                pluginUrl = new URL("https://github.com/" + userName + "/" + repoName + "/zipball/v" + Version.CURRENT.number());
118                                System.out.println("Trying " + pluginUrl.toExternalForm() + "...");
119                                try {
120                                    downloadHelper.download(pluginUrl, pluginFile, new HttpDownloadHelper.VerboseProgress(System.out));
121                                    downloaded = true;
122                                } catch (IOException e1) {
123                                    // download master
124                                    pluginUrl = new URL("https://github.com/" + userName + "/" + repoName + "/zipball/master");
125                                    System.out.println("Trying " + pluginUrl.toExternalForm() + "...");
126                                    try {
127                                        downloadHelper.download(pluginUrl, pluginFile, new HttpDownloadHelper.VerboseProgress(System.out));
128                                        downloaded = true;
129                                    } catch (IOException e2) {
130                                        // ignore
131                                    }
132                                }
133                            }
134                        } else {
135                            // download explicit version
136                            URL pluginUrl = new URL("https://github.com/downloads/" + userName + "/" + repoName + "/" + repoName + "-" + version + ".zip");
137                            System.out.println("Trying " + pluginUrl.toExternalForm() + "...");
138                            try {
139                                downloadHelper.download(pluginUrl, pluginFile, new HttpDownloadHelper.VerboseProgress(System.out));
140                                downloaded = true;
141                            } catch (IOException e) {
142                                // try a tag with ES version
143                                pluginUrl = new URL("https://github.com/" + userName + "/" + repoName + "/zipball/v" + version);
144                                System.out.println("Trying " + pluginUrl.toExternalForm() + "...");
145                                try {
146                                    downloadHelper.download(pluginUrl, pluginFile, new HttpDownloadHelper.VerboseProgress(System.out));
147                                    downloaded = true;
148                                } catch (IOException e1) {
149                                    // ignore
150                                }
151                            }
152                        }
153                    } else {
154                        URL pluginUrl = new URL(url + "/" + name + "/elasticsearch-" + name + "-" + Version.CURRENT.number() + ".zip");
155                        System.out.println("Trying " + pluginUrl.toExternalForm() + "...");
156                        try {
157                            downloadHelper.download(pluginUrl, pluginFile, new HttpDownloadHelper.VerboseProgress(System.out));
158                            downloaded = true;
159                        } catch (IOException e) {
160                            // ignore
161                        }
162                    }
163                }
164            } else {
165                System.out.println("Using plugin from local fs: " + pluginFile.getAbsolutePath());
166                downloaded = true;
167            }
168        } else {
169            System.out.println("Using plugin from local fs: " + pluginFile.getAbsolutePath());
170            downloaded = true;
171        }
172
173        if (!downloaded) {
174            throw new IOException("failed to download");
175        }
176
177        // extract the plugin
178        File extractLocation = new File(environment.pluginsFile(), name);
179        ZipFile zipFile = null;
180        try {
181            zipFile = new ZipFile(pluginFile);
182            Enumeration<? extends ZipEntry> zipEntries = zipFile.entries();
183            while (zipEntries.hasMoreElements()) {
184                ZipEntry zipEntry = zipEntries.nextElement();
185                if (zipEntry.isDirectory()) {
186                    continue;
187                }
188                String zipName = zipEntry.getName().replace('\\', '/');
189                if (filterZipName != null) {
190                    if (zipName.startsWith(filterZipName)) {
191                        zipName = zipName.substring(zipName.indexOf('/'));
192                    }
193                }
194                File target = new File(extractLocation, zipName);
195                FileSystemUtils.mkdirs(target.getParentFile());
196                Streams.copy(zipFile.getInputStream(zipEntry), new FileOutputStream(target));
197            }
198        } catch (Exception e) {
199            System.err.println("failed to extract plugin [" + pluginFile + "]");
200        } finally {
201            if (zipFile != null) {
202                try {
203                    zipFile.close();
204                } catch (IOException e) {
205                    // ignore
206                }
207            }
208            pluginFile.delete();
209        }
210
211        // try and identify the plugin type, see if it has no .class or .jar files in it
212        // so its probably a _site, and it it does not have a _site in it, move everything to _site
213        if (!new File(extractLocation, "_site").exists()) {
214            if (!FileSystemUtils.hasExtensions(extractLocation, ".class", ".jar")) {
215                System.out.println("Identified as a _site plugin, moving to _site structure ...");
216                File site = new File(extractLocation, "_site");
217                File tmpLocation = new File(environment.pluginsFile(), name + ".tmp");
218                extractLocation.renameTo(tmpLocation);
219                FileSystemUtils.mkdirs(extractLocation);
220                tmpLocation.renameTo(site);
221            }
222        }
223
224        System.out.println("Installed " + name);
225    }
226
227    public void removePlugin(String name) throws IOException {
228        File pluginToDelete = new File(environment.pluginsFile(), name);
229        if (pluginToDelete.exists()) {
230            FileSystemUtils.deleteRecursively(pluginToDelete, true);
231        }
232        pluginToDelete = new File(environment.pluginsFile(), name + ".zip");
233        if (pluginToDelete.exists()) {
234            pluginToDelete.delete();
235        }
236    }
237
238    public static void main(String[] args) {
239        Tuple<Settings, Environment> initialSettings = InternalSettingsPerparer.prepareSettings(EMPTY_SETTINGS, true);
240
241        if (!initialSettings.v2().pluginsFile().exists()) {
242            FileSystemUtils.mkdirs(initialSettings.v2().pluginsFile());
243        }
244
245        String url = null;
246        for (int i = 0; i < args.length; i++) {
247            if ("url".equals(args[i]) || "-url".equals(args[i])) {
248                url = args[i + 1];
249                break;
250            }
251        }
252
253        PluginManager pluginManager = new PluginManager(initialSettings.v2(), url);
254
255        if (args.length < 1) {
256            System.out.println("Usage:");
257            System.out.println("    -url     [plugins location]  : Set URL to download plugins from");
258            System.out.println("    -install [plugin name]       : Downloads and installs listed plugins");
259            System.out.println("    -remove  [plugin name]       : Removes listed plugins");
260        }
261        for (int c = 0; c < args.length; c++) {
262            String command = args[c];
263            if (command.equals("install") || command.equals("-install")) {
264                String pluginName = args[++c];
265                System.out.println("-> Installing " + pluginName + "...");
266                try {
267                    pluginManager.downloadAndExtract(pluginName);
268                } catch (IOException e) {
269                    System.out.println("Failed to install " + pluginName + ", reason: " + e.getMessage());
270                }
271            } else if (command.equals("remove") || command.equals("-remove")) {
272                String pluginName = args[++c];
273                System.out.println("-> Removing " + pluginName + " ");
274                try {
275                    pluginManager.removePlugin(pluginName);
276                } catch (IOException e) {
277                    System.out.println("Failed to remove " + pluginName + ", reason: " + e.getMessage());
278                }
279            } else {
280                // not install or remove, continue
281                c++;
282            }
283        }
284    }
285}