Search in sources :

Example 1 with DockerClient

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

the class DockerClientFactory method getDockerClient.

/**
     * Produces a {@link DockerClient} to communicate with Docker
     */
@SuppressWarnings("resource")
public static DockerClient getDockerClient(DockerComponent dockerComponent, DockerConfiguration dockerConfiguration, Message message) throws DockerException {
    ObjectHelper.notNull(dockerConfiguration, "dockerConfiguration");
    Integer port = DockerHelper.getProperty(DockerConstants.DOCKER_PORT, dockerConfiguration, message, Integer.class, dockerConfiguration.getPort());
    String host = DockerHelper.getProperty(DockerConstants.DOCKER_HOST, dockerConfiguration, message, String.class, dockerConfiguration.getHost());
    Integer maxTotalConnections = DockerHelper.getProperty(DockerConstants.DOCKER_MAX_TOTAL_CONNECTIONS, dockerConfiguration, message, Integer.class, dockerConfiguration.getMaxTotalConnections());
    Integer maxPerRouteConnections = DockerHelper.getProperty(DockerConstants.DOCKER_MAX_PER_ROUTE_CONNECTIONS, dockerConfiguration, message, Integer.class, dockerConfiguration.getMaxPerRouteConnections());
    String username = DockerHelper.getProperty(DockerConstants.DOCKER_USERNAME, dockerConfiguration, message, String.class, dockerConfiguration.getUsername());
    String password = DockerHelper.getProperty(DockerConstants.DOCKER_PASSWORD, dockerConfiguration, message, String.class, dockerConfiguration.getPassword());
    String email = DockerHelper.getProperty(DockerConstants.DOCKER_EMAIL, dockerConfiguration, message, String.class, dockerConfiguration.getEmail());
    Integer requestTimeout = DockerHelper.getProperty(DockerConstants.DOCKER_API_REQUEST_TIMEOUT, dockerConfiguration, message, Integer.class, dockerConfiguration.getRequestTimeout());
    String serverAddress = DockerHelper.getProperty(DockerConstants.DOCKER_SERVER_ADDRESS, dockerConfiguration, message, String.class, dockerConfiguration.getServerAddress());
    String certPath = DockerHelper.getProperty(DockerConstants.DOCKER_CERT_PATH, dockerConfiguration, message, String.class, dockerConfiguration.getCertPath());
    Boolean secure = DockerHelper.getProperty(DockerConstants.DOCKER_SECURE, dockerConfiguration, message, Boolean.class, dockerConfiguration.isSecure());
    Boolean tlsVerify = DockerHelper.getProperty(DockerConstants.DOCKER_TLSVERIFY, dockerConfiguration, message, Boolean.class, dockerConfiguration.isTlsVerify());
    Boolean socket = DockerHelper.getProperty(DockerConstants.DOCKER_SOCKET_ENABLED, dockerConfiguration, message, Boolean.class, dockerConfiguration.isSocket());
    String cmdExecFactory = DockerHelper.getProperty(DockerConstants.DOCKER_CMD_EXEC_FACTORY, dockerConfiguration, message, String.class, dockerConfiguration.getCmdExecFactory());
    DockerClientProfile clientProfile = new DockerClientProfile();
    clientProfile.setHost(host);
    clientProfile.setPort(port);
    clientProfile.setEmail(email);
    clientProfile.setUsername(username);
    clientProfile.setPassword(password);
    clientProfile.setRequestTimeout(requestTimeout);
    clientProfile.setServerAddress(serverAddress);
    clientProfile.setCertPath(certPath);
    clientProfile.setMaxTotalConnections(maxTotalConnections);
    clientProfile.setMaxPerRouteConnections(maxPerRouteConnections);
    clientProfile.setSecure(secure);
    clientProfile.setTlsVerify(tlsVerify);
    clientProfile.setSocket(socket);
    clientProfile.setCmdExecFactory(cmdExecFactory);
    DockerClient dockerClient = dockerComponent.getClient(clientProfile);
    if (dockerClient != null) {
        return dockerClient;
    }
    SSLConfig sslConfig;
    if (clientProfile.isSecure() != null && clientProfile.isSecure()) {
        ObjectHelper.notNull(clientProfile.getCertPath(), "certPath must be specified in secure mode");
        sslConfig = new LocalDirectorySSLConfig(clientProfile.getCertPath());
    } else {
        // docker-java requires an implementation of SslConfig interface
        // to be available for DockerCmdExecFactoryImpl
        sslConfig = new NoImplSslConfig();
    }
    DefaultDockerClientConfig.Builder configBuilder = DefaultDockerClientConfig.createDefaultConfigBuilder().withDockerHost(clientProfile.toUrl()).withDockerTlsVerify(clientProfile.isTlsVerify()).withRegistryUsername(clientProfile.getUsername()).withRegistryPassword(clientProfile.getPassword()).withRegistryEmail(clientProfile.getEmail()).withRegistryUrl(clientProfile.getServerAddress()).withCustomSslConfig(sslConfig);
    if (clientProfile.getCertPath() != null) {
        configBuilder.withDockerCertPath(clientProfile.getCertPath());
    }
    CamelContext camelContext = dockerComponent.getCamelContext();
    try {
        DockerCmdExecFactory factory = null;
        if (cmdExecFactory.equals(JerseyDockerCmdExecFactory.class.getName())) {
            factory = new JerseyDockerCmdExecFactory();
            ((JerseyDockerCmdExecFactory) factory).withReadTimeout(clientProfile.getRequestTimeout()).withConnectTimeout(clientProfile.getRequestTimeout()).withMaxTotalConnections(clientProfile.getMaxTotalConnections()).withMaxPerRouteConnections(clientProfile.getMaxPerRouteConnections());
        } else if (cmdExecFactory.equals(NettyDockerCmdExecFactory.class.getName())) {
            factory = new NettyDockerCmdExecFactory();
            ((NettyDockerCmdExecFactory) factory).withConnectTimeout(clientProfile.getRequestTimeout());
        } else {
            Class<DockerCmdExecFactory> clazz = camelContext.getClassResolver().resolveMandatoryClass(cmdExecFactory, DockerCmdExecFactory.class);
            factory = ObjectHelper.newInstance(clazz);
        }
        dockerClient = DockerClientBuilder.getInstance(configBuilder).withDockerCmdExecFactory(factory).build();
        dockerComponent.setClient(clientProfile, dockerClient);
        return dockerClient;
    } catch (ClassNotFoundException e) {
        throw new IllegalStateException("Unable to resolve DockerCmdExecFactory class: " + cmdExecFactory, e);
    }
}
Also used : CamelContext(org.apache.camel.CamelContext) LocalDirectorySSLConfig(com.github.dockerjava.core.LocalDirectorySSLConfig) SSLConfig(com.github.dockerjava.core.SSLConfig) DockerClient(com.github.dockerjava.api.DockerClient) LocalDirectorySSLConfig(com.github.dockerjava.core.LocalDirectorySSLConfig) NoImplSslConfig(org.apache.camel.component.docker.ssl.NoImplSslConfig) DockerCmdExecFactory(com.github.dockerjava.api.command.DockerCmdExecFactory) NettyDockerCmdExecFactory(com.github.dockerjava.netty.NettyDockerCmdExecFactory) JerseyDockerCmdExecFactory(com.github.dockerjava.jaxrs.JerseyDockerCmdExecFactory) JerseyDockerCmdExecFactory(com.github.dockerjava.jaxrs.JerseyDockerCmdExecFactory) DefaultDockerClientConfig(com.github.dockerjava.core.DefaultDockerClientConfig) NettyDockerCmdExecFactory(com.github.dockerjava.netty.NettyDockerCmdExecFactory)

Example 2 with DockerClient

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

the class DockerProducer method process.

public void process(Exchange exchange) throws Exception {
    Message message = exchange.getIn();
    DockerClient client = DockerClientFactory.getDockerClient(component, configuration, message);
    DockerOperation operation = configuration.getOperation();
    Object result = null;
    switch(operation) {
        /** General **/
        case AUTH:
            result = executeAuthRequest(client, message).exec();
            break;
        case INFO:
            result = executeInfoRequest(client, message).exec();
            break;
        case PING:
            result = executePingRequest(client, message).exec();
            break;
        case VERSION:
            result = executeVersionRequest(client, message).exec();
            break;
        case CREATE_IMAGE:
            result = executeCreateImageRequest(client, message).exec();
            break;
        case INSPECT_IMAGE:
            result = executeInspectImageRequest(client, message).exec();
            break;
        case LIST_IMAGES:
            result = executeListImagesRequest(client, message).exec();
            break;
        case REMOVE_IMAGE:
            result = executeRemoveImageRequest(client, message).exec();
            break;
        case SEARCH_IMAGES:
            result = executeSearchImageRequest(client, message).exec();
            break;
        case TAG_IMAGE:
            result = executeTagImageRequest(client, message).exec();
            break;
        case COMMIT_CONTAINER:
            result = executeCommitContainerRequest(client, message).exec();
            break;
        case COPY_FILE_CONTAINER:
            result = executeCopyFileContainerRequest(client, message).exec();
            break;
        case CREATE_CONTAINER:
            result = executeCreateContainerRequest(client, message).exec();
            break;
        case DIFF_CONTAINER:
            result = executeDiffContainerRequest(client, message).exec();
            break;
        case INSPECT_CONTAINER:
            result = executeInspectContainerRequest(client, message).exec();
            break;
        case LIST_CONTAINERS:
            result = executeListContainersRequest(client, message).exec();
            break;
        case KILL_CONTAINER:
            result = executeKillContainerRequest(client, message).exec();
            break;
        case PAUSE_CONTAINER:
            result = executePauseContainerRequest(client, message).exec();
            break;
        case REMOVE_CONTAINER:
            result = executeRemoveContainerRequest(client, message).exec();
            break;
        case RESTART_CONTAINER:
            result = executeRestartContainerRequest(client, message).exec();
            break;
        case START_CONTAINER:
            result = executeStartContainerRequest(client, message).exec();
            break;
        case STOP_CONTAINER:
            result = executeStopContainerRequest(client, message).exec();
            break;
        case TOP_CONTAINER:
            result = executeTopContainerRequest(client, message).exec();
            break;
        case UNPAUSE_CONTAINER:
            result = executeUnpauseContainerRequest(client, message).exec();
            break;
        case EXEC_CREATE:
            result = executeExecCreateRequest(client, message).exec();
            break;
        default:
            throw new DockerException("Invalid operation: " + operation);
    }
    // If request included a response, set as body
    if (result != null) {
        exchange.getIn().setBody(result);
    }
}
Also used : DockerException(org.apache.camel.component.docker.exception.DockerException) Message(org.apache.camel.Message) DockerClient(com.github.dockerjava.api.DockerClient) DockerOperation(org.apache.camel.component.docker.DockerOperation)

Example 3 with DockerClient

use of com.github.dockerjava.api.DockerClient in project spring-boot by spring-projects.

the class SysVinitLaunchScriptIT method doTest.

private String doTest(String script) throws Exception {
    DockerClient docker = createClient();
    String imageId = buildImage(docker);
    String container = createContainer(docker, imageId, script);
    try {
        copyFilesToContainer(docker, container, script);
        docker.startContainerCmd(container).exec();
        StringBuilder output = new StringBuilder();
        AttachContainerResultCallback resultCallback = docker.attachContainerCmd(container).withStdOut(true).withStdErr(true).withFollowStream(true).withLogs(true).exec(new AttachContainerResultCallback() {

            @Override
            public void onNext(Frame item) {
                output.append(new String(item.getPayload()));
                super.onNext(item);
            }
        });
        resultCallback.awaitCompletion(60, TimeUnit.SECONDS).close();
        docker.waitContainerCmd(container).exec();
        return output.toString();
    } finally {
        docker.removeContainerCmd(container).exec();
    }
}
Also used : Frame(com.github.dockerjava.api.model.Frame) DockerClient(com.github.dockerjava.api.DockerClient) Matchers.containsString(org.hamcrest.Matchers.containsString) AttachContainerResultCallback(com.github.dockerjava.core.command.AttachContainerResultCallback)

Example 4 with DockerClient

use of com.github.dockerjava.api.DockerClient 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 5 with DockerClient

use of com.github.dockerjava.api.DockerClient in project spring-boot by spring-projects.

the class SysVinitLaunchScriptIT method createClient.

private DockerClient createClient() {
    DockerClientConfig config = DockerClientConfig.createDefaultConfigBuilder().withVersion("1.19").build();
    DockerClient docker = DockerClientBuilder.getInstance(config).withDockerCmdExecFactory(this.commandExecFactory).build();
    return docker;
}
Also used : DockerClient(com.github.dockerjava.api.DockerClient) DockerClientConfig(com.github.dockerjava.core.DockerClientConfig)

Aggregations

DockerClient (com.github.dockerjava.api.DockerClient)5 Frame (com.github.dockerjava.api.model.Frame)2 AttachContainerResultCallback (com.github.dockerjava.core.command.AttachContainerResultCallback)2 Message (org.apache.camel.Message)2 DockerOperation (org.apache.camel.component.docker.DockerOperation)2 DockerException (org.apache.camel.component.docker.exception.DockerException)2 DockerCmdExecFactory (com.github.dockerjava.api.command.DockerCmdExecFactory)1 BuildResponseItem (com.github.dockerjava.api.model.BuildResponseItem)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 DefaultDockerClientConfig (com.github.dockerjava.core.DefaultDockerClientConfig)1 DockerClientConfig (com.github.dockerjava.core.DockerClientConfig)1 LocalDirectorySSLConfig (com.github.dockerjava.core.LocalDirectorySSLConfig)1 SSLConfig (com.github.dockerjava.core.SSLConfig)1 BuildImageResultCallback (com.github.dockerjava.core.command.BuildImageResultCallback)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