From 3baafbb739b6c6be9ef105d2be8d9fd22859cbd8 Mon Sep 17 00:00:00 2001 From: Fredrik Wrede Date: Tue, 27 Feb 2024 17:55:23 +0100 Subject: [PATCH] fix and add more config envs --- config/reducer-settings.override.yaml | 2 +- ...late => settings-controller.yaml.template} | 0 docker-compose.yaml | 37 +++-- fedn/cli/run_cmd.py | 1 + fedn/fedn/common/config.py | 132 ++++++++++++------ 5 files changed, 113 insertions(+), 59 deletions(-) rename config/{settings-reducer.yaml.template => settings-controller.yaml.template} (100%) diff --git a/config/reducer-settings.override.yaml b/config/reducer-settings.override.yaml index af5ee5126..1ebfed445 100644 --- a/config/reducer-settings.override.yaml +++ b/config/reducer-settings.override.yaml @@ -6,4 +6,4 @@ services: reducer: volumes: - ${HOST_REPO_DIR:-.}/fedn:/app/fedn - - ${HOST_REPO_DIR:-.}/config/settings-reducer.yaml:/app/config/settings-reducer.yaml + - ${HOST_REPO_DIR:-.}/config/settings-controller.yaml:/app/config/settings-controller.yaml diff --git a/config/settings-reducer.yaml.template b/config/settings-controller.yaml.template similarity index 100% rename from config/settings-reducer.yaml.template rename to config/settings-controller.yaml.template diff --git a/docker-compose.yaml b/docker-compose.yaml index 85c27c6a3..c24594a99 100644 --- a/docker-compose.yaml +++ b/docker-compose.yaml @@ -64,23 +64,28 @@ services: - USER=test - PROJECT=project - FLASK_DEBUG=1 + # Controller + - FEDN_CONTROLLER_HOST=api-server + - FEDN_CONTROLLER_PORT=8092 + - FEDN_CONTROLLER_DEBUG=True + - FEDN_CONTROLLER_CONFIG=/app/config/settings-controller.yaml # Statestore - - STATESTORE_HOST=mongo - - STATESTORE_PORT=6534 - - STATESTORE_USER=fedn_admin - - STATESTORE_PASSWORD=password + - FEDN_STATESTORE_HOST=mongo + - FEDN_STATESTORE_PORT=6534 + - FEDN_STATESTORE_USER=fedn_admin + - FEDN_STATESTORE_PASSWORD=password # will be overriden by env vars above - - STATESTORE_CONFIG=/app/config/settings-reducer.yaml + - FEDN_STATESTORE_CONFIG=/app/config/settings-controller.yaml # Modelstorage - - MODELSTORAGE_HOST=minio - - MODELSTORAGE_PORT=9000 - - MODELSTORAGE_ACCESS_KEY=fedn_admin - - MODELSTORAGE_SECRET_KEY=password - - MODELSTORAGE_BUCKET=fedn-models - - MODELSTORAGE_CONTEXT_BUCKET=fedn-context - - MODELSTORAGE_SECURE_MODE=False + - FEDN_MODELSTORAGE_HOST=minio + - FEDN_MODELSTORAGE_PORT=9000 + - FEDN_MODELSTORAGE_ACCESS_KEY=fedn_admin + - FEDN_MODELSTORAGE_SECRET_KEY=password + - FEDN_MODELSTORAGE_BUCKET=fedn-models + - FEDN_MODELSTORAGE_CONTEXT_BUCKET=fedn-context + - FEDN_MODELSTORAGE_SECURE_MODE=False # will be overriden by env vars above - - MODELSTORAGE_CONFIG=/app/config/settings-reducer.yaml + - FEDN_MODELSTORAGE_CONFIG=/app/config/settings-controller.yaml build: context: . args: @@ -110,9 +115,10 @@ services: working_dir: /app volumes: - ${HOST_REPO_DIR:-.}/fedn:/app/fedn + - ${HOST_REPO_DIR:-.}/config:/app/config entrypoint: [ "sh", "-c" ] command: - - "/venv/bin/pip install --no-cache-dir -e /app/fedn && /venv/bin/fedn run combiner --init config/settings-combiner.yaml" + - "/venv/bin/pip install --no-cache-dir -e /app/fedn && /venv/bin/fedn run combiner --init config/settings-combiner.yaml.template" ports: - 12080:12080 healthcheck: @@ -139,9 +145,10 @@ services: working_dir: /app volumes: - ${HOST_REPO_DIR:-.}/fedn:/app/fedn + - ${HOST_REPO_DIR:-.}/config:/app/config entrypoint: [ "sh", "-c" ] command: - - "/venv/bin/pip install --no-cache-dir -e /app/fedn && /venv/bin/fedn run client --init config/settings-client.yaml" + - "/venv/bin/pip install --no-cache-dir -e /app/fedn && /venv/bin/fedn run client --init config/settings-client.yaml.template" deploy: replicas: 0 depends_on: diff --git a/fedn/cli/run_cmd.py b/fedn/cli/run_cmd.py index 9cacc6a12..5e7564a13 100644 --- a/fedn/cli/run_cmd.py +++ b/fedn/cli/run_cmd.py @@ -170,6 +170,7 @@ def combiner_cmd(ctx, discoverhost, discoverport, token, name, host, port, fqdn, :param max_clients: :param init: """ + # TODO: Should use fedn.common.config.get_combiner_config() instead of this config = {'discover_host': discoverhost, 'discover_port': discoverport, 'token': token, 'host': host, 'port': port, 'fqdn': fqdn, 'name': name, 'secure': secure, 'verify': verify, 'max_clients': max_clients, 'init': init} diff --git a/fedn/fedn/common/config.py b/fedn/fedn/common/config.py index 7b1643692..51ec89b3c 100644 --- a/fedn/fedn/common/config.py +++ b/fedn/fedn/common/config.py @@ -39,15 +39,35 @@ def get_environment_config(): """ global STATESTORE_CONFIG global MODELSTORAGE_CONFIG - - STATESTORE_CONFIG = os.environ.get('STATESTORE_CONFIG', - '/workspaces/fedn/config/settings-reducer.yaml.template') - MODELSTORAGE_CONFIG = os.environ.get('MODELSTORAGE_CONFIG', - '/workspaces/fedn/config/settings-reducer.yaml.template') - + global COMBINER_CONFIG + global CONTROLLER_CONFIG + + STATESTORE_CONFIG = os.environ.get('FEDN_STATESTORE_CONFIG', + '/workspaces/fedn/config/settings-controller.yaml.template') + MODELSTORAGE_CONFIG = os.environ.get('FEDN_MODELSTORAGE_CONFIG', + '/workspaces/fedn/config/settings-controller.yaml.template') + COMBINER_CONFIG = os.environ.get('FEDN_COMBINER_CONFIG', + '/workspaces/fedn/config/settings-combiner.yaml.template') + CONTROLLER_CONFIG = os.environ.get('FEDN_CONTROLLER_CONFIG', + '/workspaces/fedn/config/settings-controller.yaml.template') + +def read_config_file(file): + """ Read a yaml configuration file. + + :param file: The configuration file path. + :type file: str + :return: The configuration as a dict. + :rtype: dict + """ + with open(file, 'r') as config_file: + try: + config = dict(yaml.safe_load(config_file)) + except yaml.YAMLError as e: + raise (e) + return config def get_statestore_config(file=None): - """ Get the statestore configuration from file. + """ Get the statestore configuration. :param file: The statestore configuration file (yaml) path (optional). :type file: str @@ -56,31 +76,27 @@ def get_statestore_config(file=None): """ if file is None: # check if environment variables are set - host = get_env("STATESTORE_HOST", "mongo") + host = get_env("FEDN_STATESTORE_HOST") if host is not None: return { "type": "MongoDB", "mongo_config": { "host": host, - "port": int(get_env("STATESTORE_PORT", 6534)), - "username": get_env("STATESTORE_USER", "fedn_admin"), - "password": get_env("STATESTORE_PASSWORD"), + "port": int(get_env("FEDN_STATESTORE_PORT", 6534)), + "username": get_env("FEDN_STATESTORE_USER", "fedn_admin"), + "password": get_env("FEDN_STATESTORE_PASSWORD"), } } else: # else use the default file get_environment_config() file = STATESTORE_CONFIG - with open(file, 'r') as config_file: - try: - settings = dict(yaml.safe_load(config_file)) - except yaml.YAMLError as e: - raise (e) + settings = read_config_file(file) return settings["statestore"] def get_modelstorage_config(file=None): - """ Get the model storage configuration from file. + """ Get the model storage configuration. :param file: The model storage configuration file (yaml) path (optional). :type file: str @@ -89,29 +105,25 @@ def get_modelstorage_config(file=None): """ if file is None: # check if environment variables are set - host = get_env("MODELSTORAGE_HOST", "minio") + host = get_env("FEDN_MODELSTORAGE_HOST") if host is not None: return { "storage_type": "S3", "storage_config": { "storage_hostname": host, - "storage_port": int(get_env("MODELSTORAGE_PORT", 9000)), - "storage_access_key": get_env("MODELSTORAGE_ACCESS_KEY"), - "storage_secret_key": get_env("MODELSTORAGE_SECRET_KEY"), - "storage_bucket": get_env("MODELSTORAGE_BUCKET", "fedn-models"), - "context_bucket": get_env("MODELSTORAGE_CONTEXT_BUCKET", "fedn-context"), - "storage_secure_mode": get_boolean_env("MODELSTORAGE_SECURE_MODE", False), + "storage_port": int(get_env("FEDN_MODELSTORAGE_PORT", 9000)), + "storage_access_key": get_env("FEDN_MODELSTORAGE_ACCESS_KEY"), + "storage_secret_key": get_env("FEDN_MODELSTORAGE_SECRET_KEY"), + "storage_bucket": get_env("FEDN_MODELSTORAGE_BUCKET", "fedn-models"), + "context_bucket": get_env("FEDN_MODELSTORAGE_CONTEXT_BUCKET", "fedn-context"), + "storage_secure_mode": get_boolean_env("FEDN_MODELSTORAGE_SECURE_MODE", False), } } else: - # else use the default file + # else use the config file get_environment_config() file = MODELSTORAGE_CONFIG - with open(file, 'r') as config_file: - try: - settings = dict(yaml.safe_load(config_file)) - except yaml.YAMLError as e: - raise (e) + settings = read_config_file(file) return settings["storage"] @@ -131,17 +143,14 @@ def get_network_config(file=None): else: # else use the default file get_environment_config() + # TODO: This is a temporary fix, network_id is the database name in the statestore file = STATESTORE_CONFIG - with open(file, 'r') as config_file: - try: - settings = dict(yaml.safe_load(config_file)) - except yaml.YAMLError as e: - raise (e) + settings = read_config_file(file) return settings["network_id"] def get_controller_config(file=None): - """ Get the controller configuration from file. + """ Get the controller configuration. :param file: The controller configuration file (yaml) path (optional). :type file: str @@ -149,11 +158,48 @@ def get_controller_config(file=None): :rtype: dict """ if file is None: - get_environment_config() - file = STATESTORE_CONFIG - with open(file, 'r') as config_file: - try: - settings = dict(yaml.safe_load(config_file)) - except yaml.YAMLError as e: - raise (e) + # check if environment variables are set + host = get_env("FEDN_CONTROLLER_HOST") + if host is not None: + return { + "host": host, + "port": int(get_env("FEDN_CONTROLLER_PORT", 8092)), + "debug": get_boolean_env("FEDN_CONTROLLER_DEBUG", False), + } + else: + # else use the default file + get_environment_config() + file = CONTROLLER_CONFIG + settings = read_config_file(file) return settings["controller"] + +def get_combiner_config(file=None): + """ Get the combiner configuration. + + :param file: The combiner configuration file (yaml) path (optional). + :type file: str + :return: The combiner configuration as a dict. + :rtype: dict + """ + if file is None: + # check if environment variables are set + host = get_env("FEDN_COMBINER_HOST") + if host is not None: + return { + "host": host, + "name": get_env("FEDN_COMBINER_NAME"), + "port": int(get_env("FEDN_COMBINER_PORT", 12080)), + "max_clients": int(get_env("FEDN_COMBINER_MAX_CLIENTS", 30)), + "network_id": get_network_config(), + "discovery_host": get_env("FEDN_CONTROLLER_HOST"), + "discovery_port": int(get_env("FEDN_CONTROLLER_PORT", 8092)), + "fqdn": get_env("FEDN_COMBINER_FQDN"), + "secure": get_boolean_env("FEDN_GRPC_SECURE", False), + "verify": get_boolean_env("FEDN_VERIFY_TLS", False), + } + else: + # else use the default file + get_environment_config() + file = COMBINER_CONFIG + settings = read_config_file(file) + return settings["combiner"] \ No newline at end of file