StackStorm分析(八)Rule说明

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/wlhdo71920145/article/details/80352382

StackStorm介绍

       StackStorm是一个强大的自动化平台,结合DevOpsChatOps,提供可扩展、灵活和健壮的工具链用于应用、服务和工作流的自动化能力。

 


 

Rule

       Rule是映射Trigger到Action(或者Workflow),即当事件触发后,通过Rule定义的标准(Criteria)进行匹配,当匹配成功将执行Action(或者Workflow)。

 

Rule的定义格式:

---
    name: "rule_name"                      # required
    pack: "examples"                       # optional
    description: "Rule description."       # optional
    enabled: true                          # required
 
    trigger:                               # required
        type: "trigger_type_ref"
 
    criteria:                              # optional
        trigger.payload_parameter_name1:
            type: "regex"
            pattern : "^value$"
        trigger.payload_parameter_name2:
            type: "iequals"
            pattern : "watchevent"
 
    action:                                # required
        ref: "action_ref"
        parameters:                        # optional
            foo: "bar"
            baz: "{{ trigger.payload_parameter_1 }}"

 

  • name: Rule的名称
  • pack: Rule归属的Pack
  • enabled: Rule的启用开关
  • trigger: 配置Trigger的类型和参数
  • criteria:Rule的匹配规则,当 Trigger的参数满足的条件触发Action
  • action: Rule匹配需要执行的Action配置

 

我们可以通过命令行创建Rule

$ st2 rule create /usr/share/doc/st2/examples/rules/sample_rule_with_webhook.yaml

 

查询Rule

$ st2 rule list
$ st2 rule get examples.sample_rule_with_webhook

 

Criteria

Rule的Criteria 提供了一系列方法,可以设置各种匹配规则:

  • equals Values are equal (for values of arbitrary type).
  • nequals Values are not equal (for values of arbitrary type).
  • lessthan Trigger value is less than the provided value.
  • greaterthan Trigger value is greater than the provided value.
  • matchwildcard Trigger value matches the provided wildcard-like string. This operator provides support for Unix shell-style wildcards which means you can use characters such as * and ?. This operator is preferred over regex for simple string matches.
  • regex Trigger value matches the provided regular expression pattern. This operator behaves like re.search('pattern', trigger_value).
  • iregex Trigger value matches the provided regular expression pattern case insensitively. This operator behaves like re.search('pattern', trigger_value, re.IGNORECASE).
  • matchregex Trigger value matches the provided regular expression pattern. This operator is deprecated in favor of regex and iregex
  • iequals String trigger value equals the provided value case insensitively.
  • contains Trigger value contains the provided value. Keep in mind that the trigger value can be either a string or an array (list).
  • ncontains Trigger value does not contain the provided value. Keep in mind that the trigger value can be either a string or an array (list).
  • icontains String trigger value contains the provided value case insensitively.
  • incontains String trigger value does not contain the provided string value case insensitively.
  • startswith Beginning of the string trigger value matches the provided string value.
  • istartswith Beginning of the string trigger value matches the provided string value case insensitively.
  • endswith End of the string trigger value matches the provided string value.
  • iendswith End of the string trigger value matches the provided string value case insensitively.
  • timediff_lt Time difference between trigger value and current time is less than the provided value.
  • timediff_gt Time difference between trigger value and current time is greater than the provided value.
  • exists Key exists in payload.
  • nexists Key doesn't exist in payload.
  • inside Trigger payload is inside provided value. (e.g. testing if "trigger.payload in provided_value"). Reverse of contains. (where contains would test for "trigger.payload contains provided_value").
  • ninside Trigger payload is not inside provided value. (e.g. testing if "trigger.payload not in provided_value"). Reverse of ncontains (where contains would test for "trigger.payload does not contain provided_value").
  • search Search an array (list) in the trigger payload that matches child criteria. See the Advanced Comparison section for more information and examples.


测试

StackStorm提供一个工具st2-rule-tester可以用于快速测试Rule。

 

首先创建Rule:

st2-rule-tester

---
  name: "relayed_matched_irc_message"
  pack: "irc"
  description: "Relay IRC message to Slack if the message contains word StackStorm"
  enabled: true
 
  trigger:
    type: "irc.pubmsg"
    parameters: {}
 
  criteria:
      trigger.message:
          type: "icontains"
          pattern: "StackStorm"
 
  action:
    ref: "slack.post_message"
    parameters:
        message: "{{ trigger.source.nick }} on {{ trigger.channel }}: {{ trigger.message }}"
        channel: "#irc-relay"

         这个Rule是匹配Trigger(irc.pubmsg)的参数message是否含有字符串StackStorm(不区分大小写),匹配后执行Action(slack.post_message)。

 

然后创建用于测试的Trigger Instance:

trigger_instance_1.yaml message包含字符串StackStorm,匹配Rule

---
    trigger: "irc.pubmsg"
    payload:
      source:
          nick: "Kami_"
          host: "gateway/web/irccloud.com/x-uvv"
      channel: "#stackstorm"
      timestamp: 1419166748,
      message: "stackstorm is cool!"

 

trigger_instance_2.yaml:不匹配Rule

---
    trigger: "irc.pubmsg"
    payload:
      source:
          nick: "Kami_"
          host: "gateway/web/irccloud.com/x-uvv"
      channel: "#stackstorm"
      timestamp: 1419166748,
      message: "blah blah"

 

执行测试

Trigger Instace 1匹配Rule并执行Action

$ st2-rule-tester --rule=./my_rule.yaml --trigger-instance=./trigger_instance_1.yaml --config-file=/etc/st2/st2.conf

2018-05-17 06:44:17,972 INFO [-] Connecting to database "st2" @ "mongo:27017" as user "None".

2018-05-17 06:44:18,144 INFO [-] Validating rule irc.relayed_matched_irc_message for pubmsg.

2018-05-17 06:44:18,169 INFO [-] 1 rule(s) found to enforce for pubmsg.

2018-05-17 06:44:18,170 INFO [-] Failed to retrieve config for pack <Mock name='mock.pack' id='140111251335056'> and user stanley: 'Mock' object is not iterable

2018-05-17 06:44:18,173 INFO [-] Action parameters resolved to:

2018-05-17 06:44:18,173 INFO [-]    message: Kami_ on #stackstorm: stackstorm is cool!

2018-05-17 06:44:18,174 INFO [-]    channel: #irc-relay

2018-05-17 06:44:18,174 INFO [-] === RULE MATCHES ===

 

Trigger Instace 2不匹配Rule

$ st2-rule-tester --rule=./my_rule.yaml --trigger-instance=./trigger_instance_2.yaml --config-file=/etc/st2/st2.conf

2018-05-17 06:47:27,426 INFO [-] Connecting to database "st2" @ "mongo:27017" as user "None".

2018-05-17 06:47:27,605 INFO [-] Validating rule irc.relayed_matched_irc_message for pubmsg.

2018-05-17 06:47:27,627 INFO [-] Validation for rule irc.relayed_matched_irc_message failed on criteria -

  key: trigger.message

  pattern: StackStorm

  type: icontains

  payload: blah blah

2018-05-17 06:47:27,627 INFO [-] 0 rule(s) found to enforce for pubmsg.

2018-05-17 06:47:27,628 INFO [-] === RULE DOES NOT MATCH ===

另外也可以根据实际的Trigger Instance去测试:

$ st2-rule-tester --rule-ref=my_pack.fire_on_execution --trigger-instance-id=566b4be632ed352a09cd347d --config-file=/etc/st2/st2.conf

 

参考

  • https://github.com/StackStorm/st2docs/blob/master/docs/source/rules.rst

作者简介

吴龙辉,现任网宿科技云计算架构师,致力于云计算PaaS的研究和实践,《Kubernetes实战》作者,活跃于CloudFoundry,Docker,Kubernetes等开源社区,贡献代码和撰写技术文档。 
邮箱:wlh6666@qq.com
阅读更多
想对作者说点什么?

博主推荐

换一批

没有更多推荐了,返回首页