Search in sources :

Example 1 with ExecCreateConflictException

use of com.spotify.docker.client.exceptions.ExecCreateConflictException in project docker-client by spotify.

the class DefaultDockerClient method execCreate.

@Override
public ExecCreation execCreate(final String containerId, final String[] cmd, final ExecCreateParam... params) throws DockerException, InterruptedException {
    final ContainerInfo containerInfo = inspectContainer(containerId);
    if (!containerInfo.state().running()) {
        throw new IllegalStateException("Container " + containerId + " is not running.");
    }
    final WebTarget resource = resource().path("containers").path(containerId).path("exec");
    final StringWriter writer = new StringWriter();
    try {
        final JsonGenerator generator = objectMapper().getFactory().createGenerator(writer);
        generator.writeStartObject();
        for (final ExecCreateParam param : params) {
            if (param.value().equals("true") || param.value().equals("false")) {
                generator.writeBooleanField(param.name(), Boolean.valueOf(param.value()));
            } else {
                generator.writeStringField(param.name(), param.value());
            }
        }
        generator.writeArrayFieldStart("Cmd");
        for (final String s : cmd) {
            generator.writeString(s);
        }
        generator.writeEndArray();
        generator.writeEndObject();
        generator.close();
    } catch (IOException e) {
        throw new DockerException(e);
    }
    try {
        return request(POST, ExecCreation.class, resource, resource.request(APPLICATION_JSON_TYPE), Entity.json(writer.toString()));
    } catch (DockerRequestException e) {
        switch(e.status()) {
            case 404:
                throw new ContainerNotFoundException(containerId, e);
            case 409:
                throw new ExecCreateConflictException(containerId, e);
            default:
                throw e;
        }
    }
}
Also used : DockerException(com.spotify.docker.client.exceptions.DockerException) StringWriter(java.io.StringWriter) DockerRequestException(com.spotify.docker.client.exceptions.DockerRequestException) ContainerInfo(com.spotify.docker.client.messages.ContainerInfo) JsonGenerator(com.fasterxml.jackson.core.JsonGenerator) WebTarget(javax.ws.rs.client.WebTarget) InterruptedIOException(java.io.InterruptedIOException) IOException(java.io.IOException) ContainerNotFoundException(com.spotify.docker.client.exceptions.ContainerNotFoundException) ExecCreateConflictException(com.spotify.docker.client.exceptions.ExecCreateConflictException)

Aggregations

JsonGenerator (com.fasterxml.jackson.core.JsonGenerator)1 ContainerNotFoundException (com.spotify.docker.client.exceptions.ContainerNotFoundException)1 DockerException (com.spotify.docker.client.exceptions.DockerException)1 DockerRequestException (com.spotify.docker.client.exceptions.DockerRequestException)1 ExecCreateConflictException (com.spotify.docker.client.exceptions.ExecCreateConflictException)1 ContainerInfo (com.spotify.docker.client.messages.ContainerInfo)1 IOException (java.io.IOException)1 InterruptedIOException (java.io.InterruptedIOException)1 StringWriter (java.io.StringWriter)1 WebTarget (javax.ws.rs.client.WebTarget)1