service_manager.py 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181
  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 LoadUnit(self, name: str) -> jeepney.low_level.Message:
  37. return jeepney.new_method_call(
  38. remote_obj=self, method="LoadUnit", signature="s", body=(name,)
  39. )
  40. def StartUnit(self, name: str, mode: str) -> jeepney.low_level.Message:
  41. return jeepney.new_method_call(
  42. remote_obj=self,
  43. method="StartUnit",
  44. signature="ss",
  45. body=(
  46. name,
  47. mode,
  48. ),
  49. )
  50. def StopUnit(self, name: str, mode: str) -> jeepney.low_level.Message:
  51. return jeepney.new_method_call(
  52. remote_obj=self,
  53. method="StopUnit",
  54. signature="ss",
  55. body=(
  56. name,
  57. mode,
  58. ),
  59. )
  60. def RestartUnit(self, name: str, mode: str) -> jeepney.low_level.Message:
  61. return jeepney.new_method_call(
  62. remote_obj=self,
  63. method="RestartUnit",
  64. signature="ss",
  65. body=(
  66. name,
  67. mode,
  68. ),
  69. )
  70. class Unit(systemctl_mqtt._dbus.Properties): # pylint: disable=protected-access
  71. """
  72. https://www.freedesktop.org/software/systemd/man/latest/org.freedesktop.systemd1.html#Unit%20Objects
  73. """
  74. # pylint: disable=too-few-public-methods
  75. interface = "org.freedesktop.systemd1.Unit"
  76. def __init__(self, *, object_path: str):
  77. super().__init__(object_path=object_path, bus_name="org.freedesktop.systemd1")
  78. # pylint: disable=invalid-name
  79. def start_unit(unit_name: str):
  80. proxy = get_service_manager_proxy()
  81. try:
  82. proxy.StartUnit(unit_name, "replace")
  83. _LOGGER.debug("Starting unit: %s", unit_name)
  84. # pylint: disable=broad-exception-caught
  85. except jeepney.wrappers.DBusErrorResponse as exc:
  86. _LOGGER.error("Failed to start unit: %s because %s ", unit_name, exc.name)
  87. def stop_unit(unit_name: str):
  88. proxy = get_service_manager_proxy()
  89. try:
  90. proxy.StopUnit(unit_name, "replace")
  91. _LOGGER.debug("Stopping unit: %s", unit_name)
  92. # pylint: disable=broad-exception-caught
  93. except jeepney.wrappers.DBusErrorResponse as exc:
  94. _LOGGER.error("Failed to stop unit: %s because %s ", unit_name, exc.name)
  95. def restart_unit(unit_name: str):
  96. proxy = get_service_manager_proxy()
  97. try:
  98. proxy.RestartUnit(unit_name, "replace")
  99. _LOGGER.debug("Restarting unit: %s", unit_name)
  100. # pylint: disable=broad-exception-caught
  101. except jeepney.wrappers.DBusErrorResponse as exc:
  102. _LOGGER.error("Failed to restart unit: %s because %s ", unit_name, exc.name)
  103. def isolate_unit(unit_name: str):
  104. proxy = get_service_manager_proxy()
  105. try:
  106. proxy.StartUnit(unit_name, "isolate")
  107. _LOGGER.debug("Isolating unit: %s", unit_name)
  108. # pylint: disable=broad-exception-caught
  109. except jeepney.wrappers.DBusErrorResponse as exc:
  110. _LOGGER.error("Failed to isolate unit: %s because %s ", unit_name, exc.name)
  111. def is_isolate_unit_allowed(unit_name: str) -> bool:
  112. if (unit_proxy := _get_unit_proxy(unit_name=unit_name)) is None:
  113. return False
  114. try:
  115. ((_, allowed),) = unit_proxy.Get("AllowIsolate")
  116. _LOGGER.debug("AllowIsolate for %s = %s", unit_name, allowed)
  117. return bool(allowed)
  118. # pylint: disable=broad-exception-caught
  119. except jeepney.wrappers.DBusErrorResponse as exc:
  120. _LOGGER.error(
  121. "Failed to get AllowIsolate property of unit %s because %s",
  122. unit_name,
  123. exc.name,
  124. )
  125. return False
  126. def _get_connection() -> jeepney.io.blocking.DBusConnection:
  127. return jeepney.io.blocking.open_dbus_connection(bus="SYSTEM")
  128. def _get_unit_proxy(unit_name: str) -> jeepney.io.blocking.Proxy | None:
  129. connection = _get_connection()
  130. proxy = jeepney.io.blocking.Proxy(msggen=ServiceManager(), connection=connection)
  131. try:
  132. (unit_path,) = proxy.LoadUnit(name=unit_name)
  133. # pylint: disable=broad-exception-caught
  134. except jeepney.wrappers.DBusErrorResponse as exc:
  135. _LOGGER.error("Failed to load unit: %s because %s", unit_name, exc.name)
  136. return None
  137. return jeepney.io.blocking.Proxy(
  138. msggen=Unit(object_path=unit_path), connection=connection
  139. )
  140. def get_service_manager_proxy() -> jeepney.io.blocking.Proxy:
  141. # https://jeepney.readthedocs.io/en/latest/integrate.html
  142. # https://gitlab.com/takluyver/jeepney/-/blob/master/examples/aio_notify.py
  143. return jeepney.io.blocking.Proxy(
  144. msggen=ServiceManager(), connection=_get_connection()
  145. )