Skip to content

Commit

Permalink
Merge pull request fabric8io#62 from krystiannowak/krystiannowak/remo…
Browse files Browse the repository at this point in the history
…ve-image

fabric8io#47 - removeImage Mojo added
  • Loading branch information
davidxia committed Mar 25, 2015
2 parents 46d1199 + 36322e3 commit c613b5d
Show file tree
Hide file tree
Showing 4 changed files with 161 additions and 0 deletions.
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,10 @@ will need to do this binding so the image gets built when maven is run from the
</executions>
</plugin>

To remove the image named `foobar` run the following command:

mvn docker:removeImage -DimageName=foobar

For a complete list of configuration options run:
`mvn com.spotify:docker-maven-plugin:<version>:help -Ddetail=true`

Expand Down
63 changes: 63 additions & 0 deletions src/main/java/com/spotify/docker/RemoveImageMojo.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
/*
* Copyright (c) 2014 Spotify AB.
*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/

package com.spotify.docker;

import java.io.IOException;

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.DockerRequestException;
import com.spotify.docker.client.shaded.javax.ws.rs.NotFoundException;

/**
* 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;

protected void execute(DockerClient docker)
throws MojoExecutionException, DockerException, IOException, InterruptedException {

getLog().info("Removing -f " + imageName);

try {
// force the image to be removed but don't remove untagged parents
docker.removeImage(imageName, true, false);
} catch (DockerRequestException e) {
// ignoring 404 errors only
if (e.getCause() != null && NotFoundException.class.equals(e.getCause().getClass())) {
getLog().warn("Image " + imageName +
" does not exist and cannot be deleted - ignoring");
} else {
throw e;
}
}
}
}
47 changes: 47 additions & 0 deletions src/test/java/com/spotify/docker/RemoveImageMojoTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
/*
* Copyright (c) 2014 Spotify AB.
*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/

package com.spotify.docker;

import com.spotify.docker.client.DockerClient;

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

import java.io.File;

import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.verify;

public class RemoveImageMojoTest extends AbstractMojoTestCase {

public void testRemoveImage() 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);
mojo.execute(docker);
verify(docker).removeImage("imageToRemove", true, false);
}

}
47 changes: 47 additions & 0 deletions src/test/resources/pom-removeImage.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
<?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>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>2.5.1</version>
<configuration>
<source>1.7</source>
<target>1.7</target>
</configuration>
</plugin>
</plugins>
</build>
</project>

0 comments on commit c613b5d

Please sign in to comment.