Merge ~gschiano/squid-reverseproxy-charm:add-systemd-support into squid-reverseproxy-charm:master

Proposed by Grégory Schiano
Status: Merged
Approved by: Haw Loeung
Approved revision: 86725ef4071f4b703357a7df0aa80b07cb50826f
Merged at revision: a50829993d60c0be60618ec3617d834a41d0f103
Proposed branch: ~gschiano/squid-reverseproxy-charm:add-systemd-support
Merge into: squid-reverseproxy-charm:master
Diff against target: 118 lines (+85/-4)
1 file modified
hooks/hooks.py (+85/-4)
Reviewer Review Type Date Requested Status
Haw Loeung +1 Approve
Canonical IS Reviewers Pending
Review via email: mp+460781@code.launchpad.net

Commit message

Support setting Max file descriptor if service is using Systemd

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
Haw Loeung (hloeung) wrote :

LGTM

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

Change successfully merged at revision a50829993d60c0be60618ec3617d834a41d0f103

Preview Diff

[H/L] Next/Prev Comment, [J/K] Next/Prev File, [N/P] Next/Prev Hunk
1diff --git a/hooks/hooks.py b/hooks/hooks.py
2index 3aad58c..ae2e1b2 100755
3--- a/hooks/hooks.py
4+++ b/hooks/hooks.py
5@@ -51,7 +51,8 @@ metrics_script_path = "/usr/local/bin/squid_metrics.py"
6 metrics_config_path = "/usr/local/etc/squid_metrics.ini"
7 site_mib_path = "/usr/share/mibs/site"
8 Server = collections.namedtuple("Server", "name address port options")
9-service_affecting_packages = ['squid']
10+service_affecting_packages = ["squid"]
11+max_fd = 65536
12
13 ###############################################################################
14 # Supporting functions
15@@ -450,7 +451,6 @@ def write_metrics_cronjob(script_path, config_path, cron_path):
16 def service_squid3(action=None, squid3_config=default_squid3_config):
17 """Convenience function to start/stop/restart/reload the squid3
18 service"""
19- release = int(host.lsb_release()['DISTRIB_RELEASE'][0:2])
20 service_name = squid_name
21 if action is None or squid3_config is None:
22 return
23@@ -562,12 +562,93 @@ def install_hook():
24 if not os.path.exists(default_squid3_config_cache_dir):
25 host.mkdir(default_squid3_config_cache_dir, 'proxy', 'proxy', 0o755)
26 if not os.path.exists(default_squid3_cache_dir):
27- host.mkdir(default_squid3_cache_dir, 'proxy', 'proxy', 0o755)
28- shutil.copy2('%s/files/default.squid3' % charm_dir, '/etc/default/squid3')
29+ host.mkdir(default_squid3_cache_dir, "proxy", "proxy", 0o755)
30 install_packages()
31+
32+ service_file_path = f"/lib/systemd/system/{squid_name}.service"
33+ new_service_file_path = f"/etc/systemd/system/{squid_name}.service"
34+ if host.init_is_systemd(squid_name) and os.path.exists(service_file_path):
35+ edit_systemd_file_descriptors_limit(
36+ service_file_path, new_service_file_path, max_fd
37+ )
38+ systemd_daemon_reload()
39+ else:
40+ shutil.copy2(
41+ "%s/files/default.squid3" % charm_dir, f"/etc/default/{squid_name}"
42+ )
43 return True
44
45
46+def edit_systemd_file_descriptors_limit(
47+ service_file_path: str, new_service_file_path: str, file_descriptor_limit: int
48+):
49+ """
50+ Edits a systemd service unit file to set the file descriptor limit.
51+
52+ Args:
53+ service_file_path: The path to the systemd service file.
54+ file_descriptor_limit: The new file descriptor limit.
55+
56+ """
57+ updated = False
58+ in_service_block = False
59+
60+ directive = f"LimitNOFILE={file_descriptor_limit}\n"
61+
62+ with open(service_file_path, "r") as service:
63+ with open(new_service_file_path, "w") as new_service:
64+ for line in service:
65+ if line.strip() == "[Service]":
66+ in_service_block = True
67+ elif line.startswith("[") and in_service_block:
68+ # Exiting the [Service] block without finding the directive
69+ if not updated:
70+ new_service.write(directive)
71+ updated = True
72+ in_service_block = False
73+
74+ if in_service_block and line.startswith("LimitNOFILE"):
75+ new_service.write(directive)
76+ updated = True
77+ continue
78+
79+ new_service.write(line)
80+
81+ if not updated and in_service_block:
82+ # If still in the [Service] block and haven't updated, append the directive
83+ new_service.write(directive)
84+
85+
86+def systemd_daemon_reload(check: bool = False) -> int:
87+ """Reload systemd service files.
88+
89+ Args:
90+ check: Check the output of the systemctl command. Default: False.
91+
92+ Returns:
93+ Returncode of systemctl command execution.
94+
95+ Raises:
96+ SystemdError: Raised if calling systemctl returns a non-zero returncode and check is True.
97+ """
98+ cmd = ["systemctl", "daemon-reload"]
99+ log(f"Executing command: {cmd}", "debug")
100+ proc = subprocess.run(
101+ cmd,
102+ stdout=subprocess.PIPE,
103+ stderr=subprocess.STDOUT,
104+ text=True,
105+ bufsize=1,
106+ encoding="utf-8",
107+ check=check,
108+ )
109+ log(
110+ f"Command {cmd} exit code: {proc.returncode}. systemctl output:\n{proc.stdout}",
111+ "debug",
112+ )
113+ return proc.returncode
114+
115+
116 def service_can_start():
117 # If wait_for_auth_helper is set, wait until squid has started and the
118 # squid-auth-helper relation has been joined.

Subscribers

People subscribed via source and target branches

to all changes: