Permalink
Browse files

[FIXED JENKINS-37951] Honor withTool from Image.inside.

  • Loading branch information...
jglick committed Sep 30, 2016
1 parent 7354b1c commit 8b745de6fea3d998d6ce062bd5b0a6ae9898b11e
Showing with 24 additions and 6 deletions.
  1. +24 −6 src/main/java/org/jenkinsci/plugins/docker/workflow/WithContainerStep.java
@@ -57,7 +57,9 @@
import javax.annotation.Nonnull;

import hudson.util.VersionNumber;
import javax.annotation.CheckForNull;
import org.jenkinsci.plugins.docker.commons.fingerprint.DockerFingerprints;
import org.jenkinsci.plugins.docker.commons.tools.DockerTool;
import org.jenkinsci.plugins.workflow.steps.AbstractStepDescriptorImpl;
import org.jenkinsci.plugins.workflow.steps.AbstractStepExecutionImpl;
import org.jenkinsci.plugins.workflow.steps.AbstractStepImpl;
@@ -174,7 +176,7 @@ private static void destroy(String container, Launcher launcher, Node node, EnvV
DockerFingerprints.addRunFacet(dockerClient.getContainerRecord(env, container), run);
ImageAction.add(step.image, run);
getContext().newBodyInvoker().
withContext(BodyInvoker.mergeLauncherDecorators(getContext().get(LauncherDecorator.class), new Decorator(container, envHost, ws))).
withContext(BodyInvoker.mergeLauncherDecorators(getContext().get(LauncherDecorator.class), new Decorator(container, envHost, ws, toolName))).
withCallback(new Callback(container, toolName)).
start();
return false;
@@ -200,17 +202,25 @@ private static FilePath tempDir(FilePath ws) {
private final String container;
private final String[] envHost;
private final String ws;
private final @CheckForNull String toolName;

Decorator(String container, EnvVars envHost, String ws) {
Decorator(String container, EnvVars envHost, String ws, String toolName) {
this.container = container;
this.envHost = Util.mapToEnv(envHost);
this.ws = ws;
this.toolName = toolName;
}

@Override public Launcher decorate(final Launcher launcher, Node node) {
@Override public Launcher decorate(final Launcher launcher, final Node node) {
return new Launcher.DecoratedLauncher(launcher) {
@Override public Proc launch(Launcher.ProcStarter starter) throws IOException {
List<String> prefix = new ArrayList<String>(Arrays.asList("docker", "exec", container, "env"));
String executable;
try {
executable = getExecutable();
} catch (InterruptedException x) {
throw new IOException(x);
}
List<String> prefix = new ArrayList<>(Arrays.asList(executable, "exec", container, "env"));
if (ws != null) {
FilePath cwd = starter.pwd();
if (cwd != null) {
@@ -234,7 +244,8 @@ private static FilePath tempDir(FilePath ws) {
}
@Override public void kill(Map<String,String> modelEnvVars) throws IOException, InterruptedException {
ByteArrayOutputStream baos = new ByteArrayOutputStream();
if (getInner().launch().cmds("docker", "exec", container, "ps", "-A", "-o", "pid,command", "e").stdout(baos).quiet(true).join() != 0) {
String executable = getExecutable();
if (getInner().launch().cmds(executable, "exec", container, "ps", "-A", "-o", "pid,command", "e").stdout(baos).quiet(true).join() != 0) {
throw new IOException("failed to run ps");
}
List<String> pids = new ArrayList<String>();
@@ -254,13 +265,20 @@ private static FilePath tempDir(FilePath ws) {
}
LOGGER.log(Level.FINE, "killing {0}", pids);
if (!pids.isEmpty()) {
List<String> cmds = new ArrayList<String>(Arrays.asList("docker", "exec", container, "kill"));
List<String> cmds = new ArrayList<>(Arrays.asList(executable, "exec", container, "kill"));
cmds.addAll(pids);
if (getInner().launch().cmds(cmds).quiet(true).join() != 0) {
throw new IOException("failed to run kill");
}
}
}
private String getExecutable() throws IOException, InterruptedException {
EnvVars env = new EnvVars();
for (String pair : envHost) {
env.addLine(pair);
}
return DockerTool.getExecutable(toolName, node, getListener(), env);
}
};
}

0 comments on commit 8b745de

Please sign in to comment.