Nelze vybrat více než 25 témat Téma musí začínat písmenem nebo číslem, může obsahovat pomlčky („-“) a může být dlouhé až 35 znaků.

ServerPluginJarsInstaller.java 10KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286
  1. /*
  2. * SonarQube, open source software quality management tool.
  3. * Copyright (C) 2008-2014 SonarSource
  4. * mailto:contact AT sonarsource DOT com
  5. *
  6. * SonarQube is free software; you can redistribute it and/or
  7. * modify it under the terms of the GNU Lesser General Public
  8. * License as published by the Free Software Foundation; either
  9. * version 3 of the License, or (at your option) any later version.
  10. *
  11. * SonarQube is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  14. * Lesser General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU Lesser General Public License
  17. * along with this program; if not, write to the Free Software Foundation,
  18. * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
  19. */
  20. package org.sonar.server.plugins;
  21. import com.google.common.base.Function;
  22. import com.google.common.base.Joiner;
  23. import com.google.common.collect.Maps;
  24. import org.sonar.api.platform.PluginMetadata;
  25. import org.sonar.api.platform.Server;
  26. import org.sonar.api.platform.ServerUpgradeStatus;
  27. import org.sonar.api.utils.MessageException;
  28. import org.sonar.api.utils.log.Logger;
  29. import org.sonar.api.utils.log.Loggers;
  30. import org.sonar.api.utils.log.Profiler;
  31. import org.sonar.core.plugins.DefaultPluginMetadata;
  32. import org.sonar.server.platform.DefaultServerFileSystem;
  33. import org.sonar.updatecenter.common.PluginReferential;
  34. import javax.annotation.Nonnull;
  35. import java.io.File;
  36. import java.io.IOException;
  37. import java.util.Arrays;
  38. import java.util.Collection;
  39. import java.util.Collections;
  40. import java.util.HashSet;
  41. import java.util.List;
  42. import java.util.Map;
  43. import java.util.Set;
  44. import static com.google.common.collect.Iterables.transform;
  45. import static com.google.common.collect.Lists.newArrayList;
  46. import static java.lang.String.format;
  47. import static org.apache.commons.io.FileUtils.cleanDirectory;
  48. import static org.apache.commons.io.FileUtils.copyFile;
  49. import static org.apache.commons.io.FileUtils.deleteDirectory;
  50. import static org.apache.commons.io.FileUtils.deleteQuietly;
  51. import static org.apache.commons.io.FileUtils.forceMkdir;
  52. import static org.apache.commons.io.FileUtils.listFiles;
  53. import static org.apache.commons.io.FileUtils.moveFile;
  54. import static org.apache.commons.io.FileUtils.moveFileToDirectory;
  55. import static org.apache.commons.lang.StringUtils.isNotBlank;
  56. public class ServerPluginJarsInstaller {
  57. private static final Logger LOG = Loggers.get(ServerPluginJarsInstaller.class);
  58. private static final String FILE_EXTENSION_JAR = "jar";
  59. private static final Joiner SLASH_JOINER = Joiner.on(" / ").skipNulls();
  60. private final Server server;
  61. private final DefaultServerFileSystem fs;
  62. private final ServerPluginJarInstaller installer;
  63. private final Map<String, PluginMetadata> pluginByKeys = Maps.newHashMap();
  64. private final ServerUpgradeStatus serverUpgradeStatus;
  65. private static final Set<String> BLACKLISTED_PLUGINS = new HashSet<String>(Arrays.asList("scmactivity", "issuesreport"));
  66. public ServerPluginJarsInstaller(Server server, ServerUpgradeStatus serverUpgradeStatus,
  67. DefaultServerFileSystem fs, ServerPluginJarInstaller installer) {
  68. this.server = server;
  69. this.serverUpgradeStatus = serverUpgradeStatus;
  70. this.fs = fs;
  71. this.installer = installer;
  72. }
  73. public void install() {
  74. Profiler profiler = Profiler.create(LOG).startInfo("Install plugins");
  75. deleteTrash();
  76. loadInstalledPlugins();
  77. copyBundledPlugins();
  78. moveDownloadedPlugins();
  79. loadCorePlugins();
  80. deployPlugins();
  81. profiler.stopDebug();
  82. }
  83. private void deleteTrash() {
  84. File trashDir = fs.getTrashPluginsDir();
  85. try {
  86. if (trashDir.exists()) {
  87. deleteDirectory(trashDir);
  88. }
  89. } catch (IOException e) {
  90. throw new IllegalStateException("Fail to clean the plugin trash directory: " + trashDir, e);
  91. }
  92. }
  93. private void loadInstalledPlugins() {
  94. for (File file : fs.getUserPlugins()) {
  95. PluginMetadata metadata = installer.fileToPlugin().apply(file);
  96. if (isNotBlank(metadata.getKey())) {
  97. loadInstalledPlugin(metadata);
  98. }
  99. }
  100. }
  101. private void loadInstalledPlugin(PluginMetadata metadata) {
  102. if (BLACKLISTED_PLUGINS.contains(metadata.getKey())) {
  103. LOG.warn("Plugin {} is blacklisted. Please uninstall it.", metadata.getName());
  104. } else {
  105. PluginMetadata existing = pluginByKeys.put(metadata.getKey(), metadata);
  106. if (existing != null) {
  107. throw MessageException.of(format("Found two files for the same plugin '%s': %s and %s",
  108. metadata.getKey(), metadata.getFile().getName(), existing.getFile().getName()));
  109. }
  110. }
  111. }
  112. private void moveDownloadedPlugins() {
  113. if (fs.getDownloadedPluginsDir().exists()) {
  114. for (File sourceFile : listJarFiles(fs.getDownloadedPluginsDir())) {
  115. overridePlugin(sourceFile, true);
  116. }
  117. }
  118. }
  119. private void copyBundledPlugins() {
  120. if (serverUpgradeStatus.isFreshInstall()) {
  121. for (File sourceFile : fs.getBundledPlugins()) {
  122. PluginMetadata metadata = installer.fileToPlugin().apply(sourceFile);
  123. // lib/bundled-plugins should be copied only if the plugin is not already
  124. // available in extensions/plugins
  125. if (!pluginByKeys.containsKey(metadata.getKey())) {
  126. overridePlugin(sourceFile, false);
  127. }
  128. }
  129. }
  130. }
  131. private void overridePlugin(File sourceFile, boolean deleteSource) {
  132. File destDir = fs.getUserPluginsDir();
  133. File destFile = new File(destDir, sourceFile.getName());
  134. if (destFile.exists()) {
  135. // plugin with same filename already installed
  136. deleteQuietly(destFile);
  137. }
  138. try {
  139. if (deleteSource) {
  140. moveFile(sourceFile, destFile);
  141. } else {
  142. copyFile(sourceFile, destFile, true);
  143. }
  144. } catch (IOException e) {
  145. LOG.error(format("Fail to move or copy plugin: %s to %s",
  146. sourceFile.getAbsolutePath(), destFile.getAbsolutePath()), e);
  147. }
  148. PluginMetadata metadata = installer.fileToPlugin().apply(destFile);
  149. if (isNotBlank(metadata.getKey())) {
  150. PluginMetadata existing = pluginByKeys.put(metadata.getKey(), metadata);
  151. if (existing != null) {
  152. if (!existing.getFile().getName().equals(destFile.getName())) {
  153. deleteQuietly(existing.getFile());
  154. }
  155. LOG.info("Plugin " + metadata.getKey() + " replaced by new version");
  156. }
  157. }
  158. }
  159. private void loadCorePlugins() {
  160. for (File file : fs.getCorePlugins()) {
  161. PluginMetadata metadata = installer.fileToCorePlugin().apply(file);
  162. PluginMetadata existing = pluginByKeys.put(metadata.getKey(), metadata);
  163. if (existing != null) {
  164. throw new IllegalStateException("Found two plugins with the same key '" + metadata.getKey() + "': " + metadata.getFile().getName() + " and "
  165. + existing.getFile().getName());
  166. }
  167. }
  168. }
  169. public void uninstall(String pluginKey) {
  170. for (String key : getPluginReferential().findLastReleasesWithDependencies(pluginKey)) {
  171. uninstallPlugin(key);
  172. }
  173. }
  174. private void uninstallPlugin(String pluginKey) {
  175. PluginMetadata metadata = pluginByKeys.get(pluginKey);
  176. if (metadata != null && !metadata.isCore()) {
  177. try {
  178. File masterFile = new File(fs.getUserPluginsDir(), metadata.getFile().getName());
  179. moveFileToDirectory(masterFile, fs.getTrashPluginsDir(), true);
  180. } catch (IOException e) {
  181. throw new IllegalStateException("Fail to uninstall plugin: " + pluginKey, e);
  182. }
  183. }
  184. }
  185. public List<String> getUninstalledPluginFilenames() {
  186. if (!fs.getTrashPluginsDir().exists()) {
  187. return Collections.emptyList();
  188. }
  189. return newArrayList(transform(listJarFiles(fs.getTrashPluginsDir()), FileToName.INSTANCE));
  190. }
  191. /**
  192. * @return the list of plugins to be uninstalled as {@link DefaultPluginMetadata} instances
  193. */
  194. public Collection<DefaultPluginMetadata> getUninstalledPlugins() {
  195. if (!fs.getTrashPluginsDir().exists()) {
  196. return Collections.emptyList();
  197. }
  198. return newArrayList(transform(listJarFiles(fs.getTrashPluginsDir()), installer.fileToPlugin()));
  199. }
  200. public void cancelUninstalls() {
  201. if (fs.getTrashPluginsDir().exists()) {
  202. for (File file : listJarFiles(fs.getTrashPluginsDir())) {
  203. try {
  204. moveFileToDirectory(file, fs.getUserPluginsDir(), false);
  205. } catch (IOException e) {
  206. throw new IllegalStateException("Fail to cancel plugin uninstalls", e);
  207. }
  208. }
  209. }
  210. }
  211. private void deployPlugins() {
  212. for (PluginMetadata metadata : pluginByKeys.values()) {
  213. deploy((DefaultPluginMetadata) metadata);
  214. }
  215. }
  216. private void deploy(DefaultPluginMetadata plugin) {
  217. LOG.info("Deploy plugin {}", SLASH_JOINER.join(plugin.getName(), plugin.getVersion(), plugin.getImplementationBuild()));
  218. if (!plugin.isCompatibleWith(server.getVersion())) {
  219. throw MessageException.of(format(
  220. "Plugin %s needs a more recent version of SonarQube than %s. At least %s is expected",
  221. plugin.getKey(), server.getVersion(), plugin.getSonarVersion()));
  222. }
  223. try {
  224. File pluginDeployDir = new File(fs.getDeployedPluginsDir(), plugin.getKey());
  225. forceMkdir(pluginDeployDir);
  226. cleanDirectory(pluginDeployDir);
  227. installer.installToDir(plugin, pluginDeployDir);
  228. } catch (IOException e) {
  229. throw new IllegalStateException("Fail to deploy the plugin " + plugin, e);
  230. }
  231. }
  232. public Collection<PluginMetadata> getMetadata() {
  233. return pluginByKeys.values();
  234. }
  235. public PluginMetadata getMetadata(String pluginKey) {
  236. return pluginByKeys.get(pluginKey);
  237. }
  238. private PluginReferential getPluginReferential() {
  239. return PluginReferentialMetadataConverter.getInstalledPluginReferential(getMetadata());
  240. }
  241. private static Collection<File> listJarFiles(File pluginDir) {
  242. return listFiles(pluginDir, new String[] {FILE_EXTENSION_JAR}, false);
  243. }
  244. private enum FileToName implements Function<File, String> {
  245. INSTANCE;
  246. @Override
  247. public String apply(@Nonnull File file) {
  248. return file.getName();
  249. }
  250. }
  251. }