Skip to content

Commit

Permalink
Merge pull request #3212 from oleg-nenashev/bug/JENKINS-48761-remotin…
Browse files Browse the repository at this point in the history
…g-API

[JENKINS-48761] - Restore binary compatibility with agents running old Remoting versions
  • Loading branch information
oleg-nenashev committed Jan 3, 2018
2 parents 99ca101 + 01b8ed8 commit 8eb4254
Show file tree
Hide file tree
Showing 11 changed files with 363 additions and 8 deletions.
@@ -0,0 +1,6 @@
# Remoting version, which is embedded into the core
# This version MAY differ from what is really classloaded (see the "Pluggable Core Components", JENKINS-41196)
remoting.embedded.version=${remoting.version}

# Minimum Remoting version on external agents which is supported by the core
remoting.minimum.supported.version=${remoting.minimum.supported.version}
7 changes: 5 additions & 2 deletions core/src/main/java/hudson/Launcher.java
Expand Up @@ -44,7 +44,6 @@
import org.kohsuke.accmod.restrictions.NoExternalUse;

import javax.annotation.CheckForNull;
import javax.annotation.concurrent.GuardedBy;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.IOException;
Expand Down Expand Up @@ -1308,7 +1307,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
1 change: 1 addition & 0 deletions core/src/main/java/hudson/TcpSlaveAgentListener.java
Expand Up @@ -290,6 +290,7 @@ private void respondHello(String header, Socket s) throws IOException {
try {
Writer o = new OutputStreamWriter(s.getOutputStream(), "UTF-8");

//TODO: expose version about minimum supported Remoting version (JENKINS-48766)
if (header.startsWith("GET / ")) {
o.write("HTTP/1.0 200 OK\r\n");
o.write("Content-Type: text/plain;charset=UTF-8\r\n");
Expand Down
2 changes: 0 additions & 2 deletions core/src/main/java/hudson/model/Computer.java
Expand Up @@ -32,7 +32,6 @@
import hudson.slaves.Cloud;
import jenkins.util.SystemProperties;
import hudson.Util;
import hudson.cli.declarative.CLIMethod;
import hudson.cli.declarative.CLIResolver;
import hudson.console.AnnotatedLargeText;
import hudson.init.Initializer;
Expand Down Expand Up @@ -85,7 +84,6 @@
import org.kohsuke.stapler.WebMethod;
import org.kohsuke.stapler.export.Exported;
import org.kohsuke.stapler.export.ExportedBean;
import org.kohsuke.args4j.Option;
import org.kohsuke.stapler.interceptor.RequirePOST;

import javax.annotation.OverridingMethodsMustInvokeSuper;
Expand Down
8 changes: 6 additions & 2 deletions core/src/main/java/hudson/slaves/SlaveComputer.java
Expand Up @@ -38,12 +38,12 @@
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;
import hudson.slaves.OfflineCause.ChannelTermination;
import hudson.util.Futures;
import hudson.util.IOUtils;
import hudson.util.NullStream;
import hudson.util.RingBufferLogHandler;
import hudson.util.StreamTaskListener;
Expand Down Expand Up @@ -867,7 +867,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
27 changes: 26 additions & 1 deletion core/src/main/java/jenkins/security/MasterToSlaveCallable.java
@@ -1,7 +1,11 @@
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;


/**
Expand All @@ -11,10 +15,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: remove once Minimum supported Remoting version is 3.15 or above
@Override
public Channel getChannelOrFail() throws ChannelClosedException {
final Channel ch = Channel.current();
if (ch == null) {
throw new ChannelClosedException(new IllegalStateException("No channel associated with the thread"));
}
return ch;
}

//TODO: remove once Callable#getOpenChannelOrFail() once Minimaumsupported Remoting version is 3.15 or above
@Override
public Channel getOpenChannelOrFail() throws ChannelClosedException {
final Channel ch = getChannelOrFail();
if (ch.isClosingOrClosed()) { // TODO: Since Remoting 2.33, we still need to explicitly declare minimum Remoting version
throw new ChannelClosedException(new IllegalStateException("The associated channel " + ch + " is closing down or has closed down", ch.getCloseRequestCause()));
}
return ch;
}
}
103 changes: 103 additions & 0 deletions core/src/main/java/jenkins/slaves/RemotingVersionInfo.java
@@ -0,0 +1,103 @@
/*
* The MIT License
*
* Copyright (c) 2018, 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.slaves;

import hudson.util.VersionNumber;
import org.kohsuke.accmod.Restricted;
import org.kohsuke.accmod.restrictions.NoExternalUse;

import javax.annotation.CheckForNull;
import javax.annotation.Nonnull;
import java.io.IOException;
import java.io.InputStream;
import java.util.Properties;
import java.util.logging.Level;
import java.util.logging.Logger;

// TODO: Make the API public (JENKINS-48766)
/**
* Provides information about Remoting versions used within the core.
* @author Oleg Nenashev
*/
@Restricted(NoExternalUse.class)
public class RemotingVersionInfo {

private static final Logger LOGGER = Logger.getLogger(RemotingVersionInfo.class.getName());
private static final String RESOURCE_NAME="remoting-info.properties";

@CheckForNull
private static VersionNumber EMBEDDED_VERSION;

@CheckForNull
private static VersionNumber MINIMUM_SUPPORTED_VERSION;

private RemotingVersionInfo() {}

static {
Properties props = new Properties();
try (InputStream is = RemotingVersionInfo.class.getResourceAsStream(RESOURCE_NAME)) {
if(is!=null) {
props.load(is);
}
} catch (IOException e) {
LOGGER.log(Level.WARNING, "Failed to load Remoting Info from " + RESOURCE_NAME, e);
}

EMBEDDED_VERSION = tryExtractVersion(props, "remoting.embedded.version");
MINIMUM_SUPPORTED_VERSION = tryExtractVersion(props, "remoting.minimum.supported.version");
}

@CheckForNull
private static VersionNumber tryExtractVersion(@Nonnull Properties props, @Nonnull String propertyName) {
String prop = props.getProperty(propertyName);
if (prop == null) {
LOGGER.log(Level.FINE, "Property {0} is not defined in {1}", new Object[] {propertyName, RESOURCE_NAME});
return null;
}

if(prop.contains("${")) { // Due to whatever reason, Maven does not nullify them
LOGGER.log(Level.WARNING, "Property {0} in {1} has unresolved variable(s). Raw value: {2}",
new Object[] {propertyName, RESOURCE_NAME, prop});
return null;
}

try {
return new VersionNumber(prop);
} catch (RuntimeException ex) {
LOGGER.log(Level.WARNING, String.format("Failed to parse version for for property %s in %s. Raw Value: %s",
propertyName, RESOURCE_NAME, prop), ex);
return null;
}
}

@CheckForNull
public static VersionNumber getEmbeddedVersion() {
return EMBEDDED_VERSION;
}

@CheckForNull
public static VersionNumber getMinimumSupportedVersion() {
return MINIMUM_SUPPORTED_VERSION;
}
}
47 changes: 47 additions & 0 deletions core/src/test/java/jenkins/slaves/RemotingVersionInfoTest.java
@@ -0,0 +1,47 @@
/*
* The MIT License
*
* Copyright (c) 2018, 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.slaves;

import org.junit.Test;

import static org.hamcrest.CoreMatchers.*;
import static org.junit.Assert.assertThat;

/**
* Tests for {@link RemotingVersionInfo}.
*/
public class RemotingVersionInfoTest {

@Test
public void shouldLoadEmbeddedVersionByDefault() {
assertThat("Remoting Embedded version is not defined",
RemotingVersionInfo.getEmbeddedVersion(), notNullValue());
}

@Test
public void shouldLoadMinimumSupportedVersionByDefault() {
assertThat("Remoting Minimum supported version is not defined",
RemotingVersionInfo.getMinimumSupportedVersion(), notNullValue());
}
}
6 changes: 5 additions & 1 deletion pom.xml
Expand Up @@ -104,6 +104,10 @@ THE SOFTWARE.

<maven-war-plugin.version>3.0.0</maven-war-plugin.version> <!-- JENKINS-47127 bump when 3.2.0 is out. Cf. MWAR-407 -->

<!-- Minimum Remoting version, which is tested for API compatibility -->
<remoting.version>3.15</remoting.version>
<remoting.minimum.supported.version>2.60</remoting.minimum.supported.version>

</properties>

<!-- Note that the 'repositories' and 'pluginRepositories' blocks below are actually copy-pasted
Expand Down Expand Up @@ -175,7 +179,7 @@ THE SOFTWARE.
<dependency>
<groupId>org.jenkins-ci.main</groupId>
<artifactId>remoting</artifactId>
<version>3.15</version>
<version>${remoting.version}</version>
</dependency>

<dependency>
Expand Down
25 changes: 25 additions & 0 deletions test/pom.xml
Expand Up @@ -224,6 +224,31 @@ THE SOFTWARE.
<!-- version specified in grandparent pom -->
<extensions>true</extensions>
</plugin>
<plugin>
<artifactId>maven-dependency-plugin</artifactId>
<executions>
<execution>
<id>old-remoting-for-test</id>
<phase>generate-test-resources</phase>
<goals>
<!-- we use copy as this is a dependency from outside the reactor -->
<goal>copy</goal>
</goals>
<configuration>
<artifactItems>
<artifactItem>
<groupId>org.jenkins-ci.main</groupId>
<artifactId>remoting</artifactId>
<version>${remoting.minimum.supported.version}</version>
<type>jar</type>
<outputDirectory>${project.build.outputDirectory}/old-remoting</outputDirectory>
<destFileName>remoting-minimal-supported.jar</destFileName>
</artifactItem>
</artifactItems>
</configuration>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
Expand Down

0 comments on commit 8eb4254

Please sign in to comment.