From 5538403fa7ce1b0fa1143b0683cfb0d2f1031df8 Mon Sep 17 00:00:00 2001 From: Stefano Alberto Russo <stefano.russo@gmail.com> Date: Fri, 1 May 2020 17:30:53 +0200 Subject: [PATCH] Parametrised webapp host+port and local docker registry host+port. Moved all data (registry+shared) in the data_rosetta folder. --- docker-compose.yml | 10 +++-- services/webapp/code/rosetta/base_app/api.py | 8 ++-- .../rosetta/base_app/computing_managers.py | 26 ++++++++----- .../webapp/code/rosetta/base_app/utils.py | 37 +++++++++++++++++++ 4 files changed, 63 insertions(+), 18 deletions(-) diff --git a/docker-compose.yml b/docker-compose.yml index 3389d17..f69b79f 100644 --- a/docker-compose.yml +++ b/docker-compose.yml @@ -9,7 +9,7 @@ services: - SAFEMODE=False privileged: true volumes: - - ./data/shared:/shared + - ./data_rosetta/shared:/shared # - ./data/singularity_cache:/rosetta/.singularity/cache # Not working, check permissions... ports: - "8590:8590" @@ -23,7 +23,7 @@ services: - SAFEMODE=False privileged: true volumes: - - ./data/shared:/shared + - ./data_rosetta/shared:/shared ports: - "8591:8590" - "5901:5900" @@ -33,7 +33,7 @@ services: hostname: dregistry image: "rosetta/dregistry" volumes: - - ./data/dregistry:/var/lib/registry + - ./data_rosetta/dregistry:/var/lib/registry ports: - "5000:5000" @@ -45,6 +45,10 @@ services: - SAFEMODE=False - DJANGO_LOG_LEVEL=CRITICAL - ROSETTA_LOG_LEVEL=DEBUG + #- ROSETTA_WEBAPP_HOST= + #- ROSETTA_WEBAPP_PORT=8080 + #- LOCAL_DOCKER_REGISTRY_HOST= + #- LOCAL_DOCKER_REGISTRY_PORT=5000 ports: - "8080:8080" - "8000:8590" diff --git a/services/webapp/code/rosetta/base_app/api.py b/services/webapp/code/rosetta/base_app/api.py index ff89265..2d5974e 100644 --- a/services/webapp/code/rosetta/base_app/api.py +++ b/services/webapp/code/rosetta/base_app/api.py @@ -221,11 +221,9 @@ class agent_api(PublicGETAPI): except (Task.DoesNotExist, ValidationError): return HttpResponse('Unknown task uuid "{}"'.format(task_uuid)) - import socket - hostname = socket.gethostname() - webapp_ip = socket.gethostbyname(hostname) - host_conn_string = 'http://{}:8080'.format(webapp_ip) + from.utils import get_webapp_conn_string + webapp_conn_string = get_webapp_conn_string() action = request.GET.get('action', None) @@ -275,7 +273,7 @@ while True: break logger.info(' - ports: "{},{},{}"'.format(port, port+1, port+2)) -response = urlopen("'''+host_conn_string+'''/api/v1/base/agent/?task_uuid={}&action=set_ip_port&ip={}&port={}".format(task_uuid, ip, port)) +response = urlopen("'''+webapp_conn_string+'''/api/v1/base/agent/?task_uuid={}&action=set_ip_port&ip={}&port={}".format(task_uuid, ip, port)) response_content = response.read() if response_content != 'OK': logger.error(response_content) diff --git a/services/webapp/code/rosetta/base_app/computing_managers.py b/services/webapp/code/rosetta/base_app/computing_managers.py index 2a49a13..211d846 100644 --- a/services/webapp/code/rosetta/base_app/computing_managers.py +++ b/services/webapp/code/rosetta/base_app/computing_managers.py @@ -169,6 +169,11 @@ class RemoteComputingManager(ComputingManager): else: raise NotImplementedError('Remote tasks not requiring keys are not yet supported') + # Get webapp conn string + from.utils import get_webapp_conn_string + webapp_conn_string = get_webapp_conn_string() + + # 1) Run the container on the host (non blocking) if task.container.type == 'singularity': @@ -182,12 +187,8 @@ class RemoteComputingManager(ComputingManager): else: authstring = '' - import socket - hostname = socket.gethostname() - webapp_ip = socket.gethostbyname(hostname) - run_command = 'ssh -i {} -4 -o StrictHostKeyChecking=no {}@{} '.format(user_keys.private_key_file, user, host) - run_command += '/bin/bash -c \'"wget {}:8080/api/v1/base/agent/?task_uuid={} -O /tmp/agent_{}.py &> /dev/null && export BASE_PORT=\$(python /tmp/agent_{}.py 2> /tmp/{}.log) && '.format(webapp_ip, task.uuid, task.uuid, task.uuid, task.uuid) + run_command += '/bin/bash -c \'"wget {}/api/v1/base/agent/?task_uuid={} -O /tmp/agent_{}.py &> /dev/null && export BASE_PORT=\$(python /tmp/agent_{}.py 2> /tmp/{}.log) && '.format(webapp_conn_string, task.uuid, task.uuid, task.uuid, task.uuid) run_command += 'export SINGULARITY_NOHTTPS=true && export SINGULARITYENV_BASE_PORT=\$BASE_PORT && {} '.format(authstring) run_command += 'exec nohup singularity run --pid --writable-tmpfs --containall --cleanenv ' @@ -295,6 +296,10 @@ class SlurmComputingManager(ComputingManager): else: raise NotImplementedError('Remote tasks not requiring keys are not yet supported') + # Get webapp conn string + from.utils import get_webapp_conn_string + webapp_conn_string = get_webapp_conn_string() + # Get task computing parameters and set sbatch args sbatch_args = '' if task.computing_options: @@ -314,6 +319,7 @@ class SlurmComputingManager(ComputingManager): # Set output and error files sbatch_args += ' --output=\$HOME/{}.log --error=\$HOME/{}.log '.format(task.uuid, task.uuid) + # 1) Run the container on the host (non blocking) if task.container.type == 'singularity': @@ -326,13 +332,10 @@ class SlurmComputingManager(ComputingManager): else: authstring = '' - import socket - hostname = socket.gethostname() - webapp_ip = socket.gethostbyname(hostname) run_command = 'ssh -i {} -4 -o StrictHostKeyChecking=no {}@{} '.format(user_keys.private_key_file, user, host) - run_command += '\'bash -c "echo \\"#!/bin/bash\nwget {}:8080/api/v1/base/agent/?task_uuid={} -O \$HOME/agent_{}.py &> /dev/null && export BASE_PORT=\\\\\\$(python \$HOME/agent_{}.py 2> \$HOME/{}.log) && '.format(webapp_ip, task.uuid, task.uuid, task.uuid, task.uuid) + run_command += '\'bash -c "echo \\"#!/bin/bash\nwget {}/api/v1/base/agent/?task_uuid={} -O \$HOME/agent_{}.py &> \$HOME/{}.log && export BASE_PORT=\\\\\\$(python \$HOME/agent_{}.py 2> \$HOME/{}.log) && '.format(webapp_conn_string, task.uuid, task.uuid, task.uuid, task.uuid, task.uuid) run_command += 'export SINGULARITY_NOHTTPS=true && export SINGULARITYENV_BASE_PORT=\\\\\\$BASE_PORT && {} '.format(authstring) run_command += 'exec nohup singularity run --pid --writable-tmpfs --containall --cleanenv ' @@ -340,7 +343,10 @@ class SlurmComputingManager(ComputingManager): # Set registry if task.container.registry == 'docker_local': - registry = 'docker://dregistry:5000/' + # Get local Docker registry conn string + from.utils import get_local_docker_registry_conn_string + local_docker_registry_conn_string = get_local_docker_registry_conn_string() + registry = 'docker://{}/'.format(local_docker_registry_conn_string) elif task.container.registry == 'docker_hub': registry = 'docker://' else: diff --git a/services/webapp/code/rosetta/base_app/utils.py b/services/webapp/code/rosetta/base_app/utils.py index 0ec1348..fa369d6 100644 --- a/services/webapp/code/rosetta/base_app/utils.py +++ b/services/webapp/code/rosetta/base_app/utils.py @@ -1,3 +1,4 @@ +import os import traceback import hashlib import random @@ -425,7 +426,43 @@ class dt_range(object): return self.__next__() +#================================ +# Others +#================================ + def debug_param(**kwargs): for item in kwargs: logger.critical('Param "{}": "{}"'.format(item, kwargs[item])) +def get_my_ip(): + import socket + hostname = socket.gethostname() + my_ip = socket.gethostbyname(hostname) + return my_ip + +def get_webapp_conn_string(): + webapp_host = os.environ.get('ROSETTA_WEBAPP_HOST', get_my_ip()) + webapp_port = os.environ.get('ROSETTA_WEBAPP_PORT', '8080') + webapp_conn_string = 'http://{}:{}'.format(webapp_host, webapp_port) + return webapp_conn_string + +def get_local_docker_registry_conn_string(): + local_docker_registry_host = os.environ.get('LOCAL_DOCKER_REGISTRY_HOST', 'dregistry') + local_docker_registry_port = os.environ.get('LOCAL_DOCKER_REGISTRY_PORT', '5000') + local_docker_registry_conn_string = '{}:{}'.format(local_docker_registry_host, local_docker_registry_port) + return local_docker_registry_conn_string + + + + + + + + + + + + + + + -- GitLab