Ejemplo n.º 1
0
    def __init__(self, pack, file_path, class_name, trigger_types,
                 poll_interval=None, parent_args=None):
        """
        :param pack: Name of the pack this sensor belongs to.
        :type pack: ``str``

        :param file_path: Path to the sensor module file.
        :type file_path: ``str``

        :param class_name: Sensor class name.
        :type class_name: ``str``

        :param trigger_types: A list of references to trigger types which
                                  belong to this sensor.
        :type trigger_types: ``list`` of ``str``

        :param poll_interval: Sensor poll interval (in seconds).
        :type poll_interval: ``int`` or ``None``

        :param parent_args: Command line arguments passed to the parent process.
        :type parse_args: ``list``
        """
        self._pack = pack
        self._file_path = file_path
        self._class_name = class_name
        self._trigger_types = trigger_types or []
        self._poll_interval = poll_interval
        self._parent_args = parent_args or []
        self._trigger_names = {}

        # 1. Parse the config with inherited parent args
        try:
            config.parse_args(args=self._parent_args)
        except Exception:
            pass

        # 2. Establish DB connection
        username = cfg.CONF.database.username if hasattr(cfg.CONF.database, 'username') else None
        password = cfg.CONF.database.password if hasattr(cfg.CONF.database, 'password') else None
        db_setup_with_retry(cfg.CONF.database.db_name, cfg.CONF.database.host,
                            cfg.CONF.database.port, username=username, password=password)

        # 3. Instantiate the watcher
        self._trigger_watcher = TriggerWatcher(create_handler=self._handle_create_trigger,
                                               update_handler=self._handle_update_trigger,
                                               delete_handler=self._handle_delete_trigger,
                                               trigger_types=self._trigger_types,
                                               queue_suffix='sensorwrapper_%s_%s' %
                                               (self._pack, self._class_name),
                                               exclusive=True)

        # 4. Set up logging
        self._logger = logging.getLogger('SensorWrapper.%s.%s' %
                                         (self._pack, self._class_name))
        logging.setup(cfg.CONF.sensorcontainer.logging)

        if '--debug' in parent_args:
            set_log_level_for_all_loggers()

        self._sensor_instance = self._get_sensor_instance()
Ejemplo n.º 2
0
    def __init__(self, pack, file_path, class_name, trigger_types,
                 poll_interval=None, parent_args=None):
        """
        :param pack: Name of the pack this sensor belongs to.
        :type pack: ``str``

        :param file_path: Path to the sensor module file.
        :type file_path: ``str``

        :param class_name: Sensor class name.
        :type class_name: ``str``

        :param trigger_types: A list of references to trigger types which
                                  belong to this sensor.
        :type trigger_types: ``list`` of ``str``

        :param poll_interval: Sensor poll interval (in seconds).
        :type poll_interval: ``int`` or ``None``

        :param parent_args: Command line arguments passed to the parent process.
        :type parse_args: ``list``
        """
        self._pack = pack
        self._file_path = file_path
        self._class_name = class_name
        self._trigger_types = trigger_types or []
        self._poll_interval = poll_interval
        self._parent_args = parent_args or []
        self._trigger_names = {}

        # 1. Parse the config with inherited parent args
        try:
            config.parse_args(args=self._parent_args)
        except Exception:
            pass

        # 2. Establish DB connection
        username = cfg.CONF.database.username if hasattr(cfg.CONF.database, 'username') else None
        password = cfg.CONF.database.password if hasattr(cfg.CONF.database, 'password') else None
        db_setup_with_retry(cfg.CONF.database.db_name, cfg.CONF.database.host,
                            cfg.CONF.database.port, username=username, password=password)

        # 3. Instantiate the watcher
        self._trigger_watcher = TriggerWatcher(create_handler=self._handle_create_trigger,
                                               update_handler=self._handle_update_trigger,
                                               delete_handler=self._handle_delete_trigger,
                                               trigger_types=self._trigger_types,
                                               queue_suffix='sensorwrapper_%s_%s' %
                                               (self._pack, self._class_name),
                                               exclusive=True)

        # 4. Set up logging
        self._logger = logging.getLogger('SensorWrapper.%s.%s' %
                                         (self._pack, self._class_name))
        logging.setup(cfg.CONF.sensorcontainer.logging)

        if '--debug' in parent_args:
            set_log_level_for_all_loggers()

        self._sensor_instance = self._get_sensor_instance()
Ejemplo n.º 3
0
def enable_debugging():
    global ENABLE_DEBUGGING
    ENABLE_DEBUGGING = True

    set_log_level_for_all_loggers(level=stdlib_logging.DEBUG)

    return ENABLE_DEBUGGING
Ejemplo n.º 4
0
def setup(service, config, setup_db=True, register_mq_exchanges=True,
          register_signal_handlers=True, run_migrations=True):
    """
    Common setup function.

    Currently it performs the following operations:

    1. Parses config and CLI arguments
    2. Establishes DB connection
    3. Set log level for all the loggers to DEBUG if --debug flag is present
    4. Registers RabbitMQ exchanges
    5. Registers common signal handlers

    :param service: Name of the service.
    :param config: Config object to use to parse args.
    """
    # Set up logger which logs everything which happens during and before config
    # parsing to sys.stdout
    logging.setup(DEFAULT_LOGGING_CONF_PATH)

    # Parse args to setup config.
    config.parse_args()

    config_file_paths = cfg.CONF.config_file
    config_file_paths = [os.path.abspath(path) for path in config_file_paths]
    LOG.debug('Using config files: %s', ','.join(config_file_paths))

    # Setup logging.
    logging_config_path = config.get_logging_config_path()
    logging_config_path = os.path.abspath(logging_config_path)

    LOG.debug('Using logging config: %s', logging_config_path)
    logging.setup(logging_config_path)

    if cfg.CONF.debug:
        set_log_level_for_all_loggers(level=stdlib_logging.DEBUG)

    # All other setup which requires config to be parsed and logging to
    # be correctly setup.
    if setup_db:
        db_setup()

    if register_mq_exchanges:
        register_exchanges()

    if register_signal_handlers:
        register_common_signal_handlers()

    # TODO: This is a "not so nice" workaround until we have a proper migration system in place
    if run_migrations:
        insert_system_roles()

    if cfg.CONF.rbac.enable and not cfg.CONF.auth.enable:
        msg = ('Authentication is not enabled. RBAC only works when authentication is enabled.'
               'You can either enable authentication or disable RBAC.')
        raise Exception(msg)
Ejemplo n.º 5
0
def disable_debugging():
    global ENABLE_DEBUGGING
    ENABLE_DEBUGGING = False

    set_log_level_for_all_loggers(level=stdlib_logging.INFO)

    setup_logging(loglevel=stdlib_logging.INFO)
    paramiko.common.logging.basicConfig(level=paramiko.common.INFO)

    return ENABLE_DEBUGGING
Ejemplo n.º 6
0
def disable_debugging():
    global ENABLE_DEBUGGING
    ENABLE_DEBUGGING = False

    set_log_level_for_all_loggers(level=stdlib_logging.INFO)

    setup_logging(loglevel=stdlib_logging.INFO)
    paramiko.common.logging.basicConfig(level=paramiko.common.INFO)

    return ENABLE_DEBUGGING
Ejemplo n.º 7
0
def enable_debugging():
    global ENABLE_DEBUGGING
    ENABLE_DEBUGGING = True

    # Set debug level for all StackStorm loggers
    set_log_level_for_all_loggers(level=stdlib_logging.DEBUG)

    # Set debug log level for kombu
    setup_logging(loglevel=stdlib_logging.DEBUG)

    # Set debug log level for paramiko
    paramiko.common.logging.basicConfig(level=paramiko.common.DEBUG)

    return ENABLE_DEBUGGING
Ejemplo n.º 8
0
def enable_debugging():
    global ENABLE_DEBUGGING
    ENABLE_DEBUGGING = True

    # Set debug level for all StackStorm loggers
    set_log_level_for_all_loggers(level=stdlib_logging.DEBUG)

    # Set debug log level for kombu
    setup_logging(loglevel=stdlib_logging.DEBUG)

    # Set debug log level for paramiko
    paramiko.common.logging.basicConfig(level=paramiko.common.DEBUG)

    return ENABLE_DEBUGGING
Ejemplo n.º 9
0
def setup(service,
          config,
          setup_db=True,
          register_mq_exchanges=True,
          register_signal_handlers=True):
    """
    Common setup function.

    Currently it performs the following operations:

    1. Parses config and CLI arguments
    2. Establishes DB connection
    3. Set log level for all the loggers to DEBUG if --debug flag is present
    4. Registers RabbitMQ exchanges
    5. Registers common signal handlers

    :param service: Name of the service.
    :param config: Config object to use to parse args.
    """
    # Set up logger which logs everything which happens during and before config
    # parsing to sys.stdout
    logging.setup(DEFAULT_LOGGING_CONF_PATH)

    # Parse args to setup config.
    config.parse_args()

    config_file_paths = cfg.CONF.config_file
    config_file_paths = [os.path.abspath(path) for path in config_file_paths]
    LOG.debug('Using config files: %s', ','.join(config_file_paths))

    # Setup logging.
    logging_config_path = config.get_logging_config_path()
    logging_config_path = os.path.abspath(logging_config_path)

    LOG.debug('Using logging config: %s', logging_config_path)
    logging.setup(logging_config_path)

    if cfg.CONF.debug:
        set_log_level_for_all_loggers(level=stdlib_logging.DEBUG)

    # All other setup which requires config to be parsed and logging to
    # be correctly setup.
    if setup_db:
        db_setup()

    if register_mq_exchanges:
        register_exchanges()

    if register_signal_handlers:
        register_common_signal_handlers()
Ejemplo n.º 10
0
def setup(service, config, setup_db=True, register_mq_exchanges=True,
          register_signal_handlers=True):
    """
    Common setup function.

    Currently it performs the following operations:

    1. Parses config and CLI arguments
    2. Establishes DB connection
    3. Set log level for all the loggers to DEBUG if --debug flag is present
    4. Registers RabbitMQ exchanges
    5. Registers common signal handlers

    :param service: Name of the service.
    :param config: Config object to use to parse args.
    """
    # Set up logger which logs everything which happens during and before config
    # parsing to sys.stdout
    logging.setup(DEFAULT_LOGGING_CONF_PATH)

    # Parse args to setup config.
    config.parse_args()

    config_file_paths = cfg.CONF.config_file
    config_file_paths = [os.path.abspath(path) for path in config_file_paths]
    LOG.debug('Using config files: %s', ','.join(config_file_paths))

    # Setup logging.
    logging_config_path = config.get_logging_config_path()
    logging_config_path = os.path.abspath(logging_config_path)

    LOG.debug('Using logging config: %s', logging_config_path)
    logging.setup(logging_config_path)

    if cfg.CONF.debug:
        set_log_level_for_all_loggers(level=stdlib_logging.DEBUG)

    # All other setup which requires config to be parsed and logging to
    # be correctly setup.
    if setup_db:
        db_setup()

    if register_mq_exchanges:
        register_exchanges()

    if register_signal_handlers:
        register_common_signal_handlers()
Ejemplo n.º 11
0
def setup(service,
          config,
          setup_db=True,
          register_mq_exchanges=True,
          register_signal_handlers=True,
          register_internal_trigger_types=False,
          run_migrations=True,
          config_args=None):
    """
    Common setup function.

    Currently it performs the following operations:

    1. Parses config and CLI arguments
    2. Establishes DB connection
    3. Set log level for all the loggers to DEBUG if --debug flag is present
    4. Registers RabbitMQ exchanges
    5. Registers common signal handlers
    6. Register internal trigger types

    :param service: Name of the service.
    :param config: Config object to use to parse args.
    """
    # Set up logger which logs everything which happens during and before config
    # parsing to sys.stdout
    logging.setup(DEFAULT_LOGGING_CONF_PATH)

    # Parse args to setup config.
    if config_args:
        config.parse_args(config_args)
    else:
        config.parse_args()

    config_file_paths = cfg.CONF.config_file
    config_file_paths = [os.path.abspath(path) for path in config_file_paths]
    LOG.debug('Using config files: %s', ','.join(config_file_paths))

    # Setup logging.
    logging_config_path = config.get_logging_config_path()
    logging_config_path = os.path.abspath(logging_config_path)

    LOG.debug('Using logging config: %s', logging_config_path)
    logging.setup(logging_config_path)

    if cfg.CONF.debug:
        set_log_level_for_all_loggers(level=stdlib_logging.DEBUG)

    # All other setup which requires config to be parsed and logging to
    # be correctly setup.
    if setup_db:
        db_setup()

    if cfg.CONF.debug or cfg.CONF.profile:
        enable_profiling()

    if register_mq_exchanges:
        register_exchanges()

    if register_signal_handlers:
        register_common_signal_handlers()

    if register_internal_trigger_types:
        triggers.register_internal_trigger_types()

    # TODO: This is a "not so nice" workaround until we have a proper migration system in place
    if run_migrations:
        run_all_rbac_migrations()

    if cfg.CONF.rbac.enable and not cfg.CONF.auth.enable:
        msg = (
            'Authentication is not enabled. RBAC only works when authentication is enabled.'
            'You can either enable authentication or disable RBAC.')
        raise Exception(msg)
Ejemplo n.º 12
0
    def __init__(
        self,
        pack,
        file_path,
        class_name,
        trigger_types,
        poll_interval=None,
        parent_args=None,
        db_ensure_indexes=True,
    ):
        """
        :param pack: Name of the pack this sensor belongs to.
        :type pack: ``str``

        :param file_path: Path to the sensor module file.
        :type file_path: ``str``

        :param class_name: Sensor class name.
        :type class_name: ``str``

        :param trigger_types: A list of references to trigger types which
                                  belong to this sensor.
        :type trigger_types: ``list`` of ``str``

        :param poll_interval: Sensor poll interval (in seconds).
        :type poll_interval: ``int`` or ``None``

        :param parent_args: Command line arguments passed to the parent process.
        :type parse_args: ``list``

        :param db_ensure_indexes: True to ensure indexes. This should really only be set to False
                                  in tests to speed things up.
        """
        self._pack = pack
        self._file_path = file_path
        self._class_name = class_name
        self._trigger_types = trigger_types or []
        self._poll_interval = poll_interval
        self._parent_args = parent_args or []
        self._trigger_names = {}

        # 1. Parse the config with inherited parent args
        try:
            config.parse_args(args=self._parent_args)
        except Exception:
            LOG.exception("Failed to parse config using parent args "
                          '(parent_args=%s): "%s".' % (str(self._parent_args)))

        # 2. Establish DB connection
        username = (cfg.CONF.database.username if hasattr(
            cfg.CONF.database, "username") else None)
        password = (cfg.CONF.database.password if hasattr(
            cfg.CONF.database, "password") else None)
        db_setup_with_retry(
            cfg.CONF.database.db_name,
            cfg.CONF.database.host,
            cfg.CONF.database.port,
            username=username,
            password=password,
            ensure_indexes=db_ensure_indexes,
            ssl=cfg.CONF.database.ssl,
            ssl_keyfile=cfg.CONF.database.ssl_keyfile,
            ssl_certfile=cfg.CONF.database.ssl_certfile,
            ssl_cert_reqs=cfg.CONF.database.ssl_cert_reqs,
            ssl_ca_certs=cfg.CONF.database.ssl_ca_certs,
            authentication_mechanism=cfg.CONF.database.
            authentication_mechanism,
            ssl_match_hostname=cfg.CONF.database.ssl_match_hostname,
        )

        # 3. Instantiate the watcher
        self._trigger_watcher = TriggerWatcher(
            create_handler=self._handle_create_trigger,
            update_handler=self._handle_update_trigger,
            delete_handler=self._handle_delete_trigger,
            trigger_types=self._trigger_types,
            queue_suffix="sensorwrapper_%s_%s" %
            (self._pack, self._class_name),
            exclusive=True,
        )

        # 4. Set up logging
        self._logger = logging.getLogger("SensorWrapper.%s.%s" %
                                         (self._pack, self._class_name))
        logging.setup(cfg.CONF.sensorcontainer.logging)

        if "--debug" in parent_args:
            set_log_level_for_all_loggers()
        else:
            # NOTE: statsd logger logs everything by default under INFO so we ignore those log
            # messages unless verbose / debug mode is used
            logging.ignore_statsd_log_messages()

        self._sensor_instance = self._get_sensor_instance()