Beispiel #1
0
def main():
    _parse_config()
    if CONF.verbose:
        _setup_logging()
        output = logging.getLogger(__name__).info
    else:
        output = pprint.pprint
    _setup_db()
    _refire_trigger_instance(trigger_instance_id=CONF.trigger_instance_id,
                             log_=logging.getLogger(__name__))
    output('Trigger re-fired')
    db_teardown()
    def __init__(self, pack, file_path, parameters=None, parent_args=None):
        """
        :param pack: Name of the pack this action belongs to.
        :type pack: ``str``

        :param file_path: Path to the action module.
        :type file_path: ``str``

        :param parameters: action parameters.
        :type parameters: ``dict`` 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._parameters = parameters or {}
        self._parent_args = parent_args or []
        self._class_name = None
        self._logger = logging.getLogger('PythonActionWrapper')

        try:
            config.parse_args(args=self._parent_args)
        except Exception:
            pass
        else:
            db_setup()
Beispiel #3
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()
Beispiel #4
0
 def get_logger(self, name):
     """
     Retrieve an instance of a logger to be used by the sensor class.
     """
     logger_name = '%s.%s' % (self._sensor_wrapper._logger.name, name)
     logger = logging.getLogger(logger_name)
     logger.propagate = True
     return logger
Beispiel #5
0
 def test_log_audit(self):
     """Test that AUDIT log entry goes to the audit log."""
     logging.setup(self.cfg_path)
     log = logging.getLogger(__name__)
     msg = uuid.uuid4().hex
     log.audit(msg)
     info_log_entries = open(self.info_log_path).read()
     self.assertIn(msg, info_log_entries)
     audit_log_entries = open(self.audit_log_path).read()
     self.assertIn(msg, audit_log_entries)
Beispiel #6
0
 def test_log_critical(self):
     """Test that CRITICAL log entry does not go to the audit log."""
     logging.setup(self.cfg_path)
     log = logging.getLogger(__name__)
     msg = uuid.uuid4().hex
     log.critical(msg)
     info_log_entries = open(self.info_log_path).read()
     self.assertIn(msg, info_log_entries)
     audit_log_entries = open(self.audit_log_path).read()
     self.assertNotIn(msg, audit_log_entries)
Beispiel #7
0
 def test_logger_set_level(self):
     logging.setup(self.cfg_path)
     log = logging.getLogger(__name__)
     self.assertEqual(log.getEffectiveLevel(), logbase.DEBUG)
     log.setLevel(logbase.INFO)
     self.assertEqual(log.getEffectiveLevel(), logbase.INFO)
     log.setLevel(logbase.WARN)
     self.assertEqual(log.getEffectiveLevel(), logbase.WARN)
     log.setLevel(logbase.ERROR)
     self.assertEqual(log.getEffectiveLevel(), logbase.ERROR)
     log.setLevel(logbase.CRITICAL)
     self.assertEqual(log.getEffectiveLevel(), logbase.CRITICAL)
     log.setLevel(logbase.AUDIT)
     self.assertEqual(log.getEffectiveLevel(), logbase.AUDIT)
    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. 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)

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

        self._sensor_instance = self._get_sensor_instance()
Beispiel #9
0
def get_logger_for_python_runner_action(action_name):
    """
    Set up a logger which logs all the messages with level DEBUG and above to stderr.
    """
    logger_name = 'actions.python.%s' % (action_name)
    logger = logging.getLogger(logger_name)

    console = stdlib_logging.StreamHandler()
    console.setLevel(stdlib_logging.DEBUG)

    formatter = stdlib_logging.Formatter('%(name)-12s: %(levelname)-8s %(message)s')
    console.setFormatter(formatter)
    logger.addHandler(console)
    logger.setLevel(stdlib_logging.DEBUG)

    return logger
Beispiel #10
0
    def _set_up_logger(self):
        """
        Set up a logger which logs all the messages with level DEBUG
        and above to stderr.
        """
        logger_name = "actions.python.%s" % (self.__class__.__name__)
        logger = logging.getLogger(logger_name)

        console = stdlib_logging.StreamHandler()
        console.setLevel(stdlib_logging.DEBUG)

        formatter = stdlib_logging.Formatter("%(name)-12s: %(levelname)-8s %(message)s")
        console.setFormatter(formatter)
        logger.addHandler(console)
        logger.setLevel(stdlib_logging.DEBUG)

        return logger
Beispiel #11
0
    def __init__(self, pack, file_path, config=None, parameters=None, user=None, parent_args=None,
                 log_level=PYTHON_RUNNER_DEFAULT_LOG_LEVEL):
        """
        :param pack: Name of the pack this action belongs to.
        :type pack: ``str``

        :param file_path: Path to the action module.
        :type file_path: ``str``

        :param config: Pack config.
        :type config: ``dict``

        :param parameters: action parameters.
        :type parameters: ``dict`` or ``None``

        :param user: Name of the user who triggered this action execution.
        :type user: ``str``

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

        self._pack = pack
        self._file_path = file_path
        self._config = config or {}
        self._parameters = parameters or {}
        self._user = user
        self._parent_args = parent_args or []
        self._log_level = log_level

        self._class_name = None
        self._logger = logging.getLogger('PythonActionWrapper')

        try:
            st2common_config.parse_args(args=self._parent_args)
        except Exception as e:
            LOG.debug('Failed to parse config using parent args (parent_args=%s): %s' %
                      (str(self._parent_args), str(e)))

        # Note: We can only set a default user value if one is not provided after parsing the
        # config
        if not self._user:
            # Note: We use late import to avoid performance overhead
            from oslo_config import cfg
            self._user = cfg.CONF.system_user.user
Beispiel #12
0
    def __init__(self, pack, file_path, parameters=None, user=None, parent_args=None):
        """
        :param pack: Name of the pack this action belongs to.
        :type pack: ``str``

        :param file_path: Path to the action module.
        :type file_path: ``str``

        :param parameters: action parameters.
        :type parameters: ``dict`` or ``None``

        :param user: Name of the user who triggered this action execution.
        :type user: ``str``

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

        self._pack = pack
        self._file_path = file_path
        self._parameters = parameters or {}
        self._user = user
        self._parent_args = parent_args or []

        self._class_name = None
        self._logger = logging.getLogger('PythonActionWrapper')

        try:
            config.parse_args(args=self._parent_args)
        except Exception as e:
            LOG.debug('Failed to parse config using parent args (parent_args=%s): %s' %
                      (str(self._parent_args), str(e)))

        # We don't need to ensure indexes every subprocess because they should already be created
        # and ensured by other services
        db_setup(ensure_indexes=False)

        # Note: We can only set a default user value if one is not provided after parsing the
        # config
        if not self._user:
            self._user = cfg.CONF.system_user.user
Beispiel #13
0
def main():
    args = _parse_args()
    if args.verbose:
        _setup_logging()
        output = logging.getLogger(__name__).info
    else:
        output = pprint.pprint

    rule_file_path = os.path.realpath(args.rule)
    trigger_instance_file_path = os.path.realpath(args.trigger_instance)

    tester = RuleTester(rule_file_path=rule_file_path,
                        trigger_instance_file_path=trigger_instance_file_path)
    matches = tester.evaluate()

    if matches:
        output('=== RULE MATCHES ===')
        sys.exit(0)
    else:
        output('=== RULE DOES NOT MATCH ===')
        sys.exit(1)
Beispiel #14
0
    def __init__(self, pack, file_path, parameters=None, user=None, parent_args=None):
        """
        :param pack: Name of the pack this action belongs to.
        :type pack: ``str``

        :param file_path: Path to the action module.
        :type file_path: ``str``

        :param parameters: action parameters.
        :type parameters: ``dict`` or ``None``

        :param user: Name of the user who triggered this action execution.
        :type user: ``str``

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

        self._pack = pack
        self._file_path = file_path
        self._parameters = parameters or {}
        self._user = user
        self._parent_args = parent_args or []
        self._class_name = None
        self._logger = logging.getLogger('PythonActionWrapper')

        try:
            config.parse_args(args=self._parent_args)
        except Exception:
            pass

        db_setup()

        # Note: We can only set a default user value if one is not provided after parsing the
        # config
        if not self._user:
            self._user = cfg.CONF.system_user.user
Beispiel #15
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(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')

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

        self._sensor_instance = self._get_sensor_instance()
Beispiel #16
0
from st2common.util.loader import register_callback_module
from st2common.util.api import get_full_public_api_url
from st2common.util.deprecation import deprecated

__all__ = [
    'ActionRunner',
    'AsyncActionRunner',
    'PollingAsyncActionRunner',
    'ShellRunnerMixin',

    'get_runner',
    'get_metadata'
]


LOG = logging.getLogger(__name__)

# constants to lookup in runner_parameters
RUNNER_COMMAND = 'cmd'


def get_runner(package_name, module_name, config=None):
    """
    Load the module and return an instance of the runner.
    """

    if not package_name:
        # Backward compatibility for Pre 2.7.0 where package name always equaled module name
        package_name = module_name

    LOG.debug('Runner loading Python module: %s.%s', package_name, module_name)
Beispiel #17
0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

from __future__ import absolute_import
from st2common import log as logging
from st2common.services.rules import get_rules_given_trigger
from st2common.services.triggers import get_trigger_db_by_ref
from st2reactor.rules.enforcer import RuleEnforcer
from st2reactor.rules.matcher import RulesMatcher
from st2common.metrics.base import get_driver

LOG = logging.getLogger('st2reactor.rules.RulesEngine')

__all__ = [
    'RulesEngine'
]


class RulesEngine(object):
    def handle_trigger_instance(self, trigger_instance):
        # Find matching rules for trigger instance.
        matching_rules = self.get_matching_rules_for_trigger(trigger_instance)

        if matching_rules:
            # Create rule enforcers.
            enforcers = self.create_rule_enforcers(trigger_instance, matching_rules)
from st2common.constants import action as action_constants
from st2common.constants.trace import TRACE_CONTEXT
from st2common.models.api.trace import TraceContext
from st2common.models.db.liveaction import LiveActionDB

from st2common.models.db.rule_enforcement import RuleEnforcementDB
from st2common.models.utils import action_param_utils
from st2common.models.api.auth import get_system_username
from st2common.persistence.rule_enforcement import RuleEnforcement
from st2common.services import action as action_service
from st2common.services import trace as trace_service
from st2common.util import reference
from st2common.util import action_db as action_db_util
from st2reactor.rules.datatransform import get_transformer

LOG = logging.getLogger('st2reactor.ruleenforcement.enforce')

EXEC_KICKED_OFF_STATES = [
    action_constants.LIVEACTION_STATUS_SCHEDULED,
    action_constants.LIVEACTION_STATUS_REQUESTED
]


class RuleEnforcer(object):
    def __init__(self, trigger_instance, rule):
        self.trigger_instance = trigger_instance
        self.rule = rule

        try:
            self.data_transformer = get_transformer(trigger_instance.payload)
        except Exception as e:
Beispiel #19
0
from st2common.constants.system import AUTH_TOKEN_ENV_VARIABLE_NAME
from st2common.constants.triggers import (SENSOR_SPAWN_TRIGGER, SENSOR_EXIT_TRIGGER)
from st2common.models.system.common import ResourceReference
from st2common.services.access import create_token
from st2common.transport.reactor import TriggerDispatcher
from st2common.util.api import get_full_public_api_url
from st2common.util.shell import on_parent_exit
from st2common.util.sandboxing import get_sandbox_python_path
from st2common.util.sandboxing import get_sandbox_python_binary_path
from st2common.util.sandboxing import get_sandbox_virtualenv_path

__all__ = [
    'ProcessSensorContainer'
]

LOG = logging.getLogger('st2reactor.process_sensor_container')

SUCCESS_EXIT_CODE = 0
FAILURE_EXIT_CODE = 1

BASE_DIR = os.path.dirname(os.path.abspath(__file__))
WRAPPER_SCRIPT_NAME = 'sensor_wrapper.py'
WRAPPER_SCRIPT_PATH = os.path.join(BASE_DIR, WRAPPER_SCRIPT_NAME)

# How many times to try to subsequently respawn a sensor after a non-zero exit before giving up
SENSOR_MAX_RESPAWN_COUNTS = 2

# How many seconds after the sensor has been started we should wait before considering sensor as
# being started and running successfuly
SENSOR_SUCCESSFUL_START_THRESHOLD = 10
Beispiel #20
0
from st2common.persistence.reactor import SensorType
from st2common.signal_handlers import register_common_signal_handlers
from st2reactor.sensor import config
from st2common.transport.utils import register_exchanges
from st2common.triggers import register_internal_trigger_types
from st2reactor.container.manager import SensorContainerManager

eventlet.monkey_patch(
    os=True,
    select=True,
    socket=True,
    thread=False if '--use-debugger' in sys.argv else True,
    time=True)


LOG = logging.getLogger('st2reactor.bin.sensors_manager')


def _setup():
    # Set up logger which logs everything which happens during and before config
    # parsing to sys.stdout
    logging.setup(DEFAULT_LOGGING_CONF_PATH)

    # 1. parse config args
    config.parse_args()

    # 2. setup logging.
    logging.setup(cfg.CONF.sensorcontainer.logging)

    # 3. all other setup which requires config to be parsed and logging to
    # be correctly setup.
Beispiel #21
0
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

import six
from jsonpath_rw import parse

from st2common import log as logging
import st2common.operators as criteria_operators
from st2common.constants.rules import TRIGGER_PAYLOAD_PREFIX, RULE_TYPE_BACKSTOP
from st2common.constants.system import SYSTEM_KV_PREFIX
from st2common.services.keyvalues import KeyValueLookup
from st2common.util.templating import render_template_with_system_context

LOG = logging.getLogger('st2reactor.ruleenforcement.filter')


class RuleFilter(object):
    def __init__(self, trigger_instance, trigger, rule, extra_info=False):
        """
        :param trigger_instance: TriggerInstance DB object.
        :type trigger_instance: :class:`TriggerInstanceDB``

        :param trigger: Trigger DB object.
        :type trigger: :class:`TriggerDB`

        :param rule: Rule DB object.
        :type rule: :class:`RuleDB`
        """
        self.trigger_instance = trigger_instance
Beispiel #22
0
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

from __future__ import absolute_import

from st2common import log as logging
from st2common.constants.triggers import TRIGGER_INSTANCE_PENDING
from st2common.exceptions.db import StackStormDBObjectNotFoundError
from st2common.models.db.trigger import TriggerInstanceDB
from st2common.persistence.trigger import TriggerInstance
from st2common.services.triggers import get_trigger_db_by_ref_or_dict

LOG = logging.getLogger('st2reactor.sensor.container_utils')


def create_trigger_instance(trigger, payload, occurrence_time, raise_on_no_trigger=False):
    """
    This creates a trigger instance object given trigger and payload.
    Trigger can be just a string reference (pack.name) or a ``dict`` containing 'id' or
    'uid' or type' and 'parameters' keys.

    :param trigger: Trigger reference or dictionary with trigger query filters.
    :type trigger: ``str`` or ``dict``

    :param payload: Trigger payload.
    :type payload: ``dict``
    """
    trigger_db = get_trigger_db_by_ref_or_dict(trigger=trigger)
Beispiel #23
0
# limitations under the License.

import json

from st2common import log as logging
from st2common.util import reference
from st2common.util import action_db as action_db_util
from st2reactor.rules.datatransform import get_transformer
from st2common.services import action as action_service
from st2common.models.db.liveaction import LiveActionDB
from st2common.models.utils import action_param_utils
from st2common.constants import action as action_constants
from st2common.models.api.auth import get_system_username


LOG = logging.getLogger('st2reactor.ruleenforcement.enforce')


class RuleEnforcer(object):
    def __init__(self, trigger_instance, rule):
        self.trigger_instance = trigger_instance
        self.rule = rule

        try:
            self.data_transformer = get_transformer(trigger_instance.payload)
        except Exception as e:
            message = ('Failed to template-ize trigger payload: %s. If the payload contains '
                       'special characters such as "{{" which dont\'t reference value in '
                       'a datastore, those characters need to be escaped' % (str(e)))
            raise ValueError(message)
Beispiel #24
0
 def get_logger(self, name):
     logging.getLogger(__name__ + '.' + name)
Beispiel #25
0
 def get_logger(self, name):
     from st2common import log as logging
     logging.getLogger(__name__ + '.' + name)
Beispiel #26
0
from st2common import log as logging
from st2common.models.db import db_setup
from st2common.models.db import db_teardown
from st2common.constants.logging import DEFAULT_LOGGING_CONF_PATH
from st2common.transport.utils import register_exchanges
from st2reactor.sensor import config
from st2common.persistence.reactor import SensorType
from st2reactor.container.manager import SensorContainerManager

eventlet.monkey_patch(os=True,
                      select=True,
                      socket=True,
                      thread=False if '--use-debugger' in sys.argv else True,
                      time=True)

LOG = logging.getLogger('st2reactor.bin.sensors_manager')


def _setup():
    # Set up logger which logs everything which happens during and before config
    # parsing to sys.stdout
    logging.setup(DEFAULT_LOGGING_CONF_PATH)

    # 1. parse config args
    config.parse_args()

    # 2. setup logging.
    logging.setup(cfg.CONF.sensorcontainer.logging)

    # 3. all other setup which requires config to be parsed and logging to
    # be correctly setup.
Beispiel #27
0
import paramiko
from fabric.operations import _execute as fabric_execute_cmd_blocking

from st2common import log as logging
from st2common.exceptions.connection import AuthenticationException
from st2common.exceptions.connection import (ConnectionErrorException, UnknownHostException)

eventlet.monkey_patch(
    os=True,
    select=True,
    socket=True,
    thread=False if '--use-debugger' in sys.argv else True,
    time=True
)

LOG = logging.getLogger('st2.util.ssh.paramikoclient')
# This implementation of SSH is heavily inspired by parallel-ssh which uses gvent instead of
# eventlet.


class SSHClient(object):
    '''
        Thread unsafe version of SSHClient.
    '''
    def __init__(self, host,
                 user=None, password=None, port=None,
                 key=None, connect_max_retries=2):
        ssh_config = paramiko.SSHConfig()
        _ssh_config_file = os.path.sep.join([os.path.expanduser('~'),
                                             '.ssh',
                                             'config'])
Beispiel #28
0
from collections import defaultdict
import fnmatch
import os
import re

from st2common import log as logging
from st2common.exceptions.plugins import IncompatiblePluginException
import st2common.util.loader as sensors_loader
from st2reactor.sensor.base import Sensor

LOG = logging.getLogger('st2reactor.bin.sensor_container')


class SensorLoader(object):
    # XXX: For now, let's just hardcode the includes & excludes pattern
    # here. We should eventually move these to config if that makes sense
    # at all.
    includes = ['*.py']
    excludes = ['*/__init__.py']
    # transform glob patterns to regular expressions
    _includes = r'|'.join([fnmatch.translate(x) for x in includes])
    _excludes = r'|'.join([fnmatch.translate(x) for x in excludes])

    def _get_sensor_files(self, base_dir):
        if not os.path.isdir(base_dir):
            raise Exception('Directory containing sensors must be provided.')
        sensor_files = []
        for (dirpath, dirnames, filenames) in os.walk(base_dir):
            # exclude/include files
            files = [os.path.join(dirpath, f) for f in filenames]
            files = [f for f in files if re.match(self._includes, f)]
Beispiel #29
0
# See the License for the specific language governing permissions and
# limitations under the License.

import os

import six

from st2common import log as logging
from st2common.exceptions.sensors import TriggerTypeRegistrationException
from st2common.persistence.reactor import SensorType, TriggerInstance
from st2common.models.db.reactor import SensorTypeDB, TriggerInstanceDB
from st2common.services import triggers as TriggerService
from st2common.constants.pack import SYSTEM_PACK_NAME
from st2common.constants.sensors import MINIMUM_POLL_INTERVAL

LOG = logging.getLogger('st2reactor.sensor.container_utils')


def create_trigger_instance(trigger, payload, occurrence_time):
    """
    This creates a trigger instance object given trigger and payload.
    Trigger can be just a string reference (pack.name) or a ``dict``
    containing  'type' and 'parameters'.

    :param trigger: Dictionary with trigger query filters.
    :type trigger: ``dict``

    :param payload: Trigger payload.
    :type payload: ``dict``
    """
    # TODO: This is nasty, this should take a unique reference and not a dict
Beispiel #30
0
from st2common.service_setup import teardown as common_teardown
from st2common.util.monkey_patch import monkey_patch
from st2common.exceptions.sensors import SensorNotFoundException
from st2common.constants.exit_codes import FAILURE_EXIT_CODE
from st2reactor.sensor import config
from st2reactor.container.manager import SensorContainerManager
from st2reactor.container.partitioner_lookup import get_sensors_partitioner

__all__ = [
    'main'
]

monkey_patch()

LOGGER_NAME = get_logger_name_for_module(sys.modules[__name__])
LOG = logging.getLogger(LOGGER_NAME)


def _setup():
    capabilities = {
        'name': 'sensorcontainer',
        'type': 'passive'
    }
    common_setup(service='sensorcontainer', config=config, setup_db=True,
                 register_mq_exchanges=True, register_signal_handlers=True,
                 register_runners=False, service_registry=True, capabilities=capabilities)


def _teardown():
    common_teardown()
Beispiel #31
0
from st2common.models.db import db_teardown
from st2common.constants.logging import DEFAULT_LOGGING_CONF_PATH
from st2common.transport.utils import register_exchanges
from st2common.signal_handlers import register_common_signal_handlers
from st2reactor.rules import config
from st2reactor.rules import worker
from st2reactor.timer.base import St2Timer

eventlet.monkey_patch(
    os=True,
    select=True,
    socket=True,
    thread=False if '--use-debugger' in sys.argv else True,
    time=True)

LOG = logging.getLogger('st2reactor.bin.rulesengine')


def _setup():
    # Set up logger which logs everything which happens during and before config
    # parsing to sys.stdout
    logging.setup(DEFAULT_LOGGING_CONF_PATH)

    # 1. parse config args
    config.parse_args()

    # 2. setup logging.
    logging.setup(cfg.CONF.rulesengine.logging)

    # 3. all other setup which requires config to be parsed and logging to
    # be correctly setup.
Beispiel #32
0
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

from __future__ import absolute_import
from st2common import log as logging
from st2common.constants.rules import RULE_TYPE_BACKSTOP
from st2reactor.rules.filter import RuleFilter, SecondPassRuleFilter

LOG = logging.getLogger('st2reactor.rules.RulesMatcher')


class RulesMatcher(object):
    def __init__(self, trigger_instance, trigger, rules, extra_info=False):
        self.trigger_instance = trigger_instance
        self.trigger = trigger
        self.rules = rules
        self.extra_info = extra_info

    def get_matching_rules(self):
        first_pass, second_pass = self._split_rules_into_passes()
        # first pass
        rule_filters = [RuleFilter(trigger_instance=self.trigger_instance,
                                   trigger=self.trigger,
                                   rule=rule,
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

from kombu import Connection
from st2common import log as logging
from st2common.transport import utils as transport_utils
from st2common.transport.connection_retry_wrapper import ConnectionRetryWrapper
from st2common.transport.execution import EXECUTION_XCHG
from st2common.transport.liveaction import LIVEACTION_XCHG
from st2common.transport.reactor import TRIGGER_CUD_XCHG, TRIGGER_INSTANCE_XCHG
from st2common.transport.reactor import SENSOR_CUD_XCHG

LOG = logging.getLogger('st2common.transport.bootstrap')

__all__ = [
    'register_exchanges'
]

EXCHANGES = [EXECUTION_XCHG, LIVEACTION_XCHG, TRIGGER_CUD_XCHG, TRIGGER_INSTANCE_XCHG,
             SENSOR_CUD_XCHG]


def _do_register_exchange(exchange, connection, channel, retry_wrapper):
    try:
        kwargs = {
            'exchange': exchange.name,
            'type': exchange.type,
            'durable': exchange.durable,
Beispiel #34
0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

from kombu import Connection
from oslo_config import cfg
from st2common import log as logging
from st2common.transport.execution import EXECUTION_XCHG
from st2common.transport.liveaction import LIVEACTION_XCHG
from st2common.transport.reactor import TRIGGER_CUD_XCHG, TRIGGER_INSTANCE_XCHG
from st2common.transport.reactor import SENSOR_CUD_XCHG

LOG = logging.getLogger("st2common.transport.bootstrap")

EXCHANGES = [EXECUTION_XCHG, LIVEACTION_XCHG, TRIGGER_CUD_XCHG, TRIGGER_INSTANCE_XCHG, SENSOR_CUD_XCHG]


def _do_register_exchange(exchange, channel):
    try:
        channel.exchange_declare(
            exchange=exchange.name,
            type=exchange.type,
            durable=exchange.durable,
            auto_delete=exchange.auto_delete,
            arguments=exchange.arguments,
            nowait=False,
            passive=None,
        )
Beispiel #35
0
from st2common.service_setup import setup as common_setup
from st2common.service_setup import teardown as common_teardown
from st2common.util.monkey_patch import monkey_patch
from st2common.constants.exit_codes import FAILURE_EXIT_CODE
from st2reactor.garbage_collector import config
from st2reactor.garbage_collector.base import GarbageCollectorService

__all__ = [
    'main'
]

monkey_patch()


LOGGER_NAME = get_logger_name_for_module(sys.modules[__name__])
LOG = logging.getLogger(LOGGER_NAME)


def _setup():
    common_setup(service='garbagecollector', config=config, setup_db=True,
                 register_mq_exchanges=True, register_signal_handlers=True)


def _teardown():
    common_teardown()


def main():
    try:
        _setup()
Beispiel #36
0
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

import six
from jsonpath_rw import parse

from st2common import log as logging
import st2common.operators as criteria_operators
from st2common.constants.rules import TRIGGER_PAYLOAD_PREFIX, RULE_TYPE_BACKSTOP
from st2common.constants.system import SYSTEM_KV_PREFIX
from st2common.services.keyvalues import KeyValueLookup
from st2common.util.templating import render_template_with_system_context


LOG = logging.getLogger('st2reactor.ruleenforcement.filter')


class RuleFilter(object):
    def __init__(self, trigger_instance, trigger, rule, extra_info=False):
        """
        :param trigger_instance: TriggerInstance DB object.
        :type trigger_instance: :class:`TriggerInstanceDB``

        :param trigger: Trigger DB object.
        :type trigger: :class:`TriggerDB`

        :param rule: Rule DB object.
        :type rule: :class:`RuleDB`
        """
        self.trigger_instance = trigger_instance
Beispiel #37
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,
            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()
Beispiel #38
0
# limitations under the License.

from kombu import Connection
from kombu.mixins import ConsumerMixin
from oslo.config import cfg

from st2actions.container.base import RunnerContainer
from st2common import log as logging
from st2common.exceptions.db import StackStormDBObjectNotFoundError
from st2common.constants.action import (ACTIONEXEC_STATUS_RUNNING, ACTIONEXEC_STATUS_FAILED)
from st2common.exceptions.actionrunner import ActionRunnerException
from st2common.transport import actionexecution, publishers
from st2common.util.action_db import (get_actionexec_by_id, update_actionexecution_status)
from st2common.util.greenpooldispatch import BufferedDispatcher

LOG = logging.getLogger(__name__)


ACTIONRUNNER_WORK_Q = actionexecution.get_queue('st2.actionrunner.work',
                                                routing_key=publishers.CREATE_RK)


class Worker(ConsumerMixin):

    def __init__(self, connection):
        self.connection = connection
        self.container = RunnerContainer()
        self._dispatcher = BufferedDispatcher()

    def shutdown(self):
        self._dispatcher.shutdown()
Beispiel #39
0
from st2common.models.db import db_teardown
from st2common.constants.logging import DEFAULT_LOGGING_CONF_PATH
from st2common.transport.utils import register_exchanges
from st2common.signal_handlers import register_common_signal_handlers
from st2reactor.rules import config
from st2reactor.rules import worker
from st2reactor.timer.base import St2Timer

eventlet.monkey_patch(
    os=True,
    select=True,
    socket=True,
    thread=False if '--use-debugger' in sys.argv else True,
    time=True)

LOG = logging.getLogger('st2reactor.bin.rulesengine')


def _setup():
    # Set up logger which logs everything which happens during and before config
    # parsing to sys.stdout
    logging.setup(DEFAULT_LOGGING_CONF_PATH)

    # 1. parse config args
    config.parse_args()

    # 2. setup logging.
    logging.setup(cfg.CONF.rulesengine.logging)

    # 3. all other setup which requires config to be parsed and logging to
    # be correctly setup.