Search in sources :

Example 21 with DockerConnection

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

the class DockerConnector method removeContainer.

/**
     * Removes docker container.
     *
     * @throws IOException
     *          when a problem occurs with docker api calls
     */
public void removeContainer(final RemoveContainerParams params) throws IOException {
    try (DockerConnection connection = connectionFactory.openConnection(dockerDaemonUri).method("DELETE").path(apiVersionPathPrefix + "/containers/" + params.getContainer())) {
        addQueryParamIfNotNull(connection, "force", params.isForce());
        addQueryParamIfNotNull(connection, "v", params.isRemoveVolumes());
        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)

Example 22 with DockerConnection

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

the class DockerConnector method commit.

/**
     * Creates a new image from a container’s changes.
     *
     * @return id of a new image
     * @throws IOException
     *          when a problem occurs with docker api calls
     */
public String commit(final CommitParams params) throws IOException {
    // TODO: add option to pause container
    try (DockerConnection connection = connectionFactory.openConnection(dockerDaemonUri).method("POST").path(apiVersionPathPrefix + "/commit").query("container", params.getContainer())) {
        addQueryParamIfNotNull(connection, "repo", params.getRepository());
        addQueryParamIfNotNull(connection, "tag", params.getTag());
        addQueryParamIfNotNull(connection, "comment", (params.getComment() == null) ? null : URLEncoder.encode(params.getComment(), "UTF-8"));
        addQueryParamIfNotNull(connection, "author", (params.getAuthor() == null) ? null : URLEncoder.encode(params.getAuthor(), "UTF-8"));
        final DockerResponse response = connection.request();
        if (CREATED.getStatusCode() != response.getStatus()) {
            throw getDockerException(response);
        }
        return parseResponseStreamAndClose(response.getInputStream(), ContainerCommitted.class).getId();
    }
}
Also used : DockerConnection(org.eclipse.che.plugin.docker.client.connection.DockerConnection) ContainerCommitted(org.eclipse.che.plugin.docker.client.json.ContainerCommitted) DockerResponse(org.eclipse.che.plugin.docker.client.connection.DockerResponse)

Example 23 with DockerConnection

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

the class DockerConnector method getEvents.

/**
     * Get docker events.
     * Parameter {@code untilSecond} does nothing if {@code sinceSecond} is 0.<br>
     * If {@code untilSecond} and {@code sinceSecond} are 0 method gets new events only (streaming mode).<br>
     * If {@code untilSecond} and {@code sinceSecond} are not 0 (but less that current date)
     * methods get events that were generated between specified dates.<br>
     * If {@code untilSecond} is 0 but {@code sinceSecond} is not method gets old events and streams new ones.<br>
     * If {@code sinceSecond} is 0 no old events will be got.<br>
     * With some connection implementations method can fail due to connection timeout in streaming mode.
     *
     * @param messageProcessor
     *         processor of all found events that satisfy specified parameters
     * @throws IOException
     *          when a problem occurs with docker api calls
     */
public void getEvents(final GetEventsParams params, MessageProcessor<Event> messageProcessor) throws IOException {
    final Filters filters = params.getFilters();
    try (DockerConnection connection = connectionFactory.openConnection(dockerDaemonUri).method("GET").path(apiVersionPathPrefix + "/events")) {
        addQueryParamIfNotNull(connection, "since", params.getSinceSecond());
        addQueryParamIfNotNull(connection, "until", params.getUntilSecond());
        if (filters != null) {
            connection.query("filters", urlPathSegmentEscaper().escape(toJson(filters.getFilters())));
        }
        final DockerResponse response = connection.request();
        if (OK.getStatusCode() != response.getStatus()) {
            throw getDockerException(response);
        }
        try (InputStream responseStream = response.getInputStream()) {
            new MessagePumper<>(new JsonMessageReader<>(responseStream, Event.class), messageProcessor).start();
        }
    }
}
Also used : DockerConnection(org.eclipse.che.plugin.docker.client.connection.DockerConnection) Filters(org.eclipse.che.plugin.docker.client.json.Filters) 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)

Aggregations

DockerConnection (org.eclipse.che.plugin.docker.client.connection.DockerConnection)23 DockerResponse (org.eclipse.che.plugin.docker.client.connection.DockerResponse)22 CloseConnectionInputStream (org.eclipse.che.plugin.docker.client.connection.CloseConnectionInputStream)10 BufferedInputStream (java.io.BufferedInputStream)9 FileInputStream (java.io.FileInputStream)9 InputStream (java.io.InputStream)9 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 File (java.io.File)2 ImageNotFoundException (org.eclipse.che.plugin.docker.client.exception.ImageNotFoundException)2 Filters (org.eclipse.che.plugin.docker.client.json.Filters)2 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