All environment/configuration variables are provided by the Environ dictionary.
Environ is a singleton class, meaning that only an instance of Environ is available.
Descriptions of global/environment variables
Researcher Global Variables:
- RESEARCHER_ID : id of the researcher
- ID : equals to researcher id
- TENSORBOARD_RESULTS_DIR : path for writing tensorboard log files
- EXPERIMENTS_DIR : folder for saving experiments
- MESSAGES_QUEUE_DIR : Path for writing queue files
Nodes Global Variables:
- NODE_ID : id of the node
- ID : equals to node id
- MESSAGES_QUEUE_DIR : Path for queues
- DB_PATH : TinyDB database path where datasets/training_plans/loading plans are saved
- DEFAULT_TRAINING_PLANS_DIR : Path of directory for storing default training plans
- TRAINING_PLANS_DIR : Path of directory for storing registered training plans
- TRAINING_PLAN_APPROVAL : True if the node enables training plan approval
- ALLOW_DEFAULT_TRAINING_PLANS : True if the node enables default training plans for training plan approval
Common Global Variables:
- COMPONENT_TYPE : Node or Researcher
- CONFIG_DIR : Configuration file path
- VAR_DIR : Var directory of Fed-BioMed
- CACHE_DIR : Cache directory of Fed-BioMed
- TMP_DIR : Temporary directory
- MQTT_BROKER : MQTT broker IP address
- MQTT_BROKER_PORT : MQTT broker port
- UPLOADS_URL : Upload URL for file repository
- MPSPDZ_IP : MP-SPDZ endpoint IP of component
- DEFAULT_BIPRIMES_DIR : Path of directory for storing default secure aggregation biprimes
- ALLOW_DEFAULT_BIPRIMES : True if the component enables the default secure aggregation biprimes
- PORT_INCREMENT_FILE : File for storing next port to be allocated for MP-SPDZ
- CERT_DIR : Directory for storing certificates for MP-SPDZ
- DEFAULT_BIPRIMES_DIR : Directory for storing default biprimes files
Attributes
Classes
Environ
Environ(root_dir=None)
Singleton class contains all variables for researcher or node
Parameters:
Name | Type | Description | Default |
---|---|---|---|
root_dir | str | if not provided the directory is deduced from the package location (specifying root_dir is mainly used by the test files) | None |
Raises:
Type | Description |
---|---|
FedbiomedEnvironError | If component type is invalid |
Source code in fedbiomed/common/environ.py
def __init__(self, root_dir: str = None):
"""Class constructor
Args:
root_dir: if not provided the directory is deduced from the package location
(specifying root_dir is mainly used by the test files)
Raises:
FedbiomedEnvironError: If component type is invalid
"""
# dict with contains all configuration values
self._values = {}
self._cfg = configparser.ConfigParser()
self._root_dir = root_dir
Functions
check_and_set_config_file_version
check_and_set_config_file_version(version_from_runtime)
Check compatibility of config file and set corresponding environment value.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
version_from_runtime | Union[str, FBM_Component_Version] | the version hardcoded in common/constants.py | required |
Source code in fedbiomed/common/environ.py
def check_and_set_config_file_version(self,
version_from_runtime: Union[str, FBM_Component_Version]):
"""Check compatibility of config file and set corresponding environment value.
Args:
version_from_runtime: the version hardcoded in common/constants.py
"""
try:
config_file_version = self.from_config('default', 'version')
except FedbiomedEnvironError:
config_file_version = fedbiomed.common.utils.__default_version__
raise_for_version_compatibility(config_file_version, version_from_runtime,
f"Configuration file {self._values['CONFIG_FILE']}: "
f"found version %s expected version %s")
self._values["CONFIG_FILE_VERSION"] = config_file_version
default_config_file abstractmethod
default_config_file()
Abstract method for retrieving default configuration file path
Source code in fedbiomed/common/environ.py
@abstractmethod
def default_config_file(self) -> str:
"""Abstract method for retrieving default configuration file path"""
from_config
from_config(section, key)
Gets values from config file Args: section: the section of the key key: the name of the key
Returns:
Type | Description |
---|---|
Any | The value of the key |
Raises:
Type | Description |
---|---|
FedbiomedEnvironError | If the key does not exist in the configuration |
Source code in fedbiomed/common/environ.py
def from_config(self, section, key) -> Any:
"""Gets values from config file
Args:
section: the section of the key
key: the name of the key
Returns:
The value of the key
Raises:
FedbiomedEnvironError: If the key does not exist in the configuration
"""
try:
_cfg_value = self._cfg.get(section, key)
except configparser.Error:
_msg = f"{ErrorNumbers.FB600.value}: no {section}/{key} in config file. Please recreate a new config file"
logger.error(_msg)
raise FedbiomedEnvironError(_msg)
return _cfg_value
info abstractmethod
info()
Abstract method to return component information
Source code in fedbiomed/common/environ.py
@abstractmethod
def info(self):
"""Abstract method to return component information"""
parse_write_config_file
parse_write_config_file(new=False)
Parses configuration file.
Create new config file if it is not existing.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
new | bool | True if configuration file is not expected to exist | False |
Raise
FedbiomedEnvironError: cannot read configuration file
Source code in fedbiomed/common/environ.py
def parse_write_config_file(self, new: bool = False):
"""Parses configuration file.
Create new config file if it is not existing.
Args:
new: True if configuration file is not expected to exist
Raise:
FedbiomedEnvironError: cannot read configuration file
"""
# Sets configuration file path
self.set_config_file()
# Parse configuration if it is existing
if os.path.isfile(self._values["CONFIG_FILE"]) and not new:
# get values from .ini file
try:
self._cfg.read(self._values["CONFIG_FILE"])
except configparser.Error:
_msg = ErrorNumbers.FB600.value + ": cannot read config file, check file permissions"
logger.critical(_msg)
raise FedbiomedEnvironError(_msg)
# Create new configuration file
else:
# Create new config file
self._set_component_specific_config_parameters()
# Updates config file with MQTT configuration
self._configure_mqtt()
# Update config with secure aggregation parameters
self._configure_secure_aggregation()
# Writes config file to a file
self._write_config_file()
set_config_file
set_config_file()
Sets configuration file
Source code in fedbiomed/common/environ.py
def set_config_file(self):
"""Sets configuration file """
config_file = os.getenv('CONFIG_FILE')
if config_file:
if not os.path.isabs(config_file):
config_file = os.path.join(self._values['CONFIG_DIR'],
os.getenv('CONFIG_FILE'))
else:
config_file = self.default_config_file()
self._values["CONFIG_FILE"] = config_file
setup_environment
setup_environment()
Final environment setup function
Source code in fedbiomed/common/environ.py
def setup_environment(self):
"""Final environment setup function """
# Initialize common environment variables
self._initialize_common_variables()
# Parse config file or create if not existing
self.parse_write_config_file()
# Check that config version is compatible
self._check_config_version()
# Configuring network variables
self._set_network_variables()
# Initialize environment variables
self._set_component_specific_variables()