Skip to content

director transmits its own extra hosts to spawned services *demo #379

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Nov 29, 2018
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions services/director/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ ENV REGISTRY_USER = ''
ENV REGISTRY_PW = ''
ENV REGISTRY_URL = ''
ENV PUBLISHED_HOST_NAME=''
ENV EXTRA_HOSTS_SUFFIX = 'undefined'

WORKDIR /home/scu

Expand Down
1 change: 1 addition & 0 deletions services/director/src/simcore_service_director/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
REGISTRY_PW = os.environ.get("REGISTRY_PW", "")
REGISTRY_URL = os.environ.get("REGISTRY_URL", "")
REGISTRY_SSL = os.environ.get("REGISTRY_SSL", True)
EXTRA_HOSTS_SUFFIX = os.environ.get("EXTRA_HOSTS_SUFFIX", "undefined")

# these are the envs passed to the dynamic services by default
SERVICES_DEFAULT_ENVS = {
Expand Down
10 changes: 10 additions & 0 deletions services/director/src/simcore_service_director/producer.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
import tenacity

from . import config, exceptions, registry_proxy
from .system_utils import get_system_extra_hosts

SERVICE_RUNTIME_SETTINGS = 'simcore.service.settings'
SERVICE_RUNTIME_BOOTSETTINGS = 'simcore.service.bootsettings'
Expand Down Expand Up @@ -144,6 +145,14 @@ def __add_env_variables_to_service_runtime_params(docker_service_runtime_paramet
docker_service_runtime_parameters["env"] = service_env_variables
log.debug("Added env parameter to docker runtime parameters: %s", docker_service_runtime_parameters["env"])

def _add_extra_hosts_to_service_runtime_params(docker_service_runtime_parameters: Dict):
log.debug("Getting extra hosts with suffix: %s",config.EXTRA_HOSTS_SUFFIX)
extra_hosts = get_system_extra_hosts(config.EXTRA_HOSTS_SUFFIX)
if "hosts" in docker_service_runtime_parameters:
docker_service_runtime_parameters["hosts"].update(extra_hosts)
else:
docker_service_runtime_parameters["hosts"] = extra_hosts

def __set_service_name(docker_service_runtime_parameters: Dict, service_name: str, node_uuid: str):
# pylint: disable=C0103
docker_service_runtime_parameters["name"] = service_name + "_" + node_uuid
Expand Down Expand Up @@ -285,6 +294,7 @@ async def __prepare_runtime_parameters(user_id: str, service_key: str, service_t
__add_to_swarm_network_if_ports_published(client, docker_service_runtime_parameters)
__add_uuid_label_to_service_runtime_params(docker_service_runtime_parameters, node_uuid)
__add_env_variables_to_service_runtime_params(docker_service_runtime_parameters, user_id, node_uuid)
_add_extra_hosts_to_service_runtime_params(docker_service_runtime_parameters)
__set_service_name(docker_service_runtime_parameters,
registry_proxy.get_service_last_names(service_key),
node_uuid)
Expand Down
15 changes: 15 additions & 0 deletions services/director/src/simcore_service_director/system_utils.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
from pathlib import Path
from typing import Dict


def get_system_extra_hosts(extra_host_domain: str) -> Dict:
extra_hosts = {}
hosts_path = Path("/etc/hosts")
if hosts_path.exists():
with hosts_path.open() as hosts:
for line in hosts:
if extra_host_domain in line:
host = line.split()[1]
ip = line.split()[0]
extra_hosts[host] = ip
return extra_hosts
13 changes: 9 additions & 4 deletions services/director/tests/test_producer.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
#pylint: disable=W0621, W0611, W0613
import uuid

import docker
import pytest
from simcore_service_director import (
producer,
exceptions
)

from simcore_service_director import config, exceptions, producer


@pytest.fixture
async def run_services(loop, configure_registry_access, push_services, docker_swarm, user_id): #pylint: disable=W0613, W0621
Expand Down Expand Up @@ -90,3 +90,8 @@ async def test_interactive_service_published_port(run_services): #pylint: disabl
service_information = low_level_client.inspect_service(docker_service.id)
service_published_port = service_information["Endpoint"]["Ports"][0]["PublishedPort"]
assert service_published_port == service_port

async def test_extra_hosts_passed_to_services(run_services):
# would need to test right inside a docker or test from outside...
# start the director with extra hosts, start some services, and test if the extra hosts are added
pass