diff options
Diffstat (limited to 'osdf/config')
-rw-r--r-- | osdf/config/__init__.py | 2 | ||||
-rw-r--r-- | osdf/config/base.py | 15 | ||||
-rw-r--r-- | osdf/config/consulconfig.py | 52 | ||||
-rw-r--r-- | osdf/config/loader.py | 2 |
4 files changed, 68 insertions, 3 deletions
diff --git a/osdf/config/__init__.py b/osdf/config/__init__.py index 86156a1..e32d44e 100644 --- a/osdf/config/__init__.py +++ b/osdf/config/__init__.py @@ -27,6 +27,6 @@ class CoreConfig(metaclass=MetaSingleton): def get_core_config(self, config_file=None): if self.core_config is None: - self.core_config = yaml.load(open(config_file)) + self.core_config = yaml.safe_load(open(config_file)) return self.core_config diff --git a/osdf/config/base.py b/osdf/config/base.py index fbe9315..be693cb 100644 --- a/osdf/config/base.py +++ b/osdf/config/base.py @@ -1,5 +1,6 @@ # ------------------------------------------------------------------------- # Copyright (c) 2015-2017 AT&T Intellectual Property +# Copyright (C) 2020 Wipro Limited. # # Licensed under the Apache License, Version 2.0 (the "License"); # you may not use this file except in compliance with the License. @@ -17,16 +18,21 @@ # import os - +from osdf.config.consulconfig import call_consul_kv import osdf.config.credentials as creds import osdf.config.loader as config_loader from osdf.utils.programming_utils import DotDict +from threading import Thread config_spec = { "deployment": os.environ.get("OSDF_CONFIG_FILE", "config/osdf_config.yaml"), "core": "config/common_config.yaml" } +slicing_spec = "config/slicing_config.yaml" + +slice_config = config_loader.load_config_file(slicing_spec) + osdf_config = DotDict(config_loader.all_configs(**config_spec)) http_basic_auth_credentials = creds.load_credentials(osdf_config) @@ -34,3 +40,10 @@ http_basic_auth_credentials = creds.load_credentials(osdf_config) dmaap_creds = creds.dmaap_creds() creds_prefixes = {"so": "so", "cm": "cmPortal", "pcih": "pciHMS"} + +osdf_config_deployment = osdf_config.deployment + + +if osdf_config.deployment.get('activateConsulConfig'): + consulthread = Thread(target=call_consul_kv, args=(osdf_config,)) + consulthread.start() diff --git a/osdf/config/consulconfig.py b/osdf/config/consulconfig.py new file mode 100644 index 0000000..fc5b3fe --- /dev/null +++ b/osdf/config/consulconfig.py @@ -0,0 +1,52 @@ +from consul.base import Timeout +from consul.tornado import Consul +import json +from osdf.logging.osdf_logging import debug_log +from tornado.gen import coroutine +from tornado.ioloop import IOLoop + + +class Config(object): + def __init__(self, loop, osdf_final_config): + self.config = osdf_final_config + osdf_config = self.config['osdf_config'] + self.consul = Consul(host=osdf_config['consulHost'], port=osdf_config['consulPort'], + scheme=osdf_config['consulScheme'], verify=osdf_config['consulVerify'], + cert=osdf_config['consulCert']) + result = json.dumps(self.config) + self.consul.kv.put("osdfconfiguration", result) + loop.add_callback(self.watch) + + @coroutine + def watch(self): + index = None + while True: + try: + index, data = yield self.consul.kv.get('osdfconfiguration', index=index) + if data is not None: + self.update_config(data) + except Timeout: + pass + except Exception as e: + debug_log.debug('Exception Encountered {}'.format(e)) + + def update_config(self, data): + new_config = json.loads(data['Value'].decode('utf-8')) + osdf_deployment = new_config['osdf_config'] + osdf_core = new_config['common_config'] + self.config['osdf_config'].update(osdf_deployment) + self.config['common_config'].update(osdf_core) + debug_log.debug("updated config {}".format(new_config)) + debug_log.debug("value changed") + + +def call_consul_kv(osdf_config): + osdf_final_config = { + 'osdf_config': osdf_config.deployment, + 'common_config': osdf_config.core + } + io_loop = IOLoop() + io_loop.make_current() + IOLoop.current(instance=False) + _ = Config(io_loop, osdf_final_config) + io_loop.start() diff --git a/osdf/config/loader.py b/osdf/config/loader.py index 7cb363a..dca0033 100644 --- a/osdf/config/loader.py +++ b/osdf/config/loader.py @@ -31,7 +31,7 @@ def load_config_file(config_file: str, child_name="dockerConfiguration") -> dict with open(config_file, 'r') as fid: res = {} if config_file.endswith(".yaml"): - res = yaml.load(fid) + res = yaml.safe_load(fid) elif config_file.endswith(".json") or config_file.endswith("json"): res = json.load(fid) return res.get(child_name, res) if child_name else res |