Skip to content

Commit

Permalink
Merge branch 'master' of github.com:jenkinsci/nomad-plugin into upstr…
Browse files Browse the repository at this point in the history
…eam-master

* 'master' of github.com:jenkinsci/nomad-plugin:
  restrict visibility of attribute name to protected (jenkinsci#45)
  [maven-release-plugin] prepare for next development iteration
  [maven-release-plugin] prepare release v0.6.1
  revert POST on doCheckName() (jenkinsci#44)
  [maven-release-plugin] prepare for next development iteration
  [maven-release-plugin] prepare release v0.6.0
  add JENKINS_TUNNEL (jenkinsci#33)
  [maven-release-plugin] prepare for next development iteration
  [maven-release-plugin] prepare release v0.5.1
  add Jenkinsfile
  bump version
  commiting fix to SECURITY-1058 (jenkinsci#42)
  • Loading branch information
aphill70 committed May 28, 2019
2 parents 93123b1 + fe2cc94 commit 70e19b1
Show file tree
Hide file tree
Showing 18 changed files with 326 additions and 79 deletions.
2 changes: 2 additions & 0 deletions Jenkinsfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
/* `buildPlugin` step provided by: https://github.com/jenkins-infra/pipeline-library */
buildPlugin()
17 changes: 10 additions & 7 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,12 @@
<parent>
<groupId>org.jenkins-ci.plugins</groupId>
<artifactId>plugin</artifactId>
<version>2.14</version>
<version>3.9</version>
<relativePath />
</parent>

<artifactId>nomad</artifactId>
<version>0.5-SNAPSHOT</version>
<version>0.6.2-SNAPSHOT</version>
<packaging>hpi</packaging>

<name>Nomad Plugin</name>
Expand All @@ -27,12 +27,15 @@

<scm>
<connection>scm:git:ssh://github.com/jenkinsci/nomad-plugin.git</connection>
<developerConnection>scm:git:ssh://git@github.com/jenkinsci/nomad-plugin.git</developerConnection>
<developerConnection>scm:git:ssh://git@github.com/jenkinsci/nomad-plugin.git
</developerConnection>
<url>https://github.com/jenkinsci/nomad-plugin</url>
<tag>nomad-0.3.1</tag>
<tag>v0.6.0</tag>
</scm>

<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<java.level>8</java.level>
<findbugs.failOnError>false</findbugs.failOnError>
</properties>

Expand Down Expand Up @@ -62,17 +65,17 @@
<dependency>
<groupId>com.squareup.okhttp3</groupId>
<artifactId>okhttp</artifactId>
<version>3.2.0</version>
<version>3.10.0</version>
</dependency>
<dependency>
<groupId>com.google.code.gson</groupId>
<artifactId>gson</artifactId>
<version>2.6.2</version>
<version>2.8.4</version>
</dependency>
<dependency>
<groupId>org.json</groupId>
<artifactId>json</artifactId>
<version>20090211</version>
<version>20180130</version>
</dependency>

<!-- TESTS -->
Expand Down
30 changes: 30 additions & 0 deletions src/main/java/org/jenkinsci/plugins/nomad/Api/Network.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
package org.jenkinsci.plugins.nomad.Api;

import java.util.List;

public class Network {

private Integer MBits;
private List<Port> ReservedPorts;

public Network(Integer mbits, List<Port> reservedPorts) {
MBits = mbits;
ReservedPorts = reservedPorts;
}

public Integer getMBits() {
return MBits;
}

public void setMBits(Integer MBits) {
this.MBits = MBits;
}

public List<Port> getReservedPorts() {
return ReservedPorts;
}

public void setReservedPorts(List<Port> reservedPorts) {
this.ReservedPorts = reservedPorts;
}
}
35 changes: 35 additions & 0 deletions src/main/java/org/jenkinsci/plugins/nomad/Api/Port.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
package org.jenkinsci.plugins.nomad.Api;

import org.jenkinsci.plugins.nomad.NomadPortTemplate;

public class Port {

private String Label;
private Integer Value;

public Port(String label, Integer value) {
this.Label = label;
this.Value = value;
}

public Port(NomadPortTemplate template) {
this.Label = template.getLabel();
this.Value = Integer.parseInt(template.getValue());
}

public String getLabel() {
return Label;
}

public void setLabel(String label) {
this.Label = label;
}

public Integer getValue() {
return Value;
}

public void setValue(Integer value) {
this.Value = value;
}
}
21 changes: 21 additions & 0 deletions src/main/java/org/jenkinsci/plugins/nomad/Api/PortGroup.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package org.jenkinsci.plugins.nomad.Api;

import org.jenkinsci.plugins.nomad.NomadPortTemplate;

import java.util.ArrayList;
import java.util.List;

public class PortGroup {

private List<Port> ports = new ArrayList<>();

public PortGroup(List<? extends NomadPortTemplate> portTemplate) {
for (NomadPortTemplate template : portTemplate) {
ports.add(new Port(template));
}
}

public List<Port> getPorts() {
return ports;
}
}
21 changes: 17 additions & 4 deletions src/main/java/org/jenkinsci/plugins/nomad/Api/Resource.java
Original file line number Diff line number Diff line change
@@ -1,12 +1,25 @@
package org.jenkinsci.plugins.nomad.Api;

import java.util.List;

public class Resource {

private Integer CPU;
private Integer MemoryMB;

public Resource(Integer CPU, Integer memoryMB) {
private List<Network> Networks;

public Resource(Integer CPU, Integer memoryMB, List<Network> networks) {
this.CPU = CPU;
MemoryMB = memoryMB;
this.MemoryMB = memoryMB;
this.Networks = networks;
}

public List<Network> getNetworks() {
return Networks;
}

public void setNetworks(List<Network> networks) {
this.Networks = networks;
}

public Integer getCPU() {
Expand All @@ -22,6 +35,6 @@ public Integer getMemoryMB() {
}

public void setMemoryMB(Integer memoryMB) {
MemoryMB = memoryMB;
this.MemoryMB = memoryMB;
}
}
53 changes: 39 additions & 14 deletions src/main/java/org/jenkinsci/plugins/nomad/NomadApi.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,11 @@
import com.google.gson.Gson;
import com.google.gson.JsonObject;
import hudson.Util;
import okhttp3.*;
import okhttp3.MediaType;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.RequestBody;
import org.apache.commons.lang.StringUtils;
import org.jenkinsci.plugins.nomad.Api.*;

import java.io.IOException;
Expand All @@ -12,8 +16,6 @@
import java.util.Map;
import java.util.logging.Level;
import java.util.logging.Logger;
import org.apache.commons.lang.StringUtils;

import java.util.List;

public final class NomadApi {
Expand Down Expand Up @@ -72,13 +74,8 @@ public void stopSlave(String slaveName) {
private Map<String,Object> buildDriverConfig(String name, String secret, NomadSlaveTemplate template) {
Map<String,Object> driverConfig = new HashMap<>();

ArrayList<String> args = new ArrayList<>();
args.add("-jnlpUrl");

args.add(Util.ensureEndsWith(template.getCloud().getJenkinsUrl(), "/") + "computer/" + name + "/slave-agent.jnlp");

if (template.getUsername() != null && !template.getUsername().isEmpty()) {
Map<String,String> authConfig = new HashMap<>();
Map<String, String> authConfig = new HashMap<>();
authConfig.put("username", template.getUsername());
authConfig.put("password", template.getPassword());

Expand All @@ -88,10 +85,30 @@ private Map<String,Object> buildDriverConfig(String name, String secret, NomadSl
driverConfig.put("auth", credentials);
}

// java -cp /local/slave.jar hudson.remoting.jnlp.Main --help
ArrayList<String> args = new ArrayList<>();
args.add("-headless");

if (!template.getCloud().getJenkinsUrl().isEmpty()) {
args.add("-url");
args.add(template.getCloud().getJenkinsUrl());
}

if (!template.getCloud().getJenkinsTunnel().isEmpty()) {
args.add("-tunnel");
args.add(template.getCloud().getJenkinsTunnel());
}

if (!template.getRemoteFs().isEmpty()) {
args.add("-workDir");
args.add(Util.ensureEndsWith(template.getRemoteFs(), "/"));
}

// java -cp /local/slave.jar [options...] <secret key> <agent name>
if (!secret.isEmpty()) {
args.add("-secret");
args.add(secret);
}
args.add(name);

if (template.getDriver().equals("java")) {
driverConfig.put("jar_path", "/local/slave.jar");
Expand All @@ -102,16 +119,18 @@ private Map<String,Object> buildDriverConfig(String name, String secret, NomadSl
if (!prefixCmd.isEmpty())
{
driverConfig.put("command", "/bin/bash");
String argString = prefixCmd + "; java -jar /local/slave.jar ";
String argString =
prefixCmd + "; java -cp /local/slave.jar hudson.remoting.jnlp.Main -headless ";
argString += StringUtils.join(args, " ");
args.clear();
args.add("-c");
args.add(argString);
}
else {
driverConfig.put("command", "java");
args.add(0, "-jar");
args.add(0, "-cp");
args.add(1, "/local/slave.jar");
args.add(2, "hudson.remoting.jnlp.Main");
}
driverConfig.put("image", template.getImage());

Expand All @@ -134,15 +153,21 @@ String buildSlaveJob(
String secret,
NomadSlaveTemplate template
) {
PortGroup portGroup = new PortGroup(template.getPorts());
Network network = new Network(1, portGroup.getPorts());

ArrayList<Network> networks = new ArrayList<>(1);
networks.add(network);

Task task = new Task(
"jenkins-slave",
template.getDriver(),
template.getSwitchUser(),
buildDriverConfig(name, secret,template),
buildDriverConfig(name, secret, template),
new Resource(
template.getCpu(),
template.getMemory()
template.getMemory(),
networks
),
new LogConfig(1, 10),
new Artifact[]{
Expand Down
Loading

0 comments on commit 70e19b1

Please sign in to comment.