diff --git a/pom.xml b/pom.xml index 89e3ac65..f6a96bb1 100644 --- a/pom.xml +++ b/pom.xml @@ -69,6 +69,11 @@ gson 2.6.2 + + org.json + json + 20090211 + diff --git a/src/main/java/org/jenkinsci/plugins/nomad/Api/Constraint.java b/src/main/java/org/jenkinsci/plugins/nomad/Api/Constraint.java new file mode 100644 index 00000000..55578430 --- /dev/null +++ b/src/main/java/org/jenkinsci/plugins/nomad/Api/Constraint.java @@ -0,0 +1,52 @@ +package org.jenkinsci.plugins.nomad.Api; + +import org.jenkinsci.plugins.nomad.NomadConstraintTemplate; +import java.util.List; + +public class Constraint { + private String LTarget; + private String Operand; + private String RTarget; + + public Constraint ( + String ltarget, + String operand, + String rtarget + ) { + LTarget = ltarget; + Operand = operand; + RTarget = rtarget; + } + + public Constraint ( + NomadConstraintTemplate nomadConstraintTemplate + ) { + LTarget = nomadConstraintTemplate.getLtarget(); + Operand = nomadConstraintTemplate.getOperand(); + RTarget = nomadConstraintTemplate.getRtarget(); + } + + public String getLtarget() { + return LTarget; + } + + public void setLtarget(String ltarget) { + LTarget = ltarget; + } + + public String getOperand() { + return Operand; + } + + public void setOperand(String operand) { + Operand = operand; + } + + public String getRtarget() { + return RTarget; + } + + public void setRtarget(String rtarget) { + RTarget = rtarget; + } +} \ No newline at end of file diff --git a/src/main/java/org/jenkinsci/plugins/nomad/Api/ConstraintGroup.java b/src/main/java/org/jenkinsci/plugins/nomad/Api/ConstraintGroup.java new file mode 100644 index 00000000..08de0449 --- /dev/null +++ b/src/main/java/org/jenkinsci/plugins/nomad/Api/ConstraintGroup.java @@ -0,0 +1,31 @@ +package org.jenkinsci.plugins.nomad.Api; +import java.util.*; +import org.jenkinsci.plugins.nomad.NomadConstraintTemplate; +import com.google.gson.Gson; +import com.google.gson.JsonObject; + +public class ConstraintGroup { + private List constraints = new ArrayList(); + + public ConstraintGroup ( + List constraintTemplate + ) { + Iterator constraintIterator = constraintTemplate.iterator(); + while (constraintIterator.hasNext()) { + NomadConstraintTemplate nextTemplate = constraintIterator.next(); + constraints.add(new Constraint(nextTemplate)); + } + } + + public List getConstraints() { + Iterator constraintIterator = constraints.iterator(); + + List Constraints = new ArrayList(); + + while (constraintIterator.hasNext()) { + Constraint nextConstraint = constraintIterator.next(); + Constraints.add(nextConstraint); + } + return Constraints; + } +} \ No newline at end of file diff --git a/src/main/java/org/jenkinsci/plugins/nomad/Api/Job.java b/src/main/java/org/jenkinsci/plugins/nomad/Api/Job.java index b28f8257..ce9049eb 100644 --- a/src/main/java/org/jenkinsci/plugins/nomad/Api/Job.java +++ b/src/main/java/org/jenkinsci/plugins/nomad/Api/Job.java @@ -1,5 +1,8 @@ package org.jenkinsci.plugins.nomad.Api; +import java.util.List; +import org.jenkinsci.plugins.nomad.NomadConstraintTemplate; + public final class Job { private String ID; @@ -8,6 +11,7 @@ public final class Job { private String Type; private Integer Priority; private String[] Datacenters; + private List Constraints; private TaskGroup[] TaskGroups; public Job( @@ -17,6 +21,7 @@ public Job( String type, Integer priority, String[] datacenters, + List constraints, TaskGroup[] taskGroups) { this.ID = ID; @@ -25,6 +30,7 @@ public Job( Type = type; Priority = priority; Datacenters = datacenters; + Constraints = constraints; TaskGroups = taskGroups; } @@ -84,4 +90,12 @@ public void setTaskGroups(TaskGroup[] taskGroups) { TaskGroups = taskGroups; } + public List getConstraints() { + return Constraints; + } + + public void setConstraints(List constraints) { + Constraints = constraints; + } + } diff --git a/src/main/java/org/jenkinsci/plugins/nomad/NomadApi.java b/src/main/java/org/jenkinsci/plugins/nomad/NomadApi.java index 60d5d376..afbba580 100644 --- a/src/main/java/org/jenkinsci/plugins/nomad/NomadApi.java +++ b/src/main/java/org/jenkinsci/plugins/nomad/NomadApi.java @@ -14,6 +14,8 @@ import java.util.logging.Logger; import org.apache.commons.lang.StringUtils; +import java.util.List; + public final class NomadApi { private static final Logger LOGGER = Logger.getLogger(NomadApi.class.getName()); @@ -149,6 +151,9 @@ String buildSlaveJob( new EphemeralDisk(template.getDisk(), false, false) ); + ConstraintGroup constraintGroup = new ConstraintGroup(template.getConstraints()); + List Constraints = constraintGroup.getConstraints(); + Job job = new Job( name, name, @@ -156,6 +161,7 @@ String buildSlaveJob( "batch", template.getPriority(), template.getDatacenters().split(","), + Constraints, new TaskGroup[]{taskGroup} ); diff --git a/src/main/java/org/jenkinsci/plugins/nomad/NomadConstraintTemplate.java b/src/main/java/org/jenkinsci/plugins/nomad/NomadConstraintTemplate.java new file mode 100644 index 00000000..f986f65d --- /dev/null +++ b/src/main/java/org/jenkinsci/plugins/nomad/NomadConstraintTemplate.java @@ -0,0 +1,80 @@ +package org.jenkinsci.plugins.nomad; + +import hudson.Extension; +import org.kohsuke.stapler.DataBoundConstructor; + +import hudson.Util; +import hudson.model.Describable; +import hudson.model.Descriptor; +import jenkins.model.Jenkins; + +import javax.annotation.Nullable; +import java.util.*; +import java.util.logging.Logger; + +import org.json.JSONObject; + +public class NomadConstraintTemplate implements Describable { + + private final String ltarget; + private final String operand; + private final String rtarget; + + private NomadSlaveTemplate slave; + + @DataBoundConstructor + public NomadConstraintTemplate( + String ltarget, + String operand, + String rtarget + ) { + this.ltarget = ltarget; + this.operand = operand; + this.rtarget = rtarget; + readResolve(); + } + + protected Object readResolve() { + return this; + } + + + @Extension + public static final class DescriptorImpl extends Descriptor { + + public DescriptorImpl() { + load(); + } + + @Override + public String getDisplayName() { + return null; + } + } + + @Override + @SuppressWarnings("unchecked") + public Descriptor getDescriptor() { + return Jenkins.getInstance().getDescriptor(getClass()); + } + + public String getLtarget() { + return ltarget; + } + + public String getOperand() { + return operand; + } + + public String getRtarget() { + return rtarget; + } + + public NomadSlaveTemplate getNomadSlaveTemplate() { + return slave; + } + + public void setNomadSlaveTemplate(NomadSlaveTemplate slave) { + this.slave = slave; + } +} \ No newline at end of file diff --git a/src/main/java/org/jenkinsci/plugins/nomad/NomadSlaveTemplate.java b/src/main/java/org/jenkinsci/plugins/nomad/NomadSlaveTemplate.java index 692265ca..f2a3390e 100644 --- a/src/main/java/org/jenkinsci/plugins/nomad/NomadSlaveTemplate.java +++ b/src/main/java/org/jenkinsci/plugins/nomad/NomadSlaveTemplate.java @@ -11,10 +11,16 @@ import hudson.model.labels.LabelAtom; import jenkins.model.Jenkins; +import java.lang.reflect.Type; +import com.google.gson.reflect.TypeToken; + import javax.annotation.Nullable; import java.util.*; import java.util.logging.Logger; +import com.google.gson.Gson; +import com.google.gson.JsonObject; + public class NomadSlaveTemplate implements Describable { private static final String SLAVE_PREFIX = "jenkins-"; @@ -28,6 +34,7 @@ public class NomadSlaveTemplate implements Describable { private final int disk; private final int priority; private final String labels; + private final List constraints; private final String region; private final String remoteFs; private final String image; @@ -49,6 +56,7 @@ public NomadSlaveTemplate( String memory, String disk, String labels, + List constraints, String remoteFs, String idleTerminationInMinutes, String numExecutors, @@ -72,6 +80,11 @@ public NomadSlaveTemplate( this.mode = mode; this.remoteFs = remoteFs; this.labels = Util.fixNull(labels); + if (constraints == null) { + this.constraints = Collections.emptyList(); + } else { + this.constraints = constraints; + } this.labelSet = Label.parse(labels); this.region = region; this.image = image; @@ -138,6 +151,10 @@ public String getLabels() { return labels; } + public List getConstraints() { + return Collections.unmodifiableList(constraints); + } + public int getIdleTerminationInMinutes() { return idleTerminationInMinutes; } diff --git a/src/main/resources/org/jenkinsci/plugins/nomad/NomadConstraintTemplate/config.jelly b/src/main/resources/org/jenkinsci/plugins/nomad/NomadConstraintTemplate/config.jelly new file mode 100644 index 00000000..09cf60fe --- /dev/null +++ b/src/main/resources/org/jenkinsci/plugins/nomad/NomadConstraintTemplate/config.jelly @@ -0,0 +1,25 @@ + + + + + + + + + + + + + + + + + + + + + + +
+ +
\ No newline at end of file diff --git a/src/main/resources/org/jenkinsci/plugins/nomad/NomadSlaveTemplate/config.jelly b/src/main/resources/org/jenkinsci/plugins/nomad/NomadSlaveTemplate/config.jelly index 1e702978..038b04bf 100644 --- a/src/main/resources/org/jenkinsci/plugins/nomad/NomadSlaveTemplate/config.jelly +++ b/src/main/resources/org/jenkinsci/plugins/nomad/NomadSlaveTemplate/config.jelly @@ -16,6 +16,12 @@ + + + + + + diff --git a/src/main/webapp/help-constraints.html b/src/main/webapp/help-constraints.html new file mode 100644 index 00000000..0da393f1 --- /dev/null +++ b/src/main/webapp/help-constraints.html @@ -0,0 +1,3 @@ +
+ Read the JSON synax and Constraints API pages on nomadproject.io to see how to apply constraints (these apply on the Job level) +
\ No newline at end of file diff --git a/src/test/java/org/jenkinsci/plugins/nomad/NomadApiTest.java b/src/test/java/org/jenkinsci/plugins/nomad/NomadApiTest.java index 1da06a2a..b172f590 100644 --- a/src/test/java/org/jenkinsci/plugins/nomad/NomadApiTest.java +++ b/src/test/java/org/jenkinsci/plugins/nomad/NomadApiTest.java @@ -6,6 +6,8 @@ import java.util.Collections; import static org.junit.Assert.assertTrue; +import java.util.List; +import java.util.ArrayList; /** * @author Yegor Andreenko @@ -13,9 +15,10 @@ public class NomadApiTest { private NomadApi nomadApi = new NomadApi("http://localhost"); + private List constraintTest = new ArrayList(); private NomadSlaveTemplate slaveTemplate = new NomadSlaveTemplate( "300", "256", "100", - null, "remoteFs", "3","1", Node.Mode.NORMAL, + null, constraintTest, "remoteFs", "3","1", Node.Mode.NORMAL, "ams", "0", "image", "dc01", "", "", false, "bridge","" );