Search in sources :

Example 26 with DockerRequestException

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

the class DefaultDockerClient method deleteNode.

@Override
public void deleteNode(final String nodeId, final boolean force) throws DockerException, InterruptedException {
    assertApiVersionIsAbove("1.24");
    final WebTarget resource = resource().path("nodes").path(nodeId).queryParam("force", String.valueOf(force));
    try {
        request(DELETE, resource, resource.request(APPLICATION_JSON_TYPE));
    } catch (DockerRequestException e) {
        switch(e.status()) {
            case 404:
                throw new NodeNotFoundException(nodeId);
            case 503:
                throw new NonSwarmNodeException("Node " + nodeId + " is not a swarm node", e);
            default:
                throw e;
        }
    }
}
Also used : NodeNotFoundException(com.spotify.docker.client.exceptions.NodeNotFoundException) DockerRequestException(com.spotify.docker.client.exceptions.DockerRequestException) WebTarget(javax.ws.rs.client.WebTarget) NonSwarmNodeException(com.spotify.docker.client.exceptions.NonSwarmNodeException)

Example 27 with DockerRequestException

use of com.spotify.docker.client.exceptions.DockerRequestException 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)

Example 28 with DockerRequestException

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

the class DefaultDockerClient method createContainer.

@Override
public ContainerCreation createContainer(final ContainerConfig config, final String name) throws DockerException, InterruptedException {
    WebTarget resource = resource().path("containers").path("create");
    if (name != null) {
        checkArgument(CONTAINER_NAME_PATTERN.matcher(name).matches(), "Invalid container name: \"%s\"", name);
        resource = resource.queryParam("name", name);
    }
    log.debug("Creating container with ContainerConfig: {}", config);
    try {
        return request(POST, ContainerCreation.class, resource, resource.request(APPLICATION_JSON_TYPE), Entity.json(config));
    } catch (DockerRequestException e) {
        switch(e.status()) {
            case 404:
                throw new ImageNotFoundException(config.image(), e);
            case 406:
                throw new DockerException("Impossible to attach. Container not running.", e);
            default:
                throw e;
        }
    }
}
Also used : DockerException(com.spotify.docker.client.exceptions.DockerException) DockerRequestException(com.spotify.docker.client.exceptions.DockerRequestException) WebTarget(javax.ws.rs.client.WebTarget) ImageNotFoundException(com.spotify.docker.client.exceptions.ImageNotFoundException)

Example 29 with DockerRequestException

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

the class DefaultDockerClient method updateSwarm.

@Override
public void updateSwarm(final Long version, final boolean rotateWorkerToken, final boolean rotateManagerToken, final boolean rotateManagerUnlockKey, final SwarmSpec spec) throws DockerException, InterruptedException {
    assertApiVersionIsAbove("1.24");
    try {
        final WebTarget resource = resource().path("swarm").path("update").queryParam("version", version).queryParam("rotateWorkerToken", rotateWorkerToken).queryParam("rotateManagerToken", rotateManagerToken).queryParam("rotateManagerUnlockKey", rotateManagerUnlockKey);
        request(POST, String.class, resource, resource.request(APPLICATION_JSON_TYPE), Entity.json(spec));
    } catch (DockerRequestException e) {
        switch(e.status()) {
            case 400:
                throw new DockerException("bad parameter", e);
            default:
                throw e;
        }
    }
}
Also used : DockerException(com.spotify.docker.client.exceptions.DockerException) DockerRequestException(com.spotify.docker.client.exceptions.DockerRequestException) WebTarget(javax.ws.rs.client.WebTarget)

Example 30 with DockerRequestException

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

the class DefaultDockerClient method execResizeTty.

@Override
public void execResizeTty(final String execId, final Integer height, final Integer width) throws DockerException, InterruptedException {
    checkTtyParams(height, width);
    WebTarget resource = resource().path("exec").path(execId).path("resize");
    if (height != null && height > 0) {
        resource = resource.queryParam("h", height);
    }
    if (width != null && width > 0) {
        resource = resource.queryParam("w", width);
    }
    try {
        request(POST, resource, resource.request(TEXT_PLAIN_TYPE));
    } catch (DockerRequestException e) {
        switch(e.status()) {
            case 404:
                throw new ExecNotFoundException(execId, e);
            default:
                throw e;
        }
    }
}
Also used : DockerRequestException(com.spotify.docker.client.exceptions.DockerRequestException) WebTarget(javax.ws.rs.client.WebTarget) ExecNotFoundException(com.spotify.docker.client.exceptions.ExecNotFoundException)

Aggregations

DockerRequestException (com.spotify.docker.client.exceptions.DockerRequestException)45 WebTarget (javax.ws.rs.client.WebTarget)37 ContainerNotFoundException (com.spotify.docker.client.exceptions.ContainerNotFoundException)18 DockerException (com.spotify.docker.client.exceptions.DockerException)14 ImageNotFoundException (com.spotify.docker.client.exceptions.ImageNotFoundException)12 NonSwarmNodeException (com.spotify.docker.client.exceptions.NonSwarmNodeException)11 NetworkNotFoundException (com.spotify.docker.client.exceptions.NetworkNotFoundException)9 NodeNotFoundException (com.spotify.docker.client.exceptions.NodeNotFoundException)9 ExecNotFoundException (com.spotify.docker.client.exceptions.ExecNotFoundException)8 NotFoundException (com.spotify.docker.client.exceptions.NotFoundException)8 ServiceNotFoundException (com.spotify.docker.client.exceptions.ServiceNotFoundException)8 VolumeNotFoundException (com.spotify.docker.client.exceptions.VolumeNotFoundException)8 ContainerRenameConflictException (com.spotify.docker.client.exceptions.ContainerRenameConflictException)7 TaskNotFoundException (com.spotify.docker.client.exceptions.TaskNotFoundException)6 Test (org.junit.Test)6 BadParamException (com.spotify.docker.client.exceptions.BadParamException)5 ConflictException (com.spotify.docker.client.exceptions.ConflictException)5 IOException (java.io.IOException)5 ExecCreateConflictException (com.spotify.docker.client.exceptions.ExecCreateConflictException)4 ExecStartConflictException (com.spotify.docker.client.exceptions.ExecStartConflictException)4