Skip to content
Merged
Show file tree
Hide file tree
Changes from all 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
18 changes: 18 additions & 0 deletions src/main/java/com/github/dockerjava/api/DockerClient.java
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
import com.github.dockerjava.api.command.ListImagesCmd;
import com.github.dockerjava.api.command.ListNetworksCmd;
import com.github.dockerjava.api.command.ListServicesCmd;
import com.github.dockerjava.api.command.ListSwarmNodesCmd;
import com.github.dockerjava.api.command.ListTasksCmd;
import com.github.dockerjava.api.command.ListVolumesCmd;
import com.github.dockerjava.api.command.LoadImageCmd;
Expand All @@ -60,6 +61,7 @@
import com.github.dockerjava.api.command.UpdateContainerCmd;
import com.github.dockerjava.api.command.UpdateServiceCmd;
import com.github.dockerjava.api.command.UpdateSwarmCmd;
import com.github.dockerjava.api.command.UpdateSwarmNodeCmd;
import com.github.dockerjava.api.command.VersionCmd;
import com.github.dockerjava.api.command.WaitContainerCmd;
import com.github.dockerjava.api.exception.DockerException;
Expand Down Expand Up @@ -309,6 +311,22 @@ public interface DockerClient extends Closeable {
*/
UpdateSwarmCmd updateSwarmCmd(SwarmSpec swarmSpec);

/**
* Updates the swarm node
*
* @return the command
* @since 1.24
*/
UpdateSwarmNodeCmd updateSwarmNodeCmd();

/**
* List nodes in swarm
*
* @return the command
* @since 1.24
*/
ListSwarmNodesCmd listSwarmNodesCmd();

/**
* Command to list all services in a docker swarm. Only applicable if docker runs in swarm mode.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,11 @@ public interface UpdateSwarmNodeCmd extends SyncDockerCmd<Void> {

UpdateSwarmNodeCmd withSwarmNodeSpec(SwarmNodeSpec swarmNodeSpec);

UpdateSwarmNodeCmd withVersion(@Nonnull Integer versionId);

@CheckForNull
Integer getVersion();

interface Exec extends DockerCmdSyncExec<UpdateSwarmNodeCmd, Void> {
}
}
8 changes: 4 additions & 4 deletions src/main/java/com/github/dockerjava/api/model/SwarmNode.java
Original file line number Diff line number Diff line change
Expand Up @@ -27,14 +27,14 @@ public class SwarmNode implements Serializable {
/**
* @since 1.24
*/
@JsonProperty("Id")
@JsonProperty("ID")
private String id;

/**
* @since 1.24
*/
@JsonProperty("Version")
private String[] version;
private ObjectVersion version;

/**
* @since 1.24
Expand Down Expand Up @@ -92,14 +92,14 @@ public SwarmNode withId(String id) {
* @see #version
*/
@CheckForNull
public String[] getVersion() {
public ObjectVersion getVersion() {
return version;
}

/**
* @see #version
*/
public SwarmNode withVersion(String[] version) {
public SwarmNode withVersion(ObjectVersion version) {
this.version = version;
return this;
}
Expand Down
14 changes: 14 additions & 0 deletions src/main/java/com/github/dockerjava/core/DockerClientImpl.java
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@
import com.github.dockerjava.api.command.ListImagesCmd;
import com.github.dockerjava.api.command.ListNetworksCmd;
import com.github.dockerjava.api.command.ListServicesCmd;
import com.github.dockerjava.api.command.ListSwarmNodesCmd;
import com.github.dockerjava.api.command.ListTasksCmd;
import com.github.dockerjava.api.command.ListVolumesCmd;
import com.github.dockerjava.api.command.LoadImageCmd;
Expand All @@ -62,6 +63,7 @@
import com.github.dockerjava.api.command.UpdateContainerCmd;
import com.github.dockerjava.api.command.UpdateServiceCmd;
import com.github.dockerjava.api.command.UpdateSwarmCmd;
import com.github.dockerjava.api.command.UpdateSwarmNodeCmd;
import com.github.dockerjava.api.command.VersionCmd;
import com.github.dockerjava.api.command.WaitContainerCmd;
import com.github.dockerjava.api.model.AuthConfig;
Expand Down Expand Up @@ -102,6 +104,7 @@
import com.github.dockerjava.core.command.ListImagesCmdImpl;
import com.github.dockerjava.core.command.ListNetworksCmdImpl;
import com.github.dockerjava.core.command.ListServicesCmdImpl;
import com.github.dockerjava.core.command.ListSwarmNodesCmdImpl;
import com.github.dockerjava.core.command.ListTasksCmdImpl;
import com.github.dockerjava.core.command.ListVolumesCmdImpl;
import com.github.dockerjava.core.command.LoadImageCmdImpl;
Expand All @@ -128,6 +131,7 @@
import com.github.dockerjava.core.command.UpdateContainerCmdImpl;
import com.github.dockerjava.core.command.UpdateServiceCmdImpl;
import com.github.dockerjava.core.command.UpdateSwarmCmdImpl;
import com.github.dockerjava.core.command.UpdateSwarmNodeCmdImpl;
import com.github.dockerjava.core.command.VersionCmdImpl;
import com.github.dockerjava.core.command.WaitContainerCmdImpl;

Expand Down Expand Up @@ -528,6 +532,16 @@ public UpdateSwarmCmd updateSwarmCmd(SwarmSpec swarmSpec) {
return new UpdateSwarmCmdImpl(getDockerCmdExecFactory().createUpdateSwarmCmdExec(), swarmSpec);
}

@Override
public UpdateSwarmNodeCmd updateSwarmNodeCmd() {
return new UpdateSwarmNodeCmdImpl(getDockerCmdExecFactory().updateSwarmNodeCmdExec());
}

@Override
public ListSwarmNodesCmd listSwarmNodesCmd() {
return new ListSwarmNodesCmdImpl(getDockerCmdExecFactory().listSwarmNodeCmdExec());
}

@Override public ListServicesCmd listServicesCmd() {
return new ListServicesCmdImpl(getDockerCmdExecFactory().createListServicesCmdExec());
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,6 @@
import javax.annotation.CheckForNull;
import javax.annotation.Nonnull;

import static com.google.common.base.Preconditions.checkNotNull;

/**
* Update swarmNode spec
*
Expand All @@ -26,6 +24,12 @@ public class UpdateSwarmNodeCmdImpl extends AbstrDockerCmd<UpdateSwarmNodeCmd, V

private SwarmNodeSpec swarmNodeSpec;

private Integer version;

public UpdateSwarmNodeCmdImpl(Exec exec) {
super(exec);
}

public UpdateSwarmNodeCmdImpl(Exec exec, String swarmNodeId, SwarmNodeSpec swarmNodeSpec) {
super(exec);
withSwarmNodeId(swarmNodeId);
Expand Down Expand Up @@ -60,12 +64,22 @@ public SwarmNodeSpec getSwarmNodeSpec() {
* @see #swarmNodeSpec
*/
public UpdateSwarmNodeCmd withSwarmNodeSpec(SwarmNodeSpec swarmNodeSpec) {
checkNotNull(swarmNodeSpec.getAvailability(), "Availability in SwarmNodeSpec was not specified");
checkNotNull(swarmNodeSpec.getRole(), "Role in SwarmNodeSpec was not specified");
this.swarmNodeSpec = swarmNodeSpec;
return this;
}

@Override
public UpdateSwarmNodeCmd withVersion(@Nonnull Integer versionId) {
this.version = versionId;
return this;
}

@CheckForNull
@Override
public Integer getVersion() {
return version;
}

/**
* @throws NotFoundException No such swarmNode
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,8 @@ public UpdateSwarmNodeCmdExec(WebTarget baseResource, DockerClientConfig dockerC
@Override
protected Void execute(UpdateSwarmNodeCmd command) {
WebTarget webResource = getBaseResource().path("/nodes/{id}/update")
.resolveTemplate("id", command.getSwarmNodeId());
.resolveTemplate("id", command.getSwarmNodeId())
.queryParam("version", command.getVersion());

LOGGER.trace("POST: {}", webResource);
webResource.request().accept(MediaType.APPLICATION_JSON)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,8 @@ public UpdateSwarmNodeCmdExec(WebTarget baseResource, DockerClientConfig dockerC
@Override
protected Void execute(UpdateSwarmNodeCmd command) {
WebTarget webResource = getBaseResource().path("/nodes/{id}/update")
.resolveTemplate("id", command.getSwarmNodeId());
.resolveTemplate("id", command.getSwarmNodeId())
.queryParam("version", command.getVersion());

LOGGER.trace("POST: {}", webResource);
webResource.request().accept(MediaType.APPLICATION_JSON)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import com.github.dockerjava.api.DockerClient;
import com.github.dockerjava.api.command.CreateContainerResponse;
import com.github.dockerjava.api.exception.ConflictException;
import com.github.dockerjava.api.exception.DockerException;
import com.github.dockerjava.api.exception.NotAcceptableException;
import com.github.dockerjava.api.exception.NotFoundException;
import com.github.dockerjava.api.model.ExposedPort;
Expand Down Expand Up @@ -56,7 +57,7 @@ public void afterMethod() {
protected void removeDockersInDocker() {
for (int i = 1; i <= numberOfDockersInDocker; i++) {
try {
dockerRule.getClient().removeContainerCmd(DOCKER_IN_DOCKER_CONTAINER_PREFIX + i);
dockerRule.getClient().removeContainerCmd(DOCKER_IN_DOCKER_CONTAINER_PREFIX + i).withForce(true).exec();
} catch (NotFoundException e) {
// container does not exist
}
Expand Down Expand Up @@ -129,6 +130,10 @@ private void leaveIfInSwarm() {
dockerRule.getClient().leaveSwarmCmd().withForceEnabled(true).exec();
} catch (NotAcceptableException e) {
// do nothing, node is not part of a swarm
} catch (DockerException ex) {
if (!ex.getMessage().contains("node is not part of a swarm")) {
throw ex;
}
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
package com.github.dockerjava.cmd.swarm;

import com.github.dockerjava.api.DockerClient;
import com.github.dockerjava.api.model.SwarmNode;
import com.github.dockerjava.api.model.SwarmNodeAvailability;
import com.github.dockerjava.api.model.SwarmNodeSpec;
import com.github.dockerjava.api.model.SwarmNodeState;
import com.github.dockerjava.api.model.SwarmSpec;
import org.junit.Test;

import java.util.List;

import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.is;

public class UpdateSwarmNodeIT extends SwarmCmdIT {
@Test
public void testUpdateSwarmNode() {
DockerClient docker1 = startDockerInDocker();
docker1.initializeSwarmCmd(new SwarmSpec()).exec();
List<SwarmNode> nodes = docker1.listSwarmNodesCmd().exec();
assertThat(1, is(nodes.size()));
SwarmNode node = nodes.get(0);
assertThat(SwarmNodeState.READY, is(node.getStatus().getState()));
//update the node availability
SwarmNodeSpec nodeSpec = node.getSpec().withAvailability(SwarmNodeAvailability.PAUSE);
docker1.updateSwarmNodeCmd().withSwarmNodeId(node.getId()).withVersion(node.getVersion().getIndex())
.withSwarmNodeSpec(nodeSpec).exec();
nodes = docker1.listSwarmNodesCmd().exec();
assertThat(1, is(nodes.size()));
assertThat(SwarmNodeAvailability.PAUSE, is(nodes.get(0).getSpec().getAvailability()));
}
}