Merge ~hloeung/content-cache-charm:master into content-cache-charm:master

Proposed by Haw Loeung
Status: Rejected
Rejected by: Haw Loeung
Proposed branch: ~hloeung/content-cache-charm:master
Merge into: content-cache-charm:master
Diff against target: 82 lines (+66/-0)
2 files modified
lib/haproxy.py (+32/-0)
tests/unit/test_haproxy.py (+34/-0)
Reviewer Review Type Date Requested Status
Joel Sing (community) +1 Approve
Canonical IS Reviewers Pending
Review via email: mp+380798@code.launchpad.net

Commit message

Add map_sites_ports for persistent ports - LP:1865945

To post a comment you must log in.
Revision history for this message
🤖 Canonical IS Merge Bot (canonical-is-mergebot) wrote :

This merge proposal is being monitored by mergebot. Change the status to Approved to merge.

Revision history for this message
Joel Sing (jsing) wrote :

LGTM, but see comments.

review: Approve (+1)
Revision history for this message
Haw Loeung (hloeung) :
~hloeung/content-cache-charm:master updated
3b55472... by Haw Loeung

Fixed based on reviews

Revision history for this message
Haw Loeung (hloeung) wrote :

Rejected in favor of keeping the mappings in unitdata.kv(). See:

https://code.launchpad.net/~hloeung/content-cache-charm/+git/content-cache-charm/+merge/380871

Unmerged commits

3b55472... by Haw Loeung

Fixed based on reviews

ff8557e... by Haw Loeung

Add map_sites_ports for persistent ports - LP:1865945

map_sites_ports() to parse the on disk HAProxy config and build a list
of site to port-pair mappings.

Preview Diff

[H/L] Next/Prev Comment, [J/K] Next/Prev File, [N/P] Next/Prev Hunk
diff --git a/lib/haproxy.py b/lib/haproxy.py
index e3a4d38..48cd495 100644
--- a/lib/haproxy.py
+++ b/lib/haproxy.py
@@ -282,3 +282,35 @@ backend backend-{name}
282 with open(self.conf_file, 'w', encoding='utf-8') as f:282 with open(self.conf_file, 'w', encoding='utf-8') as f:
283 f.write(content)283 f.write(content)
284 return True284 return True
285
286 def map_sites_ports(self):
287 sites = {'cache': {}, 'backend': {}}
288 regex = {
289 'cache': re.compile(r"^\s+server server_\d 127.0.0.1:(\d+)"),
290 'backend': re.compile(r"^\s+bind 127.0.0.1:(\d+)"),
291 'backend-cache': re.compile(r"^backend backend-cached-(.*)\s*$"),
292 }
293 if not os.path.exists(self.conf_file):
294 return sites
295 with open(self.conf_file, 'r', encoding='utf-8') as f:
296 site = None
297 key = None
298 for line in f.readlines():
299 if line.startswith('listen'):
300 site = line.split()[1]
301 if site == 'stats':
302 continue
303 key = 'backend'
304 elif line.startswith('backend'):
305 m = regex['backend-cache'].match(line)
306 if m:
307 site = m.group(1)
308 key = 'cache'
309 if not site or not key:
310 continue
311 m = regex[key].match(line)
312 if m:
313 sites[key][site] = int(m.group(1))
314 key = None
315
316 return sites
diff --git a/tests/unit/test_haproxy.py b/tests/unit/test_haproxy.py
index 1b2bb96..d596c4b 100644
--- a/tests/unit/test_haproxy.py
+++ b/tests/unit/test_haproxy.py
@@ -144,3 +144,37 @@ class TestLibHAProxy(unittest.TestCase):
144 },144 },
145 }145 }
146 self.assertEqual(haproxy._merge_listen_stanzas(config), want)146 self.assertEqual(haproxy._merge_listen_stanzas(config), want)
147
148 def test_haproxy_map_sites_ports(self):
149 haproxy = HAProxy.HAProxyConf(self.tmpdir)
150
151 want = {'cache': {}, 'backend': {}}
152 self.assertEqual(haproxy.map_sites_ports(), want)
153
154 shutil.copyfile('tests/unit/files/content_cache_rendered_haproxy_test_output.txt', haproxy.conf_file)
155 want = {
156 'cache': {
157 'site1-local': 6080,
158 'site2-local': 6081,
159 'site3-local': 6082,
160 'site4-local': 6083,
161 'site5': 6084,
162 'site6-local': 6085,
163 'site7-local': 6086,
164 'site8-local': 6087,
165 'site9-local': 6088,
166 },
167 'backend': {
168 'site1-local': 8080,
169 'site2-local': 8081,
170 'site3-local': 8082,
171 'site5': 8083,
172 'site5-2': 8084,
173 'site6-local': 8085,
174 'site7-local': 8086,
175 'site8-local': 8087,
176 'site8-local-2': 8088,
177 'site9-local': 8089,
178 },
179 }
180 self.assertEqual(haproxy.map_sites_ports(), want)

Subscribers

People subscribed via source and target branches