This repository was archived by the owner on Dec 13, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 177
Expand file tree
/
Copy pathnew_parser.py
More file actions
79 lines (67 loc) · 3.45 KB
/
new_parser.py
File metadata and controls
79 lines (67 loc) · 3.45 KB
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
import haproxy.config
from haproxy.parser.base_parser import Specs, EnvParser
class NewSpecs(Specs):
def __init__(self, links):
super(self.__class__, self).__init__()
self.service_aliases = self._parse_service_aliases(links)
self.details = self._parse_details(self.service_aliases, links)
self.routes = self._parse_routes(self.details, links)
self.vhosts = self._parse_vhosts(self.details)
self._merge_services_with_same_vhost()
@staticmethod
def _parse_service_aliases(links):
service_aliases = []
for link in links.itervalues():
if link["service_name"] not in service_aliases:
service_aliases.append(link["service_name"])
return service_aliases
@staticmethod
def _parse_details(service_aliases, links):
env_parser = NewEnvParser(service_aliases)
for link in links.itervalues():
for envvar in link['container_envvars']:
env_parser.parse(link['service_name'], envvar['key'], envvar['value'])
details = env_parser.get_details()
return details
@staticmethod
def _parse_routes(details, links):
routes = {}
for link in links.itervalues():
container_name = link["container_name"]
service_alias = link["service_name"]
if service_alias not in routes:
routes[service_alias] = []
for endpoint in link["endpoints"].itervalues():
route = haproxy.config.BACKEND_MATCH.match(endpoint).groupdict()
route.update({"container_name": container_name})
route_health_check = details.get(service_alias, {}).get("health_check")
if route_health_check:
route.update({"health_check": route_health_check})
failover = None
if details.get(service_alias, {}).get("failover"):
failover = "backup"
extra_route_settings = details.get(service_alias, {}).get("extra_route_settings")
extra_route_settings = " ".join([(extra_route_settings if extra_route_settings else ""),
(failover if failover else "")]).strip()
if extra_route_settings:
route.update({"extra_route_settings": extra_route_settings})
exclude_ports = details.get(service_alias, {}).get("exclude_ports", [])
if not exclude_ports or (exclude_ports and route["port"] not in exclude_ports):
if route not in routes[service_alias]:
routes[service_alias].append(route)
return routes
class NewEnvParser(EnvParser):
def __init__(self, service_aliases):
super(self.__class__, self).__init__()
self.attrs = [method[6:] for method in self.__class__.__base__.__dict__ if method.startswith("parse_")]
for service_aliase in service_aliases:
self.details[service_aliase] = {}
for attr in self.attrs:
self.details[service_aliase][attr] = self.__getattribute__("parse_%s" % attr)("")
self.service_aliases = service_aliases
def parse(self, service, key, value):
key = key.lower()
if service in self.service_aliases:
for attr in self.attrs:
if key == attr and not self.details[service][key]:
self.details[service][key] = getattr(self, "parse_%s" % key)(value)