|
22 | 22 | import java.io.InputStream;
|
23 | 23 | import java.util.ArrayList;
|
24 | 24 | import java.util.Arrays;
|
| 25 | +import java.util.Collections; |
25 | 26 | import java.util.List;
|
26 | 27 | import java.util.concurrent.TimeUnit;
|
27 | 28 | import java.util.regex.Pattern;
|
|
32 | 33 | import javax.ws.rs.client.WebTarget;
|
33 | 34 |
|
34 | 35 | import com.github.dockerjava.api.DockerClient;
|
| 36 | +import com.github.dockerjava.api.DockerClientException; |
35 | 37 | import com.github.dockerjava.api.command.DockerCmd;
|
| 38 | +import com.github.dockerjava.api.model.BuildResponseItem; |
36 | 39 | import com.github.dockerjava.api.model.Frame;
|
37 | 40 | import com.github.dockerjava.core.CompressArchiveUtil;
|
38 | 41 | import com.github.dockerjava.core.DockerClientBuilder;
|
@@ -265,10 +268,58 @@ private DockerClient createClient() {
|
265 | 268 | }
|
266 | 269 |
|
267 | 270 | private String buildImage(DockerClient docker) {
|
268 |
| - BuildImageResultCallback resultCallback = new BuildImageResultCallback(); |
269 | 271 | String dockerfile = "src/test/resources/conf/" + this.os + "/" + this.version
|
270 | 272 | + "/Dockerfile";
|
271 | 273 | String tag = "spring-boot-it/" + this.os.toLowerCase() + ":" + this.version;
|
| 274 | + BuildImageResultCallback resultCallback = new BuildImageResultCallback() { |
| 275 | + |
| 276 | + private List<BuildResponseItem> items = new ArrayList<BuildResponseItem>(); |
| 277 | + |
| 278 | + @Override |
| 279 | + public void onNext(BuildResponseItem item) { |
| 280 | + super.onNext(item); |
| 281 | + this.items.add(item); |
| 282 | + } |
| 283 | + |
| 284 | + @Override |
| 285 | + public String awaitImageId() { |
| 286 | + try { |
| 287 | + awaitCompletion(); |
| 288 | + } |
| 289 | + catch (InterruptedException ex) { |
| 290 | + throw new DockerClientException( |
| 291 | + "Interrupted while waiting for image id", ex); |
| 292 | + } |
| 293 | + return getImageId(); |
| 294 | + } |
| 295 | + |
| 296 | + @SuppressWarnings("deprecation") |
| 297 | + private String getImageId() { |
| 298 | + if (this.items.isEmpty()) { |
| 299 | + throw new DockerClientException("Could not build image"); |
| 300 | + } |
| 301 | + String imageId = extractImageId(); |
| 302 | + if (imageId == null) { |
| 303 | + throw new DockerClientException("Could not build image: " |
| 304 | + + this.items.get(this.items.size() - 1).getError()); |
| 305 | + } |
| 306 | + return imageId; |
| 307 | + } |
| 308 | + |
| 309 | + private String extractImageId() { |
| 310 | + Collections.reverse(this.items); |
| 311 | + for (BuildResponseItem item : this.items) { |
| 312 | + if (item.isErrorIndicated() || item.getStream() == null) { |
| 313 | + return null; |
| 314 | + } |
| 315 | + if (item.getStream().contains("Successfully built")) { |
| 316 | + return item.getStream().replace("Successfully built", "").trim(); |
| 317 | + } |
| 318 | + } |
| 319 | + return null; |
| 320 | + } |
| 321 | + |
| 322 | + }; |
272 | 323 | docker.buildImageCmd(new File(dockerfile)).withTag(tag).exec(resultCallback);
|
273 | 324 | String imageId = resultCallback.awaitImageId();
|
274 | 325 | return imageId;
|
|
0 commit comments