|
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; |
@@ -257,10 +260,58 @@ private DockerClient createClient() { |
257 | 260 | } |
258 | 261 |
|
259 | 262 | private String buildImage(DockerClient docker) { |
260 | | - BuildImageResultCallback resultCallback = new BuildImageResultCallback(); |
261 | 263 | String dockerfile = "src/test/resources/conf/" + this.os + "/" + this.version |
262 | 264 | + "/Dockerfile"; |
263 | 265 | String tag = "spring-boot-it/" + this.os.toLowerCase() + ":" + this.version; |
| 266 | + BuildImageResultCallback resultCallback = new BuildImageResultCallback() { |
| 267 | + |
| 268 | + private List<BuildResponseItem> items = new ArrayList<BuildResponseItem>(); |
| 269 | + |
| 270 | + @Override |
| 271 | + public void onNext(BuildResponseItem item) { |
| 272 | + super.onNext(item); |
| 273 | + this.items.add(item); |
| 274 | + } |
| 275 | + |
| 276 | + @Override |
| 277 | + public String awaitImageId() { |
| 278 | + try { |
| 279 | + awaitCompletion(); |
| 280 | + } |
| 281 | + catch (InterruptedException ex) { |
| 282 | + throw new DockerClientException( |
| 283 | + "Interrupted while waiting for image id", ex); |
| 284 | + } |
| 285 | + return getImageId(); |
| 286 | + } |
| 287 | + |
| 288 | + @SuppressWarnings("deprecation") |
| 289 | + private String getImageId() { |
| 290 | + if (this.items.isEmpty()) { |
| 291 | + throw new DockerClientException("Could not build image"); |
| 292 | + } |
| 293 | + String imageId = extractImageId(); |
| 294 | + if (imageId == null) { |
| 295 | + throw new DockerClientException("Could not build image: " |
| 296 | + + this.items.get(this.items.size() - 1).getError()); |
| 297 | + } |
| 298 | + return imageId; |
| 299 | + } |
| 300 | + |
| 301 | + private String extractImageId() { |
| 302 | + Collections.reverse(this.items); |
| 303 | + for (BuildResponseItem item : this.items) { |
| 304 | + if (item.isErrorIndicated() || item.getStream() == null) { |
| 305 | + return null; |
| 306 | + } |
| 307 | + if (item.getStream().contains("Successfully built")) { |
| 308 | + return item.getStream().replace("Successfully built", "").trim(); |
| 309 | + } |
| 310 | + } |
| 311 | + return null; |
| 312 | + } |
| 313 | + |
| 314 | + }; |
264 | 315 | docker.buildImageCmd(new File(dockerfile)).withTag(tag).exec(resultCallback); |
265 | 316 | String imageId = resultCallback.awaitImageId(); |
266 | 317 | return imageId; |
|
0 commit comments