Skip to content

Commit

Permalink
Merge pull request #454 from jglick/hudsonUrl
Browse files Browse the repository at this point in the history
Define `hudsonUrl` in WebSocket mode to fix agent installers
  • Loading branch information
oleg-nenashev authored May 26, 2021
2 parents e9136d8 + c022fbb commit 20e4994
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 7 deletions.
22 changes: 18 additions & 4 deletions src/main/java/hudson/remoting/Engine.java
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
import java.io.FileNotFoundException;
import java.io.IOException;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.Socket;
import java.net.URI;
import java.net.URL;
Expand Down Expand Up @@ -61,6 +62,7 @@
import java.util.concurrent.atomic.AtomicReference;
import java.util.logging.Level;
import java.util.logging.Logger;
import java.util.stream.Collectors;
import javax.annotation.CheckForNull;
import javax.annotation.Nonnull;
import javax.annotation.concurrent.NotThreadSafe;
Expand Down Expand Up @@ -244,7 +246,7 @@ public Engine(EngineListener listener, List<URL> hudsonUrls, String secretKey, S
this.listener = listener;
this.directConnection = directConnection;
this.events.add(listener);
this.candidateUrls = hudsonUrls;
this.candidateUrls = hudsonUrls.stream().map(Engine::ensureTrailingSlash).collect(Collectors.toList());
this.secretKey = secretKey;
this.agentName = agentName;
this.instanceIdentity = instanceIdentity;
Expand All @@ -258,6 +260,18 @@ public Engine(EngineListener listener, List<URL> hudsonUrls, String secretKey, S
});
}

private static URL ensureTrailingSlash(URL u) {
if (u.toString().endsWith("/")) {
return u;
} else {
try {
return new URL(u + "/");
} catch (MalformedURLException x) {
throw new IllegalArgumentException(x);
}
}
}

/**
* Starts the engine.
* The procedure initializes the working directory and all the required environment
Expand Down Expand Up @@ -638,8 +652,8 @@ public void closeRead() throws IOException {
}
}
}
String wsUrl = candidateUrls.get(0).toString().replaceFirst("^http", "ws");
if(!wsUrl.endsWith("/")) wsUrl += "/";
hudsonUrl = candidateUrls.get(0);
String wsUrl = hudsonUrl.toString().replaceFirst("^http", "ws");
ContainerProvider.getWebSocketContainer().connectToServer(new AgentEndpoint(),
ClientEndpointConfig.Builder.create().configurator(headerHandler).build(), URI.create(wsUrl + "wsagents/"));
while (ch.get() == null) {
Expand All @@ -655,7 +669,7 @@ public void closeRead() throws IOException {
events.onDisconnect();
while (true) {
// Unlike JnlpAgentEndpointResolver, we do not use $jenkins/tcpSlaveAgentListener/, as that will be a 404 if the TCP port is disabled.
URL ping = new URL(candidateUrls.get(0), "login");
URL ping = new URL(hudsonUrl, "login");
try {
HttpURLConnection conn = (HttpURLConnection) ping.openConnection();
int status = conn.getResponseCode();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -376,9 +376,7 @@ private boolean isPortVisible(String hostname, int port) {

@Nonnull
private URL toAgentListenerURL(@Nonnull String jenkinsUrl) throws MalformedURLException {
return jenkinsUrl.endsWith("/")
? new URL(jenkinsUrl + "tcpSlaveAgentListener/")
: new URL(jenkinsUrl + "/tcpSlaveAgentListener/");
return new URL(jenkinsUrl + "tcpSlaveAgentListener/");
}

@Override
Expand Down

0 comments on commit 20e4994

Please sign in to comment.