Skip to content

Commit a39e52d

Browse files
committed
Improve PullImageCmd for pulling an image through docker swarm.
1 parent 2eb297e commit a39e52d

File tree

2 files changed

+68
-9
lines changed

2 files changed

+68
-9
lines changed

src/main/java/com/github/dockerjava/api/model/PullResponseItem.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ public boolean isPullSuccessIndicated() {
2323
}
2424

2525
return (getStatus().contains("Download complete") || getStatus().contains("Image is up to date") || getStatus()
26-
.contains("Downloaded newer image"));
26+
.contains("Downloaded newer image") || getStatus().contains(": downloaded"));
2727
}
2828

2929
}

src/main/java/com/github/dockerjava/core/command/PullImageResultCallback.java

Lines changed: 67 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,14 @@
33
*/
44
package com.github.dockerjava.core.command;
55

6-
import org.slf4j.Logger;
7-
import org.slf4j.LoggerFactory;
8-
96
import com.github.dockerjava.api.DockerClientException;
107
import com.github.dockerjava.api.model.PullResponseItem;
118
import com.github.dockerjava.core.async.ResultCallbackTemplate;
9+
import org.slf4j.Logger;
10+
import org.slf4j.LoggerFactory;
11+
12+
import java.util.HashMap;
13+
import java.util.Map;
1214

1315
/**
1416
*
@@ -19,14 +21,71 @@ public class PullImageResultCallback extends ResultCallbackTemplate<PullImageRes
1921

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

24+
private boolean isSwarm = false;
25+
private Map<String, PullResponseItem> results = null;
2226
private PullResponseItem latestItem = null;
2327

2428
@Override
2529
public void onNext(PullResponseItem item) {
26-
this.latestItem = item;
30+
// only do it once
31+
if (results == null && latestItem == null) {
32+
checkForDockerSwarmResponse(item);
33+
}
34+
35+
if (isSwarm) {
36+
handleDockerSwarmResponse(item);
37+
} else {
38+
handleDockerClientResponse(item);
39+
}
2740
LOGGER.debug(item.toString());
2841
}
2942

43+
private void checkForDockerSwarmResponse(PullResponseItem item) {
44+
if (item.getStatus().matches("Pulling\\s.+\\.{3}$")) {
45+
isSwarm = true;
46+
LOGGER.debug("Communicating with Docker Swarm.");
47+
}
48+
}
49+
50+
private void handleDockerSwarmResponse(PullResponseItem item) {
51+
if (results == null) {
52+
results = new HashMap<String, PullResponseItem>();
53+
}
54+
results.put(item.getId(), item);
55+
}
56+
57+
private void checkDockerSwarmPullSuccessful() {
58+
if (results.isEmpty()) {
59+
throw new DockerClientException("Could not pull image");
60+
} else {
61+
boolean pullFailed = false;
62+
StringBuilder sb = new StringBuilder();
63+
64+
for (PullResponseItem pullResponseItem : results.values()) {
65+
if (!pullResponseItem.isPullSuccessIndicated()) {
66+
pullFailed = true;
67+
sb.append("[" + pullResponseItem.getId() + ";" + pullResponseItem.getError() + "]");
68+
}
69+
}
70+
71+
if (pullFailed) {
72+
throw new DockerClientException("Could not pull image: " + sb.toString());
73+
}
74+
}
75+
}
76+
77+
private void handleDockerClientResponse(PullResponseItem item) {
78+
latestItem = item;
79+
}
80+
81+
private void checkDockerClientPullSuccessful() {
82+
if (latestItem == null) {
83+
throw new DockerClientException("Could not pull image");
84+
} else if (!latestItem.isPullSuccessIndicated()) {
85+
throw new DockerClientException("Could not pull image: " + latestItem.getError());
86+
}
87+
}
88+
3089
/**
3190
* Awaits the image to be pulled successful.
3291
*
@@ -40,10 +99,10 @@ public void awaitSuccess() {
4099
throw new DockerClientException("", e);
41100
}
42101

43-
if (latestItem == null) {
44-
throw new DockerClientException("Could not pull image");
45-
} else if (!latestItem.isPullSuccessIndicated()) {
46-
throw new DockerClientException("Could not pull image: " + latestItem.getError());
102+
if (isSwarm) {
103+
checkDockerSwarmPullSuccessful();
104+
} else {
105+
checkDockerClientPullSuccessful();
47106
}
48107
}
49108
}

0 commit comments

Comments
 (0)