private synchronized String getZaleniumNetwork(String zaleniumContainerName) { if (zaleniumNetwork != null) { return zaleniumNetwork; } String zaleniumContainerId = getContainerId(zaleniumContainerName); if (zaleniumContainerId == null) { logger.warn(String.format("Couldn't find selenium container with name or containing: %s, check that the env " + "variable ZALENIUM_CONTAINER_NAME has an appropriate value", zaleniumContainerName)); } try { ContainerInfo containerInfo = dockerClient.inspectContainer(zaleniumContainerId); ImmutableMap<String, AttachedNetwork> networks = containerInfo.networkSettings().networks(); for (Map.Entry<String, AttachedNetwork> networkEntry : networks.entrySet()) { if (!DEFAULT_DOCKER_NETWORK_NAME.equalsIgnoreCase(networkEntry.getKey())) { zaleniumNetwork = networkEntry.getKey(); return zaleniumNetwork; } } } catch (DockerException | InterruptedException | NullPointerException e) { logger.debug(nodeId + " Error while getting Zalenium network. Falling back to default.", e); ga.trackException(e); } zaleniumNetwork = DEFAULT_DOCKER_NETWORK_MODE; return zaleniumNetwork; }
@Override public String getContainerIp(String containerName) { String containerId = this.getContainerId(containerName); if (containerId == null) { return null; } try { ContainerInfo containerInfo = dockerClient.inspectContainer(containerId); if (containerInfo.networkSettings().ipAddress().trim().isEmpty()) { ImmutableMap<String, AttachedNetwork> networks = containerInfo.networkSettings().networks(); return networks.entrySet().stream().findFirst().get().getValue().ipAddress(); } return containerInfo.networkSettings().ipAddress(); } catch (DockerException | InterruptedException e) { logger.debug(nodeId + " Error while getting the container IP.", e); ga.trackException(e); } return null; }
return networkSettings.networks().values().stream() .anyMatch(network -> Objects.equals(network.ipAddress(), remoteUrl.getHost())); })
@Override public boolean equals(Object o) { if (o == this) { return true; } if (o instanceof NetworkSettings) { NetworkSettings that = (NetworkSettings) o; return ((this.ipAddress == null) ? (that.ipAddress() == null) : this.ipAddress.equals(that.ipAddress())) && ((this.ipPrefixLen == null) ? (that.ipPrefixLen() == null) : this.ipPrefixLen.equals(that.ipPrefixLen())) && ((this.gateway == null) ? (that.gateway() == null) : this.gateway.equals(that.gateway())) && ((this.bridge == null) ? (that.bridge() == null) : this.bridge.equals(that.bridge())) && ((this.portMapping == null) ? (that.portMapping() == null) : this.portMapping.equals(that.portMapping())) && ((this.ports == null) ? (that.ports() == null) : this.ports.equals(that.ports())) && ((this.macAddress == null) ? (that.macAddress() == null) : this.macAddress.equals(that.macAddress())) && ((this.networks == null) ? (that.networks() == null) : this.networks.equals(that.networks())) && ((this.endpointId == null) ? (that.endpointId() == null) : this.endpointId.equals(that.endpointId())) && ((this.sandboxId == null) ? (that.sandboxId() == null) : this.sandboxId.equals(that.sandboxId())) && ((this.sandboxKey == null) ? (that.sandboxKey() == null) : this.sandboxKey.equals(that.sandboxKey())) && ((this.hairpinMode == null) ? (that.hairpinMode() == null) : this.hairpinMode.equals(that.hairpinMode())) && ((this.linkLocalIPv6Address == null) ? (that.linkLocalIPv6Address() == null) : this.linkLocalIPv6Address.equals(that.linkLocalIPv6Address())) && ((this.linkLocalIPv6PrefixLen == null) ? (that.linkLocalIPv6PrefixLen() == null) : this.linkLocalIPv6PrefixLen.equals(that.linkLocalIPv6PrefixLen())) && ((this.globalIPv6Address == null) ? (that.globalIPv6Address() == null) : this.globalIPv6Address.equals(that.globalIPv6Address())) && ((this.globalIPv6PrefixLen == null) ? (that.globalIPv6PrefixLen() == null) : this.globalIPv6PrefixLen.equals(that.globalIPv6PrefixLen())) && ((this.ipv6Gateway == null) ? (that.ipv6Gateway() == null) : this.ipv6Gateway.equals(that.ipv6Gateway())); } return false; }
Builder(NetworkSettings source) { this.ipAddress = source.ipAddress(); this.ipPrefixLen = source.ipPrefixLen(); this.gateway = source.gateway(); this.bridge = source.bridge(); this.portMapping = source.portMapping(); this.ports = source.ports(); this.macAddress = source.macAddress(); this.networks = source.networks(); this.endpointId = source.endpointId(); this.sandboxId = source.sandboxId(); this.sandboxKey = source.sandboxKey(); this.hairpinMode = source.hairpinMode(); this.linkLocalIPv6Address = source.linkLocalIPv6Address(); this.linkLocalIPv6PrefixLen = source.linkLocalIPv6PrefixLen(); this.globalIPv6Address = source.globalIPv6Address(); this.globalIPv6PrefixLen = source.globalIPv6PrefixLen(); this.ipv6Gateway = source.ipv6Gateway(); } @Override
public String getHost(String containerId, String network) throws DockerException, InterruptedException { return IS_OS_LINUX ? dockerClient.inspectContainer(containerId).networkSettings() .networks().get(network).gateway() : dockerClient.getHost(); }
@Override public boolean equals(Object o) { if (o == this) { return true; } if (o instanceof NetworkSettings) { NetworkSettings that = (NetworkSettings) o; return ((this.ipAddress == null) ? (that.ipAddress() == null) : this.ipAddress.equals(that.ipAddress())) && ((this.ipPrefixLen == null) ? (that.ipPrefixLen() == null) : this.ipPrefixLen.equals(that.ipPrefixLen())) && ((this.gateway == null) ? (that.gateway() == null) : this.gateway.equals(that.gateway())) && ((this.bridge == null) ? (that.bridge() == null) : this.bridge.equals(that.bridge())) && ((this.portMapping == null) ? (that.portMapping() == null) : this.portMapping.equals(that.portMapping())) && ((this.ports == null) ? (that.ports() == null) : this.ports.equals(that.ports())) && ((this.macAddress == null) ? (that.macAddress() == null) : this.macAddress.equals(that.macAddress())) && ((this.networks == null) ? (that.networks() == null) : this.networks.equals(that.networks())) && ((this.endpointId == null) ? (that.endpointId() == null) : this.endpointId.equals(that.endpointId())) && ((this.sandboxId == null) ? (that.sandboxId() == null) : this.sandboxId.equals(that.sandboxId())) && ((this.sandboxKey == null) ? (that.sandboxKey() == null) : this.sandboxKey.equals(that.sandboxKey())) && ((this.hairpinMode == null) ? (that.hairpinMode() == null) : this.hairpinMode.equals(that.hairpinMode())) && ((this.linkLocalIPv6Address == null) ? (that.linkLocalIPv6Address() == null) : this.linkLocalIPv6Address.equals(that.linkLocalIPv6Address())) && ((this.linkLocalIPv6PrefixLen == null) ? (that.linkLocalIPv6PrefixLen() == null) : this.linkLocalIPv6PrefixLen.equals(that.linkLocalIPv6PrefixLen())) && ((this.globalIPv6Address == null) ? (that.globalIPv6Address() == null) : this.globalIPv6Address.equals(that.globalIPv6Address())) && ((this.globalIPv6PrefixLen == null) ? (that.globalIPv6PrefixLen() == null) : this.globalIPv6PrefixLen.equals(that.globalIPv6PrefixLen())) && ((this.ipv6Gateway == null) ? (that.ipv6Gateway() == null) : this.ipv6Gateway.equals(that.ipv6Gateway())); } return false; }
try { ContainerInfo server = docker.inspectContainer(neighbourServer.getName()); String neighbourIp = server.networkSettings().networks().values().asList().get(0).ipAddress();
/** * Deploy and start docker container, expose ports, connect to a network * * @return docker container id */ private String deployContainer() { ContainerConfig containerConfig = buildContainerConfig(); String id; try { ContainerCreation container = docker.createContainer(containerConfig, params.getName()); id = container.id(); dockerManager.addShutdownHook(params.getName()); docker.disconnectFromNetwork(id, "bridge"); docker.connectToNetwork(id, docker.inspectNetwork(universeParams.getNetworkName()).id()); docker.startContainer(id); String ipAddr = docker.inspectContainer(id) .networkSettings().networks() .values().asList().get(0) .ipAddress(); if (StringUtils.isEmpty(ipAddr)) { throw new NodeException("Empty Ip address for container: " + params.getName()); } ipAddress.set(ipAddr); } catch (InterruptedException | DockerException e) { throw new NodeException("Can't start a container", e); } return id; }
Builder(NetworkSettings source) { this.ipAddress = source.ipAddress(); this.ipPrefixLen = source.ipPrefixLen(); this.gateway = source.gateway(); this.bridge = source.bridge(); this.portMapping = source.portMapping(); this.ports = source.ports(); this.macAddress = source.macAddress(); this.networks = source.networks(); this.endpointId = source.endpointId(); this.sandboxId = source.sandboxId(); this.sandboxKey = source.sandboxKey(); this.hairpinMode = source.hairpinMode(); this.linkLocalIPv6Address = source.linkLocalIPv6Address(); this.linkLocalIPv6PrefixLen = source.linkLocalIPv6PrefixLen(); this.globalIPv6Address = source.globalIPv6Address(); this.globalIPv6PrefixLen = source.globalIPv6PrefixLen(); this.ipv6Gateway = source.ipv6Gateway(); } @Override