예제 #1
0
파일: ccsession.py 프로젝트: zhangyut/bundy
    def __init__(self,
                 spec_file_name,
                 config_handler,
                 command_handler,
                 cc_session=None,
                 handle_logging_config=True,
                 socket_file=None):
        """Initialize a ModuleCCSession. This does *NOT* send the
           specification and request the configuration yet. Use start()
           for that once the ModuleCCSession has been initialized.

           specfile_name is the path to the specification file.

           config_handler and command_handler are callback functions,
           see set_config_handler and set_command_handler for more
           information on their signatures.

           cc_session can be used to pass in an existing CCSession,
           if it is None, one will be set up. This is mainly intended
           for testing purposes.

           handle_logging_config: if True, the module session will
           automatically handle logging configuration for the module;
           it will read the system-wide Logging configuration and call
           the logger manager to apply it. It will also inform the
           logger manager when the logging configuration gets updated.
           The module does not need to do anything except initializing
           its loggers, and provide log messages. Defaults to true.

           socket_file: If cc_session was none, this optional argument
           specifies which socket file to use to connect to msgq. It
           will be overridden by the environment variable
           MSGQ_SOCKET_FILE. If none, and no environment variable is
           set, it will use the system default.
        """
        module_spec = bundy.config.module_spec_from_file(spec_file_name)
        ConfigData.__init__(self, module_spec)

        self._module_name = module_spec.get_module_name()

        self.set_config_handler(config_handler)
        self.set_command_handler(command_handler)

        if not cc_session:
            self._session = Session(socket_file)
        else:
            self._session = cc_session
        self._session.group_subscribe(self._module_name, CC_INSTANCE_WILDCARD)

        self._remote_module_configs = {}
        self._remote_module_callbacks = {}

        self._notification_callbacks = {}
        self._last_notif_id = 0

        if handle_logging_config:
            self.add_remote_config(
                path_search('logging.spec', bundy_config.PLUGIN_PATHS),
                default_logconfig_handler)
예제 #2
0
    def __init__(self, spec_file_name, config_handler, command_handler,
                 cc_session=None, handle_logging_config=True,
                 socket_file = None):
        """Initialize a ModuleCCSession. This does *NOT* send the
           specification and request the configuration yet. Use start()
           for that once the ModuleCCSession has been initialized.

           specfile_name is the path to the specification file.

           config_handler and command_handler are callback functions,
           see set_config_handler and set_command_handler for more
           information on their signatures.

           cc_session can be used to pass in an existing CCSession,
           if it is None, one will be set up. This is mainly intended
           for testing purposes.

           handle_logging_config: if True, the module session will
           automatically handle logging configuration for the module;
           it will read the system-wide Logging configuration and call
           the logger manager to apply it. It will also inform the
           logger manager when the logging configuration gets updated.
           The module does not need to do anything except initializing
           its loggers, and provide log messages. Defaults to true.

           socket_file: If cc_session was none, this optional argument
           specifies which socket file to use to connect to msgq. It
           will be overridden by the environment variable
           MSGQ_SOCKET_FILE. If none, and no environment variable is
           set, it will use the system default.
        """
        module_spec = bundy.config.module_spec_from_file(spec_file_name)
        ConfigData.__init__(self, module_spec)

        self._module_name = module_spec.get_module_name()

        self.set_config_handler(config_handler)
        self.set_command_handler(command_handler)

        if not cc_session:
            self._session = Session(socket_file)
        else:
            self._session = cc_session
        self._session.group_subscribe(self._module_name, CC_INSTANCE_WILDCARD)

        self._remote_module_configs = {}
        self._remote_module_callbacks = {}

        self._notification_callbacks = {}
        self._last_notif_id = 0

        if handle_logging_config:
            self.add_remote_config(path_search('logging.spec', bundy_config.PLUGIN_PATHS),
                                   default_logconfig_handler)
예제 #3
0
파일: ccsession.py 프로젝트: zhangyut/bundy
    def _add_remote_config_internal(self,
                                    module_spec,
                                    config_update_callback=None):
        """The guts of add_remote_config and add_remote_config_by_name"""
        module_cfg = ConfigData(module_spec)
        module_name = module_spec.get_module_name()

        self._session.group_subscribe(module_name)

        # Get the current config for that module now
        seq = self._session.group_sendmsg(
            create_command(COMMAND_GET_CONFIG, {"module_name": module_name}),
            "ConfigManager")

        try:
            answer, _ = self._session.group_recvmsg(False, seq)
        except bundy.cc.SessionTimeout:
            raise ModuleCCSessionError("No answer from ConfigManager when "
                                       "asking about Remote module " +
                                       module_name)
        call_callback = False
        if answer:
            rcode, value = parse_answer(answer)
            if rcode == 0:
                if value != None:
                    if module_spec.validate_config(False, value):
                        module_cfg.set_local_config(value)
                        call_callback = True
                    else:
                        raise ModuleCCSessionError("Bad config data for " +
                                                   module_name + ": " +
                                                   str(value))
            else:
                raise ModuleCCSessionError("Failure requesting remote " +
                                           "configuration data for " +
                                           module_name)

        # all done, add it
        self._remote_module_configs[module_name] = module_cfg
        self._remote_module_callbacks[module_name] = config_update_callback
        if call_callback and config_update_callback is not None:
            config_update_callback(value, module_cfg)
예제 #4
0
    def _add_remote_config_internal(self, module_spec,
                                    config_update_callback=None):
        """The guts of add_remote_config and add_remote_config_by_name"""
        module_cfg = ConfigData(module_spec)
        module_name = module_spec.get_module_name()

        self._session.group_subscribe(module_name)

        # Get the current config for that module now
        seq = self._session.group_sendmsg(create_command(COMMAND_GET_CONFIG, { "module_name": module_name }), "ConfigManager")

        try:
            answer, _ = self._session.group_recvmsg(False, seq)
        except bundy.cc.SessionTimeout:
            raise ModuleCCSessionError("No answer from ConfigManager when "
                                       "asking about Remote module " +
                                       module_name)
        call_callback = False
        if answer:
            rcode, value = parse_answer(answer)
            if rcode == 0:
                if value != None:
                    if module_spec.validate_config(False, value):
                        module_cfg.set_local_config(value)
                        call_callback = True
                    else:
                        raise ModuleCCSessionError("Bad config data for " +
                                                   module_name + ": " +
                                                   str(value))
            else:
                raise ModuleCCSessionError("Failure requesting remote " +
                                           "configuration data for " +
                                           module_name)

        # all done, add it
        self._remote_module_configs[module_name] = module_cfg
        self._remote_module_callbacks[module_name] = config_update_callback
        if call_callback and config_update_callback is not None:
            config_update_callback(value, module_cfg)