Skip to content
This repository was archived by the owner on Jan 17, 2024. It is now read-only.

Commit 334b1c2

Browse files
committed
Merge remote-tracking branch 'origin/master' into workspace-override
2 parents 018e981 + 943fde0 commit 334b1c2

File tree

5 files changed

+61
-14
lines changed

5 files changed

+61
-14
lines changed

src/main/java/com/gpuopenanalytics/jenkins/remotedocker/DockerLauncher.java

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -162,6 +162,7 @@ private ArgumentListBuilder getlaunchArgs(DockerConfiguration config,
162162
//TODO Set name? Maybe with build.toString().replaceAll("^\\w", "_")
163163
ArgumentListBuilder args = new ArgumentListBuilder()
164164
.add("run", "-t", "-d")
165+
.add("--name", Utils.resolveVariables(this, "$BUILD_TAG"))
165166
//Add bridge network for internet access
166167
.add("--network", "bridge");
167168
//Add inter-container network if needed
@@ -299,11 +300,14 @@ public Proc dockerExec(Launcher.ProcStarter starter,
299300
* @throws IOException
300301
* @throws InterruptedException
301302
*/
302-
public void tearDown() throws IOException, InterruptedException {
303-
boolean exception = false;
303+
public void tearDown(boolean removeContainers) throws IOException, InterruptedException {
304304
for (String containerId : containerIds) {
305-
ArgumentListBuilder args = new ArgumentListBuilder()
306-
.add("rm", "-f", containerId);
305+
ArgumentListBuilder args = new ArgumentListBuilder();
306+
if (removeContainers) {
307+
args.add("rm", "-f", containerId);
308+
} else {
309+
args.add("stop", containerId);
310+
}
307311

308312
ByteArrayOutputStream out = new ByteArrayOutputStream();
309313
int status = executeCommand(args)
@@ -312,7 +316,9 @@ public void tearDown() throws IOException, InterruptedException {
312316
.join();
313317

314318
if (status != 0) {
315-
listener.error("Failed to remove container %s", containerId);
319+
listener.error("Failed to %s container %s",
320+
removeContainers ? "remove" : "stop",
321+
containerId);
316322
}
317323
}
318324
if (network.isPresent()) {

src/main/java/com/gpuopenanalytics/jenkins/remotedocker/RemoteDockerBuildWrapper.java

Lines changed: 19 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@
4141
import net.sf.json.JSONObject;
4242
import org.apache.commons.lang.StringUtils;
4343
import org.kohsuke.stapler.DataBoundConstructor;
44+
import org.kohsuke.stapler.DataBoundSetter;
4445
import org.kohsuke.stapler.StaplerRequest;
4546

4647
import java.io.IOException;
@@ -61,6 +62,7 @@ public class RemoteDockerBuildWrapper extends BuildWrapper {
6162

6263
private boolean debug;
6364
private String workspaceOverride;
65+
private Boolean removeContainers = true;
6466
private AbstractDockerConfiguration dockerConfiguration;
6567
private List<SideDockerConfiguration> sideDockerConfigurations;
6668

@@ -86,6 +88,15 @@ public String getWorkspaceOverride() {
8688
return workspaceOverride;
8789
}
8890

91+
@DataBoundSetter
92+
public void setRemoveContainers(Boolean removeContainers) {
93+
this.removeContainers = removeContainers;
94+
}
95+
96+
public Boolean isRemoveContainers() {
97+
return removeContainers != null ? removeContainers : true;
98+
}
99+
89100
public AbstractDockerConfiguration getDockerConfiguration() {
90101
return dockerConfiguration;
91102
}
@@ -117,13 +128,13 @@ public Environment setUp(AbstractBuild build,
117128
build.addAction(new DockerAction());
118129
try {
119130
((DockerLauncher) launcher).launchContainers();
120-
return new DockerEnvironment((DockerLauncher) launcher);
131+
return new DockerEnvironment((DockerLauncher) launcher,
132+
removeContainers);
121133
} catch (IOException | InterruptedException e) {
122134
//Attempt tearDown in case we partially started some containers
123-
((DockerLauncher) launcher).tearDown();
135+
((DockerLauncher) launcher).tearDown(true);
124136
throw e;
125137
}
126-
127138
}
128139

129140
/**
@@ -132,15 +143,18 @@ public Environment setUp(AbstractBuild build,
132143
private class DockerEnvironment extends BuildWrapper.Environment {
133144

134145
private DockerLauncher launcher;
146+
private boolean removeContainers;
135147

136-
public DockerEnvironment(DockerLauncher launcher) {
148+
public DockerEnvironment(DockerLauncher launcher,
149+
boolean removeContainers) {
137150
this.launcher = launcher;
151+
this.removeContainers = removeContainers;
138152
}
139153

140154
@Override
141155
public boolean tearDown(AbstractBuild build,
142156
BuildListener listener) throws IOException, InterruptedException {
143-
this.launcher.tearDown();
157+
this.launcher.tearDown(removeContainers);
144158
return true;
145159
}
146160
}

src/main/java/com/gpuopenanalytics/jenkins/remotedocker/job/DockerImageConfiguration.java

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -88,10 +88,8 @@ public void setupImage(DockerLauncher launcher,
8888
String image = Utils.resolveVariables(launcher, getImage());
8989
args.add("docker", "pull", image);
9090
Launcher.ProcStarter proc = launcher.executeCommand(args)
91-
.stderr(launcher.getListener().getLogger());
92-
if (launcher.isDebug()) {
93-
proc = proc.stdout(launcher.getListener());
94-
}
91+
.stderr(launcher.getListener().getLogger())
92+
.stdout(launcher.getListener());
9593
int status = proc.join();
9694
if (status != 0) {
9795
throw new IOException("Could not pull image: " + image);

src/main/resources/com/gpuopenanalytics/jenkins/remotedocker/RemoteDockerBuildWrapper/config.jelly

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,9 @@
3737
<f:repeatableDeleteButton/>
3838
</f:repeatableProperty>
3939
</f:entry>
40+
<f:entry title="Remove containers" field="removeContainers">
41+
<f:checkbox default="true"/>
42+
</f:entry>
4043
<f:entry title="Debug" field="debug">
4144
<f:checkbox/>
4245
</f:entry>
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
<!--
2+
~ The MIT License
3+
~
4+
~ Copyright (c) 2019, NVIDIA CORPORATION.
5+
~
6+
~ Permission is hereby granted, free of charge, to any person obtaining a copy
7+
~ of this software and associated documentation files (the "Software"), to deal
8+
~ in the Software without restriction, including without limitation the rights
9+
~ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10+
~ copies of the Software, and to permit persons to whom the Software is
11+
~ furnished to do so, subject to the following conditions:
12+
~
13+
~ The above copyright notice and this permission notice shall be included in
14+
~ all copies or substantial portions of the Software.
15+
~
16+
~ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17+
~ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18+
~ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19+
~ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20+
~ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21+
~ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
22+
~ THE SOFTWARE.
23+
-->
24+
<div>
25+
Whether the containers should be removed (<code>docker rm</code>) when the job completes.
26+
</div>

0 commit comments

Comments
 (0)