|
12 | 12 | import subprocess
|
13 | 13 | import tempfile
|
14 | 14 | import typing
|
15 |
| -from typing import Dict, List, Optional, Tuple |
| 15 | +from typing import Dict, Iterable, List, Optional, Tuple |
16 | 16 |
|
17 | 17 | import jinja2
|
18 | 18 | from charms.mysql.v0.mysql import (
|
@@ -280,29 +280,48 @@ def write_mysqld_config(self) -> None:
|
280 | 280 | content=content_str,
|
281 | 281 | )
|
282 | 282 |
|
283 |
| - def setup_logrotate_and_cron(self) -> None: |
284 |
| - """Create and write the logrotate config file.""" |
| 283 | + def setup_logrotate_and_cron(self, enabled_log_files: Iterable) -> None: |
| 284 | + """Setup log rotation configuration for text files. |
| 285 | +
|
| 286 | + Args: |
| 287 | + enabled_log_files: a iterable of enabled text logs |
| 288 | + """ |
285 | 289 | logger.debug("Creating logrotate config file")
|
| 290 | + config_path = "/etc/logrotate.d/flush_mysql_logs" |
| 291 | + script_path = f"{self.charm.charm_dir}/logrotation.sh" |
| 292 | + cron_path = "/etc/cron.d/flush_mysql_logs" |
| 293 | + logs_dir = f"{CHARMED_MYSQL_COMMON_DIRECTORY}/var/log/mysql" |
286 | 294 |
|
287 | 295 | with open("templates/logrotate.j2", "r") as file:
|
288 | 296 | template = jinja2.Template(file.read())
|
289 | 297 |
|
290 |
| - rendered = template.render( |
| 298 | + logrotate_conf_content = template.render( |
291 | 299 | system_user=MYSQL_SYSTEM_USER,
|
292 |
| - snap_common_directory=CHARMED_MYSQL_COMMON_DIRECTORY, |
| 300 | + log_dir=logs_dir, |
293 | 301 | charm_directory=self.charm.charm_dir,
|
294 | 302 | unit_name=self.charm.unit.name,
|
| 303 | + enabled_log_files=enabled_log_files, |
295 | 304 | )
|
296 | 305 |
|
297 |
| - with open("/etc/logrotate.d/flush_mysql_logs", "w") as file: |
298 |
| - file.write(rendered) |
| 306 | + self.write_content_to_file( |
| 307 | + config_path, logrotate_conf_content, owner="root", permission=0o644 |
| 308 | + ) |
299 | 309 |
|
300 |
| - cron = ( |
301 |
| - "* 1-23 * * * root logrotate -f /etc/logrotate.d/flush_mysql_logs\n" |
302 |
| - "1-59 0 * * * root logrotate -f /etc/logrotate.d/flush_mysql_logs\n" |
| 310 | + with open("templates/run_log_rotation.sh.j2", "r") as file: |
| 311 | + template = jinja2.Template(file.read()) |
| 312 | + |
| 313 | + logrotation_script_content = template.render( |
| 314 | + log_path=f"{CHARMED_MYSQL_COMMON_DIRECTORY}/var/log/mysql", |
| 315 | + enabled_log_files=enabled_log_files, |
| 316 | + logrotate_conf=config_path, |
| 317 | + owner=MYSQL_SYSTEM_USER, |
| 318 | + group=MYSQL_SYSTEM_USER, |
303 | 319 | )
|
304 |
| - with open("/etc/cron.d/flush_mysql_logs", "w") as file: |
305 |
| - file.write(cron) |
| 320 | + |
| 321 | + self.write_content_to_file(script_path, logrotation_script_content, permission=0o550) |
| 322 | + |
| 323 | + cron_content = f"* 1-23 * * * root {script_path}\n1-59 0 * * * root {script_path}\n" |
| 324 | + self.write_content_to_file(cron_path, cron_content, owner="root") |
306 | 325 |
|
307 | 326 | def reset_root_password_and_start_mysqld(self) -> None:
|
308 | 327 | """Reset the root user password and start mysqld."""
|
|
0 commit comments