Skip to content

Commit

Permalink
Avoid -jnlpUrl if running on a recent enough version of Remoting (#670
Browse files Browse the repository at this point in the history
)
  • Loading branch information
basil authored Oct 26, 2023
1 parent cfd8faa commit 6053d71
Showing 1 changed file with 39 additions and 5 deletions.
44 changes: 39 additions & 5 deletions src/main/java/org/jvnet/hudson/test/InboundAgentRule.java
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@
import hudson.slaves.RetentionStrategy;
import hudson.slaves.SlaveComputer;
import hudson.util.StreamCopyThread;
import hudson.util.VersionNumber;
import java.io.File;
import java.io.IOException;
import java.io.Serializable;
Expand All @@ -47,6 +48,7 @@
import java.util.Objects;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ConcurrentMap;
import java.util.jar.JarFile;
import java.util.logging.Level;
import java.util.logging.Logger;
import org.apache.commons.io.FileUtils;
Expand Down Expand Up @@ -84,6 +86,11 @@ public static final class Options implements Serializable {
// TODO Java 14+ use records

Check warning on line 86 in src/main/java/org/jvnet/hudson/test/InboundAgentRule.java

View check run for this annotation

ci.jenkins.io / Open Tasks Scanner

TODO

NORMAL: Java 14+ use records

@CheckForNull private String name;

/**
* @deprecated secret is used by default when using newer versions of Remoting
*/
@Deprecated
private boolean secret;
private boolean webSocket;
@CheckForNull private String tunnel;
Expand All @@ -96,6 +103,10 @@ public String getName() {
return name;
}

/**
* @deprecated secret is used by default when using newer versions of Remoting
*/
@Deprecated
public boolean isSecret() {
return secret;
}
Expand Down Expand Up @@ -154,7 +165,9 @@ public Builder color(PrefixedOutputStream.AnsiColor color) {
* Use secret when connecting.
*
* @return this builder
* @deprecated secret is used by default when using newer versions of Remoting
*/
@Deprecated
public Builder secret() {
options.secret = true;
return this;
Expand Down Expand Up @@ -314,12 +327,23 @@ public void start(AgentArguments agentArguments, Options options) throws Excepti
cmd.add("-Xdebug");
cmd.add("Xrunjdwp:transport=dt_socket,server=y,address=" + (JenkinsRule.SLAVE_DEBUG_PORT + agentArguments.numberOfNodes - 1));
}
cmd.addAll(List.of(
"-jar", agentArguments.agentJar.getAbsolutePath(),
"-jnlpUrl", agentArguments.agentJnlpUrl));
if (options.isSecret()) {
// To watch it fail: secret = secret.replace('1', '2');
cmd.addAll(List.of("-jar", agentArguments.agentJar.getAbsolutePath()));
if (agentArguments.agentJnlpUrl.endsWith("computer/" + options.getName() + "/slave-agent.jnlp") && remotingVersion(agentArguments.agentJar).isNewerThanOrEqualTo(new VersionNumber("3186.vc3b_7249b_87eb_"))) {
cmd.addAll(List.of("-url", agentArguments.agentJnlpUrl.replaceAll("computer/" + options.getName() + "/slave-agent.jnlp$", "")));
cmd.addAll(List.of("-secret", agentArguments.secret));
cmd.addAll(List.of("-name", options.getName()));
if (options.isWebSocket()) {
cmd.add("-webSocket");
}
if (options.getTunnel() != null) {
cmd.addAll(List.of("-tunnel", options.getTunnel()));
}
} else {
cmd.addAll(List.of("-jnlpUrl", agentArguments.agentJnlpUrl));
if (options.isSecret()) {
// To watch it fail: secret = secret.replace('1', '2');
cmd.addAll(List.of("-secret", agentArguments.secret));
}
}
cmd.addAll(agentArguments.commandLineArgs);
ProcessBuilder pb = new ProcessBuilder(cmd);
Expand All @@ -330,6 +354,16 @@ public void start(AgentArguments agentArguments, Options options) throws Excepti
new StreamCopyThread("inbound-agent-" + options.getName(), proc.getInputStream(), options.prefixedOutputStreamBuilder.build(System.err)).start();
}

private static VersionNumber remotingVersion(File agentJar) throws IOException {
try (JarFile j = new JarFile(agentJar)) {
String v = j.getManifest().getMainAttributes().getValue("Version");
if (v == null) {
throw new IOException("no Version in " + agentJar);
}
return new VersionNumber(v);
}
}

/**
* Stop an existing inbound agent and wait for it to go offline.
*/
Expand Down

0 comments on commit 6053d71

Please sign in to comment.