Skip to content

Commit

Permalink
JENKINS-28646 - Add support for removing volumes while removing a con…
Browse files Browse the repository at this point in the history
…tainer

Add checkbox to remove volumes, default is unchecked
Build steps: RemoveContainer and RemoveAllContainers
Post build steps: Stop and remove container.
  • Loading branch information
npalm committed May 29, 2015
1 parent 3c2295f commit fae73c3
Show file tree
Hide file tree
Showing 6 changed files with 35 additions and 7 deletions.
Expand Up @@ -48,10 +48,12 @@ public String getDisplayName() {
public static class DockerPostBuildStep extends Recorder {

private final String containerIds;
private final boolean removeVolumes;

@DataBoundConstructor
public DockerPostBuildStep(String containerIds) {
this.containerIds = containerIds;
public DockerPostBuildStep(String containerIds, boolean removeVolumes) {
this.containerIds = containerIds;
this.removeVolumes = removeVolumes;
}

public BuildStepMonitor getRequiredMonitorService() {
Expand Down Expand Up @@ -81,7 +83,7 @@ public boolean perform(@SuppressWarnings("rawtypes") AbstractBuild build, Launch
}
}

RemoveCommand removeCommand = new RemoveCommand(containerIds, true);
RemoveCommand removeCommand = new RemoveCommand(containerIds, true, removeVolumes);
removeCommand.execute(build, clog);

return true;
Expand Down
Expand Up @@ -20,8 +20,11 @@
*/
public class RemoveAllCommand extends DockerCommand {

private final boolean removeVolumes;

@DataBoundConstructor
public RemoveAllCommand() {
public RemoveAllCommand(boolean removeVolumes) {
this.removeVolumes = removeVolumes;
}

@Override
Expand All @@ -31,7 +34,7 @@ public void execute(@SuppressWarnings("rawtypes") AbstractBuild build, ConsoleLo
List<Container> containers = client.listContainersCmd().withShowAll(true).exec();
for (Container container : containers) {
client.killContainerCmd(container.getId()).exec();
client.removeContainerCmd((container.getId())).exec();
client.removeContainerCmd((container.getId())).withRemoveVolumes(removeVolumes).exec();
console.logInfo("removed container id " + container.getId());
}
}
Expand Down
@@ -1,11 +1,13 @@
package org.jenkinsci.plugins.dockerbuildstep.cmd;

import com.github.dockerjava.api.command.RemoveContainerCmd;
import hudson.Extension;
import hudson.model.AbstractBuild;

import java.util.Arrays;
import java.util.List;

import org.apache.commons.lang.StringUtils;
import org.jenkinsci.plugins.dockerbuildstep.log.ConsoleLogger;
import org.jenkinsci.plugins.dockerbuildstep.util.Resolver;
import org.kohsuke.stapler.DataBoundConstructor;
Expand All @@ -26,11 +28,13 @@ public class RemoveCommand extends DockerCommand {

private final String containerIds;
private final boolean ignoreIfNotFound;
private final boolean removeVolumes;

@DataBoundConstructor
public RemoveCommand(String containerIds, boolean ignoreIfNotFound) {
public RemoveCommand(String containerIds, boolean ignoreIfNotFound, boolean removeVolumes) {
this.containerIds = containerIds;
this.ignoreIfNotFound = ignoreIfNotFound;
this.removeVolumes = removeVolumes;
}

public String getContainerIds() {
Expand All @@ -41,6 +45,10 @@ public boolean getIgnoreIfNotFound() {
return ignoreIfNotFound;
}

public boolean getRemoveVolumes() {
return removeVolumes;
}

@Override
public void execute(@SuppressWarnings("rawtypes") AbstractBuild build, ConsoleLogger console)
throws DockerException {
Expand All @@ -57,7 +65,7 @@ public void execute(@SuppressWarnings("rawtypes") AbstractBuild build, ConsoleLo
id = id.trim();
try {
client.killContainerCmd(id).exec();
client.removeContainerCmd(id).exec();
client.removeContainerCmd(id).withRemoveVolumes(removeVolumes).exec();
console.logInfo("removed container id " + id);
} catch (NotFoundException e) {
if (!ignoreIfNotFound) {
Expand Down
Expand Up @@ -5,4 +5,10 @@
<f:textbox />
</f:entry>

<f:advanced>
<f:entry field="removeVolumes" title="Remove volumes" description="Remove the volumes associated with the container." >
<f:checkbox />
</f:entry>
</f:advanced>

</j:jelly>
Expand Up @@ -5,4 +5,9 @@
Kill and remove all conatiners
</f:entry>

<f:advanced>
<f:entry field="removeVolumes" title="Remove volumes" description="Remove the volumes associated with the container." >
<f:checkbox />
</f:entry>
</f:advanced>
</j:jelly>
Expand Up @@ -9,6 +9,10 @@
<f:entry field="ignoreIfNotFound" title="Ignore if not found" description="Do not fail this step if any of the containers are not found." >
<f:checkbox />
</f:entry>

<f:entry field="removeVolumes" title="Remove volumes" description="Remove the volumes associated with the container." >
<f:checkbox />
</f:entry>
</f:advanced>

</j:jelly>

0 comments on commit fae73c3

Please sign in to comment.