1
0
mirror of https://github.com/altlinux/gpupdate.git synced 2025-03-21 18:50:38 +03:00

systemd applier: perform permanent enable/disable and also check state

This commit is contained in:
Игорь Чудов 2019-12-13 01:16:43 +04:00
parent 788877b1ad
commit 2d9ea4d8ca
Signed by untrusted user: nir
GPG Key ID: 0F3883600CAE7AAC

View File

@ -1,8 +1,6 @@
import dbus
import logging
logging.basicConfig(level=logging.DEBUG)
class systemd_unit:
__system_bus = dbus.SystemBus()
__systemd_dbus = __system_bus.get_object('org.freedesktop.systemd1', '/org/freedesktop/systemd1')
@ -11,19 +9,29 @@ class systemd_unit:
def __init__(self, unit_name, state):
self.unit_name = unit_name
self.desired_state = state
self.unit = self.__manager.LoadUnit(self.unit_name)
self.unit = self.__manager.LoadUnit(dbus.String(self.unit_name))
self.unit_proxy = self.__system_bus.get_object('org.freedesktop.systemd1', str(self.unit))
self.unit_interface = dbus.Interface(self.unit_proxy, dbus_interface='org.freedesktop.systemd1.Unit')
self.unit_properties = dbus.Interface(self.unit_proxy, dbus_interface='org.freedesktop.DBus.Properties')
def apply(self):
if self.desired_state == 1:
self.__manager.UnmaskUnitFiles([self.unit_name], dbus.Boolean(True))
self.__manager.EnableUnitFiles([self.unit_name], dbus.Boolean(True), dbus.Boolean(True))
self.__manager.UnmaskUnitFiles([self.unit_name], dbus.Boolean(False))
self.__manager.EnableUnitFiles([self.unit_name], dbus.Boolean(False), dbus.Boolean(True))
self.__manager.StartUnit(self.unit_name, 'replace')
logging.info('Starting systemd unit: {}'.format(self.unit_name))
if self._get_state() != 'active':
logging.error('Unable to start systemd unit {}'.format(self.unit_name))
else:
self.__manager.MaskUnitFiles([self.unit_name], dbus.Boolean(True), dbus.Boolean(True))
self.__manager.DisableUnitFiles([self.unit_name], dbus.Boolean(True))
self.__manager.StopUnit(self.unit_name, 'replace')
self.__manager.DisableUnitFiles([self.unit_name], dbus.Boolean(False))
self.__manager.MaskUnitFiles([self.unit_name], dbus.Boolean(False), dbus.Boolean(True))
logging.info('Stopping systemd unit: {}'.format(self.unit_name))
if self._get_state() != 'stopped':
logging.error('Unable to stop systemd unit {}'.format(self.unit_name))
def _get_state(self):
'''
Get the string describing service state.
'''
return self.unit_properties.Get('org.freedesktop.systemd1.Unit', 'ActiveState')