Пример #1
0
    def __init__(self, parent, name, type_name, directory, settings,
                 *args, **kwargs):
        """The DBUS_INTERFACE_ROLE_INSTANCE implementation.

        :param parent: The DBusRole Object this is attached to
        :param name: Instance name
        :param type_name: Role name
        :param directory: FIXME: unused???
        :param settings: RoleSettings for the role
        :param path: (Implicit in *args) FIXME: unused???
        """
        super(RoleBase, self).__init__(*args, **kwargs)
        self._path = args[0]
        self._parent = parent
        self._name = name
        self._escaped_name = dbus_label_escape(name)
        self._type = type_name
        self._escaped_type = dbus_label_escape(type_name)
        self._log_prefix = "role.%s.%s" % (self._escaped_type,
                                           self._escaped_name)
        self._directory = directory
        self._settings = settings
        # TODO: place target_unit in settings
        self.target_unit = "role-%s-%s.target" % (self._type, self.get_name())

        # No loaded self._settings, set state to NASCENT
        if not "state" in self._settings:
            self._settings["state"] = NASCENT

        # Check role instance state if role instance is in READY_TO_START or
        # RUNNING state
        if self._settings["state"] in [ READY_TO_START, RUNNING ]:
            try:
                state = target_unit_state(self.target_unit)
            except Exception as e:
                log.error("Getting information about the unit target failed: %s", e)
            else:
                # Update state:
                #
                #  Old instance   | systemd unit | New instance
                #  state          | target state | state
                # ----------------+--------------+----------------
                #  RUNNING        | inactive     | READY_TO_START
                #  READY_TO_START | active       | RUNNING

                if state == "inactive" and self._settings["state"] == RUNNING:
                    log.warning("'%s' is inactive, moving to %s state.",
                                self.target_unit, READY_TO_START)
                    self.change_state(READY_TO_START, write=True)
                elif state == "active" and \
                     self._settings["state"] == READY_TO_START:
                    log.warning("'%s' is active, moving to %s state.",
                                self.target_unit, RUNNING)
                    self.change_state(RUNNING, write=True)

        self.timeout_restart()
Пример #2
0
    def cleanup_targets(self):
        # remove created target units
        if "target_unit" not in self._settings:
            return
        try:
            os.unlink(os.path.join(SYSTEMD_UNITS,
                                   self._settings['target_unit']))
        except Exception as e:
            log.warning(
                "Couldn't remove unit '{}': {!s}\n".format(
                    self._settings['target_unit'], e))
        else:
            log.debug9("Removed unit '{}'\n".format(self._settings['target_unit']))

        # tell systemd about it
        with SystemdJobHandler() as job_handler:
            job_handler.manager.Reload()
Пример #3
0
    def cleanup_targets(self):
        # remove created target units
        if "target_unit" not in self._settings:
            return
        try:
            os.unlink(
                os.path.join(SYSTEMD_UNITS, self._settings['target_unit']))
        except Exception as e:
            log.warning("Couldn't remove unit '{}': {!s}\n".format(
                self._settings['target_unit'], e))
        else:
            log.debug9("Removed unit '{}'\n".format(
                self._settings['target_unit']))

        # tell systemd about it
        with SystemdJobHandler() as job_handler:
            job_handler.manager.Reload()