Skip to content

Commit

Permalink
Stop relying on deprecated /computer/${NAME}/jenkins-agent.jnlp end…
Browse files Browse the repository at this point in the history
…point (#601)
  • Loading branch information
basil committed Dec 13, 2023
1 parent 759bc76 commit 3ceb8dd
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 8 deletions.
6 changes: 2 additions & 4 deletions client/src/main/java/hudson/plugins/swarm/Client.java
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@
import java.nio.file.InvalidPathException;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.List;
import java.util.Optional;
import java.util.logging.Level;
import java.util.logging.Logger;
Expand Down Expand Up @@ -216,10 +215,9 @@ static void run(SwarmClient swarmClient, Options options, String... args) throws

/*
* Note that any instances of InterruptedException or RuntimeException thrown
* internally by the next two lines get wrapped in RetryException.
* internally by the next line get wrapped in RetryException.
*/
List<String> jnlpArgs = swarmClient.getJnlpArgs(url);
swarmClient.connect(jnlpArgs, url);
swarmClient.connect(url);
if (options.noRetryAfterConnected) {
logger.warning("Connection closed, exiting...");
swarmClient.exitWithStatus(0);
Expand Down
24 changes: 20 additions & 4 deletions client/src/main/java/hudson/plugins/swarm/SwarmClient.java
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,7 @@ public class SwarmClient {

private final Options options;
private final String hash;
private String secret;
private String name;
private HttpServer prometheusServer = null;

Expand Down Expand Up @@ -125,8 +126,10 @@ public URL getUrl() {
/**
* @param url The URL
* @return The JNLP arguments, as returned from {@link Launcher#parseJnlpArguments()}.
* @deprecated removed without replacement
*/
List<String> getJnlpArgs(URL url) throws IOException, RetryException {
@Deprecated
private List<String> getJnlpArgs(URL url) throws IOException, RetryException {
logger.fine("connect() invoked");

Launcher launcher = new Launcher();
Expand Down Expand Up @@ -157,14 +160,22 @@ List<String> getJnlpArgs(URL url) throws IOException, RetryException {
*
* <p>Interrupt the thread to abort it and try connecting again.
*/
void connect(List<String> jnlpArgs, URL url) throws IOException, RetryException {
void connect(URL url) throws IOException, RetryException {
List<String> args = new ArrayList<>();

args.add("-url");
args.add(url.toString());

args.add("-secret");
args.add(jnlpArgs.get(0));
if (secret == null) {
List<String> jnlpArgs = getJnlpArgs(url);
if (!jnlpArgs.isEmpty()) {
secret = jnlpArgs.get(0);
}
}
if (secret != null) {
args.add("-secret");
args.add(secret);
}

args.add("-name");
args.add(name);
Expand Down Expand Up @@ -368,6 +379,11 @@ void createSwarmAgent(URL url) throws IOException, InterruptedException, RetryEx
props.load(stream);
}

String secret = props.getProperty("secret");
if (secret != null) {
this.secret = secret.trim();
}

String name = props.getProperty("name");
if (name == null) {
this.name = options.name;
Expand Down
3 changes: 3 additions & 0 deletions plugin/src/main/java/hudson/plugins/swarm/PluginImpl.java
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
import java.util.Set;
import javax.servlet.http.HttpServletResponse;
import jenkins.model.Jenkins;
import jenkins.slaves.JnlpAgentReceiver;
import org.apache.commons.lang.ArrayUtils;
import org.kohsuke.stapler.QueryParameter;
import org.kohsuke.stapler.StaplerRequest;
Expand Down Expand Up @@ -135,6 +136,7 @@ public void doCreateSlave(
Jenkins jenkins = Jenkins.get();

jenkins.checkPermission(Computer.CREATE);
jenkins.checkPermission(Computer.CONNECT);

List<NodeProperty<Node>> nodeProperties = new ArrayList<>();

Expand Down Expand Up @@ -210,6 +212,7 @@ public void doCreateSlave(
try (OutputStream outputStream = rsp.getCompressedOutputStream(req)) {
Properties props = new Properties();
props.put("name", name);
props.put("secret", JnlpAgentReceiver.SLAVE_SECRET.mac(name));
props.store(outputStream, "");
}
} catch (FormException e) {
Expand Down

0 comments on commit 3ceb8dd

Please sign in to comment.