Skip to content

Commit

Permalink
Indroduce SimpleBuildWrapper that runs and stops node.
Browse files Browse the repository at this point in the history
No tasks executions internally.

Signed-off-by: Kanstantsin Shautsou <[email protected]>
  • Loading branch information
KostyaSha committed Mar 9, 2017
1 parent 07390d9 commit a7abaac
Show file tree
Hide file tree
Showing 37 changed files with 1,846 additions and 271 deletions.
4 changes: 2 additions & 2 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -43,13 +43,13 @@
</distributionManagement>

<properties>
<jenkins.version>1.625.3</jenkins.version>
<jenkins.version>2.19.4</jenkins.version>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<maven-release-plugin.version>2.5.3</maven-release-plugin.version>
<findbugs-maven-plugin.version>3.0.3</findbugs-maven-plugin.version>
<maven-compiler-plugin.version>3.3</maven-compiler-plugin.version>
<maven-javadoc-plugin.version>2.10.3</maven-javadoc-plugin.version>
<hpi-plugin.version>1.119</hpi-plugin.version>
<!--<hpi-plugin.version>1.119</hpi-plugin.version>-->
<stapler.version>1.102</stapler.version>
<jdk.version>1.8</jdk.version>
<java.level>8</java.level>
Expand Down
2 changes: 1 addition & 1 deletion yet-another-docker-its/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -247,7 +247,7 @@
<dependency>
<groupId>org.jenkins-ci.plugins</groupId>
<artifactId>cloud-stats</artifactId>
<version>0.3</version>
<version>0.7</version>
</dependency>
<dependency>
<groupId>org.jenkins-ci.plugins</groupId>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -174,7 +174,9 @@ private void prepareDockerCli() {
clientConfig = createDefaultConfigBuilder()
.build();

dockerCmdExecFactory = new JerseyDockerCmdExecFactory().withConnectTimeout(10 * 1000);
dockerCmdExecFactory = new JerseyDockerCmdExecFactory()
.withConnectTimeout(10 * 1000)
.withReadTimeout(10 * 1000);

dockerClient = DockerClientBuilder.getInstance(clientConfig)
.withDockerCmdExecFactory(dockerCmdExecFactory)
Expand Down
2 changes: 1 addition & 1 deletion yet-another-docker-plugin/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@
<dependency>
<groupId>org.jenkins-ci.plugins</groupId>
<artifactId>cloud-stats</artifactId>
<version>0.5</version>
<version>0.7</version>
</dependency>
<dependency>
<groupId>org.jenkins-ci.plugins</groupId>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import com.github.kostyasha.yad.commons.AbstractCloud;
import com.github.kostyasha.yad.commons.DockerCreateContainer;
import com.github.kostyasha.yad.launcher.DockerComputerLauncher;
import com.github.kostyasha.yad_docker_java.com.github.dockerjava.api.DockerClient;
import com.github.kostyasha.yad_docker_java.com.github.dockerjava.api.command.CreateContainerCmd;
import com.github.kostyasha.yad_docker_java.com.github.dockerjava.api.command.CreateContainerResponse;
Expand Down Expand Up @@ -113,6 +114,7 @@ public synchronized Collection<PlannedNode> provision(@CheckForNull Label label,
LOG.warn("Bad template '{}' in cloud '{}': '{}'. Trying next template...",
t.getDockerContainerLifecycle().getImage(), getDisplayName(), e.getMessage(), e);
tryTemplates.remove(t);

continue;
}

Expand Down Expand Up @@ -200,6 +202,7 @@ private DockerSlave provisionWithWait(DockerSlaveTemplate template, Provisioning
throws IOException, Descriptor.FormException {
final DockerContainerLifecycle dockerContainerLifecycle = template.getDockerContainerLifecycle();
final String imageId = dockerContainerLifecycle.getImage();
final DockerComputerLauncher computerLauncher = template.getLauncher();

//pull image
dockerContainerLifecycle.getPullImage().exec(getClient(), imageId);
Expand Down Expand Up @@ -227,13 +230,13 @@ private DockerSlave provisionWithWait(DockerSlaveTemplate template, Provisioning

String slaveName = String.format("%s-%s", getDisplayName(), containerId.substring(0, 12));

if (template.getLauncher().waitUp(getDisplayName(), template, ir)) {
if (computerLauncher.waitUp(getDisplayName(), template, ir)) {
LOG.debug("Container {} is ready for ssh slave connection", containerId);
} else {
LOG.error("Container {} is not ready for ssh slave connection.", containerId);
}

final ComputerLauncher launcher = template.getLauncher().getPreparedLauncher(getDisplayName(), template, ir);
final ComputerLauncher launcher = computerLauncher.getPreparedLauncher(getDisplayName(), template, ir);
return new DockerSlave(slaveName, nodeDescription, launcher, containerId, template, getDisplayName(), id);
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
package com.github.kostyasha.yad;

import hudson.model.Run;
import hudson.model.TaskListener;
import hudson.remoting.Channel;
import hudson.slaves.AbstractCloudComputer;
import org.jenkinsci.plugins.cloudstats.ProvisioningActivity;
import org.jenkinsci.plugins.cloudstats.TrackedItem;

import javax.annotation.Nonnull;
import javax.annotation.Nullable;
import java.io.IOException;
import java.io.OutputStream;
import java.nio.charset.Charset;

import static java.util.Objects.nonNull;

/**
* @author Kanstantsin Shautsou
*/
public class DockerComputerSingle extends AbstractCloudComputer<DockerSlaveSingle> implements TrackedItem {
private final ProvisioningActivity.Id activityId;
private transient TaskListener listener;
private transient Run run;

public DockerComputerSingle(@Nonnull DockerSlaveSingle slave, @Nonnull ProvisioningActivity.Id activityId) {
super(slave);
this.activityId = activityId;
}

@Override
public TaskListener getListener() {
return nonNull(listener) ? listener : super.getListener();
}

public void setListener(TaskListener listener) {
this.listener = listener;
}

public Run getRun() {
return run;
}

public void setRun(Run run) {
this.run = run;
}

@Override
public void setChannel(Channel channel, OutputStream launchLog, Channel.Listener listener)
throws IOException, InterruptedException {
super.setChannel(channel, launchLog, listener);
}

public boolean isReallyOffline() {
return super.isOffline();
}

@Override
public boolean isOffline() {
// create executors to pick tasks
return false;
}

@Override
public Charset getDefaultCharset() {
// either fails
// java.lang.NullPointerException
// at hudson.model.Run.execute(Run.java:1702)
// at hudson.model.FreeStyleBuild.run(FreeStyleBuild.java:43)
// at hudson.model.ResourceController.execute(ResourceController.java:98)
// at hudson.model.Executor.run(Executor.java:404)
return Charset.forName("UTF-8");
}

@Nullable
@Override
public ProvisioningActivity.Id getId() {
return activityId;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,25 +3,24 @@
import com.cloudbees.plugins.credentials.CredentialsMatchers;
import com.cloudbees.plugins.credentials.CredentialsProvider;
import com.cloudbees.plugins.credentials.common.StandardCredentials;
import com.github.kostyasha.yad.connector.YADockerConnector;
import com.github.kostyasha.yad.other.ConnectorType;
import com.github.kostyasha.yad.utils.CredentialsListBoxModel;
import com.github.kostyasha.yad_docker_java.com.github.dockerjava.api.DockerClient;
import com.github.kostyasha.yad_docker_java.com.github.dockerjava.api.exception.DockerException;
import com.github.kostyasha.yad_docker_java.com.github.dockerjava.api.model.Version;
import com.github.kostyasha.yad_docker_java.com.github.dockerjava.core.DefaultDockerClientConfig;
import com.github.kostyasha.yad_docker_java.com.github.dockerjava.core.RemoteApiVersion;
import com.github.kostyasha.yad_docker_java.com.google.common.base.Preconditions;
import com.github.kostyasha.yad_docker_java.org.apache.commons.lang.StringUtils;
import com.github.kostyasha.yad.other.ConnectorType;
import com.github.kostyasha.yad.utils.CredentialsListBoxModel;
import com.github.kostyasha.yad_docker_java.org.glassfish.jersey.client.ClientProperties;
import com.google.common.base.Throwables;
import edu.umd.cs.findbugs.annotations.SuppressFBWarnings;
import hudson.Extension;
import hudson.model.Describable;
import hudson.model.Descriptor;
import hudson.model.ItemGroup;
import hudson.security.ACL;
import hudson.util.FormValidation;
import hudson.util.ListBoxModel;
import jenkins.model.Jenkins;
import org.apache.commons.lang.builder.EqualsBuilder;
import org.apache.commons.lang.builder.HashCodeBuilder;
import org.kohsuke.stapler.AncestorInPath;
Expand All @@ -30,26 +29,27 @@
import org.kohsuke.stapler.QueryParameter;

import javax.annotation.CheckForNull;
import javax.annotation.Nonnull;
import javax.servlet.ServletException;
import java.io.IOException;
import java.security.GeneralSecurityException;
import java.util.Collections;
import java.util.List;

import static com.github.kostyasha.yad.client.ClientBuilderForConnector.newClientBuilderForConnector;
import static com.github.kostyasha.yad_docker_java.com.github.dockerjava.core.RemoteApiVersion.parseConfig;
import static com.github.kostyasha.yad.other.ConnectorType.NETTY;
import static com.github.kostyasha.yad_docker_java.com.github.dockerjava.core.RemoteApiVersion.parseConfig;
import static hudson.util.FormValidation.ok;
import static hudson.util.FormValidation.warning;
import static org.apache.commons.lang.builder.ToStringBuilder.reflectionToString;
import static org.apache.commons.lang.builder.ToStringStyle.MULTI_LINE_STYLE;

/**
* Settings for connecting to docker.
* Settings for connecting to docker via docker-java configured connection.
*
* @author Kanstantsin Shautsou
*/
public class DockerConnector implements Describable<DockerConnector> {
public class DockerConnector extends YADockerConnector {

@CheckForNull
private String serverUrl;
Expand All @@ -70,6 +70,9 @@ public class DockerConnector implements Describable<DockerConnector> {
@CheckForNull
private Integer connectTimeout;

@CheckForNull
private Integer readTimeout;

@DataBoundConstructor
public DockerConnector(String serverUrl) {
setServerUrl(serverUrl);
Expand Down Expand Up @@ -125,6 +128,18 @@ public void setConnectTimeout(Integer connectTimeout) {
this.connectTimeout = connectTimeout;
}

@CheckForNull
public Integer getReadTimeout() {
return readTimeout;
}

/**
* @see ClientProperties#READ_TIMEOUT
*/
public void setReadTimeout(Integer readTimeout) {
this.readTimeout = readTimeout;
}

public DockerClient getClient() {
if (client == null) {
try {
Expand Down Expand Up @@ -187,14 +202,8 @@ public int hashCode() {
.toHashCode();
}

@Override
public Descriptor<DockerConnector> getDescriptor() {
return (DescriptorImpl) Jenkins.getActiveInstance().getDescriptor(DockerConnector.class);
}


@Extension
public static class DescriptorImpl extends Descriptor<DockerConnector> {
@Extension(ordinal = 100)
public static class DescriptorImpl extends YADockerConnectorDescriptor {

public ListBoxModel doFillCredentialsIdItems(@AncestorInPath ItemGroup context) {
List<StandardCredentials> credentials =
Expand Down Expand Up @@ -222,7 +231,7 @@ public FormValidation doTestConnection(
final DockerClient testClient = newClientBuilderForConnector()
.withConfigBuilder(configBuilder)
.withConnectorType(connectorType)
.withCredentials(credentialsId)
.withCredentialsId(credentialsId)
.withConnectTimeout(connectTimeout)
.build();

Expand Down Expand Up @@ -253,9 +262,10 @@ public FormValidation doCheckApiVersion(@QueryParameter String apiVersion) {
return ok();
}

@Nonnull
@Override
public String getDisplayName() {
return "Docker Connector";
return "Direct Docker Connector";
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,14 @@
* @author Kanstantsin Shautsou
*/
public class DockerOfflineCause extends OfflineCause {
private String message;

public DockerOfflineCause(String message) {
this.message = message;
}

@Override
public String toString() {
return "Shutting down Docker";
return message;
}
}
Loading

0 comments on commit a7abaac

Please sign in to comment.