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

Define hudsonUrl in WebSocket mode to fix agent installers #454

Merged
merged 1 commit into from
May 26, 2021
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
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 @@ -243,7 +245,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 @@ -257,6 +259,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 @@ -623,8 +637,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 @@ -640,7 +654,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