use of org.eclipse.che.plugin.docker.client.connection.DockerConnection 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);
}
}
}
use of org.eclipse.che.plugin.docker.client.connection.DockerConnection 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();
}
}
}
use of org.eclipse.che.plugin.docker.client.connection.DockerConnection 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;
}
}
}
}
use of org.eclipse.che.plugin.docker.client.connection.DockerConnection 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);
}
}
use of org.eclipse.che.plugin.docker.client.connection.DockerConnection 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);
}
}
}
Aggregations