Add logrotate template rendered upon installation

There are no logrotate configurations for the mysql router logs.
This PR introduces a template rendered upon installation of the charm.
A maximum of 10 log files (10M each) are retained.

Change-Id: I18376228af4ca809c10a1b138ceb96459686daa4
Closes-Bug: #1880083
This commit is contained in:
Shayan Patel 2022-03-24 08:48:18 -04:00
parent a799fe9ca1
commit d9a1bb7965
3 changed files with 28 additions and 1 deletions

View File

@ -66,6 +66,11 @@ class MySQLRouterCharm(charms_openstack.charm.OpenStackCharm):
"/etc/systemd/system",
"{}.service".format(name))
logrotate_file = os.path.join(
"/etc/logrotate.d",
"{}".format(name)
)
services = [name]
restart_map = {
"/var/lib/mysql/{}/mysqlrouter.conf".format(name): services
@ -301,6 +306,19 @@ class MySQLRouterCharm(charms_openstack.charm.OpenStackCharm):
cmd = ["systemctl", "enable", self.name]
subprocess.check_output(cmd, stderr=subprocess.STDOUT)
# Logrotate File
ch_core.templating.render(
source="logrotate",
template_loader=os_templating.get_loader(
"templates/", self.release),
target=self.logrotate_file,
context={
"owner": self.mysqlrouter_user,
"group": self.mysqlrouter_group
},
perms=0o644,
)
def get_db_helper(self):
"""Get an instance of the MySQLDB8Helper class.

9
src/templates/logrotate Normal file
View File

@ -0,0 +1,9 @@
/var/lib/mysql/*/log/*.log {
rotate 9
notifempty
size 10M
create 0644 {{ owner }} {{ group }}
postrotate
kill -HUP $(pidof mysqlrouter)
endscript
}

View File

@ -268,7 +268,7 @@ class TestMySQLRouterCharm(test_utils.PatchHelper):
self.mkdir.assert_called_once_with(
"/var/lib/mysql", group="mysql", owner="mysql", perms=0o755)
self.render.assert_called_once()
self.assertEqual(self.render.call_count, 2)
self.subprocess.check_output.assert_called_once_with(
['systemctl', 'enable', _name],
stderr=self.subprocess.STDOUT)