/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

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