From 3ae466c5feb8f05f5bbcaf1eb731d7f9f579d8a8 Mon Sep 17 00:00:00 2001 From: Konrad Windszus Date: Thu, 1 Jul 2021 10:38:06 +0200 Subject: [PATCH] Fallback from mirrors to Jenkins archive (#342) This closes #341 --- .../pluginmanager/impl/PluginManager.java | 66 +++++++++++-------- 1 file changed, 40 insertions(+), 26 deletions(-) diff --git a/plugin-management-library/src/main/java/io/jenkins/tools/pluginmanager/impl/PluginManager.java b/plugin-management-library/src/main/java/io/jenkins/tools/pluginmanager/impl/PluginManager.java index d12d40f0..8b0a4a13 100644 --- a/plugin-management-library/src/main/java/io/jenkins/tools/pluginmanager/impl/PluginManager.java +++ b/plugin-management-library/src/main/java/io/jenkins/tools/pluginmanager/impl/PluginManager.java @@ -109,6 +109,7 @@ public class PluginManager implements Closeable { private CacheManager cm; private static final int DEFAULT_MAX_RETRIES = 3; + private static final String MIRROR_FALLBACK_BASE_URL = "https://archives.jenkins.io/"; @SuppressFBWarnings(value = "PATH_TRAVERSAL_IN", justification = "we want the user to be able to specify a path") public PluginManager(Config cfg) { @@ -1256,14 +1257,19 @@ public boolean downloadToFile(String urlString, Plugin plugin, @CheckForNull Fil try { httpClient.execute(httpGet, new FileDownloadResponseHandler(pluginFile), context); } catch (IOException e) { - logVerbose(String.format("Unable to resolve plugin URL %s, or download plugin %s to file", - urlString, plugin.getName())); + String message = String.format("Unable to resolve plugin URL %s, or download plugin %s to file: %s", + urlString, plugin.getName(), e.getMessage()); + if (i >= maxRetries -1) { + System.out.println(message); + } else { + logVerbose(message); + } success = false; } finally { // get final URI (after all redirects) List locations = context.getRedirectLocations(); if (locations != null) { - String message = String.format("%s %s from %s", success ? "Downloaded" : "Tried downloading", plugin.getName(), locations.get(locations.size() - 1)); + String message = String.format("%s %s from %s (attempt %d of %d)", success ? "Downloaded" : "Tried downloading", plugin.getName(), locations.get(locations.size() - 1), i+1, maxRetries); if (success) { logVerbose(message); } else { @@ -1272,41 +1278,49 @@ public boolean downloadToFile(String urlString, Plugin plugin, @CheckForNull Fil } } - // Check integrity of plugin file + // Check if plugin is a proper ZIP file if (success) { try (JarFile ignored = new JarFile(pluginFile)) { plugin.setFile(pluginFile); + logVerbose("Downloaded and validated plugin " + plugin.getName()); + break; } catch (IOException e) { System.out.println("Downloaded file for " + plugin.getName() + " is not a valid ZIP"); + if (i >= maxRetries -1) { + if (verbose) { + e.printStackTrace(); + } + } success = false; } } - - // both the download and zip validation passed - if (success) { - logVerbose("Downloaded plugin " + plugin.getName()); - break; - } } - // Check integrity of plugin file - try (JarFile ignored = new JarFile(pluginFile)) { - verifyChecksum(plugin, pluginFile); - plugin.setFile(pluginFile); - } catch (IOException e) { - failedPlugins.add(plugin); - System.out.println("Downloaded file is not a valid ZIP"); - if (verbose) { - e.printStackTrace(); - } - return false; - } catch (PluginChecksumMismatchException e) { - failedPlugins.add(plugin); - System.out.println(e.getMessage()); - return false; + if (!success && !urlString.startsWith(MIRROR_FALLBACK_BASE_URL)) { + System.out.println("Downloading from mirrors failed, falling back to " + MIRROR_FALLBACK_BASE_URL); + // as fallback try to directly download from Jenkins server (only if mirrors fail) + urlString = appendPathOntoUrl(MIRROR_FALLBACK_BASE_URL, "/plugins", plugin.getName(), plugin.getVersion(), plugin.getName() + ".hpi"); + return downloadToFile(urlString, plugin, fileLocation, 1); } - if (!success) { + if (success) { + // Check integrity of plugin file + try (JarFile ignored = new JarFile(pluginFile)) { + verifyChecksum(plugin, pluginFile); + plugin.setFile(pluginFile); + } catch (IOException e) { + failedPlugins.add(plugin); + System.out.println("Downloaded file is not a valid ZIP"); + if (verbose) { + e.printStackTrace(); + } + return false; + } catch (PluginChecksumMismatchException e) { + failedPlugins.add(plugin); + System.out.println(e.getMessage()); + return false; + } + } else { failedPlugins.add(plugin); } return success;