Title: [40758] trunk/hudson/plugins/thinBackup: restore plugins from the update server

Diff

Modified: trunk/hudson/plugins/thinBackup/pom.xml (40757 => 40758)


--- trunk/hudson/plugins/thinBackup/pom.xml	2012-11-03 00:21:49 UTC (rev 40757)
+++ trunk/hudson/plugins/thinBackup/pom.xml	2012-11-08 20:17:14 UTC (rev 40758)
@@ -42,8 +42,6 @@
       <id>tofuatjava</id>
       <name>Thomas Fuerer</name>
       <email>tfuerer.java...@gmail.com</email>
-      <organization>Borland (a Microfocus Company)</organization>
-      <organizationUrl>www.borland.com</organizationUrl>
       <timezone>CET</timezone>
     </developer>
     <developer>
@@ -60,10 +58,6 @@
     <developerConnection>scm:svn:https://svn.jenkins-ci.org/trunk/hudson/plugins/thinBackup</developerConnection>
     <url>http://fisheye.jenkins-ci.org/browse/Hudson/trunk/hudson/plugins/thinBackup</url>
   </scm>
-  <organization>
-    <name>Borland (a Microfocus Company)</name>
-    <url>www.borland.com</url>
-  </organization>
   <repositories>
    <repository>
       <id>repo.jenkins-ci.org</id>
@@ -89,11 +83,16 @@
   <dependencies>
     <dependency>
       <groupId>org.mockito</groupId>
-      <artifactId>mockito-all</artifactId>
-      <version>1.9.0-rc1</version>
-      <type>jar</type>
+      <artifactId>mockito-core</artifactId>
+      <version>1.9.5</version>
       <scope>test</scope>
     </dependency>
+    <dependency>
+      <groupId>junit</groupId>
+      <artifactId>junit</artifactId>
+      <version>4.10</version>
+      <scope>test</scope>
+    </dependency>
   </dependencies>
   <distributionManagement>
     <repository>

Modified: trunk/hudson/plugins/thinBackup/src/main/java/org/jvnet/hudson/plugins/thinbackup/backup/HudsonBackup.java (40757 => 40758)


--- trunk/hudson/plugins/thinBackup/src/main/java/org/jvnet/hudson/plugins/thinbackup/backup/HudsonBackup.java	2012-11-03 00:21:49 UTC (rev 40757)
+++ trunk/hudson/plugins/thinBackup/src/main/java/org/jvnet/hudson/plugins/thinbackup/backup/HudsonBackup.java	2012-11-08 20:17:14 UTC (rev 40758)
@@ -16,7 +16,6 @@
  */
 package org.jvnet.hudson.plugins.thinbackup.backup;
 
-import hudson.Extension;
 import hudson.PluginWrapper;
 import hudson.model.Hudson;
 

Modified: trunk/hudson/plugins/thinBackup/src/main/java/org/jvnet/hudson/plugins/thinbackup/restore/HudsonRestore.java (40757 => 40758)


--- trunk/hudson/plugins/thinBackup/src/main/java/org/jvnet/hudson/plugins/thinbackup/restore/HudsonRestore.java	2012-11-03 00:21:49 UTC (rev 40757)
+++ trunk/hudson/plugins/thinBackup/src/main/java/org/jvnet/hudson/plugins/thinbackup/restore/HudsonRestore.java	2012-11-08 20:17:14 UTC (rev 40758)
@@ -19,6 +19,7 @@
 import hudson.PluginManager;
 import hudson.model.Hudson;
 import hudson.model.UpdateCenter;
+import hudson.model.UpdateCenter.InstallationJob;
 import hudson.model.UpdateCenter.UpdateCenterJob;
 import hudson.model.UpdateSite;
 import hudson.model.UpdateSite.Plugin;
@@ -31,7 +32,6 @@
 import java.io.FilenameFilter;
 import java.io.IOException;
 import java.io.Writer;
-import java.lang.reflect.Field;
 import java.util.ArrayList;
 import java.util.Collection;
 import java.util.Date;
@@ -72,8 +72,8 @@
     this.backupPath = backupPath;
     this.restoreFromDate = restoreFromDate;
     this.restoreNextBuildNumber = restoreNextBuildNumber;
-    // this.restorePlugins = restorePlugins;
-    this.restorePlugins = false;
+    this.restorePlugins = restorePlugins;
+    //this.restorePlugins = false;
 
     this.availablePluginLocations = new HashMap<String, List<Plugin>>();
   }
@@ -208,7 +208,7 @@
 
     if (list.length != 1) {
       LOGGER
-          .severe("Cannot restore plugins because no or mulible files with the name 'installedPlugins.xml' are in the backup.");
+          .severe("Cannot restore plugins because no or mulitble files with the name 'installedPlugins.xml' are in the backup.");
       return;
     }
 
@@ -274,9 +274,13 @@
           if (plugin.name.equals(pluginID)) {
             LOGGER.info("Restore plugin '" + pluginID + "'.");
             if (!plugin.version.equals(version)) {
-              setVersion(plugin, version);
-            }
-            return plugin.deploy();
+              Hudson.getInstance().checkPermission(Hudson.ADMINISTER);
+              UpdateCenter uc = Hudson.getInstance().getUpdateCenter();
+              PluginRestoreUpdateCenter pruc = new PluginRestoreUpdateCenter();
+              
+              return pruc.addJob(pruc.new PluginRestoreJob(site, Hudson.getAuthentication(), plugin, version));
+            } else
+              return plugin.deploy();
           }
         }
       }
@@ -285,35 +289,4 @@
         .warning("Cannot find plugin '" + pluginID + "' with the version '" + version + "'. Please install manually!");
     return null;
   }
-
-  /**
-   * manipulate existing plugin object to downgrade to a specific version.
-   * 
-   * @param plugin
-   * @param version
-   */
-  private void setVersion(Plugin plugin, String version) {
-    Class clazz = Plugin.class;
-    try {
-      Field versionField = clazz.getField("version");
-      versionField.setAccessible(true);
-      versionField.set(plugin.version, version);
-
-      String[] url = ""
-      url[url.length - 2] = version;
-      StringBuilder newUrl = new StringBuilder();
-      for (String s : url) {
-        newUrl.append(s);
-        newUrl.append("/");
-      }
-      newUrl.replace(newUrl.length() - 1, newUrl.length(), "");
-
-      Field urlField = clazz.getField("url");
-      urlField.setAccessible(true);
-      urlField.set(plugin.url, newUrl.toString());
-
-    } catch (Throwable e) {
-      LOGGER.log(Level.SEVERE, "Cannot downgrade plugin [" + plugin.name + "]. Please install manually.", e);
-    }
-  }
 }

Added: trunk/hudson/plugins/thinBackup/src/main/java/org/jvnet/hudson/plugins/thinbackup/restore/PluginRestoreUpdateCenter.java (0 => 40758)


--- trunk/hudson/plugins/thinBackup/src/main/java/org/jvnet/hudson/plugins/thinbackup/restore/PluginRestoreUpdateCenter.java	                        (rev 0)
+++ trunk/hudson/plugins/thinBackup/src/main/java/org/jvnet/hudson/plugins/thinbackup/restore/PluginRestoreUpdateCenter.java	2012-11-08 20:17:14 UTC (rev 40758)
@@ -0,0 +1,105 @@
+/**
+ *  Copyright (C) 2011  Matthias Steinkogler, Thomas Fürer
+ *
+ *  This program is free software: you can redistribute it and/or modify
+ *  it under the terms of the GNU General Public License as published by
+ *  the Free Software Foundation, either version 3 of the License, or
+ *  (at your option) any later version.
+ *
+ *  This program is distributed in the hope that it will be useful,
+ *  but WITHOUT ANY WARRANTY; without even the implied warranty of
+ *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ *  GNU General Public License for more details
+ *
+ *  You should have received a copy of the GNU General Public License
+ *  along with this program.  If not, see http://www.gnu.org/licenses.
+ */
+package org.jvnet.hudson.plugins.thinbackup.restore;
+
+import hudson.PluginManager;
+import hudson.PluginWrapper;
+import hudson.model.Hudson;
+import hudson.model.UpdateCenter;
+import hudson.model.UpdateSite;
+import hudson.model.UpdateSite.Plugin;
+import hudson.security.ACL;
+
+import java.io.File;
+import java.io.IOException;
+import java.net.MalformedURLException;
+import java.net.URL;
+import java.util.HashSet;
+import java.util.Set;
+import java.util.concurrent.Future;
+
+import org.acegisecurity.Authentication;
+import org.acegisecurity.context.SecurityContextHolder;
+
+public class PluginRestoreUpdateCenter extends UpdateCenter {
+  public class PluginRestoreJob extends DownloadJob {
+  
+    private Plugin plugin;
+    private String version;
+  
+    private final PluginManager pm;
+  
+    public PluginRestoreJob(UpdateSite site, Authentication auth, Plugin plugin, String version) {
+      super(site, auth);
+      this.plugin = plugin;
+      this.version = version;
+  
+      this.pm = Hudson.getInstance().getPluginManager();
+    }
+  
+    @Override
+    protected URL getURL() throws MalformedURLException {
+      String latestVersion = plugin.version;
+      String newUrl = plugin.url.replace(latestVersion, version);
+      return new URL(newUrl);
+    }
+  
+    @Override
+    protected File getDestination() {
+      return new File(pm.rootDir, plugin.name + ".hpi");
+    }
+  
+    @Override
+    public String getName() {
+      return plugin.getDisplayName();
+    }
+  
+    @Override
+    protected void onSuccess() {
+      pm.pluginUploaded = true;
+    }
+  
+    @Override
+    public String toString() {
+      return super.toString() + "[plugin=" + plugin.title + "]";
+    }
+  
+    @Override
+    protected void _run() throws IOException {
+      super._run();
+  
+      PluginWrapper pw = plugin.getInstalled();
+      if (pw != null && pw.isBundled())
+        try {
+          SecurityContextHolder.getContext().setAuthentication(ACL.SYSTEM);
+          pw.doPin();
+        } finally {
+          SecurityContextHolder.clearContext();
+        }
+    }
+  
+  }
+
+  private static final UpdateCenter ORIGINAL_UPDATECENTER = Hudson.getInstance().getUpdateCenter();  
+  private Set<UpdateSite> knownUpdateSites = new HashSet<UpdateSite>();
+
+  synchronized Future<UpdateCenterJob> addJob(UpdateCenterJob job) {
+    if (knownUpdateSites.add(job.site))
+      new ConnectionCheckJob(job.site).submit();
+    return job.submit();
+  }
+}

Modified: trunk/hudson/plugins/thinBackup/src/main/resources/org/jvnet/hudson/plugins/thinbackup/ThinBackupMgmtLink/restoreOptions.jelly (40757 => 40758)


--- trunk/hudson/plugins/thinBackup/src/main/resources/org/jvnet/hudson/plugins/thinbackup/ThinBackupMgmtLink/restoreOptions.jelly	2012-11-03 00:21:49 UTC (rev 40757)
+++ trunk/hudson/plugins/thinBackup/src/main/resources/org/jvnet/hudson/plugins/thinbackup/ThinBackupMgmtLink/restoreOptions.jelly	2012-11-08 20:17:14 UTC (rev 40758)
@@ -40,11 +40,11 @@
                 <f:optionalBlock title="Restore next build number file (if found in backup)"
                     help="/plugin/thinBackup/help/help-restoreNextBuildNumber.html"
                     name="restoreNextBuildNumber" />
-                <!--
+                
                 <f:optionalBlock title="Restore plugins"
                     help="/plugin/thinBackup/help/help-restorePlugins.html"
                     name="restorePlugins" />
-                 -->
+                 
                     
               </f:section>
               

Modified: trunk/hudson/plugins/thinBackup/src/main/webapp/help/help-restorePlugins.html (40757 => 40758)


--- trunk/hudson/plugins/thinBackup/src/main/webapp/help/help-restorePlugins.html	2012-11-03 00:21:49 UTC (rev 40757)
+++ trunk/hudson/plugins/thinBackup/src/main/webapp/help/help-restorePlugins.html	2012-11-08 20:17:14 UTC (rev 40758)
@@ -24,6 +24,7 @@
 
 <div>
   <p>
-    If this option is enabled, the plugins get restored. 
+    If this option is enabled, the plugins get restored.<br/>
+    You need an active internet connection to the update server, because plugins will be downloaded from the update server to keep the backup small.
   </p>
 </div>

Reply via email to