Search in sources :

Example 11 with DockerConnection

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

the class DockerConnector method putResource.

/**
     * Puts files into specified container.
     *
     * @throws IOException
     *          when a problem occurs with docker api calls, or during file system operations
     * @apiNote this method implements 1.20 docker API and requires docker not less than 1.8 version
     */
public void putResource(final PutResourceParams params) throws IOException {
    File tarFile;
    long length;
    try (InputStream sourceData = params.getSourceStream()) {
        // TODO according to http spec http://www.w3.org/Protocols/rfc2616/rfc2616-sec4.html#sec4.4,
        // it is possible to send request without specifying content length if chunked encoding header is set
        // Investigate is it possible to write the stream to request directly
        // we save stream to file, because we have to know its length
        Path tarFilePath = Files.createTempFile("compressed-resources", ".tar");
        tarFile = tarFilePath.toFile();
        length = Files.copy(sourceData, tarFilePath, StandardCopyOption.REPLACE_EXISTING);
    }
    try (InputStream tarStream = new BufferedInputStream(new FileInputStream(tarFile));
        DockerConnection connection = connectionFactory.openConnection(dockerDaemonUri).method("PUT").path(apiVersionPathPrefix + "/containers/" + params.getContainer() + "/archive").query("path", params.getTargetPath()).header("Content-Type", ExtMediaType.APPLICATION_X_TAR).header("Content-Length", length).entity(tarStream)) {
        addQueryParamIfNotNull(connection, "noOverwriteDirNonDir", params.isNoOverwriteDirNonDir());
        final DockerResponse response = connection.request();
        if (response.getStatus() != OK.getStatusCode()) {
            throw getDockerException(response);
        }
    } finally {
        FileCleaner.addFile(tarFile);
    }
}
Also used : Path(java.nio.file.Path) DockerConnection(org.eclipse.che.plugin.docker.client.connection.DockerConnection) BufferedInputStream(java.io.BufferedInputStream) BufferedInputStream(java.io.BufferedInputStream) CloseConnectionInputStream(org.eclipse.che.plugin.docker.client.connection.CloseConnectionInputStream) FileInputStream(java.io.FileInputStream) InputStream(java.io.InputStream) DockerResponse(org.eclipse.che.plugin.docker.client.connection.DockerResponse) File(java.io.File) FileInputStream(java.io.FileInputStream)

Example 12 with DockerConnection

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

the class DockerConnector method stopContainer.

/**
     * Stops container.
     *
     * @throws IOException
     *          when a problem occurs with docker api calls
     */
public void stopContainer(final StopContainerParams params) throws IOException {
    final Long timeout = (params.getTimeout() == null) ? null : (params.getTimeunit() == null) ? params.getTimeout() : params.getTimeunit().toSeconds(params.getTimeout());
    try (DockerConnection connection = connectionFactory.openConnection(dockerDaemonUri).method("POST").path(apiVersionPathPrefix + "/containers/" + params.getContainer() + "/stop")) {
        addQueryParamIfNotNull(connection, "t", timeout);
        final DockerResponse response = connection.request();
        if (response.getStatus() / 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)

Example 13 with DockerConnection

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

the class DockerConnector method attachContainer.

/**
     * Attaches to the container with specified id.
     * <br/>
     * Note, that if @{code stream} parameter is {@code true} then get 'live' stream from container.
     * Typically need to run this method in separate thread, if {@code stream}
     * is {@code true} since this method blocks until container is running.
     * @param containerLogsProcessor
     *         output for container logs
     * @throws IOException
     *          when a problem occurs with docker api calls
     */
public void attachContainer(final AttachContainerParams params, MessageProcessor<LogMessage> containerLogsProcessor) throws IOException {
    final Boolean stream = params.isStream();
    try (DockerConnection connection = connectionFactory.openConnection(dockerDaemonUri).method("POST").path(apiVersionPathPrefix + "/containers/" + params.getContainer() + "/attach").query("stdout", 1).query("stderr", 1)) {
        addQueryParamIfNotNull(connection, "stream", stream);
        addQueryParamIfNotNull(connection, "logs", stream);
        final DockerResponse response = connection.request();
        if (OK.getStatusCode() != response.getStatus()) {
            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)

Example 14 with DockerConnection

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

the class DockerConnector method createExec.

/**
     * Sets up an exec instance in a running container.
     *
     * @return just created exec info
     * @throws IOException
     *          when a problem occurs with docker api calls
     */
public Exec createExec(final CreateExecParams params) throws IOException {
    final ExecConfig execConfig = new ExecConfig().withCmd(params.getCmd()).withAttachStderr(params.isDetach() == Boolean.FALSE).withAttachStdout(params.isDetach() == Boolean.FALSE);
    byte[] entityBytesArray = toJson(execConfig).getBytes(StandardCharsets.UTF_8);
    try (DockerConnection connection = connectionFactory.openConnection(dockerDaemonUri).method("POST").path(apiVersionPathPrefix + "/containers/" + params.getContainer() + "/exec").header("Content-Type", MediaType.APPLICATION_JSON).header("Content-Length", entityBytesArray.length).entity(entityBytesArray)) {
        final DockerResponse response = connection.request();
        if (response.getStatus() / 100 != 2) {
            throw getDockerException(response);
        }
        return new Exec(params.getCmd(), parseResponseStreamAndClose(response.getInputStream(), ExecCreated.class).getId());
    }
}
Also used : ExecConfig(org.eclipse.che.plugin.docker.client.json.ExecConfig) DockerConnection(org.eclipse.che.plugin.docker.client.connection.DockerConnection) DockerResponse(org.eclipse.che.plugin.docker.client.connection.DockerResponse)

Example 15 with DockerConnection

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

the class DockerConnector method tag.

/**
     * Tag the docker image into a repository.
     *
     * @throws ImageNotFoundException
     *         when docker api return 404 status
     * @throws IOException
     *         when a problem occurs with docker api calls
     */
public void tag(final TagParams params) throws ImageNotFoundException, IOException {
    try (DockerConnection connection = connectionFactory.openConnection(dockerDaemonUri).method("POST").path(apiVersionPathPrefix + "/images/" + params.getImage() + "/tag").query("repo", params.getRepository())) {
        addQueryParamIfNotNull(connection, "force", params.isForce());
        addQueryParamIfNotNull(connection, "tag", params.getTag());
        final DockerResponse response = connection.request();
        final int status = response.getStatus();
        if (status == 404) {
            throw new ImageNotFoundException(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) ImageNotFoundException(org.eclipse.che.plugin.docker.client.exception.ImageNotFoundException)

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