Skip to content
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

Jenkins 48050 #636

Merged
merged 10 commits into from
Mar 14, 2018
39 changes: 28 additions & 11 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<java.level>8</java.level>
<groovy.version>2.4.7</groovy.version>
<jenkins.version>2.60.3</jenkins.version>
</properties>

Expand All @@ -41,6 +42,18 @@
<groupId>org.jenkins-ci.plugins</groupId>
<artifactId>docker-java-api</artifactId>
<version>3.0.14</version>
<exclusions>
<exclusion>
<groupId>org.apache.httpcomponents</groupId>
<artifactId>httpcore</artifactId>
</exclusion>
</exclusions>
</dependency>

<dependency>
<groupId>org.jenkins-ci.plugins</groupId>
<artifactId>apache-httpcomponents-client-4-api</artifactId>
<version>4.5.3-2.0</version>
</dependency>

<dependency>
Expand Down Expand Up @@ -74,7 +87,7 @@
<dependency>
<groupId>org.jenkins-ci.plugins.workflow</groupId>
<artifactId>workflow-step-api</artifactId>
<version>2.13</version>
<version>2.14</version>
<optional>true</optional>
</dependency>
<dependency>
Expand Down Expand Up @@ -111,11 +124,10 @@
<dependency>
<groupId>org.jenkins-ci.plugins.workflow</groupId>
<artifactId>workflow-step-api</artifactId>
<version>2.13</version>
<version>2.14</version>
<optional>true</optional>
</dependency>


<dependency>
<groupId>com.kohlschutter.junixsocket</groupId>
<artifactId>junixsocket-common</artifactId>
Expand Down Expand Up @@ -148,15 +160,11 @@
<dependency>
<groupId>com.google.code.findbugs</groupId>
<artifactId>annotations</artifactId>
<version>3.0.1</version>
<version>3.0.0</version>
</dependency>

<dependency>
<groupId>org.jenkins-ci.plugins.workflow</groupId>
<artifactId>workflow-cps</artifactId>
<version>2.41</version>
<scope>test</scope>
</dependency>


<dependency>
<groupId>org.jenkins-ci.main</groupId>
<artifactId>jenkins-test-harness-tools</artifactId>
Expand Down Expand Up @@ -185,6 +193,16 @@
</dependency>
</dependencies>

<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.jenkins-ci.plugins</groupId>
<artifactId>structs</artifactId>
<version>1.9</version>
</dependency>
</dependencies>
</dependencyManagement>

<build>
<plugins>
<plugin>
Expand All @@ -207,7 +225,6 @@
<compatibleSinceVersion>0.17</compatibleSinceVersion>
</configuration>
</plugin>

</plugins>
</build>

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import com.github.dockerjava.api.DockerClient;
import com.github.dockerjava.api.command.CreateContainerCmd;
import com.github.dockerjava.api.command.InspectImageResponse;
import com.github.dockerjava.api.command.PullImageCmd;
import com.github.dockerjava.api.exception.DockerClientException;
import com.github.dockerjava.api.exception.DockerException;
Expand Down Expand Up @@ -31,6 +32,7 @@
import io.jenkins.docker.client.DockerAPI;
import io.jenkins.docker.connector.DockerComputerConnector;
import jenkins.model.Jenkins;
import org.apache.commons.lang.StringUtils;
import org.jenkinsci.plugins.docker.commons.credentials.DockerRegistryEndpoint;
import org.kohsuke.accmod.Restricted;
import org.kohsuke.accmod.restrictions.NoExternalUse;
Expand Down Expand Up @@ -62,7 +64,7 @@ public class DockerTemplate implements Describable<DockerTemplate> {
@Deprecated
private transient DockerComputerLauncher launcher;

public String remoteFs = "/home/jenkins";
public String remoteFs;

public final int instanceCap;

Expand Down Expand Up @@ -90,17 +92,26 @@ public DockerTemplate() {
this.instanceCap = 1;
}

@DataBoundConstructor
public DockerTemplate(@Nonnull DockerTemplateBase dockerTemplateBase,
DockerComputerConnector connector,
String labelString,
String remoteFs,
String instanceCapStr) {
this(dockerTemplateBase, connector, labelString, instanceCapStr);
setRemoteFs(remoteFs);
}


@DataBoundConstructor
public DockerTemplate(@Nonnull DockerTemplateBase dockerTemplateBase,
DockerComputerConnector connector,
String labelString,
String instanceCapStr
) {
this.dockerTemplateBase = dockerTemplateBase;
this.connector = connector;
this.labelString = Util.fixNull(labelString);
this.remoteFs = Strings.isNullOrEmpty(remoteFs) ? "/home/jenkins" : remoteFs;
this.remoteFs = remoteFs;

if (Strings.isNullOrEmpty(instanceCapStr)) {
this.instanceCap = Integer.MAX_VALUE;
Expand Down Expand Up @@ -252,6 +263,11 @@ public String getRemoteFs() {
return remoteFs;
}

@DataBoundSetter
public void setRemoteFs(String remoteFs) {
this.remoteFs = remoteFs;
}

public String getInstanceCapStr() {
if (instanceCap == Integer.MAX_VALUE) {
return "";
Expand Down Expand Up @@ -377,7 +393,7 @@ public Descriptor<DockerTemplate> getDescriptor() {
return (DescriptorImpl) Jenkins.getInstance().getDescriptor(getClass());
}

void pullImage(DockerAPI api, TaskListener listener) throws IOException, InterruptedException {
InspectImageResponse pullImage(DockerAPI api, TaskListener listener) throws IOException, InterruptedException {

String image = getFullImageId();
final DockerClient client = api.getClient();
Expand Down Expand Up @@ -408,13 +424,21 @@ public void onNext(PullResponseItem item) {
LOGGER.info("Finished pulling image '{}', took {} ms", image, pullTime);
}

return client.inspectImageCmd(image).exec();

}

@Restricted(NoExternalUse.class)
public DockerTransientNode provisionNode(DockerAPI api, TaskListener listener) throws IOException, Descriptor.FormException, InterruptedException {

final DockerComputerConnector connector = getConnector();
pullImage(api, listener);
final InspectImageResponse image = pullImage(api, listener);
if (StringUtils.isBlank(remoteFs)) {
remoteFs = image.getContainerConfig().getWorkingDir();
}
if (StringUtils.isBlank(remoteFs)) {
remoteFs = "/";
}

LOGGER.info("Trying to run container for {}", getImage());

Expand Down
21 changes: 18 additions & 3 deletions src/main/java/io/jenkins/docker/pipeline/DockerNodeStep.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
import hudson.model.Node;
import hudson.model.TaskListener;
import hudson.util.ListBoxModel;
import io.jenkins.docker.connector.DockerComputerConnector;
import jenkins.model.Jenkins;
import org.jenkinsci.plugins.docker.commons.credentials.DockerServerEndpoint;
import org.jenkinsci.plugins.workflow.graph.FlowNode;
Expand Down Expand Up @@ -37,11 +38,12 @@ public class DockerNodeStep extends Step {

private String remoteFs;

private DockerComputerConnector connector;

@DataBoundConstructor
public DockerNodeStep(String dockerHost, String image, String remoteFs) {
public DockerNodeStep(String dockerHost, String image) {
this.dockerHost = dockerHost;
this.image = image;
this.remoteFs = remoteFs;
}

public String getDockerHost() {
Expand All @@ -65,10 +67,23 @@ public String getRemoteFs() {
return remoteFs;
}

@DataBoundSetter
public void setRemoteFs(String remoteFs) {
this.remoteFs = remoteFs;
}

public DockerComputerConnector getConnector() {
return connector;
}

@DataBoundSetter
public void setConnector(DockerComputerConnector connector) {
this.connector = connector;
}

@Override
public StepExecution start(StepContext context) throws Exception {
return new DockerNodeStepExecution(context, dockerHost, credentialsId, image, remoteFs);
return new DockerNodeStepExecution(context, connector, dockerHost, credentialsId, image, remoteFs);
}

@Extension(optional = true)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
import io.jenkins.docker.DockerTransientNode;
import io.jenkins.docker.client.DockerAPI;
import io.jenkins.docker.connector.DockerComputerAttachConnector;
import io.jenkins.docker.connector.DockerComputerConnector;
import jenkins.model.Jenkins;
import jenkins.model.NodeListener;
import org.jenkinsci.plugins.docker.commons.credentials.DockerServerEndpoint;
Expand All @@ -37,11 +38,13 @@ class DockerNodeStepExecution extends StepExecution {
private final String credentialsId;
private final String image;
private final String remoteFs;
private final DockerComputerConnector connector;
private transient volatile CompletableFuture<DockerTransientNode> task;
private volatile String nodeName;

public DockerNodeStepExecution(StepContext context, String dockerHost, String credentialsId, String image, String remoteFs) {
public DockerNodeStepExecution(StepContext context, DockerComputerConnector connector, String dockerHost, String credentialsId, String image, String remoteFs) {
super(context);
this.connector = connector != null ? connector : new DockerComputerAttachConnector();
this.dockerHost = dockerHost;
this.credentialsId = credentialsId;
this.image = image;
Expand Down Expand Up @@ -75,7 +78,7 @@ private DockerTransientNode createNode(TaskListener listener) {

final DockerTemplate t = new DockerTemplate(
new DockerTemplateBase(image),
new DockerComputerAttachConnector(),
connector,
uuid, remoteFs, "1");

t.setMode(Node.Mode.EXCLUSIVE);
Expand Down Expand Up @@ -120,7 +123,7 @@ private void invokeBody(DockerTransientNode node, TaskListener listener) {
EnvVars env = null;
try {
// TODO workspace should be a volume
ws = node.createPath(remoteFs + "/workspace");
ws = node.createPath(node.getRemoteFS() + "/workspace");
FlowNode flowNode = getContext().get(FlowNode.class);
flowNode.addAction(new WorkspaceActionImpl(ws, flowNode));

Expand Down