Skip to content

Commit

Permalink
CHE-3686: fix docker machines addresses setting
Browse files Browse the repository at this point in the history
Set internal machine address by CLI from CHE_IP.
Add and fix docs in che.env.
Change behavior of default machine server address evaluation
to be similar to the previous state.
Code cleanup.
Signed-off-by: Alexander Garagatyi <[email protected]>
  • Loading branch information
Alexander Garagatyi committed Jan 19, 2017
1 parent 9a6118c commit f2ca9a8
Show file tree
Hide file tree
Showing 14 changed files with 450 additions and 737 deletions.
4 changes: 2 additions & 2 deletions assembly/assembly-main/src/assembly/bin/che.sh
Original file line number Diff line number Diff line change
Expand Up @@ -184,9 +184,9 @@ usage () {
set_environment_variables () {
### Set value of derived environment variables.

# CHE_DOCKER_MACHINE_HOST is used internally by Che to set its IP address
# CHE_DOCKER_IP is used internally by Che to set its IP address
if [[ -n "${CHE_IP}" ]]; then
export CHE_DOCKER_MACHINE_HOST="${CHE_IP}"
export CHE_DOCKER_IP="${CHE_IP}"
fi

#if [ "${WIN}" == "true" ] && [ ! -z "${JAVA_HOME}" ]; then
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,6 @@ public interface Server {
* External address of the server in form <b>hostname:port</b>.
* <p>
* This address is used by the browser to communicate with the server.
* <b>hostname</b> can be configured using property machine.docker.local_node_host.external
* or environment variable CHE_DOCKER_MACHINE_HOST_EXTERNAL.
* <b>port</b> is the external port and cannot be configured.
* If not explicitly configured that address is set using {@link ServerProperties#getInternalAddress()}
*/
Expand Down
4 changes: 2 additions & 2 deletions dockerfiles/che/entrypoint.sh
Original file line number Diff line number Diff line change
Expand Up @@ -79,9 +79,9 @@ usage () {
set_environment_variables () {
### Set value of derived environment variables.

# CHE_DOCKER_MACHINE_HOST is used internally by Che to set its IP address
# CHE_DOCKER_IP is used internally by Che to set its IP address
if [[ -n "${CHE_IP}" ]]; then
export CHE_DOCKER_MACHINE_HOST="${CHE_IP}"
export CHE_DOCKER_IP="${CHE_IP}"
fi

# Convert Tomcat environment variables to POSIX format.
Expand Down
24 changes: 16 additions & 8 deletions dockerfiles/init/manifests/che.env
Original file line number Diff line number Diff line change
Expand Up @@ -134,16 +134,24 @@
# Che Server --> Workspace Connection (see Workspace Address Resolution Strategy, below):
# - If CHE_DOCKER_SERVER__EVALUATION__STRATEGY is 'default':
# 1. Use the value of che.docker.ip
# 2. Else, use address of docker0 bridge network, if available
# 3. Else, use localhost
# 2. Else, if server connects over Unix socket, then use localhost
# 3. Else, use DOCKER_HOST
# - If CHE_DOCKER_SERVER__EVALUATION__STRATEGY is 'docker-local':
# 1. Use the address of the workspace container within the docker network
# 2. If address cannot be read, try localhost
# 1. Use the address of the workspace container within the docker network and exposed ports
# 2. If address is missing, if server connects over Unix socket, then use localhost and published ports
# 3. Else, use DOCKER_HOST and published ports
#
# Browser --> Workspace Connection:
# 1. Use the value of che.docker.ip
# 2. Else, if server connects over Unix socket, then use localhost
# 3. Else, use DOCKER_HOST
# Browser --> Workspace Connection (see Workspace Address Resolution Strategy, below):
# - If CHE_DOCKER_SERVER__EVALUATION__STRATEGY is 'default':
# 1. If set use the value of che.docker.ip.external
# 2. Else if set use the value of che.docker.ip
# 3. Else, if server connects over Unix socket, then use localhost
# 4. Else, use DOCKER_HOST
# - If CHE_DOCKER_SERVER__EVALUATION__STRATEGY is 'docker-local':
# 1. If set use the value of che.docker.ip.external
# 2. Else use the address of the workspace container within the docker network, if it is set
# 3. If address is missing, if server connects over Unix socket, then use localhost
# 4. Else, use DOCKER_HOST
#
# Workspace Agent --> Che Server
# 1. Default is http://che-host:${SERVER_PORT}/wsmaster/api, where che-host is IP of server.
Expand Down
4 changes: 2 additions & 2 deletions dockerfiles/launcher/launcher.sh
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ init_global_variables() {
DEFAULT_CHE_DEBUG_SERVER="false"
DEFAULT_CHE_DEBUG_SERVER_PORT="8000"
DEFAULT_CHE_DEBUG_SERVER_SUSPEND="false"
DEFAULT_CHE_DOCKER_MACHINE_HOST_EXTERNAL=$(get_docker_external_hostname)
DEFAULT_CHE_DOCKER_IP_EXTERNAL=$(get_docker_external_hostname)

# Clean eventual user provided paths
# This is a user-provided override
Expand Down Expand Up @@ -96,7 +96,7 @@ init_global_variables() {
CHE_DEBUG_SERVER=${CHE_DEBUG_SERVER:-${DEFAULT_CHE_DEBUG_SERVER}}
CHE_DEBUG_SERVER_PORT=${CHE_DEBUG_SERVER_PORT:-${DEFAULT_CHE_DEBUG_SERVER_PORT}}
CHE_DEBUG_SERVER_SUSPEND=${CHE_DEBUG_SERVER_SUSPEND:-${DEFAULT_CHE_DEBUG_SERVER_SUSPEND}}
CHE_DOCKER_MACHINE_HOST_EXTERNAL=${CHE_DOCKER_MACHINE_HOST_EXTERNAL:-${DEFAULT_CHE_DOCKER_MACHINE_HOST_EXTERNAL}}
CHE_DOCKER_IP_EXTERNAL=${CHE_DOCKER_IP_EXTERNAL:-${DEFAULT_CHE_DOCKER_IP_EXTERNAL}}

USAGE="
Usage:
Expand Down
4 changes: 2 additions & 2 deletions dockerfiles/launcher/launcher_cmds.sh
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,7 @@ print_debug_info() {
info "DOCKER_INSTALL_TYPE = ${DOCKER_INSTALL_TYPE}"
info "DOCKER_HOST_OS = $(get_docker_host_os)"
info "DOCKER_HOST_IP = ${DEFAULT_DOCKER_HOST_IP}"
info "DOCKER_HOST_EXTERNAL_IP = ${DEFAULT_CHE_DOCKER_MACHINE_HOST_EXTERNAL:-not set}"
info "DOCKER_HOST_EXTERNAL_IP = ${DEFAULT_CHE_DOCKER_IP_EXTERNAL:-not set}"
info "DOCKER_DAEMON_VERSION = $(get_docker_daemon_version)"
info ""
info ""
Expand Down Expand Up @@ -160,4 +160,4 @@ print_debug_info() {
info "CHE_HOSTNAME = ${CHE_HOSTNAME}"
info "CHE_SERVER_CONTAINER_NAME = ${CHE_SERVER_CONTAINER_NAME}"
info "CHE_SERVER_IMAGE_NAME = ${CHE_SERVER_IMAGE_NAME}"
}
}
4 changes: 2 additions & 2 deletions dockerfiles/launcher/launcher_funcs.sh
Original file line number Diff line number Diff line change
Expand Up @@ -165,7 +165,7 @@ docker_run_with_conf() {

docker_run_with_external_hostname() {
if has_external_hostname; then
docker_run_with_conf -e "CHE_DOCKER_MACHINE_HOST_EXTERNAL=${CHE_DOCKER_MACHINE_HOST_EXTERNAL}" "$@"
docker_run_with_conf -e "CHE_DOCKER_IP_EXTERNAL=${CHE_DOCKER_IP_EXTERNAL}" "$@"
else
docker_run_with_conf "$@"
fi
Expand Down Expand Up @@ -217,7 +217,7 @@ has_assembly() {
}

has_external_hostname() {
if [ "${CHE_DOCKER_MACHINE_HOST_EXTERNAL}" = "" ]; then
if [ "${CHE_DOCKER_IP_EXTERNAL}" = "" ]; then
return 1
else
return 0
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,22 +8,16 @@
* Contributors:
* Red Hat Inc. - initial API and implementation
*******************************************************************************/

package org.eclipse.che.plugin.docker.machine;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import com.google.inject.Inject;
import com.google.inject.name.Named;

import org.eclipse.che.api.machine.server.model.impl.ServerImpl;
import org.eclipse.che.commons.annotation.Nullable;
import org.eclipse.che.plugin.docker.client.json.ContainerInfo;
import org.eclipse.che.plugin.docker.client.json.PortBinding;

import com.google.inject.Inject;
import com.google.inject.name.Named;
import static com.google.common.base.Strings.isNullOrEmpty;

import java.util.Map;

/**
* Represents the default server evaluation strategy. By default, calling
Expand All @@ -34,6 +28,7 @@
* {@code che.docker.ip} and {@code che.docker.ip.external}, respectively.
*
* @author Angel Misevski <[email protected]>
* @author Alexander Garagatyi
* @see ServerEvaluationStrategy
*/
public class DefaultServerEvaluationStrategy extends ServerEvaluationStrategy {
Expand All @@ -49,48 +44,29 @@ public class DefaultServerEvaluationStrategy extends ServerEvaluationStrategy {
protected String externalAddressProperty;

@Inject
public DefaultServerEvaluationStrategy (@Nullable @Named("che.docker.ip") String internalAddress,
@Nullable @Named("che.docker.ip.external") String externalAddress) {
public DefaultServerEvaluationStrategy(@Nullable @Named("che.docker.ip") String internalAddress,
@Nullable @Named("che.docker.ip.external") String externalAddress) {
this.internalAddressProperty = internalAddress;
this.externalAddressProperty = externalAddress;
}

@Override
protected Map<String, String> getInternalAddressesAndPorts(ContainerInfo containerInfo, String internalHost) {
String internalAddressContainer = containerInfo.getNetworkSettings().getGateway();

String internalAddress = internalAddressProperty != null ?
internalAddressProperty :
!isNullOrEmpty(internalAddressContainer) ?
internalAddressContainer :
internalHost;

Map<String, List<PortBinding>> portBindings = containerInfo.getNetworkSettings().getPorts();

return getAddressesAndPorts(internalAddress, portBindings);
return getExposedPortsToAddressPorts(internalAddress, containerInfo.getNetworkSettings().getPorts());
}

@Override
protected Map<String, String> getExternalAddressesAndPorts(ContainerInfo containerInfo, String internalHost) {
String externalAddressContainer = containerInfo.getNetworkSettings().getGateway();

String externalAddress = externalAddressProperty != null ?
externalAddressProperty :
!isNullOrEmpty(externalAddressContainer) ?
externalAddressContainer :
internalAddressProperty != null ?
internalAddressProperty :
internalHost;

Map<String, List<PortBinding>> portBindings = containerInfo.getNetworkSettings().getPorts();

return getAddressesAndPorts(externalAddress, portBindings);
}

private Map<String, String> getAddressesAndPorts(String address, Map<String, List<PortBinding>> ports) {
Map<String, String> addressesAndPorts = new HashMap<>();
for (String portKey : ports.keySet()) {
String port = ports.get(portKey).get(0).getHostPort();
addressesAndPorts.put(portKey, address + ":" + port);
}
return addressesAndPorts;
return super.getExposedPortsToAddressPorts(externalAddress, containerInfo.getNetworkSettings().getPorts());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -74,9 +74,9 @@ public class DockerInstanceRuntimeInfo implements MachineRuntimeInfo {
*/
public static final String USER_TOKEN = "USER_TOKEN";

private final ContainerInfo info;
private final Map<String, ServerConfImpl> serversConf;
private final String internalHost;
private final ContainerInfo info;
private final Map<String, ServerConfImpl> serversConf;
private final String internalHost;
private final ServerEvaluationStrategyProvider provider;

@Inject
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,20 +8,20 @@
* Contributors:
* Red Hat Inc. - initial API and implementation
*******************************************************************************/

package org.eclipse.che.plugin.docker.machine;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import com.google.inject.Inject;
import com.google.inject.name.Named;

import org.eclipse.che.api.machine.server.model.impl.ServerImpl;
import org.eclipse.che.commons.annotation.Nullable;
import org.eclipse.che.plugin.docker.client.json.ContainerInfo;
import org.eclipse.che.plugin.docker.client.json.PortBinding;

import com.google.inject.Inject;
import com.google.inject.name.Named;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import static com.google.common.base.Strings.isNullOrEmpty;

/**
Expand Down Expand Up @@ -49,8 +49,8 @@ public class LocalDockerServerEvaluationStrategy extends ServerEvaluationStrateg
protected String externalAddressProperty;

@Inject
public LocalDockerServerEvaluationStrategy (@Nullable @Named("che.docker.ip") String internalAddress,
@Nullable @Named("che.docker.ip.external") String externalAddress) {
public LocalDockerServerEvaluationStrategy(@Nullable @Named("che.docker.ip") String internalAddress,
@Nullable @Named("che.docker.ip.external") String externalAddress) {
this.internalAddressProperty = internalAddress;
this.externalAddressProperty = externalAddress;
}
Expand Down Expand Up @@ -93,13 +93,6 @@ protected Map<String, String> getExternalAddressesAndPorts(ContainerInfo contain
externalAddressContainer :
internalHost;

Map<String, List<PortBinding>> portBindings = containerInfo.getNetworkSettings().getPorts();

Map<String, String> addressesAndPorts = new HashMap<>();
for (String portKey : portBindings.keySet()) {
String port = portBindings.get(portKey).get(0).getHostPort();
addressesAndPorts.put(portKey, externalAddress + ":" + port);
}
return addressesAndPorts;
return getExposedPortsToAddressPorts(externalAddress, containerInfo.getNetworkSettings().getPorts());
}
}
Loading

0 comments on commit f2ca9a8

Please sign in to comment.