Search in sources :

Example 1 with ProgressDetail

use of com.github.dockerjava.api.model.ResponseItem.ProgressDetail 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 2 with ProgressDetail

use of com.github.dockerjava.api.model.ResponseItem.ProgressDetail 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)

Aggregations

PullImageResultCallback (com.github.dockerjava.api.command.PullImageResultCallback)2 PullResponseItem (com.github.dockerjava.api.model.PullResponseItem)2 ProgressDetail (com.github.dockerjava.api.model.ResponseItem.ProgressDetail)2 PullImageCmd (com.github.dockerjava.api.command.PullImageCmd)1 AuthConfig (com.github.dockerjava.api.model.AuthConfig)1 TimeoutException (java.util.concurrent.TimeoutException)1