config: improve plugin configuration loading (implement 'init_config')

parent 8a9c5f31
......@@ -273,10 +273,10 @@ class Config(metaclass=ConfigMeta):
"""Get the list of the enabled plugins"""
plugins = []
env_config = os.getenv('BEIRAN_PLUGINS')
if env_config:
env_plugins = os.getenv('BEIRAN_PLUGINS')
if env_plugins:
try:
for p_package in env_config.split(','):
for p_package in env_plugins.split(','):
(p_type, p_name) = p_package.split('.')
if p_type not in self.plugin_types:
raise Exception("Unknown plugin type: %s" % (p_type))
......@@ -307,7 +307,21 @@ class Config(metaclass=ConfigMeta):
return plugins
def get_plugin_config(self, p_type, name):
"""Get params of package plugin"""
"""Get params of package plugin. To set the plugin's
configuration with the environment variable,
set ``BEIRAN_<plugin type>_<plugin name>_CONFIG`` like this.
``BEIRAN_PACKAGE_DOCKER_CONFIG="chahce_dir=temp,foo=bar"``
"""
env_config = os.getenv("BEIRAN_{}_{}_CONFIG".format(p_type.upper(), name.upper()))
conf = dict()
if env_config:
for item in env_config.split(','):
key, value = item.split('=')
conf[key] = value
return conf
conf = self.get_config(ckey='%s.%s' % (p_type, name))
if not conf:
return dict()
......
......@@ -272,7 +272,7 @@ class BeiranDaemon(EventEmitter):
shared_config_for_plugins = {
"version": VERSION,
}
for plugin in config.get_enabled_plugins():
for plugin in config.enabled_plugins:
type_specific_config = dict()
if plugin['type'] == 'discovery':
type_specific_config = {
......
......@@ -76,6 +76,7 @@ class AbstractBasePlugin(metaclass=ABCMeta):
class BasePlugin(AbstractBasePlugin, EventEmitter): # pylint: disable=too-many-instance-attributes
"""BeiranPlugin with EventEmmiter
"""
DEFAULTS = {} # type: dict
def __init__(self, config: dict) -> None:
"""
......@@ -102,7 +103,7 @@ class BasePlugin(AbstractBasePlugin, EventEmitter): # pylint: disable=too-many-
if self.log.level == logging.NOTSET:
self.log.setLevel(logging.WARN)
self.daemon = config.pop('daemon')
self.config = config
self.config = self.init_config(config)
self.loop = get_event_loop()
self.status = 'init'
......@@ -139,6 +140,13 @@ class BasePlugin(AbstractBasePlugin, EventEmitter): # pylint: disable=too-many-
# """Get plugin status"""
# return self.__status
def init_config(self, config: dict):
"""Initialize plugin configuration. Values ​​not in ``config`` are
set with default values"""
for key, value in self.DEFAULTS.items():
config.setdefault(key, value)
return config
def emit(self, event: str, *args: Any, **kwargs: Any) -> None:
if event != 'new_listener':
# self.log.debug('[' + self.plugin_type
......
......@@ -10,7 +10,7 @@ services:
- .:/src
environment:
- BEIRAN_DISCOVERY_METHOD=dns
- DISCOVERY_SERVICE_ADDRESS=beiran_dns
- BEIRAN_DISCOVERY_DNS_CONFIG='discovery_service_address=beiran_dns'
- BEIRAN_LISTEN_ADDR=0.0.0.0
- BEIRAN_LOG_LEVEL=INFO
......
......@@ -16,5 +16,5 @@ services:
environment:
- BEIRAN_DISCOVERY_METHOD=zeroconf
- BEIRAN_LISTEN_ADDR=0.0.0.0
- DISCOVERY_SERVICE_ADDRESS=beirand
- BEIRAN_DISCOVERY_DNS_CONFIG='discovery_service_address=beirand'
- BEIRAN_LOG_LEVEL=INFO
......@@ -22,7 +22,6 @@
Zeroconf multicast discovery service implementation
"""
import os
import asyncio
import aiodns
......@@ -40,6 +39,9 @@ DEFAULT_DOMAIN = "_beiran._tcp.local."
class DNSDiscovery(BaseDiscoveryPlugin):
"""Beiran Implementation of DNS Service Discovery
"""
DEFAULTS = {
'discovery_service_address': 'beirand'
}
def __init__(self, config: dict) -> None:
""" Creates an instance of Dns Discovery Service
......@@ -64,7 +66,7 @@ class DNSDiscovery(BaseDiscoveryPlugin):
Returns:
str: hostname to query
"""
return os.getenv('DISCOVERY_SERVICE_ADDRESS', 'beirand')
return self.config['discovery_service_address']
async def start(self):
""" Starts discovery service
......
......@@ -44,6 +44,7 @@ DEFAULT_DOMAIN = "_beiran._tcp.local."
class ZeroconfDiscovery(BaseDiscoveryPlugin):
"""Beiran Implementation of Zeroconf Multicast DNS Service Discovery
"""
DEFAULTS = {} # type: dict
def __init__(self, config: dict) -> None:
""" Creates an instance of Zeroconf Discovery Service
......
......@@ -39,6 +39,8 @@ PLUGIN_TYPE = 'interface'
# pylint: disable=attribute-defined-outside-init
class K8SInterface(BaseInterfacePlugin):
"""CRI v1alpha2 support for Beiran"""
DEFAULTS = {} # type: dict
def __init__(self, plugin_config: dict) -> None:
super().__init__(plugin_config)
if 'unix_socket_path' in plugin_config:
......
......@@ -49,6 +49,7 @@ PLUGIN_TYPE = 'package'
# pylint: disable=attribute-defined-outside-init
class DockerPackaging(BasePackagePlugin): # pylint: disable=too-many-instance-attributes
"""Docker support for Beiran"""
DEFAULTS = {} # type: dict
def __init__(self, config_dict: dict) -> None:
super().__init__({
......
......@@ -14,7 +14,7 @@ services:
- test
environment:
- BEIRAN_DISCOVERY_METHOD=dns
- DISCOVERY_SERVICE_ADDRESS=beirand
- BEIRAN_DISCOVERY_DNS_CONFIG='discovery_service_address=beirand'
- BEIRAN_LOG_LEVEL=INFO
- DOCKER_HOST=http://172.17.0.1:2375
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment