|
| 1 | +/* |
| 2 | + * Created on 21.07.2015 |
| 3 | + */ |
| 4 | +package com.github.dockerjava.api.command; |
| 5 | + |
| 6 | +import com.github.dockerjava.api.async.ResultCallback; |
| 7 | +import com.github.dockerjava.api.exception.DockerClientException; |
| 8 | +import com.github.dockerjava.api.model.PullResponseItem; |
| 9 | +import org.slf4j.Logger; |
| 10 | +import org.slf4j.LoggerFactory; |
| 11 | + |
| 12 | +import javax.annotation.CheckForNull; |
| 13 | +import java.util.HashMap; |
| 14 | +import java.util.Map; |
| 15 | + |
| 16 | +/** |
| 17 | + * |
| 18 | + * @author Marcus Linke |
| 19 | + * |
| 20 | + */ |
| 21 | +class PullImageResultCallback extends ResultCallback.Adapter<PullResponseItem> { |
| 22 | + |
| 23 | + private static final Logger LOGGER = LoggerFactory.getLogger(PullImageResultCallback.class); |
| 24 | + |
| 25 | + private boolean isSwarm = false; |
| 26 | + private Map<String, PullResponseItem> results = null; |
| 27 | + |
| 28 | + @CheckForNull |
| 29 | + private PullResponseItem latestItem = null; |
| 30 | + |
| 31 | + @Override |
| 32 | + public void onNext(PullResponseItem item) { |
| 33 | + // only do it once |
| 34 | + if (results == null && latestItem == null) { |
| 35 | + checkForDockerSwarmResponse(item); |
| 36 | + } |
| 37 | + |
| 38 | + if (isSwarm) { |
| 39 | + handleDockerSwarmResponse(item); |
| 40 | + } else { |
| 41 | + handleDockerClientResponse(item); |
| 42 | + } |
| 43 | + |
| 44 | + LOGGER.debug(item.toString()); |
| 45 | + } |
| 46 | + |
| 47 | + private void checkForDockerSwarmResponse(PullResponseItem item) { |
| 48 | + if (item.getStatus().matches("Pulling\\s.+\\.{3}$")) { |
| 49 | + isSwarm = true; |
| 50 | + LOGGER.debug("Communicating with Docker Swarm."); |
| 51 | + } |
| 52 | + } |
| 53 | + |
| 54 | + private void handleDockerSwarmResponse(final PullResponseItem item) { |
| 55 | + if (results == null) { |
| 56 | + results = new HashMap<>(); |
| 57 | + } |
| 58 | + |
| 59 | + // Swarm terminates a pull sometimes with an empty line. |
| 60 | + // Therefore keep first success message |
| 61 | + PullResponseItem currentItem = results.get(item.getId()); |
| 62 | + if (currentItem == null || !currentItem.isPullSuccessIndicated()) { |
| 63 | + results.put(item.getId(), item); |
| 64 | + } |
| 65 | + } |
| 66 | + |
| 67 | + private void handleDockerClientResponse(PullResponseItem item) { |
| 68 | + latestItem = item; |
| 69 | + } |
| 70 | + |
| 71 | + private void checkDockerSwarmPullSuccessful() { |
| 72 | + if (results.isEmpty()) { |
| 73 | + throw new DockerClientException("Could not pull image through Docker Swarm"); |
| 74 | + } else { |
| 75 | + boolean pullFailed = false; |
| 76 | + StringBuilder sb = new StringBuilder(); |
| 77 | + |
| 78 | + for (PullResponseItem pullResponseItem : results.values()) { |
| 79 | + if (!pullResponseItem.isPullSuccessIndicated()) { |
| 80 | + pullFailed = true; |
| 81 | + sb.append("[" + pullResponseItem.getId() + ":" + messageFromPullResult(pullResponseItem) + "]"); |
| 82 | + } |
| 83 | + } |
| 84 | + |
| 85 | + if (pullFailed) { |
| 86 | + throw new DockerClientException("Could not pull image: " + sb.toString()); |
| 87 | + } |
| 88 | + } |
| 89 | + } |
| 90 | + |
| 91 | + private void checkDockerClientPullSuccessful() { |
| 92 | + if (latestItem == null) { |
| 93 | + throw new DockerClientException("Could not pull image"); |
| 94 | + } else if (!latestItem.isPullSuccessIndicated()) { |
| 95 | + throw new DockerClientException("Could not pull image: " + messageFromPullResult(latestItem)); |
| 96 | + } |
| 97 | + } |
| 98 | + |
| 99 | + private String messageFromPullResult(PullResponseItem pullResponseItem) { |
| 100 | + return (pullResponseItem.getError() != null) ? pullResponseItem.getError() : pullResponseItem.getStatus(); |
| 101 | + } |
| 102 | + |
| 103 | + @Override |
| 104 | + protected void throwFirstError() { |
| 105 | + super.throwFirstError(); |
| 106 | + |
| 107 | + if (isSwarm) { |
| 108 | + checkDockerSwarmPullSuccessful(); |
| 109 | + } else { |
| 110 | + checkDockerClientPullSuccessful(); |
| 111 | + } |
| 112 | + } |
| 113 | +} |
0 commit comments