forked from mwiede/docker-java
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPullImageResultCallback.java
More file actions
115 lines (94 loc) · 3.41 KB
/
Copy pathPullImageResultCallback.java
File metadata and controls
115 lines (94 loc) · 3.41 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
/*
* Created on 21.07.2015
*/
package com.github.dockerjava.api.command;
import com.github.dockerjava.api.async.ResultCallback;
import com.github.dockerjava.api.exception.DockerClientException;
import com.github.dockerjava.api.model.PullResponseItem;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import javax.annotation.CheckForNull;
import java.util.HashMap;
import java.util.Map;
/**
*
* @author Marcus Linke
*
*/
public class PullImageResultCallback extends ResultCallback.Adapter<PullResponseItem> {
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) {
// 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) {
return;
}
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();
}
}
}