8000 Add signal query argument for Restart Container command by dmmax · Pull Request #2186 · docker-java/docker-java · GitHub
[go: up one dir, main page]

Skip to content

Add signal query argument for Restart Container command #2186

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Merged
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
8000 Expand Up @@ -8,9 +8,8 @@
/**
* Restart a running container.
*
* @param timeout
* - Timeout in seconds before killing the container. Defaults to 10 seconds.
*
* @param signal - Signal to send to the container as an integer or string (e.g. SIGINT).
* @param timeout - Timeout in seconds before killing the container. Defaults to 10 seconds.
*/
public interface RestartContainerCmd extends SyncDockerCmd<Void> {

Expand All @@ -20,6 +19,12 @@ public interface RestartContainerCmd extends SyncDockerCmd<Void> {
@CheckForNull
Integer getTimeout();

/**
* @since {@link com.github.dockerjava.core.RemoteApiVersion#VERSION_1_42}
*/
@CheckForNull
String getSignal();

RestartContainerCmd withContainerId(@Nonnull String containerId);

/**
Expand All @@ -32,9 +37,10 @@ default RestartContainerCmd withtTimeout(Integer timeout) {

RestartContainerCmd withTimeout(Integer timeout);

RestartContainerCmd withSignal(String signal);

/**
* @throws NotFoundException
* No such container
* @throws NotFoundException No such container
*/
@Override
Void exec() throws NotFoundException;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,8 @@ public class RemoteApiVersion implements Serializable {
public static final RemoteApiVersion VERSION_1_37 = RemoteApiVersion.create(1, 37);
public static final RemoteApiVersion VERSION_1_38 = RemoteApiVersion.create(1, 38);
public static final RemoteApiVersion VERSION_1_40 = RemoteApiVersion.create(1, 40);
public static final RemoteApiVersion VERSION_1_41 = RemoteApiVersion.create(1, 41);
public static final RemoteApiVersion VERSION_1_42 = RemoteApiVersion.create(1, 42);


/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,19 +7,22 @@
import com.github.dockerjava.api.command.RestartContainerCmd;
import com.github.dockerjava.api.exception.NotFoundException;

import javax.annotation.CheckForNull;

/**
* Restart a running container.
*
* @param timeout
* - Timeout in seconds before killing the container. Defaults to 10 seconds.
*
* @param signal - Signal to send to the container as an integer or string (e.g. SIGINT).
* @param timeout - Timeout in seconds before killing the container. Defaults to 10 seconds.
*/
public class RestartContainerCmdImpl extends AbstrDockerCmd<RestartContainerCmd, Void> implements RestartContainerCmd {

private String containerId;

private Integer timeout = 10;

private String signal;

public RestartContainerCmdImpl(RestartContainerCmd.Exec exec, String containerId) {
super(exec);
withContainerId(containerId);
Expand All @@ -35,6 +38,15 @@ public Integer getTimeout() {
return timeout;
}

/**
* @since {@link com.github.dockerjava.core.RemoteApiVersion#VERSION_1_42}
*/
@CheckForNull
@Override
public String getSignal() {
return signal;
}

@Override
public RestartContainerCmd withContainerId(String containerId) {
Objects.requireNonNull(containerId, "containerId was not specified");
Expand All @@ -50,9 +62,15 @@ public RestartContainerCmd withTimeout(Integer timeout) {
return this;
}

@Override
public RestartContainerCmd withSignal(String signal) {
Objects.requireNonNull(signal, "signal was not specified");
this.signal = signal;
return this;
}

/**
* @throws NotFoundException
* No such container
* @throws NotFoundException No such container
*/
@Override
public Void exec() throws NotFoundException {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,10 @@ protected Void execute(RestartContainerCmd command) {
WebTarget webResource = getBaseResource().path("/containers/{id}/restart").resolveTemplate("id",
command.getContainerId());

if (command.getSignal() != null) {
webResource = webResource.queryParam("signal", command.getSignal());
}

if (command.getTimeout() != null) {
webResource = webResource.queryParam("t", String.valueOf(command.getTimeout()));
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,17 +1,22 @@
package com.github.dockerjava.cmd;

import com.github.dockerjava.api.DockerClient;
import com.github.dockerjava.api.command.CreateContainerResponse;
import com.github.dockerjava.api.command.InspectContainerResponse;
import com.github.dockerjava.api.exception.DockerException;
import com.github.dockerjava.api.exception.NotFoundException;
import com.github.dockerjava.core.DefaultDockerClientConfig;
import com.github.dockerjava.core.RemoteApiVersion;
import org.junit.Test;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import static com.github.dockerjava.core.DockerRule.DEFAULT_IMAGE;
import static com.github.dockerjava.utils.TestUtils.getVersion;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.emptyString;
import static org.hamcrest.Matchers.equalTo;
import static org.hamcrest.Matchers.is;
import static org.hamcrest.Matchers.emptyString;
import static org.hamcrest.Matchers.not;

public class RestartContainerCmdImplIT extends CmdIT {
Expand Down Expand Up @@ -44,6 +49,43 @@ public void restartContainer() throws DockerException {
dockerRule.getClient().killContainerCmd(container.getId()).exec();
}

@Test
public void restartContainerWithSignal() throws Exception {
DefaultDockerClientConfig dockerClientConfig = DefaultDockerClientConfig.createDefaultConfigBuilder()
.withApiVersion(RemoteApiVersion.VERSION_1_42)
.withRegistryUrl("https://index.docker.io/v1/")
.build();
try (DockerClient dockerClient = createDockerClient(dockerClientConfig)) {
if (!getVersion(dockerClient).isGreaterOrEqual(RemoteApiVersion.VERSION_1_42)) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think we can use Assume instead.

LOG.info("API version is less than 1.42. Skipping test.");
return;
}
String expectedUserSignal = "10";
String initialCommandWithTrap = "trap 'echo \"exit trapped\"' %s; sleep 9999;";
final String containerId = dockerClient
.createContainerCmd(DEFAULT_IMAGE)
.withCmd(
"/bin/sh",
"-c",
String.format(initialCommandWithTrap, expectedUserSignal))
.exec()
.getId();
assertThat(containerId, not(is(emptyString())));
dockerClient.startContainerCmd(containerId).exec();

// Restart container without signal
dockerClient.restartContainerCmd(containerId).exec();
String log = dockerRule.containerLog(containerId);
assertThat(log.trim(), emptyString());

dockerClient.restartContainerCmd(containerId).withSignal(expectedUserSignal).exec();
log = dockerRule.containerLog(containerId);
assertThat(log.trim(), is("exit trapped"));

dockerClient.removeContainerCmd(containerId).withForce(true).withRemoveVolumes(true).exec();
}
}

@Test(expected = NotFoundException.class)
public void restartNonExistingContainer() throws DockerException {

Expand Down
0