Search in sources :

Example 1 with PullImageCmd

use of com.github.dockerjava.api.command.PullImageCmd in project camel by apache.

the class AsyncDockerProducer method executePullImageRequest.

/**
     * Produces a pull image request
     *
     * @param client
     * @param message
     * @return
     */
private PullImageCmd executePullImageRequest(DockerClient client, Message message) {
    LOGGER.debug("Executing Docker Pull Image Request");
    String repository = DockerHelper.getProperty(DockerConstants.DOCKER_REPOSITORY, configuration, message, String.class);
    ObjectHelper.notNull(repository, "Repository must be specified");
    PullImageCmd pullImageCmd = client.pullImageCmd(repository);
    String registry = DockerHelper.getProperty(DockerConstants.DOCKER_REGISTRY, configuration, message, String.class);
    if (registry != null) {
        pullImageCmd.withRegistry(registry);
    }
    String tag = DockerHelper.getProperty(DockerConstants.DOCKER_TAG, configuration, message, String.class);
    if (tag != null) {
        pullImageCmd.withTag(tag);
    }
    AuthConfig authConfig = client.authConfig();
    if (authConfig != null) {
        pullImageCmd.withAuthConfig(authConfig);
    }
    return pullImageCmd;
}
Also used : PullImageCmd(com.github.dockerjava.api.command.PullImageCmd) AuthConfig(com.github.dockerjava.api.model.AuthConfig)

Example 2 with PullImageCmd

use of com.github.dockerjava.api.command.PullImageCmd in project dockerunit by qzagarese.

the class DefaultServiceBuilder method pullImage.

private CompletableFuture<Void> pullImage(CreateContainerCmd cmd, DockerClient client) {
    PullImageCmd pullImageCmd = client.pullImageCmd(cmd.getImage());
    CompletableFuture<Void> pullFut = new CompletableFuture<Void>();
    ResultCallback<PullResponseItem> resultCallback = new ResultCallback<PullResponseItem>() {

        private Closeable closeable;

        @Override
        public void close() throws IOException {
            try {
                closeable.close();
            } catch (IOException e) {
                throw new RuntimeException("Cannot close closeable " + closeable, e);
            }
        }

        @Override
        public void onStart(Closeable closeable) {
            this.closeable = closeable;
        }

        @Override
        public void onNext(PullResponseItem object) {
            if (object.getId() != null) {
                logger.info("Pulling image " + object.getId() + "...");
            }
        }

        @Override
        public void onError(Throwable throwable) {
            pullFut.completeExceptionally(new RuntimeException("Failed pulling image " + cmd.getImage(), throwable));
        }

        @Override
        public void onComplete() {
            pullFut.complete(null);
        }
    };
    pullImageCmd.exec(resultCallback);
    return pullFut;
}
Also used : ResultCallback(com.github.dockerjava.api.async.ResultCallback) CompletableFuture(java.util.concurrent.CompletableFuture) PullImageCmd(com.github.dockerjava.api.command.PullImageCmd) Closeable(java.io.Closeable) IOException(java.io.IOException) PullResponseItem(com.github.dockerjava.api.model.PullResponseItem)

Example 3 with PullImageCmd

use of com.github.dockerjava.api.command.PullImageCmd in project hub-docker-inspector by blackducksoftware.

the class DockerClientManager method pullImage.

public String pullImage(final String imageName, final String tagName) throws HubIntegrationException {
    logger.info(String.format("Pulling image %s:%s", imageName, tagName));
    final DockerClient dockerClient = hubDockerClient.getDockerClient();
    final Image alreadyPulledImage = getLocalImage(dockerClient, imageName, tagName);
    if (alreadyPulledImage == null) {
        // Only pull if we dont already have it
        final PullImageCmd pull = dockerClient.pullImageCmd(imageName).withTag(tagName);
        try {
            pull.exec(new PullImageResultCallback()).awaitSuccess();
        } catch (final NotFoundException e) {
            final String msg = String.format("Pull failed: Image %s:%s not found. Please check the image name/tag", imageName, tagName);
            logger.error(msg);
            throw new HubIntegrationException(msg, e);
        }
        final Image justPulledImage = getLocalImage(dockerClient, imageName, tagName);
        if (justPulledImage == null) {
            final String msg = String.format("Pulled image %s:%s not found in image list.", imageName, tagName);
            logger.error(msg);
            throw new HubIntegrationException(msg);
        }
        return justPulledImage.getId();
    } else {
        logger.info("Image already pulled");
        return alreadyPulledImage.getId();
    }
}
Also used : DockerClient(com.github.dockerjava.api.DockerClient) PullImageCmd(com.github.dockerjava.api.command.PullImageCmd) PullImageResultCallback(com.github.dockerjava.core.command.PullImageResultCallback) NotFoundException(com.github.dockerjava.api.exception.NotFoundException) Image(com.github.dockerjava.api.model.Image) HubIntegrationException(com.blackducksoftware.integration.hub.exception.HubIntegrationException)

Example 4 with PullImageCmd

use of com.github.dockerjava.api.command.PullImageCmd in project vespa by vespa-engine.

the class DockerImplTest method pullImageAsyncIfNeededSuccessfully.

@Test
@SuppressWarnings({ "unchecked", "rawtypes" })
public void pullImageAsyncIfNeededSuccessfully() {
    final DockerImage image = new DockerImage("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);
    final DockerClient dockerClient = mock(DockerClient.class);
    when(dockerClient.inspectImageCmd(image.asString())).thenReturn(imageInspectCmd);
    when(dockerClient.pullImageCmd(eq(image.asString()))).thenReturn(pullImageCmd);
    final DockerImpl docker = new DockerImpl(dockerClient);
    docker.setMetrics(new MetricReceiverWrapper(MetricReceiver.nullImplementation));
    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));
}
Also used : InspectImageCmd(com.github.dockerjava.api.command.InspectImageCmd) MetricReceiverWrapper(com.yahoo.vespa.hosted.dockerapi.metrics.MetricReceiverWrapper) ExecStartResultCallback(com.github.dockerjava.core.command.ExecStartResultCallback) ResultCallback(com.github.dockerjava.api.async.ResultCallback) DockerClient(com.github.dockerjava.api.DockerClient) PullImageCmd(com.github.dockerjava.api.command.PullImageCmd) InspectImageResponse(com.github.dockerjava.api.command.InspectImageResponse) NotFoundException(com.github.dockerjava.api.exception.NotFoundException) Test(org.junit.Test)

Example 5 with PullImageCmd

use of com.github.dockerjava.api.command.PullImageCmd in project vespa by vespa-engine.

the class DockerImpl method pullImageAsyncIfNeeded.

@Override
public boolean pullImageAsyncIfNeeded(final DockerImage image) {
    try {
        synchronized (monitor) {
            if (scheduledPulls.contains(image))
                return true;
            if (imageIsDownloaded(image))
                return false;
            scheduledPulls.add(image);
            PullImageCmd pullImageCmd = dockerClient.pullImageCmd(image.asString());
            dockerRegistryCredentialsSupplier.flatMap(credentialsSupplier -> credentialsSupplier.getCredentials(image)).map(credentials -> new AuthConfig().withRegistryAddress(credentials.registry.toString()).withUsername(credentials.username).withPassword(credentials.password)).ifPresent(pullImageCmd::withAuthConfig);
            logger.log(LogLevel.INFO, "Starting download of " + image.asString());
            pullImageCmd.exec(new ImagePullCallback(image));
            return true;
        }
    } catch (RuntimeException e) {
        numberOfDockerDaemonFails.add();
        throw new DockerException("Failed to pull image '" + image.asString() + "'", e);
    }
}
Also used : Arrays(java.util.Arrays) NotModifiedException(com.github.dockerjava.api.exception.NotModifiedException) Dimensions(com.yahoo.vespa.hosted.dockerapi.metrics.Dimensions) ByteArrayOutputStream(java.io.ByteArrayOutputStream) ExecStartResultCallback(com.github.dockerjava.core.command.ExecStartResultCallback) Inject(com.google.inject.Inject) HashMap(java.util.HashMap) ExecCreateCmdResponse(com.github.dockerjava.api.command.ExecCreateCmdResponse) DockerClientException(com.github.dockerjava.api.exception.DockerClientException) DockerClient(com.github.dockerjava.api.DockerClient) InetAddress(java.net.InetAddress) HashSet(java.util.HashSet) MetricReceiverWrapper(com.yahoo.vespa.hosted.dockerapi.metrics.MetricReceiverWrapper) ResultCallbackTemplate(com.github.dockerjava.core.async.ResultCallbackTemplate) DockerClientConfig(com.github.dockerjava.core.DockerClientConfig) DockerClientImpl(com.github.dockerjava.core.DockerClientImpl) NetworkAddressInterface(com.yahoo.vespa.hosted.dockerapi.DockerNetworkCreator.NetworkAddressInterface) Duration(java.time.Duration) Map(java.util.Map) LogLevel(com.yahoo.log.LogLevel) CounterWrapper(com.yahoo.vespa.hosted.dockerapi.metrics.CounterWrapper) InspectImageResponse(com.github.dockerjava.api.command.InspectImageResponse) DefaultDockerClientConfig(com.github.dockerjava.core.DefaultDockerClientConfig) PullImageResultCallback(com.github.dockerjava.core.command.PullImageResultCallback) NotFoundException(com.github.dockerjava.api.exception.NotFoundException) PullImageCmd(com.github.dockerjava.api.command.PullImageCmd) AuthConfig(com.github.dockerjava.api.model.AuthConfig) InspectExecResponse(com.github.dockerjava.api.command.InspectExecResponse) ExecStartCmd(com.github.dockerjava.api.command.ExecStartCmd) Set(java.util.Set) Statistics(com.github.dockerjava.api.model.Statistics) IOException(java.io.IOException) GuardedBy(javax.annotation.concurrent.GuardedBy) Logger(java.util.logging.Logger) InspectContainerResponse(com.github.dockerjava.api.command.InspectContainerResponse) Collectors(java.util.stream.Collectors) Defaults.getDefaults(com.yahoo.vespa.defaults.Defaults.getDefaults) Image(com.github.dockerjava.api.model.Image) Network(com.github.dockerjava.api.model.Network) TimeUnit(java.util.concurrent.TimeUnit) Inet6Address(java.net.Inet6Address) CountDownLatch(java.util.concurrent.CountDownLatch) List(java.util.List) Stream(java.util.stream.Stream) InspectContainerCmd(com.github.dockerjava.api.command.InspectContainerCmd) JerseyDockerCmdExecFactory(com.github.dockerjava.jaxrs.JerseyDockerCmdExecFactory) Optional(java.util.Optional) Collections(java.util.Collections) PullImageCmd(com.github.dockerjava.api.command.PullImageCmd) AuthConfig(com.github.dockerjava.api.model.AuthConfig)

Aggregations

PullImageCmd (com.github.dockerjava.api.command.PullImageCmd)6 DockerClient (com.github.dockerjava.api.DockerClient)4 NotFoundException (com.github.dockerjava.api.exception.NotFoundException)4 ResultCallback (com.github.dockerjava.api.async.ResultCallback)3 ExecStartResultCallback (com.github.dockerjava.core.command.ExecStartResultCallback)3 MetricReceiverWrapper (com.yahoo.vespa.hosted.dockerapi.metrics.MetricReceiverWrapper)3 InspectImageCmd (com.github.dockerjava.api.command.InspectImageCmd)2 InspectImageResponse (com.github.dockerjava.api.command.InspectImageResponse)2 AuthConfig (com.github.dockerjava.api.model.AuthConfig)2 Image (com.github.dockerjava.api.model.Image)2 PullImageResultCallback (com.github.dockerjava.core.command.PullImageResultCallback)2 IOException (java.io.IOException)2 HubIntegrationException (com.blackducksoftware.integration.hub.exception.HubIntegrationException)1 ExecCreateCmdResponse (com.github.dockerjava.api.command.ExecCreateCmdResponse)1 ExecStartCmd (com.github.dockerjava.api.command.ExecStartCmd)1 InspectContainerCmd (com.github.dockerjava.api.command.InspectContainerCmd)1 InspectContainerResponse (com.github.dockerjava.api.command.InspectContainerResponse)1 InspectExecResponse (com.github.dockerjava.api.command.InspectExecResponse)1 DockerClientException (com.github.dockerjava.api.exception.DockerClientException)1 NotModifiedException (com.github.dockerjava.api.exception.NotModifiedException)1