Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Merge pull request #79 from jglick/CommandLauncher-JENKINS-47393
[JENKINS-47393] Remove signature and runtime references to CommandLauncher
  • Loading branch information
jglick committed Oct 17, 2017
2 parents 2d7d6b8 + 913fb84 commit 175e6b4
Show file tree
Hide file tree
Showing 4 changed files with 145 additions and 8 deletions.
8 changes: 4 additions & 4 deletions src/main/java/org/jvnet/hudson/test/HudsonTestCase.java
Expand Up @@ -57,8 +57,8 @@
import hudson.security.GroupDetails;
import hudson.security.SecurityRealm;
import hudson.security.csrf.CrumbIssuer;
import hudson.slaves.CommandLauncher;
import hudson.slaves.ComputerConnector;
import hudson.slaves.ComputerLauncher;
import hudson.slaves.ComputerListener;
import hudson.slaves.DumbSlave;
import hudson.slaves.NodeProperty;
Expand Down Expand Up @@ -662,14 +662,14 @@ public PretendSlave createPretendSlave(FakeLauncher faker) throws Exception {
}

/**
* Creates a {@link CommandLauncher} for launching a slave locally.
* Creates a {@link ComputerLauncher} for launching a slave locally.
*
* @param env
* Environment variables to add to the slave process. Can be null.
*/
public CommandLauncher createComputerLauncher(EnvVars env) throws URISyntaxException, MalformedURLException {
public ComputerLauncher createComputerLauncher(EnvVars env) throws URISyntaxException, IOException {
int sz = jenkins.getNodes().size();
return new CommandLauncher(
return new SimpleCommandLauncher(
String.format("\"%s/bin/java\" %s -jar \"%s\"",
System.getProperty("java.home"),
SLAVE_DEBUG_PORT>0 ? " -Xdebug -Xrunjdwp:transport=dt_socket,server=y,address="+(SLAVE_DEBUG_PORT+sz): "",
Expand Down
8 changes: 4 additions & 4 deletions src/main/java/org/jvnet/hudson/test/JenkinsRule.java
Expand Up @@ -91,8 +91,8 @@
import hudson.security.AbstractPasswordBasedSecurityRealm;
import hudson.security.GroupDetails;
import hudson.security.csrf.CrumbIssuer;
import hudson.slaves.CommandLauncher;
import hudson.slaves.ComputerConnector;
import hudson.slaves.ComputerLauncher;
import hudson.slaves.DumbSlave;
import hudson.slaves.OfflineCause;
import hudson.slaves.RetentionStrategy;
Expand Down Expand Up @@ -904,14 +904,14 @@ public PretendSlave createPretendSlave(FakeLauncher faker) throws Exception {
}

/**
* Creates a {@link hudson.slaves.CommandLauncher} for launching a slave locally.
* Creates a launcher for starting a local agent.
*
* @param env
* Environment variables to add to the slave process. Can be null.
*/
public CommandLauncher createComputerLauncher(EnvVars env) throws URISyntaxException, MalformedURLException {
public ComputerLauncher createComputerLauncher(EnvVars env) throws URISyntaxException, IOException {
int sz = jenkins.getNodes().size();
return new CommandLauncher(
return new SimpleCommandLauncher(
String.format("\"%s/bin/java\" %s -jar \"%s\"",
System.getProperty("java.home"),
SLAVE_DEBUG_PORT>0 ? " -Xdebug -Xrunjdwp:transport=dt_socket,server=y,address="+(SLAVE_DEBUG_PORT+sz): "",
Expand Down
106 changes: 106 additions & 0 deletions src/main/java/org/jvnet/hudson/test/SimpleCommandLauncher.java
@@ -0,0 +1,106 @@
/*
* The MIT License
*
* Copyright 2017 CloudBees, Inc.
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/

package org.jvnet.hudson.test;

import hudson.AbortException;
import hudson.EnvVars;
import hudson.Extension;
import hudson.Util;
import hudson.model.Descriptor;
import hudson.model.Slave;
import hudson.model.TaskListener;
import hudson.remoting.Channel;
import hudson.slaves.ComputerLauncher;
import hudson.slaves.SlaveComputer;
import hudson.util.ProcessTree;
import hudson.util.StreamCopyThread;
import java.io.IOException;
import java.util.logging.Level;
import java.util.logging.Logger;
import org.kohsuke.stapler.DataBoundConstructor;

/**
* Stripped-down clone of {@code CommandLauncher}.
*/
public class SimpleCommandLauncher extends ComputerLauncher {

private static final Logger LOGGER = Logger.getLogger(SimpleCommandLauncher.class.getName());

public final String cmd;
private final EnvVars env;

@DataBoundConstructor // in case anyone needs to configRoundtrip such a node
public SimpleCommandLauncher(String cmd) {
this(cmd, null);
}

SimpleCommandLauncher(String cmd, EnvVars env) {
this.cmd = cmd;
this.env = env;
}

@Override
public void launch(SlaveComputer computer, final TaskListener listener) {
try {
Slave node = computer.getNode();
if (node == null) {
throw new AbortException("Cannot launch commands on deleted nodes");
}
listener.getLogger().println("$ " + cmd);
ProcessBuilder pb = new ProcessBuilder(Util.tokenize(cmd));
final EnvVars cookie = EnvVars.createCookie();
pb.environment().putAll(cookie);
if (env != null) {
pb.environment().putAll(env);
}
final Process proc = pb.start();
new StreamCopyThread("stderr copier for remote agent on " + computer.getDisplayName(), proc.getErrorStream(), listener.getLogger()).start();
computer.setChannel(proc.getInputStream(), proc.getOutputStream(), listener.getLogger(), new Channel.Listener() {
@Override
public void onClosed(Channel channel, IOException cause) {
try {
ProcessTree.get().killAll(proc, cookie);
} catch (Exception x) {
LOGGER.log(Level.WARNING, null, x);
}
}
});
LOGGER.log(Level.INFO, "agent launched for {0}", computer.getName());
} catch (Exception x) {
LOGGER.log(Level.WARNING, null, x);
}
}

@Extension
public static class DescriptorImpl extends Descriptor<ComputerLauncher> {

@Override // TODO pending 1.635
public String getDisplayName() {
return "SimpleCommandLauncher";
}

}

}
@@ -0,0 +1,31 @@
<?xml version="1.0" encoding="UTF-8"?>
<!--
The MIT License
Copyright 2017 CloudBees, Inc.
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
-->

<?jelly escape-by-default='true'?>
<j:jelly xmlns:j="jelly:core" xmlns:f="/lib/form">
<f:entry field="cmd" title="cmd">
<f:textbox/>
</f:entry>
</j:jelly>

0 comments on commit 175e6b4

Please sign in to comment.