Search in sources :

Example 6 with DockerResponse

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

the class DockerConnector method startExec.

/**
     * Starts a previously set up exec instance.
     *
     * @param execOutputProcessor
     *         processor for exec output
     * @throws IOException
     *          when a problem occurs with docker api calls
     */
public void startExec(final StartExecParams params, @Nullable MessageProcessor<LogMessage> execOutputProcessor) throws IOException {
    final ExecStart execStart = new ExecStart().withDetach(params.isDetach() == Boolean.TRUE).withTty(params.isTty() == Boolean.TRUE);
    byte[] entityBytesArray = toJson(execStart).getBytes(StandardCharsets.UTF_8);
    try (DockerConnection connection = connectionFactory.openConnection(dockerDaemonUri).method("POST").path(apiVersionPathPrefix + "/exec/" + params.getExecId() + "/start").header("Content-Type", MediaType.APPLICATION_JSON).header("Content-Length", entityBytesArray.length).entity(entityBytesArray)) {
        final DockerResponse response = connection.request();
        final int status = response.getStatus();
        // in fact docker API returns 200 or 204 status.
        if (status / 100 != 2) {
            throw getDockerException(response);
        }
        if (status != NO_CONTENT.getStatusCode() && execOutputProcessor != null) {
            try (InputStream responseStream = response.getInputStream()) {
                new LogMessagePumper(responseStream, execOutputProcessor).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) ExecStart(org.eclipse.che.plugin.docker.client.json.ExecStart)

Example 7 with DockerResponse

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

the class DockerConnector method inspectContainer.

/**
     * Gets detailed information about docker container.
     *
     * @return detailed information about {@code container}
     * @throws IOException
     *          when a problem occurs with docker api calls
     */
public ContainerInfo inspectContainer(final InspectContainerParams params) throws IOException {
    try (DockerConnection connection = connectionFactory.openConnection(dockerDaemonUri).method("GET").path(apiVersionPathPrefix + "/containers/" + params.getContainer() + "/json")) {
        addQueryParamIfNotNull(connection, "size", params.isReturnContainerSize());
        final DockerResponse response = connection.request();
        if (OK.getStatusCode() != response.getStatus()) {
            throw getDockerException(response);
        }
        return parseResponseStreamAndClose(response.getInputStream(), ContainerInfo.class);
    }
}
Also used : DockerConnection(org.eclipse.che.plugin.docker.client.connection.DockerConnection) DockerResponse(org.eclipse.che.plugin.docker.client.connection.DockerResponse)

Example 8 with DockerResponse

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

the class DockerConnector method removeNetwork.

/**
     * Removes network matching provided params
     *
     * @throws NetworkNotFoundException
     *         if network is not found
     * @throws IOException
     *         when a problem occurs with docker api calls
     */
public void removeNetwork(RemoveNetworkParams params) throws IOException {
    try (DockerConnection connection = connectionFactory.openConnection(dockerDaemonUri).method("DELETE").path(apiVersionPathPrefix + "/networks/" + params.getNetworkId())) {
        final DockerResponse response = connection.request();
        int status = response.getStatus();
        if (status == 404) {
            throw new NetworkNotFoundException(readAndCloseQuietly(response.getInputStream()));
        }
        if (status / 100 != 2) {
            throw getDockerException(response);
        }
    }
}
Also used : DockerConnection(org.eclipse.che.plugin.docker.client.connection.DockerConnection) DockerResponse(org.eclipse.che.plugin.docker.client.connection.DockerResponse) NetworkNotFoundException(org.eclipse.che.plugin.docker.client.exception.NetworkNotFoundException)

Example 9 with DockerResponse

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

the class DockerConnector method removeImage.

/**
     * Removes docker image.
     *
     * @throws IOException
     *          when a problem occurs with docker api calls
     */
public void removeImage(final RemoveImageParams params) throws IOException {
    try (DockerConnection connection = connectionFactory.openConnection(dockerDaemonUri).method("DELETE").path(apiVersionPathPrefix + "/images/" + params.getImage())) {
        addQueryParamIfNotNull(connection, "force", params.isForce());
        final DockerResponse response = connection.request();
        if (OK.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 10 with DockerResponse

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

the class DockerConnector method getResource.

/**
     * Gets files from the specified container.
     *
     * @return stream of resources from the specified container filesystem, with retention connection
     * @throws IOException
     *          when a problem occurs with docker api calls
     * @apiNote this method implements 1.20 docker API and requires docker not less than 1.8.0 version
     */
public InputStream getResource(final GetResourceParams params) throws IOException {
    DockerConnection connection = null;
    try {
        connection = connectionFactory.openConnection(dockerDaemonUri).method("GET").path(apiVersionPathPrefix + "/containers/" + params.getContainer() + "/archive").query("path", params.getSourcePath());
        final DockerResponse response = connection.request();
        if (response.getStatus() != OK.getStatusCode()) {
            throw getDockerException(response);
        }
        return new CloseConnectionInputStream(response.getInputStream(), connection);
    } catch (IOException io) {
        connection.close();
        throw io;
    }
}
Also used : DockerConnection(org.eclipse.che.plugin.docker.client.connection.DockerConnection) CloseConnectionInputStream(org.eclipse.che.plugin.docker.client.connection.CloseConnectionInputStream) DockerResponse(org.eclipse.che.plugin.docker.client.connection.DockerResponse) IOException(java.io.IOException)

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