예제 #1
0
    def test_persist_cascades(self):
        config = Config(self.parser, auto_persist=False)
        self.assertEqual("baz", config["foo"]["quux"])
        config["foo"]["quux"] = "fizz"
        config.persist()
        self.assertEqual("fizz", config["foo"]["quux"])

        reparsed = RhsmConfigParser(self.fid.name)
        self.assertEqual("fizz", reparsed.get("foo", "quux"))
예제 #2
0
    def test_persist_cascades(self):
        config = Config(self.parser, auto_persist=False)
        self.assertEqual('baz', config['foo']['quux'])
        config['foo']['quux'] = 'fizz'
        config.persist()
        self.assertEqual('fizz', config['foo']['quux'])

        reparsed = RhsmConfigParser(self.fid.name)
        self.assertEqual('fizz', reparsed.get('foo', 'quux'))
예제 #3
0
 def __init__(self,
              conn=None,
              object_path=None,
              bus_name=None,
              parser=None):
     self.config = Config(parser)
     super(ConfigDBusObject, self).__init__(conn=conn,
                                            object_path=object_path,
                                            bus_name=bus_name)
예제 #4
0
    def test_persist_cascades(self):
        config = Config(self.parser, auto_persist=False)
        self.assertEqual('baz', config['foo']['quux'])
        config['foo']['quux'] = 'fizz'
        config.persist()
        self.assertEqual('fizz', config['foo']['quux'])

        reparsed = RhsmConfigParser(self.fid.name)
        self.assertEqual('fizz', reparsed.get('foo', 'quux'))
예제 #5
0
class ConfigDBusObject(base_object.BaseObject):
    default_dbus_path = constants.CONFIG_DBUS_PATH
    interface_name = constants.CONFIG_INTERFACE

    def __init__(self,
                 conn=None,
                 object_path=None,
                 bus_name=None,
                 parser=None):
        self.config = Config(parser)
        super(ConfigDBusObject, self).__init__(conn=conn,
                                               object_path=object_path,
                                               bus_name=bus_name)

    @util.dbus_service_method(constants.CONFIG_INTERFACE, in_signature='sv')
    @util.dbus_handle_exceptions
    def Set(self, property_name, new_value, sender=None):
        property_name = dbus_utils.dbus_to_python(property_name, str)
        new_value = dbus_utils.dbus_to_python(new_value, str)
        section, _dot, property_name = property_name.partition('.')

        if not property_name:
            raise DBusException(
                "Setting an entire section is not supported.  Use 'section.property' format."
            )

        self.config[section][property_name] = new_value
        self.config.persist()

    @util.dbus_service_method(constants.CONFIG_INTERFACE,
                              in_signature='',
                              out_signature='a{sv}')
    @util.dbus_handle_exceptions
    def GetAll(self, sender=None):
        d = dbus.Dictionary({}, signature='sv')
        for k, v in six.iteritems(self.config):
            d[k] = dbus.Dictionary({}, signature='ss')
            for kk, vv in six.iteritems(v):
                d[k][kk] = vv

        return d

    @util.dbus_service_method(constants.CONFIG_INTERFACE,
                              in_signature='s',
                              out_signature='v')
    @util.dbus_handle_exceptions
    def Get(self, property_name, sender=None):
        section, _dot, property_name = property_name.partition('.')

        if property_name:
            return self.config[section][property_name]
        else:
            return dbus.Dictionary(self.config[section], signature='sv')
예제 #6
0
    def SetAll(self, configuration, locale, sender=None):
        """
        Method for setting multiple configuration options. Of course all of them could be set.
        :param configuration: d-bus dictionary with configuration. Keys have to include section. e.g.
                              server.hostname. Configuration file is saved to the file at the end of method.
        :param locale: string with locale
        :param sender: not used
        :return: None
        """
        configuration = dbus_utils.dbus_to_python(configuration,
                                                  expected_type=dict)
        locale = dbus_utils.dbus_to_python(locale, expected_type=str)
        Locale.set(locale)

        log.debug("Setting new configuration values: %s" % str(configuration))

        logging_changed = False

        for property_name, new_value in configuration.items():
            section_name, _dot, property_name = property_name.partition(".")

            if not property_name:
                raise DBusException(
                    "Setting an entire section is not supported.  Use 'section.property' format."
                )

            self.config[section_name][property_name] = new_value

            if section_name == "logging":
                logging_changed = True

        # Try to temporary disable dir watcher, because 'self.config.persist()' writes configuration
        # file and it would trigger file system monitor callback function and saved values would be
        # read again. It can cause race conditions, when SetAll() is called multiple times
        Server.temporary_disable_dir_watchers({CONFIG_WATCHER})

        # Write new config value to configuration file
        self.config.persist()

        Server.enable_dir_watchers({CONFIG_WATCHER})

        # When anything in logging section was just changed, then we have to re-initialize logger
        if logging_changed is True:
            parser = rhsm.config.get_config_parser()
            self.config = Config(parser)
            rhsm.logutil.init_logger(parser)
예제 #7
0
    def test_auto_persist(self):
        config = Config(self.parser, auto_persist=True)
        self.assertEqual("baz", config["foo"]["quux"])
        config["foo"]["quux"] = "fizz"
        self.assertEqual("fizz", config["foo"]["quux"])

        reparsed = RhsmConfigParser(self.fid.name)
        self.assertEqual("fizz", reparsed.get("foo", "quux"))
예제 #8
0
    def Set(self, property_name, new_value, locale, sender=None):
        """
        Method used for setting only one value. When more than one value is going to be set, then it is
        strongly recomended to use method SetAll(), because configuration is saved to the configuration
        file at the end of method Set()
        :param property_name: string with property e.g. server.hostname
        :param new_value: string with new value
        :param locale: string with locale
        :param sender: not used
        :return: None
        """
        property_name = dbus_utils.dbus_to_python(property_name,
                                                  expected_type=str)
        new_value = dbus_utils.dbus_to_python(new_value, expected_type=str)
        locale = dbus_utils.dbus_to_python(locale, expected_type=str)
        Locale.set(locale)

        section, _dot, property_name = property_name.partition(".")

        if not property_name:
            raise DBusException(
                "Setting an entire section is not supported.  Use 'section.property' format."
            )

        self.config[section][property_name] = new_value

        if section == "logging":
            logging_changed = True
        else:
            logging_changed = False

        # Try to temporary disable dir watcher, because 'self.config.persist()' writes configuration
        # file and it would trigger file system monitor callback function and saved values would be
        # read again. It can cause race conditions, when Set() is called multiple times
        Server.temporary_disable_dir_watchers({CONFIG_WATCHER})

        # Write new config value to configuration file
        self.config.persist()

        Server.enable_dir_watchers({CONFIG_WATCHER})

        # When anything in logging section was just chnaged, then we have to re-initialize logger
        if logging_changed is True:
            parser = rhsm.config.get_config_parser()
            self.config = Config(parser)
            rhsm.logutil.init_logger(parser)
예제 #9
0
    def test_auto_persist(self):
        config = Config(self.parser, auto_persist=True)
        self.assertEqual('baz', config['foo']['quux'])
        config['foo']['quux'] = 'fizz'
        self.assertEqual('fizz', config['foo']['quux'])

        reparsed = RhsmConfigParser(self.fid.name)
        self.assertEqual('fizz', reparsed.get('foo', 'quux'))
예제 #10
0
    def reload(self):
        """
        This callback method is called, when i-notify or periodical directory polling detects
        any change of rhsm.conf file. Thus configuration file is reloaded and new values are used.
        """
        parser = rhsm.config.get_config_parser()

        # We are going to read configuration file again, but we have to clean all data in parser object
        # this way, because iniparse module doesn't provide better method to do that.
        parser.data = ini.INIConfig(None, optionxformsource=parser)

        # We have to read parser again to get fresh data from the file
        files_read = parser.read()

        if len(files_read) > 0:
            log.debug("files read: %s" % str(files_read))
            self.config = Config(parser)
            rhsm.logutil.init_logger(parser)
            log.debug("Configuration file: %s reloaded: %s" %
                      (parser.config_file, str(self.config)))
        else:
            log.warning("Unable to read configuration file: %s" %
                        parser.config_file)
예제 #11
0
 def test_does_not_auto_persist_by_default(self):
     config = Config(self.parser, auto_persist=False)
     config["foo"] = {"hello": "world"}
     reparsed = RhsmConfigParser(self.fid.name)
     self.assertEqual("baz", reparsed.get("foo", "quux"))
     self.assertRaises(NoOptionError, reparsed.get, "foo", "hello")
예제 #12
0
 def test_auto_persists(self):
     config = Config(self.parser, auto_persist=True)
     config["foo"] = {"hello": "world"}
     reparsed = RhsmConfigParser(self.fid.name)
     self.assertEqual("world", reparsed.get("foo", "hello"))
     self.assertRaises(NoOptionError, reparsed.get, "foo", "quux")
예제 #13
0
class ConfigDBusObject(base_object.BaseObject):
    default_dbus_path = constants.CONFIG_DBUS_PATH
    interface_name = constants.CONFIG_INTERFACE

    def __init__(self, conn=None, object_path=None, bus_name=None, parser=None):
        self.config = Config(parser)
        super(ConfigDBusObject, self).__init__(conn=conn, object_path=object_path, bus_name=bus_name)

    @util.dbus_service_signal(
        constants.CONFIG_INTERFACE,
        signature=''
    )
    @util.dbus_handle_exceptions
    def ConfigChanged(self):
        """
        Signal fired, when config is created/deleted/changed
        :return: None
        """
        log.debug("D-Bus signal %s emitted" % constants.CONFIG_INTERFACE)
        return None

    @util.dbus_service_method(
        constants.CONFIG_INTERFACE,
        in_signature='svs')
    @util.dbus_handle_exceptions
    def Set(self, property_name, new_value, locale, sender=None):
        property_name = dbus_utils.dbus_to_python(property_name, expected_type=str)
        new_value = dbus_utils.dbus_to_python(new_value, expected_type=str)
        locale = dbus_utils.dbus_to_python(locale, expected_type=str)
        Locale.set(locale)

        section, _dot, property_name = property_name.partition('.')

        if not property_name:
            raise DBusException("Setting an entire section is not supported.  Use 'section.property' format.")

        self.config[section][property_name] = new_value
        self.config.persist()

    @util.dbus_service_method(
        constants.CONFIG_INTERFACE,
        in_signature='s',
        out_signature='a{sv}')
    @util.dbus_handle_exceptions
    def GetAll(self, locale, sender=None):
        locale = dbus_utils.dbus_to_python(locale, expected_type=str)
        Locale.set(locale)

        d = dbus.Dictionary({}, signature='sv')
        for k, v in six.iteritems(self.config):
            d[k] = dbus.Dictionary({}, signature='ss')
            for kk, vv in six.iteritems(v):
                d[k][kk] = vv

        return d

    @util.dbus_service_method(
        constants.CONFIG_INTERFACE,
        in_signature='ss',
        out_signature='v')
    @util.dbus_handle_exceptions
    def Get(self, property_name, locale, sender=None):
        locale = dbus_utils.dbus_to_python(locale, expected_type=str)
        Locale.set(locale)

        section, _dot, property_name = property_name.partition('.')

        if property_name:
            return self.config[section][property_name]
        else:
            return dbus.Dictionary(self.config[section], signature='sv')

    def reload(self):
        parser = rhsm.config.initConfig("/etc/rhsm/rhsm.conf")
        self.config = Config(parser)
        log.debug("port: %s" % str(self.config["server"]["port"]))
        log.debug("reloaded successfully")
예제 #14
0
 def __init__(self, conn=None, object_path=None, bus_name=None, parser=None):
     self.config = Config(parser)
     super(ConfigDBusObject, self).__init__(conn=conn, object_path=object_path, bus_name=bus_name)
예제 #15
0
 def reload(self):
     parser = rhsm.config.initConfig("/etc/rhsm/rhsm.conf")
     self.config = Config(parser)
     log.debug("port: %s" % str(self.config["server"]["port"]))
     log.debug("reloaded successfully")
예제 #16
0
 def test_does_not_auto_persist_by_default(self):
     config = Config(self.parser, auto_persist=False)
     config['foo'] = {'hello': 'world'}
     reparsed = RhsmConfigParser(self.fid.name)
     self.assertEqual('baz', reparsed.get('foo', 'quux'))
     self.assertRaises(NoOptionError, reparsed.get, 'foo', 'hello')
예제 #17
0
 def reload(self):
     parser = rhsm.config.initConfig("/etc/rhsm/rhsm.conf")
     self.config = Config(parser)
     log.debug("port: %s" % str(self.config["server"]["port"]))
     log.debug("reloaded successfully")
예제 #18
0
class ConfigDBusObject(base_object.BaseObject):
    default_dbus_path = constants.CONFIG_DBUS_PATH
    interface_name = constants.CONFIG_INTERFACE

    def __init__(self,
                 conn=None,
                 object_path=None,
                 bus_name=None,
                 parser=None):
        self.config = Config(parser)
        super(ConfigDBusObject, self).__init__(conn=conn,
                                               object_path=object_path,
                                               bus_name=bus_name)

    @util.dbus_service_signal(constants.CONFIG_INTERFACE, signature='')
    @util.dbus_handle_exceptions
    def ConfigChanged(self):
        """
        Signal fired, when config is created/deleted/changed
        :return: None
        """
        log.debug("D-Bus signal %s emitted" % constants.CONFIG_INTERFACE)
        return None

    @util.dbus_service_method(constants.CONFIG_INTERFACE, in_signature='svs')
    @util.dbus_handle_exceptions
    def Set(self, property_name, new_value, locale, sender=None):
        property_name = dbus_utils.dbus_to_python(property_name,
                                                  expected_type=str)
        new_value = dbus_utils.dbus_to_python(new_value, expected_type=str)
        locale = dbus_utils.dbus_to_python(locale, expected_type=str)
        Locale.set(locale)

        section, _dot, property_name = property_name.partition('.')

        if not property_name:
            raise DBusException(
                "Setting an entire section is not supported.  Use 'section.property' format."
            )

        self.config[section][property_name] = new_value
        self.config.persist()

    @util.dbus_service_method(constants.CONFIG_INTERFACE,
                              in_signature='s',
                              out_signature='a{sv}')
    @util.dbus_handle_exceptions
    def GetAll(self, locale, sender=None):
        locale = dbus_utils.dbus_to_python(locale, expected_type=str)
        Locale.set(locale)

        d = dbus.Dictionary({}, signature='sv')
        for k, v in six.iteritems(self.config):
            d[k] = dbus.Dictionary({}, signature='ss')
            for kk, vv in six.iteritems(v):
                d[k][kk] = vv

        return d

    @util.dbus_service_method(constants.CONFIG_INTERFACE,
                              in_signature='ss',
                              out_signature='v')
    @util.dbus_handle_exceptions
    def Get(self, property_name, locale, sender=None):
        locale = dbus_utils.dbus_to_python(locale, expected_type=str)
        Locale.set(locale)

        section, _dot, property_name = property_name.partition('.')

        if property_name:
            return self.config[section][property_name]
        else:
            return dbus.Dictionary(self.config[section], signature='sv')

    def reload(self):
        parser = rhsm.config.initConfig("/etc/rhsm/rhsm.conf")
        self.config = Config(parser)
        log.debug("port: %s" % str(self.config["server"]["port"]))
        log.debug("reloaded successfully")
예제 #19
0
class ConfigDBusObject(base_object.BaseObject):
    default_dbus_path = constants.CONFIG_DBUS_PATH
    interface_name = constants.CONFIG_INTERFACE

    def __init__(self,
                 conn=None,
                 object_path=None,
                 bus_name=None,
                 parser=None):
        self.config = Config(parser)
        super(ConfigDBusObject, self).__init__(conn=conn,
                                               object_path=object_path,
                                               bus_name=bus_name)

    @util.dbus_service_signal(
        constants.CONFIG_INTERFACE,
        signature="",
    )
    @util.dbus_handle_exceptions
    def ConfigChanged(self):
        """
        Signal fired, when config is created/deleted/changed
        :return: None
        """
        log.debug("D-Bus signal %s emitted" % constants.CONFIG_INTERFACE)
        return None

    @util.dbus_service_method(
        constants.CONFIG_INTERFACE,
        in_signature="svs",
    )
    @util.dbus_handle_sender
    @util.dbus_handle_exceptions
    def Set(self, property_name, new_value, locale, sender=None):
        """
        Method used for setting only one value. When more than one value is going to be set, then it is
        strongly recomended to use method SetAll(), because configuration is saved to the configuration
        file at the end of method Set()
        :param property_name: string with property e.g. server.hostname
        :param new_value: string with new value
        :param locale: string with locale
        :param sender: not used
        :return: None
        """
        property_name = dbus_utils.dbus_to_python(property_name,
                                                  expected_type=str)
        new_value = dbus_utils.dbus_to_python(new_value, expected_type=str)
        locale = dbus_utils.dbus_to_python(locale, expected_type=str)
        Locale.set(locale)

        section, _dot, property_name = property_name.partition(".")

        if not property_name:
            raise DBusException(
                "Setting an entire section is not supported.  Use 'section.property' format."
            )

        self.config[section][property_name] = new_value

        if section == "logging":
            logging_changed = True
        else:
            logging_changed = False

        # Try to temporary disable dir watcher, because 'self.config.persist()' writes configuration
        # file and it would trigger file system monitor callback function and saved values would be
        # read again. It can cause race conditions, when Set() is called multiple times
        Server.temporary_disable_dir_watchers({CONFIG_WATCHER})

        # Write new config value to configuration file
        self.config.persist()

        Server.enable_dir_watchers({CONFIG_WATCHER})

        # When anything in logging section was just chnaged, then we have to re-initialize logger
        if logging_changed is True:
            parser = rhsm.config.get_config_parser()
            self.config = Config(parser)
            rhsm.logutil.init_logger(parser)

    @util.dbus_service_method(
        constants.CONFIG_INTERFACE,
        in_signature="a{sv}s",
    )
    @util.dbus_handle_sender
    @util.dbus_handle_exceptions
    def SetAll(self, configuration, locale, sender=None):
        """
        Method for setting multiple configuration options. Of course all of them could be set.
        :param configuration: d-bus dictionary with configuration. Keys have to include section. e.g.
                              server.hostname. Configuration file is saved to the file at the end of method.
        :param locale: string with locale
        :param sender: not used
        :return: None
        """
        configuration = dbus_utils.dbus_to_python(configuration,
                                                  expected_type=dict)
        locale = dbus_utils.dbus_to_python(locale, expected_type=str)
        Locale.set(locale)

        log.debug("Setting new configuration values: %s" % str(configuration))

        logging_changed = False

        for property_name, new_value in configuration.items():
            section_name, _dot, property_name = property_name.partition(".")

            if not property_name:
                raise DBusException(
                    "Setting an entire section is not supported.  Use 'section.property' format."
                )

            self.config[section_name][property_name] = new_value

            if section_name == "logging":
                logging_changed = True

        # Try to temporary disable dir watcher, because 'self.config.persist()' writes configuration
        # file and it would trigger file system monitor callback function and saved values would be
        # read again. It can cause race conditions, when SetAll() is called multiple times
        Server.temporary_disable_dir_watchers({CONFIG_WATCHER})

        # Write new config value to configuration file
        self.config.persist()

        Server.enable_dir_watchers({CONFIG_WATCHER})

        # When anything in logging section was just changed, then we have to re-initialize logger
        if logging_changed is True:
            parser = rhsm.config.get_config_parser()
            self.config = Config(parser)
            rhsm.logutil.init_logger(parser)

    @util.dbus_service_method(
        constants.CONFIG_INTERFACE,
        in_signature="s",
        out_signature="a{sv}",
    )
    @util.dbus_handle_sender
    @util.dbus_handle_exceptions
    def GetAll(self, locale, sender=None):
        """
        Method for getting whole configuration
        :param locale: string with locale
        :param sender: not used
        :return: D-bus dictionary with configuration
        """
        locale = dbus_utils.dbus_to_python(locale, expected_type=str)
        Locale.set(locale)

        d = dbus.Dictionary({}, signature="sv")
        for k, v in self.config.items():
            d[k] = dbus.Dictionary({}, signature="ss")
            for kk, vv in v.items():
                d[k][kk] = vv

        return d

    @util.dbus_service_method(
        constants.CONFIG_INTERFACE,
        in_signature="ss",
        out_signature="v",
    )
    @util.dbus_handle_sender
    @util.dbus_handle_exceptions
    def Get(self, property_name, locale, sender=None):
        """
        D-Bus method for getting one configuration property or one section
        :param property_name: string with name of property e.g. server.hostname or section e.g. server
        :param locale: string with locale
        :param sender: not used
        :return: string with value of property or dictionary with dictionary of one section
        """
        locale = dbus_utils.dbus_to_python(locale, expected_type=str)
        Locale.set(locale)

        section, _dot, property_name = property_name.partition(".")

        if property_name:
            return self.config[section][property_name]
        else:
            return dbus.Dictionary(self.config[section], signature="sv")

    def reload(self):
        """
        This callback method is called, when i-notify or periodical directory polling detects
        any change of rhsm.conf file. Thus configuration file is reloaded and new values are used.
        """
        parser = rhsm.config.get_config_parser()

        # We are going to read configuration file again, but we have to clean all data in parser object
        # this way, because iniparse module doesn't provide better method to do that.
        parser.data = ini.INIConfig(None, optionxformsource=parser)

        # We have to read parser again to get fresh data from the file
        files_read = parser.read()

        if len(files_read) > 0:
            log.debug("files read: %s" % str(files_read))
            self.config = Config(parser)
            rhsm.logutil.init_logger(parser)
            log.debug("Configuration file: %s reloaded: %s" %
                      (parser.config_file, str(self.config)))
        else:
            log.warning("Unable to read configuration file: %s" %
                        parser.config_file)
예제 #20
0
 def setUp(self):
     super(BaseConfigTest, self).setUp()
     self.fid = self.write_temp_file(TEST_CONFIG)
     self.parser = RhsmConfigParser(self.fid.name)
     self.config = Config(self.parser)
     self.addCleanup(self.fid.close)
예제 #21
0
 def test_auto_persists(self):
     config = Config(self.parser, auto_persist=True)
     config['foo'] = {'hello': 'world'}
     reparsed = RhsmConfigParser(self.fid.name)
     self.assertEqual('world', reparsed.get('foo', 'hello'))
     self.assertRaises(NoOptionError, reparsed.get, 'foo', 'quux')