com.github.dockerjava.api.command.PullImageCmd Java Examples

The following examples show how to use com.github.dockerjava.api.command.PullImageCmd. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: DockerImplTest.java    From vespa with Apache License 2.0 6 votes vote down vote up
@Test
@SuppressWarnings({"unchecked", "rawtypes"})
public void pullImageAsyncIfNeededWithError() {
    final DockerImage image = DockerImage.fromString("test:1.2.3");

    InspectImageCmd imageInspectCmd = mock(InspectImageCmd.class);
    when(imageInspectCmd.exec()).thenThrow(new NotFoundException("Image not found"));

    // Array to make it final
    ArgumentCaptor<ResultCallback> resultCallback = ArgumentCaptor.forClass(ResultCallback.class);
    PullImageCmd pullImageCmd = mock(PullImageCmd.class);
    when(pullImageCmd.exec(resultCallback.capture())).thenReturn(null);

    when(dockerClient.inspectImageCmd(image.asString())).thenReturn(imageInspectCmd);
    when(dockerClient.pullImageCmd(eq(image.asString()))).thenReturn(pullImageCmd);

    assertTrue("Should return true, we just scheduled the pull", docker.pullImageAsyncIfNeeded(image));
    assertTrue("Should return true, the pull i still ongoing", docker.pullImageAsyncIfNeeded(image));

    try {
        resultCallback.getValue().onComplete();
    } catch (Exception ignored) { }

    assertFalse(docker.imageIsDownloaded(image));
    assertTrue("Should return true, new pull scheduled", docker.pullImageAsyncIfNeeded(image));
}
 
Example #2
Source File: PullImageCmdExec.java    From docker-java with Apache License 2.0 6 votes vote down vote up
@Override
protected Void execute0(PullImageCmd command, ResultCallback<PullResponseItem> resultCallback) {

    WebTarget webResource = getBaseResource().path("/images/create").queryParam("tag", command.getTag())
            .queryParam("fromImage", command.getRepository()).queryParam("registry", command.getRegistry());

    if (command.getPlatform() != null) {
        webResource = webResource.queryParam("platform", command.getPlatform());
    }

    LOGGER.trace("POST: {}", webResource);
    resourceWithOptionalAuthConfig(command.getAuthConfig(), webResource.request())
            .accept(MediaType.APPLICATION_OCTET_STREAM)
            .post(null, new TypeReference<PullResponseItem>() {
            }, resultCallback);

    return null;
}
 
Example #3
Source File: DockerBuilderControlOptionRun.java    From docker-plugin with MIT License 6 votes vote down vote up
private void executePullOnDocker(Run<?, ?> build, PrintStream llog, String xImage, DockerClient client)
        throws DockerException {
    PullImageResultCallback resultCallback = new PullImageResultCallback() {
        @Override
        public void onNext(PullResponseItem item) {
            if (item.getStatus() != null && item.getProgress() == null) {
                llog.print(item.getId() + ":" + item.getStatus());
                LOG.info("{} : {}", item.getId(), item.getStatus());
            }
            super.onNext(item);
        }
    };

    PullImageCmd cmd = client.pullImageCmd(xImage);
    DockerCloud.setRegistryAuthentication(cmd, getRegistry(), build.getParent().getParent());
    try {
        cmd.exec(resultCallback).awaitCompletion();
    } catch (InterruptedException e) {
        throw new DockerClientException("Interrupted while pulling image", e);
    }
}
 
Example #4
Source File: DockerImplTest.java    From vespa with Apache License 2.0 5 votes vote down vote up
@Test
@SuppressWarnings({"unchecked", "rawtypes"})
public void pullImageAsyncIfNeededSuccessfully() {
    final DockerImage image = DockerImage.fromString("test:1.2.3");

    InspectImageResponse inspectImageResponse = mock(InspectImageResponse.class);
    when(inspectImageResponse.getId()).thenReturn(image.asString());

    InspectImageCmd imageInspectCmd = mock(InspectImageCmd.class);
    when(imageInspectCmd.exec())
            .thenThrow(new NotFoundException("Image not found"))
            .thenReturn(inspectImageResponse);

    // Array to make it final
    ArgumentCaptor<ResultCallback> resultCallback = ArgumentCaptor.forClass(ResultCallback.class);
    PullImageCmd pullImageCmd = mock(PullImageCmd.class);
    when(pullImageCmd.exec(resultCallback.capture())).thenReturn(null);

    when(dockerClient.inspectImageCmd(image.asString())).thenReturn(imageInspectCmd);
    when(dockerClient.pullImageCmd(eq(image.asString()))).thenReturn(pullImageCmd);

    assertTrue("Should return true, we just scheduled the pull", docker.pullImageAsyncIfNeeded(image));
    assertTrue("Should return true, the pull i still ongoing", docker.pullImageAsyncIfNeeded(image));

    assertTrue(docker.imageIsDownloaded(image));
    resultCallback.getValue().onComplete();
    assertFalse(docker.pullImageAsyncIfNeeded(image));
}
 
Example #5
Source File: DockerCloud.java    From docker-plugin with MIT License 5 votes vote down vote up
@Restricted(NoExternalUse.class)
public static void setRegistryAuthentication(PullImageCmd cmd, DockerRegistryEndpoint registry, ItemGroup context) {
    if (registry != null && registry.getCredentialsId() != null) {
        AuthConfig auth = getAuthConfig(registry, context);
        cmd.withAuthConfig(auth);
    }
}
 
Example #6
Source File: DockerTemplate.java    From docker-plugin with MIT License 5 votes vote down vote up
@Nonnull
InspectImageResponse pullImage(DockerAPI api, TaskListener listener) throws IOException, InterruptedException {
    final String image = getFullImageId();

    final boolean shouldPullImage;
    try(final DockerClient client = api.getClient()) {
        shouldPullImage = getPullStrategy().shouldPullImage(client, image);
    }
    if (shouldPullImage) {
        // TODO create a FlyWeightTask so end-user get visibility on pull operation progress
        LOGGER.info("Pulling image '{}'. This may take awhile...", image);

        long startTime = System.currentTimeMillis();

        try(final DockerClient client = api.getClient(pullTimeout)) {
            final PullImageCmd cmd =  client.pullImageCmd(image);
            final DockerRegistryEndpoint registry = getRegistry();
            DockerCloud.setRegistryAuthentication(cmd, registry, Jenkins.getInstance());
            cmd.exec(new PullImageResultCallback() {
                @Override
                public void onNext(PullResponseItem item) {
                    super.onNext(item);
                    listener.getLogger().println(item.getStatus());
                }
            }).awaitCompletion();
        }

        long pullTime = System.currentTimeMillis() - startTime;
        LOGGER.info("Finished pulling image '{}', took {} ms", image, pullTime);
    }

    final InspectImageResponse result;
    try(final DockerClient client = api.getClient()) {
        result = client.inspectImageCmd(image).exec();
    } catch (NotFoundException e) {
        throw new DockerClientException("Could not pull image: " + image, e);
    }
    return result;
}
 
Example #7
Source File: DockerClientImpl.java    From docker-java with Apache License 2.0 4 votes vote down vote up
/**
 * * IMAGE API *
 */
@Override
public PullImageCmd pullImageCmd(String repository) {
    return new PullImageCmdImpl(getDockerCmdExecFactory().createPullImageCmdExec(),
            dockerClientConfig.effectiveAuthConfig(repository), repository);
}
 
Example #8
Source File: AbstractDockerCmdExecFactory.java    From docker-java with Apache License 2.0 4 votes vote down vote up
@Override
public PullImageCmd.Exec createPullImageCmdExec() {
    return new PullImageCmdExec(getBaseResource(), getDockerClientConfig());
}
 
Example #9
Source File: PullImageCmdImpl.java    From docker-java with Apache License 2.0 4 votes vote down vote up
public PullImageCmdImpl(PullImageCmd.Exec exec, AuthConfig authConfig, String repository) {
    super(exec);
    withAuthConfig(authConfig);
    withRepository(repository);
}
 
Example #10
Source File: PullImageCmdImpl.java    From docker-java with Apache License 2.0 4 votes vote down vote up
public PullImageCmd withAuthConfig(AuthConfig authConfig) {
    this.authConfig = authConfig;
    return this;
}
 
Example #11
Source File: PullImageCmdImpl.java    From docker-java with Apache License 2.0 4 votes vote down vote up
@Override
public PullImageCmd withRepository(String repository) {
    checkNotNull(repository, "repository was not specified");
    this.repository = repository;
    return this;
}
 
Example #12
Source File: PullImageCmdImpl.java    From docker-java with Apache License 2.0 4 votes vote down vote up
@Override
public PullImageCmd withTag(String tag) {
    checkNotNull(tag, "tag was not specified");
    this.tag = tag;
    return this;
}
 
Example #13
Source File: PullImageCmdImpl.java    From docker-java with Apache License 2.0 4 votes vote down vote up
@Override
public PullImageCmd withPlatform(String platform) {
    this.platform = platform;
    return this;
}
 
Example #14
Source File: PullImageCmdImpl.java    From docker-java with Apache License 2.0 4 votes vote down vote up
@Override
public PullImageCmd withRegistry(String registry) {
    checkNotNull(registry, "registry was not specified");
    this.registry = registry;
    return this;
}
 
Example #15
Source File: DelegatingDockerClient.java    From docker-plugin with MIT License 4 votes vote down vote up
@Override
public PullImageCmd pullImageCmd(String arg0) {
    return getDelegate().pullImageCmd(arg0);
}
 
Example #16
Source File: DockerClient.java    From docker-java with Apache License 2.0 votes vote down vote up
/**
 * * IMAGE API *
 */

PullImageCmd pullImageCmd(@Nonnull String repository);