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

Commit 0d31feb

Browse files
committed
FEA Option to stop instead of remove containers
1 parent 732131d commit 0d31feb

File tree

4 files changed

+56
-10
lines changed

4 files changed

+56
-10
lines changed

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

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -289,11 +289,14 @@ public Proc dockerExec(Launcher.ProcStarter starter,
289289
* @throws IOException
290290
* @throws InterruptedException
291291
*/
292-
public void tearDown() throws IOException, InterruptedException {
293-
boolean exception = false;
292+
public void tearDown(boolean removeContainers) throws IOException, InterruptedException {
294293
for (String containerId : containerIds) {
295-
ArgumentListBuilder args = new ArgumentListBuilder()
296-
.add("rm", "-f", containerId);
294+
ArgumentListBuilder args = new ArgumentListBuilder();
295+
if (removeContainers) {
296+
args.add("rm", "-f", containerId);
297+
} else {
298+
args.add("stop", containerId);
299+
}
297300

298301
ByteArrayOutputStream out = new ByteArrayOutputStream();
299302
int status = executeCommand(args)
@@ -302,7 +305,9 @@ public void tearDown() throws IOException, InterruptedException {
302305
.join();
303306

304307
if (status != 0) {
305-
listener.error("Failed to remove container %s", containerId);
308+
listener.error("Failed to %s container %s",
309+
removeContainers ? "remove" : "stop",
310+
containerId);
306311
}
307312
}
308313
if (network.isPresent()) {

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

Lines changed: 17 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@
4040
import jenkins.model.Jenkins;
4141
import net.sf.json.JSONObject;
4242
import org.kohsuke.stapler.DataBoundConstructor;
43+
import org.kohsuke.stapler.DataBoundSetter;
4344
import org.kohsuke.stapler.StaplerRequest;
4445

4546
import java.io.IOException;
@@ -56,6 +57,7 @@
5657
public class RemoteDockerBuildWrapper extends BuildWrapper {
5758

5859
private boolean debug;
60+
private Boolean removeContainers = true;
5961
private AbstractDockerConfiguration dockerConfiguration;
6062
private List<SideDockerConfiguration> sideDockerConfigurations;
6163

@@ -74,6 +76,15 @@ public boolean isDebug() {
7476
return debug;
7577
}
7678

79+
@DataBoundSetter
80+
public void setRemoveContainers(Boolean removeContainers) {
81+
this.removeContainers = removeContainers;
82+
}
83+
84+
public Boolean isRemoveContainers() {
85+
return removeContainers != null ? removeContainers : true;
86+
}
87+
7788
public AbstractDockerConfiguration getDockerConfiguration() {
7889
return dockerConfiguration;
7990
}
@@ -96,13 +107,12 @@ public Environment setUp(AbstractBuild build,
96107
build.addAction(new DockerAction());
97108
try {
98109
((DockerLauncher) launcher).launchContainers();
99-
return new DockerEnvironment((DockerLauncher) launcher);
110+
return new DockerEnvironment((DockerLauncher) launcher, removeContainers);
100111
} catch (IOException | InterruptedException e) {
101112
//Attempt tearDown in case we partially started some containers
102-
((DockerLauncher) launcher).tearDown();
113+
((DockerLauncher) launcher).tearDown(true);
103114
throw e;
104115
}
105-
106116
}
107117

108118
/**
@@ -111,15 +121,17 @@ public Environment setUp(AbstractBuild build,
111121
private class DockerEnvironment extends BuildWrapper.Environment {
112122

113123
private DockerLauncher launcher;
124+
private boolean removeContainers;
114125

115-
public DockerEnvironment(DockerLauncher launcher) {
126+
public DockerEnvironment(DockerLauncher launcher, boolean removeContainers) {
116127
this.launcher = launcher;
128+
this.removeContainers=removeContainers;
117129
}
118130

119131
@Override
120132
public boolean tearDown(AbstractBuild build,
121133
BuildListener listener) throws IOException, InterruptedException {
122-
this.launcher.tearDown();
134+
this.launcher.tearDown(removeContainers);
123135
return true;
124136
}
125137
}

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)