Skip to content

Commit d2d89d8

Browse files
author
逯文涛
committed
load image and get docker response
1 parent 0306031 commit d2d89d8

File tree

16 files changed

+388
-5
lines changed

16 files changed

+388
-5
lines changed

docker-java-api/src/main/java/com/github/dockerjava/api/DockerClient.java

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@
4242
import com.github.dockerjava.api.command.ListSwarmNodesCmd;
4343
import com.github.dockerjava.api.command.ListTasksCmd;
4444
import com.github.dockerjava.api.command.ListVolumesCmd;
45+
import com.github.dockerjava.api.command.LoadImageAsyncCmd;
4546
import com.github.dockerjava.api.command.LoadImageCmd;
4647
import com.github.dockerjava.api.command.LogContainerCmd;
4748
import com.github.dockerjava.api.command.LogSwarmObjectCmd;
@@ -130,6 +131,13 @@ public interface DockerClient extends Closeable {
130131
*/
131132
LoadImageCmd loadImageCmd(@Nonnull InputStream imageStream);
132133

134+
/**
135+
* Loads a tarball with a set of images and tags into a Docker repository and get response
136+
* @param imageStream stream of the tarball file
137+
* @return created command
138+
*/
139+
LoadImageAsyncCmd loadImageAsyncCmd(@Nonnull InputStream imageStream);
140+
133141
SearchImagesCmd searchImagesCmd(@Nonnull String term);
134142

135143
RemoveImageCmd removeImageCmd(@Nonnull String imageId);

docker-java-api/src/main/java/com/github/dockerjava/api/command/DelegatingDockerCmdExecFactory.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,11 @@ public LoadImageCmd.Exec createLoadImageCmdExec() {
7575
return getDockerCmdExecFactory().createLoadImageCmdExec();
7676
}
7777

78+
@Override
79+
public LoadImageAsyncCmd.Exec createLoadImageAsyncCmdExec() {
80+
return getDockerCmdExecFactory().createLoadImageAsyncCmdExec();
81+
}
82+
7883
@Override
7984
public SearchImagesCmd.Exec createSearchImagesCmdExec() {
8085
return getDockerCmdExecFactory().createSearchImagesCmdExec();

docker-java-api/src/main/java/com/github/dockerjava/api/command/DockerCmdExecFactory.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,8 @@ public interface DockerCmdExecFactory extends Closeable {
2727

2828
LoadImageCmd.Exec createLoadImageCmdExec();
2929

30+
LoadImageAsyncCmd.Exec createLoadImageAsyncCmdExec();
31+
3032
SearchImagesCmd.Exec createSearchImagesCmdExec();
3133

3234
RemoveImageCmd.Exec createRemoveImageCmdExec();
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
package com.github.dockerjava.api.command;
2+
3+
import com.github.dockerjava.api.model.LoadImageAsyncResponseItem;
4+
5+
import javax.annotation.CheckForNull;
6+
import javax.annotation.Nonnull;
7+
import java.io.InputStream;
8+
9+
/**
10+
* load image async and return image id or tag
11+
*/
12+
public interface LoadImageAsyncCmd extends AsyncDockerCmd<LoadImageAsyncCmd, LoadImageAsyncResponseItem> {
13+
14+
@CheckForNull
15+
InputStream getImageStream();
16+
17+
/**
18+
* @param imageStream
19+
* the InputStream of the tar file
20+
*/
21+
LoadImageAsyncCmd withImageStream(@Nonnull InputStream imageStream);
22+
23+
interface Exec extends DockerCmdAsyncExec<LoadImageAsyncCmd, LoadImageAsyncResponseItem> {
24+
}
25+
26+
@Override
27+
default LoadImageAsyncResultCallback start() {
28+
return exec(new LoadImageAsyncResultCallback());
29+
}
30+
}
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
package com.github.dockerjava.api.command;
2+
3+
import com.github.dockerjava.api.async.ResultCallbackTemplate;
4+
import com.github.dockerjava.api.exception.DockerClientException;
5+
import com.github.dockerjava.api.model.LoadImageAsyncResponseItem;
6+
import org.slf4j.Logger;
7+
import org.slf4j.LoggerFactory;
8+
9+
10+
public class LoadImageAsyncResultCallback extends ResultCallbackTemplate<LoadImageAsyncResultCallback, LoadImageAsyncResponseItem> {
11+
12+
private static final Logger LOGGER = LoggerFactory.getLogger(LoadImageAsyncResultCallback.class);
13+
14+
private String imageId;
15+
16+
private String error;
17+
18+
19+
@Override
20+
public void onNext(LoadImageAsyncResponseItem item) {
21+
if (item.isLoadSuccessIndicated()) {
22+
this.imageId = item.getImageId();
23+
} else if (item.isErrorIndicated()) {
24+
this.error = item.getError();
25+
}
26+
LOGGER.debug(item.toString());
27+
}
28+
29+
/**
30+
* Awaits the image id from the response stream.
31+
*
32+
* @throws DockerClientException
33+
* if the load fails.
34+
*/
35+
public String awaitImageId() {
36+
try {
37+
awaitCompletion();
38+
} catch (InterruptedException e) {
39+
throw new DockerClientException("", e);
40+
}
41+
42+
return getImageId();
43+
}
44+
45+
private String getImageId() {
46+
if (imageId != null) {
47+
return imageId;
48+
}
49+
50+
if (error == null) {
51+
throw new DockerClientException("Could not load image");
52+
}
53+
54+
throw new DockerClientException("Could not load image: " + error);
55+
}
56+
}
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
package com.github.dockerjava.api.model;
2+
3+
import com.fasterxml.jackson.annotation.JsonIgnore;
4+
5+
6+
public class LoadImageAsyncResponseItem extends ResponseItem {
7+
8+
private static final long serialVersionUID = 6531167952240938125L;
9+
10+
private static final String LOADED_SUCCESS = "Loaded image";
11+
12+
private static final String LOADED_ID_SUCCESS = "Loaded image ID: sha256:";
13+
14+
private static final String LOADED_TAG_SUCCESS = "Loaded image:";
15+
16+
/**
17+
* Returns whether the stream field indicates a successful load operation
18+
*/
19+
@JsonIgnore
20+
public boolean isLoadSuccessIndicated() {
21+
if (isErrorIndicated() || getStream() == null) {
22+
return false;
23+
}
24+
return getStream().contains(LOADED_SUCCESS);
25+
}
26+
27+
@JsonIgnore
28+
public String getImageId() {
29+
if (!isLoadSuccessIndicated()) {
30+
return null;
31+
}
32+
// loaded image successful then get image id
33+
if (getStream().startsWith(LOADED_ID_SUCCESS)) {
34+
return getStream().replaceFirst(LOADED_ID_SUCCESS, "").trim();
35+
}
36+
// else get image tag
37+
return getStream().replaceFirst(LOADED_TAG_SUCCESS, "").trim();
38+
}
39+
}

docker-java-core/src/main/java/com/github/dockerjava/core/AbstractDockerCmdExecFactory.java

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@
4444
import com.github.dockerjava.api.command.ListSwarmNodesCmd;
4545
import com.github.dockerjava.api.command.ListTasksCmd;
4646
import com.github.dockerjava.api.command.ListVolumesCmd;
47+
import com.github.dockerjava.api.command.LoadImageAsyncCmd;
4748
import com.github.dockerjava.api.command.LoadImageCmd;
4849
import com.github.dockerjava.api.command.LogContainerCmd;
4950
import com.github.dockerjava.api.command.LogSwarmObjectCmd;
@@ -99,13 +100,9 @@
99100
import com.github.dockerjava.core.exec.EventsCmdExec;
100101
import com.github.dockerjava.core.exec.ExecCreateCmdExec;
101102
import com.github.dockerjava.core.exec.ExecStartCmdExec;
102-
import com.github.dockerjava.core.exec.InspectConfigCmdExec;
103-
import com.github.dockerjava.core.exec.ListConfigsCmdExec;
104-
import com.github.dockerjava.core.exec.RemoveConfigCmdExec;
105-
import com.github.dockerjava.core.exec.ResizeContainerCmdExec;
106-
import com.github.dockerjava.core.exec.ResizeExecCmdExec;
107103
import com.github.dockerjava.core.exec.InfoCmdExec;
108104
import com.github.dockerjava.core.exec.InitializeSwarmCmdExec;
105+
import com.github.dockerjava.core.exec.InspectConfigCmdExec;
109106
import com.github.dockerjava.core.exec.InspectContainerCmdExec;
110107
import com.github.dockerjava.core.exec.InspectExecCmdExec;
111108
import com.github.dockerjava.core.exec.InspectImageCmdExec;
@@ -117,6 +114,7 @@
117114
import com.github.dockerjava.core.exec.JoinSwarmCmdExec;
118115
import com.github.dockerjava.core.exec.KillContainerCmdExec;
119116
import com.github.dockerjava.core.exec.LeaveSwarmCmdExec;
117+
import com.github.dockerjava.core.exec.ListConfigsCmdExec;
120118
import com.github.dockerjava.core.exec.ListContainersCmdExec;
121119
import com.github.dockerjava.core.exec.ListImagesCmdExec;
122120
import com.github.dockerjava.core.exec.ListNetworksCmdExec;
@@ -125,6 +123,7 @@
125123
import com.github.dockerjava.core.exec.ListSwarmNodesCmdExec;
126124
import com.github.dockerjava.core.exec.ListTasksCmdExec;
127125
import com.github.dockerjava.core.exec.ListVolumesCmdExec;
126+
import com.github.dockerjava.core.exec.LoadImageAsyncCmdExec;
128127
import com.github.dockerjava.core.exec.LoadImageCmdExec;
129128
import com.github.dockerjava.core.exec.LogContainerCmdExec;
130129
import com.github.dockerjava.core.exec.LogSwarmObjectExec;
@@ -133,6 +132,7 @@
133132
import com.github.dockerjava.core.exec.PruneCmdExec;
134133
import com.github.dockerjava.core.exec.PullImageCmdExec;
135134
import com.github.dockerjava.core.exec.PushImageCmdExec;
135+
import com.github.dockerjava.core.exec.RemoveConfigCmdExec;
136136
import com.github.dockerjava.core.exec.RemoveContainerCmdExec;
137137
import com.github.dockerjava.core.exec.RemoveImageCmdExec;
138138
import com.github.dockerjava.core.exec.RemoveNetworkCmdExec;
@@ -141,6 +141,8 @@
141141
import com.github.dockerjava.core.exec.RemoveSwarmNodeCmdExec;
142142
import com.github.dockerjava.core.exec.RemoveVolumeCmdExec;
143143
import com.github.dockerjava.core.exec.RenameContainerCmdExec;
144+
import com.github.dockerjava.core.exec.ResizeContainerCmdExec;
145+
import com.github.dockerjava.core.exec.ResizeExecCmdExec;
144146
import com.github.dockerjava.core.exec.RestartContainerCmdExec;
145147
import com.github.dockerjava.core.exec.SaveImageCmdExec;
146148
import com.github.dockerjava.core.exec.SaveImagesCmdExec;
@@ -255,6 +257,11 @@ public LoadImageCmd.Exec createLoadImageCmdExec() {
255257
return new LoadImageCmdExec(getBaseResource(), getDockerClientConfig());
256258
}
257259

260+
@Override
261+
public LoadImageAsyncCmd.Exec createLoadImageAsyncCmdExec() {
262+
return new LoadImageAsyncCmdExec(getBaseResource(), getDockerClientConfig());
263+
}
264+
258265
@Override
259266
public SearchImagesCmd.Exec createSearchImagesCmdExec() {
260267
return new SearchImagesCmdExec(getBaseResource(), getDockerClientConfig());

docker-java-core/src/main/java/com/github/dockerjava/core/DockerClientImpl.java

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@
4444
import com.github.dockerjava.api.command.ListSwarmNodesCmd;
4545
import com.github.dockerjava.api.command.ListTasksCmd;
4646
import com.github.dockerjava.api.command.ListVolumesCmd;
47+
import com.github.dockerjava.api.command.LoadImageAsyncCmd;
4748
import com.github.dockerjava.api.command.LoadImageCmd;
4849
import com.github.dockerjava.api.command.LogContainerCmd;
4950
import com.github.dockerjava.api.command.LogSwarmObjectCmd;
@@ -126,6 +127,7 @@
126127
import com.github.dockerjava.core.command.ListSwarmNodesCmdImpl;
127128
import com.github.dockerjava.core.command.ListTasksCmdImpl;
128129
import com.github.dockerjava.core.command.ListVolumesCmdImpl;
130+
import com.github.dockerjava.core.command.LoadImageAsyncCmdImpl;
129131
import com.github.dockerjava.core.command.LoadImageCmdImpl;
130132
import com.github.dockerjava.core.command.LogContainerCmdImpl;
131133
import com.github.dockerjava.core.command.LogSwarmObjectImpl;
@@ -348,6 +350,11 @@ public LoadImageCmd loadImageCmd(@Nonnull InputStream imageStream) {
348350
return new LoadImageCmdImpl(getDockerCmdExecFactory().createLoadImageCmdExec(), imageStream);
349351
}
350352

353+
@Override
354+
public LoadImageAsyncCmd loadImageAsyncCmd(@Nonnull InputStream imageStream) {
355+
return new LoadImageAsyncCmdImpl(getDockerCmdExecFactory().createLoadImageAsyncCmdExec(), imageStream);
356+
}
357+
351358
@Override
352359
public SearchImagesCmd searchImagesCmd(String term) {
353360
return new SearchImagesCmdImpl(getDockerCmdExecFactory().createSearchImagesCmdExec(), term);
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
package com.github.dockerjava.core.command;
2+
3+
import com.github.dockerjava.api.command.LoadImageAsyncCmd;
4+
import com.github.dockerjava.api.model.LoadImageAsyncResponseItem;
5+
6+
import javax.annotation.CheckForNull;
7+
import javax.annotation.Nonnull;
8+
import java.io.IOException;
9+
import java.io.InputStream;
10+
11+
import static com.google.common.base.Preconditions.checkNotNull;
12+
13+
14+
public class LoadImageAsyncCmdImpl extends AbstrAsyncDockerCmd<LoadImageAsyncCmd, LoadImageAsyncResponseItem> implements LoadImageAsyncCmd {
15+
16+
private InputStream imageStream;
17+
18+
public LoadImageAsyncCmdImpl(Exec exec, InputStream imageStream) {
19+
super(exec);
20+
withImageStream(imageStream);
21+
}
22+
23+
@CheckForNull
24+
@Override
25+
public InputStream getImageStream() {
26+
return imageStream;
27+
}
28+
29+
@Override
30+
public LoadImageAsyncCmd withImageStream(@Nonnull InputStream imageStream) {
31+
checkNotNull(imageStream, "imageStream was not specified");
32+
this.imageStream = imageStream;
33+
return this;
34+
}
35+
36+
@Override
37+
public void close() {
38+
super.close();
39+
try {
40+
imageStream.close();
41+
} catch (IOException e) {
42+
throw new RuntimeException(e);
43+
}
44+
}
45+
}
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
package com.github.dockerjava.core.exec;
2+
3+
import com.fasterxml.jackson.core.type.TypeReference;
4+
import com.github.dockerjava.api.async.ResultCallback;
5+
import com.github.dockerjava.api.command.LoadImageAsyncCmd;
6+
import com.github.dockerjava.api.model.LoadImageAsyncResponseItem;
7+
import com.github.dockerjava.core.DockerClientConfig;
8+
import com.github.dockerjava.core.WebTarget;
9+
import org.slf4j.Logger;
10+
import org.slf4j.LoggerFactory;
11+
12+
13+
public class LoadImageAsyncCmdExec extends AbstrAsyncDockerCmdExec<LoadImageAsyncCmd, LoadImageAsyncResponseItem> implements
14+
LoadImageAsyncCmd.Exec {
15+
16+
private static final Logger LOGGER = LoggerFactory.getLogger(LoadImageAsyncCmdExec.class);
17+
18+
19+
public LoadImageAsyncCmdExec(WebTarget baseResource, DockerClientConfig dockerClientConfig) {
20+
super(baseResource, dockerClientConfig);
21+
}
22+
23+
24+
@Override
25+
protected Void execute0(LoadImageAsyncCmd command, ResultCallback<LoadImageAsyncResponseItem> resultCallback) {
26+
WebTarget webResource = getBaseResource().path("/images/load");
27+
28+
LOGGER.trace("POST: {}", webResource);
29+
webResource.request().post(new TypeReference<LoadImageAsyncResponseItem>() {
30+
}, resultCallback, command.getImageStream());
31+
return null;
32+
}
33+
}

0 commit comments

Comments
 (0)