Skip to content
Closed
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
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@
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.LoadImageAsyncCmd;
import com.github.dockerjava.api.command.LoadImageCmd;
import com.github.dockerjava.api.command.LogContainerCmd;
import com.github.dockerjava.api.command.LogSwarmObjectCmd;
Expand Down Expand Up @@ -130,6 +131,13 @@ public interface DockerClient extends Closeable {
*/
LoadImageCmd loadImageCmd(@Nonnull InputStream imageStream);

/**
* Loads a tarball with a set of images and tags into a Docker repository and get response
* @param imageStream stream of the tarball file
* @return created command
*/
LoadImageAsyncCmd loadImageAsyncCmd(@Nonnull InputStream imageStream);

SearchImagesCmd searchImagesCmd(@Nonnull String term);

RemoveImageCmd removeImageCmd(@Nonnull String imageId);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,11 @@ public LoadImageCmd.Exec createLoadImageCmdExec() {
return getDockerCmdExecFactory().createLoadImageCmdExec();
}

@Override
public LoadImageAsyncCmd.Exec createLoadImageAsyncCmdExec() {
return getDockerCmdExecFactory().createLoadImageAsyncCmdExec();
}

@Override
public SearchImagesCmd.Exec createSearchImagesCmdExec() {
return getDockerCmdExecFactory().createSearchImagesCmdExec();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@ public interface DockerCmdExecFactory extends Closeable {

LoadImageCmd.Exec createLoadImageCmdExec();

LoadImageAsyncCmd.Exec createLoadImageAsyncCmdExec();

SearchImagesCmd.Exec createSearchImagesCmdExec();

RemoveImageCmd.Exec createRemoveImageCmdExec();
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
package com.github.dockerjava.api.command;

import com.github.dockerjava.api.model.LoadImageAsyncResponseItem;

import javax.annotation.CheckForNull;
import javax.annotation.Nonnull;
import java.io.InputStream;

/**
* load image async and return image id or tag
*/
public interface LoadImageAsyncCmd extends AsyncDockerCmd<LoadImageAsyncCmd, LoadImageAsyncResponseItem> {

@CheckForNull
InputStream getImageStream();

/**
* @param imageStream
* the InputStream of the tar file
*/
LoadImageAsyncCmd withImageStream(@Nonnull InputStream imageStream);

interface Exec extends DockerCmdAsyncExec<LoadImageAsyncCmd, LoadImageAsyncResponseItem> {
}

@Override
default LoadImageAsyncResultCallback start() {
return exec(new LoadImageAsyncResultCallback());
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
package com.github.dockerjava.api.command;

import com.github.dockerjava.api.async.ResultCallbackTemplate;
import com.github.dockerjava.api.exception.DockerClientException;
import com.github.dockerjava.api.model.LoadImageAsyncResponseItem;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;


public class LoadImageAsyncResultCallback extends ResultCallbackTemplate<LoadImageAsyncResultCallback, LoadImageAsyncResponseItem> {

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

private String imageId;

private String error;


@Override
public void onNext(LoadImageAsyncResponseItem item) {
if (item.isLoadSuccessIndicated()) {
this.imageId = item.getImageId();
} else if (item.isErrorIndicated()) {
this.error = item.getError();
}
LOGGER.debug(item.toString());
}

/**
* Awaits the image id from the response stream.
*
* @throws DockerClientException
* if the load fails.
*/
public String awaitImageId() {
try {
awaitCompletion();
} catch (InterruptedException e) {
throw new DockerClientException("", e);
}

return getImageId();
}

private String getImageId() {
if (imageId != null) {
return imageId;
}

if (error == null) {
throw new DockerClientException("Could not load image");
}

throw new DockerClientException("Could not load image: " + error);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
package com.github.dockerjava.api.model;

import com.fasterxml.jackson.annotation.JsonIgnore;


public class LoadImageAsyncResponseItem extends ResponseItem {

private static final long serialVersionUID = 6531167952240938125L;

private static final String LOADED_SUCCESS = "Loaded image";

private static final String LOADED_ID_SUCCESS = "Loaded image ID: sha256:";

private static final String LOADED_TAG_SUCCESS = "Loaded image:";

/**
* Returns whether the stream field indicates a successful load operation
*/
@JsonIgnore
public boolean isLoadSuccessIndicated() {
if (isErrorIndicated() || getStream() == null) {
return false;
}
return getStream().contains(LOADED_SUCCESS);
}

@JsonIgnore
public String getImageId() {
if (!isLoadSuccessIndicated()) {
return null;
}
// loaded image successful then get image id
if (getStream().startsWith(LOADED_ID_SUCCESS)) {
return getStream().replaceFirst(LOADED_ID_SUCCESS, "").trim();
}
// else get image tag
return getStream().replaceFirst(LOADED_TAG_SUCCESS, "").trim();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@
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.LoadImageAsyncCmd;
import com.github.dockerjava.api.command.LoadImageCmd;
import com.github.dockerjava.api.command.LogContainerCmd;
import com.github.dockerjava.api.command.LogSwarmObjectCmd;
Expand Down Expand Up @@ -99,13 +100,9 @@
import com.github.dockerjava.core.exec.EventsCmdExec;
import com.github.dockerjava.core.exec.ExecCreateCmdExec;
import com.github.dockerjava.core.exec.ExecStartCmdExec;
import com.github.dockerjava.core.exec.InspectConfigCmdExec;
import com.github.dockerjava.core.exec.ListConfigsCmdExec;
import com.github.dockerjava.core.exec.RemoveConfigCmdExec;
import com.github.dockerjava.core.exec.ResizeContainerCmdExec;
import com.github.dockerjava.core.exec.ResizeExecCmdExec;
import com.github.dockerjava.core.exec.InfoCmdExec;
import com.github.dockerjava.core.exec.InitializeSwarmCmdExec;
import com.github.dockerjava.core.exec.InspectConfigCmdExec;
import com.github.dockerjava.core.exec.InspectContainerCmdExec;
import com.github.dockerjava.core.exec.InspectExecCmdExec;
import com.github.dockerjava.core.exec.InspectImageCmdExec;
Expand All @@ -117,6 +114,7 @@
import com.github.dockerjava.core.exec.JoinSwarmCmdExec;
import com.github.dockerjava.core.exec.KillContainerCmdExec;
import com.github.dockerjava.core.exec.LeaveSwarmCmdExec;
import com.github.dockerjava.core.exec.ListConfigsCmdExec;
import com.github.dockerjava.core.exec.ListContainersCmdExec;
import com.github.dockerjava.core.exec.ListImagesCmdExec;
import com.github.dockerjava.core.exec.ListNetworksCmdExec;
Expand All @@ -125,6 +123,7 @@
import com.github.dockerjava.core.exec.ListSwarmNodesCmdExec;
import com.github.dockerjava.core.exec.ListTasksCmdExec;
import com.github.dockerjava.core.exec.ListVolumesCmdExec;
import com.github.dockerjava.core.exec.LoadImageAsyncCmdExec;
import com.github.dockerjava.core.exec.LoadImageCmdExec;
import com.github.dockerjava.core.exec.LogContainerCmdExec;
import com.github.dockerjava.core.exec.LogSwarmObjectExec;
Expand All @@ -133,6 +132,7 @@
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.RemoveConfigCmdExec;
import com.github.dockerjava.core.exec.RemoveContainerCmdExec;
import com.github.dockerjava.core.exec.RemoveImageCmdExec;
import com.github.dockerjava.core.exec.RemoveNetworkCmdExec;
Expand All @@ -141,6 +141,8 @@
import com.github.dockerjava.core.exec.RemoveSwarmNodeCmdExec;
import com.github.dockerjava.core.exec.RemoveVolumeCmdExec;
import com.github.dockerjava.core.exec.RenameContainerCmdExec;
import com.github.dockerjava.core.exec.ResizeContainerCmdExec;
import com.github.dockerjava.core.exec.ResizeExecCmdExec;
import com.github.dockerjava.core.exec.RestartContainerCmdExec;
import com.github.dockerjava.core.exec.SaveImageCmdExec;
import com.github.dockerjava.core.exec.SaveImagesCmdExec;
Expand Down Expand Up @@ -255,6 +257,11 @@ public LoadImageCmd.Exec createLoadImageCmdExec() {
return new LoadImageCmdExec(getBaseResource(), getDockerClientConfig());
}

@Override
public LoadImageAsyncCmd.Exec createLoadImageAsyncCmdExec() {
return new LoadImageAsyncCmdExec(getBaseResource(), getDockerClientConfig());
}

@Override
public SearchImagesCmd.Exec createSearchImagesCmdExec() {
return new SearchImagesCmdExec(getBaseResource(), getDockerClientConfig());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@
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.LoadImageAsyncCmd;
import com.github.dockerjava.api.command.LoadImageCmd;
import com.github.dockerjava.api.command.LogContainerCmd;
import com.github.dockerjava.api.command.LogSwarmObjectCmd;
Expand Down Expand Up @@ -126,6 +127,7 @@
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.LoadImageAsyncCmdImpl;
import com.github.dockerjava.core.command.LoadImageCmdImpl;
import com.github.dockerjava.core.command.LogContainerCmdImpl;
import com.github.dockerjava.core.command.LogSwarmObjectImpl;
Expand Down Expand Up @@ -348,6 +350,11 @@ public LoadImageCmd loadImageCmd(@Nonnull InputStream imageStream) {
return new LoadImageCmdImpl(getDockerCmdExecFactory().createLoadImageCmdExec(), imageStream);
}

@Override
public LoadImageAsyncCmd loadImageAsyncCmd(@Nonnull InputStream imageStream) {
return new LoadImageAsyncCmdImpl(getDockerCmdExecFactory().createLoadImageAsyncCmdExec(), imageStream);
}

@Override
public SearchImagesCmd searchImagesCmd(String term) {
return new SearchImagesCmdImpl(getDockerCmdExecFactory().createSearchImagesCmdExec(), term);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
package com.github.dockerjava.core.command;

import com.github.dockerjava.api.command.LoadImageAsyncCmd;
import com.github.dockerjava.api.model.LoadImageAsyncResponseItem;

import javax.annotation.CheckForNull;
import javax.annotation.Nonnull;
import java.io.IOException;
import java.io.InputStream;

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


public class LoadImageAsyncCmdImpl extends AbstrAsyncDockerCmd<LoadImageAsyncCmd, LoadImageAsyncResponseItem> implements LoadImageAsyncCmd {

private InputStream imageStream;

public LoadImageAsyncCmdImpl(Exec exec, InputStream imageStream) {
super(exec);
withImageStream(imageStream);
}

@CheckForNull
@Override
public InputStream getImageStream() {
return imageStream;
}

@Override
public LoadImageAsyncCmd withImageStream(@Nonnull InputStream imageStream) {
checkNotNull(imageStream, "imageStream was not specified");
this.imageStream = imageStream;
return this;
}

@Override
public void close() {
super.close();
try {
imageStream.close();
} catch (IOException e) {
throw new RuntimeException(e);
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
package com.github.dockerjava.core.exec;

import com.fasterxml.jackson.core.type.TypeReference;
import com.github.dockerjava.api.async.ResultCallback;
import com.github.dockerjava.api.command.LoadImageAsyncCmd;
import com.github.dockerjava.api.model.LoadImageAsyncResponseItem;
import com.github.dockerjava.core.DockerClientConfig;
import com.github.dockerjava.core.WebTarget;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;


public class LoadImageAsyncCmdExec extends AbstrAsyncDockerCmdExec<LoadImageAsyncCmd, LoadImageAsyncResponseItem> implements
LoadImageAsyncCmd.Exec {

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


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


@Override
protected Void execute0(LoadImageAsyncCmd command, ResultCallback<LoadImageAsyncResponseItem> resultCallback) {
WebTarget webResource = getBaseResource().path("/images/load");

LOGGER.trace("POST: {}", webResource);
webResource.request().post(new TypeReference<LoadImageAsyncResponseItem>() {
}, resultCallback, command.getImageStream());
return null;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import com.github.dockerjava.api.model.Binds;
import com.github.dockerjava.api.model.BuildResponseItem;
import com.github.dockerjava.api.model.LoadImageAsyncResponseItem;
import com.github.dockerjava.api.model.PullResponseItem;
import com.github.dockerjava.api.model.PushResponseItem;
import com.github.dockerjava.api.model.ResponseItem;
Expand Down Expand Up @@ -35,6 +36,7 @@ public class ModelsSerializableTest {
PullResponseItem.class.getName(),
PushResponseItem.class.getName(),
ResponseItem.class.getName(),
LoadImageAsyncResponseItem.class.getName(),
ResponseItem.ErrorDetail.class.getName(),
ResponseItem.ProgressDetail.class.getName()
);
Expand Down
Loading