fix(gateway): normalize optional systemd directives in stale-check (#41119)
On older systemd versions that don't support RestartMaxDelaySec / RestartSteps, the installed unit file has those directives silently dropped. systemd_unit_is_current() did a strict text comparison, so the unit was perpetually flagged as outdated. Fix: _strip_optional_systemd_directives() removes RestartMaxDelaySec and RestartSteps from both the installed and expected text before comparison. Units that differ only by these optional directives are now correctly considered current.
This commit is contained in:
+32
-2
@@ -2473,6 +2473,29 @@ def _normalize_service_definition(text: str) -> str:
|
||||
return "\n".join(line.rstrip() for line in text.strip().splitlines())
|
||||
|
||||
|
||||
# Directives that older systemd versions silently ignore/strip. Normalize
|
||||
# them out of stale-check comparisons so a unit that differs only by these
|
||||
# directives is not perpetually flagged as outdated.
|
||||
_SYSTEMD_OPTIONAL_DIRECTIVES = (
|
||||
"RestartMaxDelaySec",
|
||||
"RestartSteps",
|
||||
)
|
||||
|
||||
|
||||
def _strip_optional_systemd_directives(text: str) -> str:
|
||||
"""Remove systemd directives that older hosts silently drop."""
|
||||
lines = text.splitlines()
|
||||
filtered = []
|
||||
for line in lines:
|
||||
stripped = line.strip()
|
||||
if stripped and not stripped.startswith("#"):
|
||||
key = stripped.split("=", 1)[0].strip()
|
||||
if key in _SYSTEMD_OPTIONAL_DIRECTIVES:
|
||||
continue
|
||||
filtered.append(line)
|
||||
return "\n".join(filtered)
|
||||
|
||||
|
||||
def _normalize_launchd_plist_for_comparison(text: str) -> str:
|
||||
"""Normalize launchd plist text for staleness checks.
|
||||
|
||||
@@ -2500,9 +2523,16 @@ def systemd_unit_is_current(system: bool = False) -> bool:
|
||||
installed = unit_path.read_text(encoding="utf-8")
|
||||
expected_user = _read_systemd_user_from_unit(unit_path) if system else None
|
||||
expected = generate_systemd_unit(system=system, run_as_user=expected_user)
|
||||
return _normalize_service_definition(installed) == _normalize_service_definition(
|
||||
expected
|
||||
# Normalize out directives that older systemd versions silently drop
|
||||
# (RestartMaxDelaySec, RestartSteps) so a unit that differs only by
|
||||
# those directives is not perpetually flagged as outdated.
|
||||
norm_installed = _normalize_service_definition(
|
||||
_strip_optional_systemd_directives(installed)
|
||||
)
|
||||
norm_expected = _normalize_service_definition(
|
||||
_strip_optional_systemd_directives(expected)
|
||||
)
|
||||
return norm_installed == norm_expected
|
||||
|
||||
|
||||
def refresh_systemd_unit_if_needed(system: bool = False) -> bool:
|
||||
|
||||
Reference in New Issue
Block a user