Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
Some tweaks for use in gradle-docker.
- Change the default Docker port to 2375 to match current Docker version.

- Add toString() to all commands which prints out a form similar to the
  corresponding Docker command line.  Useful in tracing.

- Create client logging filter variant which allows us to avoid logging
  requests whose content type is likely to cause issues for both the
  Windows and OSX consoles.  This is an adaptation of a change from a
  while back
  (alexec@887a112)
  which seems to have be lost in the move.
  • Loading branch information
Sean Fitts committed Jul 3, 2014
commit ffe330141ac4e04d8528dced3ed2b9e4be5164c8
7 changes: 3 additions & 4 deletions src/main/java/com/github/dockerjava/client/DockerClient.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package com.github.dockerjava.client;

import static org.apache.commons.io.IOUtils.closeQuietly;
import static org.apache.commons.io.IOUtils.*;

import java.io.File;
import java.io.IOException;
Expand Down Expand Up @@ -54,7 +54,6 @@
import com.sun.jersey.api.client.WebResource;
import com.sun.jersey.api.client.config.ClientConfig;
import com.sun.jersey.api.client.config.DefaultClientConfig;
import com.sun.jersey.api.client.filter.LoggingFilter;
import com.sun.jersey.client.apache4.ApacheHttpClient4;
import com.sun.jersey.client.apache4.ApacheHttpClient4Handler;

Expand All @@ -63,7 +62,7 @@
*/
public class DockerClient {

private Client client;
private Client client;
private WebResource baseResource;
private AuthConfig authConfig;

Expand Down Expand Up @@ -110,7 +109,7 @@ private DockerClient(Config config) {
// client = new UnixSocketClient(clientConfig);

client.addFilter(new JsonClientFilter());
client.addFilter(new LoggingFilter());
client.addFilter(new SelectiveLoggingFilter());

baseResource = client.resource(config.url + "/v" + config.version);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
package com.github.dockerjava.client;

import java.util.Set;

import javax.ws.rs.core.HttpHeaders;
import javax.ws.rs.core.MediaType;

import com.google.common.collect.ImmutableSet;
import com.sun.jersey.api.client.ClientHandlerException;
import com.sun.jersey.api.client.ClientRequest;
import com.sun.jersey.api.client.ClientResponse;
import com.sun.jersey.api.client.filter.LoggingFilter;

/**
* A version of the logging filter that will avoid trying to log entities which can cause
* issues with the console.
*
* @author sfitts
*
*/
public class SelectiveLoggingFilter extends LoggingFilter {

private static final Set<String> SKIPPED_CONTENT = ImmutableSet.<String>builder()
.add(MediaType.APPLICATION_OCTET_STREAM)
.add("application/tar")
.build();

@Override
public ClientResponse handle(ClientRequest request) throws ClientHandlerException {
// Unless the content type is in the list of those we want to ellide, then just have
// our super-class handle things.
MediaType contentType = (MediaType) request.getHeaders().getFirst(HttpHeaders.CONTENT_TYPE);
if (SKIPPED_CONTENT.contains(contentType.toString())) {
// Skip logging this.
//
// N.B. -- I'd actually love to reproduce (or better yet just use) the logging code from
// our super-class. However, everything is private (so we can't use it) and the code
// is under a modified GPL which means we can't pull it into an ASL project. Right now
// I don't have the energy to do a clean implementation.
return getNext().handle(request);
}

// Do what we normally would
return super.handle(request);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -34,4 +34,9 @@ protected Void impl() throws DockerException {
throw new DockerException(e);
}
}

@Override
public String toString() {
return "authenticate using " + this.authConfig;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,15 @@ public BuildImgCmd withNoCache(boolean noCache) {
return this;
}

@Override
public String toString() {
return new StringBuilder("build ")
.append(tag != null ? "-t " + tag + " " : "")
.append(noCache ? "--nocache=true " : "")
.append(dockerFolder != null ? dockerFolder.getPath() : "-")
.toString();
}

protected ClientResponse impl() {
if (tarInputStream == null) {
File dockerFolderTar = buildDockerFolderTar();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,17 @@ public CommitCmd withRun(String run) {
return this;
}


@Override
public String toString() {
return new StringBuilder("commit ")
.append(commitConfig.getAuthor() != null ? "--author " + commitConfig.getAuthor() + " " : "")
.append(commitConfig.getMessage() != null ? "--message " + commitConfig.getMessage() + " " : "")
.append(commitConfig.getContainerId())
.append(commitConfig.getRepo() != null ? " " + commitConfig.getRepo() + ":" : " ")
.append(commitConfig.getTag() != null ? commitConfig.getTag() : "")
.toString();
}

private void checkCommitConfig(CommitConfig commitConfig) {
Preconditions.checkNotNull(commitConfig, "CommitConfig was not specified");
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,14 @@ public ContainerDiffCmd withContainerId(String containerId) {
return this;
}

protected List<ChangeLog> impl() throws DockerException {
@Override
public String toString() {
return new StringBuilder("diff ")
.append(containerId)
.toString();
}

protected List<ChangeLog> impl() throws DockerException {
WebResource webResource = baseResource.path(String.format("/containers/%s/changes", containerId));

try {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,15 @@ public CopyFileFromContainerCmd withResource(String resource) {
return this;
}

@Override
public String toString() {
return new StringBuilder("cp ")
.append(containerId)
.append(":")
.append(resource)
.toString();
}

protected ClientResponse impl() throws DockerException {
CopyConfig copyConfig = new CopyConfig();
copyConfig.setResource(resource);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,14 @@ public CreateContainerCmd withExposedPorts(ExposedPort... exposedPorts) {
return this;
}

@Override
public String toString() {
return new StringBuilder("create container ")
.append(name != null ? "name=" + name + " " : "")
.append(containerCreateConfig)
.toString();
}

protected ContainerCreateResponse impl() {
MultivaluedMap<String, String> params = new MultivaluedMapImpl();
if (name != null) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,14 @@ public ImportImageCmd withTag(String tag) {
return this;
}

@Override
public String toString() {
return new StringBuilder("import - ")
.append(repository != null ? repository + ":" : "")
.append(tag != null ? tag : "")
.toString();
}

protected ImageCreateResponse impl() {
MultivaluedMap<String, String> params = new MultivaluedMapImpl();
params.add("repo", repository);
Expand Down
10 changes: 6 additions & 4 deletions src/main/java/com/github/dockerjava/client/command/InfoCmd.java
Original file line number Diff line number Diff line change
Expand Up @@ -12,15 +12,17 @@


/**
*
*
*
* Return Docker server info
*/
public class InfoCmd extends AbstrDockerCmd<InfoCmd, Info> {

private static final Logger LOGGER = LoggerFactory.getLogger(InfoCmd.class);


@Override
public String toString() {
return "info";
}

protected Info impl() throws DockerException {
WebResource webResource = baseResource.path("/info");

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,7 @@
import com.sun.jersey.api.client.WebResource;

/**
*
*
*
* Inspect the details of a container.
*/
public class InspectContainerCmd extends AbstrDockerCmd<InspectContainerCmd, ContainerInspectResponse> {

Expand All @@ -33,6 +31,11 @@ public InspectContainerCmd withContainerId(String containerId) {
return this;
}

@Override
public String toString() {
return "inspect " + containerId;
}

protected ContainerInspectResponse impl() throws DockerException {
WebResource webResource = baseResource.path(String.format("/containers/%s/json", containerId));

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,7 @@


/**
*
*
*
* Inspect the details of an image.
*/
public class InspectImageCmd extends AbstrDockerCmd<InspectImageCmd, ImageInspectResponse> {

Expand All @@ -34,6 +32,11 @@ public InspectImageCmd withImageId(String imageId) {
return this;
}

@Override
public String toString() {
return "inspect " + imageId;
}

protected ImageInspectResponse impl() {
WebResource webResource = baseResource.path(String.format("/images/%s/json", imageId));

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,7 @@
import com.sun.jersey.api.client.WebResource;

/**
*
*
*
* Kill a running container.
*/
public class KillContainerCmd extends AbstrDockerCmd<KillContainerCmd, Void> {

Expand All @@ -31,6 +29,11 @@ public KillContainerCmd withContainerId(String containerId) {
return this;
}

@Override
public String toString() {
return "kill " + containerId;
}

protected Void impl() throws DockerException {
WebResource webResource = baseResource.path(String.format("/containers/%s/kill", containerId));

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,17 @@ public ListContainersCmd withBefore(String before) {
return this;
}

@Override
public String toString() {
return new StringBuilder("ps ")
.append(showAll ? "--all=true" : "")
.append(showSize ? "--size=true" : "")
.append(sinceId != null ? "--since " + sinceId : "")
.append(beforeId != null ? "--before " + beforeId : "")
.append(limit != -1 ? "-n " + limit : "")
.toString();
}

protected List<Container> impl() {
MultivaluedMap<String, String> params = new MultivaluedMapImpl();
if(limit >= 0) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,14 @@ public ListImagesCmd withFilter(String filter) {
return this;
}

@Override
public String toString() {
return new StringBuilder("images ")
.append(showAll ? "--all=true" : "")
.append(filter != null ? "--filter " + filter : "")
.toString();
}

protected List<Image> impl() {
MultivaluedMap<String, String> params = new MultivaluedMapImpl();
params.add("filter", filter);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,14 @@ public LogContainerCmd withStdErr(boolean stderr) {
return this;
}


@Override
public String toString() {
return new StringBuilder("logs ")
.append(followStream ? "--follow=true" : "")
.append(timestamps ? "--timestamps=true" : "")
.append(containerId)
.toString();
}

protected ClientResponse impl() throws DockerException {
MultivaluedMap<String, String> params = new MultivaluedMapImpl();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,14 @@ public PullImageCmd withRegistry(String registry) {
return this;
}

@Override
public String toString() {
return new StringBuilder("pull ")
.append(repository)
.append(tag != null ? ":" + tag : "")
.toString();
}

protected ClientResponse impl() {
Preconditions.checkNotNull(repository, "Repository was not specified");

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,13 @@ public PushImageCmd withName(String name) {
return this;
}

@Override
public String toString() {
return new StringBuilder("push ")
.append(name)
.toString();
}

protected ClientResponse impl() {
WebResource webResource = baseResource.path("/images/" + name(name) + "/push");
try {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,15 @@ public RemoveContainerCmd withForce(boolean force) {
return this;
}

@Override
public String toString() {
return new StringBuilder("rm ")
.append(removeVolumes ? "--volumes=true" : "")
.append(force ? "--force=true" : "")
.append(containerId)
.toString();
}

protected Void impl() throws DockerException {
Preconditions.checkState(!StringUtils.isEmpty(containerId), "Container ID can't be empty");

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,15 @@ public RemoveImageCmd withNoPrune(boolean noPrune) {
return this;
}

@Override
public String toString() {
return new StringBuilder("rmi ")
.append(noPrune ? "--no-prune=true" : "")
.append(force ? "--force=true" : "")
.append(imageId)
.toString();
}

protected Void impl() throws DockerException {
Preconditions.checkState(!StringUtils.isEmpty(imageId), "Image ID can't be empty");

Expand Down
Loading