Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[JENKINS-39596,JENKINS-39617] - hudsonUrl in Remoting Engine was always null since 3.0 #131

Merged
merged 2 commits into from
Nov 9, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions src/main/java/hudson/remoting/Engine.java
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,7 @@ public void run() {
* This value is determined from {@link #candidateUrls} after a successful connection.
* Note that this URL <b>DOES NOT</b> have "tcpSlaveAgentListener" in it.
*/
@CheckForNull
private URL hudsonUrl;

private final String secretKey;
Expand Down Expand Up @@ -171,6 +172,12 @@ public void setJarCache(JarCache jarCache) {
this.jarCache = jarCache;
}

/**
* Provides Jenkins URL if available.
* @return Jenkins URL. May return {@code null} if the connection is not established or if the URL cannot be determined
* in the {@link JnlpAgentEndpointResolver}.
*/
@CheckForNull
public URL getHudsonUrl() {
return hudsonUrl;
}
Expand Down Expand Up @@ -334,6 +341,7 @@ private void innerRun(IOHub hub, SSLContext context, ExecutorService service) {
events.status("Could not resolve server among " + candidateUrls);
return;
}
hudsonUrl = endpoint.getServiceUrl();

events.status(String.format("Agent discovery successful%n"
+ " Agent address: %s%n"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
import java.net.InetAddress;
import java.net.InetSocketAddress;
import java.net.Socket;
import java.net.URL;
import java.nio.channels.SocketChannel;
import java.security.interfaces.RSAPublicKey;
import java.util.Collections;
Expand Down Expand Up @@ -65,24 +66,44 @@ public class JnlpAgentEndpoint {
*/
@CheckForNull
private final Set<String> protocols;

/**
* Jenkins URL for the discovered endpoint.
* @since TODO
*/
@CheckForNull
private final URL serviceUrl;

/**
* @deprecated Use {@link #JnlpAgentEndpoint(java.lang.String, int, java.security.interfaces.RSAPublicKey, java.util.Set, java.lang.String)}
*/
@Deprecated
public JnlpAgentEndpoint(@Nonnull String host, int port, @CheckForNull RSAPublicKey publicKey,
@CheckForNull Set<String> protocols) {
this(host, port, publicKey, protocols, null);
}

/**
* Constructor for a remote {@code Jenkins} instance.
*
* @param host the hostname.
* @param port the port.
* @param publicKey the {@code InstanceIdentity.getPublic()} of the remote instance (if known).
* @param protocols The supported protocols.
* @param serviceURL URL of the service hosting the remoting endpoint.
* Use {@code null} if it is not a web service or if the URL cannot be determined
* @since TODO
*/
public JnlpAgentEndpoint(@Nonnull String host, int port, @CheckForNull RSAPublicKey publicKey,
@CheckForNull Set<String> protocols) {
@CheckForNull Set<String> protocols, @CheckForNull URL serviceURL) {
if (port <= 0 || 65536 <= port) {
throw new IllegalArgumentException("Port " + port + " is not in the range 1-65535");
}
this.host = host;
this.port = port;
this.publicKey = publicKey;
this.protocols = protocols == null ? null : Collections.unmodifiableSet(new LinkedHashSet<String>(protocols));
this.serviceUrl = serviceURL;
}

/**
Expand All @@ -95,6 +116,15 @@ public InetSocketAddress getAddress() {
return new InetSocketAddress(host, port);
}

/**
* Retrieves URL of the web service providing the remoting endpoint.
* @return Service URL if available. {@code null} otherwise.
*/
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Missing @since. Will fix during the merge if there is no other bugs

@CheckForNull
public URL getServiceUrl() {
return serviceUrl;
}

/**
* Gets the hostname.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,16 @@ public void setTunnel(String tunnel) {
public JnlpAgentEndpoint resolve() throws IOException {
IOException firstError = null;
for (String jenkinsUrl : jenkinsUrls) {
URL salURL = toAgentListenerURL(jenkinsUrl);

final URL selectedJenkinsURL;
final URL salURL;
try {
selectedJenkinsURL = new URL(jenkinsUrl);
salURL = toAgentListenerURL(jenkinsUrl);
} catch (MalformedURLException ex) {
LOGGER.log(Level.WARNING, String.format("Cannot parse agent endpoint URL %s. Skipping it", jenkinsUrl), ex);
continue;
}

// find out the TCP port
HttpURLConnection con =
Expand Down Expand Up @@ -223,7 +232,8 @@ public int compare(String o1, String o2) {
if (tokens[0].length() > 0) host = tokens[0];
if (tokens[1].length() > 0) port = Integer.parseInt(tokens[1]);
}
return new JnlpAgentEndpoint(host, port, identity, agentProtocolNames);

return new JnlpAgentEndpoint(host, port, identity, agentProtocolNames, selectedJenkinsURL);
} finally {
con.disconnect();
}
Expand Down