Skip to content

Commit

Permalink
add regex for stop and remove
Browse files Browse the repository at this point in the history
  • Loading branch information
John Moon committed Nov 30, 2017
1 parent 80d1ff0 commit 68e4c12
Show file tree
Hide file tree
Showing 8 changed files with 180 additions and 12 deletions.
21 changes: 18 additions & 3 deletions src/main/java/io/fabric8/maven/docker/RemoveMojo.java
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@
import io.fabric8.maven.docker.service.QueryService;
import io.fabric8.maven.docker.service.ServiceHub;

import java.util.List;

import org.apache.maven.plugins.annotations.LifecyclePhase;
import org.apache.maven.plugins.annotations.Mojo;
import org.apache.maven.plugins.annotations.Parameter;
Expand All @@ -45,16 +47,29 @@ public class RemoveMojo extends AbstractDockerMojo {
@Parameter(property = "docker.removeAll", defaultValue = "false")
private boolean removeAll;

@Parameter(property = "docker.removeUsingRegex", defaultValue = "false")
private boolean removeUsingRegex;

@Override
protected void executeInternal(ServiceHub hub) throws DockerAccessException {
QueryService queryService = hub.getQueryService();

for (ImageConfiguration image : getResolvedImages()) {
String name = image.getName();
String nameRegex = image.getNameRegex();
if (removeAll || image.isDataImage()) {
if (queryService.hasImage(name)) {
if (hub.getDockerAccess().removeImage(name,true)) {
log.info("%s: Remove",image.getDescription());
if(removeUsingRegex) {
List<String> imageNames = queryService.findImageNamesByRegex(nameRegex);
for(String rName : imageNames) {
if (hub.getDockerAccess().removeImage(rName,true)) {
log.info("%s: Remove",rName);
}
}
} else {
if (queryService.hasImage(name)) {
if (hub.getDockerAccess().removeImage(name,true)) {
log.info("%s: Remove",image.getDescription());
}
}
}
}
Expand Down
25 changes: 22 additions & 3 deletions src/main/java/io/fabric8/maven/docker/StopMojo.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package io.fabric8.maven.docker;

import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.HashSet;
Expand Down Expand Up @@ -55,6 +56,8 @@ public class StopMojo extends AbstractDockerMojo {
@Parameter( property = "docker.sledgeHammer", defaultValue = "false" )
private boolean sledgeHammer;

@Parameter( property = "docker.stopUsingRegex", defaultValue = "false" )
private boolean userRegexForStopping;

@Override
protected void executeInternal(ServiceHub hub) throws MojoExecutionException, DockerAccessException {
Expand Down Expand Up @@ -93,10 +96,26 @@ private List<Container> getContainersToStop(QueryService queryService, ImageConf
RunImageConfiguration.NamingStrategy strategy = image.getRunConfiguration().getNamingStrategy();

if (strategy == RunImageConfiguration.NamingStrategy.alias) {
Container container = queryService.getContainer(image.getAlias());
return container != null ? Collections.singletonList(container) : Collections.<Container>emptyList();
if(userRegexForStopping) {
return queryService.findContainersByRegex(image.getAliasRegex());
}
else {
Container container = queryService.getContainer(image.getAlias());
return container != null ? Collections.singletonList(container) : Collections.<Container>emptyList();
}
} else {
return queryService.getContainersForImage(image.getName());
if(userRegexForStopping) {
List<String> imageNames = queryService.findImageNamesByRegex(image.getNameRegex());
List<Container> containers = new ArrayList<Container>();
for(String imageName : imageNames) {
List<Container> foundContainers = queryService.getContainersForImage(imageName);
containers.addAll(foundContainers);
}
return containers;
}
else {
return queryService.getContainersForImage(image.getName());
}
}
}

Expand Down
17 changes: 17 additions & 0 deletions src/main/java/io/fabric8/maven/docker/access/DockerAccess.java
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,16 @@ public interface DockerAccess {
*/
InspectedContainer getContainer(String containerIdOrName) throws DockerAccessException;

/**
* Get a container
*
* @param containerIdOrName container id or name
* @return <code>ContainerDetails<code> representing the container or null if none could be found
* @throws DockerAccessException if the container could not be inspected
*/
List<Container> findContainerByRegex(String regexContainerIdOrName) throws DockerAccessException;


/**
* Check whether the given name exists as image at the docker daemon
*
Expand All @@ -46,6 +56,13 @@ public interface DockerAccess {
*/
boolean hasImage(String name) throws DockerAccessException;

/**
* Check whether the given name exists as image at the docker daemon
*
* @param name image name to check
* @return true if the image exists
*/
List<String> findImageByRegexName(String nameRegex) throws DockerAccessException;
/**
* Get the image id of a given name or <code>null</code> if no such image exists
*
Expand Down
7 changes: 6 additions & 1 deletion src/main/java/io/fabric8/maven/docker/access/UrlBuilder.java
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,11 @@ public String inspectImage(String name) {
.build();
}

public String listImages() {
return u("images/json").p("all", true)
.build();
}

public String containerLogs(String containerId, boolean follow) {
return u("containers/%s/logs", containerId)
.p("stdout",true)
Expand Down Expand Up @@ -76,7 +81,7 @@ public String inspectContainer(String containerId) {
}

public String listContainers(String ... filter) {
Builder builder = u("containers/json");
Builder builder = u("containers/json").p("all", true);
addFilters(builder, filter);
return builder.build();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import java.io.*;
import java.net.URI;
import java.util.*;
import java.util.regex.Pattern;

import io.fabric8.maven.docker.access.*;
import io.fabric8.maven.docker.access.chunked.BuildJsonResponseHandler;
Expand Down Expand Up @@ -85,7 +86,7 @@ public DockerAccessWithHcClient(String apiVersion,
this.delegate = createHttpClient(new UnixSocketClientBuilder(uri.getPath(), maxConnections, log));
this.urlBuilder = new UrlBuilder(UNIX_URL, apiVersion);
} else if (uri.getScheme().equalsIgnoreCase("npipe")) {
this.delegate = createHttpClient(new NamedPipeClientBuilder(uri.getPath(), maxConnections, log), false);
this.delegate = createHttpClient(new NamedPipeClientBuilder(uri.getPath(), maxConnections, log), false);
this.urlBuilder = new UrlBuilder(NPIPE_URL, apiVersion);
} else {
this.delegate = createHttpClient(new HttpClientBuilder(isSSL(baseUrl) ? certPath : null, maxConnections));
Expand Down Expand Up @@ -285,6 +286,42 @@ public InspectedContainer getContainer(String containerIdOrName) throws DockerAc
}
}

@Override
public List<Container> findContainerByRegex(String regexContainerIdOrName) throws DockerAccessException {
String url = urlBuilder.listContainers();
Pattern p = Pattern.compile(regexContainerIdOrName);
List<Container> containers = new ArrayList<>();
List<String> matchingNames = new ArrayList<>();
try {
HttpBodyAndStatus response = delegate.get(url, new BodyAndStatusResponseHandler(), HTTP_OK, HTTP_NOT_FOUND);
if (response.getStatusCode() == HTTP_NOT_FOUND) {
return containers;
} else {
JSONArray array = new JSONArray(response.getBody());

for (int i = 0; i < array.length(); i++) {
JSONObject container = array.getJSONObject(i);
JSONArray names = container.getJSONArray("Names");
for(int x = 0; x < names.length(); x++) {
String name = names.getString(x);
if(name.startsWith("/")) {
name = name.substring(1);
}
if(p.matcher(name).matches()){
matchingNames.add(name);
}
}
}
for(String name : matchingNames) {
containers.add(getContainer(name));
}
return containers;
}
} catch(Exception e) {
throw new DockerAccessException(e, "Unable to list container regex for [%s]", regexContainerIdOrName);
}
}

private HttpBodyAndStatus inspectContainer(String containerIdOrName) throws DockerAccessException {
try {
String url = urlBuilder.inspectContainer(containerIdOrName);
Expand Down Expand Up @@ -547,7 +584,7 @@ public void shutdown() {
}

ApacheHttpClientDelegate createHttpClient(ClientBuilder builder) throws IOException {
return createHttpClient(builder, true);
return createHttpClient(builder, true);
}

ApacheHttpClientDelegate createHttpClient(ClientBuilder builder, boolean pooled) throws IOException {
Expand Down Expand Up @@ -654,4 +691,43 @@ public Object handleResponse(HttpResponse response) throws IOException {
return null;
}
}

@Override
public List<String> findImageByRegexName(String nameRegex) throws DockerAccessException {
String url = urlBuilder.listImages();
List<String> imageNames = new ArrayList<>();
Pattern p = Pattern.compile(nameRegex);

try {
HttpBodyAndStatus response = delegate.get(url, new BodyAndStatusResponseHandler(), HTTP_OK, HTTP_NOT_FOUND);
if (response.getStatusCode() == HTTP_NOT_FOUND) {
return imageNames;
}
JSONArray imageDetails = new JSONArray(response.getBody());

for(int i=0;i<imageDetails.length();i++) {
JSONObject image = imageDetails.getJSONObject(i);
if(!image.has("RepoTags") || image.isNull("RepoTags")) {
log.debug("Invalid RepoTags found on %s", image.get("Id"));
continue;
}
try{
JSONArray tags = image.getJSONArray("RepoTags");
for(int x=0;x<tags.length();x++) {
String tag = tags.getString(x);
if(p.matcher(tag).matches()) {
imageNames.add(tag);
}
}
}catch(Exception ex) {
log.error("Error processing RepoTags section of %s", image.get("Id"));
ex.printStackTrace();
}
}
return imageNames;

} catch (IOException e) {
throw new DockerAccessException(e, "Unable to find name by regex [%s]", nameRegex);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,15 @@ public class ImageConfiguration implements StartOrderResolver.Resolvable, Serial
@Parameter(required = true)
private String name;

@Parameter
private String nameRegex;

@Parameter
private String alias;

@Parameter
private String aliasRegex;

@Parameter
private RunImageConfiguration run;

Expand Down Expand Up @@ -52,7 +58,7 @@ public void setName(String name) {
}

@Override
public String getAlias() {
public String getAlias() {
return alias;
}

Expand Down Expand Up @@ -211,4 +217,20 @@ public Builder watchConfig(WatchImageConfiguration watchConfig) {
return this;
}
}

public String getNameRegex() {
return nameRegex;
}

public void setNameRegex(String nameRegex) {
this.nameRegex = nameRegex;
}

public String getAliasRegex() {
return aliasRegex;
}

public void setAliasRegex(String aliasRegex) {
this.aliasRegex = aliasRegex;
}
}
14 changes: 14 additions & 0 deletions src/main/java/io/fabric8/maven/docker/service/QueryService.java
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,20 @@ public Container getMandatoryContainer(String containerIdOrName) throws DockerAc
return container;
}

/**
* Find containers by the given regex name
* @param nameRegex of container to lookup
* @return the List of containers found or empty List if no container is available.
* @throws DockerAccessException in case of an remote error
*/
public List<Container> findContainersByRegex(final String regexContainerIdOrName) throws DockerAccessException {
return docker.findContainerByRegex(regexContainerIdOrName);
}

public List<String> findImageNamesByRegex(String nameRegex) throws DockerAccessException {
return docker.findImageByRegexName(nameRegex);
}

/**
* Get a container running for a given container name.
* @param containerIdOrName name of container to lookup
Expand Down
4 changes: 2 additions & 2 deletions src/test/java/io/fabric8/maven/docker/UrlBuilderTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -89,8 +89,8 @@ public void getImage() throws URISyntaxException {
public void listContainers() throws MalformedURLException, UnsupportedEncodingException, URISyntaxException {
UrlBuilder builder = new UrlBuilder("","1.0");

assertEquals(new URI("/1.0/containers/json"), new URI(builder.listContainers()));
assertEquals(new URI("/1.0/containers/json?filters=" + URLEncoder.encode("{\"ancestor\":[\"nginx\"]}","UTF8")),
assertEquals(new URI("/1.0/containers/json?all=1"), new URI(builder.listContainers()));
assertEquals(new URI("/1.0/containers/json?all=1&filters=" + URLEncoder.encode("{\"ancestor\":[\"nginx\"]}","UTF8")),
new URI(builder.listContainers("ancestor", "nginx")));

try {
Expand Down

0 comments on commit 68e4c12

Please sign in to comment.