service_manager.py 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  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 StartUnit(self, name: str, mode: str) -> jeepney.low_level.Message:
  37. return jeepney.new_method_call(
  38. remote_obj=self,
  39. method="StartUnit",
  40. signature="ss",
  41. body=(
  42. name,
  43. mode,
  44. ),
  45. )
  46. def StopUnit(self, name: str, mode: str) -> jeepney.low_level.Message:
  47. return jeepney.new_method_call(
  48. remote_obj=self,
  49. method="StopUnit",
  50. signature="ss",
  51. body=(
  52. name,
  53. mode,
  54. ),
  55. )
  56. def RestartUnit(self, name: str, mode: str) -> jeepney.low_level.Message:
  57. return jeepney.new_method_call(
  58. remote_obj=self,
  59. method="RestartUnit",
  60. signature="ss",
  61. body=(
  62. name,
  63. mode,
  64. ),
  65. )
  66. class Unit(systemctl_mqtt._dbus.Properties): # pylint: disable=protected-access
  67. """
  68. https://www.freedesktop.org/software/systemd/man/latest/org.freedesktop.systemd1.html#Unit%20Objects
  69. """
  70. # pylint: disable=too-few-public-methods
  71. interface = "org.freedesktop.systemd1.Unit"
  72. def __init__(self, *, object_path: str):
  73. super().__init__(object_path=object_path, bus_name="org.freedesktop.systemd1")
  74. # pylint: disable=invalid-name
  75. def start_unit(unit_name: str):
  76. proxy = get_service_manager_proxy()
  77. try:
  78. proxy.StartUnit(unit_name, "replace")
  79. _LOGGER.debug("Starting unit: %s", unit_name)
  80. # pylint: disable=broad-exception-caught
  81. except jeepney.wrappers.DBusErrorResponse as exc:
  82. _LOGGER.error("Failed to start unit: %s because %s ", unit_name, exc.name)
  83. def stop_unit(unit_name: str):
  84. proxy = get_service_manager_proxy()
  85. try:
  86. proxy.StopUnit(unit_name, "replace")
  87. _LOGGER.debug("Stopping unit: %s", unit_name)
  88. # pylint: disable=broad-exception-caught
  89. except jeepney.wrappers.DBusErrorResponse as exc:
  90. _LOGGER.error("Failed to stop unit: %s because %s ", unit_name, exc.name)
  91. def restart_unit(unit_name: str):
  92. proxy = get_service_manager_proxy()
  93. try:
  94. proxy.RestartUnit(unit_name, "replace")
  95. _LOGGER.debug("Restarting unit: %s", unit_name)
  96. # pylint: disable=broad-exception-caught
  97. except jeepney.wrappers.DBusErrorResponse as exc:
  98. _LOGGER.error("Failed to restart unit: %s because %s ", unit_name, exc.name)
  99. def get_service_manager_proxy() -> jeepney.io.blocking.Proxy:
  100. # https://jeepney.readthedocs.io/en/latest/integrate.html
  101. # https://gitlab.com/takluyver/jeepney/-/blob/master/examples/aio_notify.py
  102. return jeepney.io.blocking.Proxy(
  103. msggen=ServiceManager(),
  104. connection=jeepney.io.blocking.open_dbus_connection(
  105. bus="SYSTEM",
  106. ),
  107. )