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
9 changes: 9 additions & 0 deletions src/main/java/com/github/dockerjava/api/DockerClient.java
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@
import com.github.dockerjava.api.command.LogSwarmObjectCmd;
import com.github.dockerjava.api.command.PauseContainerCmd;
import com.github.dockerjava.api.command.PingCmd;
import com.github.dockerjava.api.command.PruneCmd;
import com.github.dockerjava.api.command.PullImageCmd;
import com.github.dockerjava.api.command.PushImageCmd;
import com.github.dockerjava.api.command.RemoveContainerCmd;
Expand All @@ -68,6 +69,7 @@
import com.github.dockerjava.api.exception.DockerException;
import com.github.dockerjava.api.model.AuthConfig;
import com.github.dockerjava.api.model.Identifier;
import com.github.dockerjava.api.model.PruneType;
import com.github.dockerjava.api.model.ServiceSpec;
import com.github.dockerjava.api.model.SwarmSpec;
import com.github.dockerjava.core.RemoteApiVersion;
Expand Down Expand Up @@ -391,6 +393,13 @@ public interface DockerClient extends Closeable {
*/
LogSwarmObjectCmd logTaskCmd(String taskId);

/**
* Command to delete unused containers/images/networks/volumes
*
* @since {@link RemoteApiVersion#VERSION_1_25}
*/
PruneCmd pruneCmd(PruneType pruneType);

@Override
void close() throws IOException;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -207,6 +207,13 @@ public interface DockerCmdExecFactory extends Closeable {
*/
ListTasksCmd.Exec listTasksCmdExec();

/**
* Delete unused content (containers, images, volumes, networks, build relicts)
*
* @since {@link RemoteApiVersion#VERSION_1_25}
*/
PruneCmd.Exec pruneCmdExec();

@Override
void close() throws IOException;

Expand Down
54 changes: 54 additions & 0 deletions src/main/java/com/github/dockerjava/api/command/PruneCmd.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
package com.github.dockerjava.api.command;

import com.github.dockerjava.api.model.PruneResponse;
import com.github.dockerjava.api.model.PruneType;
import com.github.dockerjava.core.RemoteApiVersion;

import javax.annotation.CheckForNull;
import javax.annotation.Nonnull;
import java.util.List;
import java.util.Map;

/**
* Delete unused content (containers, images, volumes, networks, build relicts)
*
* @since {@link RemoteApiVersion#VERSION_1_25}
*/
public interface PruneCmd extends SyncDockerCmd<PruneResponse> {

@Nonnull
PruneType getPruneType();

@Nonnull
String getApiPath();

@CheckForNull
Map<String, List<String>> getFilters();

PruneCmd withPruneType(final PruneType pruneType);
/**
* Prune containers created before this timestamp
* Meaningful only for CONTAINERS and IMAGES prune type
* @param until Can be Unix timestamps, date formatted timestamps,
* or Go duration strings (e.g. 10m, 1h30m) computed relative to the daemon machine’s time.
*/
PruneCmd withUntilFilter(String until);

/**
* When set to true, prune only unused and untagged images. When set to false, all unused images are pruned.
* Meaningful only for IMAGES prune type
*/
PruneCmd withDangling(Boolean dangling);

/**
* Prune containers with the specified labels
*/
PruneCmd withLabelFilter(String... label);

@Override
PruneResponse exec();

interface Exec extends DockerCmdSyncExec<PruneCmd, PruneResponse> {
}

}
65 changes: 65 additions & 0 deletions src/main/java/com/github/dockerjava/api/model/PruneResponse.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
package com.github.dockerjava.api.model;

import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
import com.fasterxml.jackson.annotation.JsonProperty;
import org.apache.commons.lang.builder.EqualsBuilder;
import org.apache.commons.lang.builder.HashCodeBuilder;

import java.io.Serializable;

/**
* Delete unused content (containers, images, volumes, networks, build relicts)
*/
@JsonIgnoreProperties(ignoreUnknown = true)
public class PruneResponse implements Serializable {
private static final long serialVersionUID = 1L;

@JsonProperty("SpaceReclaimed")
private Long spaceReclaimed;

/**
* Default constructor for the deserialization.
*/
public PruneResponse() {
}

/**
* Constructor.
*
* @param spaceReclaimed Space reclaimed after purification
*/
public PruneResponse(Long spaceReclaimed) {
this.spaceReclaimed = spaceReclaimed;
}

/**
* Disk space reclaimed in bytes
*/
public Long getSpaceReclaimed() {
return spaceReclaimed;
}

@Override
public String toString() {
StringBuilder sb = new StringBuilder();
sb.append("PruneResponse {");
sb.append(" spaceReclaimed: ").append(getSpaceReclaimed());
sb.append("}");
return sb.toString();
}

@Override
public boolean equals(Object o) {
if (o instanceof Ulimit) {
Ulimit other = (Ulimit) o;
return new EqualsBuilder().append(spaceReclaimed, other.getName()).isEquals();
} else {
return super.equals(o);
}
}

@Override
public int hashCode() {
return new HashCodeBuilder().append(spaceReclaimed).toHashCode();
}
}
9 changes: 9 additions & 0 deletions src/main/java/com/github/dockerjava/api/model/PruneType.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package com.github.dockerjava.api.model;

public enum PruneType {
BUILD,
CONTAINERS,
IMAGES,
NETWORKS,
VOLUMES
}
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@
import com.github.dockerjava.api.command.LogSwarmObjectCmd;
import com.github.dockerjava.api.command.PauseContainerCmd;
import com.github.dockerjava.api.command.PingCmd;
import com.github.dockerjava.api.command.PruneCmd;
import com.github.dockerjava.api.command.PullImageCmd;
import com.github.dockerjava.api.command.PushImageCmd;
import com.github.dockerjava.api.command.RemoveContainerCmd;
Expand Down Expand Up @@ -110,6 +111,7 @@
import com.github.dockerjava.core.exec.LogContainerCmdExec;
import com.github.dockerjava.core.exec.PauseContainerCmdExec;
import com.github.dockerjava.core.exec.PingCmdExec;
import com.github.dockerjava.core.exec.PruneCmdExec;
import com.github.dockerjava.core.exec.PullImageCmdExec;
import com.github.dockerjava.core.exec.PushImageCmdExec;
import com.github.dockerjava.core.exec.RemoveContainerCmdExec;
Expand Down Expand Up @@ -492,5 +494,10 @@ public LogSwarmObjectCmd.Exec logSwarmObjectExec(String endpoint) {
return new LogSwarmObjectExec(getBaseResource(), getDockerClientConfig(), endpoint);
}

@Override
public PruneCmd.Exec pruneCmdExec() {
return new PruneCmdExec(getBaseResource(), getDockerClientConfig());
}

protected abstract WebTarget getBaseResource();
}
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@
import com.github.dockerjava.api.command.LogSwarmObjectCmd;
import com.github.dockerjava.api.command.PauseContainerCmd;
import com.github.dockerjava.api.command.PingCmd;
import com.github.dockerjava.api.command.PruneCmd;
import com.github.dockerjava.api.command.PullImageCmd;
import com.github.dockerjava.api.command.PushImageCmd;
import com.github.dockerjava.api.command.RemoveContainerCmd;
Expand All @@ -69,6 +70,7 @@
import com.github.dockerjava.api.command.WaitContainerCmd;
import com.github.dockerjava.api.model.AuthConfig;
import com.github.dockerjava.api.model.Identifier;
import com.github.dockerjava.api.model.PruneType;
import com.github.dockerjava.api.model.ServiceSpec;
import com.github.dockerjava.api.model.SwarmSpec;
import com.github.dockerjava.core.command.AttachContainerCmdImpl;
Expand Down Expand Up @@ -113,6 +115,7 @@
import com.github.dockerjava.core.command.LogSwarmObjectImpl;
import com.github.dockerjava.core.command.PauseContainerCmdImpl;
import com.github.dockerjava.core.command.PingCmdImpl;
import com.github.dockerjava.core.command.PruneCmdImpl;
import com.github.dockerjava.core.command.PullImageCmdImpl;
import com.github.dockerjava.core.command.PushImageCmdImpl;
import com.github.dockerjava.core.command.RemoveContainerCmdImpl;
Expand Down Expand Up @@ -579,6 +582,11 @@ public LogSwarmObjectCmd logTaskCmd(String taskId) {
return new LogSwarmObjectImpl(getDockerCmdExecFactory().logSwarmObjectExec("tasks"), taskId);
}

@Override
public PruneCmd pruneCmd(PruneType pruneType) {
return new PruneCmdImpl(getDockerCmdExecFactory().pruneCmdExec(), pruneType);
}

@Override
public ListTasksCmd listTasksCmd() {
return new ListTasksCmdImpl(getDockerCmdExecFactory().listTasksCmdExec());
Expand Down
102 changes: 102 additions & 0 deletions src/main/java/com/github/dockerjava/core/command/PruneCmdImpl.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,102 @@
package com.github.dockerjava.core.command;

import com.github.dockerjava.api.command.PruneCmd;
import com.github.dockerjava.api.model.PruneResponse;
import com.github.dockerjava.api.model.PruneType;
import com.github.dockerjava.core.util.FiltersBuilder;

import javax.annotation.CheckForNull;
import javax.annotation.Nonnull;
import java.util.List;
import java.util.Map;

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

/**
* Delete unused content (containers, images, volumes, networks, build relicts)
*/
public class PruneCmdImpl extends AbstrDockerCmd<PruneCmd, PruneResponse> implements PruneCmd {

private static final String BUILD_API_PATH = "/build/prune";
private static final String CONTAINERS_API_PATH = "/containers/prune";
private static final String IMAGES_API_PATH = "/images/prune";
private static final String VOLUMES_API_PATH = "/volumes/prune";
private static final String NETWORKS_API_PATH = "/networks/prune";

private FiltersBuilder filters = new FiltersBuilder();
private PruneType pruneType;

public PruneCmdImpl(Exec exec, PruneType pruneType) {
super(exec);
this.pruneType = pruneType;
}

@Nonnull
@Override
public PruneType getPruneType() {
return pruneType;
}

@Nonnull
@Override
public String getApiPath() {
String apiPath;
switch (getPruneType()) {
case BUILD:
apiPath = BUILD_API_PATH;
break;
case IMAGES:
apiPath = IMAGES_API_PATH;
break;
case NETWORKS:
apiPath = NETWORKS_API_PATH;
break;
case VOLUMES:
apiPath = VOLUMES_API_PATH;
break;
default:
apiPath = CONTAINERS_API_PATH;
break;
}
return apiPath;
}

@CheckForNull
@Override
public Map<String, List<String>> getFilters() {
return filters.build();
}

@Override
public PruneCmd withPruneType(final PruneType pruneType) {
checkNotNull(pruneType, "pruneType has not been specified");
this.pruneType = pruneType;
return this;
}

@Override
public PruneCmd withDangling(Boolean dangling) {
checkNotNull(dangling, "dangling has not been specified");
filters.withFilter("dangling", dangling ? "1" : "0");
return this;
}

@Override
public PruneCmd withUntilFilter(final String until) {
checkNotNull(until, "until has not been specified");
filters.withUntil(until);
return this;
}

@Override
public PruneCmd withLabelFilter(final String... labels) {
checkNotNull(labels, "labels have not been specified");
filters.withLabels(labels);
return this;
}

@Override
public PruneResponse exec() {
return super.exec();
}
}
40 changes: 40 additions & 0 deletions src/main/java/com/github/dockerjava/core/exec/PruneCmdExec.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
package com.github.dockerjava.core.exec;

import com.fasterxml.jackson.core.type.TypeReference;
import com.github.dockerjava.api.command.PruneCmd;
import com.github.dockerjava.api.model.PruneResponse;
import com.github.dockerjava.core.DockerClientConfig;
import com.github.dockerjava.core.MediaType;
import com.github.dockerjava.core.WebTarget;
import com.github.dockerjava.core.util.FiltersEncoder;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class PruneCmdExec extends AbstrSyncDockerCmdExec<PruneCmd, PruneResponse> implements PruneCmd.Exec {

private static final Logger LOGGER = LoggerFactory.getLogger(PruneCmdExec.class);


public PruneCmdExec(WebTarget baseResource, DockerClientConfig dockerClientConfig) {
super(baseResource, dockerClientConfig);
}

@Override
protected PruneResponse execute(PruneCmd command) {
WebTarget webTarget = getBaseResource().path(command.getApiPath());

if (command.getFilters() != null && !command.getFilters().isEmpty()) {
webTarget = webTarget.queryParam("filters", FiltersEncoder.jsonEncode(command.getFilters()));
}

LOGGER.trace("POST: {}", webTarget);

PruneResponse response = webTarget.request().accept(MediaType.APPLICATION_JSON)
.post(null, new TypeReference<PruneResponse>() { });

LOGGER.trace("Response: {}", response);

return response;
}

}
Loading