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.

PluginUninstaller.java 5.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  1. /*
  2. * SonarQube
  3. * Copyright (C) 2009-2023 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;
  21. import java.io.File;
  22. import java.io.IOException;
  23. import java.util.Collection;
  24. import java.util.Collections;
  25. import java.util.HashSet;
  26. import java.util.Set;
  27. import java.util.stream.Collectors;
  28. import org.apache.commons.io.FileUtils;
  29. import org.sonar.api.Startable;
  30. import org.slf4j.Logger;
  31. import org.slf4j.LoggerFactory;
  32. import org.sonar.core.platform.PluginInfo;
  33. import org.sonar.server.platform.ServerFileSystem;
  34. import static java.lang.String.format;
  35. import static org.apache.commons.io.FileUtils.forceMkdir;
  36. import static org.apache.commons.io.FileUtils.moveFileToDirectory;
  37. import static org.sonar.core.plugin.PluginType.EXTERNAL;
  38. public class PluginUninstaller implements Startable {
  39. private static final Logger LOG = LoggerFactory.getLogger(PluginUninstaller.class);
  40. private static final String PLUGIN_EXTENSION = "jar";
  41. private final ServerFileSystem fs;
  42. private final ServerPluginRepository pluginRepository;
  43. public PluginUninstaller(ServerFileSystem fs, ServerPluginRepository pluginRepository) {
  44. this.fs = fs;
  45. this.pluginRepository = pluginRepository;
  46. }
  47. @Override
  48. public void start() {
  49. try {
  50. forceMkdir(fs.getUninstalledPluginsDir());
  51. } catch (IOException e) {
  52. throw new IllegalStateException("Fail to create the directory: " + fs.getUninstalledPluginsDir(), e);
  53. }
  54. }
  55. @Override
  56. public void stop() {
  57. // Nothing to do
  58. }
  59. /**
  60. * Uninstall a plugin and its dependents
  61. */
  62. public void uninstall(String pluginKey) {
  63. if (!pluginRepository.hasPlugin(pluginKey) || pluginRepository.getPlugin(pluginKey).getType() != EXTERNAL) {
  64. throw new IllegalArgumentException(format("Plugin [%s] is not installed", pluginKey));
  65. }
  66. Set<String> uninstallKeys = new HashSet<>();
  67. uninstallKeys.add(pluginKey);
  68. appendDependentPluginKeys(pluginKey, uninstallKeys);
  69. for (String uninstallKey : uninstallKeys) {
  70. PluginInfo info = pluginRepository.getPluginInfo(uninstallKey);
  71. // we don't check type because the dependent of an external plugin should never be a bundled plugin!
  72. uninstall(info.getKey(), info.getName(), info.getNonNullJarFile().getName());
  73. }
  74. }
  75. public void cancelUninstalls() {
  76. for (File file : listJarFiles(fs.getUninstalledPluginsDir())) {
  77. try {
  78. moveFileToDirectory(file, fs.getInstalledExternalPluginsDir(), false);
  79. } catch (IOException e) {
  80. throw new IllegalStateException("Fail to cancel plugin uninstalls", e);
  81. }
  82. }
  83. }
  84. /**
  85. * @return the list of plugins to be uninstalled as {@link PluginInfo} instances
  86. */
  87. public Collection<PluginInfo> getUninstalledPlugins() {
  88. return listJarFiles(fs.getUninstalledPluginsDir()).stream()
  89. .map(PluginInfo::create)
  90. .toList();
  91. }
  92. private static Collection<File> listJarFiles(File dir) {
  93. if (dir.exists()) {
  94. return FileUtils.listFiles(dir, new String[] {PLUGIN_EXTENSION}, false);
  95. }
  96. return Collections.emptyList();
  97. }
  98. private void uninstall(String key, String name, String fileName) {
  99. try {
  100. if (!getPluginFile(fileName).exists()) {
  101. LOG.info("Plugin already uninstalled: {} [{}]", name, key);
  102. return;
  103. }
  104. LOG.info("Uninstalling plugin {} [{}]", name, key);
  105. File masterFile = getPluginFile(fileName);
  106. moveFileToDirectory(masterFile, fs.getUninstalledPluginsDir(), true);
  107. } catch (IOException e) {
  108. throw new IllegalStateException(format("Fail to uninstall plugin %s [%s]", name, key), e);
  109. }
  110. }
  111. private File getPluginFile(String fileName) {
  112. // just to be sure that file is located in from extensions/plugins
  113. return new File(fs.getInstalledExternalPluginsDir(), fileName);
  114. }
  115. private void appendDependentPluginKeys(String pluginKey, Set<String> appendTo) {
  116. for (PluginInfo otherPlugin : pluginRepository.getPluginInfos()) {
  117. if (otherPlugin.getKey().equals(pluginKey)) {
  118. continue;
  119. }
  120. for (PluginInfo.RequiredPlugin requirement : otherPlugin.getRequiredPlugins()) {
  121. if (requirement.getKey().equals(pluginKey)) {
  122. appendTo.add(otherPlugin.getKey());
  123. appendDependentPluginKeys(otherPlugin.getKey(), appendTo);
  124. }
  125. }
  126. }
  127. }
  128. }