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

Task experiment #126

Open
wants to merge 17 commits into
base: master
Choose a base branch
from
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
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 @@ -96,7 +97,7 @@ public synchronized Collection<PlannedNode> provision(@CheckForNull Label label,
LOG.info("Asked to provision load: '{}', for: '{}' label", excessWorkload, label);

List<PlannedNode> r = new ArrayList<>(excessWorkload);
final List<DockerSlaveTemplate> tryTemplates = getTemplates(label);
final List<DockerSlaveTemplate> tryTemplates = getAllTemplates(label);

while (excessWorkload > 0 && !tryTemplates.isEmpty()) {
final DockerSlaveTemplate t = tryTemplates.get(0); // get first
Expand All @@ -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 Expand Up @@ -361,7 +364,7 @@ public DescriptorImpl getDescriptor() {
}

@Extension
public static class DescriptorImpl extends Descriptor<Cloud> {
public static class DescriptorImpl extends AbstractCloudDescriptor {

public FormValidation doCheckName(@QueryParameter String name) {
if (StringUtils.isEmpty(name)) {
Expand All @@ -371,6 +374,7 @@ public FormValidation doCheckName(@QueryParameter String name) {
return FormValidation.ok();
}

@Nonnull
@Override
public String getDisplayName() {
return "Yet Another Docker";
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,23 @@
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.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 +28,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 Down Expand Up @@ -187,14 +186,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 +215,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 +246,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
@@ -0,0 +1,50 @@
package com.github.kostyasha.yad;

import com.github.kostyasha.yad.jobconfig.SlaveJobConfig;
import hudson.Extension;
import hudson.model.Job;
import jenkins.model.OptionalJobProperty;
import org.kohsuke.stapler.DataBoundConstructor;
import org.kohsuke.stapler.DataBoundSetter;

import javax.annotation.Nonnull;

/**
* Property for storing docker specific job settings.
*
* @author Kanstantsin Shautsou
*/
public class DockerJobProperty extends OptionalJobProperty<Job<?, ?>> {

/**
* Any type of configuration for running job in single container.
*/
private SlaveJobConfig slaveJobConfig;

@DataBoundConstructor
public DockerJobProperty() {
}

public SlaveJobConfig getSlaveJobConfig() {
return slaveJobConfig;
}

@DataBoundSetter
public void setSlaveJobConfig(SlaveJobConfig slaveJobConfig) {
this.slaveJobConfig = slaveJobConfig;
}

@Extension
public static class DescriptorImpl extends OptionalJobPropertyDescriptor {
@Override
public boolean isApplicable(Class<? extends Job> jobType) {
return Job.class.isAssignableFrom(jobType);
}

@Nonnull
@Override
public String getDisplayName() {
return "Docker Job Property";
}
}
}
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