Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix log rotation #288

Merged
merged 4 commits into from
Aug 1, 2024
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions src/charm.py
Original file line number Diff line number Diff line change
Expand Up @@ -272,8 +272,6 @@ def _log_rotate_layer(self) -> Layer:
max_log_size=Config.LogRotate.MAX_LOG_SIZE,
max_rotations=Config.LogRotate.MAX_ROTATIONS_TO_KEEP,
),
user=Config.UNIX_USER,
group=Config.UNIX_GROUP,
)

layer_config = {
Expand Down Expand Up @@ -547,6 +545,8 @@ def _on_mongod_pebble_ready(self, event) -> None:
if self.is_role(Config.Role.CONFIG_SERVER):
container.add_layer("mongos", self._mongos_layer, combine=True)

container.exec(["chmod", "644", "/etc/logrotate.d/mongodb"])

# Restart changed services and start startup-enabled services.
container.replan()

Expand Down
31 changes: 15 additions & 16 deletions templates/logrotate.j2
Original file line number Diff line number Diff line change
@@ -1,18 +1,17 @@
{{logs_directory}}/*.log
{
rotate {{max_rotations}}
size {{max_log_size}}
missingok
notifempty
create 0600 {{mongo_user}} {{mongo_user}}
compress
nomail
nocopytruncate
sharedscripts
postrotate
{% for id in service_ids %}
pebble restart mongod_{{ id }}
{% endfor %}
endscript
}

rotate {{max_rotations}}
size {{max_log_size}}
missingok
notifempty
create 0600 {{mongo_user}} {{mongo_user}}
compress
delaycompress
nomail
nocopytruncate
sharedscripts
postrotate
PID=$(pgrep -f "mongod.*--logpath={{logs_directory}}/mongodb.log")
/bin/kill -SIGUSR1 $PID
endscript
}
14 changes: 13 additions & 1 deletion tests/integration/test_charm.py
Original file line number Diff line number Diff line change
Expand Up @@ -185,12 +185,24 @@ async def test_log_rotate(ops_test: OpsTest) -> None:
universal_newlines=True,
)

log_rotated = "audit.log.1.gz" in log_files
log_rotated = "audit.log.1" in log_files
assert log_rotated, f"Could not find rotated log in {log_files}"

audit_log_exists = "audit.log" in log_files
assert audit_log_exists, f"Could not find audit.log log in {log_files}"

# wait for some logs to be collected
time.sleep(10)

audit_log_content = subprocess.check_output(
f"JUJU_MODEL={ops_test.model_full_name} juju ssh --container mongod {unit.name} 'cat {audit_log_path}.audit.log'",
stderr=subprocess.PIPE,
shell=True,
universal_newlines=True,
)
audit_log_lines = audit_log_content.strip().split("\n")
assert len(audit_log_lines) > 0, "New audit logs have not been written after log rotation."


@pytest.mark.group(1)
async def test_monitor_user(ops_test: OpsTest) -> None:
Expand Down
Loading