Skip to content

Commit

Permalink
Fixes rabbitmq config in server (#1183)
Browse files Browse the repository at this point in the history
* Fixes rabbitmq config in server

* Renamed RABBITMQ_ by RABBIT_
Variables needs to be prefixed with service name in docker-compose

* Updates utils to find published port
Ensures environ variable exist before replacing them

* Fixes auth of rabbit
  • Loading branch information
pcrespov authored Nov 25, 2019
1 parent 771da8e commit b09308d
Show file tree
Hide file tree
Showing 11 changed files with 73 additions and 49 deletions.
9 changes: 4 additions & 5 deletions .env-devel
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,12 @@ POSTGRES_PASSWORD=adminadmin
POSTGRES_PORT=5432
POSTGRES_USER=scu

RABBITMQ_USER=admin
RABBITMQ_LOG_CHANNEL=comp.backend.channels.log
RABBITMQ_PASSWORD=adminadmin
RABBITMQ_PROGRESS_CHANNEL=comp.backend.channels.progress

RABBIT_HOST=rabbit
RABBIT_LOG_CHANNEL=comp.backend.channels.log
RABBIT_PASSWORD=adminadmin
RABBIT_PORT=5672
RABBIT_PROGRESS_CHANNEL=comp.backend.channels.progress
RABBIT_USER=admin

REGISTRY_AUTH=True
REGISTRY_PW=adminadmin
Expand Down
28 changes: 14 additions & 14 deletions packages/simcore-sdk/src/simcore_sdk/config/rabbit.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@


# TODO: adapt all data below!
# TODO: can use venv as defaults? e.g. $RABBITMQ_LOG_CHANNEL
# TODO: can use venv as defaults? e.g. $RABBIT_LOG_CHANNEL
CONFIG_SCHEMA = T.Dict({
T.Key("name", default="tasks", optional=True): T.String(),
T.Key("enabled", default=True, optional=True): T.Bool(),
Expand Down Expand Up @@ -75,36 +75,36 @@ def __init__(self, config=None):
else:
config = {}

RABBITMQ_USER = env.get('RABBITMQ_USER','simcore')
RABBITMQ_PASSWORD = env.get('RABBITMQ_PASSWORD','simcore')
RABBITMQ_HOST=env.get('RABBITMQ_HOST','rabbit')
RABBITMQ_PORT=int(env.get('RABBITMQ_PORT', 5672))
RABBITMQ_LOG_CHANNEL = env.get('RABBITMQ_LOG_CHANNEL','comp.backend.channels.log')
RABBITMQ_PROGRESS_CHANNEL = env.get('RABBITMQ_PROGRESS_CHANNEL','comp.backend.channels.progress')
RABBIT_USER = env.get('RABBIT_USER','simcore')
RABBIT_PASSWORD = env.get('RABBIT_PASSWORD','simcore')
RABBIT_HOST=env.get('RABBIT_HOST','rabbit')
RABBIT_PORT=int(env.get('RABBIT_PORT', 5672))
RABBIT_LOG_CHANNEL = env.get('RABBIT_LOG_CHANNEL','comp.backend.channels.log')
RABBIT_PROGRESS_CHANNEL = env.get('RABBIT_PROGRESS_CHANNEL','comp.backend.channels.progress')
CELERY_RESULT_BACKEND=env.get('CELERY_RESULT_BACKEND','rpc://')
# FIXME: get variables via config.get('') or
# rabbit

try:
self._broker_url = eval_broker(config)
except: # pylint: disable=W0702
self._broker_url = 'amqp://{user}:{pw}@{url}:{port}'.format(user=RABBITMQ_USER, pw=RABBITMQ_PASSWORD, url=RABBITMQ_HOST, port=RABBITMQ_PORT)
self._broker_url = 'amqp://{user}:{pw}@{url}:{port}'.format(user=RABBIT_USER, pw=RABBIT_PASSWORD, url=RABBIT_HOST, port=RABBIT_PORT)

self._result_backend = config.get("celery", {}).get("result_backend") or CELERY_RESULT_BACKEND
self._module_name = config.get("name") or "tasks"

# pika
self._pika_credentials = pika.PlainCredentials(
config.get("user") or RABBITMQ_USER,
config.get("password") or RABBITMQ_PASSWORD)
config.get("user") or RABBIT_USER,
config.get("password") or RABBIT_PASSWORD)
self._pika_parameters = pika.ConnectionParameters(
host=config.get("host") or RABBITMQ_HOST,
port=config.get("port") or RABBITMQ_PORT,
host=config.get("host") or RABBIT_HOST,
port=config.get("port") or RABBIT_PORT,
credentials=self._pika_credentials,
connection_attempts=100)

self._log_channel = config.get("celery", {}).get("result_backend") or RABBITMQ_LOG_CHANNEL
self._progress_channel = config.get("celery", {}).get("result_backend") or RABBITMQ_PROGRESS_CHANNEL
self._log_channel = config.get("celery", {}).get("result_backend") or RABBIT_LOG_CHANNEL
self._progress_channel = config.get("celery", {}).get("result_backend") or RABBIT_PROGRESS_CHANNEL

@property
def parameters(self):
Expand Down
12 changes: 6 additions & 6 deletions services/docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -87,10 +87,10 @@ services:
- log:/home/scu/log
- /var/run/docker.sock:/var/run/docker.sock
environment:
- RABBITMQ_USER=${RABBITMQ_USER}
- RABBITMQ_PASSWORD=${RABBITMQ_PASSWORD}
- RABBITMQ_LOG_CHANNEL=${RABBITMQ_LOG_CHANNEL}
- RABBITMQ_PROGRESS_CHANNEL=${RABBITMQ_PROGRESS_CHANNEL}
- RABBIT_USER=${RABBIT_USER}
- RABBIT_PASSWORD=${RABBIT_PASSWORD}
- RABBIT_LOG_CHANNEL=${RABBIT_LOG_CHANNEL}
- RABBIT_PROGRESS_CHANNEL=${RABBIT_PROGRESS_CHANNEL}
- POSTGRES_ENDPOINT=${POSTGRES_ENDPOINT}
- POSTGRES_USER=${POSTGRES_USER}
- POSTGRES_PASSWORD=${POSTGRES_PASSWORD}
Expand Down Expand Up @@ -149,8 +149,8 @@ services:
image: itisfoundation/rabbitmq:3.8.0-management
init: true
environment:
- RABBITMQ_DEFAULT_USER=${RABBITMQ_USER}
- RABBITMQ_DEFAULT_PASS=${RABBITMQ_PASSWORD}
- RABBITMQ_DEFAULT_USER=${RABBIT_USER}
- RABBITMQ_DEFAULT_PASS=${RABBIT_PASSWORD}
networks:
- default
- computational_services_subnet
Expand Down
8 changes: 4 additions & 4 deletions services/sidecar/tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,8 +43,8 @@ def docker_compose_file(here):
os.environ['POSTGRES_ENDPOINT']="FOO" # TODO: update config schema!!
os.environ['MINIO_ACCESS_KEY']=ACCESS_KEY
os.environ['MINIO_SECRET_KEY']=SECRET_KEY
os.environ['RABBITMQ_USER']=RABBIT_USER
os.environ['RABBITMQ_PASSWORD']=RABBIT_PWD
os.environ['RABBIT_USER']=RABBIT_USER
os.environ['RABBIT_PASSWORD']=RABBIT_PWD


dc_path = here / 'docker-compose.yml'
Expand Down Expand Up @@ -85,8 +85,8 @@ def postgres_service(docker_services, docker_ip):
@pytest.fixture(scope='session')
def rabbit_service(docker_services, docker_ip):
# set env var here that is explicitly used from sidecar
os.environ['RABBITMQ_HOST'] = "{host}".format(host=docker_ip)
os.environ['RABBITMQ_PORT'] = "{port}".format(port=docker_services.port_for('rabbit', 15672))
os.environ['RABBIT_HOST'] = "{host}".format(host=docker_ip)
os.environ['RABBIT_PORT'] = "{port}".format(port=docker_services.port_for('rabbit', 15672))

rabbit_service = "dummy"
return rabbit_service
Expand Down
4 changes: 2 additions & 2 deletions services/sidecar/tests/docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ services:
rabbit:
image: rabbitmq:3-management
environment:
- RABBITMQ_DEFAULT_USER=${RABBITMQ_USER:-rabbit}
- RABBITMQ_DEFAULT_PASS=${RABBITMQ_PASSWORD:-carrot}
- RABBIT_DEFAULT_USER=${RABBIT_USER:-rabbit}
- RABBIT_DEFAULT_PASS=${RABBIT_PASSWORD:-carrot}
ports:
- "15672:15672"
Original file line number Diff line number Diff line change
Expand Up @@ -23,11 +23,14 @@ db:
port: 5432
user: simcore
rabbit:
enabled: True
host: rabbit
port: 5672
user: simcore
password: simcore
channels:
log: comp.backend.channels.log
progress: comp.backend.channels.progress
password: simcore
user: simcore
# s3:
# access_key: 'Q3AM3UQ867SPQQA43P2F'
# bucket_name: simcore
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,11 +25,11 @@ db:
rabbit:
host: ${RABBIT_HOST}
port: ${RABBIT_PORT}
user: ${RABBITMQ_USER}
password: ${RABBITMQ_PASSWORD}
user: ${RABBIT_USER}
password: ${RABBIT_PASSWORD}
channels:
progress: ${RABBITMQ_PROGRESS_CHANNEL}
log: ${RABBITMQ_LOG_CHANNEL}
progress: ${RABBIT_PROGRESS_CHANNEL}
log: ${RABBIT_LOG_CHANNEL}
# s3:
# endpoint: ${S3_ENDPOINT}
# access_key: ${S3_ACCESS_KEY}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,11 +23,13 @@ db:
host: ${POSTGRES_HOST}
port: ${POSTGRES_PORT}
rabbit:
user: ${RABBITMQ_USER}
password: ${RABBITMQ_PASSWORD}
host: ${RABBIT_HOST}
port: ${RABBIT_PORT}
user: ${RABBIT_USER}
password: ${RABBIT_PASSWORD}
channels:
progress: ${RABBITMQ_PROGRESS_CHANNEL}
log: ${RABBITMQ_LOG_CHANNEL}
progress: ${RABBIT_PROGRESS_CHANNEL}
log: ${RABBIT_LOG_CHANNEL}
# s3:
# endpoint: ${S3_ENDPOINT}
# access_key: ${S3_ACCESS_KEY}
Expand Down
22 changes: 18 additions & 4 deletions services/web/server/tests/helpers/utils_docker.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
wait=wait_fixed(2),
stop=stop_after_attempt(10),
after=after_log(log, logging.WARN))
def get_service_published_port(service_name: str) -> str:
def get_service_published_port(service_name: str, target_port: Optional[int]=None) -> str:
"""
WARNING: ENSURE that service name exposes a port in Dockerfile file or docker-compose config file
"""
Expand All @@ -31,10 +31,24 @@ def get_service_published_port(service_name: str) -> str:
if not service_ports:
raise RuntimeError(f"Cannot find published port for service '{service_name}' in endpoint. Probably services still not started.")

if len(service_ports)>1:
log.warning("Multiple porst published in service '%s'. Defaulting to first from %s", service_name, service_ports)
published_port = None
msg = ", ".join( f"{p.get('TargetPort')} -> {p.get('PublishedPort')}" for p in service_ports )

if target_port is None:
if len(service_ports)>1:
log.warning("Multiple ports published in service '%s': %s. Defaulting to first", service_name, msg)
published_port = service_ports[0]["PublishedPort"]

else:
target_port = int(target_port)
for p in service_ports:
if p['TargetPort'] == target_port:
published_port = p['PublishedPort']
break

if published_port is None:
raise RuntimeError(f"Cannot find published port for {target_port}. Got {msg}")

published_port = service_ports[0]["PublishedPort"]
return str(published_port)


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@
]

ops_services = [
'minio'
'minio',
# 'adminer',
# 'portainer'
]
Expand Down
12 changes: 9 additions & 3 deletions services/web/server/tests/integration/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -74,12 +74,18 @@ def webserver_environ(request, docker_stack: Dict, simcore_docker_compose: Dict)
if 'ports' in simcore_docker_compose['services'][name] ]

for name in services_with_published_ports:

host_key = f'{name.upper()}_HOST'
port_key = f'{name.upper()}_PORT'

# published port is sometimes dynamically defined by the swarm
published_port = get_service_published_port(name)
assert host_key in environ, "Variables names expected to be prefix with service names in docker-compose"
assert port_key in environ

environ['%s_HOST' % name.upper()] = '127.0.0.1'
environ['%s_PORT' % name.upper()] = published_port
# to swarm boundary since webserver is installed in the host and therefore outside the swarm's network
published_port = get_service_published_port(name, int(environ.get(port_key)))
environ[host_key] = '127.0.0.1'
environ[port_key] = published_port

pprint(environ) # NOTE: displayed only if error
return environ
Expand Down

0 comments on commit b09308d

Please sign in to comment.