/** * @return the port bindings */ default List<String> getPortBindings() { List<String> portBindings = new ArrayList<>(); final Ports hostPortBindings = this.getContainerInfo().getHostConfig().getPortBindings(); for (Map.Entry<ExposedPort, Ports.Binding[]> binding : hostPortBindings.getBindings().entrySet()) { for (Ports.Binding portBinding : binding.getValue()) { portBindings.add(String.format("%s:%s", portBinding.toString(), binding.getKey())); } } return portBindings; }
@Override public Ports deserialize(JsonParser jsonParser, DeserializationContext deserializationContext) throws IOException, JsonProcessingException { Ports out = new Ports(); ObjectCodec oc = jsonParser.getCodec(); JsonNode node = oc.readTree(jsonParser); for (Iterator<Map.Entry<String, JsonNode>> it = node.fields(); it.hasNext();) { Map.Entry<String, JsonNode> portNode = it.next(); JsonNode bindingsArray = portNode.getValue(); if (bindingsArray.equals(NullNode.getInstance())) { out.bind(ExposedPort.parse(portNode.getKey()), null); } else { for (int i = 0; i < bindingsArray.size(); i++) { JsonNode bindingNode = bindingsArray.get(i); if (!bindingNode.equals(NullNode.getInstance())) { String hostIp = bindingNode.get("HostIp").textValue(); String hostPort = bindingNode.get("HostPort").textValue(); out.bind(ExposedPort.parse(portNode.getKey()), new Binding(hostIp, hostPort)); } } } } return out; } }
public Ports(PortBinding... portBindings) { add(portBindings); }
public HostConfig withPortBindings(PortBinding... portBindings) { checkNotNull(portBindings, "portBindings was not specified"); withPortBindings(new Ports(portBindings)); return this; }
/** * Creates a {@link Ports} object with an initial {@link PortBinding} for the specified {@link ExposedPort} and {@link Binding}. Use * {@link #bind(ExposedPort, Binding)} or {@link #add(PortBinding...)} to add more {@link PortBinding}s. */ public Ports(ExposedPort exposedPort, Binding host) { bind(exposedPort, host); }
public HostConfig withPortBindings(PortBinding... portBindings) { checkNotNull(portBindings, "portBindings was not specified"); withPortBindings(new Ports(portBindings)); return this; }
/** * Adds the specified {@link PortBinding}(s) to the list of {@link PortBinding}s. */ public void add(PortBinding... portBindings) { for (PortBinding binding : portBindings) { bind(binding.getExposedPort(), binding.getBinding()); } }
@Override public void serialize(Ports portBindings, JsonGenerator jsonGen, SerializerProvider serProvider) throws IOException, JsonProcessingException { jsonGen.writeStartObject(); for (Entry<ExposedPort, Binding[]> entry : portBindings.getBindings().entrySet()) { jsonGen.writeFieldName(entry.getKey().toString()); if (entry.getValue() != null) { jsonGen.writeStartArray(); for (Binding binding : entry.getValue()) { jsonGen.writeStartObject(); jsonGen.writeStringField("HostIp", binding.getHostIp() == null ? "" : binding.getHostIp()); jsonGen.writeStringField("HostPort", binding.getHostPortSpec() == null ? "" : binding.getHostPortSpec()); jsonGen.writeEndObject(); } jsonGen.writeEndArray(); } else { jsonGen.writeNull(); } } jsonGen.writeEndObject(); }
@Before public void setup() { HostConfig hostConfig = new HostConfig(); hostConfig.withPortBindings(new Ports()); when(inspectContainerResponse.getHostConfig()).thenReturn(hostConfig); when(inspectContainerCmd.exec()).thenReturn(inspectContainerResponse); when(dockerClient.inspectContainerCmd(anyString())).thenReturn(inspectContainerCmd); when(executor.getDockerClient()).thenReturn(dockerClient); CubeContainer cubeContainer = new CubeContainer(); cubeContainer.setRemoveVolumes(false); cube = injectorInst.get().inject(new DockerCube(ID, cubeContainer, executor)); }
/** * Creates a {@link Ports} object with an initial {@link PortBinding} for the specified {@link ExposedPort} and {@link Binding}. Use * {@link #bind(ExposedPort, Binding)} or {@link #add(PortBinding...)} to add more {@link PortBinding}s. */ public Ports(ExposedPort exposedPort, Binding host) { bind(exposedPort, host); }
public Ports(PortBinding... portBindings) { add(portBindings); }
@Override public Ports deserialize(JsonParser jsonParser, DeserializationContext deserializationContext) throws IOException, JsonProcessingException { Ports out = new Ports(); ObjectCodec oc = jsonParser.getCodec(); JsonNode node = oc.readTree(jsonParser); for (Iterator<Map.Entry<String, JsonNode>> it = node.fields(); it.hasNext();) { Map.Entry<String, JsonNode> portNode = it.next(); JsonNode bindingsArray = portNode.getValue(); if (bindingsArray.equals(NullNode.getInstance())) { out.bind(ExposedPort.parse(portNode.getKey()), null); } else { for (int i = 0; i < bindingsArray.size(); i++) { JsonNode bindingNode = bindingsArray.get(i); if (!bindingNode.equals(NullNode.getInstance())) { String hostIp = bindingNode.get("HostIp").textValue(); String hostPort = bindingNode.get("HostPort").textValue(); out.bind(ExposedPort.parse(portNode.getKey()), new Binding(hostIp, hostPort)); } } } } return out; } }
/** * Get the actual mapped port for a given port exposed by the container. * * @param originalPort the original TCP port that is exposed * @return the port that the exposed port is mapped to, or null if it is not exposed */ default Integer getMappedPort(int originalPort) { Preconditions.checkState(this.getContainerId() != null, "Mapped port can only be obtained after the container is started"); Ports.Binding[] binding = new Ports.Binding[0]; final InspectContainerResponse containerInfo = this.getContainerInfo(); if (containerInfo != null) { binding = containerInfo.getNetworkSettings().getPorts().getBindings().get(new ExposedPort(originalPort)); } if (binding != null && binding.length > 0 && binding[0] != null) { return Integer.valueOf(binding[0].getHostPortSpec()); } else { throw new IllegalArgumentException("Requested port (" + originalPort + ") is not mapped"); } }
Ports ports = new Ports(port, Ports.Binding.bindPortRange(this.containerBindPortLow,this.containerBindPortHigh));
/** * Adds the specified {@link PortBinding}(s) to the list of {@link PortBinding}s. */ public void add(PortBinding... portBindings) { for (PortBinding binding : portBindings) { bind(binding.getExposedPort(), binding.getBinding()); } }
@Override public void beforeContainerCreated(DockerAPI api, String workdir, CreateContainerCmd cmd) throws IOException, InterruptedException { // TODO define a strategy for SSHD process configuration so we support more than openssh's sshd if (cmd.getCmd() == null || cmd.getCmd().length == 0) { if (sshKeyStrategy.getInjectedKey() != null) { cmd.withCmd("/usr/sbin/sshd", "-D", "-p", String.valueOf(port), // override sshd_config to force retrieval of InstanceIdentity public for as authentication "-o", "AuthorizedKeysCommand=/root/authorized_key", "-o", "AuthorizedKeysCommandUser=root" ); } else { cmd.withCmd("/usr/sbin/sshd", "-D", "-p", String.valueOf(port)); } } cmd.withPortSpecs(port+"/tcp"); final PortBinding sshPortBinding = PortBinding.parse(":" + port); final Ports portBindings = cmd.getPortBindings(); if(portBindings != null) { portBindings.add(sshPortBinding); cmd.withPortBindings(portBindings); } else { cmd.withPortBindings(sshPortBinding); } cmd.withExposedPorts(ExposedPort.parse(port+"/tcp")); }
private Ports toPortBindings(Collection<PortBinding> portBindings) { Ports ports = new Ports(); for (PortBinding portBinding : portBindings) { ports.bind( new ExposedPort( portBinding.getExposedPort().getExposed(), InternetProtocol.parse(portBinding.getExposedPort().getType())), new Binding(portBinding.getHost(), Integer.toString(portBinding.getBound()))); } return ports; }
public boolean hasPortBindings() { Ports ports = containerInfo.getNetworkSettings().getPorts(); return ((ports != null) && (ports.getBindings() != null) && (!ports.getBindings().isEmpty())); }