|
22 | 22 |
|
23 | 23 | import com.spotify.docker.client.DockerClient.ListContainersParam; |
24 | 24 | import com.spotify.docker.client.DockerClient.RemoveContainerParam; |
| 25 | +import com.spotify.docker.client.ProgressHandler; |
| 26 | +import com.spotify.docker.client.exceptions.DockerException; |
| 27 | +import com.spotify.docker.client.exceptions.NotFoundException; |
25 | 28 | import com.spotify.docker.client.messages.Container.PortMapping; |
26 | 29 | import com.spotify.docker.client.messages.ContainerConfig; |
27 | 30 | import com.spotify.docker.client.messages.ContainerCreation; |
28 | 31 | import com.spotify.docker.client.messages.ContainerInfo; |
29 | 32 | import com.spotify.docker.client.messages.HostConfig; |
30 | 33 | import com.spotify.docker.client.messages.HostConfig.Builder; |
31 | 34 | import com.spotify.docker.client.messages.PortBinding; |
| 35 | +import com.spotify.docker.client.messages.ProgressMessage; |
| 36 | +import com.spotify.docker.client.messages.RegistryAuth; |
32 | 37 | import eu.openanalytics.containerproxy.model.runtime.Container; |
33 | 38 | import eu.openanalytics.containerproxy.model.runtime.ExistingContainerInfo; |
34 | 39 | import eu.openanalytics.containerproxy.model.runtime.Proxy; |
|
37 | 42 | import eu.openanalytics.containerproxy.model.runtime.runtimevalues.RuntimeValueKey; |
38 | 43 | import eu.openanalytics.containerproxy.model.runtime.runtimevalues.UserIdKey; |
39 | 44 | import eu.openanalytics.containerproxy.model.spec.ContainerSpec; |
| 45 | +import org.slf4j.Logger; |
| 46 | +import org.slf4j.LoggerFactory; |
40 | 47 |
|
41 | 48 | import java.net.URI; |
42 | 49 | import java.net.URL; |
|
49 | 56 |
|
50 | 57 | public class DockerEngineBackend extends AbstractDockerBackend { |
51 | 58 |
|
| 59 | + private static final String PROPERTY_IMG_PULL_POLICY = "image-pull-policy"; |
| 60 | + |
| 61 | + private ImagePullPolicy imagePullPolicy; |
| 62 | + private final Logger logger = LoggerFactory.getLogger(getClass()); |
| 63 | + |
| 64 | + @Override |
| 65 | + public void initialize() { |
| 66 | + super.initialize(); |
| 67 | + imagePullPolicy = environment.getProperty(getPropertyPrefix() + PROPERTY_IMG_PULL_POLICY, ImagePullPolicy.class, ImagePullPolicy.IfNotPresent); |
| 68 | + } |
| 69 | + |
52 | 70 | @Override |
53 | 71 | protected Container startContainer(ContainerSpec spec, Proxy proxy) throws Exception { |
54 | 72 | Builder hostConfigBuilder = HostConfig.builder(); |
55 | | - |
| 73 | + |
| 74 | + if (imagePullPolicy == ImagePullPolicy.Always |
| 75 | + || (imagePullPolicy == ImagePullPolicy.IfNotPresent && !isImagePresent(spec))) { |
| 76 | + logger.info("Pulling image {}", spec.getImage()); |
| 77 | + pullImage(spec); |
| 78 | + } |
| 79 | + |
56 | 80 | Map<String, List<PortBinding>> portBindings = new HashMap<>(); |
57 | 81 | if (isUseInternalNetwork()) { |
58 | 82 | // In internal networking mode, we can access container ports directly, no need to bind on host. |
@@ -203,6 +227,36 @@ public List<ExistingContainerInfo> scanExistingContainers() throws Exception { |
203 | 227 |
|
204 | 228 | return containers; |
205 | 229 | } |
206 | | - |
| 230 | + |
| 231 | + private boolean isImagePresent(ContainerSpec spec) throws DockerException, InterruptedException { |
| 232 | + try { |
| 233 | + dockerClient.inspectImage(spec.getImage()); |
| 234 | + return true; |
| 235 | + } catch (NotFoundException ex) { |
| 236 | + return false; |
| 237 | + } |
| 238 | + } |
| 239 | + |
| 240 | + private void pullImage(ContainerSpec spec) throws DockerException, InterruptedException { |
| 241 | + if (spec.getDockerSwarmRegistryDomain() != null |
| 242 | + && spec.getDockerSwarmRegistryUsername() != null |
| 243 | + && spec.getDockerSwarmRegistryPassword() != null) { |
| 244 | + |
| 245 | + RegistryAuth registryAuth = RegistryAuth.builder() |
| 246 | + .serverAddress(spec.getDockerSwarmRegistryDomain()) |
| 247 | + .username(spec.getDockerSwarmRegistryUsername()) |
| 248 | + .password(spec.getDockerSwarmRegistryPassword()) |
| 249 | + .build(); |
| 250 | + dockerClient.pull(spec.getImage(), registryAuth, message -> {}); |
| 251 | + } else { |
| 252 | + dockerClient.pull(spec.getImage(), message -> {}); |
| 253 | + } |
| 254 | + } |
| 255 | + |
| 256 | + public enum ImagePullPolicy { |
| 257 | + Never, |
| 258 | + Always, |
| 259 | + IfNotPresent |
| 260 | + } |
207 | 261 |
|
208 | 262 | } |
0 commit comments