deploy/journalctl: anchor server log to current unit start
The Server Log panel showed the last 200 lines of the unit's entire journal — mixing the current run with leftovers from prior starts. Resolve the unit's InactiveExitTimestamp inside the journalctl helper and pass it as journalctl --since so the panel begins at the latest unit start. Never-run units fall back to the legacy unit-only filter so -f attaches on first start. No Python changes; the helper's argv shape and sudoers grant stay identical. Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
This commit is contained in:
parent
2adf42655e
commit
c3ce6d447a
2 changed files with 32 additions and 3 deletions
|
|
@ -37,6 +37,16 @@ case "$follow_flag" in
|
||||||
esac
|
esac
|
||||||
|
|
||||||
unit="left4me-server@${name}.service"
|
unit="left4me-server@${name}.service"
|
||||||
|
|
||||||
|
if [ -x /bin/systemctl ]; then
|
||||||
|
systemctl=/bin/systemctl
|
||||||
|
elif [ -x /usr/bin/systemctl ]; then
|
||||||
|
systemctl=/usr/bin/systemctl
|
||||||
|
else
|
||||||
|
printf '%s\n' 'systemctl not found at /bin/systemctl or /usr/bin/systemctl' >&2
|
||||||
|
exit 69
|
||||||
|
fi
|
||||||
|
|
||||||
if [ -x /bin/journalctl ]; then
|
if [ -x /bin/journalctl ]; then
|
||||||
journalctl=/bin/journalctl
|
journalctl=/bin/journalctl
|
||||||
elif [ -x /usr/bin/journalctl ]; then
|
elif [ -x /usr/bin/journalctl ]; then
|
||||||
|
|
@ -46,8 +56,20 @@ else
|
||||||
exit 69
|
exit 69
|
||||||
fi
|
fi
|
||||||
|
|
||||||
|
# Anchor `--since` to the moment systemd began the unit's current start
|
||||||
|
# transaction so the log panel starts at the latest run. Force LC_ALL=C so
|
||||||
|
# the day-of-week prefix is in a locale journalctl reliably parses.
|
||||||
|
start_time=$(LC_ALL=C "$systemctl" show -p InactiveExitTimestamp --value "$unit" 2>/dev/null || true)
|
||||||
|
|
||||||
|
if [ -n "$start_time" ]; then
|
||||||
|
if [ -n "$follow_arg" ]; then
|
||||||
|
exec "$journalctl" -u "$unit" --since "$start_time" -n "$lines" -o cat "$follow_arg"
|
||||||
|
fi
|
||||||
|
exec "$journalctl" -u "$unit" --since "$start_time" -n "$lines" -o cat
|
||||||
|
fi
|
||||||
|
|
||||||
|
# Unit has never run: no --since cutoff. `-f` will attach on first start.
|
||||||
if [ -n "$follow_arg" ]; then
|
if [ -n "$follow_arg" ]; then
|
||||||
exec "$journalctl" -u "$unit" -n "$lines" -o cat "$follow_arg"
|
exec "$journalctl" -u "$unit" -n "$lines" -o cat "$follow_arg"
|
||||||
fi
|
fi
|
||||||
|
|
||||||
exec "$journalctl" -u "$unit" -n "$lines" -o cat
|
exec "$journalctl" -u "$unit" -n "$lines" -o cat
|
||||||
|
|
|
||||||
|
|
@ -22,10 +22,17 @@ def test_journalctl_helper_passes_shell_syntax_check_and_rejects_bad_args(tmp_pa
|
||||||
env=env_with_fake_commands(tmp_path),
|
env=env_with_fake_commands(tmp_path),
|
||||||
check=False,
|
check=False,
|
||||||
)
|
)
|
||||||
assert result.returncode != 0
|
assert result.returncode != 0, f"helper accepted bad args: {args!r}"
|
||||||
assert not marker.exists()
|
assert not marker.exists(), f"helper invoked journalctl for: {args!r}"
|
||||||
|
|
||||||
script = JOURNALCTL_HELPER.read_text()
|
script = JOURNALCTL_HELPER.read_text()
|
||||||
assert 'unit="left4me-server@${name}.service"' in script
|
assert 'unit="left4me-server@${name}.service"' in script
|
||||||
|
# Anchors `--since` to the unit's most recent start so the panel shows
|
||||||
|
# the current run (and any post-restart lines until reload).
|
||||||
|
assert 'InactiveExitTimestamp' in script
|
||||||
|
assert 'LC_ALL=C' in script
|
||||||
|
assert 'exec "$journalctl" -u "$unit" --since "$start_time" -n "$lines" -o cat "$follow_arg"' in script
|
||||||
|
assert 'exec "$journalctl" -u "$unit" --since "$start_time" -n "$lines" -o cat' in script
|
||||||
|
# Never-started fallback keeps the legacy unit-only form.
|
||||||
assert 'exec "$journalctl" -u "$unit" -n "$lines" -o cat "$follow_arg"' in script
|
assert 'exec "$journalctl" -u "$unit" -n "$lines" -o cat "$follow_arg"' in script
|
||||||
assert 'exec "$journalctl" -u "$unit" -n "$lines" -o cat' in script
|
assert 'exec "$journalctl" -u "$unit" -n "$lines" -o cat' in script
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue