Search in sources :

Example 1 with BuildImageResultCallback

use of com.github.dockerjava.core.command.BuildImageResultCallback in project spring-boot by spring-projects.

the class SysVinitLaunchScriptIT method buildImage.

private String buildImage(DockerClient docker) {
    BuildImageResultCallback resultCallback = new BuildImageResultCallback();
    String dockerfile = "src/test/resources/conf/" + this.os + "/" + this.version + "/Dockerfile";
    String tag = "spring-boot-it/" + this.os.toLowerCase() + ":" + this.version;
    docker.buildImageCmd(new File(dockerfile)).withTag(tag).exec(resultCallback);
    String imageId = resultCallback.awaitImageId();
    return imageId;
}
Also used : BuildImageResultCallback(com.github.dockerjava.core.command.BuildImageResultCallback) Matchers.containsString(org.hamcrest.Matchers.containsString) File(java.io.File)

Example 2 with BuildImageResultCallback

use of com.github.dockerjava.core.command.BuildImageResultCallback in project camel by apache.

the class AsyncDockerProducer method process.

@Override
public boolean process(Exchange exchange, AsyncCallback callback) {
    try {
        Message message = exchange.getIn();
        DockerClient client = DockerClientFactory.getDockerClient(component, configuration, message);
        DockerOperation operation = configuration.getOperation();
        Object result = null;
        switch(operation) {
            /** Images **/
            case BUILD_IMAGE:
                // result contain an image id value
                result = executeBuildImageRequest(client, message).exec(new BuildImageResultCallback() {

                    @Override
                    public void onNext(BuildResponseItem item) {
                        log.trace("build image callback {}", item);
                        super.onNext(item);
                    }
                });
                if (result != null) {
                    String imageId = ((BuildImageResultCallback) result).awaitImageId();
                    ((BuildImageResultCallback) result).close();
                    result = imageId;
                }
                break;
            case PULL_IMAGE:
                result = executePullImageRequest(client, message).exec(new PullImageResultCallback() {

                    @Override
                    public void onNext(PullResponseItem item) {
                        log.trace("pull image callback {}", item);
                        super.onNext(item);
                    }
                });
                if (result != null) {
                    result = ((PullImageResultCallback) result).awaitCompletion();
                    ((PullImageResultCallback) result).close();
                }
                break;
            case PUSH_IMAGE:
                result = executePushImageRequest(client, message).exec(new PushImageResultCallback() {

                    @Override
                    public void onNext(PushResponseItem item) {
                        log.trace("push image callback {}", item);
                        super.onNext(item);
                    }
                });
                if (result != null) {
                    result = ((PushImageResultCallback) result).awaitCompletion();
                    ((PushImageResultCallback) result).close();
                }
                break;
            /** Containers **/
            case ATTACH_CONTAINER:
                result = executeAttachContainerRequest(client, message).exec(new AttachContainerResultCallback() {

                    @Override
                    public void onNext(Frame item) {
                        log.trace("attach container callback {}", item);
                        super.onNext(item);
                    }
                });
                if (result != null) {
                    result = ((AttachContainerResultCallback) result).awaitCompletion();
                    ((AttachContainerResultCallback) result).close();
                }
                break;
            case LOG_CONTAINER:
                result = executeLogContainerRequest(client, message).exec(new LogContainerResultCallback() {

                    @Override
                    public void onNext(Frame item) {
                        log.trace("log container callback {}", item);
                        super.onNext(item);
                    }
                });
                if (result != null) {
                    result = ((LogContainerResultCallback) result).awaitCompletion();
                    ((LogContainerResultCallback) result).close();
                }
                break;
            case WAIT_CONTAINER:
                // result contain a status code value
                result = executeWaitContainerRequest(client, message).exec(new WaitContainerResultCallback() {

                    @Override
                    public void onNext(WaitResponse item) {
                        log.trace("wait contanier callback {}", item);
                        super.onNext(item);
                    }
                });
                if (result != null) {
                    Integer statusCode = ((WaitContainerResultCallback) result).awaitStatusCode();
                    ((WaitContainerResultCallback) result).close();
                    result = statusCode;
                }
                break;
            case EXEC_START:
                result = executeExecStartRequest(client, message).exec(new ExecStartResultCallback() {

                    @Override
                    public void onNext(Frame item) {
                        log.trace("exec start callback {}", item);
                        super.onNext(item);
                    }
                });
                if (result != null) {
                    result = ((ExecStartResultCallback) result).awaitCompletion();
                    ((ExecStartResultCallback) result).close();
                }
                break;
            default:
                throw new DockerException("Invalid operation: " + operation);
        }
        // If request included a response, set as body
        if (result != null) {
            exchange.getIn().setBody(result);
            return true;
        }
    } catch (DockerException | InterruptedException | IOException e) {
        log.error(e.getMessage(), e);
        return false;
    }
    return false;
}
Also used : DockerException(org.apache.camel.component.docker.exception.DockerException) BuildResponseItem(com.github.dockerjava.api.model.BuildResponseItem) Frame(com.github.dockerjava.api.model.Frame) Message(org.apache.camel.Message) DockerClient(com.github.dockerjava.api.DockerClient) ExecStartResultCallback(com.github.dockerjava.core.command.ExecStartResultCallback) PushResponseItem(com.github.dockerjava.api.model.PushResponseItem) PushImageResultCallback(com.github.dockerjava.core.command.PushImageResultCallback) IOException(java.io.IOException) WaitResponse(com.github.dockerjava.api.model.WaitResponse) AttachContainerResultCallback(com.github.dockerjava.core.command.AttachContainerResultCallback) BuildImageResultCallback(com.github.dockerjava.core.command.BuildImageResultCallback) WaitContainerResultCallback(com.github.dockerjava.core.command.WaitContainerResultCallback) PullImageResultCallback(com.github.dockerjava.core.command.PullImageResultCallback) LogContainerResultCallback(com.github.dockerjava.core.command.LogContainerResultCallback) DockerOperation(org.apache.camel.component.docker.DockerOperation) PullResponseItem(com.github.dockerjava.api.model.PullResponseItem)

Example 3 with BuildImageResultCallback

use of com.github.dockerjava.core.command.BuildImageResultCallback in project testcontainers-java by testcontainers.

the class ImageFromDockerfile method resolve.

@Override
protected final String resolve() {
    Logger logger = DockerLoggerFactory.getLogger(dockerImageName);
    Profiler profiler = new Profiler("Rule creation - build image");
    profiler.setLogger(logger);
    DockerClient dockerClient = DockerClientFactory.instance().client();
    try {
        if (deleteOnExit) {
            imagesToDelete.add(dockerImageName);
        }
        BuildImageResultCallback resultCallback = new BuildImageResultCallback() {

            @Override
            public void onNext(BuildResponseItem item) {
                super.onNext(item);
                if (item.isErrorIndicated()) {
                    logger.error(item.getErrorDetail().getMessage());
                } else {
                    logger.debug(StringUtils.chomp(item.getStream(), "\n"));
                }
            }
        };
        // We have to use pipes to avoid high memory consumption since users might want to build really big images
        @Cleanup PipedInputStream in = new PipedInputStream();
        @Cleanup PipedOutputStream out = new PipedOutputStream(in);
        profiler.start("Configure image");
        BuildImageCmd buildImageCmd = dockerClient.buildImageCmd(in);
        configure(buildImageCmd);
        profiler.start("Build image");
        BuildImageResultCallback exec = buildImageCmd.exec(resultCallback);
        // To build an image, we have to send the context to Docker in TAR archive format
        profiler.start("Send context as TAR");
        try (TarArchiveOutputStream tarArchive = new TarArchiveOutputStream(new GZIPOutputStream(out))) {
            tarArchive.setLongFileMode(TarArchiveOutputStream.LONGFILE_POSIX);
            for (Map.Entry<String, Transferable> entry : transferables.entrySet()) {
                Transferable transferable = entry.getValue();
                final String destination = entry.getKey();
                transferable.transferTo(tarArchive, destination);
            }
            tarArchive.finish();
        }
        profiler.start("Wait for an image id");
        exec.awaitImageId();
        return dockerImageName;
    } catch (IOException e) {
        throw new RuntimeException("Can't close DockerClient", e);
    } finally {
        profiler.stop().log();
    }
}
Also used : BuildResponseItem(com.github.dockerjava.api.model.BuildResponseItem) DockerClient(com.github.dockerjava.api.DockerClient) PipedOutputStream(java.io.PipedOutputStream) PipedInputStream(java.io.PipedInputStream) IOException(java.io.IOException) Logger(org.slf4j.Logger) Cleanup(lombok.Cleanup) BuildImageResultCallback(com.github.dockerjava.core.command.BuildImageResultCallback) Profiler(org.slf4j.profiler.Profiler) GZIPOutputStream(java.util.zip.GZIPOutputStream) BuildImageCmd(com.github.dockerjava.api.command.BuildImageCmd) TarArchiveOutputStream(org.apache.commons.compress.archivers.tar.TarArchiveOutputStream) HashMap(java.util.HashMap) Map(java.util.Map)

Aggregations

BuildImageResultCallback (com.github.dockerjava.core.command.BuildImageResultCallback)3 DockerClient (com.github.dockerjava.api.DockerClient)2 BuildResponseItem (com.github.dockerjava.api.model.BuildResponseItem)2 IOException (java.io.IOException)2 BuildImageCmd (com.github.dockerjava.api.command.BuildImageCmd)1 Frame (com.github.dockerjava.api.model.Frame)1 PullResponseItem (com.github.dockerjava.api.model.PullResponseItem)1 PushResponseItem (com.github.dockerjava.api.model.PushResponseItem)1 WaitResponse (com.github.dockerjava.api.model.WaitResponse)1 AttachContainerResultCallback (com.github.dockerjava.core.command.AttachContainerResultCallback)1 ExecStartResultCallback (com.github.dockerjava.core.command.ExecStartResultCallback)1 LogContainerResultCallback (com.github.dockerjava.core.command.LogContainerResultCallback)1 PullImageResultCallback (com.github.dockerjava.core.command.PullImageResultCallback)1 PushImageResultCallback (com.github.dockerjava.core.command.PushImageResultCallback)1 WaitContainerResultCallback (com.github.dockerjava.core.command.WaitContainerResultCallback)1 File (java.io.File)1 PipedInputStream (java.io.PipedInputStream)1 PipedOutputStream (java.io.PipedOutputStream)1 HashMap (java.util.HashMap)1 Map (java.util.Map)1