Skip to content

Commit

Permalink
Rebased #513 to branch integration and refactored to use only a globa…
Browse files Browse the repository at this point in the history
…l fetch parameter.
  • Loading branch information
rhuss committed Jul 12, 2016
2 parents 4693e7e + 02eb9b6 commit 1067e63
Show file tree
Hide file tree
Showing 14 changed files with 86 additions and 59 deletions.
4 changes: 2 additions & 2 deletions src/main/asciidoc/inc/_external-configuration.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -40,8 +40,8 @@ Example:
</image>
----

Given this example configuration a single image configuration is build
up from the following properties, which correspond to corresponding
Given this example configuration a single image configuration is built
up from the following properties, which correspond to the corresponding
values in the `<build>` and `<run>` sections.

.External properties
Expand Down
4 changes: 4 additions & 0 deletions src/main/asciidoc/inc/_global-configuration.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,10 @@ the URL is:
. `unix:///var/run/docker.sock` if it is a readable socket.
| `docker.host`

| *fetchLimit*
| Number of running and stopped containers to return. A value of 0 returns all running and stopped containers. Default is 100.
| `docker.fetchLimit`

| *image*
| In order to temporarily restrict the operation of plugin goals this configuration option can be used. Typically this will be set via the system property `docker.image` when Maven is called. The value can be a single image name (either its alias or full name) or it can be a comma separated list with multiple image names. Any name which doesn't refer an image in the configuration will be ignored.
| `docker.image`
Expand Down
10 changes: 9 additions & 1 deletion src/main/java/io/fabric8/maven/docker/AbstractDockerMojo.java
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,10 @@ public abstract class AbstractDockerMojo extends AbstractMojo implements Context
@Parameter(property = "docker.skip", defaultValue = "false")
private boolean skip;

// Max number of containers to fetch when stopping/removing
@Parameter(property = "docker.fetchLimit", defaultValue = "100")
protected int fetchLimit;

/**
* Whether the usage of docker machine should be skipped competely
*/
Expand Down Expand Up @@ -254,6 +258,7 @@ public List<ImageConfiguration> resolve(ImageConfiguration image) {

// Customization hook for subclasses to influence the final configuration. This method is called
// before initialization and validation of the configuration.
@Override
public List<ImageConfiguration> customizeConfig(List<ImageConfiguration> imageConfigs) {
return imageConfigs;
}
Expand All @@ -266,7 +271,10 @@ private DockerAccess createDockerAccess(String minimalVersion) throws MojoExecut
String dockerUrl = dockerConnectionDetector.extractUrl(dockerHost);
String version = minimalVersion != null ? minimalVersion : API_VERSION;
access = new DockerAccessWithHcClient("v" + version, dockerUrl,
dockerConnectionDetector.getCertPath(certPath), maxConnections, log);
dockerConnectionDetector.getCertPath(certPath),
maxConnections,
fetchLimit,
log);
access.start();
setDockerHostAddressProperty(dockerUrl);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,11 +48,10 @@ public interface DockerAccess {
/**
* List containers
*
* @param limit limit of containers to list
* @return list of <code>Container<code> objects
* @throws DockerAccessException if the containers could not be listed
*/
List<Container> listContainers(int limit) throws DockerAccessException;
List<Container> listContainers() throws DockerAccessException;

/**
* Starts a previously set up exec instance id.
Expand Down
14 changes: 9 additions & 5 deletions src/main/java/io/fabric8/maven/docker/access/UrlBuilder.java
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ public String containerLogs(String containerId, boolean follow) {
.p("follow", follow)
.build();
}

public String createContainer(String name) {
return u("containers/create")
.p("name", name)
Expand All @@ -70,11 +70,15 @@ public String inspectContainer(String containerId) {
return u("containers/%s/json", containerId)
.build();
}

public String listContainers(int limit) {
return u("containers/json")
.p("limit", limit)
.build();
Builder builder = u("containers/json");
if (limit == 0) {
builder.p("all", 1);
} else {
builder.p("limit", limit);
}
return builder.build();
}

public String pullImage(ImageName name, String registry) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -59,19 +59,29 @@ public class DockerAccessWithHcClient implements DockerAccess {
private final ApacheHttpClientDelegate delegate;
private final UrlBuilder urlBuilder;

// limit how many containers to fetch
private final int fetchLimit;

/**
* Create a new access for the given URL
*
* @param baseUrl base URL for accessing the docker Daemon
* @param certPath used to build up a keystore with the given keys and certificates found in this
* directory
* @param maxConnections maximum parallel connections allowed to docker daemon (if a pool is used)
* @param fetchLimit how many containers to fetch with a list operation
* @param log a log handler for printing out logging information
* @paran usePool whether to use a connection bool or not
*/
public DockerAccessWithHcClient(String apiVersion, String baseUrl, String certPath, int maxConnections, Logger log)
public DockerAccessWithHcClient(String apiVersion,
String baseUrl,
String certPath,
int maxConnections,
int fetchLimit,
Logger log)
throws IOException {
this.log = log;
this.fetchLimit = fetchLimit;
URI uri = URI.create(baseUrl);
if (uri.getScheme() == null) {
throw new IllegalArgumentException("The docker access url '" + baseUrl + "' must contain a schema tcp:// or unix://");
Expand Down Expand Up @@ -190,7 +200,7 @@ public void stopContainer(String containerId, int killWait) throws DockerAccessE

@Override
public void buildImage(String image, File dockerArchive, String dockerfileName, boolean forceRemove, boolean noCache,
Map<String, String> buildArgs) throws DockerAccessException {
Map<String, String> buildArgs) throws DockerAccessException {
try {
String url = urlBuilder.buildImage(image, dockerfileName, forceRemove, noCache, buildArgs);
delegate.post(url, dockerArchive, createBuildResponseHandler(), HTTP_OK);
Expand All @@ -200,7 +210,7 @@ public void buildImage(String image, File dockerArchive, String dockerfileName,
}

@Override
public void copyArchive(String containerId,File archive, String targetPath)
public void copyArchive(String containerId, File archive, String targetPath)
throws DockerAccessException {
try {
String url = urlBuilder.copyArchive(containerId, targetPath);
Expand Down Expand Up @@ -236,8 +246,8 @@ public Container inspectContainer(String containerId) throws DockerAccessExcepti
}

@Override
public List<Container> listContainers(int limit) throws DockerAccessException {
String url = urlBuilder.listContainers(limit);
public List<Container> listContainers() throws DockerAccessException {
String url = urlBuilder.listContainers(fetchLimit);

try {
String response = delegate.get(url, HTTP_OK);
Expand Down Expand Up @@ -302,7 +312,7 @@ public void pullImage(String image, AuthConfig authConfig, String registry)

try {
delegate.post(pullUrl, null, createAuthHeader(authConfig),
createPullOrPushResponseHandler(), HTTP_OK);
createPullOrPushResponseHandler(), HTTP_OK);
} catch (IOException e) {
throw new DockerAccessException(e, "Unable to pull '%s'%s", image, (registry != null) ? " from registry '" + registry + "'" : "");
}
Expand All @@ -316,7 +326,7 @@ public void pushImage(String image, AuthConfig authConfig, String registry)
String temporaryImage = tagTemporaryImage(name, registry);
try {
delegate.post(pushUrl, null, createAuthHeader(authConfig),
createPullOrPushResponseHandler(), HTTP_OK);
createPullOrPushResponseHandler(), HTTP_OK);
} catch (IOException e) {
throw new DockerAccessException(e, "Unable to push '%s'%s", image, (registry != null) ? " from registry '" + registry + "'" : "");
} finally {
Expand All @@ -336,7 +346,7 @@ public void tag(String sourceImage, String targetImage, boolean force)
delegate.post(url, HTTP_CREATED);
} catch (IOException e) {
throw new DockerAccessException(e, "Unable to add tag [%s] to image [%s]", targetImage,
sourceImage, e);
sourceImage, e);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -109,15 +109,18 @@ public class RunImageConfiguration {
@Parameter
private WaitConfiguration wait;

@Parameter
private Integer fetchLimit;

@Parameter
private LogConfiguration log;

@Parameter
private RestartPolicy restartPolicy;

@Parameter
private boolean skip = false;

public RunImageConfiguration() { }

public String initAndValidate() {
Expand Down Expand Up @@ -197,6 +200,10 @@ public WaitConfiguration getWaitConfiguration() {
return wait;
}

public Integer getFetchLimit() {
return fetchLimit;
}

public LogConfiguration getLogConfiguration() {
return log;
}
Expand Down Expand Up @@ -224,7 +231,7 @@ public List<String> getDnsSearch() {
public List<String> getExtraHosts() {
return extraHosts;
}

public VolumeConfiguration getVolumeConfiguration() {
return volumes;
}
Expand All @@ -248,7 +255,7 @@ public enum NamingStrategy {
public NamingStrategy getNamingStrategy() {
return namingStrategy == null ? NamingStrategy.none : namingStrategy;
}

public Boolean getPrivileged() {
return privileged;
}
Expand All @@ -260,7 +267,7 @@ public RestartPolicy getRestartPolicy() {
public boolean skip() {
return skip;
}

// ======================================================================================

public static class Builder {
Expand Down Expand Up @@ -387,12 +394,16 @@ public Builder wait(WaitConfiguration wait) {
return this;
}

public Builder fetchLimit(Integer fetchLimit) {
config.fetchLimit = fetchLimit;
return this;
}

public Builder log(LogConfiguration log) {
config.log = log;
return this;
}


public Builder namingStrategy(String namingStrategy) {
config.namingStrategy = namingStrategy == null ?
NamingStrategy.none :
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
package io.fabric8.maven.docker.config.handler.property;/*
*
*
* Copyright 2014 Roland Huss
*
* Licensed under the Apache License, Version 2.0 (the "License");
Expand All @@ -15,8 +15,6 @@
* limitations under the License.
*/

import static io.fabric8.maven.docker.assembly.DockerFileKeyword.WORKDIR;

/**
* Enum holding possible configuration keys
*
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
package io.fabric8.maven.docker.config.handler.property;/*
*
*
* Copyright 2014 Roland Huss
*
* Licensed under the Apache License, Version 2.0 (the "License");
Expand Down Expand Up @@ -40,14 +40,14 @@ public String getType() {
@Override
public List<ImageConfiguration> resolve(ImageConfiguration config, Properties properties) throws IllegalArgumentException {
String prefix = getPrefix(config);

RunImageConfiguration run = extractRunConfiguration(prefix,properties);
BuildImageConfiguration build = extractBuildConfiguration(prefix,properties);
WatchImageConfiguration watch = extractWatchConfig(prefix, properties);

String name = extractName(prefix, properties);
String alias = withPrefix(prefix, ALIAS, properties);

return Collections.singletonList(
new ImageConfiguration.Builder()
.name(name)
Expand Down Expand Up @@ -85,7 +85,7 @@ private BuildImageConfiguration extractBuildConfiguration(String prefix, Propert
}

private RunImageConfiguration extractRunConfiguration(String prefix, Properties properties) {

return new RunImageConfiguration.Builder()
.capAdd(listWithPrefix(prefix, CAP_ADD, properties))
.capDrop(listWithPrefix(prefix, CAP_DROP, properties))
Expand Down Expand Up @@ -120,7 +120,7 @@ private RunImageConfiguration extractRunConfiguration(String prefix, Properties

private AssemblyConfiguration extractAssembly(String prefix, Properties properties) {
return new AssemblyConfiguration.Builder()
.basedir(withPrefix(prefix, ASSEMBLY_BASEDIR, properties))
.basedir(withPrefix(prefix, ASSEMBLY_BASEDIR, properties))
.descriptor(withPrefix(prefix, ASSEMBLY_DESCRIPTOR, properties))
.descriptorRef(withPrefix(prefix, ASSEMBLY_DESCRIPTOR_REF, properties))
.dockerFileDir(withPrefix(prefix, ASSEMBLY_DOCKER_FILE_DIR, properties))
Expand All @@ -131,15 +131,15 @@ private AssemblyConfiguration extractAssembly(String prefix, Properties properti
.mode(withPrefix(prefix, ASSEMBLY_MODE, properties))
.build();
}

private String extractName(String prefix, Properties properties) throws IllegalArgumentException {
String name = withPrefix(prefix, NAME, properties);
if (name == null) {
throw new IllegalArgumentException(String.format("Mandatory property [%s] is not defined", NAME));
}
return name;
}

// Extract only the values of the port mapping
private List<String> extractPortValues(String prefix, Properties properties) {
List<String> ret = new ArrayList<>();
Expand Down Expand Up @@ -206,7 +206,7 @@ private WaitConfiguration extractWaitConfig(String prefix, Properties properties
.tcpMode(withPrefix(prefix, WAIT_TCP_MODE, properties))
.build();
}

private WatchImageConfiguration extractWatchConfig(String prefix, Properties properties) {
return new WatchImageConfiguration.Builder()
.interval(asInt(withPrefix(prefix, WATCH_INTERVAL, properties)))
Expand All @@ -221,7 +221,7 @@ private VolumeConfiguration extractVolumeConfig(String prefix, Properties proper
.from(listWithPrefix(prefix, VOLUMES_FROM, properties))
.build();
}

private int asInt(String s) {
return s != null ? Integer.parseInt(s) : 0;
}
Expand All @@ -243,11 +243,11 @@ private List<Integer> asIntList(List<String> strings) {
private List<String> listWithPrefix(String prefix, ConfigKey key, Properties properties) {
return extractFromPropertiesAsList(key.asPropertyKey(prefix), properties);
}

private Map<String, String> mapWithPrefix(String prefix, ConfigKey key, Properties properties) {
return extractFromPropertiesAsMap(key.asPropertyKey(prefix), properties);
}

private String withPrefix(String prefix, ConfigKey key, Properties properties) {
return properties.getProperty(key.asPropertyKey(prefix));
}
Expand Down
Loading

0 comments on commit 1067e63

Please sign in to comment.