forked from docker-archive/dockercloud-haproxy
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbase_parser.py
More file actions
183 lines (147 loc) · 5.45 KB
/
base_parser.py
File metadata and controls
183 lines (147 loc) · 5.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
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
import urlparse
class Specs(object):
def __init__(self):
self.service_aliases = []
self.details = {}
self.routes = {}
self.vhosts = []
def _merge_services_with_same_vhost(self):
services_with_same_vhost = {}
unique_vhost = {}
for service_alias, detail in self.details.iteritems():
vhost_str = detail['virtual_host_str']
if vhost_str:
if vhost_str in unique_vhost:
services_with_same_vhost[service_alias] = unique_vhost[vhost_str]
else:
unique_vhost[vhost_str] = service_alias
for service_alias in services_with_same_vhost:
self.service_aliases.remove(service_alias)
del self.details[service_alias]
for route in self.routes[service_alias]:
self.routes[services_with_same_vhost[service_alias]].append(route)
del self.routes[service_alias]
vhosts = []
for vhost in self.vhosts:
if vhost['service_alias'] != service_alias:
vhosts.append(vhost)
self.vhosts = vhosts
@staticmethod
def _parse_vhosts(details):
for service_alias, attr in details.iteritems():
virtual_host_str = attr["virtual_host_str"] = attr["virtual_host"]
parsed_virtual_host = []
if virtual_host_str:
for h in [h.strip() for h in virtual_host_str.strip().split(",")]:
pr = urlparse.urlparse(h)
if not pr.netloc:
pr = urlparse.urlparse("http://%s" % h)
port = '443' if pr.scheme.lower() in ['https', 'wss'] else "80"
host = pr.netloc
if ":" in pr.netloc:
host_port = pr.netloc.split(":")
host = host_port[0]
port = host_port[1]
parsed_virtual_host.append({"scheme": pr.scheme,
"host": host,
"port": port,
"path": pr.path})
details[service_alias]["virtual_host"] = parsed_virtual_host
vhosts = []
for service_alias, attr in details.iteritems():
virtual_hosts = attr["virtual_host"]
if virtual_hosts:
for v in virtual_hosts:
vhost = dict(v)
vhost["service_alias"] = service_alias
vhosts.append(vhost)
try:
return sorted(vhosts, key=lambda vhost: details[vhost["service_alias"]]["virtual_host_weight"],
reverse=True)
except:
return
def get_details(self):
return self.details
def get_routes(self):
return self.routes
def get_vhosts(self):
return self.vhosts
def get_service_aliases(self):
return self.service_aliases
def get_default_ssl_cert(self):
if not hasattr(self, "default_ssl_cert"):
self.default_ssl_cert = filter(lambda x: x,
[attr["default_ssl_cert"] for attr in self.details.itervalues() if
"default_ssl_cert" in attr])
return self.default_ssl_cert
def get_ssl_cert(self):
if not hasattr(self, "ssl_cert"):
self.ssl_cert = filter(lambda x: x, [attr["ssl_cert"] for attr in self.details.itervalues() if
"ssl_cert" in attr])
return self.ssl_cert
class EnvParser(object):
def __init__(self):
self.details = {}
def get_details(self):
return self.details
@staticmethod
def parse_default_ssl_cert(value):
if value:
return value.replace(r'\n', '\n')
return ""
@staticmethod
def parse_ssl_cert(value):
return EnvParser.parse_default_ssl_cert(value)
@staticmethod
def parse_exclude_ports(value):
# '3306, 8080' => ['3306', '8080']
if value:
return [x.strip() for x in value.strip().split(",") if x.strip()]
return []
@staticmethod
def parse_virtual_host(value):
return value
@staticmethod
def parse_force_ssl(value):
return value
@staticmethod
def parse_appsession(value):
return value
@staticmethod
def parse_balance(value):
return value
@staticmethod
def parse_cookie(value):
return value
@staticmethod
def parse_tcp_ports(value):
# '9000, 22/ssl_bind_string' => ['9000', '22/ssl_bind_string']
if value:
return [x.strip() for x in value.strip().split(",") if x.strip()]
return []
@staticmethod
def parse_health_check(value):
return value
@staticmethod
def parse_http_check(value):
return value
@staticmethod
def parse_virtual_host_weight(value):
try:
return int(value)
except:
return 0
@staticmethod
def parse_hsts_max_age(value):
return value
@staticmethod
def parse_gzip_compression_type(value):
return value
@staticmethod
def parse_option(value):
if value:
return [x.strip() for x in value.strip().split(",") if x.strip()]
return []
@staticmethod
def parse_extra_settings(value):
return value