Search in sources :

Example 21 with DockerRequestException

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

the class DefaultDockerClient method removeContainer.

@Override
public void removeContainer(final String containerId, final RemoveContainerParam... params) throws DockerException, InterruptedException {
    try {
        WebTarget resource = resource().path("containers").path(containerId);
        for (final RemoveContainerParam param : params) {
            resource = resource.queryParam(param.name(), param.value());
        }
        request(DELETE, resource, resource.request(APPLICATION_JSON_TYPE));
    } catch (DockerRequestException e) {
        switch(e.status()) {
            case 400:
                throw new BadParamException(getQueryParamMap(resource()), e);
            case 404:
                throw new ContainerNotFoundException(containerId, e);
            default:
                throw e;
        }
    }
}
Also used : BadParamException(com.spotify.docker.client.exceptions.BadParamException) DockerRequestException(com.spotify.docker.client.exceptions.DockerRequestException) WebTarget(javax.ws.rs.client.WebTarget) ContainerNotFoundException(com.spotify.docker.client.exceptions.ContainerNotFoundException)

Example 22 with DockerRequestException

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

the class DefaultDockerClient method createService.

@Override
public ServiceCreateResponse createService(final ServiceSpec spec, final RegistryAuth config) throws DockerException, InterruptedException {
    assertApiVersionIsAbove("1.24");
    final WebTarget resource = resource().path("services").path("create");
    try {
        return request(POST, ServiceCreateResponse.class, resource, resource.request(APPLICATION_JSON_TYPE).header("X-Registry-Auth", authHeader(config)), Entity.json(spec));
    } catch (DockerRequestException e) {
        switch(e.status()) {
            case 406:
                throw new DockerException("Server error or node is not part of swarm.", e);
            case 409:
                throw new DockerException("Name conflicts with an existing object.", 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 23 with DockerRequestException

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

the class DefaultDockerClientTest method testRenameContainer.

@Test
public void testRenameContainer() throws Exception {
    sut.pull(BUSYBOX_LATEST);
    final String originalName = randomName();
    final String newName = randomName();
    // Create a container with originalName
    final ContainerConfig config = ContainerConfig.builder().image(BUSYBOX_LATEST).build();
    final ContainerCreation creation = sut.createContainer(config, originalName);
    final String id = creation.id();
    assertThat(sut.inspectContainer(id).name(), equalToIgnoreLeadingSlash(originalName));
    // Rename to newName
    sut.renameContainer(id, newName);
    assertThat(sut.inspectContainer(id).name(), equalToIgnoreLeadingSlash(newName));
    // We should no longer find a container with originalName
    try {
        sut.inspectContainer(originalName);
        fail("There should be no container with name " + originalName);
    } catch (ContainerNotFoundException e) {
        // Note, even though property in ContainerNotFoundException is named containerId,
        // in this case it holds the name, since that is what we passed to inspectContainer.
        assertThat(e.getContainerId(), equalToIgnoreLeadingSlash(originalName));
    }
    // Try to rename to a disallowed name (not matching /?[a-zA-Z0-9_-]+).
    // Should get IllegalArgumentException.
    final String badName = "abc123.!*";
    try {
        sut.renameContainer(id, badName);
        fail("We should not be able to rename a container " + badName);
    } catch (IllegalArgumentException ignored) {
    // Pass
    }
    // Try to rename to null
    try {
        sut.renameContainer(id, null);
        fail("We should not be able to rename a container null");
    } catch (IllegalArgumentException ignored) {
    // Pass
    }
    // Create another container with originalName
    final ContainerConfig config2 = ContainerConfig.builder().image(BUSYBOX_LATEST).build();
    final ContainerCreation creation2 = sut.createContainer(config2, originalName);
    final String id2 = creation2.id();
    assertThat(sut.inspectContainer(id2).name(), equalToIgnoreLeadingSlash(originalName));
    // Try to rename another container to newName. Should get a ContainerRenameConflictException.
    try {
        sut.renameContainer(id2, newName);
        fail("We should not be able to rename container " + id2 + " to " + newName);
    } catch (ContainerRenameConflictException e) {
        assertThat(e.getContainerId(), equalTo(id2));
        assertThat(e.getNewName(), equalToIgnoreLeadingSlash(newName));
    } catch (DockerRequestException ignored) {
        // This is a docker bug. Docker responds with HTTP 500 when it should be HTTP 409.
        // See https://github.com/docker/docker/issues/21016.
        // Fixed in version 1.11.0 API version 1.23. So we should see a lower API version here.
        // Seems to be a regression in API version 1.3[23] https://github.com/moby/moby/issues/35472
        assertThat(dockerApiVersionLessThan("1.23") || dockerApiVersionEquals("1.32") || dockerApiVersionEquals("1.33"), is(true));
    }
    // Rename a non-existent id. Should get ContainerNotFoundException.
    final String badId = "no_container_with_this_id_should_exist_otherwise_things_are_weird";
    try {
        sut.renameContainer(badId, randomName());
        fail("There should be no container with id " + badId);
    } catch (ContainerNotFoundException e) {
        assertThat(e.getContainerId(), equalTo(badId));
    }
}
Also used : ContainerConfig(com.spotify.docker.client.messages.ContainerConfig) ContainerCreation(com.spotify.docker.client.messages.ContainerCreation) ContainerRenameConflictException(com.spotify.docker.client.exceptions.ContainerRenameConflictException) DockerRequestException(com.spotify.docker.client.exceptions.DockerRequestException) Long.toHexString(java.lang.Long.toHexString) Matchers.isEmptyOrNullString(org.hamcrest.Matchers.isEmptyOrNullString) Matchers.containsString(org.hamcrest.Matchers.containsString) ContainerNotFoundException(com.spotify.docker.client.exceptions.ContainerNotFoundException) Test(org.junit.Test)

Example 24 with DockerRequestException

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

the class SyslogRedirectionTest method test.

@Test
public void test() throws Exception {
    final String syslogOutput = "should-be-redirected";
    try (final DockerClient docker = getNewDockerClient()) {
        // Start a container that will be our "syslog" endpoint (just run netcat and print whatever
        // we receive).
        final String port = "4711";
        final String expose = port + "/udp";
        docker.pull(ALPINE);
        final HostConfig hostConfig = HostConfig.builder().publishAllPorts(true).build();
        final ContainerConfig config = ContainerConfig.builder().image(// includes spotify/busybox:latest with netcat with udp support
        ALPINE).cmd(asList("nc", "-p", port, "-l", "-u")).exposedPorts(ImmutableSet.of(expose)).hostConfig(hostConfig).build();
        final ContainerCreation creation = docker.createContainer(config, testTag + "_syslog");
        final String syslogContainerId = creation.id();
        docker.startContainer(syslogContainerId);
        final ContainerInfo containerInfo = docker.inspectContainer(syslogContainerId);
        assertThat(containerInfo.state().running(), equalTo(true));
        final String syslogEndpoint = syslogHost + ":" + containerInfo.networkSettings().ports().get(expose).get(0).hostPort();
        // Run a Helios job that logs to syslog.
        startDefaultMaster();
        startDefaultAgent(testHost(), "--syslog-redirect", syslogEndpoint);
        awaitHostStatus(testHost(), UP, LONG_WAIT_SECONDS, SECONDS);
        final List<String> command = Lists.newArrayList();
        final JobId jobId = createJob(testJobName, testJobVersion, testImage, command, ImmutableMap.of("SYSLOG_REDIRECTOR", "/syslog-redirector"));
        deployJob(jobId, testHost());
        final TaskStatus taskStatus = awaitTaskState(jobId, testHost(), EXITED);
        {
            // Verify the log for the task container
            LogStream logs = null;
            try {
                logs = docker.logs(taskStatus.getContainerId(), stdout(), stderr());
                final String log = logs.readFully();
                // for old docker versions should be nothing in the docker output log, either error text
                // or our message
                assertEquals("", log);
            } catch (DockerRequestException e) {
                // for new docker versions, trying to read logs should throw an error but the syslog
                // option should be set
                final String logType = docker.inspectContainer(taskStatus.getContainerId()).hostConfig().logConfig().logType();
                assertEquals("syslog", logType);
            } finally {
                if (logs != null) {
                    logs.close();
                }
            }
        }
        // Verify the log for the syslog container
        {
            final String log;
            try (LogStream logs = docker.logs(syslogContainerId, stdout(), stderr())) {
                log = logs.readFully();
            }
            // the output message from the command should appear in the syslog container
            assertThat(log, containsString(syslogOutput));
        }
    }
}
Also used : ContainerConfig(com.spotify.docker.client.messages.ContainerConfig) ContainerCreation(com.spotify.docker.client.messages.ContainerCreation) DockerClient(com.spotify.docker.client.DockerClient) DockerRequestException(com.spotify.docker.client.exceptions.DockerRequestException) HostConfig(com.spotify.docker.client.messages.HostConfig) ContainerInfo(com.spotify.docker.client.messages.ContainerInfo) Matchers.containsString(org.hamcrest.Matchers.containsString) LogStream(com.spotify.docker.client.LogStream) TaskStatus(com.spotify.helios.common.descriptors.TaskStatus) JobId(com.spotify.helios.common.descriptors.JobId) Test(org.junit.Test)

Example 25 with DockerRequestException

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

the class DefaultDockerClient method resizeTty.

@Override
public void resizeTty(final String containerId, final Integer height, final Integer width) throws DockerException, InterruptedException {
    checkTtyParams(height, width);
    WebTarget resource = resource().path("containers").path(containerId).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 ContainerNotFoundException(containerId, e);
            default:
                throw e;
        }
    }
}
Also used : DockerRequestException(com.spotify.docker.client.exceptions.DockerRequestException) WebTarget(javax.ws.rs.client.WebTarget) ContainerNotFoundException(com.spotify.docker.client.exceptions.ContainerNotFoundException)

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