Search in sources :

Example 6 with PullImageResultCallback

use of com.github.dockerjava.api.command.PullImageResultCallback in project wildfly-camel by wildfly-extras.

the class DockerManager method pullImage.

public DockerManager pullImage(String imgName) throws TimeoutException {
    PullImageResultCallback callback = new PullImageResultCallback() {

        @Override
        public void onNext(PullResponseItem item) {
            ProgressDetail detail = item.getProgressDetail();
            if (detail != null) {
                Long current = detail.getCurrent();
                Long total = detail.getTotal();
                if (current != null && total != null) {
                    LOG.info("{}: {}%", imgName, 100 * current / total);
                }
            }
            super.onNext(item);
        }
    };
    try {
        PullImageCmd pullCmd = client.pullImageCmd(imgName);
        AuthConfig authConfig = pullCmd.getAuthConfig();
        if (authConfig != null) {
            // Optional first token is the registry
            String[] toks = imgName.split("/");
            String imgRegistry = toks.length > 2 ? toks[0] : null;
            // Using auth config with no credetials on registry mismatch
            String authRegistry = authConfig.getRegistryAddress();
            if (imgRegistry != null && !authRegistry.contains(imgRegistry)) {
                authConfig = new AuthConfig().withRegistryAddress(authRegistry);
                pullCmd.withAuthConfig(authConfig);
            }
            String username = authConfig.getUsername();
            String password = authConfig.getPassword();
            password = password != null ? "*******" : null;
            LOG.info("Pull {}/{} {} {}", username, password, authRegistry, imgName);
        } else {
            LOG.info("Pull unauthorized {}", imgName);
        }
        if (!pullCmd.exec(callback).awaitCompletion(10, TimeUnit.MINUTES)) {
            throw new TimeoutException("Timeout pulling: " + imgName);
        }
    } catch (InterruptedException ex) {
    // ignore
    }
    return this;
}
Also used : PullImageCmd(com.github.dockerjava.api.command.PullImageCmd) ProgressDetail(com.github.dockerjava.api.model.ResponseItem.ProgressDetail) PullImageResultCallback(com.github.dockerjava.api.command.PullImageResultCallback) AuthConfig(com.github.dockerjava.api.model.AuthConfig) PullResponseItem(com.github.dockerjava.api.model.PullResponseItem) TimeoutException(java.util.concurrent.TimeoutException)

Example 7 with PullImageResultCallback

use of com.github.dockerjava.api.command.PullImageResultCallback in project hortonmachine by TheHortonMachine.

the class DockerHandler method pullImage.

public void pullImage(String imageName, String tag, IHMProgressMonitor pm) throws Exception {
    PullImageResultCallback resultCallback = new PullImageResultCallback();
    if (pm != null) {
        resultCallback = new PullImageResultCallback() {

            @Override
            public void onNext(PullResponseItem item) {
                String id = item.getId();
                if (item != null) {
                    ProgressDetail progressDetail = item.getProgressDetail();
                    if (progressDetail != null) {
                        Long currentObj = progressDetail.getCurrent();
                        Long totalObj = progressDetail.getTotal();
                        if (currentObj != null && totalObj != null) {
                            int current = (int) (currentObj / 1000);
                            int total = (int) (totalObj / 1000);
                            String totalUnit = "KB";
                            String currentUnit = "KB";
                            if (total > 1024) {
                                total = total / 1000;
                                totalUnit = "MB";
                            }
                            if (current > 1024) {
                                current = current / 1000;
                                currentUnit = "MB";
                            }
                            String msg = null;
                            if (current == total) {
                                msg = "Finished downloading " + id;
                            } else {
                                msg = "Downloading " + id + " ( " + current + currentUnit + "/" + total + totalUnit + " )";
                            }
                            pm.message(msg);
                        }
                    }
                }
                super.onNext(item);
            }

            @Override
            public void onError(Throwable throwable) {
                pm.errorMessage("Failed to start pull command:" + throwable.getMessage());
                super.onError(throwable);
            }
        };
    }
    // 
    dockerClient.pullImageCmd(imageName).withTag(// 
    tag).exec(resultCallback);
    resultCallback.awaitCompletion();
}
Also used : ProgressDetail(com.github.dockerjava.api.model.ResponseItem.ProgressDetail) PullImageResultCallback(com.github.dockerjava.api.command.PullImageResultCallback) PullResponseItem(com.github.dockerjava.api.model.PullResponseItem)

Example 8 with PullImageResultCallback

use of com.github.dockerjava.api.command.PullImageResultCallback in project polaris by ractf.

the class DockerRunner method updatePod.

@Override
public void updatePod(final Task task, final Container pod) {
    final var credentials = state.getCredential(pod.getRepoCredentials());
    final var authConfig = authConfigFactory.createAuthConfig(credentials);
    final Map<String, String> filter = new HashMap<>();
    filter.put("polaris-pod", pod.getId());
    // TODO: surely there's a better way to do this
    try {
        log.info("Trying to pull image {}", pod.getImage());
        dockerClient.pullImageCmd(pod.getImage()).withTag(pod.getTag()).withAuthConfig(authConfig).exec(new PullImageResultCallback() {

            @Override
            public void onNext(final PullResponseItem item) {
                System.out.println(item.toString());
                if (item.getStatus() != null && item.getStatus().contains("Downloaded newer image")) {
                    for (final var container : dockerClient.listContainersCmd().withLabelFilter(filter).exec()) {
                        dockerClient.stopContainerCmd(container.getId()).withTimeout(pod.getTerminationTimeout()).exec();
                        createPod(task, pod, state.getInstance(container.getLabels().get("polaris-instance")));
                        startPod(task, pod, state.getInstance(container.getLabels().get("polaris-instance")));
                    }
                    Namespace namespace = null;
                    if (pod.getRepoCredentials() != null) {
                        namespace = state.getNamespace(pod.getRepoCredentials().getNamespace());
                    }
                    notifications.info(NotificationTarget.NAMESPACE_ADMIN, namespace, "Updated pod " + pod.getId(), "");
                }
            }
        }).awaitCompletion();
    } catch (final Exception e) {
        Namespace namespace = null;
        if (pod.getRepoCredentials() != null) {
            namespace = state.getNamespace(pod.getRepoCredentials().getNamespace());
        }
        notifications.error(NotificationTarget.NAMESPACE_ADMIN, namespace, "Failed to update pod " + pod.getId(), e.getMessage());
        e.printStackTrace();
    }
}
Also used : PullImageResultCallback(com.github.dockerjava.api.command.PullImageResultCallback) Namespace(uk.co.ractf.polaris.api.namespace.Namespace)

Example 9 with PullImageResultCallback

use of com.github.dockerjava.api.command.PullImageResultCallback in project developer-be by EdgeGallery.

the class ContainerImageServiceImpl method downloadHarborImage.

/**
 * download harbor image.
 *
 * @param imageId imageId
 * @return
 */
@Override
public ResponseEntity<InputStreamResource> downloadHarborImage(String imageId) {
    if (StringUtils.isEmpty(imageId)) {
        LOGGER.error("imageId is null");
        throw new IllegalRequestException("imageId is null", ResponseConsts.RET_REQUEST_PARAM_EMPTY);
    }
    ContainerImage containerImage = containerImageMapper.getContainerImage(imageId);
    if (containerImage == null) {
        LOGGER.error("imageId is incorrect");
        throw new IllegalRequestException("imageId is incorrect", ResponseConsts.RET_REQUEST_PARAM_ERROR);
    }
    String image = containerImage.getImagePath();
    String fileName = containerImage.getFileName();
    if (StringUtils.isEmpty(image) || StringUtils.isEmpty(fileName)) {
        String msg = "image or fileName of this record in db is empty";
        LOGGER.error(msg);
        throw new IllegalRequestException(msg, ResponseConsts.RET_QUERY_DATA_EMPTY);
    }
    try {
        DockerClient dockerClient = ContainerImageUtil.getDockerClient();
        // pull image
        dockerClient.pullImageCmd(image).exec(new PullImageResultCallback()).awaitCompletion().close();
        String[] images = image.trim().split(":");
        // save image
        SaveImageCmd saveImage = dockerClient.saveImageCmd(images[0]).withTag(images[1]);
        InputStream input = saveImage.exec();
        if (input == null) {
            String msg = "save image failed!";
            LOGGER.error(msg);
            throw new FileOperateException(msg, ResponseConsts.RET_SAVE_FILE_FAIL);
        }
        return ResponseEntity.ok().header(HttpHeaders.CONTENT_TYPE, MediaType.APPLICATION_OCTET_STREAM_VALUE).header(HttpHeaders.CONTENT_DISPOSITION, "attachment;filename=" + fileName).body(new InputStreamResource(input));
    } catch (InterruptedException | IOException e) {
        Thread.currentThread().interrupt();
        String msg = "download Harbor image occur exception!";
        LOGGER.error("download Harbor image failed! {}", e.getMessage());
        throw new FileOperateException(msg, ResponseConsts.RET_DOWNLOAD_FILE_FAIL);
    }
}
Also used : DockerClient(com.github.dockerjava.api.DockerClient) FileInputStream(java.io.FileInputStream) InputStream(java.io.InputStream) SaveImageCmd(com.github.dockerjava.api.command.SaveImageCmd) IOException(java.io.IOException) IllegalRequestException(org.edgegallery.developer.exception.IllegalRequestException) ContainerImage(org.edgegallery.developer.model.resource.container.ContainerImage) PullImageResultCallback(com.github.dockerjava.api.command.PullImageResultCallback) FileOperateException(org.edgegallery.developer.exception.FileOperateException) InputStreamResource(org.springframework.core.io.InputStreamResource)

Example 10 with PullImageResultCallback

use of com.github.dockerjava.api.command.PullImageResultCallback in project airavata-data-lake by apache.

the class GenericDataParsingTask method runContainer.

private void runContainer(DataParser parser, String inputPath, String outputPath, Map<String, String> environmentValues) throws Exception {
    DefaultDockerClientConfig.Builder config = DefaultDockerClientConfig.createDefaultConfigBuilder();
    DockerClient dockerClient = DockerClientBuilder.getInstance(config.build()).build();
    logger.info("Pulling image " + parser.getDockerImage());
    try {
        dockerClient.pullImageCmd(parser.getDockerImage().split(":")[0]).withTag(parser.getDockerImage().split(":")[1]).exec(new PullImageResultCallback()).awaitCompletion();
    } catch (InterruptedException e) {
        logger.error("Interrupted while pulling image", e);
        throw e;
    }
    logger.info("Successfully pulled image " + parser.getDockerImage());
    String containerId = UUID.randomUUID().toString();
    String[] commands = parser.getExecCommand().split(" ");
    CreateContainerResponse containerResponse = dockerClient.createContainerCmd(parser.getDockerImage()).withCmd(commands).withName(containerId).withBinds(Bind.parse(inputPath + ":" + parser.getInputPath()), Bind.parse(outputPath + ":" + parser.getOutputPath())).withTty(true).withAttachStdin(true).withAttachStdout(true).withEnv(environmentValues.entrySet().stream().map(entry -> entry.getKey() + "=" + entry.getValue()).collect(Collectors.toList())).exec();
    logger.info("Created the container with id " + containerResponse.getId());
    final StringBuilder dockerLogs = new StringBuilder();
    if (containerResponse.getWarnings() != null && containerResponse.getWarnings().length > 0) {
        StringBuilder warningStr = new StringBuilder();
        for (String w : containerResponse.getWarnings()) {
            warningStr.append(w).append(",");
        }
        logger.warn("Container " + containerResponse.getId() + " warnings : " + warningStr);
    } else {
        logger.info("Starting container with id " + containerResponse.getId());
        dockerClient.startContainerCmd(containerResponse.getId()).exec();
        LogContainerCmd logContainerCmd = dockerClient.logContainerCmd(containerResponse.getId()).withStdOut(true).withStdErr(true);
        try {
            logContainerCmd.exec(new ResultCallback.Adapter<Frame>() {

                @Override
                public void onNext(Frame item) {
                    logger.info("Got frame: {}", item);
                    ;
                    if (item.getStreamType() == StreamType.STDOUT) {
                        dockerLogs.append(new String(item.getPayload(), StandardCharsets.UTF_8));
                        dockerLogs.append("\n");
                    } else if (item.getStreamType() == StreamType.STDERR) {
                        dockerLogs.append(new String(item.getPayload(), StandardCharsets.UTF_8));
                        dockerLogs.append("\n");
                    }
                    super.onNext(item);
                }

                @Override
                public void onError(Throwable throwable) {
                    logger.error("Errored while running the container {}", containerId, throwable);
                    super.onError(throwable);
                }

                @Override
                public void onComplete() {
                    logger.info("Container {} successfully completed", containerId);
                    super.onComplete();
                }
            }).awaitCompletion();
        } catch (InterruptedException e) {
            logger.error("Interrupted while reading container log" + e.getMessage());
            throw e;
        }
        logger.info("Waiting for the container to stop");
        Integer statusCode = dockerClient.waitContainerCmd(containerResponse.getId()).exec(new WaitContainerResultCallback()).awaitStatusCode();
        logger.info("Container " + containerResponse.getId() + " exited with status code " + statusCode);
        if (statusCode != 0) {
            logger.error("Failing as non zero status code was returned");
            throw new Exception("Failing as non zero status code was returned");
        }
        logger.info("Container logs " + dockerLogs.toString());
    }
    dockerClient.removeContainerCmd(containerResponse.getId()).exec();
    logger.info("Successfully removed container with id " + containerResponse.getId());
}
Also used : TaskResult(org.apache.helix.task.TaskResult) DockerClientBuilder(com.github.dockerjava.core.DockerClientBuilder) CreateContainerResponse(com.github.dockerjava.api.command.CreateContainerResponse) Bind(com.github.dockerjava.api.model.Bind) StringMap(org.apache.airavata.datalake.orchestrator.workflow.engine.task.types.StringMap) ManagedChannel(io.grpc.ManagedChannel) LoggerFactory(org.slf4j.LoggerFactory) BlockingTask(org.apache.airavata.datalake.orchestrator.workflow.engine.task.BlockingTask) HashMap(java.util.HashMap) StreamType(com.github.dockerjava.api.model.StreamType) DockerClient(com.github.dockerjava.api.DockerClient) org.apache.airavata.datalake.data.orchestrator.api.stub.parsing(org.apache.airavata.datalake.data.orchestrator.api.stub.parsing) Map(java.util.Map) WaitContainerResultCallback(com.github.dockerjava.api.command.WaitContainerResultCallback) Frame(com.github.dockerjava.api.model.Frame) PullImageResultCallback(com.github.dockerjava.api.command.PullImageResultCallback) DefaultDockerClientConfig(com.github.dockerjava.core.DefaultDockerClientConfig) Logger(org.slf4j.Logger) LogContainerResultCallback(com.github.dockerjava.core.command.LogContainerResultCallback) BlockingTaskDef(org.apache.airavata.datalake.orchestrator.workflow.engine.task.annotation.BlockingTaskDef) Files(java.nio.file.Files) TaskParam(org.apache.airavata.datalake.orchestrator.workflow.engine.task.annotation.TaskParam) IOException(java.io.IOException) UUID(java.util.UUID) LogContainerCmd(com.github.dockerjava.api.command.LogContainerCmd) Collectors(java.util.stream.Collectors) File(java.io.File) StandardCharsets(java.nio.charset.StandardCharsets) ManagedChannelBuilder(io.grpc.ManagedChannelBuilder) IOUtils(org.apache.commons.io.IOUtils) List(java.util.List) Paths(java.nio.file.Paths) ResultCallback(com.github.dockerjava.api.async.ResultCallback) Frame(com.github.dockerjava.api.model.Frame) DockerClient(com.github.dockerjava.api.DockerClient) LogContainerCmd(com.github.dockerjava.api.command.LogContainerCmd) IOException(java.io.IOException) WaitContainerResultCallback(com.github.dockerjava.api.command.WaitContainerResultCallback) PullImageResultCallback(com.github.dockerjava.api.command.PullImageResultCallback) CreateContainerResponse(com.github.dockerjava.api.command.CreateContainerResponse) DefaultDockerClientConfig(com.github.dockerjava.core.DefaultDockerClientConfig)

Aggregations

PullImageResultCallback (com.github.dockerjava.api.command.PullImageResultCallback)10 DockerClient (com.github.dockerjava.api.DockerClient)5 IOException (java.io.IOException)3 PullResponseItem (com.github.dockerjava.api.model.PullResponseItem)2 ProgressDetail (com.github.dockerjava.api.model.ResponseItem.ProgressDetail)2 ResultCallback (com.github.dockerjava.api.async.ResultCallback)1 CreateContainerResponse (com.github.dockerjava.api.command.CreateContainerResponse)1 LogContainerCmd (com.github.dockerjava.api.command.LogContainerCmd)1 PullImageCmd (com.github.dockerjava.api.command.PullImageCmd)1 SaveImageCmd (com.github.dockerjava.api.command.SaveImageCmd)1 WaitContainerResultCallback (com.github.dockerjava.api.command.WaitContainerResultCallback)1 NotFoundException (com.github.dockerjava.api.exception.NotFoundException)1 AuthConfig (com.github.dockerjava.api.model.AuthConfig)1 Bind (com.github.dockerjava.api.model.Bind)1 Frame (com.github.dockerjava.api.model.Frame)1 Image (com.github.dockerjava.api.model.Image)1 StreamType (com.github.dockerjava.api.model.StreamType)1 DefaultDockerClientConfig (com.github.dockerjava.core.DefaultDockerClientConfig)1 DockerClientBuilder (com.github.dockerjava.core.DockerClientBuilder)1 LogContainerResultCallback (com.github.dockerjava.core.command.LogContainerResultCallback)1