Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
[JENKINS-33226] add options to configure runtime constraints
  • Loading branch information
tcollignon committed Feb 29, 2016
1 parent 6bdc638 commit 3cf26ff
Show file tree
Hide file tree
Showing 6 changed files with 37 additions and 5 deletions.
Expand Up @@ -173,7 +173,7 @@ public void kill(String container) throws IOException, InterruptedException {
throw new RuntimeException("Failed to remove docker container "+container);
}

public String runDetached(String image, String workdir, Map<String, String> volumes, Map<Integer, Integer> ports, Map<String, String> links, EnvVars environment, Set sensitiveBuildVariables, String net, String... command) throws IOException, InterruptedException {
public String runDetached(String image, String workdir, Map<String, String> volumes, Map<Integer, Integer> ports, Map<String, String> links, EnvVars environment, Set sensitiveBuildVariables, String net, String memory, String cpu, String... command) throws IOException, InterruptedException {

String docker0 = getDocker0Ip(launcher, image);

Expand All @@ -198,6 +198,14 @@ public String runDetached(String image, String workdir, Map<String, String> volu
args.add("--net", net);
}

if (StringUtils.isNotBlank(memory)) {
args.add("--memory", memory);
}

if (StringUtils.isNotBlank(cpu)) {
args.add("--cpu-shares", cpu);
}

if (!"host".equals(net)){
//--add-host and --net=host are incompatible
args.add("--add-host", "dockerhost:"+docker0);
Expand Down
Expand Up @@ -69,11 +69,15 @@ public class DockerBuildWrapper extends BuildWrapper {

private String net;

private String memory;

private String cpu;

@DataBoundConstructor
public DockerBuildWrapper(DockerImageSelector selector, String dockerInstallation, DockerServerEndpoint dockerHost, String dockerRegistryCredentials, boolean verbose, boolean privileged,
List<Volume> volumes, String group, String command,
boolean forcePull,
String net) {
String net, String memory, String cpu) {
this.selector = selector;
this.dockerInstallation = dockerInstallation;
this.dockerHost = dockerHost;
Expand All @@ -85,6 +89,8 @@ public DockerBuildWrapper(DockerImageSelector selector, String dockerInstallatio
this.command = command;
this.forcePull = forcePull;
this.net = net;
this.memory = memory;
this.cpu = cpu;
}

public DockerImageSelector getSelector() {
Expand Down Expand Up @@ -129,6 +135,10 @@ public boolean isForcePull() {

public String getNet() { return net;}

public String getMemory() { return memory;}

public String getCpu() { return cpu;}

@Override
public Launcher decorateLauncher(final AbstractBuild build, final Launcher launcher, final BuildListener listener) throws IOException, InterruptedException, Run.RunnerAbortedException {
final Docker docker = new Docker(dockerHost, dockerInstallation, dockerRegistryCredentials, build, launcher, listener, verbose, privileged);
Expand Down Expand Up @@ -201,7 +211,7 @@ private String startBuildContainer(BuiltInContainer runInContainer, AbstractBuil

return runInContainer.getDocker().runDetached(runInContainer.image, workdir,
runInContainer.getVolumes(build), runInContainer.getPortsMap(), links,
environment, build.getSensitiveBuildVariables(), net,
environment, build.getSensitiveBuildVariables(), net, memory, cpu,
command); // Command expected to hung until killed

} catch (InterruptedException e) {
Expand Down
Expand Up @@ -64,6 +64,12 @@ THE SOFTWARE.
<f:entry field="net" title="Network bridge">
<f:textbox default="bridge"/>
</f:entry>
<f:entry field="memory" title="Memory limit">
<f:textbox/>
</f:entry>
<f:entry field="cpu" title="CPU shares">
<f:textbox/>
</f:entry>
</f:advanced>

</f:nested>
Expand Down
@@ -0,0 +1,4 @@
This advanced option let you configure the CPU shares constraint.
You should not change this option until you have some advanced requirements.

See <a href="https://docs.docker.com/engine/reference/run/#cpu-share-constraint">container CPU share constraints</a> documentation for more info.
@@ -0,0 +1,4 @@
This advanced option let you configure the limit memory constraint.
You should not change this option until you have some advanced requirements.

See <a href="https://docs.docker.com/engine/reference/run/#user-memory-constraints">container memory constraints</a> documentation for more info.
Expand Up @@ -33,7 +33,7 @@ public void run_inside_pulled_container() throws Exception {
project.getBuildWrappersList().add(
new DockerBuildWrapper(
new PullDockerImageSelector("ubuntu:14.04"),
"", new DockerServerEndpoint("", ""), "", true, false, Collections.<Volume>emptyList(), null, "cat", false, "bridge")
"", new DockerServerEndpoint("", ""), "", true, false, Collections.<Volume>emptyList(), null, "cat", false, "bridge", null, null)
);
project.getBuildersList().add(new Shell("lsb_release -a"));

Expand All @@ -52,7 +52,7 @@ public void run_inside_built_container() throws Exception {
project.getBuildWrappersList().add(
new DockerBuildWrapper(
new DockerfileImageSelector(".", "$WORKSPACE/Dockerfile"),
"", new DockerServerEndpoint("", ""), "", true, false, Collections.<Volume>emptyList(), null, "cat", false, "bridge")
"", new DockerServerEndpoint("", ""), "", true, false, Collections.<Volume>emptyList(), null, "cat", false, "bridge", null, null)
);
project.getBuildersList().add(new Shell("lsb_release -a"));

Expand Down

0 comments on commit 3cf26ff

Please sign in to comment.