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

Proposed by Haw Loeung
Status: Merged
Approved by: Colin Misare
Approved revision: 762a2110351fd14307f4c42c17ded04e76c8c895
Merged at revision: d562691618d272142f70f7d33233216b3bef38d3
Proposed branch: ~hloeung/content-cache-charm:maxfds
Merge into: content-cache-charm:master
Diff against target: 102 lines (+54/-2)
4 files modified
lib/haproxy.py (+5/-0)
lib/utils.py (+17/-0)
tests/unit/test_haproxy.py (+4/-2)
tests/unit/test_utils.py (+28/-0)
Reviewer Review Type Date Requested Status
Colin Misare Approve
Canonical IS Reviewers Pending
Review via email: mp+450739@code.launchpad.net

Commit message

Ship out systemd service override to increase max. fds for HAProxy

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
Colin Misare (cmisare) wrote :

LGTM

review: Approve
Revision history for this message
🤖 Canonical IS Merge Bot (canonical-is-mergebot) wrote :

Change successfully merged at revision d562691618d272142f70f7d33233216b3bef38d3

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 a8165c9..1e3b804 100644
3--- a/lib/haproxy.py
4+++ b/lib/haproxy.py
5@@ -465,6 +465,11 @@ backend backend-{name}
6 if haproxy_maxfds and haproxy_maxfds != 'unlimited' and int(maxfds) > int(haproxy_maxfds):
7 cmd = ['prlimit', '--pid', str(haproxy_pid), '--nofile={}'.format(str(maxfds))]
8 subprocess.call(cmd, stdout=subprocess.DEVNULL)
9+
10+ # We also need to make it persistent across HAproxy restarts
11+ opts = {'LimitNOFILE': int(maxfds)}
12+ utils.systemd_override(service='haproxy', opts=opts)
13+
14 return True
15
16 return False
17diff --git a/lib/utils.py b/lib/utils.py
18index 65adc48..a04107d 100644
19--- a/lib/utils.py
20+++ b/lib/utils.py
21@@ -291,3 +291,20 @@ def normalize_ip(ip):
22 except ValueError:
23 pass
24 raise ValueError("{} is not a valid ip address".format(repr(ip)))
25+
26+
27+def systemd_override(service, opts, systemd_path='/etc/systemd/system'):
28+ service_path = os.path.join(systemd_path, '{}.service.d'.format(service))
29+ if not os.path.exists(service_path):
30+ os.mkdir(service_path)
31+
32+ content = ['[Service]']
33+ for k, v in opts.items():
34+ content.append('{}={}'.format(k, v))
35+
36+ override_path = os.path.join(service_path, 'override.conf')
37+ with open(override_path, 'w', encoding='utf-8') as f:
38+ f.write('\n'.join(content) + '\n')
39+
40+ cmd = ['systemctl', 'daemon-reload']
41+ subprocess.call(cmd, stdout=subprocess.DEVNULL, stderr=subprocess.DEVNULL)
42diff --git a/tests/unit/test_haproxy.py b/tests/unit/test_haproxy.py
43index 7055281..e0d0ad7 100644
44--- a/tests/unit/test_haproxy.py
45+++ b/tests/unit/test_haproxy.py
46@@ -303,8 +303,9 @@ class TestLibHAProxy(unittest.TestCase):
47 self.assertEqual(haproxy.get_parent_pid(pidfile='tests/unit/files/some-file-doesnt-exist.pid'), 1)
48
49 @mock.patch('lib.utils.process_rlimits')
50+ @mock.patch('lib.utils.systemd_override')
51 @mock.patch('subprocess.call')
52- def test_increase_maxfds(self, call, process_rlimits):
53+ def test_increase_maxfds(self, call, systemd_override, process_rlimits):
54 haproxy = HAProxy.HAProxyConf(self.tmpdir)
55
56 call.reset_mock()
57@@ -332,7 +333,8 @@ class TestLibHAProxy(unittest.TestCase):
58 call.assert_not_called()
59
60 @mock.patch('lib.utils.process_rlimits')
61- def test_increase_maxfds_cpe(self, process_rlimits):
62+ @mock.patch('lib.utils.systemd_override')
63+ def test_increase_maxfds_cpe(self, systemd_override, process_rlimits):
64 haproxy = HAProxy.HAProxyConf(self.tmpdir)
65
66 process_rlimits.return_value = '10'
67diff --git a/tests/unit/test_utils.py b/tests/unit/test_utils.py
68index 9d9a081..62fe1e1 100644
69--- a/tests/unit/test_utils.py
70+++ b/tests/unit/test_utils.py
71@@ -315,3 +315,31 @@ class TestLibUtils(unittest.TestCase):
72 lambda _: utils.parse_ip_blocklist_config(incorrect_config),
73 "firewall config containing incorrect seperator should be invalid",
74 )
75+
76+ @mock.patch('subprocess.call')
77+ def test_systemd_override(self, call):
78+ opts = {'LimitNOFILE': 12345}
79+ utils.systemd_override('haproxy', opts, systemd_path=self.tmpdir)
80+ with open(os.path.join(self.tmpdir, 'haproxy.service.d', 'override.conf')) as f:
81+ want = textwrap.dedent(
82+ """\
83+ [Service]
84+ LimitNOFILE=12345
85+ """
86+ )
87+ got = f.read()
88+ self.assertEqual(want, got)
89+
90+ call.assert_called_once_with(['systemctl', 'daemon-reload'], stdout=-3, stderr=-3)
91+
92+ opts = {'LimitNOFILE': 66666}
93+ utils.systemd_override('haproxy', opts, systemd_path=self.tmpdir)
94+ with open(os.path.join(self.tmpdir, 'haproxy.service.d', 'override.conf')) as f:
95+ want = textwrap.dedent(
96+ """\
97+ [Service]
98+ LimitNOFILE=66666
99+ """
100+ )
101+ got = f.read()
102+ self.assertEqual(want, got)

Subscribers

People subscribed via source and target branches