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
1diff --git a/lib/haproxy.py b/lib/haproxy.py
2index e3a4d38..48cd495 100644
3--- a/lib/haproxy.py
4+++ b/lib/haproxy.py
5@@ -282,3 +282,35 @@ backend backend-{name}
6 with open(self.conf_file, 'w', encoding='utf-8') as f:
7 f.write(content)
8 return True
9+
10+ def map_sites_ports(self):
11+ sites = {'cache': {}, 'backend': {}}
12+ regex = {
13+ 'cache': re.compile(r"^\s+server server_\d 127.0.0.1:(\d+)"),
14+ 'backend': re.compile(r"^\s+bind 127.0.0.1:(\d+)"),
15+ 'backend-cache': re.compile(r"^backend backend-cached-(.*)\s*$"),
16+ }
17+ if not os.path.exists(self.conf_file):
18+ return sites
19+ with open(self.conf_file, 'r', encoding='utf-8') as f:
20+ site = None
21+ key = None
22+ for line in f.readlines():
23+ if line.startswith('listen'):
24+ site = line.split()[1]
25+ if site == 'stats':
26+ continue
27+ key = 'backend'
28+ elif line.startswith('backend'):
29+ m = regex['backend-cache'].match(line)
30+ if m:
31+ site = m.group(1)
32+ key = 'cache'
33+ if not site or not key:
34+ continue
35+ m = regex[key].match(line)
36+ if m:
37+ sites[key][site] = int(m.group(1))
38+ key = None
39+
40+ return sites
41diff --git a/tests/unit/test_haproxy.py b/tests/unit/test_haproxy.py
42index 1b2bb96..d596c4b 100644
43--- a/tests/unit/test_haproxy.py
44+++ b/tests/unit/test_haproxy.py
45@@ -144,3 +144,37 @@ class TestLibHAProxy(unittest.TestCase):
46 },
47 }
48 self.assertEqual(haproxy._merge_listen_stanzas(config), want)
49+
50+ def test_haproxy_map_sites_ports(self):
51+ haproxy = HAProxy.HAProxyConf(self.tmpdir)
52+
53+ want = {'cache': {}, 'backend': {}}
54+ self.assertEqual(haproxy.map_sites_ports(), want)
55+
56+ shutil.copyfile('tests/unit/files/content_cache_rendered_haproxy_test_output.txt', haproxy.conf_file)
57+ want = {
58+ 'cache': {
59+ 'site1-local': 6080,
60+ 'site2-local': 6081,
61+ 'site3-local': 6082,
62+ 'site4-local': 6083,
63+ 'site5': 6084,
64+ 'site6-local': 6085,
65+ 'site7-local': 6086,
66+ 'site8-local': 6087,
67+ 'site9-local': 6088,
68+ },
69+ 'backend': {
70+ 'site1-local': 8080,
71+ 'site2-local': 8081,
72+ 'site3-local': 8082,
73+ 'site5': 8083,
74+ 'site5-2': 8084,
75+ 'site6-local': 8085,
76+ 'site7-local': 8086,
77+ 'site8-local': 8087,
78+ 'site8-local-2': 8088,
79+ 'site9-local': 8089,
80+ },
81+ }
82+ self.assertEqual(haproxy.map_sites_ports(), want)

Subscribers

People subscribed via source and target branches