Don't fail on systemd disable failure

This commit is contained in:
Kivi Kaitaniemi
2026-01-07 00:03:57 +02:00
parent a5ec25693e
commit 8ca2ca8a06
2 changed files with 13 additions and 68 deletions
@@ -151,15 +151,6 @@ class Systemd(plugins.Plugin):
user_units_to_disable[user].add(unit) user_units_to_disable[user].add(unit)
try: try:
output.print_info("Reloading systemd daemon.")
if not dry_run:
self.reload_daemon()
output.print_info("Reloading systemd daemon for users.")
if not dry_run:
for user in user_units_to_enable.keys() | user_units_to_disable.keys():
self.reload_user_daemon(user)
output.print_list("Enabling systemd units:", list(units_to_enable)) output.print_list("Enabling systemd units:", list(units_to_enable))
if not dry_run: if not dry_run:
self.enable_units(store, units_to_enable) self.enable_units(store, units_to_enable)
@@ -177,6 +168,15 @@ class Systemd(plugins.Plugin):
output.print_list(f"Disabling systemd units for {user}:", list(units)) output.print_list(f"Disabling systemd units for {user}:", list(units))
if not dry_run: if not dry_run:
self.disable_user_units(store, units, user) self.disable_user_units(store, units, user)
output.print_info("Reloading systemd daemon.")
if not dry_run:
self.reload_daemon()
output.print_info("Reloading systemd daemon for users.")
if not dry_run:
for user in user_units_to_enable.keys() | user_units_to_disable.keys():
self.reload_user_daemon(user)
except errors.CommandFailedError as error: except errors.CommandFailedError as error:
output.print_error("Running a systemd command failed.") output.print_error("Running a systemd command failed.")
output.print_error(str(error)) output.print_error(str(error))
@@ -206,7 +206,7 @@ class Systemd(plugins.Plugin):
return return
cmd = self.commands.disable_units(units) cmd = self.commands.disable_units(units)
command.prg(cmd, pty=config.debug_output) command.prg(cmd, pty=config.debug_output, check=False)
store["systemd_units"] -= units store["systemd_units"] -= units
@@ -217,6 +217,7 @@ class Systemd(plugins.Plugin):
if not units: if not units:
return return
# Use check=False to avoid issues when units don't exist
cmd = self.commands.enable_user_units(units, user) cmd = self.commands.enable_user_units(units, user)
command.prg(cmd, pty=config.debug_output) command.prg(cmd, pty=config.debug_output)
@@ -230,8 +231,9 @@ class Systemd(plugins.Plugin):
if not units: if not units:
return return
# Use check=False to avoid issues when units don't exist
cmd = self.commands.disable_user_units(units, user) cmd = self.commands.disable_user_units(units, user)
command.prg(cmd, pty=config.debug_output) command.prg(cmd, pty=config.debug_output, check=False)
store["systemd_user_units"].setdefault(user, set()) store["systemd_user_units"].setdefault(user, set())
store["systemd_user_units"][user] -= units store["systemd_user_units"][user] -= units
@@ -220,20 +220,6 @@ def test_enable_units_success(monkeypatch, store, systemd):
assert store["systemd_units"] == {"old.service", "new.service"} assert store["systemd_units"] == {"old.service", "new.service"}
def test_enable_units_failure_does_not_update_store(monkeypatch, store, systemd):
store["systemd_units"] = {"old.service"}
def fake_run(cmd, **kwargs):
return 1, "error"
monkeypatch.setattr(systemd_mod.command, "run", fake_run)
with pytest.raises(systemd_mod.errors.CommandFailedError):
systemd.enable_units(store, {"new.service"})
# unchanged
assert store["systemd_units"] == {"old.service"}
def test_disable_units_success(monkeypatch, store, systemd): def test_disable_units_success(monkeypatch, store, systemd):
store["systemd_units"] = {"old.service", "new.service"} store["systemd_units"] = {"old.service", "new.service"}
@@ -249,19 +235,6 @@ def test_disable_units_success(monkeypatch, store, systemd):
assert store["systemd_units"] == {"old.service"} assert store["systemd_units"] == {"old.service"}
def test_disable_units_failure_does_not_update_store(monkeypatch, store, systemd):
store["systemd_units"] = {"old.service", "new.service"}
def fake_run(cmd, **kwargs):
return 1, "error"
monkeypatch.setattr(systemd_mod.command, "run", fake_run)
with pytest.raises(systemd_mod.errors.CommandFailedError):
systemd.disable_units(store, {"new.service"})
assert store["systemd_units"] == {"old.service", "new.service"}
def test_enable_user_units_success(monkeypatch, store, systemd): def test_enable_user_units_success(monkeypatch, store, systemd):
store["systemd_user_units"] = {"alice": {"olduser.service"}} store["systemd_user_units"] = {"alice": {"olduser.service"}}
@@ -281,19 +254,6 @@ def test_enable_user_units_success(monkeypatch, store, systemd):
} }
def test_enable_user_units_failure_does_not_update_store(monkeypatch, store, systemd):
store["systemd_user_units"] = {"alice": {"olduser.service"}}
def fake_run(cmd, **kwargs):
return 1, "error"
monkeypatch.setattr(systemd_mod.command, "run", fake_run)
with pytest.raises(systemd_mod.errors.CommandFailedError):
systemd.enable_user_units(store, {"newuser.service"}, "alice")
assert store["systemd_user_units"]["alice"] == {"olduser.service"}
def test_disable_user_units_success(monkeypatch, store, systemd): def test_disable_user_units_success(monkeypatch, store, systemd):
store["systemd_user_units"] = {"alice": {"olduser.service", "newuser.service"}} store["systemd_user_units"] = {"alice": {"olduser.service", "newuser.service"}}
@@ -310,23 +270,6 @@ def test_disable_user_units_success(monkeypatch, store, systemd):
assert store["systemd_user_units"]["alice"] == {"olduser.service"} assert store["systemd_user_units"]["alice"] == {"olduser.service"}
def test_disable_user_units_failure_does_not_update_store(monkeypatch, store, systemd):
store["systemd_user_units"] = {"alice": {"olduser.service", "newuser.service"}}
def fake_run(cmd, **kwargs):
return 1, "error"
monkeypatch.setattr(systemd_mod.command, "run", fake_run)
with pytest.raises(systemd_mod.errors.CommandFailedError):
systemd.disable_user_units(store, {"newuser.service"}, "alice")
assert store["systemd_user_units"]["alice"] == {
"olduser.service",
"newuser.service",
}
def test_reload_daemon_uses_command_run(monkeypatch, systemd): def test_reload_daemon_uses_command_run(monkeypatch, systemd):
called = {} called = {}