Skip to content
Merged
Show file tree
Hide file tree
Changes from all 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
15 changes: 14 additions & 1 deletion src/serverfs/etc/logrotate.d/linuxmuster-linbo7
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
/var/log/linuxmuster/linbo/*.log {
# *[!0-9].log leaves out the per-run linbo-remote logs
# (<host>_linbo-remote_<YYYYmmddHHMMSS>.log): they never grow after their run
# and are pruned by linbo-remote itself (RUN_LOG_RETENTION_DAYS).
/var/log/linuxmuster/linbo/*[!0-9].log {
rotate 4
weekly
compress
Expand All @@ -7,3 +10,13 @@
notifempty
create 600 nobody nogroup
}

# one JSON line per linbo-remote run
/var/log/linuxmuster/linbo/linbo-remote_runs.jsonl {
rotate 12
monthly
compress
missingok
notifempty
create 644 root root
}
2 changes: 2 additions & 0 deletions src/serverfs/usr/share/linuxmuster/linbo/linbo_remote_cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -300,6 +300,8 @@ def sendCmds(hosts, commands, wait, secrets_uploaded, dry_run=False):
if wait:
doWait(wait, f'Waiting {wait} second(s) for client(s) to boot', leading_blank_line=True)

lib.pruneRunLogs(environment.LINBOLOGDIR)

print()
print('Sending command(s) to:')
for host in hosts:
Expand Down
37 changes: 37 additions & 0 deletions src/serverfs/usr/share/linuxmuster/linbo/linbo_remote_lib.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@
"""

import json
import os
import re
import shlex
import subprocess
Expand Down Expand Up @@ -353,6 +354,42 @@ def runLogName(hostname, timestamp=None):

RUN_RECORD_BASENAME = 'linbo-remote_runs.jsonl'

# Per-run logs older than this are deleted when a new run starts. Run records
# in RUN_RECORD_BASENAME are rotated by logrotate instead.
RUN_LOG_RETENTION_DAYS = 90

_RUN_LOG_RE = re.compile(r'.+_linbo-remote_\d{14}\.log$')


def pruneRunLogs(logdir, max_age_days=RUN_LOG_RETENTION_DAYS, now=None):
"""
Delete per-run logs (see runLogName()) in logdir whose modification time
is more than max_age_days old; returns the number removed. Only files
matching the per-run name are touched. Best effort: a file that cannot be
stat'ed or removed is skipped, pruning must never break a run.

logrotate cannot do this: every per-run log has a unique name, so it would
never rotate (and thus never delete) the small ones, and its size rule
would rotate big ones into an empty file of the same name.
"""
cutoff = (time.time() if now is None else now) - max_age_days * 86400
removed = 0
try:
names = os.listdir(logdir)
except OSError:
return 0
for name in names:
if not _RUN_LOG_RE.match(name):
continue
path = os.path.join(logdir, name)
try:
if os.path.getmtime(path) < cutoff:
os.remove(path)
removed += 1
except OSError:
continue
return removed


def buildRunRecordPrefix(hostname, commands, started, logfile, mode='direct', dry_run=False):
"""
Expand Down
28 changes: 28 additions & 0 deletions tests/python/test_linbo_remote_lib.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
RUN_RECORD_BASENAME,
buildOnbootCmds,
buildRunRecordPrefix,
pruneRunLogs,
getMacFromAd,
getIpFromAd,
hostsInGroup,
Expand Down Expand Up @@ -490,3 +491,30 @@ def test_run_record_of_aborted_run_has_signal_exit_status(monkeypatch, tmp_path,
proc.wait(timeout=10)
record = json.loads(record_file.read_text().splitlines()[0])
assert record['rc'] == expected_rc


# --- retention of per-run logs ----------------------------------------------

def test_prune_run_logs_removes_only_old_per_run_logs(tmp_path):
now = time.time()
old = tmp_path / 'r100-pc01_linbo-remote_20260101000000.log'
fresh = tmp_path / 'r100-pc01_linbo-remote_20261001120000.log'
others = [
tmp_path / 'r100-pc01_linbo.log',
tmp_path / 'rsync-pre-download.log',
tmp_path / RUN_RECORD_BASENAME,
tmp_path / 'r100-pc01_linbo-remote_20260101000000.log.1.gz',
]
for f in [old, fresh, *others]:
f.write_text('x')
os.utime(f, (now - 200 * 86400, now - 200 * 86400))
os.utime(fresh, (now - 1 * 86400, now - 1 * 86400))

assert pruneRunLogs(str(tmp_path), max_age_days=90, now=now) == 1
assert not old.exists()
assert fresh.exists()
assert all(f.exists() for f in others)


def test_prune_run_logs_missing_dir_is_not_an_error(tmp_path):
assert pruneRunLogs(str(tmp_path / 'nope')) == 0
Loading