Skip to content

Commit

Permalink
Support multiple image tags for image removal
Browse files Browse the repository at this point in the history
  • Loading branch information
aharon hacmon authored and davidxia committed Jul 29, 2015
1 parent 82b0826 commit 9e86907
Show file tree
Hide file tree
Showing 4 changed files with 137 additions and 16 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
# Build products and artifacts
.*
!/.gitignore
*.html
.m2-repository/
target/
Expand Down
57 changes: 41 additions & 16 deletions src/main/java/com/spotify/docker/RemoveImageMojo.java
Original file line number Diff line number Diff line change
Expand Up @@ -21,37 +21,62 @@

package com.spotify.docker;

import java.io.IOException;
import com.spotify.docker.client.DockerClient;
import com.spotify.docker.client.DockerException;
import com.spotify.docker.client.ImageNotFoundException;
import com.spotify.docker.client.messages.RemovedImage;
import com.spotify.docker.client.shaded.javax.ws.rs.NotFoundException;

import org.apache.maven.plugin.MojoExecutionException;
import org.apache.maven.plugins.annotations.Mojo;
import org.apache.maven.plugins.annotations.Parameter;

import com.spotify.docker.client.DockerClient;
import com.spotify.docker.client.DockerException;
import com.spotify.docker.client.ImageNotFoundException;
import java.io.IOException;
import java.util.Collections;
import java.util.List;

import static com.google.common.base.Strings.isNullOrEmpty;
import static com.spotify.docker.Utils.parseImageName;

/**
* Removes a docker image.
*/
@Mojo(name = "removeImage")
public class RemoveImageMojo extends AbstractDockerMojo {

/** Name of image to remove. */
@Parameter(property = "imageName", required = true)
private String imageName;
/**
* Name of image to remove.
*/
@Parameter(property = "imageName", required = true)
private String imageName;

protected void execute(DockerClient docker)
throws MojoExecutionException, DockerException, IOException, InterruptedException {
/**
* Additional tags to tag the image with.
*/
@Parameter(property = "dockerImageTags")
private List<String> imageTags;

protected void execute(final DockerClient docker)
throws MojoExecutionException, DockerException, IOException, InterruptedException {
final String imageNameWithoutTag = parseImageName(imageName)[0];
if (imageTags == null) {
imageTags = Collections.singletonList("");
}

getLog().info("Removing -f " + imageName);
for (final String imageTag : imageTags) {
String currImageName = imageNameWithoutTag +
((isNullOrEmpty(imageTag)) ? "" : (":" + imageTag));
getLog().info("Removing -f " + currImageName);

try {
// force the image to be removed but don't remove untagged parents
docker.removeImage(imageName, true, false);
} catch (ImageNotFoundException e) {
getLog().warn("Image " + imageName +
" does not exist and cannot be deleted - ignoring");
try {
// force the image to be removed but don't remove untagged parents
for (RemovedImage removedImage : docker.removeImage(currImageName, true, false)) {
getLog().info("Removed: " + removedImage.imageId());
}
} catch (ImageNotFoundException | NotFoundException e) {
// ignoring 404 errors only
getLog().warn("Image " + imageName + " doesn't exist and cannot be deleted - ignoring");
}
}
}
}
43 changes: 43 additions & 0 deletions src/test/java/com/spotify/docker/RemoveImageMojoTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,15 @@
package com.spotify.docker;

import com.spotify.docker.client.DockerClient;
import com.spotify.docker.client.DockerException;
import com.spotify.docker.client.ImageNotFoundException;
import com.spotify.docker.client.messages.RemovedImage;

import org.apache.maven.plugin.testing.AbstractMojoTestCase;
import org.mockito.Mockito;

import java.io.File;
import java.util.ArrayList;

import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.verify;
Expand All @@ -44,4 +49,42 @@ public void testRemoveImage() throws Exception {
verify(docker).removeImage("imageToRemove", true, false);
}

public void testRemoveMissingImage() throws Exception {
final File pom = getTestFile("src/test/resources/pom-removeImage.xml");
assertNotNull("Null pom.xml", pom);
assertTrue("pom.xml does not exist", pom.exists());

final RemoveImageMojo mojo = (RemoveImageMojo) lookupMojo("removeImage", pom);
assertNotNull(mojo);
final DockerClient docker = mock(DockerClient.class);
Mockito.when(docker.removeImage("imageToRemove", true, false)).thenThrow(new ImageNotFoundException("imageToRemove"));
try {
mojo.execute(docker);
verify(docker).removeImage("imageToRemove", true, false);
}
catch (DockerException e){
assertFalse("image to remove was missing", e instanceof ImageNotFoundException);
}
}

public void testRemoveImageWithTags() throws Exception {
final File pom = getTestFile("src/test/resources/pom-removeMultipleImages.xml");
assertNotNull("Null pom.xml", pom);
assertTrue("pom.xml does not exist", pom.exists());

final RemoveImageMojo mojo = (RemoveImageMojo) lookupMojo("removeImage", pom);
assertNotNull(mojo);
final DockerClient docker = mock(DockerClient.class);
Mockito.when(docker.removeImage("imageToRemove", true, false)).thenThrow(new ImageNotFoundException("imageToRemove"));
Mockito.when(docker.removeImage("imageToRemove:123456", true, false)).thenThrow(new ImageNotFoundException("imageToRemove:123456"));
Mockito.when(docker.removeImage("imageToRemove:bbbbbbb", true, false)).thenReturn(new ArrayList<RemovedImage>());
try {
mojo.execute(docker);
}
catch (DockerException e){
assertFalse("image to remove was missing", e instanceof ImageNotFoundException);
}
verify(docker).removeImage("imageToRemove:123456", true, false);
verify(docker).removeImage("imageToRemove:bbbbbbb", true, false);
}
}
51 changes: 51 additions & 0 deletions src/test/resources/pom-removeMultipleImages.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>

<name>Docker Maven Plugin Test Pom</name>
<groupId>com.spotify</groupId>
<artifactId>docker-maven-plugin-test</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>

<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>

<dependencies>
<dependency>
<groupId>com.google.guava</groupId>
<artifactId>guava</artifactId>
<version>15.0</version>
</dependency>
</dependencies>

<build>
<plugins>
<plugin>
<groupId>com.spotify</groupId>
<artifactId>docker-maven-plugin</artifactId>
<version>0.1-SNAPSHOT</version>
<configuration>
<dockerHost>http://host:2375</dockerHost>
<imageName>imageToRemove</imageName>
<imageTags>
<tag>123456</tag>
<tag>bbbbbbb</tag>
</imageTags>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.3</version>
<configuration>
<source>1.7</source>
<target>1.7</target>
</configuration>
</plugin>
</plugins>
</build>
</project>

0 comments on commit 9e86907

Please sign in to comment.