Skip to content

Commit 4e66441

Browse files
hawky-4s-KostyaSha
authored andcommitted
Improve PullImageCmd for pulling an image through docker swarm.
1 parent 7494bc8 commit 4e66441

File tree

1 file changed

+69
-6
lines changed

1 file changed

+69
-6
lines changed

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

Lines changed: 69 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,11 @@
1111
import com.github.dockerjava.api.exception.DockerClientException;
1212
import com.github.dockerjava.api.model.PullResponseItem;
1313
import com.github.dockerjava.core.async.ResultCallbackTemplate;
14+
import org.slf4j.Logger;
15+
import org.slf4j.LoggerFactory;
16+
17+
import java.util.HashMap;
18+
import java.util.Map;
1419

1520
/**
1621
*
@@ -21,15 +26,74 @@ public class PullImageResultCallback extends ResultCallbackTemplate<PullImageRes
2126

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

29+
private boolean isSwarm = false;
30+
private Map<String, PullResponseItem> results = null;
31+
2432
@CheckForNull
2533
private PullResponseItem latestItem = null;
2634

2735
@Override
2836
public void onNext(PullResponseItem item) {
29-
this.latestItem = item;
37+
// only do it once
38+
if (results == null && latestItem == null) {
39+
checkForDockerSwarmResponse(item);
40+
}
41+
42+
if (isSwarm) {
43+
handleDockerSwarmResponse(item);
44+
} else {
45+
handleDockerClientResponse(item);
46+
}
3047
LOGGER.debug(item.toString());
3148
}
3249

50+
private void checkForDockerSwarmResponse(PullResponseItem item) {
51+
if (item.getStatus().matches("Pulling\\s.+\\.{3}$")) {
52+
isSwarm = true;
53+
LOGGER.debug("Communicating with Docker Swarm.");
54+
}
55+
}
56+
57+
private void handleDockerSwarmResponse(PullResponseItem item) {
58+
if (results == null) {
59+
results = new HashMap<String, PullResponseItem>();
60+
}
61+
results.put(item.getId(), item);
62+
}
63+
64+
private void checkDockerSwarmPullSuccessful() {
65+
if (results.isEmpty()) {
66+
throw new DockerClientException("Could not pull image");
67+
} else {
68+
boolean pullFailed = false;
69+
StringBuilder sb = new StringBuilder();
70+
71+
for (PullResponseItem pullResponseItem : results.values()) {
72+
if (!pullResponseItem.isPullSuccessIndicated()) {
73+
pullFailed = true;
74+
sb.append("[" + pullResponseItem.getId() + ";" + pullResponseItem.getError() + "]");
75+
}
76+
}
77+
78+
if (pullFailed) {
79+
throw new DockerClientException("Could not pull image: " + sb.toString());
80+
}
81+
}
82+
}
83+
84+
private void handleDockerClientResponse(PullResponseItem item) {
85+
latestItem = item;
86+
}
87+
88+
private void checkDockerClientPullSuccessful() {
89+
if (latestItem == null) {
90+
throw new DockerClientException("Could not pull image");
91+
} else if (!latestItem.isPullSuccessIndicated()) {
92+
String message = (latestItem.getError() != null) ? latestItem.getError() : latestItem.getStatus();
93+
throw new DockerClientException("Could not pull image: " + message);
94+
}
95+
}
96+
3397
/**
3498
* Awaits the image to be pulled successful.
3599
*
@@ -43,11 +107,10 @@ public void awaitSuccess() {
43107
throw new DockerClientException("", e);
44108
}
45109

46-
if (latestItem == null) {
47-
throw new DockerClientException("Could not pull image");
48-
} else if (!latestItem.isPullSuccessIndicated()) {
49-
String message = (latestItem.getError() != null) ? latestItem.getError() : latestItem.getStatus();
50-
throw new DockerClientException("Could not pull image: " + message);
110+
if (isSwarm) {
111+
checkDockerSwarmPullSuccessful();
112+
} else {
113+
checkDockerClientPullSuccessful();
51114
}
52115
}
53116
}

0 commit comments

Comments
 (0)