/
plugin.py
207 lines (166 loc) · 7.53 KB
/
plugin.py
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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
# Copyright 2016-2019 The Wazo Authors (see the AUTHORS file)
# SPDX-License-Identifier: GPL-3.0-or-later
import logging
import threading
import time
from uuid import UUID
import kombu
import yaml
from jinja2 import Environment, FileSystemLoader, TemplateNotFound
from xivo.consul_helpers import ServiceFinder
from xivo_bus.marshaler import InvalidMessage, Marshaler
from xivo_bus.resources.services.event import ServiceRegisteredEvent
from wazo_dird import BaseServicePlugin
logger = logging.getLogger(__name__)
class ServiceDiscoveryServicePlugin(BaseServicePlugin):
def __init__(self):
self._service = None
def load(self, dependencies):
config = dependencies['config']
bus = dependencies['bus']
source_manager = dependencies['source_manager']
controller = dependencies['controller']
self._service = _Service(config, bus, source_manager, controller)
class _Service:
QUEUE = kombu.Queue(exchange=kombu.Exchange('xivo', type='topic'),
routing_key='service.registered.*',
exclusive=True)
def __init__(self, config, bus, source_manager, controller):
self._controller = controller
self._config = config
self._source_manager = source_manager
service_disco_config = config['services'].get('service_discovery')
if not service_disco_config:
logger.info('"service_discovery" key missing from the configuration')
return
self._source_config_generator = SourceConfigGenerator(service_disco_config)
self._profile_config_updater = ProfileConfigUpdater(config)
bus.add_consumer(self.QUEUE, self._on_service_registered)
finder = ServiceFinder(config['consul'])
fetcher_thread = threading.Thread(target=self._add_remote_services,
args=(finder, service_disco_config))
fetcher_thread.daemon = True
fetcher_thread.start()
def _add_remote_services(self, finder, service_disco_config):
logger.info('Searching for remote services...')
while True:
try:
for service_name in service_disco_config['services']:
for service in finder.list_healthy_services(service_name):
uuid = _find_first_uuid(service['Tags'])
if not uuid:
continue
host = service['Address']
port = service['Port']
self._on_service_added(service_name, host, port, uuid)
logger.debug('Searching done')
return
except Exception as e:
logger.info('failed to find running services: %s', e)
time.sleep(2)
def _on_service_added(self, service_name, host, port, uuid):
logger.debug('%s registered %s:%s with uuid %s', service_name, host, port, uuid)
config = self._source_config_generator.generate_from_new_service(
service_name, uuid, host, port)
if not config:
return
source_name = config.get('name')
source_service = self._controller.services['source']
if self._source_exists(source_service, config):
return
source_service.create('wazo', **config)
self._profile_config_updater.on_service_added(source_name, service_name)
logger.info('new source added %s', source_name)
def _source_exists(self, source_service, config):
search_params = {
'backend': 'wazo',
'visible_tenants': [config['tenant_uuid']],
'name': config['name'],
}
return True if source_service.list_(**search_params) else False
def _on_service_registered(self, body, message):
try:
event = Marshaler.unmarshal_message(body, ServiceRegisteredEvent)
except (InvalidMessage, KeyError):
logger.exception('Ignoring the following malformed bus message: %s', body)
else:
uuid = _find_first_uuid(event.tags)
if uuid:
self._on_service_added(event.service_name,
event.advertise_address,
event.advertise_port,
uuid)
message.ack()
def _find_first_uuid(tags):
for tag in tags:
try:
return str(UUID(tag))
except (AttributeError, ValueError):
continue
class ProfileConfigUpdater:
def __init__(self, config):
self._config = config
self._watched_services = {}
self._host_configs = config['services']['service_discovery']['hosts']
consul_services = config['services']['service_discovery']['services']
for name, config in consul_services.items():
self._watched_services[name] = {
'lookup': self._profiles_for(config, 'lookup'),
'reverse': self._profiles_for(config, 'reverse'),
'favorites': self._profiles_for(config, 'favorites'),
}
def on_service_added(self, source_name, discovered_service):
consul_service_config = self._watched_services.get(discovered_service)
if not consul_service_config:
return
for dird_service, profiles in consul_service_config.items():
dird_service_config = self._config['services'].get(dird_service)
if not dird_service_config:
continue
for profile in profiles:
if profile not in dird_service_config:
dird_service_config[profile] = {'sources': {}}
if 'sources' not in dird_service_config[profile]:
dird_service_config[profile]['sources'] = {}
dird_service_config[profile]['sources'][source_name] = True
@staticmethod
def _profiles_for(config, service):
profiles = config.get(service, {})
return [profile for profile, enabled in profiles.items() if enabled]
class SourceConfigGenerator:
enabled = False
def __init__(self, service_discovery_config):
logger.debug('Starting with %s', service_discovery_config)
template_path = service_discovery_config.get('template_path')
if not template_path:
logger.info('service discovery service error: no "template_path" configured')
return
loader = FileSystemLoader(template_path)
self._env = Environment(loader=loader)
self._host_configs = service_discovery_config['hosts']
self._template_files = {
service: config['template']
for service, config
in service_discovery_config['services'].items()
}
self.enabled = True
def generate_from_new_service(self, service, uuid, host, port):
if not self.enabled:
return
if uuid not in self._host_configs:
return
template_file = self._template_files.get(service)
if not template_file:
logger.info('no template configured for service %s', service)
return
try:
template = self._env.get_template(template_file)
except TemplateNotFound:
logger.info('no template found with name %s', template_file)
return
template_args = dict(self._host_configs[uuid])
template_args['uuid'] = uuid
template_args['hostname'] = host
template_args['port'] = port
yaml_representation = template.render(template_args)
return yaml.safe_load(yaml_representation)