service_manager.py 3.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. # systemctl-mqtt - MQTT client triggering & reporting shutdown on systemd-based systems
  2. #
  3. # Copyright (C) 2024 Fabian Peter Hammerle <fabian@hammerle.me>
  4. #
  5. # This program is free software: you can redistribute it and/or modify
  6. # it under the terms of the GNU General Public License as published by
  7. # the Free Software Foundation, either version 3 of the License, or
  8. # any later version.
  9. #
  10. # This program is distributed in the hope that it will be useful,
  11. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. # GNU General Public License for more details.
  14. #
  15. # You should have received a copy of the GNU General Public License
  16. # along with this program. If not, see <https://www.gnu.org/licenses/>.
  17. import logging
  18. import jeepney
  19. import systemctl_mqtt._dbus
  20. _LOGGER = logging.getLogger(__name__)
  21. class ServiceManager(jeepney.MessageGenerator):
  22. """
  23. https://www.freedesktop.org/software/systemd/man/latest/org.freedesktop.systemd1.html
  24. """
  25. # pylint: disable=too-few-public-methods
  26. interface = "org.freedesktop.systemd1.Manager"
  27. def __init__(self):
  28. super().__init__(
  29. object_path="/org/freedesktop/systemd1", bus_name="org.freedesktop.systemd1"
  30. )
  31. # pylint: disable=invalid-name
  32. def GetUnit(self, name: str) -> jeepney.low_level.Message:
  33. return jeepney.new_method_call(
  34. remote_obj=self, method="GetUnit", signature="s", body=(name,)
  35. )
  36. def RestartUnit(self, name: str, mode: str) -> jeepney.low_level.Message:
  37. return jeepney.new_method_call(
  38. remote_obj=self,
  39. method="RestartUnit",
  40. signature="ss",
  41. body=(
  42. name,
  43. mode,
  44. ),
  45. )
  46. class Unit(systemctl_mqtt._dbus.Properties): # pylint: disable=protected-access
  47. """
  48. https://www.freedesktop.org/software/systemd/man/latest/org.freedesktop.systemd1.html#Unit%20Objects
  49. """
  50. # pylint: disable=too-few-public-methods
  51. interface = "org.freedesktop.systemd1.Unit"
  52. def __init__(self, *, object_path: str):
  53. super().__init__(object_path=object_path, bus_name="org.freedesktop.systemd1")
  54. # pylint: disable=invalid-name
  55. def restart_unit(unit_name: str):
  56. proxy = get_service_manager_proxy()
  57. try:
  58. proxy.RestartUnit(unit_name, "replace")
  59. _LOGGER.debug("Restarting unit: %s", unit_name)
  60. # pylint: disable=broad-exception-caught
  61. except jeepney.wrappers.DBusErrorResponse as exc:
  62. _LOGGER.error("Failed to restart unit: %s because %s ", unit_name, exc.name)
  63. def get_service_manager_proxy() -> jeepney.io.blocking.Proxy:
  64. # https://jeepney.readthedocs.io/en/latest/integrate.html
  65. # https://gitlab.com/takluyver/jeepney/-/blob/master/examples/aio_notify.py
  66. return jeepney.io.blocking.Proxy(
  67. msggen=ServiceManager(),
  68. connection=jeepney.io.blocking.open_dbus_connection(
  69. bus="SYSTEM",
  70. ),
  71. )