Skip to content

[FIXED] JENKINS-60473 #235

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

Closed
wants to merge 6 commits into from
Closed
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
Original file line number Diff line number Diff line change
Expand Up @@ -73,34 +73,68 @@
import java.util.logging.Logger;
import java.util.stream.Collectors;

/**
* The type With container step.
*/
public class WithContainerStep extends AbstractStepImpl {

private static final Logger LOGGER = Logger.getLogger(WithContainerStep.class.getName());
private final @Nonnull String image;
private String args;
private String toolName;
private static boolean needToContainerizePath = false;

/**
* Instantiates a new With container step.
*
* @param image the image
*/
@DataBoundConstructor public WithContainerStep(@Nonnull String image) {
this.image = image;
}


/**
* Gets image.
*
* @return the image
*/
public String getImage() {
return image;
}

/**
* Sets args.
*
* @param args the args
*/
@DataBoundSetter
public void setArgs(String args) {
this.args = Util.fixEmpty(args);
}

/**
* Gets args.
*
* @return the args
*/
public String getArgs() {
return args;
}

/**
* Gets tool name.
*
* @return the tool name
*/
public String getToolName() {
return toolName;
}

/**
* Sets tool name.
*
* @param toolName the tool name
*/
@DataBoundSetter public void setToolName(String toolName) {
this.toolName = Util.fixEmpty(toolName);
}
Expand All @@ -109,7 +143,10 @@ private static void destroy(String container, Launcher launcher, Node node, EnvV
new DockerClient(launcher, node, toolName).stop(launcherEnv, container);
}

// TODO switch to GeneralNonBlockingStepExecution
/**
* The type Execution.
*/
// TODO switch to GeneralNonBlockingStepExecution
public static class Execution extends AbstractStepExecutionImpl {
private static final long serialVersionUID = 1;
@Inject(optional=true) private transient WithContainerStep step;
Expand All @@ -124,6 +161,9 @@ public static class Execution extends AbstractStepExecutionImpl {
private String container;
private String toolName;

/**
* Instantiates a new Execution.
*/
public Execution() {
}

Expand All @@ -144,6 +184,14 @@ public Execution() {
? new DockerClient(launcher, node, toolName)
: new WindowsDockerClient(launcher, node, toolName);

String containerOsType = dockerClient.inspect(new EnvVars(), step.image, ".Os");

if (!launcher.isUnix() && containerOsType != null && containerOsType.equalsIgnoreCase("linux")) {
needToContainerizePath = true;
dockerClient.setNeedToContainerizePath(true);
dockerClient.setContainerUnix(true);
}

VersionNumber dockerVersion = dockerClient.version();
if (dockerVersion != null) {
if (dockerVersion.isOlderThan(new VersionNumber("1.7"))) {
Expand Down Expand Up @@ -194,7 +242,7 @@ public Execution() {
volumes.put(tmp, tmp);
}

String command = launcher.isUnix() ? "cat" : "cmd.exe";
String command = dockerClient.runCommand();
container = dockerClient.run(env, step.image, step.args, ws, volumes, volumesFromContainers, envReduced, dockerClient.whoAmI(), /* expected to hang until killed */ command);
final List<String> ps = dockerClient.listProcess(env, container);
if (!ps.contains(command)) {
Expand All @@ -207,9 +255,9 @@ public Execution() {

ImageAction.add(step.image, run);
getContext().newBodyInvoker().
withContext(BodyInvoker.mergeLauncherDecorators(getContext().get(LauncherDecorator.class), new Decorator(container, envHost, ws, toolName, dockerVersion))).
withCallback(new Callback(container, toolName)).
start();
withContext(BodyInvoker.mergeLauncherDecorators(getContext().get(LauncherDecorator.class), new Decorator(container, envHost, ws, toolName, dockerVersion))).
withCallback(new Callback(container, toolName)).
start();
return false;
}

Expand Down Expand Up @@ -246,6 +294,15 @@ private static class Decorator extends LauncherDecorator implements Serializable
private final boolean hasEnv;
private final boolean hasWorkdir;

/**
* Instantiates a new Decorator.
*
* @param container the container
* @param envHost the env host
* @param ws the ws
* @param toolName the tool name
* @param dockerVersion the docker version
*/
Decorator(String container, EnvVars envHost, String ws, String toolName, VersionNumber dockerVersion) {
this.container = container;
this.envHost = Util.mapToEnv(envHost);
Expand Down Expand Up @@ -311,8 +368,8 @@ private static class Decorator extends LauncherDecorator implements Serializable
masksPrefixList.add(false);
prefix.addAll(envReduced);
masksPrefixList.addAll(envReduced.stream()
.map(v -> true)
.collect(Collectors.toList()));
.map(v -> true)
.collect(Collectors.toList()));
}

boolean[] originalMasks = starter.masks();
Expand All @@ -332,8 +389,17 @@ private static class Decorator extends LauncherDecorator implements Serializable
System.arraycopy(originalMasks, 0, masks, prefix.size(), originalMasks.length);
starter.masks(masks);

if (needToContainerizePath && ws != null) {
String wsTrimmed = ws.replaceAll("[/\\\\]+$", "");
List<String> cmds = starter.cmds();
for (int i = 0; i < cmds.size(); i++) {
cmds.set(i, WindowsDockerClient.containerizePath(cmds.get(i), wsTrimmed));
}
}

return super.launch(starter);
}

@Override public void kill(Map<String,String> modelEnvVars) throws IOException, InterruptedException {
ByteArrayOutputStream baos = new ByteArrayOutputStream();
String executable = getExecutable();
Expand Down Expand Up @@ -382,6 +448,12 @@ private static class Callback extends BodyExecutionCallback.TailCall {
private final String container;
private final String toolName;

/**
* Instantiates a new Callback.
*
* @param container the container
* @param toolName the tool name
*/
Callback(String container, String toolName) {
this.container = container;
this.toolName = toolName;
Expand All @@ -393,8 +465,14 @@ private static class Callback extends BodyExecutionCallback.TailCall {

}

/**
* The type Descriptor.
*/
@Extension public static class DescriptorImpl extends AbstractStepDescriptorImpl {

/**
* Instantiates a new Descriptor.
*/
public DescriptorImpl() {
super(Execution.class);
}
Expand Down
Loading