Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
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
4 changes: 2 additions & 2 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,9 @@ env:
# - repo="main" DOCKER_HOST="unix:///var/run/docker.sock" DOCKER_VERSION="17.09.0~ce-0~ubuntu-trusty"
# - repo="main" DOCKER_HOST="tcp://127.0.0.1:2375" DOCKER_VERSION="17.06.2~ce-0~ubuntu-trusty" DEPLOY=true COVERITY=true
- repo="main" DOCKER_HOST="tcp://127.0.0.1:2375" DOCKER_VERSION="17.05.0~ce-0~ubuntu-trusty" DEPLOY=true COVERITY=true
- repo="main" DOCKER_HOST="tcp://127.0.0.1:2377" DOCKER_VERSION="17.05.0~ce-0~ubuntu-trusty" SWARM_VERSION="1.2.6"
- repo="main" DOCKER_HOST="tcp://127.0.0.1:2377" DOCKER_VERSION="17.05.0~ce-0~ubuntu-trusty" SWARM_VERSION="1.2.8"
- repo="main" DOCKER_HOST="unix:///var/run/docker.sock" DOCKER_VERSION="17.05.0~ce-0~ubuntu-trusty"
- repo="main" DOCKER_HOST="tcp://127.0.0.1:2377" DOCKER_VERSION="1.13.1-0~ubuntu-trusty" SWARM_VERSION="1.2.6"
- repo="main" DOCKER_HOST="tcp://127.0.0.1:2377" DOCKER_VERSION="1.13.1-0~ubuntu-trusty" SWARM_VERSION="1.2.8"
- repo="main" DOCKER_HOST="tcp://127.0.0.1:2375" DOCKER_VERSION="1.13.1-0~ubuntu-trusty"
- repo="main" DOCKER_HOST="unix:///var/run/docker.sock" DOCKER_VERSION="1.13.1-0~ubuntu-trusty"
- repo="main" DOCKER_HOST="tcp://127.0.0.1:2375" DOCKER_VERSION="1.12.6-0~ubuntu-trusty"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,7 @@ public InspectImageResponse withComment(String comment) {
}

/**
* Get the image commit configuration
* @see #config
*/
@CheckForNull
Expand Down Expand Up @@ -154,6 +155,8 @@ public InspectImageResponse withContainer(String container) {
}

/**
* If the image was created from a container, this config contains the configuration of the container
* which was committed in this image
* @see #containerConfig
*/
@CheckForNull
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,19 +3,16 @@
*/
package com.github.dockerjava.core.async;

import com.github.dockerjava.api.async.ResultCallback;
import com.google.common.base.Throwables;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.Closeable;
import java.io.IOException;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.TimeUnit;

import javax.annotation.CheckForNull;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.github.dockerjava.api.async.ResultCallback;
import com.google.common.base.Throwables;

/**
* Abstract template implementation of {@link ResultCallback}
*
Expand Down Expand Up @@ -91,7 +88,7 @@ public void close() throws IOException {
public RC_T awaitCompletion() throws InterruptedException {
completed.await();
// eventually (re)throws RuntimeException
getFirstError();
throwFirstError();
return (RC_T) this;
}

Expand All @@ -102,13 +99,14 @@ public RC_T awaitCompletion() throws InterruptedException {
*/
public boolean awaitCompletion(long timeout, TimeUnit timeUnit) throws InterruptedException {
boolean result = completed.await(timeout, timeUnit);
getFirstError();
throwFirstError();
return result;
}

/**
* Blocks until {@link ResultCallback#onStart()} was called. {@link ResultCallback#onStart()} is called when the request was processed
* on the server side and the response is incoming.
* Blocks until {@link ResultCallback#onStart(Closeable)} was called.
* {@link ResultCallback#onStart(Closeable)} is called when the request was processed on the server
* side and the response is incoming.
*/
@SuppressWarnings("unchecked")
public RC_T awaitStarted() throws InterruptedException {
Expand All @@ -117,22 +115,25 @@ public RC_T awaitStarted() throws InterruptedException {
}

/**
* Blocks until {@link ResultCallback#onStart()} was called or the given timeout occurs. {@link ResultCallback#onStart()} is called when
* the request was processed on the server side and the response is incoming.
* Blocks until {@link ResultCallback#onStart(Closeable)} was called or the given timeout occurs.
* {@link ResultCallback#onStart(Closeable)} is called when the request was processed on the server side
* and the response is incoming.
* @return {@code true} if started and {@code false} if the waiting time elapsed
* before {@link ResultCallback#onStart()} was called.
* before {@link ResultCallback#onStart(Closeable)} was called.
*/
public boolean awaitStarted(long timeout, TimeUnit timeUnit) throws InterruptedException {
return started.await(timeout, timeUnit);
}

@CheckForNull
protected RuntimeException getFirstError() {
/**
* Throws the first occurred error as a runtime exception
* @throws com.github.dockerjava.api.exception.DockerException The first docker based Error
* @throws RuntimeException on any other occurred error
*/
protected void throwFirstError() {
if (firstError != null) {
// this call throws a RuntimeException
return Throwables.propagate(firstError);
} else {
return null;
Throwables.propagate(firstError);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,16 @@
*/
package com.github.dockerjava.core.command;

import javax.annotation.CheckForNull;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.github.dockerjava.api.exception.DockerClientException;
import com.github.dockerjava.api.model.PullResponseItem;
import com.github.dockerjava.core.async.ResultCallbackTemplate;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import javax.annotation.CheckForNull;
import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.TimeUnit;

/**
*
Expand All @@ -21,18 +23,99 @@ public class PullImageResultCallback extends ResultCallbackTemplate<PullImageRes

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

private boolean isSwarm = false;
private Map<String, PullResponseItem> results = null;

@CheckForNull
private PullResponseItem latestItem = null;

@Override
public void onNext(PullResponseItem item) {
this.latestItem = item;
// only do it once
if (results == null && latestItem == null) {
checkForDockerSwarmResponse(item);
}

if (isSwarm) {
handleDockerSwarmResponse(item);
} else {
handleDockerClientResponse(item);
}

LOGGER.debug(item.toString());
}

private void checkForDockerSwarmResponse(PullResponseItem item) {
if (item.getStatus().matches("Pulling\\s.+\\.{3}$")) {
isSwarm = true;
LOGGER.debug("Communicating with Docker Swarm.");
}
}

private void handleDockerSwarmResponse(final PullResponseItem item) {
if (results == null) {
results = new HashMap<>();
}

// Swarm terminates a pull sometimes with an empty line.
// Therefore keep first success message
PullResponseItem currentItem = results.get(item.getId());
if (currentItem == null || !currentItem.isPullSuccessIndicated()) {
results.put(item.getId(), item);
}
}

private void handleDockerClientResponse(PullResponseItem item) {
latestItem = item;
}

private void checkDockerSwarmPullSuccessful() {
if (results.isEmpty()) {
throw new DockerClientException("Could not pull image through Docker Swarm");
} else {
boolean pullFailed = false;
StringBuilder sb = new StringBuilder();

for (PullResponseItem pullResponseItem : results.values()) {
if (!pullResponseItem.isPullSuccessIndicated()) {
pullFailed = true;
sb.append("[" + pullResponseItem.getId() + ":" + messageFromPullResult(pullResponseItem) + "]");
}
}

if (pullFailed) {
throw new DockerClientException("Could not pull image: " + sb.toString());
}
}
}

private void checkDockerClientPullSuccessful() {
if (latestItem == null) {
throw new DockerClientException("Could not pull image");
} else if (!latestItem.isPullSuccessIndicated()) {
throw new DockerClientException("Could not pull image: " + messageFromPullResult(latestItem));
}
}

private String messageFromPullResult(PullResponseItem pullResponseItem) {
return (pullResponseItem.getError() != null) ? pullResponseItem.getError() : pullResponseItem.getStatus();
}

@Override
protected void throwFirstError() {
super.throwFirstError();

if (isSwarm) {
checkDockerSwarmPullSuccessful();
} else {
checkDockerClientPullSuccessful();
}
}

/**
* Awaits the image to be pulled successful.
*
* @deprecated use {@link #awaitCompletion()} or {@link #awaitCompletion(long, TimeUnit)} instead
* @throws DockerClientException
* if the pull fails.
*/
Expand All @@ -42,12 +125,5 @@ public void awaitSuccess() {
} catch (InterruptedException e) {
throw new DockerClientException("", e);
}

if (latestItem == null) {
throw new DockerClientException("Could not pull image");
} else if (!latestItem.isPullSuccessIndicated()) {
String message = (latestItem.getError() != null) ? latestItem.getError() : latestItem.getStatus();
throw new DockerClientException("Could not pull image: " + message);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,14 @@
*/
package com.github.dockerjava.core.command;

import javax.annotation.CheckForNull;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.github.dockerjava.api.exception.DockerClientException;
import com.github.dockerjava.api.model.PushResponseItem;
import com.github.dockerjava.core.async.ResultCallbackTemplate;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import javax.annotation.CheckForNull;
import java.util.concurrent.TimeUnit;

/**
*
Expand All @@ -30,9 +30,21 @@ public void onNext(PushResponseItem item) {
LOGGER.debug(item.toString());
}

@Override
protected void throwFirstError() {
super.throwFirstError();

if (latestItem == null) {
throw new DockerClientException("Could not push image");
} else if (latestItem.isErrorIndicated()) {
throw new DockerClientException("Could not push image: " + latestItem.getError());
}
}

/**
* Awaits the image to be pulled successful.
*
* @deprecated use {@link #awaitCompletion()} or {@link #awaitCompletion(long, TimeUnit)} instead
* @throws DockerClientException
* if the push fails.
*/
Expand All @@ -42,11 +54,5 @@ public void awaitSuccess() {
} catch (InterruptedException e) {
throw new DockerClientException("", e);
}

if (latestItem == null) {
throw new DockerClientException("Could not push image");
} else if (latestItem.isErrorIndicated()) {
throw new DockerClientException("Could not push image: " + latestItem.getError());
}
}
}
29 changes: 14 additions & 15 deletions src/main/java/com/github/dockerjava/netty/InvocationBuilder.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,18 @@
package com.github.dockerjava.netty;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.github.dockerjava.api.async.ResultCallback;
import com.github.dockerjava.api.exception.DockerClientException;
import com.github.dockerjava.api.model.Frame;
import com.github.dockerjava.core.async.ResultCallbackTemplate;
import com.github.dockerjava.netty.handler.FramedResponseStreamHandler;
import com.github.dockerjava.netty.handler.HttpConnectionHijackHandler;
import com.github.dockerjava.netty.handler.HttpRequestProvider;
import com.github.dockerjava.netty.handler.HttpResponseHandler;
import com.github.dockerjava.netty.handler.HttpResponseStreamHandler;
import com.github.dockerjava.netty.handler.JsonResponseCallbackHandler;
import io.netty.buffer.Unpooled;
import io.netty.channel.Channel;
import io.netty.channel.ChannelFuture;
Expand Down Expand Up @@ -29,20 +42,6 @@
import java.util.Map;
import java.util.concurrent.CountDownLatch;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.github.dockerjava.api.async.ResultCallback;
import com.github.dockerjava.api.exception.DockerClientException;
import com.github.dockerjava.api.model.Frame;
import com.github.dockerjava.core.async.ResultCallbackTemplate;
import com.github.dockerjava.netty.handler.FramedResponseStreamHandler;
import com.github.dockerjava.netty.handler.HttpConnectionHijackHandler;
import com.github.dockerjava.netty.handler.HttpRequestProvider;
import com.github.dockerjava.netty.handler.HttpResponseHandler;
import com.github.dockerjava.netty.handler.HttpResponseStreamHandler;
import com.github.dockerjava.netty.handler.JsonResponseCallbackHandler;

/**
* This class is basically a replacement of javax.ws.rs.client.Invocation.Builder to allow simpler migration of JAX-RS code to a netty based
* implementation.
Expand Down Expand Up @@ -122,7 +121,7 @@ public A_RES_T awaitResult() {
} catch (InterruptedException e) {
throw new RuntimeException(e);
}
getFirstError();
throwFirstError();
return result;
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -214,11 +214,6 @@ public void onNext(Frame item) {
super.onNext(item);
}

@Override
public RuntimeException getFirstError() {
return super.getFirstError();
}

@Override
public String toString() {
return log.toString();
Expand Down
Loading