blob: fc5b3febb988384d21f5ae12ed73dfa453ac45fb (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
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()
|