Skip to content

Commit

Permalink
[JENKINS-48761] - Restore binary compatibility with agents running ol…
Browse files Browse the repository at this point in the history
…d Remoting versions
  • Loading branch information
oleg-nenashev committed Jan 2, 2018
1 parent 0ac93de commit 4eb314b
Show file tree
Hide file tree
Showing 6 changed files with 43 additions and 7 deletions.
8 changes: 6 additions & 2 deletions core/src/main/java/hudson/Launcher.java
Expand Up @@ -1289,7 +1289,7 @@ private static class RemoteLaunchCallable extends MasterToSlaveCallable<RemotePr
}

public RemoteProcess call() throws IOException {
final Channel channel = getOpenChannelOrFail();
final Channel channel = _getOpenChannelOrFail();
Launcher.ProcStarter ps = new LocalLauncher(listener).launch();
ps.cmds(cmd).masks(masks).envs(env).stdin(in).stdout(out).stderr(err).quiet(quiet);
if(workDir!=null) ps.pwd(workDir);
Expand All @@ -1308,7 +1308,11 @@ public int join() throws InterruptedException, IOException {
Channel taskChannel = null;
try {
// Sync IO will fail automatically if the channel is being closed, no need to use getOpenChannelOrFail()
taskChannel = Channel.currentOrFail();
// TODOL Replace by Channel#currentOrFail() when Remoting version allows
taskChannel = Channel.current();
if (taskChannel == null) {
throw new IOException("No Remoting channel associated with this thread");
}
taskChannel.syncIO();
} catch (Throwable t) {
// this includes a failure to sync, agent.jar too old, etc
Expand Down
2 changes: 1 addition & 1 deletion core/src/main/java/hudson/model/Computer.java
Expand Up @@ -1426,7 +1426,7 @@ public void doDumpExportTable( StaplerRequest req, StaplerResponse rsp ) throws

private static final class DumpExportTableTask extends MasterToSlaveCallable<String,IOException> {
public String call() throws IOException {
final Channel ch = getChannelOrFail();
final Channel ch = _getChannelOrFail();
StringWriter sw = new StringWriter();
try (PrintWriter pw = new PrintWriter(sw)) {
ch.dumpExportTable(pw);
Expand Down
2 changes: 1 addition & 1 deletion core/src/main/java/hudson/slaves/ChannelPinger.java
Expand Up @@ -134,7 +134,7 @@ public void install(Channel channel) {
@Override
public Void call() throws IOException {
// No sense in setting up channel pinger if the channel is being closed
setUpPingForChannel(getOpenChannelOrFail(), null, pingTimeoutSeconds, pingIntervalSeconds, false);
setUpPingForChannel(_getOpenChannelOrFail(), null, pingTimeoutSeconds, pingIntervalSeconds, false);
return null;
}

Expand Down
7 changes: 6 additions & 1 deletion core/src/main/java/hudson/slaves/SlaveComputer.java
Expand Up @@ -38,6 +38,7 @@
import hudson.model.User;
import hudson.remoting.Channel;
import hudson.remoting.ChannelBuilder;
import hudson.remoting.ChannelClosedException;
import hudson.remoting.Launcher;
import hudson.remoting.VirtualChannel;
import hudson.security.ACL;
Expand Down Expand Up @@ -867,7 +868,11 @@ public Void call() {
// ignore this error.
}

Channel.currentOrFail().setProperty("slave",Boolean.TRUE); // indicate that this side of the channel is the slave side.
try {
_getChannelOrFail().setProperty("slave",Boolean.TRUE); // indicate that this side of the channel is the slave side.
} catch (ChannelClosedException e) {
throw new IllegalStateException(e);
}

return null;
}
Expand Down
29 changes: 28 additions & 1 deletion core/src/main/java/jenkins/security/MasterToSlaveCallable.java
@@ -1,7 +1,13 @@
package jenkins.security;

import hudson.remoting.Callable;
import hudson.remoting.Channel;
import hudson.remoting.ChannelClosedException;
import org.jenkinsci.remoting.RoleChecker;
import org.kohsuke.accmod.Restricted;
import org.kohsuke.accmod.restrictions.NoExternalUse;

import javax.annotation.Nonnull;


/**
Expand All @@ -11,10 +17,31 @@
* @since 1.587 / 1.580.1
*/
public abstract class MasterToSlaveCallable<V, T extends Throwable> implements Callable<V,T> {

private static final long serialVersionUID = 1L;

@Override
public void checkRoles(RoleChecker checker) throws SecurityException {
checker.check(this,Roles.SLAVE);
}

private static final long serialVersionUID = 1L;
//TODO: replace by Callable#getChannelOrFail() once Minimal supported Remoting version is 3.15 or above
@Restricted(NoExternalUse.class)
protected static Channel _getChannelOrFail() throws ChannelClosedException {
final Channel ch = Channel.current();
if (ch == null) {
throw new ChannelClosedException("No channel associated with the thread", null);
}
return ch;
}

//TODO: replace by Callable#getOpenChannelOrFail() once Minimal supported Remoting version is 3.15 or above
@Restricted(NoExternalUse.class)
protected static Channel _getOpenChannelOrFail() throws ChannelClosedException {
final Channel ch = _getChannelOrFail();
if (ch.isClosingOrClosed()) { // TODO: Since Remoting 2.33, we still need to explicitly declare minimal Remoting version
throw new ChannelClosedException("The associated channel " + ch + " is closing down or has closed down", ch.getCloseRequestCause());
}
return ch;
}
}
Expand Up @@ -39,7 +39,7 @@ public void preOnline(Computer c, Channel channel, FilePath root, TaskListener l
private static final class ChannelSwapper extends MasterToSlaveCallable<Boolean,Exception> {
public Boolean call() throws Exception {
if (File.pathSeparatorChar==';') return false; // Windows
Channel c = getOpenChannelOrFail();
Channel c = _getOpenChannelOrFail();
StandardOutputStream sos = (StandardOutputStream) c.getProperty(StandardOutputStream.class);
if (sos!=null) {
swap(sos);
Expand Down

0 comments on commit 4eb314b

Please sign in to comment.