|
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
|
|
|
import sys |
|
17
|
|
|
|
|
18
|
|
|
from oslo_config import cfg |
|
19
|
|
|
|
|
20
|
|
|
from st2common import config |
|
21
|
|
|
from st2common import log as logging |
|
22
|
|
|
from st2common.script_setup import setup as common_setup |
|
23
|
|
|
from st2common.script_setup import teardown as common_teardown |
|
24
|
|
|
from st2reactor.rules.tester import RuleTester |
|
25
|
|
|
|
|
26
|
|
|
__all__ = [ |
|
27
|
|
|
'main' |
|
28
|
|
|
] |
|
29
|
|
|
|
|
30
|
|
|
LOG = logging.getLogger(__name__) |
|
31
|
|
|
|
|
32
|
|
|
|
|
33
|
|
|
def _do_register_cli_opts(opts, ignore_errors=False): |
|
34
|
|
|
for opt in opts: |
|
35
|
|
|
try: |
|
36
|
|
|
cfg.CONF.register_cli_opt(opt) |
|
37
|
|
|
except: |
|
38
|
|
|
if not ignore_errors: |
|
39
|
|
|
raise |
|
40
|
|
|
|
|
41
|
|
|
|
|
42
|
|
|
def _register_cli_opts(): |
|
43
|
|
|
cli_opts = [ |
|
44
|
|
|
cfg.StrOpt('rule', default=None, |
|
45
|
|
|
help='Path to the file containing rule definition.'), |
|
46
|
|
|
cfg.StrOpt('rule-ref', default=None, |
|
47
|
|
|
help='Ref of the rule.'), |
|
48
|
|
|
cfg.StrOpt('trigger-instance', default=None, |
|
49
|
|
|
help='Path to the file containing trigger instance definition'), |
|
50
|
|
|
cfg.StrOpt('trigger-instance-id', default=None, |
|
51
|
|
|
help='Id of the Trigger Instance to use for validation.') |
|
52
|
|
|
] |
|
53
|
|
|
_do_register_cli_opts(cli_opts) |
|
54
|
|
|
|
|
55
|
|
|
|
|
56
|
|
|
def main(): |
|
57
|
|
|
_register_cli_opts() |
|
58
|
|
|
common_setup(config=config, setup_db=True, register_mq_exchanges=False) |
|
59
|
|
|
|
|
60
|
|
|
try: |
|
61
|
|
|
tester = RuleTester(rule_file_path=cfg.CONF.rule, |
|
62
|
|
|
rule_ref=cfg.CONF.rule_ref, |
|
63
|
|
|
trigger_instance_file_path=cfg.CONF.trigger_instance, |
|
64
|
|
|
trigger_instance_id=cfg.CONF.trigger_instance_id) |
|
65
|
|
|
matches = tester.evaluate() |
|
66
|
|
|
finally: |
|
67
|
|
|
common_teardown() |
|
68
|
|
|
|
|
69
|
|
|
if matches: |
|
70
|
|
|
LOG.info('=== RULE MATCHES ===') |
|
71
|
|
|
sys.exit(0) |
|
72
|
|
|
else: |
|
73
|
|
|
LOG.info('=== RULE DOES NOT MATCH ===') |
|
74
|
|
|
sys.exit(1) |
|
75
|
|
|
|