Search in sources :

Example 1 with DockerResponse

use of org.eclipse.che.plugin.docker.client.connection.DockerResponse in project che by eclipse.

the class DockerConnector method pull.

/**
     * Pull an image from registry.
     * To pull from private registry use registry.address:port/image as image.
     *
     * @param progressMonitor
     *         ProgressMonitor for images creation process
     * @param dockerDaemonUri
     *         docker service URI
     * @throws IOException
     *          when a problem occurs with docker api calls
     */
protected void pull(final PullParams params, final ProgressMonitor progressMonitor, final URI dockerDaemonUri) throws IOException {
    try (DockerConnection connection = connectionFactory.openConnection(dockerDaemonUri).method("POST").path(apiVersionPathPrefix + "/images/create").query("fromImage", params.getFullRepo()).header("X-Registry-Auth", authResolver.getXRegistryAuthHeaderValue(params.getRegistry(), params.getAuthConfigs()))) {
        addQueryParamIfNotNull(connection, "tag", params.getTag());
        final DockerResponse response = connection.request();
        if (OK.getStatusCode() != response.getStatus()) {
            throw getDockerException(response);
        }
        try (InputStream responseStream = response.getInputStream()) {
            JsonMessageReader<ProgressStatus> progressReader = new JsonMessageReader<>(responseStream, ProgressStatus.class);
            // Here do some trick to be able interrupt output streaming process.
            // Current unix socket implementation of DockerConnection doesn't react to interruption.
            // So to be able to close unix socket connection and free resources we use main thread.
            // In case of any exception main thread cancels future and close connection.
            // If Docker connection implementation supports interrupting it will stop streaming on interruption,
            // if not it will be stopped by closure of unix socket
            Future<Object> pullFuture = executor.submit(() -> {
                ProgressStatus progressStatus;
                while ((progressStatus = progressReader.next()) != null) {
                    progressMonitor.updateProgress(progressStatus);
                }
                return null;
            });
            // perform get to be able to get execution exception
            pullFuture.get();
        } catch (ExecutionException e) {
            // unwrap exception thrown by task with .getCause()
            throw new DockerException(e.getCause().getLocalizedMessage(), 500);
        } catch (InterruptedException e) {
            Thread.currentThread().interrupt();
            throw new DockerException("Docker image pulling was interrupted", 500);
        }
    }
}
Also used : DockerConnection(org.eclipse.che.plugin.docker.client.connection.DockerConnection) DockerException(org.eclipse.che.plugin.docker.client.exception.DockerException) ProgressStatus(org.eclipse.che.plugin.docker.client.json.ProgressStatus) DockerResponse(org.eclipse.che.plugin.docker.client.connection.DockerResponse) BufferedInputStream(java.io.BufferedInputStream) CloseConnectionInputStream(org.eclipse.che.plugin.docker.client.connection.CloseConnectionInputStream) FileInputStream(java.io.FileInputStream) InputStream(java.io.InputStream) ExecutionException(java.util.concurrent.ExecutionException)

Example 2 with DockerResponse

use of org.eclipse.che.plugin.docker.client.connection.DockerResponse in project che by eclipse.

the class DockerConnector method getContainerLogs.

/**
     * Get stdout and stderr logs from container.
     *
     * @param containerLogsProcessor
     *         output for container logs
     * @throws ContainerNotFoundException
     *         when container not found by docker (docker api returns 404)
     * @throws IOException
     *         when a problem occurs with docker api calls
     */
public void getContainerLogs(final GetContainerLogsParams params, MessageProcessor<LogMessage> containerLogsProcessor) throws IOException {
    try (DockerConnection connection = connectionFactory.openConnection(dockerDaemonUri).method("GET").path(apiVersionPathPrefix + "/containers/" + params.getContainer() + "/logs").query("stdout", 1).query("stderr", 1)) {
        addQueryParamIfNotNull(connection, "details", params.isDetails());
        addQueryParamIfNotNull(connection, "follow", params.isFollow());
        addQueryParamIfNotNull(connection, "since", params.getSince());
        addQueryParamIfNotNull(connection, "timestamps", params.isTimestamps());
        addQueryParamIfNotNull(connection, "tail", params.getTail());
        final DockerResponse response = connection.request();
        final int status = response.getStatus();
        if (status == 404) {
            throw new ContainerNotFoundException(readAndCloseQuietly(response.getInputStream()));
        }
        if (status != OK.getStatusCode()) {
            throw getDockerException(response);
        }
        try (InputStream responseStream = response.getInputStream()) {
            new LogMessagePumper(responseStream, containerLogsProcessor).start();
        }
    }
}
Also used : DockerConnection(org.eclipse.che.plugin.docker.client.connection.DockerConnection) DockerResponse(org.eclipse.che.plugin.docker.client.connection.DockerResponse) BufferedInputStream(java.io.BufferedInputStream) CloseConnectionInputStream(org.eclipse.che.plugin.docker.client.connection.CloseConnectionInputStream) FileInputStream(java.io.FileInputStream) InputStream(java.io.InputStream) ContainerNotFoundException(org.eclipse.che.plugin.docker.client.exception.ContainerNotFoundException)

Example 3 with DockerResponse

use of org.eclipse.che.plugin.docker.client.connection.DockerResponse in project che by eclipse.

the class DockerConnector method startContainer.

/**
     * Starts docker container.
     *
     * @throws IOException
     *          when a problem occurs with docker api calls
     */
public void startContainer(final StartContainerParams params) throws IOException {
    try (DockerConnection connection = connectionFactory.openConnection(dockerDaemonUri).method("POST").path(apiVersionPathPrefix + "/containers/" + params.getContainer() + "/start")) {
        final DockerResponse response = connection.request();
        final int status = response.getStatus();
        if (NO_CONTENT.getStatusCode() != status && NOT_MODIFIED.getStatusCode() != status) {
            final DockerException dockerException = getDockerException(response);
            if (OK.getStatusCode() == status) {
                // docker API 1.20 returns 200 with warning message about usage of loopback docker backend
                LOG.warn(dockerException.getLocalizedMessage());
            } else {
                throw dockerException;
            }
        }
    }
}
Also used : DockerConnection(org.eclipse.che.plugin.docker.client.connection.DockerConnection) DockerException(org.eclipse.che.plugin.docker.client.exception.DockerException) DockerResponse(org.eclipse.che.plugin.docker.client.connection.DockerResponse)

Example 4 with DockerResponse

use of org.eclipse.che.plugin.docker.client.connection.DockerResponse in project che by eclipse.

the class DockerConnector method createContainer.

/**
     * Creates docker container.
     *
     * @return information about just created container
     * @throws IOException
     *          when a problem occurs with docker api calls
     */
public ContainerCreated createContainer(final CreateContainerParams params) throws IOException {
    byte[] entityBytesArray = toJson(params.getContainerConfig()).getBytes(StandardCharsets.UTF_8);
    try (DockerConnection connection = connectionFactory.openConnection(dockerDaemonUri).method("POST").path(apiVersionPathPrefix + "/containers/create").header("Content-Type", MediaType.APPLICATION_JSON).header("Content-Length", entityBytesArray.length).entity(entityBytesArray)) {
        addQueryParamIfNotNull(connection, "name", params.getContainerName());
        final DockerResponse response = connection.request();
        if (CREATED.getStatusCode() != response.getStatus()) {
            throw getDockerException(response);
        }
        return parseResponseStreamAndClose(response.getInputStream(), ContainerCreated.class);
    }
}
Also used : DockerConnection(org.eclipse.che.plugin.docker.client.connection.DockerConnection) DockerResponse(org.eclipse.che.plugin.docker.client.connection.DockerResponse)

Example 5 with DockerResponse

use of org.eclipse.che.plugin.docker.client.connection.DockerResponse in project che by eclipse.

the class DockerConnector method killContainer.

/**
     * Sends specified signal to running container.
     * If signal not set, then SIGKILL will be used.
     *
     * @throws IOException
     *          when a problem occurs with docker api calls
     */
public void killContainer(final KillContainerParams params) throws IOException {
    try (DockerConnection connection = connectionFactory.openConnection(dockerDaemonUri).method("POST").path(apiVersionPathPrefix + "/containers/" + params.getContainer() + "/kill")) {
        addQueryParamIfNotNull(connection, "signal", params.getSignal());
        final DockerResponse response = connection.request();
        if (NO_CONTENT.getStatusCode() != response.getStatus()) {
            throw getDockerException(response);
        }
    }
}
Also used : DockerConnection(org.eclipse.che.plugin.docker.client.connection.DockerConnection) DockerResponse(org.eclipse.che.plugin.docker.client.connection.DockerResponse)

Aggregations

DockerConnection (org.eclipse.che.plugin.docker.client.connection.DockerConnection)22 DockerResponse (org.eclipse.che.plugin.docker.client.connection.DockerResponse)22 CloseConnectionInputStream (org.eclipse.che.plugin.docker.client.connection.CloseConnectionInputStream)9 BufferedInputStream (java.io.BufferedInputStream)8 FileInputStream (java.io.FileInputStream)8 InputStream (java.io.InputStream)8 DockerException (org.eclipse.che.plugin.docker.client.exception.DockerException)4 ExecutionException (java.util.concurrent.ExecutionException)3 ProgressStatus (org.eclipse.che.plugin.docker.client.json.ProgressStatus)3 ImageNotFoundException (org.eclipse.che.plugin.docker.client.exception.ImageNotFoundException)2 Filters (org.eclipse.che.plugin.docker.client.json.Filters)2 File (java.io.File)1 IOException (java.io.IOException)1 Path (java.nio.file.Path)1 List (java.util.List)1 ContainerNotFoundException (org.eclipse.che.plugin.docker.client.exception.ContainerNotFoundException)1 NetworkNotFoundException (org.eclipse.che.plugin.docker.client.exception.NetworkNotFoundException)1 ContainerCommitted (org.eclipse.che.plugin.docker.client.json.ContainerCommitted)1 ExecConfig (org.eclipse.che.plugin.docker.client.json.ExecConfig)1 ExecStart (org.eclipse.che.plugin.docker.client.json.ExecStart)1