Skip to content

Commit

Permalink
Merge pull request #2299 from kzantow/JENKINS-34494-enable-required-p…
Browse files Browse the repository at this point in the history
…lugins

[JENKINS-34494] enable required plugins during plugin installs
  • Loading branch information
daniel-beck committed May 1, 2016
2 parents 59f0664 + 0c95d25 commit 3ac7ee9
Show file tree
Hide file tree
Showing 5 changed files with 155 additions and 7 deletions.
27 changes: 24 additions & 3 deletions core/src/main/java/hudson/PluginManager.java
Expand Up @@ -108,6 +108,7 @@
import java.util.HashMap;
import java.util.HashSet;
import java.util.Hashtable;
import java.util.Iterator;
import java.util.LinkedHashSet;
import java.util.List;
import java.util.ListIterator;
Expand Down Expand Up @@ -659,6 +660,14 @@ private boolean containsHpiJpi(Collection<String> bundledPlugins, String name) {
* TODO: revisit where/how to expose this. This is an experiment.
*/
public void dynamicLoad(File arc) throws IOException, InterruptedException, RestartRequiredException {
dynamicLoad(arc, false);
}

/**
* Try the dynamicLoad, removeExisting to attempt to dynamic load disabled plugins
*/
@Restricted(NoExternalUse.class)
public void dynamicLoad(File arc, boolean removeExisting) throws IOException, InterruptedException, RestartRequiredException {
LOGGER.info("Attempting to dynamic load "+arc);
PluginWrapper p = null;
String sn;
Expand All @@ -669,9 +678,21 @@ public void dynamicLoad(File arc) throws IOException, InterruptedException, Rest
p = strategy.createPluginWrapper(arc);
sn = p.getShortName();
}
if (getPlugin(sn)!=null)
throw new RestartRequiredException(Messages._PluginManager_PluginIsAlreadyInstalled_RestartRequired(sn));

PluginWrapper pw = getPlugin(sn);
if (pw!=null) {
if (removeExisting) { // try to load disabled plugins
for (Iterator<PluginWrapper> i = plugins.iterator(); i.hasNext();) {
pw = i.next();
if(sn.equals(pw.getShortName())) {
i.remove();
pw = null;
break;
}
}
} else {
throw new RestartRequiredException(Messages._PluginManager_PluginIsAlreadyInstalled_RestartRequired(sn));
}
}
if (p == null) {
p = strategy.createPluginWrapper(arc);
}
Expand Down
53 changes: 49 additions & 4 deletions core/src/main/java/hudson/model/UpdateCenter.java
Expand Up @@ -1404,7 +1404,52 @@ public String[] getStatuses() {


}


/**
* Enables a required plugin, provides feedback in the update center
*/
public class EnableJob extends InstallationJob {
public EnableJob(UpdateSite site, Authentication auth, @Nonnull Plugin plugin, boolean dynamicLoad) {
super(plugin, site, auth, dynamicLoad);
}

public Plugin getPlugin() {
return plugin;
}

@Override
public void run() {
try {
plugin.getInstalled().enable();
} catch (IOException e) {
LOGGER.log(Level.SEVERE, "Failed to enable " + plugin.getDisplayName(), e);
error = e;
}

if (dynamicLoad) {
try {
// remove the existing, disabled inactive plugin to force a new one to load
pm.dynamicLoad(getDestination(), true);
} catch (Exception e) {
LOGGER.log(Level.SEVERE, "Failed to dynamically load " + plugin.getDisplayName(), e);
error = e;
requiresRestart = true;
}
} else {
requiresRestart = true;
}
}
}

/**
* A no-op, e.g. this plugin is already installed
*/
public class NoOpJob extends EnableJob {
public NoOpJob(UpdateSite site, Authentication auth, @Nonnull Plugin plugin) {
super(site, auth, plugin, false);
}
}

/**
* Base class for a job that downloads a file from the Jenkins project.
*/
Expand Down Expand Up @@ -1618,19 +1663,19 @@ private void verifyChecksums(String expectedSHA1, String actualSha1, File downlo
/**
* Represents the state of the installation activity of one plugin.
*/
public final class InstallationJob extends DownloadJob {
public class InstallationJob extends DownloadJob {
/**
* What plugin are we trying to install?
*/
@Exported
public final Plugin plugin;

private final PluginManager pm = Jenkins.getInstance().getPluginManager();
protected final PluginManager pm = Jenkins.getInstance().getPluginManager();

/**
* True to load the plugin into this Jenkins, false to wait until restart.
*/
private final boolean dynamicLoad;
protected final boolean dynamicLoad;

/**
* @deprecated as of 1.442
Expand Down
16 changes: 16 additions & 0 deletions core/src/main/java/hudson/model/UpdateSite.java
Expand Up @@ -731,6 +731,10 @@ public List<Plugin> getNeededDependencies() {
else if (current.isOlderThan(requiredVersion)) {
deps.add(depPlugin);
}
// JENKINS-34494 - or if the plugin is disabled, this will allow us to enable it
else if (!current.isEnabled()) {
deps.add(depPlugin);
}
}

for(Map.Entry<String,String> e : optionalDependencies.entrySet()) {
Expand Down Expand Up @@ -853,6 +857,18 @@ public Future<UpdateCenterJob> deploy(boolean dynamicLoad, @CheckForNull UUID co
LOGGER.log(Level.WARNING, "Dependent install of " + dep.name + " for plugin " + name + " already added, skipping");
}
}
PluginWrapper pw = getInstalled();
if(pw != null) { // JENKINS-34494 - check for this plugin being disabled
Future<UpdateCenterJob> enableJob = null;
if(!pw.isEnabled()) {
UpdateCenter.EnableJob job = uc.new EnableJob(UpdateSite.this, null, this, dynamicLoad);
job.setCorrelationId(correlationId);
enableJob = uc.addJob(job);
}
if(pw.getVersionNumber().equals(new VersionNumber(version))) {
return enableJob != null ? enableJob : uc.addJob(uc.new NoOpJob(UpdateSite.this, null, this));
}
}
UpdateCenter.InstallationJob job = uc.new InstallationJob(this, UpdateSite.this, Jenkins.getAuthentication(), dynamicLoad);
job.setCorrelationId(correlationId);
return uc.addJob(job);
Expand Down
@@ -0,0 +1,33 @@
<!--
The MIT License
Copyright (c) 2016, CloudBees, Inc.
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
-->

<?jelly escape-by-default='true'?>
<j:jelly xmlns:j="jelly:core" xmlns:st="jelly:stapler" xmlns:d="jelly:define" xmlns:l="/lib/layout" xmlns:t="/lib/hudson" xmlns:f="/lib/form">
<tr id="row${it.id}">
<td style="vertical-align: top; padding-right:1em">${it.plugin.displayName}</td>
<td style="vertical-align:middle">
<l:icon class="icon-${it.error!=null?'yellow':'blue'} icon-md"/> ${%Enabled Dependency}
</td>
</tr>
</j:jelly>
@@ -0,0 +1,33 @@
<!--
The MIT License
Copyright (c) 2016, CloudBees, Inc.
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
-->

<?jelly escape-by-default='true'?>
<j:jelly xmlns:j="jelly:core" xmlns:st="jelly:stapler" xmlns:d="jelly:define" xmlns:l="/lib/layout" xmlns:t="/lib/hudson" xmlns:f="/lib/form">
<tr id="row${it.id}">
<td style="vertical-align: top; padding-right:1em">${it.plugin.displayName}</td>
<td style="vertical-align:middle">
<l:icon class="icon-${it.error!=null?'yellow':'blue'} icon-md"/> ${%Already Installed}
</td>
</tr>
</j:jelly>

0 comments on commit 3ac7ee9

Please sign in to comment.