Merge pull request #4681 from Vlatombe/JENKINS-62006

This commit is contained in:
Tim Jacomb authored and GitHub committed 2020-05-01 20:40:39 +01:00
commit 8f978a960b
3 files changed
+68 -13

No files matched your search

@@ -54,6 +54,7 @@ import hudson.util.StreamTaskListener;
import hudson.util.VersionNumber;
import hudson.util.io.RewindableFileOutputStream;
import hudson.util.io.RewindableRotatingFileOutputStream;
import jenkins.agents.AgentComputerUtil;
import jenkins.model.Jenkins;
import jenkins.security.ChannelConfigurator;
import jenkins.security.MasterToSlaveCallable;
@@ -1067,17 +1068,11 @@ public class SlaveComputer extends Computer {
*
* @return null if the calling thread doesn't have any trace of where its master is.
* @since 1.362
* @deprecated Use {@link AgentComputerUtil#getChannelToMaster()} instead.
*/
@Deprecated
public static VirtualChannel getChannelToMaster() {
if (Jenkins.getInstanceOrNull()!=null) // check if calling thread is on master or on slave
return FilePath.localChannel;
// if this method is called from within the agent computation thread, this should work
Channel c = Channel.current();
if (c!=null && Boolean.TRUE.equals(c.getProperty("slave")))
return c;
return null;
return AgentComputerUtil.getChannelToMaster();
}
/**
@@ -33,7 +33,7 @@ import hudson.FilePath;
import hudson.Util;
import hudson.remoting.Channel;
import hudson.remoting.VirtualChannel;
import hudson.slaves.SlaveComputer;
import jenkins.agents.AgentComputerUtil;
import hudson.util.ProcessKillingVeto.VetoCause;
import hudson.util.ProcessTree.OSProcess;
import hudson.util.ProcessTreeRemoting.IOSProcess;
@@ -193,7 +193,7 @@ public abstract class ProcessTree implements Iterable<OSProcess>, IProcessTree,
/*package*/ final List<ProcessKiller> getKillers() throws InterruptedException {
if (killers==null)
try {
VirtualChannel channelToMaster = SlaveComputer.getChannelToMaster();
VirtualChannel channelToMaster = AgentComputerUtil.getChannelToMaster();
if (channelToMaster!=null) {
killers = channelToMaster.call(new ListAll());
} else {
@@ -288,7 +288,7 @@ public abstract class ProcessTree implements Iterable<OSProcess>, IProcessTree,
// Quick check, does anything exist to check against
if (!skipVetoes) {
try {
VirtualChannel channelToMaster = SlaveComputer.getChannelToMaster();
VirtualChannel channelToMaster = AgentComputerUtil.getChannelToMaster();
if (channelToMaster!=null) {
CheckVetoes vetoCheck = new CheckVetoes(this);
causeMessage = channelToMaster.call(vetoCheck);
@@ -429,7 +429,7 @@ public abstract class ProcessTree implements Iterable<OSProcess>, IProcessTree,
// Check for the existance of vetoers if I don't know already
if (vetoersExist == null) {
try {
VirtualChannel channelToMaster = SlaveComputer.getChannelToMaster();
VirtualChannel channelToMaster = AgentComputerUtil.getChannelToMaster();
if (channelToMaster != null) {
vetoersExist = channelToMaster.call(new DoVetoersExist());
}
@@ -0,0 +1,60 @@
/*
* The MIT License
*
* Copyright 2020 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.
*/
package jenkins.agents;
import edu.umd.cs.findbugs.annotations.CheckForNull;
import hudson.FilePath;
import hudson.remoting.Channel;
import hudson.remoting.VirtualChannel;
import jenkins.model.Jenkins;
public final class AgentComputerUtil {
private AgentComputerUtil() {
}
/**
* Obtains a {@link VirtualChannel} that allows some computation to be performed on the master.
* This method can be called from any thread on the master, or from agent (more precisely,
* it only works from the remoting request-handling thread in agents, which means if you've started
* separate thread on agents, that'll fail.)
*
* @return null if the calling thread doesn't have any trace of where its master is.
* @since TODO
*/
@CheckForNull
public static VirtualChannel getChannelToMaster() {
if (Jenkins.getInstanceOrNull() != null) {// check if calling thread is on master or on agent
return FilePath.localChannel;
}
// if this method is called from within the agent computation thread, this should work
Channel c = Channel.current();
if (c != null && Boolean.TRUE.equals(c.getProperty("slave"))) {
return c;
}
return null;
}
}