From f53ef6e876e12b573d772b2f21e22517a86dacff Mon Sep 17 00:00:00 2001 From: Kanstantsin Shautsou Date: Sun, 22 Oct 2017 05:10:34 +0300 Subject: [PATCH] Temporary workaround when image pulled but docker-java fails. (#207) * Temporary workaround when image pulled but docker-java fails. * And swarm also * Checkstyle --- .../yad/commons/DockerPullImage.java | 32 ++++++++++++++----- .../yad/commons/DockerPullImageTest.java | 3 ++ 2 files changed, 27 insertions(+), 8 deletions(-) diff --git a/yet-another-docker-plugin/src/main/java/com/github/kostyasha/yad/commons/DockerPullImage.java b/yet-another-docker-plugin/src/main/java/com/github/kostyasha/yad/commons/DockerPullImage.java index ea53457d..4472c7e9 100644 --- a/yet-another-docker-plugin/src/main/java/com/github/kostyasha/yad/commons/DockerPullImage.java +++ b/yet-another-docker-plugin/src/main/java/com/github/kostyasha/yad/commons/DockerPullImage.java @@ -8,6 +8,8 @@ import com.github.kostyasha.yad.credentials.DockerRegistryAuthCredentials; import com.github.kostyasha.yad_docker_java.com.github.dockerjava.api.DockerClient; import com.github.kostyasha.yad_docker_java.com.github.dockerjava.api.command.PullImageCmd; +import com.github.kostyasha.yad_docker_java.com.github.dockerjava.api.exception.DockerClientException; +import com.github.kostyasha.yad_docker_java.com.github.dockerjava.api.exception.NotFoundException; import com.github.kostyasha.yad_docker_java.com.github.dockerjava.api.model.Image; import com.github.kostyasha.yad_docker_java.com.github.dockerjava.core.NameParser; import com.github.kostyasha.yad_docker_java.org.apache.commons.lang.StringUtils; @@ -127,9 +129,23 @@ public void exec(@Nonnull final DockerClient client, @Nonnull final String image } } - pullImageCmd - .exec(new DockerPullImageListenerLogger(listener)) - .awaitSuccess(); + try { + pullImageCmd + .exec(new DockerPullImageListenerLogger(listener)) + .awaitSuccess(); + } catch (DockerClientException exception) { + String exMsg = exception.getMessage(); + if (exMsg.contains("Could not pull image: Digest:") || + exMsg.contains(": downloaded")) { + try { + client.inspectImageCmd(imageName).exec(); + } catch (NotFoundException notFoundEx) { + throw exception; + } + } else { + throw exception; + } + } long pullTime = System.currentTimeMillis() - startTime; LOG.info("Finished pulling image '{}', took {} ms", imageName, pullTime); @@ -155,11 +171,11 @@ protected boolean shouldPullImage(DockerClient client, String imageName) { List images = client.listImagesCmd().exec(); boolean hasImage = images.stream().anyMatch(image -> - nonNull(image.getRepoTags()) && - Arrays.stream(image.getRepoTags()) - .anyMatch(repoTag -> - repoTag.contains(fullImageName) || repoTag.contains("docker.io/" + fullImageName) - ) + nonNull(image.getRepoTags()) && + Arrays.stream(image.getRepoTags()) + .anyMatch(repoTag -> + repoTag.contains(fullImageName) || repoTag.contains("docker.io/" + fullImageName) + ) ); return hasImage ? diff --git a/yet-another-docker-plugin/src/test/java/com/github/kostyasha/yad/commons/DockerPullImageTest.java b/yet-another-docker-plugin/src/test/java/com/github/kostyasha/yad/commons/DockerPullImageTest.java index 5a0c2598..7386beb0 100644 --- a/yet-another-docker-plugin/src/test/java/com/github/kostyasha/yad/commons/DockerPullImageTest.java +++ b/yet-another-docker-plugin/src/test/java/com/github/kostyasha/yad/commons/DockerPullImageTest.java @@ -34,6 +34,8 @@ public static Collection data() { return Arrays.asList(new Object[][]{ {"docker.io/kostyasha/yet-another-docker-plugin:wget-master", "kostyasha/yet-another-docker-plugin:wget-master", PULL_LATEST, false}, + {"kostyasha/yet-another-docker-plugin:wget-master", + "kostyasha/yet-another-docker-plugin:wget-master", PULL_LATEST, false}, {"", "repo/name", PULL_LATEST, true}, {"repo/name:latest", "repo/name", PULL_LATEST, true}, {"", "repo/name:latest", PULL_LATEST, true}, @@ -48,6 +50,7 @@ public static Collection data() { {"", "repo/name:1.0", PULL_ALWAYS, true}, {"repo/name:1.0", "repo/name:1.0", PULL_ALWAYS, true}, + {"", "repo/name", PULL_NEVER, false}, {"repo/name:latest", "repo/name", PULL_NEVER, false}, {"", "repo/name:latest", PULL_NEVER, false},