private List<Container> listChildContainers() { return dockerClient.listContainersCmd() .withShowAll(true) .exec().stream() .filter(container -> Arrays.stream(container.getNames()).anyMatch(name -> name.startsWith("/" + project))) .collect(toList()); }
private Set<Link> findLinksFromThisContainer(String alias, LinkableContainer linkableContainer) { return dockerClient.listContainersCmd() .withStatusFilter(Arrays.asList("running")) .exec().stream() .flatMap(container -> Stream.of(container.getNames())) .filter(name -> name.endsWith(linkableContainer.getContainerName())) .map(name -> new Link(name, alias)) .collect(Collectors.toSet()); }
private Set<String> findAllNetworksForLinkedContainers(LinkableContainer linkableContainer) { return dockerClient.listContainersCmd().exec().stream() .filter(container -> container.getNames()[0].endsWith(linkableContainer.getContainerName())) .filter(container -> container.getNetworkSettings() != null && container.getNetworkSettings().getNetworks() != null) .flatMap(container -> container.getNetworkSettings().getNetworks().keySet().stream()) .distinct() .collect(Collectors.toSet()); }
public boolean isServiceReady() { try { List<Container> allContainers = dockerClient.listContainersCmd().withShowAll(true).exec(); for (Container container : allContainers) { if (asList(container.getNames()).contains("/" + otherContainerName) && !container.getStatus().contains("Exited")) { return true; } } } catch (DockerException e) { return false; } return false; } }
private void createServiceInstance(Container container) { String serviceName = getServiceNameFromContainer(container); final ComposeServiceWaitStrategyTarget containerInstance = new ComposeServiceWaitStrategyTarget(container, ambassadorContainer, ambassadorPortMappings.getOrDefault(serviceName, new HashMap<>())); String containerId = containerInstance.getContainerId(); if (tailChildContainers) { followLogs(containerId, new Slf4jLogConsumer(logger()).withPrefix(container.getNames()[0])); } //follow logs using registered consumers for this service logConsumers.getOrDefault(serviceName, Collections.emptyList()).forEach(consumer -> followLogs(containerId, consumer)); serviceInstanceMap.putIfAbsent(serviceName, containerInstance); }
public String getContainerName(Container container) { return container.getNames()[0].substring(1); }
/** * Filters the set based on the container name * * @param pattern regular expression pattern of the container name * @return filtered set */ public DockerContainersUtil filterByName(String pattern) { if (this.containers == null) { return this; } List<Container> matched = new ArrayList<>(); for (Container container : containers) { String[] names = container.getNames(); for (String name : names) { // all names start with '/' if (name.substring(1).matches(pattern)) { matched.add(container); } } } return new DockerContainersUtil(matched); }
@Override public boolean isRunningOnRemote() { // TODO should we create an adapter class so we don't expose client classes in this part? List<com.github.dockerjava.api.model.Container> runningContainers = executor.listRunningContainers(); for (com.github.dockerjava.api.model.Container container : runningContainers) { for (String name : container.getNames()) { if (name.startsWith("/")) { name = name.substring(1); // Names array adds an slash to the docker name container. } if (name.equals(getId())) { // cube id is the container name in docker0 Id in docker is the hash // that identifies it. return true; } } } return false; }
private boolean isCubeRunning(String cube) { //TODO should we create an adapter class so we don't expose client classes in this part? List<com.github.dockerjava.api.model.Container> runningContainers = dockerClientExecutor.get().listRunningContainers(); for (com.github.dockerjava.api.model.Container container : runningContainers) { for (String name : container.getNames()) { if (name.startsWith("/")) { name = name.substring(1); //Names array adds an slash to the docker name container. } if (name.equals( cube)) { //cube id is the container name in docker0 Id in docker is the hash that identifies it. return true; } } } return false; }
public DockerService(Container container, String host) { this.host = host; containerId = container.getId(); containerNames = Arrays.stream(container.getNames()).collect(Collectors.toList()); if (!containerNames.isEmpty()) { name = containerNames.get(0); } else { name = containerId; } for (ContainerPort port : container.getPorts()) { Record record = createRecord(container, port); if (record != null) { records.add(record); } } }
@Override public boolean isRunningOnRemote() { // TODO should we create an adapter class so we don't expose client classes in this part? List<com.github.dockerjava.api.model.Container> runningContainers = executor.listRunningContainers(); for (com.github.dockerjava.api.model.Container container : runningContainers) { for (String name : container.getNames()) { if (name.startsWith("/")) { name = name.substring(1); // Names array adds an slash to the docker name container. } if (name.equals(getId())) { // cube id is the container name in docker0 Id in docker is the hash // that identifies it. return true; } } } return false; }
private boolean isCubeRunning(String cube) { //TODO should we create an adapter class so we don't expose client classes in this part? List<com.github.dockerjava.api.model.Container> runningContainers = dockerClientExecutor.get().listRunningContainers(); for (com.github.dockerjava.api.model.Container container : runningContainers) { for (String name : container.getNames()) { if (name.startsWith("/")) { name = name.substring(1); //Names array adds an slash to the docker name container. } if (name.equals( cube)) { //cube id is the container name in docker0 Id in docker is the hash that identifies it. return true; } } } return false; }
public boolean isServiceReady() { try { List<Container> allContainers = dockerClient.listContainersCmd().withShowAll(true).exec(); for (Container container : allContainers) { if (asList(container.getNames()).contains("/" + otherContainerName) && !container.getStatus().contains("Exited")) { return true; } } } catch (DockerException e) { return false; } return false; }
private List<Container> findContainers(Id id, boolean allContainers) { final List<Container> matchingContainers = new ArrayList<>(); for (Container container : docker.listContainersCmd().withShowAll(allContainers).exec()) { boolean imageNameMatches = container.getImage().equals(repo.imageName(id)); String[] containerNames = container.getNames(); if (containerNames == null) { // Every container should have a name, but this is not the case // on Circle CI. Containers with no name are broken residues of // building the image and therefore will be ignored. continue; } boolean containerNameMatches = asList(containerNames).contains(containerName(id)); if (imageNameMatches || containerNameMatches) { matchingContainers.add(container); } } return matchingContainers; }
private Link[] links(Id id) { final List<com.alexecollins.docker.orchestration.model.Link> links = conf(id).getLinks(); final Link[] out = new Link[links.size()]; final Set<String> seenAliases = Sets.newHashSet(); for (int i = 0; i < links.size(); i++) { com.alexecollins.docker.orchestration.model.Link link = links.get(i); Container container = findContainer(link.getId()); if (container == null) { throw new OrchestrationException(String.format("Could not find container for link %s", link.getId())); } final String name = com.alexecollins.docker.orchestration.util.Links.name(container.getNames()); final String alias = link.getAlias(); if (seenAliases.contains(alias)) { throw new OrchestrationException(String.format("Alias %s already used for a link with container %s", alias, id)); } seenAliases.add(alias); out[i] = new Link(name, alias); } return out; }
private void stop(final Id id) { if (id == null) { throw new IllegalArgumentException("id is null"); } logger.info("Stopping " + id); for (Container container : findRunningContainers(id)) { logger.info("Stopping container " + Arrays.toString(container.getNames())); try { docker.stopContainerCmd(container.getId()).withTimeout(1).exec(); } catch (DockerException e) { throw new OrchestrationException(e); } } for (Plugin plugin : plugins) { plugin.stopped(id, conf(id)); } }
String name = ContainerName.getFromDockerNames(container.getNames()); if (ContainerName.belongsToCluster(name, clusterId)) {
@Test public void shouldStartAContainerInStartOrConnectModeAndStopIt() { Map<String, String> cubeData = new HashMap<String, String>(); cubeData.put("connectionMode", ConnectionMode.STARTORCONNECT.name()); Map<String, String> dockerData = new HashMap<String, String>(); dockerData.put("autoStartContainers", "a,b"); dockerData.put("dockerContainers", "a:\n image: a\nb:\n image: a\n"); CubeConfiguration cubeConfiguration = CubeConfiguration.fromMap(cubeData); bind(ApplicationScoped.class, CubeConfiguration.class, cubeConfiguration); CubeDockerConfiguration dockerConfiguration = CubeDockerConfiguration.fromMap(dockerData, null); bind(ApplicationScoped.class, CubeDockerConfiguration.class, dockerConfiguration); Container container = mock(Container.class); when(container.getNames()).thenReturn(new String[] {"alreadyrun"}); when(executor.listRunningContainers()).thenReturn(Arrays.asList(container)); bind(ApplicationScoped.class, DockerClientExecutor.class, executor); fire(new BeforeSuite()); assertEventFired(CreateCube.class, 2); assertEventFired(StartCube.class, 2); assertEventFired(PreRunningCube.class, 0); assertEventFiredOnOtherThread(CreateCube.class); assertEventFiredOnOtherThread(StartCube.class); }
@Test public void shouldUsePreRunningContainers() { Map<String, String> cubeData = new HashMap<String, String>(); cubeData.put("connectionMode", ConnectionMode.STARTORCONNECT.name()); Map<String, String> dockerData = new HashMap<String, String>(); dockerData.put("autoStartContainers", "a,b"); dockerData.put("dockerContainers", "a:\n image: a\nb:\n image: a\n"); CubeConfiguration cubeConfiguration = CubeConfiguration.fromMap(cubeData); bind(ApplicationScoped.class, CubeConfiguration.class, cubeConfiguration); CubeDockerConfiguration dockerConfiguration = CubeDockerConfiguration.fromMap(dockerData, null); bind(ApplicationScoped.class, CubeDockerConfiguration.class, dockerConfiguration); Container container = mock(Container.class); when(container.getNames()).thenReturn(new String[] {"a"}); when(executor.listRunningContainers()).thenReturn(Arrays.asList(container)); bind(ApplicationScoped.class, DockerClientExecutor.class, executor); fire(new BeforeSuite()); assertEventFired(CreateCube.class, 1); assertEventFired(StartCube.class, 1); assertEventFired(PreRunningCube.class, 1); assertEventFiredOnOtherThread(CreateCube.class); assertEventFiredOnOtherThread(StartCube.class); assertEventFiredOnOtherThread(PreRunningCube.class); }
@Test public void shouldStartAContainerInStartOrConnectAndLeaveModeAndNotStopIt() { Map<String, String> cubeData = new HashMap<String, String>(); cubeData.put("connectionMode", ConnectionMode.STARTORCONNECTANDLEAVE.name()); Map<String, String> dockerData = new HashMap<String, String>(); dockerData.put("autoStartContainers", "a,b"); dockerData.put("dockerContainers", "a:\n image: a\nb:\n image: a\n"); CubeConfiguration cubeConfiguration = CubeConfiguration.fromMap(cubeData); bind(ApplicationScoped.class, CubeConfiguration.class, cubeConfiguration); CubeDockerConfiguration dockerConfiguration = CubeDockerConfiguration.fromMap(dockerData, null); bind(ApplicationScoped.class, CubeDockerConfiguration.class, dockerConfiguration); Container container = mock(Container.class); when(container.getNames()).thenReturn(new String[] {"alreadyrun"}); when(executor.listRunningContainers()).thenReturn(Arrays.asList(container)); bind(ApplicationScoped.class, DockerClientExecutor.class, executor); fire(new BeforeSuite()); assertEventFired(CreateCube.class, 2); assertEventFired(StartCube.class, 2); assertEventFired(PreRunningCube.class, 2); assertEventFiredOnOtherThread(CreateCube.class); assertEventFiredOnOtherThread(StartCube.class); assertEventFiredOnOtherThread(PreRunningCube.class); } }