@Test public void pullPublicImage() throws Exception { final String testImage = "hello-world"; removeImage(testImage); assertTrue("No test image should exist before pulling", !imageExists(testImage)); manager.pullImage(testImage); assertTrue("Test image should exist after pulling", imageExists(testImage)); }
public void pullImage(String imageName) { if (!DOCKER_AUTOPULL) { LOGGER.warn("Skipping image pulling because DOCKER_AUTOPULL is unset"); return; } LOGGER.info("Pulling the image \"{}\"", imageName); ServiceSpec.Builder serviceCfgBuilder = ServiceSpec.builder(); TaskSpec.Builder taskCfgBuilder = TaskSpec.builder(); ContainerSpec.Builder cfgBuilder = ContainerSpec.builder(); serviceCfgBuilder.mode(ServiceMode.withGlobal()); taskCfgBuilder.restartPolicy(RestartPolicy.builder().condition(RestartPolicy.RESTART_POLICY_NONE).build()); cfgBuilder.image(imageName); if (gelfAddress != null) { Map<String, String> logOptions = new HashMap<String, String>(); logOptions.put("gelf-address", gelfAddress); logOptions.put("tag", LOGGING_TAG); taskCfgBuilder.logDriver(Driver.builder().name(LOGGING_DRIVER_GELF).options(logOptions).build()); } String[] command = imageName.equals("hello-world") ? new String[] { "/hello" } : new String[] { "true" }; cfgBuilder.command(command); ContainerSpec cfg = cfgBuilder.build(); taskCfgBuilder.containerSpec(cfg); serviceCfgBuilder.taskTemplate(taskCfgBuilder.build()); serviceCfgBuilder.name(getInstanceName(imageName, "pull")); ServiceSpec serviceCfg = serviceCfgBuilder.build(); Integer totalNodes; try { totalNodes = dockerClient.listNodes().size(); } catch (Exception e) { LOGGER.error("Couldn't retrieve list of swarm nodes!"); return; } try { ServiceCreateResponse resp; if ((gitlabAuth != null) && (imageName.startsWith(gitlabAuth.serverAddress()))) { resp = dockerClient.createService(serviceCfg, gitlabAuth); } else { resp = dockerClient.createService(serviceCfg, nullAuth); } String serviceId = resp.id(); LOGGER.info("Pulling service id: {}", serviceId); final Set<String> finshedTaskIds = Collections.synchronizedSet(new HashSet<String>()); try { Waiting.waitFor(() -> { List<Task> pullingTasks = dockerClient .listTasks(Task.Criteria.builder().serviceName(serviceId).build()); for (Task pullingTask : pullingTasks) { String state = pullingTask.status().state(); if (!UNFINISHED_TASK_STATES.contains(state)) { if (state.equals(TaskStatus.TASK_STATE_REJECTED)) { LOGGER.error("Couldn't pull image {} on node {}. {}", imageName, pullingTask.nodeId(), pullingTask.status().err()); throw new Exception("Couldn't pull image on node " + pullingTask.nodeId() + ": " + pullingTask.status().err()); } finshedTaskIds.add(pullingTask.id()); } } if (finshedTaskIds.size() >= totalNodes) { LOGGER.info("Swarm pulled the image \"{}\" ({})", imageName, pullingTasks.stream().map(t -> t.status().state()).collect(Collectors.joining(", "))); return true; } else { return false; } }, DOCKER_POLL_INTERVAL, DOCKER_IMAGE_PULL_MAX_WAITING_TIME); } catch (InterruptedException e) { LOGGER.warn( "Interrupted while waiting for the image {} to be pulled. Assuming that pulling was successful. Exception: {}", imageName, e.getLocalizedMessage()); } dockerClient.removeService(serviceId); } catch (Exception e) { LOGGER.error("Exception while pulling the image \"" + imageName + "\".", e); } }
ContainerManagerImpl implements ContainerManager { public void pullImage(String imageName) { if (!DOCKER_AUTOPULL) { LOGGER.warn("Skipping image pulling because DOCKER_AUTOPULL is unset"); return; } LOGGER.info("Pulling the image \"{}\"", imageName); ServiceSpec.Builder serviceCfgBuilder = ServiceSpec.builder(); TaskSpec.Builder taskCfgBuilder = TaskSpec.builder(); ContainerSpec.Builder cfgBuilder = ContainerSpec.builder(); serviceCfgBuilder.mode(ServiceMode.withGlobal()); taskCfgBuilder.restartPolicy(RestartPolicy.builder().condition(RestartPolicy.RESTART_POLICY_NONE).build()); cfgBuilder.image(imageName); if (gelfAddress != null) { Map<String, String> logOptions = new HashMap<String, String>(); logOptions.put("gelf-address", gelfAddress); logOptions.put("tag", LOGGING_TAG); taskCfgBuilder.logDriver(Driver.builder().name(LOGGING_DRIVER_GELF).options(logOptions).build()); } String[] command = imageName.equals("hello-world") ? new String[] { "/hello" } : new String[] { "true" }; cfgBuilder.command(command); ContainerSpec cfg = cfgBuilder.build(); taskCfgBuilder.containerSpec(cfg); serviceCfgBuilder.taskTemplate(taskCfgBuilder.build()); serviceCfgBuilder.name(getInstanceName(imageName, "pull")); ServiceSpec serviceCfg = serviceCfgBuilder.build(); Integer totalNodes; try { totalNodes = dockerClient.listNodes().size(); } catch (Exception e) { LOGGER.error("Couldn't retrieve list of swarm nodes!"); return; } try { ServiceCreateResponse resp; if ((gitlabAuth != null) && (imageName.startsWith(gitlabAuth.serverAddress()))) { resp = dockerClient.createService(serviceCfg, gitlabAuth); } else { resp = dockerClient.createService(serviceCfg, nullAuth); } String serviceId = resp.id(); LOGGER.info("Pulling service id: {}", serviceId); final Set<String> finshedTaskIds = Collections.synchronizedSet(new HashSet<String>()); try { Waiting.waitFor(() -> { List<Task> pullingTasks = dockerClient .listTasks(Task.Criteria.builder().serviceName(serviceId).build()); for (Task pullingTask : pullingTasks) { String state = pullingTask.status().state(); if (!UNFINISHED_TASK_STATES.contains(state)) { if (state.equals(TaskStatus.TASK_STATE_REJECTED)) { LOGGER.error("Couldn't pull image {} on node {}. {}", imageName, pullingTask.nodeId(), pullingTask.status().err()); throw new Exception("Couldn't pull image on node " + pullingTask.nodeId() + ": " + pullingTask.status().err()); } finshedTaskIds.add(pullingTask.id()); } } if (finshedTaskIds.size() >= totalNodes) { LOGGER.info("Swarm pulled the image \"{}\" ({})", imageName, pullingTasks.stream().map(t -> t.status().state()).collect(Collectors.joining(", "))); return true; } else { return false; } }, DOCKER_POLL_INTERVAL, DOCKER_IMAGE_PULL_MAX_WAITING_TIME); } catch (InterruptedException e) { LOGGER.warn( "Interrupted while waiting for the image {} to be pulled. Assuming that pulling was successful. Exception: {}", imageName, e.getLocalizedMessage()); } dockerClient.removeService(serviceId); } catch (Exception e) { LOGGER.error("Exception while pulling the image \"" + imageName + "\".", e); } } }
ContainerManagerImpl implements ContainerManager { public void pullImage(String imageName) { if (!DOCKER_AUTOPULL) { LOGGER.warn("Skipping image pulling because DOCKER_AUTOPULL is unset"); return; } LOGGER.info("Pulling the image \"{}\"", imageName); ServiceSpec.Builder serviceCfgBuilder = ServiceSpec.builder(); TaskSpec.Builder taskCfgBuilder = TaskSpec.builder(); ContainerSpec.Builder cfgBuilder = ContainerSpec.builder(); serviceCfgBuilder.mode(ServiceMode.withGlobal()); taskCfgBuilder.restartPolicy(RestartPolicy.builder().condition(RestartPolicy.RESTART_POLICY_NONE).build()); cfgBuilder.image(imageName); if (gelfAddress != null) { Map<String, String> logOptions = new HashMap<String, String>(); logOptions.put("gelf-address", gelfAddress); logOptions.put("tag", LOGGING_TAG); taskCfgBuilder.logDriver(Driver.builder().name(LOGGING_DRIVER_GELF).options(logOptions).build()); } String[] command = imageName.equals("hello-world") ? new String[] { "/hello" } : new String[] { "true" }; cfgBuilder.command(command); ContainerSpec cfg = cfgBuilder.build(); taskCfgBuilder.containerSpec(cfg); serviceCfgBuilder.taskTemplate(taskCfgBuilder.build()); serviceCfgBuilder.name(getInstanceName(imageName, "pull")); ServiceSpec serviceCfg = serviceCfgBuilder.build(); Integer totalNodes; try { totalNodes = dockerClient.listNodes().size(); } catch (Exception e) { LOGGER.error("Couldn't retrieve list of swarm nodes!"); return; } try { ServiceCreateResponse resp; if ((gitlabAuth != null) && (imageName.startsWith(gitlabAuth.serverAddress()))) { resp = dockerClient.createService(serviceCfg, gitlabAuth); } else { resp = dockerClient.createService(serviceCfg, nullAuth); } String serviceId = resp.id(); LOGGER.info("Pulling service id: {}", serviceId); final Set<String> finshedTaskIds = Collections.synchronizedSet(new HashSet<String>()); try { Waiting.waitFor(() -> { List<Task> pullingTasks = dockerClient .listTasks(Task.Criteria.builder().serviceName(serviceId).build()); for (Task pullingTask : pullingTasks) { String state = pullingTask.status().state(); if (!UNFINISHED_TASK_STATES.contains(state)) { if (state.equals(TaskStatus.TASK_STATE_REJECTED)) { LOGGER.error("Couldn't pull image {} on node {}. {}", imageName, pullingTask.nodeId(), pullingTask.status().err()); throw new Exception("Couldn't pull image on node " + pullingTask.nodeId() + ": " + pullingTask.status().err()); } finshedTaskIds.add(pullingTask.id()); } } if (finshedTaskIds.size() >= totalNodes) { LOGGER.info("Swarm pulled the image \"{}\" ({})", imageName, pullingTasks.stream().map(t -> t.status().state()).collect(Collectors.joining(", "))); return true; } else { return false; } }, DOCKER_POLL_INTERVAL, DOCKER_IMAGE_PULL_MAX_WAITING_TIME); } catch (InterruptedException e) { LOGGER.warn( "Interrupted while waiting for the image {} to be pulled. Assuming that pulling was successful. Exception: {}", imageName, e.getLocalizedMessage()); } dockerClient.removeService(serviceId); } catch (Exception e) { LOGGER.error("Exception while pulling the image \"" + imageName + "\".", e); } } ContainerManagerImpl(); }
ContainerManagerImpl implements ContainerManager { public void pullImage(String imageName) { if (!DOCKER_AUTOPULL) { LOGGER.warn("Skipping image pulling because DOCKER_AUTOPULL is unset"); return; } LOGGER.info("Pulling the image \"{}\"", imageName); ServiceSpec.Builder serviceCfgBuilder = ServiceSpec.builder(); TaskSpec.Builder taskCfgBuilder = TaskSpec.builder(); ContainerSpec.Builder cfgBuilder = ContainerSpec.builder(); serviceCfgBuilder.mode(ServiceMode.withGlobal()); taskCfgBuilder.restartPolicy(RestartPolicy.builder().condition(RestartPolicy.RESTART_POLICY_NONE).build()); cfgBuilder.image(imageName); if (gelfAddress != null) { Map<String, String> logOptions = new HashMap<String, String>(); logOptions.put("gelf-address", gelfAddress); logOptions.put("tag", LOGGING_TAG); taskCfgBuilder.logDriver(Driver.builder().name(LOGGING_DRIVER_GELF).options(logOptions).build()); } String[] command = imageName.equals("hello-world") ? new String[] { "/hello" } : new String[] { "true" }; cfgBuilder.command(command); ContainerSpec cfg = cfgBuilder.build(); taskCfgBuilder.containerSpec(cfg); serviceCfgBuilder.taskTemplate(taskCfgBuilder.build()); serviceCfgBuilder.name(getInstanceName(imageName, "pull")); ServiceSpec serviceCfg = serviceCfgBuilder.build(); Integer totalNodes; try { totalNodes = dockerClient.listNodes().size(); } catch (Exception e) { LOGGER.error("Couldn't retrieve list of swarm nodes!"); return; } try { ServiceCreateResponse resp; if ((gitlabAuth != null) && (imageName.startsWith(gitlabAuth.serverAddress()))) { resp = dockerClient.createService(serviceCfg, gitlabAuth); } else { resp = dockerClient.createService(serviceCfg, nullAuth); } String serviceId = resp.id(); LOGGER.info("Pulling service id: {}", serviceId); final Set<String> finshedTaskIds = Collections.synchronizedSet(new HashSet<String>()); try { Waiting.waitFor(() -> { List<Task> pullingTasks = dockerClient .listTasks(Task.Criteria.builder().serviceName(serviceId).build()); for (Task pullingTask : pullingTasks) { String state = pullingTask.status().state(); if (!UNFINISHED_TASK_STATES.contains(state)) { if (state.equals(TaskStatus.TASK_STATE_REJECTED)) { LOGGER.error("Couldn't pull image {} on node {}. {}", imageName, pullingTask.nodeId(), pullingTask.status().err()); throw new Exception("Couldn't pull image on node " + pullingTask.nodeId() + ": " + pullingTask.status().err()); } finshedTaskIds.add(pullingTask.id()); } } if (finshedTaskIds.size() >= totalNodes) { LOGGER.info("Swarm pulled the image \"{}\" ({})", imageName, pullingTasks.stream().map(t -> t.status().state()).collect(Collectors.joining(", "))); return true; } else { return false; } }, DOCKER_POLL_INTERVAL, DOCKER_IMAGE_PULL_MAX_WAITING_TIME); } catch (InterruptedException e) { LOGGER.warn( "Interrupted while waiting for the image {} to be pulled. Assuming that pulling was successful. Exception: {}", imageName, e.getLocalizedMessage()); } dockerClient.removeService(serviceId); } catch (Exception e) { LOGGER.error("Exception while pulling the image \"" + imageName + "\".", e); } } ContainerManagerImpl(); void pullImage(String imageName); @Deprecated String startContainer(String imageName); @Deprecated String startContainer(String imageName, String[] command); String startContainer(String imageName, String type, String parent); @Override String startContainer(String imageName, String containerType, String parentId, String[] command); @Override String startContainer(String imageName, String containerType, String parentId, String[] env,
            String[] command); @Override String startContainer(String imageName, String containerType, String parentId, String[] env,
    String[] netAliases, String[] command); @Override String startContainer(String imageName, String containerType, String parentId, String[] env,
            String[] command, boolean pullImage); @Override String startContainer(String imageName, String containerType, String parentId, String[] env,
            String[] netAliases, String[] command, boolean pullImage); @Override String startContainer(String imageName, String containerType, String parentId, String[] env,
            String[] netAliases, String[] command, String experimentId); @Override void removeContainer(String serviceName); @Deprecated @Override void stopContainer(String containerId); @Deprecated @Override void stopParentAndChildren(String parentId); @Override void removeParentAndChildren(String parent); @Override Service getContainerInfo(String serviceName); @Override List<Service> getContainers(Service.Criteria criteria); @Override Integer getContainerExitCode(String serviceName); @Deprecated @Override String getContainerId(String name); @Deprecated @Override String getContainerName(String containerId); @Override void addContainerObserver(ContainerStateObserver containerObserver); static boolean containsVersionTag(String imageName); @Override ContainerStats getStats(String containerId); }
ContainerManagerImpl implements ContainerManager { public void pullImage(String imageName) { if (!DOCKER_AUTOPULL) { LOGGER.warn("Skipping image pulling because DOCKER_AUTOPULL is unset"); return; } LOGGER.info("Pulling the image \"{}\"", imageName); ServiceSpec.Builder serviceCfgBuilder = ServiceSpec.builder(); TaskSpec.Builder taskCfgBuilder = TaskSpec.builder(); ContainerSpec.Builder cfgBuilder = ContainerSpec.builder(); serviceCfgBuilder.mode(ServiceMode.withGlobal()); taskCfgBuilder.restartPolicy(RestartPolicy.builder().condition(RestartPolicy.RESTART_POLICY_NONE).build()); cfgBuilder.image(imageName); if (gelfAddress != null) { Map<String, String> logOptions = new HashMap<String, String>(); logOptions.put("gelf-address", gelfAddress); logOptions.put("tag", LOGGING_TAG); taskCfgBuilder.logDriver(Driver.builder().name(LOGGING_DRIVER_GELF).options(logOptions).build()); } String[] command = imageName.equals("hello-world") ? new String[] { "/hello" } : new String[] { "true" }; cfgBuilder.command(command); ContainerSpec cfg = cfgBuilder.build(); taskCfgBuilder.containerSpec(cfg); serviceCfgBuilder.taskTemplate(taskCfgBuilder.build()); serviceCfgBuilder.name(getInstanceName(imageName, "pull")); ServiceSpec serviceCfg = serviceCfgBuilder.build(); Integer totalNodes; try { totalNodes = dockerClient.listNodes().size(); } catch (Exception e) { LOGGER.error("Couldn't retrieve list of swarm nodes!"); return; } try { ServiceCreateResponse resp; if ((gitlabAuth != null) && (imageName.startsWith(gitlabAuth.serverAddress()))) { resp = dockerClient.createService(serviceCfg, gitlabAuth); } else { resp = dockerClient.createService(serviceCfg, nullAuth); } String serviceId = resp.id(); LOGGER.info("Pulling service id: {}", serviceId); final Set<String> finshedTaskIds = Collections.synchronizedSet(new HashSet<String>()); try { Waiting.waitFor(() -> { List<Task> pullingTasks = dockerClient .listTasks(Task.Criteria.builder().serviceName(serviceId).build()); for (Task pullingTask : pullingTasks) { String state = pullingTask.status().state(); if (!UNFINISHED_TASK_STATES.contains(state)) { if (state.equals(TaskStatus.TASK_STATE_REJECTED)) { LOGGER.error("Couldn't pull image {} on node {}. {}", imageName, pullingTask.nodeId(), pullingTask.status().err()); throw new Exception("Couldn't pull image on node " + pullingTask.nodeId() + ": " + pullingTask.status().err()); } finshedTaskIds.add(pullingTask.id()); } } if (finshedTaskIds.size() >= totalNodes) { LOGGER.info("Swarm pulled the image \"{}\" ({})", imageName, pullingTasks.stream().map(t -> t.status().state()).collect(Collectors.joining(", "))); return true; } else { return false; } }, DOCKER_POLL_INTERVAL, DOCKER_IMAGE_PULL_MAX_WAITING_TIME); } catch (InterruptedException e) { LOGGER.warn( "Interrupted while waiting for the image {} to be pulled. Assuming that pulling was successful. Exception: {}", imageName, e.getLocalizedMessage()); } dockerClient.removeService(serviceId); } catch (Exception e) { LOGGER.error("Exception while pulling the image \"" + imageName + "\".", e); } } ContainerManagerImpl(); void pullImage(String imageName); @Deprecated String startContainer(String imageName); @Deprecated String startContainer(String imageName, String[] command); String startContainer(String imageName, String type, String parent); @Override String startContainer(String imageName, String containerType, String parentId, String[] command); @Override String startContainer(String imageName, String containerType, String parentId, String[] env,
            String[] command); @Override String startContainer(String imageName, String containerType, String parentId, String[] env,
    String[] netAliases, String[] command); @Override String startContainer(String imageName, String containerType, String parentId, String[] env,
            String[] command, boolean pullImage); @Override String startContainer(String imageName, String containerType, String parentId, String[] env,
            String[] netAliases, String[] command, boolean pullImage); @Override String startContainer(String imageName, String containerType, String parentId, String[] env,
            String[] netAliases, String[] command, String experimentId); @Override void removeContainer(String serviceName); @Deprecated @Override void stopContainer(String containerId); @Deprecated @Override void stopParentAndChildren(String parentId); @Override void removeParentAndChildren(String parent); @Override Service getContainerInfo(String serviceName); @Override List<Service> getContainers(Service.Criteria criteria); @Override Integer getContainerExitCode(String serviceName); @Deprecated @Override String getContainerId(String name); @Deprecated @Override String getContainerName(String containerId); @Override void addContainerObserver(ContainerStateObserver containerObserver); static boolean containsVersionTag(String imageName); @Override ContainerStats getStats(String containerId); static final String DEPLOY_ENV_KEY; static final String DOCKER_AUTOPULL_ENV_KEY; static final String LOGGING_GELF_ADDRESS_KEY; static final String USER_NAME_KEY; static final String USER_EMAIL_KEY; static final String USER_PASSWORD_KEY; static final String REGISTRY_URL_KEY; static final String HOBBIT_DOCKER_NETWORK; static final String LOGGING_TAG; static final Set<String> NEW_TASKS_STATES; static final Set<String> UNFINISHED_TASK_STATES; }