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);
}
}
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);
}
}
}
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();
}
}
}
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());
}
}
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);
}
}
}
Aggregations