You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

EditionInstaller.java 6.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  1. /*
  2. * SonarQube
  3. * Copyright (C) 2009-2018 SonarSource SA
  4. * mailto:info AT sonarsource DOT com
  5. *
  6. * This program 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. * This program 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.edition;
  21. import com.google.common.base.Optional;
  22. import java.util.Collection;
  23. import java.util.Collections;
  24. import java.util.Map;
  25. import java.util.Set;
  26. import java.util.concurrent.Semaphore;
  27. import java.util.stream.Collectors;
  28. import org.sonar.api.utils.log.Logger;
  29. import org.sonar.api.utils.log.Loggers;
  30. import org.sonar.core.platform.PluginInfo;
  31. import org.sonar.server.edition.License;
  32. import org.sonar.server.edition.MutableEditionManagementState;
  33. import org.sonar.server.plugins.ServerPluginRepository;
  34. import org.sonar.server.plugins.UpdateCenterMatrixFactory;
  35. import org.sonar.updatecenter.common.UpdateCenter;
  36. public class EditionInstaller {
  37. private static final Logger LOG = Loggers.get(EditionInstaller.class);
  38. private final Semaphore semaphore = new Semaphore(1);
  39. private final EditionInstallerExecutor executor;
  40. private final EditionPluginDownloader editionPluginDownloader;
  41. private final EditionPluginUninstaller editionPluginUninstaller;
  42. private final ServerPluginRepository pluginRepository;
  43. private final UpdateCenterMatrixFactory updateCenterMatrixFactory;
  44. private final MutableEditionManagementState editionManagementState;
  45. public EditionInstaller(EditionPluginDownloader editionDownloader, EditionPluginUninstaller editionPluginUninstaller,
  46. ServerPluginRepository pluginRepository, EditionInstallerExecutor executor, UpdateCenterMatrixFactory updateCenterMatrixFactory,
  47. MutableEditionManagementState editionManagementState) {
  48. this.editionPluginDownloader = editionDownloader;
  49. this.editionPluginUninstaller = editionPluginUninstaller;
  50. this.pluginRepository = pluginRepository;
  51. this.executor = executor;
  52. this.updateCenterMatrixFactory = updateCenterMatrixFactory;
  53. this.editionManagementState = editionManagementState;
  54. }
  55. /**
  56. * Refreshes the update center, and submits in a executor a task to download all the needed plugins (asynchronously).
  57. * If the update center is disabled or if we are offline, the task is not submitted.
  58. *
  59. * @throws IllegalStateException if an installation is already in progress
  60. */
  61. public void install(License newLicense) {
  62. if (semaphore.tryAcquire()) {
  63. try {
  64. Optional<UpdateCenter> updateCenter = updateCenterMatrixFactory.getUpdateCenter(true);
  65. if (!updateCenter.isPresent()) {
  66. LOG.info("Installation of edition '{}' needs to be done manually", newLicense.getEditionKey());
  67. editionManagementState.startManualInstall(newLicense);
  68. return;
  69. }
  70. editionManagementState.startAutomaticInstall(newLicense);
  71. executor.execute(() -> asyncInstall(newLicense, updateCenter.get()));
  72. } catch (RuntimeException e) {
  73. semaphore.release();
  74. throw e;
  75. }
  76. } else {
  77. throw new IllegalStateException("Another installation of an edition is already running");
  78. }
  79. }
  80. public void uninstall() {
  81. Map<String, PluginInfo> pluginInfosByKeys = pluginRepository.getPluginInfosByKeys();
  82. Set<String> pluginsToRemove = pluginsToRemove(Collections.emptySet(), pluginInfosByKeys.values());
  83. uninstallPlugins(pluginsToRemove);
  84. }
  85. /**
  86. * Check if the update center is disabled or unreachable. It uses the cached status (it doesn't refresh),
  87. * to be a cost-free check.
  88. */
  89. public boolean isOffline() {
  90. return !updateCenterMatrixFactory.getUpdateCenter(false).isPresent();
  91. }
  92. public boolean requiresInstallationChange(Set<String> editionPluginKeys) {
  93. Map<String, PluginInfo> pluginInfosByKeys = pluginRepository.getPluginInfosByKeys();
  94. return !pluginsToInstall(editionPluginKeys, pluginInfosByKeys.keySet()).isEmpty()
  95. || !pluginsToRemove(editionPluginKeys, pluginInfosByKeys.values()).isEmpty();
  96. }
  97. private void asyncInstall(License newLicense, UpdateCenter updateCenter) {
  98. try {
  99. Set<String> editionPluginKeys = newLicense.getPluginKeys();
  100. Map<String, PluginInfo> pluginInfosByKeys = pluginRepository.getPluginInfosByKeys();
  101. Set<String> pluginsToRemove = pluginsToRemove(editionPluginKeys, pluginInfosByKeys.values());
  102. Set<String> pluginsToInstall = pluginsToInstall(editionPluginKeys, pluginInfosByKeys.keySet());
  103. LOG.info("Installing edition '{}', download: {}, remove: {}",
  104. newLicense.getEditionKey(), pluginsToInstall, pluginsToRemove);
  105. editionPluginDownloader.downloadEditionPlugins(pluginsToInstall, updateCenter);
  106. uninstallPlugins(pluginsToRemove);
  107. editionManagementState.automaticInstallReady();
  108. } catch (Throwable t) {
  109. LOG.error("Failed to install edition {} with plugins {}", newLicense.getEditionKey(), newLicense.getPluginKeys(), t);
  110. editionManagementState.installFailed(t.getMessage());
  111. } finally {
  112. semaphore.release();
  113. }
  114. }
  115. private void uninstallPlugins(Set<String> pluginsToRemove) {
  116. pluginsToRemove.stream().forEach(editionPluginUninstaller::uninstall);
  117. }
  118. private static Set<String> pluginsToInstall(Set<String> editionPluginKeys, Set<String> installedPluginKeys) {
  119. return editionPluginKeys.stream()
  120. .filter(p -> !installedPluginKeys.contains(p))
  121. .collect(Collectors.toSet());
  122. }
  123. private static Set<String> pluginsToRemove(Set<String> editionPluginKeys, Collection<PluginInfo> installedPluginInfos) {
  124. Set<String> installedCommercialPluginKeys = installedPluginInfos.stream()
  125. .filter(EditionBundledPlugins::isEditionBundled)
  126. .map(PluginInfo::getKey)
  127. .collect(Collectors.toSet());
  128. return installedCommercialPluginKeys.stream()
  129. .filter(p -> !editionPluginKeys.contains(p))
  130. .collect(Collectors.toSet());
  131. }
  132. }