Skip to content

Commit

Permalink
Implement configuration handling for logs #49 #8
Browse files Browse the repository at this point in the history
  • Loading branch information
rhuss committed Dec 2, 2014
1 parent f629b1d commit eee93bf
Show file tree
Hide file tree
Showing 10 changed files with 253 additions and 48 deletions.
8 changes: 4 additions & 4 deletions samples/data-jolokia-demo/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,7 @@
<artifactId>docker-maven-plugin</artifactId>
<version>${project.version}</version>
<configuration>
<logDate>default</logDate>
<images>
<!-- Docker Image to use -->
<image>
Expand Down Expand Up @@ -104,10 +105,9 @@
<time>10000</time>
</wait>
<log>
<prefix>SRV</prefix>
<timestamp>ISO8601</timestamp>
<color>GREEN</color>
</log>
<enabled>true</enabled>
<color>red</color>
</log>
</run>
</image>
<image>
Expand Down
84 changes: 79 additions & 5 deletions src/main/java/org/jolokia/docker/maven/AbstractDockerMojo.java
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,9 @@
import org.fusesource.jansi.Ansi;
import org.fusesource.jansi.AnsiConsole;
import org.jolokia.docker.maven.access.*;
import org.jolokia.docker.maven.config.ImageConfiguration;
import org.jolokia.docker.maven.config.*;
import org.jolokia.docker.maven.config.handler.ImageConfigResolver;
import org.jolokia.docker.maven.log.ContainerLogOutputSpec;
import org.jolokia.docker.maven.log.LogDispatcher;
import org.jolokia.docker.maven.util.*;

Expand Down Expand Up @@ -65,16 +66,25 @@ public abstract class AbstractDockerMojo extends AbstractMojo implements LogHand
/** @parameter property = "docker.certPath" */
private String certPath;

// If logging is enabled globally
/** @parameter property = "docker.showLogs" default-value="false" */
private boolean showLogs;

// Whether to use color
/** @parameter property = "docker.useColor" default-value = "true" */
private boolean useColor;
protected boolean useColor;

// The date format to use when putting out logs
/** @parameter property = "docker.logDate" */
protected String logDate;

// Whether to skip docker altogether
/** @parameter property = "docker.skip" default-value = "false" */
private boolean skip;

// Whether to restrict operation to a single image. This can be either
// the image or an alias name
// the image or an alias name. It can also be comma separated list.
// This parameter is typically set via the command line.
/** @parameter property = "docker.image" */
private String image;

Expand Down Expand Up @@ -266,7 +276,10 @@ private String getDefaultUserName() {

// Color init
private void colorInit() {
if (useColor && System.console() != null) {
if (System.console() == null) {
useColor = false;
}
if (useColor) {
AnsiConsole.systemInstall();
Ansi.setEnabled(true);
} else {
Expand Down Expand Up @@ -355,13 +368,74 @@ protected static String getContainerAndImageDescription(String container, String
protected LogDispatcher getLogDispatcher(DockerAccess docker) {
LogDispatcher dispatcher = (LogDispatcher) getPluginContext().get(CONTEXT_KEY_LOG_DISPATCHER);
if (dispatcher == null) {
dispatcher = new LogDispatcher(docker);
dispatcher = new LogDispatcher(docker,useColor);
dispatcher.addLogOutputStream(System.out);
getPluginContext().put(CONTEXT_KEY_LOG_DISPATCHER,dispatcher);
}
return dispatcher;
}

protected ContainerLogOutputSpec getContainerLogSpec(String containerId, ImageConfiguration imageConfiguration) {
ContainerLogOutputSpec.Builder builder = new ContainerLogOutputSpec.Builder();
LogConfiguration logConfig = extractLogConfiguration(imageConfiguration);

addLogFormat(builder, logConfig);
addPrefix(builder, logConfig.getPrefix(), imageConfiguration.getAlias(), containerId);

builder.containerId(containerId)
.color(logConfig.getColor());

return builder.build();
}

private void addPrefix(ContainerLogOutputSpec.Builder builder, String logPrefix, String alias, String containerId) {
String prefix = logPrefix;
if (prefix == null) {
prefix = alias;
}
if (prefix == null) {
prefix = containerId.substring(0,6);
}
builder.prefix(prefix);
}

private void addLogFormat(ContainerLogOutputSpec.Builder builder, LogConfiguration logConfig) {
String logFormat = logConfig.getDate() != null ? logConfig.getDate() : logDate;
if (logFormat != null && logFormat.equalsIgnoreCase("true")) {
logFormat = "DEFAULT";
}
if (logFormat != null) {
builder.timeFormatter(logFormat);
}
}

private LogConfiguration extractLogConfiguration(ImageConfiguration imageConfiguration) {
RunImageConfiguration runConfig = imageConfiguration.getRunConfiguration();
LogConfiguration logConfig = null;
if (runConfig != null) {
logConfig = runConfig.getLog();
}
if (logConfig == null) {
logConfig = LogConfiguration.DEFAULT;
}
return logConfig;
}

protected boolean showLog(ImageConfiguration imageConfig) {
if (showLogs) {
return true;
} else {
RunImageConfiguration runConfig = imageConfig.getRunConfiguration();
if (runConfig != null) {
LogConfiguration logConfig = runConfig.getLog();
if (logConfig != null) {
return logConfig.isEnabled();
}
}
return false;
}
}

// ==========================================================================================
// Class for registering a shutdown action

Expand Down
15 changes: 12 additions & 3 deletions src/main/java/org/jolokia/docker/maven/LogMojo.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
import org.jolokia.docker.maven.access.DockerAccess;
import org.jolokia.docker.maven.access.DockerAccessException;
import org.jolokia.docker.maven.config.ImageConfiguration;
import org.jolokia.docker.maven.log.ContainerLogOutputSpec;
import org.jolokia.docker.maven.log.LogDispatcher;

/**
Expand All @@ -29,14 +28,24 @@ public class LogMojo extends AbstractDockerMojo {
*/
private boolean follow;

// Whether to log all contaienrs or only the newest ones
/** @parameter property = "docker.logAllContainer" default-value = "false" */
private boolean logAllContainer;

protected void executeInternal(DockerAccess access) throws MojoExecutionException, DockerAccessException {

LogDispatcher logDispatcher = getLogDispatcher(access);

for (ImageConfiguration image : getImages()) {
String imageName = image.getName();
for (String container : access.getContainersForImage(imageName)) {
logDispatcher.fetchContainerLog(container, ContainerLogOutputSpec.DEFAULT);
if (logAllContainer) {
for (String container : access.getContainersForImage(imageName)) {
logDispatcher.fetchContainerLog(container,getContainerLogSpec(container,image));
}
} else {
String container = access.getNewestImageForContainer(imageName);
logDispatcher.fetchContainerLog(container,
getContainerLogSpec(container,image));
}
}
if (follow) {
Expand Down
6 changes: 4 additions & 2 deletions src/main/java/org/jolokia/docker/maven/StartMojo.java
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@
import org.jolokia.docker.maven.access.log.LogCallback;
import org.jolokia.docker.maven.access.log.LogGetHandle;
import org.jolokia.docker.maven.config.*;
import org.jolokia.docker.maven.log.ContainerLogOutputSpec;
import org.jolokia.docker.maven.log.LogDispatcher;
import org.jolokia.docker.maven.util.*;

Expand Down Expand Up @@ -78,7 +77,10 @@ public void executeInternal(DockerAccess docker) throws DockerAccessException, M
findContainersForImages(runConfig.getVolumesFrom()),
findLinksWithContainerNames(docker, runConfig.getLinks()));

dispatcher.trackContainerLog(container, ContainerLogOutputSpec.DEFAULT);

if (showLog(imageConfig)) {
dispatcher.trackContainerLog(container, getContainerLogSpec(container, imageConfig));
}

registerContainer(container, imageConfig);
info("Created and started container " +
Expand Down
10 changes: 10 additions & 0 deletions src/main/java/org/jolokia/docker/maven/access/DockerAccess.java
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,15 @@ public interface DockerAccess {
*/
List<String> getContainersForImage(String image) throws DockerAccessException;

/**
* Get the id of the newest container started for an image
*
* @param image for which its container are looked up
* @return container id or <code>null</code> if no container has been started for this image.
* @throws DockerAccessException if the request fails
*/
String getNewestImageForContainer(String image) throws DockerAccessException;

/**
* Get logs for a container up to now synchronously.
*
Expand Down Expand Up @@ -153,4 +162,5 @@ public interface DockerAccess {
* cleaning up things.
*/
void shutdown();

}
Original file line number Diff line number Diff line change
Expand Up @@ -137,22 +137,48 @@ public Map<Integer, Integer> queryContainerPortMapping(String containerId) throw
/** {@inheritDoc} */
@Override
public List<String> getContainersForImage(String image) throws DockerAccessException {
return getContainerIds(image,false);
}

@Override
public String getNewestImageForContainer(String image) throws DockerAccessException {
List<String> newestContainer = getContainerIds(image,true);
assert newestContainer.size() == 0 || newestContainer.size() == 1;
return newestContainer.size() == 0 ? null : newestContainer.get(0);
}

private List<String> getContainerIds(String image,boolean onlyLatest) throws DockerAccessException {
List<String> ret = new ArrayList<>();
HttpUriRequest req = newGet(baseUrl + "/containers/json?limit=100");
HttpResponse resp = request(req);
checkReturnCode("Fetching container information", resp, 200);
JSONArray configs = asJsonArray(resp);
long newest = 0;
for (int i = 0; i < configs.length(); i ++) {
JSONObject config = configs.getJSONObject(i);
String containerImage = config.getString("Image");

if (image.equals(containerImage)) {
ret.add(config.getString("Id"));
String id = config.getString("Id");
if (!onlyLatest) {
ret.add(id);
} else {
int timestamp = config.getInt("Created");
if (timestamp > newest) {
newest = timestamp;
if (ret.size() == 0) {
ret.add(id);
} else {
ret.set(0, id);
}
}
}
}
}
return ret;
}


@Override
public void getLogSync(String containerId, LogCallback callback) {
LogRequestor extractor = new LogRequestor(client, baseUrl, containerId, callback);
Expand Down Expand Up @@ -192,7 +218,7 @@ public void pushImage(String image, AuthConfig authConfig) throws DockerAccessEx
ImageName name = new ImageName(image);
String pushUrl = baseUrl + "/images/" + encode(name.getRepositoryWithRegistry()) + "/push";
pushUrl = addTag(pushUrl, name);
pullOrPushImage(image, pushUrl, "pushing", authConfig);
pullOrPushImage(image, pushUrl, "pushing", authConfig);
}

/** {@inheritDoc} */
Expand Down
30 changes: 16 additions & 14 deletions src/main/java/org/jolokia/docker/maven/config/LogConfiguration.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,20 +6,22 @@
*/
public class LogConfiguration {

public static final LogConfiguration DEFAULT = new LogConfiguration(false, null, null, null);

/**
* @parameter
* @parameter default-value="true"
*/
private boolean enabled = true;

/**
* @parameter default-value="true"
* @parameter
*/
private String prefix;

/**
* @parameter
*/
private String timestamp;
private String date;

/**
* @parameter
Expand All @@ -28,19 +30,19 @@ public class LogConfiguration {

public LogConfiguration() {}

private LogConfiguration(boolean enabled, String prefix, String url, String log) {
this.prefix = prefix;
this.timestamp = url;
this.color = log;
private LogConfiguration(boolean enabled, String prefix, String color, String date) {
this.enabled = enabled;
this.prefix = prefix;
this.date = date;
this.color = color;
}

public String getPrefix() {
return prefix;
}

public String getTimestamp() {
return timestamp;
public String getDate() {
return date;
}

public String getColor() {
Expand All @@ -55,30 +57,30 @@ public boolean isEnabled() {

public static class Builder {
private boolean enabled = true;
private String time,url,log;
private String prefix, timestamp, color;

public Builder enabled(boolean enabled) {
this.enabled = enabled;
return this;
}

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

public Builder timestamp(String timestamp) {
this.url = timestamp;
this.timestamp = timestamp;
return this;
}

public Builder color(String color) {
this.log = color;
this.color = color;
return this;
}

public LogConfiguration build() {
return new LogConfiguration(enabled,time,url,log);
return new LogConfiguration(enabled, prefix, color, timestamp);
}
}
}
Loading

0 comments on commit eee93bf

Please sign in to comment.