Skip to content

Commit

Permalink
- initial docker-compose support
Browse files Browse the repository at this point in the history
Signed-off-by: Jae Gangemi <jgangemi@gmail.com>
  • Loading branch information
jgangemi authored and rhuss committed Feb 14, 2016
1 parent 7c0b37d commit 479d839
Show file tree
Hide file tree
Showing 19 changed files with 1,134 additions and 170 deletions.
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@
<dependency>
<groupId>org.apache.httpcomponents</groupId>
<artifactId>httpclient</artifactId>
<version>4.3.6</version>
<version>4.5.1</version>
</dependency>

<dependency>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -132,7 +132,7 @@ public abstract class AbstractDockerMojo extends AbstractMojo implements Context
// images
@Parameter
private List<ImageConfiguration> images;

// Handler dealing with authentication credentials
private AuthConfigFactory authConfigFactory;

Expand Down Expand Up @@ -239,7 +239,7 @@ private List<ImageConfiguration> resolveImages() {
List<ImageConfiguration> ret = new ArrayList<>();
if (images != null) {
for (ImageConfiguration image : images) {
ret.addAll(imageConfigResolver.resolve(image, project.getProperties()));
ret.addAll(imageConfigResolver.resolve(image, project));
}
verifyImageNames(ret);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -265,6 +265,15 @@ public Builder cleanup(String cleanup) {
return this;
}

public Builder compression(String compression) {
if (compression == null) {
config.compression = BuildTarArchiveCompression.none;
} else {
config.compression = BuildTarArchiveCompression.valueOf(compression);
}
return this;
}

public Builder nocache(String nocache) {
if (nocache != null) {
config.nocache = Boolean.valueOf(nocache);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import java.util.*;

import org.jolokia.docker.maven.config.external.ExternalImageConfiguration;
import org.jolokia.docker.maven.util.EnvUtil;
import org.jolokia.docker.maven.util.Logger;
import org.jolokia.docker.maven.util.StartOrderResolver;
Expand Down Expand Up @@ -41,7 +42,7 @@ public class ImageConfiguration implements StartOrderResolver.Resolvable {
/**
* @parameter
*/
private Map<String,String> external;
private ExternalImageConfiguration external;

/**
* @parameter
Expand All @@ -66,17 +67,18 @@ public RunImageConfiguration getRunConfiguration() {
}

public BuildImageConfiguration getBuildConfiguration() {
return build;
// return an empty image
return (build == null) ? new BuildImageConfiguration() : build;
}

public WatchImageConfiguration getWatchConfiguration() {
return watch;
}

public Map<String, String> getExternalConfig() {
public ExternalImageConfiguration getExternalConfiguration() {
return external;
}

@Override
public List<String> getDependencies() {
RunImageConfiguration runConfig = getRunConfiguration();
Expand Down Expand Up @@ -173,7 +175,7 @@ public Builder buildConfig(BuildImageConfiguration buildConfig) {
return this;
}

public Builder externalConfig(Map<String, String> externalConfig) {
public Builder externalConfig(ExternalImageConfiguration externalConfig) {
config.external = externalConfig;
return this;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
package org.jolokia.docker.maven.config.external;

import java.util.*;

import org.jolokia.docker.maven.config.*;

public class DockerComposeConfiguration {

/** @parameter */
private String basedir;

/** @parameter */
private List<ImageConfiguration> services = new ArrayList<>();

/** @parameter */
private String yamlFile;

public String getBasedir() {
// the @parameter tags above don't actually do anything, so we need to handle the default here :(
return (basedir == null) ? "src/main/docker" : basedir;
}

public Map<String, ImageConfiguration> getServiceMap() {
if (services == null) {
return Collections.emptyMap();
}

Map<String, ImageConfiguration> map = new HashMap<>(services.size());
for (ImageConfiguration service : services) {
String alias = service.getAlias();
if (alias == null) {
throw new IllegalArgumentException("an 'alias' is required when using docker-compose files");
}

map.put(alias, service);
}

return map;
}

public String getYamlFile() {
// see 'getBasedir'
return (yamlFile == null) ? "docker-compose.yml" : yamlFile;
}

public static class Builder {
private final DockerComposeConfiguration composeConfig = new DockerComposeConfiguration();

public Builder addService(ImageConfiguration service) {
composeConfig.services.add(service);
return this;
}

public Builder basedir(String basedir) {
composeConfig.basedir = basedir;
return this;
}

public DockerComposeConfiguration build() {
return composeConfig;
}

public Builder yamlFile(String yamlFile) {
composeConfig.yamlFile = yamlFile;
return this;
}
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
package org.jolokia.docker.maven.config.external;

public class ExternalImageConfiguration {

/** @parameter */
private DockerComposeConfiguration compose;

/** @parameter */
private PropertiesConfiguration properties;

public DockerComposeConfiguration getComposeConfiguration() {
return compose;
}

public boolean hasDockerCompose() {
return compose != null;
}

public boolean hasProperties() {
return properties != null;
}

public PropertiesConfiguration getPropertiesConfiguration() {
return properties;
}

public static class Builder {
private final ExternalImageConfiguration externalConfig = new ExternalImageConfiguration();

public Builder compose(DockerComposeConfiguration compose) {
externalConfig.compose = compose;
return this;
}

public Builder properties(PropertiesConfiguration properties) {
externalConfig.properties = properties;
return this;
}

public ExternalImageConfiguration build() {
return externalConfig;
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package org.jolokia.docker.maven.config.external;

public class PropertiesConfiguration {

/** @parameter */
private String prefix;

public String getPrefix() {
return prefix;
}

public static class Builder {
private final PropertiesConfiguration propsConfig = new PropertiesConfiguration();

public Builder prefix(String prefix) {
propsConfig.prefix = prefix;
return this;
}

public PropertiesConfiguration build() {
return propsConfig;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,8 @@
*/

import java.util.List;
import java.util.Properties;

import org.apache.maven.project.MavenProject;
import org.jolokia.docker.maven.config.ImageConfiguration;

/**
Expand All @@ -42,8 +42,9 @@ public interface ExternalConfigHandler {
* {@link ImageConfiguration} objects describing the image to manage
*
* @param unresolvedConfig the original, unresolved config
* @param properties extra properties used for resolving
* @param project maven project
* @return list of image configuration. Must not be null but can be empty.
* @throws ExternalConfigHandlerException if there is a problem resolving the image configuration
*/
List<ImageConfiguration> resolve(ImageConfiguration unresolvedConfig, Properties properties);
List<ImageConfiguration> resolve(ImageConfiguration unresolvedConfig, MavenProject project) throws ExternalConfigHandlerException;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package org.jolokia.docker.maven.config.handler;

public class ExternalConfigHandlerException extends RuntimeException
{
private static final long serialVersionUID = -2742743075207582636L;

public ExternalConfigHandlerException(String message)
{
super(message);
}

public ExternalConfigHandlerException(String message, Throwable cause)
{
super(message, cause);
}
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
package org.jolokia.docker.maven.config.handler;/*
package org.jolokia.docker.maven.config.handler;
/*
*
* Copyright 2014 Roland Huss
*
Expand All @@ -21,18 +22,24 @@
import org.codehaus.plexus.component.annotations.Requirement;
import org.codehaus.plexus.personality.plexus.lifecycle.phase.Initializable;
import org.codehaus.plexus.personality.plexus.lifecycle.phase.InitializationException;
import org.apache.maven.project.MavenProject;
import org.jolokia.docker.maven.config.ImageConfiguration;
import org.jolokia.docker.maven.config.external.ExternalImageConfiguration;

/**
* Manager holding all config handlers for external configuration
*
* @author roland
* @since 18/11/14
*/

@Component(role = ImageConfigResolver.class, instantiationStrategy = "per-lookup")
public class ImageConfigResolver implements Initializable {

public static final String COMPOSE = "compose";
public static final String PROPERTIES = "properties";

private Map<String, ExternalConfigHandler> resolvers;

// Map type to handler
private Map<String,ExternalConfigHandler> registry;

Expand All @@ -56,26 +63,35 @@ public void initialize() throws InitializationException {
* is returned directly.
*
* @param unresolvedConfig the configuration to resolve
* @param properties extra properties used for resolving
* @param project maven project
* @return list of resolved image configurations
* @throws IllegalArgumentException if no type is given when an external reference configuration is provided
* or when the type is not known (i.e. no handler is registered for this type).
*/
public List<ImageConfiguration> resolve(ImageConfiguration unresolvedConfig, Properties properties) {
Map<String,String> referenceConfig = unresolvedConfig.getExternalConfig();
if (referenceConfig != null) {
String type = referenceConfig.get("type");
if (type == null) {
throw new IllegalArgumentException(unresolvedConfig.getDescription() + ": No config type given");
}
ExternalConfigHandler handler = registry.get(type);
if (handler == null) {
throw new IllegalArgumentException(unresolvedConfig.getDescription() + ": No handler for type " + type + " given");
}
return handler.resolve(unresolvedConfig,properties);
} else {
public List<ImageConfiguration> resolve(ImageConfiguration unresolvedConfig, MavenProject project) {
ExternalImageConfiguration external = unresolvedConfig.getExternalConfiguration();

if (external == null) {
return Collections.singletonList(unresolvedConfig);
}
}

String type = getHandlerType(external);
if (resolvers.containsKey(type)) {
return resolvers.get(type).resolve(unresolvedConfig, project);
}

throw new IllegalArgumentException(unresolvedConfig.getDescription() + ": No handler for type " + type + " found");
}

public void setResolvers(Map<String, ExternalConfigHandler> resolvers) {
this.resolvers = resolvers;
}

private String getHandlerType(ExternalImageConfiguration external) {
if (external.hasDockerCompose()) {
return COMPOSE;
} else {
return PROPERTIES;
}
}
}
Loading

0 comments on commit 479d839

Please sign in to comment.