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
diff --git a/lib/haproxy.py b/lib/haproxy.py
index a8165c9..1e3b804 100644
--- a/lib/haproxy.py
+++ b/lib/haproxy.py
@@ -465,6 +465,11 @@ backend backend-{name}
465 if haproxy_maxfds and haproxy_maxfds != 'unlimited' and int(maxfds) > int(haproxy_maxfds):465 if haproxy_maxfds and haproxy_maxfds != 'unlimited' and int(maxfds) > int(haproxy_maxfds):
466 cmd = ['prlimit', '--pid', str(haproxy_pid), '--nofile={}'.format(str(maxfds))]466 cmd = ['prlimit', '--pid', str(haproxy_pid), '--nofile={}'.format(str(maxfds))]
467 subprocess.call(cmd, stdout=subprocess.DEVNULL)467 subprocess.call(cmd, stdout=subprocess.DEVNULL)
468
469 # We also need to make it persistent across HAproxy restarts
470 opts = {'LimitNOFILE': int(maxfds)}
471 utils.systemd_override(service='haproxy', opts=opts)
472
468 return True473 return True
469474
470 return False475 return False
diff --git a/lib/utils.py b/lib/utils.py
index 65adc48..a04107d 100644
--- a/lib/utils.py
+++ b/lib/utils.py
@@ -291,3 +291,20 @@ def normalize_ip(ip):
291 except ValueError:291 except ValueError:
292 pass292 pass
293 raise ValueError("{} is not a valid ip address".format(repr(ip)))293 raise ValueError("{} is not a valid ip address".format(repr(ip)))
294
295
296def systemd_override(service, opts, systemd_path='/etc/systemd/system'):
297 service_path = os.path.join(systemd_path, '{}.service.d'.format(service))
298 if not os.path.exists(service_path):
299 os.mkdir(service_path)
300
301 content = ['[Service]']
302 for k, v in opts.items():
303 content.append('{}={}'.format(k, v))
304
305 override_path = os.path.join(service_path, 'override.conf')
306 with open(override_path, 'w', encoding='utf-8') as f:
307 f.write('\n'.join(content) + '\n')
308
309 cmd = ['systemctl', 'daemon-reload']
310 subprocess.call(cmd, stdout=subprocess.DEVNULL, stderr=subprocess.DEVNULL)
diff --git a/tests/unit/test_haproxy.py b/tests/unit/test_haproxy.py
index 7055281..e0d0ad7 100644
--- a/tests/unit/test_haproxy.py
+++ b/tests/unit/test_haproxy.py
@@ -303,8 +303,9 @@ class TestLibHAProxy(unittest.TestCase):
303 self.assertEqual(haproxy.get_parent_pid(pidfile='tests/unit/files/some-file-doesnt-exist.pid'), 1)303 self.assertEqual(haproxy.get_parent_pid(pidfile='tests/unit/files/some-file-doesnt-exist.pid'), 1)
304304
305 @mock.patch('lib.utils.process_rlimits')305 @mock.patch('lib.utils.process_rlimits')
306 @mock.patch('lib.utils.systemd_override')
306 @mock.patch('subprocess.call')307 @mock.patch('subprocess.call')
307 def test_increase_maxfds(self, call, process_rlimits):308 def test_increase_maxfds(self, call, systemd_override, process_rlimits):
308 haproxy = HAProxy.HAProxyConf(self.tmpdir)309 haproxy = HAProxy.HAProxyConf(self.tmpdir)
309310
310 call.reset_mock()311 call.reset_mock()
@@ -332,7 +333,8 @@ class TestLibHAProxy(unittest.TestCase):
332 call.assert_not_called()333 call.assert_not_called()
333334
334 @mock.patch('lib.utils.process_rlimits')335 @mock.patch('lib.utils.process_rlimits')
335 def test_increase_maxfds_cpe(self, process_rlimits):336 @mock.patch('lib.utils.systemd_override')
337 def test_increase_maxfds_cpe(self, systemd_override, process_rlimits):
336 haproxy = HAProxy.HAProxyConf(self.tmpdir)338 haproxy = HAProxy.HAProxyConf(self.tmpdir)
337339
338 process_rlimits.return_value = '10'340 process_rlimits.return_value = '10'
diff --git a/tests/unit/test_utils.py b/tests/unit/test_utils.py
index 9d9a081..62fe1e1 100644
--- a/tests/unit/test_utils.py
+++ b/tests/unit/test_utils.py
@@ -315,3 +315,31 @@ class TestLibUtils(unittest.TestCase):
315 lambda _: utils.parse_ip_blocklist_config(incorrect_config),315 lambda _: utils.parse_ip_blocklist_config(incorrect_config),
316 "firewall config containing incorrect seperator should be invalid",316 "firewall config containing incorrect seperator should be invalid",
317 )317 )
318
319 @mock.patch('subprocess.call')
320 def test_systemd_override(self, call):
321 opts = {'LimitNOFILE': 12345}
322 utils.systemd_override('haproxy', opts, systemd_path=self.tmpdir)
323 with open(os.path.join(self.tmpdir, 'haproxy.service.d', 'override.conf')) as f:
324 want = textwrap.dedent(
325 """\
326 [Service]
327 LimitNOFILE=12345
328 """
329 )
330 got = f.read()
331 self.assertEqual(want, got)
332
333 call.assert_called_once_with(['systemctl', 'daemon-reload'], stdout=-3, stderr=-3)
334
335 opts = {'LimitNOFILE': 66666}
336 utils.systemd_override('haproxy', opts, systemd_path=self.tmpdir)
337 with open(os.path.join(self.tmpdir, 'haproxy.service.d', 'override.conf')) as f:
338 want = textwrap.dedent(
339 """\
340 [Service]
341 LimitNOFILE=66666
342 """
343 )
344 got = f.read()
345 self.assertEqual(want, got)

Subscribers

People subscribed via source and target branches