|
1
|
|
|
# Licensed to the StackStorm, Inc ('StackStorm') under one or more |
|
2
|
|
|
# contributor license agreements. See the NOTICE file distributed with |
|
3
|
|
|
# this work for additional information regarding copyright ownership. |
|
4
|
|
|
# The ASF licenses this file to You under the Apache License, Version 2.0 |
|
5
|
|
|
# (the "License"); you may not use this file except in compliance with |
|
6
|
|
|
# the License. You may obtain a copy of the License at |
|
7
|
|
|
# |
|
8
|
|
|
# http://www.apache.org/licenses/LICENSE-2.0 |
|
9
|
|
|
# |
|
10
|
|
|
# Unless required by applicable law or agreed to in writing, software |
|
11
|
|
|
# distributed under the License is distributed on an "AS IS" BASIS, |
|
12
|
|
|
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
|
13
|
|
|
# See the License for the specific language governing permissions and |
|
14
|
|
|
# limitations under the License. |
|
15
|
|
|
|
|
16
|
|
|
from st2common import log as logging |
|
17
|
|
|
from st2common.services.rules import get_rules_given_trigger |
|
18
|
|
|
from st2common.services.triggers import get_trigger_db_given_type_and_params |
|
|
|
|
|
|
19
|
|
|
from st2common.services.triggers import get_trigger_db_by_ref |
|
20
|
|
|
from st2reactor.rules.enforcer import RuleEnforcer |
|
21
|
|
|
from st2reactor.rules.matcher import RulesMatcher |
|
22
|
|
|
|
|
23
|
|
|
LOG = logging.getLogger('st2reactor.rules.RulesEngine') |
|
24
|
|
|
|
|
25
|
|
|
|
|
26
|
|
|
class RulesEngine(object): |
|
27
|
|
|
def handle_trigger_instance(self, trigger_instance): |
|
28
|
|
|
# Find matching rules for trigger instance. |
|
29
|
|
|
matching_rules = self.get_matching_rules_for_trigger(trigger_instance) |
|
30
|
|
|
|
|
31
|
|
|
if matching_rules: |
|
32
|
|
|
# Create rule enforcers. |
|
33
|
|
|
enforcers = self.create_rule_enforcers(trigger_instance, matching_rules) |
|
34
|
|
|
|
|
35
|
|
|
# Enforce the rules. |
|
36
|
|
|
self.enforce_rules(enforcers) |
|
37
|
|
|
else: |
|
38
|
|
|
LOG.info('No matching rules found for trigger instance %s.', trigger_instance['id']) |
|
39
|
|
|
|
|
40
|
|
|
def get_matching_rules_for_trigger(self, trigger_instance): |
|
41
|
|
|
trigger = trigger_instance.trigger |
|
42
|
|
|
|
|
43
|
|
|
trigger_db = get_trigger_db_by_ref(trigger_instance.trigger) |
|
44
|
|
|
|
|
45
|
|
|
if not trigger_db: |
|
46
|
|
|
LOG.error('No matching trigger found in db for trigger instance %s.', trigger_instance) |
|
47
|
|
|
return None |
|
48
|
|
|
|
|
49
|
|
|
rules = get_rules_given_trigger(trigger=trigger) |
|
50
|
|
|
|
|
51
|
|
|
LOG.info('Found %d rules defined for trigger %s', len(rules), |
|
52
|
|
|
trigger_db.get_reference().ref) |
|
53
|
|
|
|
|
54
|
|
|
if len(rules) < 1: |
|
55
|
|
|
return rules |
|
56
|
|
|
|
|
57
|
|
|
matcher = RulesMatcher(trigger_instance=trigger_instance, |
|
58
|
|
|
trigger=trigger_db, rules=rules) |
|
59
|
|
|
|
|
60
|
|
|
matching_rules = matcher.get_matching_rules() |
|
61
|
|
|
LOG.info('Matched %s rule(s) for trigger_instance %s (trigger=%s)', len(matching_rules), |
|
62
|
|
|
trigger_instance['id'], trigger_db.ref) |
|
63
|
|
|
return matching_rules |
|
64
|
|
|
|
|
65
|
|
|
def create_rule_enforcers(self, trigger_instance, matching_rules): |
|
66
|
|
|
""" |
|
67
|
|
|
Creates a RuleEnforcer matching to each rule. |
|
68
|
|
|
|
|
69
|
|
|
This method is trigger_instance specific therefore if creation of 1 RuleEnforcer |
|
70
|
|
|
fails it is likely that all wil be broken. |
|
71
|
|
|
""" |
|
72
|
|
|
enforcers = [] |
|
73
|
|
|
for matching_rule in matching_rules: |
|
74
|
|
|
enforcers.append(RuleEnforcer(trigger_instance, matching_rule)) |
|
75
|
|
|
return enforcers |
|
76
|
|
|
|
|
77
|
|
|
def enforce_rules(self, enforcers): |
|
78
|
|
|
for enforcer in enforcers: |
|
79
|
|
|
try: |
|
80
|
|
|
enforcer.enforce() # Should this happen in an eventlet pool? |
|
81
|
|
|
except: |
|
82
|
|
|
LOG.exception('Exception enforcing rule %s.', enforcer.rule) |
|
83
|
|
|
|