Example #1
0
def MetlogLoader(**kwargs):
    # Delegates to metlog-py's client configuration functions
    cfgfilepath = kwargs['config']
    with open(cfgfilepath) as cfgfile:
        metlog = client_from_stream_config(cfgfile, 'metlog')
        CLIENT_HOLDER.set_client(metlog.logger, metlog)
    return CLIENT_HOLDER
Example #2
0
def MetlogLoader(**kwargs):
    # Delegates to metlog-py's client configuration functions
    cfgfilepath = kwargs['config']
    with open(cfgfilepath) as cfgfile:
        metlog = client_from_stream_config(cfgfile, 'metlog')
        CLIENT_HOLDER.set_client(metlog.logger, metlog)
    return CLIENT_HOLDER
Example #3
0
 def setUp(self):
     self.orig_default_client = CLIENT_HOLDER.global_config.get('default')
     client = CLIENT_HOLDER.get_client(self.client_name)
     client_config = {
         'sender_class': 'metlog.senders.DebugCaptureSender',
     }
     self.client = client_from_dict_config(client_config, client)
     CLIENT_HOLDER.set_default_client_name(self.client_name)
Example #4
0
 def setUp(self):
     self.orig_default_client = CLIENT_HOLDER.global_config.get('default')
     client = CLIENT_HOLDER.get_client(self.client_name)
     client_config = {
         'sender_class': 'metlog.senders.DebugCaptureSender',
         }
     self.client = client_from_dict_config(client_config, client)
     CLIENT_HOLDER.set_default_client_name(self.client_name)
    def setUp(self):
        self.orig_default_client = CLIENT_HOLDER.global_config.get('default')

        client = CLIENT_HOLDER.get_client(self.client_name)

        client_config = {'sender_class': 'metlog.senders.DebugCaptureSender',
                'plugins': {'plugin_section_name':
                    ('metlog_raven.raven_plugin:config_plugin',
                        {'dsn': 'udp://*****:*****@somehost.com:5000/2'})}
                    }
        self.client = client_from_dict_config(client_config, client)
        CLIENT_HOLDER.set_default_client_name(self.client_name)
    def setUp(self):
        self.orig_default_client = CLIENT_HOLDER.global_config.get('default')

        client = CLIENT_HOLDER.get_client(self.client_name)

        self.dsn = "udp://*****:*****@somehost.com:9000/2"
        client_config = {'sender_class': 'metlog.senders.DebugCaptureSender',
                'plugins': {'plugin_section_name':
                    ['metlog_raven.raven_plugin:config_plugin',
                        {'dsn': self.dsn}]
                    }}
        self.client = client_from_dict_config(client_config, client)
        CLIENT_HOLDER.set_default_client_name(self.client_name)
    def setUp(self):
        self.orig_default_client = CLIENT_HOLDER.global_config.get('default')

        client = CLIENT_HOLDER.get_client(self.client_name)

        client_config = {
            'sender_class': 'metlog.senders.DebugCaptureSender',
            'plugins': {
                'plugin_section_name':
                ('metlog_raven.raven_plugin:config_plugin', {
                    'dsn': 'udp://*****:*****@somehost.com:5000/2'
                })
            }
        }
        self.client = client_from_dict_config(client_config, client)
        CLIENT_HOLDER.set_default_client_name(self.client_name)
Example #8
0
 def client(self):
     if self._client is None:
         if self.client_name:
             self._client = CLIENT_HOLDER.get_client(self.client_name)
         else:
             self._client = CLIENT_HOLDER.default_client
     return self._client
Example #9
0
def setup_metlog(config_dict, default=False):
    """
    Instantiate a Metlog client and add it to the client holder.

    :param config_dict: Dictionary object containing the metlog client
                        configuration.
    :param default: Should this be specified as CLIENT_HOLDER's default
                    client? Note that the first client to be added will
                    automatically be specified as the default, regardless
                    of the value of this argument.
    """
    name = config_dict.get('logger', '')
    client = CLIENT_HOLDER.get_client(name)
    client = client_from_dict_config(config_dict, client)
    if default:
        CLIENT_HOLDER.set_default_client_name(name)
Example #10
0
 def client(self):
     if self._client is None:
         if self.client_name:
             self._client = CLIENT_HOLDER.get_client(self.client_name)
         else:
             self._client = CLIENT_HOLDER.default_client
     return self._client
Example #11
0
def setup_metlog(config_dict, default=False):
    """
    Instantiate a Metlog client and add it to the client holder.

    :param config_dict: Dictionary object containing the metlog client
                        configuration.
    :param default: Should this be specified as CLIENT_HOLDER's default
                    client? Note that the first client to be added will
                    automatically be specified as the default, regardless
                    of the value of this argument.
    """
    name = config_dict.get('logger', '')
    client = CLIENT_HOLDER.get_client(name)
    client = client_from_dict_config(config_dict, client)
    if default:
        CLIENT_HOLDER.set_default_client_name(name)
    def setUp(self):
        self.orig_default_client = CLIENT_HOLDER.global_config.get('default')

        client = CLIENT_HOLDER.get_client(self.client_name)

        self.dsn = "udp://*****:*****@somehost.com:9000/2"
        client_config = {
            'sender_class': 'metlog.senders.DebugCaptureSender',
            'plugins': {
                'plugin_section_name':
                ['metlog_raven.raven_plugin:config_plugin', {
                    'dsn': self.dsn
                }]
            }
        }
        self.client = client_from_dict_config(client_config, client)
        CLIENT_HOLDER.set_default_client_name(self.client_name)
Example #13
0
def get_metlog_client(name=None):
    """
    Return the specified Metlog client from the CLIENT_HOLDER.

    :param name: Name of metlog client to fetch. If not provided the
                 holder's specified default client will be used.
    """
    if name is not None:
        client = CLIENT_HOLDER.get_client(name)
    else:
        client = CLIENT_HOLDER.default_client
    return client
Example #14
0
def get_metlog_client(name=None):
    """
    Return the specified Metlog client from the CLIENT_HOLDER.

    :param name: Name of metlog client to fetch. If not provided the
                 holder's specified default client will be used.
    """
    if name is not None:
        client = CLIENT_HOLDER.get_client(name)
    else:
        client = CLIENT_HOLDER.default_client
    return client
    def __init__(self, urls, controllers, config=None, auth_class=None):
        self.mapper = Mapper()
        if config is None:
            self.config = Config()
        elif isinstance(config, Config):
            self.config = config
        else:
            # try to convert to config object
            self.config = Config(config)

        # global config
        self.retry_after = self.config.get('global.retry_after', 1800)

        # heartbeat page
        self.heartbeat_page = self.config.get('global.heartbeat_page',
                                              '__heartbeat__')

        # debug page, if any
        self.debug_page = self.config.get('global.debug_page')

        # check if we want to clean when the app ends
        self.sigclean = self.config.get('global.clean_shutdown', True)

        # load the specified plugin modules
        self.modules = dict()
        app_modules = self.config.get('app.modules', [])
        if isinstance(app_modules, basestring):
            app_modules = [app_modules]
        for module in app_modules:
            self.modules[module] = load_and_configure(self.config, module)

        if self.modules.get('metlog_loader') is not None:
            # stash the metlog client in a more convenient spot
            self.logger = self.modules.get('metlog_loader').default_client
        else:
            # there was no metlog config, default to using StdLibLoggingSender
            sender = StdLibLoggingSender('syncserver', json_types=[])
            metlog = MetlogClient(sender, 'syncserver')
            CLIENT_HOLDER.set_client(metlog.logger, metlog)
            self.logger = metlog
        if not hasattr(self.logger, "cef"):
            log_cef_fn = metlog_cef.cef_plugin.config_plugin(dict())
            self.logger.add_method(log_cef_fn)

        # XXX: this should be converted to auto-load in self.modules
        # loading the authentication tool
        self.auth = None if auth_class is None else auth_class(self.config)

        # loading and connecting controllers
        self.controllers = dict([(name, klass(self))
                                 for name, klass in controllers.items()])

        for url in urls:
            if len(url) == 4:
                verbs, match, controller, action = url
                extras = {}
            elif len(url) == 5:
                verbs, match, controller, action, extras = url
            else:
                msg = "Each URL description needs 4 or 5 elements. Got %s" \
                    % str(url)
                raise ValueError(msg)

            if isinstance(verbs, str):
                verbs = [verbs]

            # wrap action methods w/ metlog decorators
            controller_instance = self.controllers.get(controller)
            if controller_instance is not None:
                wrapped_name = '_%s_wrapped' % action
                method = getattr(controller_instance, action, None)
                if ((method is not None)
                        and (not hasattr(controller_instance, wrapped_name))):
                    # add wrapped method
                    wrapped = svc_timeit(method)
                    wrapped = incr_count(wrapped)
                    wrapped = send_services_data(wrapped)
                    setattr(controller_instance, wrapped_name, wrapped)
            self.mapper.connect(None,
                                match,
                                controller=controller,
                                action=action,
                                conditions=dict(method=verbs),
                                **extras)

        # loads host-specific configuration
        self._host_configs = {}

        # heartbeat & debug pages
        self.standard_controller = StandardController(self)

        # rehooked overridable points so they can be overridden in the base app
        self.standard_controller._debug_server = self._debug_server
        self.standard_controller._check_server = self._check_server

        # hooking callbacks when the app shuts down
        self.killing = self.shutting = False
        self.graceful_shutdown_interval = self.config.get(
            'global.graceful_shutdown_interval', 1.)
        self.hard_shutdown_interval = self.config.get(
            'global.hard_shutdown_interval', 1.)
        if self.sigclean:
            signal.signal(signal.SIGTERM, self._sigterm)
            signal.signal(signal.SIGINT, self._sigterm)
Example #16
0
 def tearDown(self):
     del CLIENT_HOLDER._clients[self.client_name]
     CLIENT_HOLDER.set_default_client_name(self.orig_default_client)
     timed_add._client = None
Example #17
0
    def __init__(self, ini_path=None, ini_dir=None, load_sections=None):
        """
        :param ini_dir: Directory path in which to start looking for the ini
        file.  Will climb the file tree from here looking for 'tests.ini' file,
        unless 'WEAVE_TESTFILE' env var is set, in which case it will climb the
        file tree from here looking for 'tests_${WEAVE_TESTFILE}.ini'.

        :param ini_path: Full path to configuration file.  Takes precedence
        over ini_dir, if both are provided.  Raises IOError if file doesn't
        exist.

        One or the other of `ini_dir` or `ini_path` arguments MUST be provided.

        :param load_sections: A sequence of strings that name the configuration
        sections that should be dynamically loaded.  Any entry in this sequence
        could alternately be a 2-tuple containing the name of the section and
        the corresponding class parameter value to use.
        """
        self.start_dir = ini_dir
        if ini_path:
            if not os.path.isfile(ini_path):
                raise IOError("invalid config file: %s" % ini_path)
            ini_dir = os.path.dirname(ini_path)
        elif ini_dir:
            if 'WEAVE_TESTFILE' in os.environ:
                test_filename = 'tests_%s.ini' % os.environ['WEAVE_TESTFILE']
            else:
                test_filename = 'tests.ini'

            while True:
                ini_path = os.path.join(ini_dir, test_filename)
                if os.path.exists(ini_path):
                    break

                if ini_path == ("/%s" % test_filename) \
                    or ini_path == test_filename:
                    raise IOError("cannot locate %s" % test_filename)

                ini_dir = os.path.split(ini_dir)[0]
            else:
                raise ValueError('No ini_path or ini_dir specified.')

        self.ini_dir = ini_dir
        self.ini_path = ini_path

        ini_cfg = RawConfigParser()
        ini_cfg.read(ini_path)

        # loading loggers
        self.config = self.convert_config(ini_cfg, ini_path)

        # Ensure that metlog is available, either from the config
        # or by setting up a default client.
        try:
            loader = load_and_configure(self.config, "metlog_loader")
            client = loader.default_client
        except KeyError:
            sender = DebugCaptureSender()
            client = MetlogClient(sender, "syncserver")
        CLIENT_HOLDER.set_client(client.logger, client)
        if not hasattr(client, "cef"):
            log_cef_fn = metlog_cef.cef_plugin.config_plugin(dict())
            client.add_method(log_cef_fn)

        if load_sections is not None:
            for section in load_sections:
                if isinstance(section, tuple):
                    self.add_class(section[0], cls_param=section[1])
                else:
                    self.add_class(section)
 def tearDown(self):
     CLIENT_HOLDER.delete_client('test')
     CLIENT_HOLDER.global_config = self.orig_globals
Example #19
0
 def tearDown(self):
     del CLIENT_HOLDER._clients[self.client_name]
     CLIENT_HOLDER.set_default_client_name(self.orig_default_client)
Example #20
0
    def __init__(self, urls, controllers, config=None,
                 auth_class=None):
        self.mapper = Mapper()
        if config is None:
            self.config = Config()
        elif isinstance(config, Config):
            self.config = config
        else:
            # try to convert to config object
            self.config = Config(config)

        # global config
        self.retry_after = self.config.get('global.retry_after', 1800)

        # heartbeat page
        self.heartbeat_page = self.config.get('global.heartbeat_page',
                                              '__heartbeat__')

        # debug page, if any
        self.debug_page = self.config.get('global.debug_page')

        # check if we want to clean when the app ends
        self.sigclean = self.config.get('global.clean_shutdown', True)

        # load the specified plugin modules
        self.modules = dict()
        app_modules = self.config.get('app.modules', [])
        if isinstance(app_modules, basestring):
            app_modules = [app_modules]
        for module in app_modules:
            self.modules[module] = load_and_configure(self.config, module)

        if self.modules.get('metlog_loader') is not None:
            # stash the metlog client in a more convenient spot
            self.logger = self.modules.get('metlog_loader').default_client
        else:
            # there was no metlog config, default to using StdLibLoggingSender
            sender = StdLibLoggingSender('syncserver', json_types=[])
            metlog = MetlogClient(sender, 'syncserver')
            CLIENT_HOLDER.set_client(metlog.logger, metlog)
            self.logger = metlog
        if not hasattr(self.logger, "cef"):
            log_cef_fn = metlog_cef.cef_plugin.config_plugin(dict())
            self.logger.add_method(log_cef_fn)

        # XXX: this should be converted to auto-load in self.modules
        # loading the authentication tool
        self.auth = None if auth_class is None else auth_class(self.config)

        # loading and connecting controllers
        self.controllers = dict([(name, klass(self)) for name, klass in
                                 controllers.items()])

        for url in urls:
            if len(url) == 4:
                verbs, match, controller, action = url
                extras = {}
            elif len(url) == 5:
                verbs, match, controller, action, extras = url
            else:
                msg = "Each URL description needs 4 or 5 elements. Got %s" \
                    % str(url)
                raise ValueError(msg)

            if isinstance(verbs, str):
                verbs = [verbs]

            # wrap action methods w/ metlog decorators
            controller_instance = self.controllers.get(controller)
            if controller_instance is not None:
                wrapped_name = '_%s_wrapped' % action
                method = getattr(controller_instance, action, None)
                if ((method is not None) and
                    (not hasattr(controller_instance, wrapped_name))):
                    # add wrapped method
                    wrapped = svc_timeit(method)
                    wrapped = incr_count(wrapped)
                    wrapped = send_services_data(wrapped)
                    setattr(controller_instance, wrapped_name, wrapped)
            self.mapper.connect(None, match, controller=controller,
                                action=action, conditions=dict(method=verbs),
                                **extras)

        # loads host-specific configuration
        self._host_configs = {}

        # heartbeat & debug pages
        self.standard_controller = StandardController(self)

        # rehooked overridable points so they can be overridden in the base app
        self.standard_controller._debug_server = self._debug_server
        self.standard_controller._check_server = self._check_server

        # hooking callbacks when the app shuts down
        self.killing = self.shutting = False
        self.graceful_shutdown_interval = self.config.get(
                                      'global.graceful_shutdown_interval', 1.)
        self.hard_shutdown_interval = self.config.get(
                                          'global.hard_shutdown_interval', 1.)
        if self.sigclean:
            signal.signal(signal.SIGTERM, self._sigterm)
            signal.signal(signal.SIGINT, self._sigterm)
Example #21
0
 def tearDown(self):
     CLIENT_HOLDER.delete_client('test')
     CLIENT_HOLDER.global_config = self.orig_globals
Example #22
0
    def __init__(self, ini_path=None, ini_dir=None, load_sections=None):
        """
        :param ini_dir: Directory path in which to start looking for the ini
        file.  Will climb the file tree from here looking for 'tests.ini' file,
        unless 'WEAVE_TESTFILE' env var is set, in which case it will climb the
        file tree from here looking for 'tests_${WEAVE_TESTFILE}.ini'.

        :param ini_path: Full path to configuration file.  Takes precedence
        over ini_dir, if both are provided.  Raises IOError if file doesn't
        exist.

        One or the other of `ini_dir` or `ini_path` arguments MUST be provided.

        :param load_sections: A sequence of strings that name the configuration
        sections that should be dynamically loaded.  Any entry in this sequence
        could alternately be a 2-tuple containing the name of the section and
        the corresponding class parameter value to use.
        """
        self.start_dir = ini_dir
        if ini_path:
            if not os.path.isfile(ini_path):
                raise IOError("invalid config file: %s" % ini_path)
            ini_dir = os.path.dirname(ini_path)
        elif ini_dir:
            if 'WEAVE_TESTFILE' in os.environ:
                test_filename = 'tests_%s.ini' % os.environ['WEAVE_TESTFILE']
            else:
                test_filename = 'tests.ini'

            while True:
                ini_path = os.path.join(ini_dir, test_filename)
                if os.path.exists(ini_path):
                    break

                if ini_path == ("/%s" % test_filename) \
                    or ini_path == test_filename:
                    raise IOError("cannot locate %s" % test_filename)

                ini_dir = os.path.split(ini_dir)[0]
            else:
                raise ValueError('No ini_path or ini_dir specified.')

        self.ini_dir = ini_dir
        self.ini_path = ini_path

        ini_cfg = RawConfigParser()
        ini_cfg.read(ini_path)

        # loading loggers
        self.config = self.convert_config(ini_cfg, ini_path)

        # Ensure that metlog is available, either from the config
        # or by setting up a default client.
        try:
            loader = load_and_configure(self.config, "metlog_loader")
            client = loader.default_client
        except KeyError:
            sender = DebugCaptureSender()
            client = MetlogClient(sender, "syncserver")
        CLIENT_HOLDER.set_client(client.logger, client)
        if not hasattr(client, "cef"):
            log_cef_fn = metlog_cef.cef_plugin.config_plugin(dict())
            client.add_method(log_cef_fn)

        if load_sections is not None:
            for section in load_sections:
                if isinstance(section, tuple):
                    self.add_class(section[0], cls_param=section[1])
                else:
                    self.add_class(section)