Skip to content

Commit

Permalink
Issue fabric8io#1111: Moved code exposing container properties from S…
Browse files Browse the repository at this point in the history
…tartMojo to StartContainerExecution in order to provide container properties to the wait configuration execution.
  • Loading branch information
Philipp Schwarz committed Oct 13, 2018
1 parent e0d8286 commit 19dac54
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 42 deletions.
47 changes: 5 additions & 42 deletions src/main/java/io/fabric8/maven/docker/StartMojo.java
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@
import java.util.Collections;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Properties;
import java.util.Queue;
import java.util.Set;
Expand All @@ -35,7 +34,6 @@
import io.fabric8.maven.docker.config.RunVolumeConfiguration;
import io.fabric8.maven.docker.config.VolumeConfiguration;
import io.fabric8.maven.docker.log.LogDispatcher;
import io.fabric8.maven.docker.model.Container;
import io.fabric8.maven.docker.service.ImagePullManager;
import io.fabric8.maven.docker.service.QueryService;
import io.fabric8.maven.docker.service.RegistryService;
Expand All @@ -48,7 +46,6 @@
import org.apache.maven.plugins.annotations.LifecyclePhase;
import org.apache.maven.plugins.annotations.Mojo;
import org.apache.maven.plugins.annotations.Parameter;
import org.codehaus.plexus.util.StringUtils;


/**
Expand Down Expand Up @@ -173,12 +170,12 @@ public synchronized void executeInternal(final ServiceHub hub) throws DockerAcce
imagesWaitingToStart.remove(image);

if (!startParallel) {
waitForStartedContainer(hub, containerStartupService, startedContainerAliases, imagesStarting);
waitForStartedContainer(containerStartupService, startedContainerAliases, imagesStarting);
}
}

if (startParallel) {
waitForStartedContainer(hub, containerStartupService, startedContainerAliases, imagesStarting);
waitForStartedContainer(containerStartupService, startedContainerAliases, imagesStarting);
}
}

Expand Down Expand Up @@ -206,17 +203,16 @@ public synchronized void executeInternal(final ServiceHub hub) throws DockerAcce
}
}

private void waitForStartedContainer(final ServiceHub hub,
private void waitForStartedContainer(
final ExecutorCompletionService<StartedContainer> containerStartupService,
final Set<String> startedContainerAliases, final Queue<ImageConfiguration> imagesStarting)
throws InterruptedException, DockerAccessException, IOException, ExecException {
throws InterruptedException, IOException, ExecException {
final Future<StartedContainer> startedContainerFuture = containerStartupService.take();
try {
final StartedContainer startedContainer = startedContainerFuture.get();
final ImageConfiguration imageConfig = startedContainer.imageConfig;

updateAliasesSet(startedContainerAliases, imageConfig.getAlias());
exposeContainerProps(hub.getQueryService(), startedContainer);

// All done with this image
imagesStarting.remove(imageConfig);
Expand Down Expand Up @@ -283,6 +279,7 @@ private void startImage(final ImageConfiguration imageConfig,
final LogDispatcher dispatcher = getLogDispatcher(hub);

StartContainerExecutor startExecutor = new StartContainerExecutor.Builder()
.exposeContainerProps(exposeContainerProps)
.dispatcher(dispatcher)
.follow(follow)
.log(log)
Expand Down Expand Up @@ -391,38 +388,4 @@ private ExecutorService getExecutorService() {
return executorService;
}

// Expose ports as project properties
private void exposeContainerProps(QueryService queryService, StartedContainer startedContainer)
throws DockerAccessException {
String propKey = getExposedPropertyKeyPart(startedContainer.imageConfig);
if (StringUtils.isNotEmpty(exposeContainerProps) && StringUtils.isNotEmpty(propKey)) {
Container container = queryService.getMandatoryContainer(startedContainer.containerId);
Properties props = project.getProperties();
String prefix = addDot(exposeContainerProps) + addDot(propKey);
props.put(prefix + "id", startedContainer.containerId);
String ip = container.getIPAddress();
if (StringUtils.isNotEmpty(ip)) {
props.put(prefix + "ip", ip);
}

Map<String, String> nets = container.getCustomNetworkIpAddresses();
if (nets != null) {
for (Map.Entry<String, String> entry : nets.entrySet()) {
props.put(prefix + addDot("net") + addDot(entry.getKey()) + "ip", entry.getValue());
}
}
}
}

private String getExposedPropertyKeyPart(ImageConfiguration image) {
String propKey = image.getRunConfiguration() != null ? image.getRunConfiguration().getExposedPropertyKey() : null;
if (StringUtils.isEmpty(propKey)) {
propKey = image.getAlias();
}
return propKey;
}

private String addDot(String part) {
return part.endsWith(".") ? part : part + ".";
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,12 @@
import java.io.File;
import java.io.IOException;
import java.util.Date;
import java.util.Map;
import java.util.Properties;

import org.codehaus.plexus.util.StringUtils;

import io.fabric8.maven.docker.access.DockerAccessException;
import io.fabric8.maven.docker.access.ExecException;
import io.fabric8.maven.docker.access.PortMapping;
import io.fabric8.maven.docker.config.ConfigHelper;
Expand All @@ -14,11 +18,14 @@
import io.fabric8.maven.docker.config.WaitConfiguration;
import io.fabric8.maven.docker.log.LogDispatcher;
import io.fabric8.maven.docker.log.LogOutputSpecFactory;
import io.fabric8.maven.docker.model.Container;
import io.fabric8.maven.docker.service.QueryService;
import io.fabric8.maven.docker.service.ServiceHub;
import io.fabric8.maven.docker.util.GavLabel;
import io.fabric8.maven.docker.util.Logger;

public class StartContainerExecutor {
private String exposeContainerProps;
private Logger log;
private LogOutputSpecFactory logOutputSpecFactory;
private ServiceHub hub;
Expand All @@ -41,11 +48,47 @@ public String startContainers() throws IOException, ExecException {
final String containerId = hub.getRunService().createAndStartContainer(imageConfig, portMapping, gavLabel, projProperties, basedir, containerNamePattern, buildDate);

showLogsIfRequested(containerId);
exposeContainerProps(containerId);
waitAndPostExec(containerId, projProperties);

return containerId;
}

private void exposeContainerProps( String containerId)
throws DockerAccessException {
String propKey = getExposedPropertyKeyPart(imageConfig);

if (StringUtils.isNotEmpty(exposeContainerProps) && StringUtils.isNotEmpty(propKey)) {
Container container = hub.getQueryService().getMandatoryContainer(containerId);

String prefix = addDot(exposeContainerProps) + addDot(propKey);
projectProperties.put(prefix + "id", containerId);
String ip = container.getIPAddress();
if (StringUtils.isNotEmpty(ip)) {
projectProperties.put(prefix + "ip", ip);
}

Map<String, String> nets = container.getCustomNetworkIpAddresses();
if (nets != null) {
for (Map.Entry<String, String> entry : nets.entrySet()) {
projectProperties.put(prefix + addDot("net") + addDot(entry.getKey()) + "ip", entry.getValue());
}
}
}
}

private String getExposedPropertyKeyPart(ImageConfiguration image) {
String propKey = image.getRunConfiguration() != null ? image.getRunConfiguration().getExposedPropertyKey() : null;
if (StringUtils.isEmpty(propKey)) {
propKey = image.getAlias();
}
return propKey;
}

private String addDot(String part) {
return part.endsWith(".") ? part : part + ".";
}

private void showLogsIfRequested(String containerId) {
if (showLogs(imageConfig)) {
dispatcher.trackContainerLog(containerId,
Expand Down Expand Up @@ -111,6 +154,11 @@ public Builder logOutputSpecFactory(LogOutputSpecFactory factory) {
return this;
}

public Builder exposeContainerProps(String exposeContainerProps) {
helper.exposeContainerProps = exposeContainerProps;
return this;
}

public Builder serviceHub(ServiceHub hub) {
helper.hub = hub;
return this;
Expand Down

0 comments on commit 19dac54

Please sign in to comment.