Skip to content

Commit 305a26a

Browse files
committed
pull image on ssh
1 parent b05d3a2 commit 305a26a

File tree

6 files changed

+70
-15
lines changed

6 files changed

+70
-15
lines changed

docker/src/main/java/com/flowci/docker/DockerSDKManager.java

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ public ImageManager getImageManager() {
4747
private class ImageMangerImpl implements ImageManager {
4848

4949
@Override
50-
public void pull(String image, int timeoutInSeconds, Consumer<PullResponseItem> progress) throws Exception {
50+
public void pull(String image, int timeoutInSeconds, Consumer<String> progress) throws Exception {
5151
String imageUrl = "docker.io/library/" + image;
5252
if (image.contains("/")) {
5353
imageUrl = "docker.io/" + image;
@@ -65,6 +65,10 @@ public void pull(String image, int timeoutInSeconds, Consumer<PullResponseItem>
6565
throw new DockerException(String.format("Timeout when pull image %s", image), 0);
6666
}
6767

68+
if (callback.hasError()) {
69+
throw new DockerException(callback.getThrowable().getMessage(), 0);
70+
}
71+
6872
if (findImage(client, image).isEmpty()) {
6973
throw new DockerException(String.format("Failed on pull image %s", image), 0);
7074
}
@@ -183,16 +187,16 @@ private DockerClient newClient() {
183187

184188
private static class PullImageCallback extends DockerCallback<PullResponseItem> {
185189

186-
private final Consumer<PullResponseItem> progress;
190+
private final Consumer<String> progress;
187191

188-
private PullImageCallback(Consumer<PullResponseItem> progress) {
192+
private PullImageCallback(Consumer<String> progress) {
189193
this.progress = progress;
190194
}
191195

192196
@Override
193197
public void onNext(PullResponseItem item) {
194198
if (progress != null && item != null) {
195-
progress.accept(item);
199+
progress.accept(item.getStatus());
196200
}
197201
}
198202
}

docker/src/main/java/com/flowci/docker/DockerSSHManager.java

Lines changed: 36 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,9 @@
88
import com.flowci.docker.domain.SSHOption;
99
import com.flowci.domain.ObjectWrapper;
1010
import com.flowci.util.StringHelper;
11+
import com.github.dockerjava.api.DockerClient;
1112
import com.github.dockerjava.api.command.InspectContainerResponse;
12-
import com.github.dockerjava.api.model.Container;
13-
import com.github.dockerjava.api.model.Frame;
14-
import com.github.dockerjava.api.model.StreamType;
13+
import com.github.dockerjava.api.model.*;
1514
import com.jcraft.jsch.*;
1615
import lombok.AllArgsConstructor;
1716
import lombok.Getter;
@@ -35,6 +34,8 @@ public class DockerSSHManager implements DockerManager {
3534

3635
private final ContainerManager containerManager = new ContainerManagerImpl();
3736

37+
private final ImageManager imageManager = new ImageManagerImpl();
38+
3839
static {
3940
mapper.setSerializationInclusion(JsonInclude.Include.NON_NULL);
4041
mapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
@@ -61,7 +62,38 @@ public ContainerManager getContainerManager() {
6162

6263
@Override
6364
public ImageManager getImageManager() {
64-
return null;
65+
return imageManager;
66+
}
67+
68+
private class ImageManagerImpl implements ImageManager {
69+
70+
@Override
71+
public void pull(String image, int ignore, Consumer<String> progress) throws Exception {
72+
List<Image> list = findImage(image);
73+
if (list.size() > 0) {
74+
return;
75+
}
76+
String cmd = String.format("docker pull %s", image);
77+
Output output = runCmd(cmd, (log) -> {
78+
if (progress != null) {
79+
progress.accept(log);
80+
}
81+
});
82+
throwExceptionIfError(output);
83+
}
84+
85+
private List<Image> findImage(String image) throws Exception {
86+
String cmd = String.format("docker image ls --filter reference=\"%s\" %s", image, FormatAsJson);
87+
List<Image> list = new LinkedList<>();
88+
Output output = runCmd(cmd, (line) -> {
89+
try {
90+
list.add(mapper.readValue(line, Image.class));
91+
} catch (JsonProcessingException ignore) {
92+
}
93+
});
94+
throwExceptionIfError(output);
95+
return list;
96+
}
6597
}
6698

6799
private class ContainerManagerImpl implements ContainerManager {
Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,11 @@
11
package com.flowci.docker;
22

3-
import com.github.dockerjava.api.model.PullResponseItem;
4-
53
import java.util.function.Consumer;
64

75
public interface ImageManager {
86

97
/**
108
* Sync call
119
*/
12-
void pull(String image, int timeoutInSeconds, Consumer<PullResponseItem> progress) throws Exception;
10+
void pull(String image, int timeoutInSeconds, Consumer<String> progress) throws Exception;
1311
}

docker/src/main/java/com/flowci/docker/domain/DockerCallback.java

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,14 +12,18 @@ public abstract class DockerCallback<T> implements ResultCallback<T> {
1212
@Getter
1313
protected final CountDownLatch counter = new CountDownLatch(1);
1414

15+
@Getter
16+
protected Throwable throwable;
17+
1518
@Override
1619
public void onStart(Closeable closeable) {
1720

1821
}
1922

2023
@Override
2124
public void onError(Throwable throwable) {
22-
25+
this.throwable = throwable;
26+
counter.countDown();
2327
}
2428

2529
@Override
@@ -31,4 +35,8 @@ public void onComplete() {
3135
public void close() throws IOException {
3236

3337
}
38+
39+
public boolean hasError() {
40+
return this.throwable != null;
41+
}
3442
}

docker/src/test/java/com/flowci/docker/test/DockerSDKManagerTest.java

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -29,9 +29,12 @@ public void clean() {
2929

3030
@Test
3131
public void should_pull_image() throws Exception {
32-
manager.getImageManager().pull("hello-world", 60, (item) -> {
33-
System.out.println(item.getStatus());
34-
});
32+
manager.getImageManager().pull("ubuntu:18.04", 60, System.out::println);
33+
}
34+
35+
@Test(expected = Exception.class)
36+
public void should_throw_exception_if_image_not_found() throws Exception {
37+
manager.getImageManager().pull("ubuntu:notfound", 120, null);
3538
}
3639

3740
@Test

docker/src/test/java/com/flowci/docker/test/DockerSSHManagerTest.java

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,16 @@ public void should_list_containers() throws Exception {
3232
Assert.assertNotNull(list);
3333
}
3434

35+
@Test
36+
public void should_pull_image_and_print_log() throws Exception {
37+
manager.getImageManager().pull("ubuntu:18.04", 60, System.out::println);
38+
}
39+
40+
@Test(expected = Exception.class)
41+
public void should_throw_exception_if_image_not_found() throws Exception {
42+
manager.getImageManager().pull("ubuntu:notfound", 10, null);
43+
}
44+
3545
@Test
3646
public void should_create_start_and_delete_container() throws Exception {
3747
DockerStartOption option = new DockerStartOption();

0 commit comments

Comments
 (0)