| FazBrowse GitHub Viewer | Trending | | Home |
| Tools: [Download Repo ZIP] [Original HTTPS Page] |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -8,9 +8,8 @@ | |||
| 8 | 8 | /** | |
| 9 | 9 | * Restart a running container. | |
| 10 | 10 | * | |
| 11 | - * @param timeout | ||
| 12 | - * - Timeout in seconds before killing the container. Defaults to 10 seconds. | ||
| 13 | - * | ||
| 11 | + * @param signal - Signal to send to the container as an integer or string (e.g. SIGINT). | ||
| 12 | + * @param timeout - Timeout in seconds before killing the container. Defaults to 10 seconds. | ||
| 14 | 13 | */ | |
| 15 | 14 | public interface RestartContainerCmd extends SyncDockerCmd<Void> { | |
| 16 | 15 | ||
@@ -20,6 +19,12 @@ public interface RestartContainerCmd extends SyncDockerCmd<Void> { | |||
| 20 | 19 | @CheckForNull | |
| 21 | 20 | Integer getTimeout(); | |
| 22 | 21 | ||
| 22 | + /** | ||
| 23 | + * @since {@link com.github.dockerjava.core.RemoteApiVersion#VERSION_1_42} | ||
| 24 | + */ | ||
| 25 | + @CheckForNull | ||
| 26 | + String getSignal(); | ||
| 27 | + | ||
| 23 | 28 | RestartContainerCmd withContainerId(@Nonnull String containerId); | |
| 24 | 29 | ||
| 25 | 30 | /** | |
@@ -32,9 +37,10 @@ default RestartContainerCmd withtTimeout(Integer timeout) { | |||
| 32 | 37 | ||
| 33 | 38 | RestartContainerCmd withTimeout(Integer timeout); | |
| 34 | 39 | ||
| 40 | + RestartContainerCmd withSignal(String signal); | ||
| 41 | + | ||
| 35 | 42 | /** | |
| 36 | - * @throws NotFoundException | ||
| 37 | - * No such container | ||
| 43 | + * @throws NotFoundException No such container | ||
| 38 | 44 | */ | |
| 39 | 45 | @Override | |
| 40 | 46 | Void exec() throws NotFoundException; | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -91,6 +91,8 @@ public class RemoteApiVersion implements Serializable { | |||
| 91 | 91 | public static final RemoteApiVersion VERSION_1_37 = RemoteApiVersion.create(1, 37); | |
| 92 | 92 | public static final RemoteApiVersion VERSION_1_38 = RemoteApiVersion.create(1, 38); | |
| 93 | 93 | public static final RemoteApiVersion VERSION_1_40 = RemoteApiVersion.create(1, 40); | |
| 94 | + public static final RemoteApiVersion VERSION_1_41 = RemoteApiVersion.create(1, 41); | ||
| 95 | + public static final RemoteApiVersion VERSION_1_42 = RemoteApiVersion.create(1, 42); | ||
| 94 | 96 | ||
| 95 | 97 | ||
| 96 | 98 | /** | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -7,19 +7,22 @@ | |||
| 7 | 7 | import com.github.dockerjava.api.command.RestartContainerCmd; | |
| 8 | 8 | import com.github.dockerjava.api.exception.NotFoundException; | |
| 9 | 9 | ||
| 10 | + import javax.annotation.CheckForNull; | ||
| 11 | + | ||
| 10 | 12 | /** | |
| 11 | 13 | * Restart a running container. | |
| 12 | 14 | * | |
| 13 | - * @param timeout | ||
| 14 | - * - Timeout in seconds before killing the container. Defaults to 10 seconds. | ||
| 15 | - * | ||
| 15 | + * @param signal - Signal to send to the container as an integer or string (e.g. SIGINT). | ||
| 16 | + * @param timeout - Timeout in seconds before killing the container. Defaults to 10 seconds. | ||
| 16 | 17 | */ | |
| 17 | 18 | public class RestartContainerCmdImpl extends AbstrDockerCmd<RestartContainerCmd, Void> implements RestartContainerCmd { | |
| 18 | 19 | ||
| 19 | 20 | private String containerId; | |
| 20 | 21 | ||
| 21 | 22 | private Integer timeout = 10; | |
| 22 | 23 | ||
| 24 | + private String signal; | ||
| 25 | + | ||
| 23 | 26 | public RestartContainerCmdImpl(RestartContainerCmd.Exec exec, String containerId) { | |
| 24 | 27 | super(exec); | |
| 25 | 28 | withContainerId(containerId); | |
@@ -35,6 +38,15 @@ public Integer getTimeout() { | |||
| 35 | 38 | return timeout; | |
| 36 | 39 | } | |
| 37 | 40 | ||
| 41 | + /** | ||
| 42 | + * @since {@link com.github.dockerjava.core.RemoteApiVersion#VERSION_1_42} | ||
| 43 | + */ | ||
| 44 | + @CheckForNull | ||
| 45 | + @Override | ||
| 46 | + public String getSignal() { | ||
| 47 | + return signal; | ||
| 48 | + } | ||
| 49 | + | ||
| 38 | 50 | @Override | |
| 39 | 51 | public RestartContainerCmd withContainerId(String containerId) { | |
| 40 | 52 | Objects.requireNonNull(containerId, "containerId was not specified"); | |
@@ -50,9 +62,15 @@ public RestartContainerCmd withTimeout(Integer timeout) { | |||
| 50 | 62 | return this; | |
| 51 | 63 | } | |
| 52 | 64 | ||
| 65 | + @Override | ||
| 66 | + public RestartContainerCmd withSignal(String signal) { | ||
| 67 | + Objects.requireNonNull(signal, "signal was not specified"); | ||
| 68 | + this.signal = signal; | ||
| 69 | + return this; | ||
| 70 | + } | ||
| 71 | + | ||
| 53 | 72 | /** | |
| 54 | - * @throws NotFoundException | ||
| 55 | - * No such container | ||
| 73 | + * @throws NotFoundException No such container | ||
| 56 | 74 | */ | |
| 57 | 75 | @Override | |
| 58 | 76 | public Void exec() throws NotFoundException { | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -24,6 +24,10 @@ protected Void execute(RestartContainerCmd command) { | |||
| 24 | 24 | WebTarget webResource = getBaseResource().path("/containers/{id}/restart").resolveTemplate("id", | |
| 25 | 25 | command.getContainerId()); | |
| 26 | 26 | ||
| 27 | + if (command.getSignal() != null) { | ||
| 28 | + webResource = webResource.queryParam("signal", command.getSignal()); | ||
| 29 | + } | ||
| 30 | + | ||
| 27 | 31 | if (command.getTimeout() != null) { | |
| 28 | 32 | webResource = webResource.queryParam("t", String.valueOf(command.getTimeout())); | |
| 29 | 33 | } | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -1,18 +1,24 @@ | |||
| 1 | 1 | package com.github.dockerjava.cmd; | |
| 2 | 2 | ||
| 3 | + import com.github.dockerjava.api.DockerClient; | ||
| 3 | 4 | import com.github.dockerjava.api.command.CreateContainerResponse; | |
| 4 | 5 | import com.github.dockerjava.api.command.InspectContainerResponse; | |
| 5 | 6 | import com.github.dockerjava.api.exception.DockerException; | |
| 6 | 7 | import com.github.dockerjava.api.exception.NotFoundException; | |
| 8 | + import com.github.dockerjava.core.DefaultDockerClientConfig; | ||
| 9 | + import com.github.dockerjava.core.RemoteApiVersion; | ||
| 7 | 10 | import org.junit.Test; | |
| 8 | 11 | import org.slf4j.Logger; | |
| 9 | 12 | import org.slf4j.LoggerFactory; | |
| 10 | 13 | ||
| 14 | + import static com.github.dockerjava.core.DockerRule.DEFAULT_IMAGE; | ||
| 15 | + import static com.github.dockerjava.junit.DockerMatchers.isGreaterOrEqual; | ||
| 11 | 16 | import static org.hamcrest.MatcherAssert.assertThat; | |
| 17 | + import static org.hamcrest.Matchers.emptyString; | ||
| 12 | 18 | import static org.hamcrest.Matchers.equalTo; | |
| 13 | 19 | import static org.hamcrest.Matchers.is; | |
| 14 | - import static org.hamcrest.Matchers.emptyString; | ||
| 15 | 20 | import static org.hamcrest.Matchers.not; | |
| 21 | + import static org.junit.Assume.assumeThat; | ||
| 16 | 22 | ||
| 17 | 23 | public class RestartContainerCmdImplIT extends CmdIT { | |
| 18 | 24 | public static final Logger LOG = LoggerFactory.getLogger(RestartContainerCmdImplIT.class); | |
@@ -44,6 +50,41 @@ public void restartContainer() throws DockerException { | |||
| 44 | 50 | dockerRule.getClient().killContainerCmd(container.getId()).exec(); | |
| 45 | 51 | } | |
| 46 | 52 | ||
| 53 | + @Test | ||
| 54 | + public void restartContainerWithSignal() throws Exception { | ||
| 55 | + assumeThat("API version should be >= 1.42", dockerRule, isGreaterOrEqual(RemoteApiVersion.VERSION_1_42)); | ||
| 56 | + | ||
| 57 | + DefaultDockerClientConfig dockerClientConfig = DefaultDockerClientConfig.createDefaultConfigBuilder() | ||
| 58 | + .withApiVersion(RemoteApiVersion.VERSION_1_42) | ||
| 59 | + .withRegistryUrl("https://index.docker.io/v1/") | ||
| 60 | + .build(); | ||
| 61 | + try (DockerClient dockerClient = createDockerClient(dockerClientConfig)) { | ||
| 62 | + String expectedUserSignal = "10"; | ||
| 63 | + String initialCommandWithTrap = "trap 'echo \"exit trapped\"' %s; sleep 9999;"; | ||
| 64 | + final String containerId = dockerClient | ||
| 65 | + .createContainerCmd(DEFAULT_IMAGE) | ||
| 66 | + .withCmd( | ||
| 67 | + "/bin/sh", | ||
| 68 | + "-c", | ||
| 69 | + String.format(initialCommandWithTrap, expectedUserSignal)) | ||
| 70 | + .exec() | ||
| 71 | + .getId(); | ||
| 72 | + assertThat(containerId, not(is(emptyString()))); | ||
| 73 | + dockerClient.startContainerCmd(containerId).exec(); | ||
| 74 | + | ||
| 75 | + // Restart container without signal | ||
| 76 | + dockerClient.restartContainerCmd(containerId).exec(); | ||
| 77 | + String log = dockerRule.containerLog(containerId); | ||
| 78 | + assertThat(log.trim(), emptyString()); | ||
| 79 | + | ||
| 80 | + dockerClient.restartContainerCmd(containerId).withSignal(expectedUserSignal).exec(); | ||
| 81 | + log = dockerRule.containerLog(containerId); | ||
| 82 | + assertThat(log.trim(), is("exit trapped")); | ||
| 83 | + | ||
| 84 | + dockerClient.removeContainerCmd(containerId).withForce(true).withRemoveVolumes(true).exec(); | ||
| 85 | + } | ||
| 86 | + } | ||
| 87 | + | ||
| 47 | 88 | @Test(expected = NotFoundException.class) | |
| 48 | 89 | public void restartNonExistingContainer() throws DockerException { | |
| 49 | 90 | ||
| Back | FazBrowse Home | New Git URL |
0 commit comments