StackStorm分析(八)Rule說明

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等開源社區,貢獻代碼和撰寫技術文檔。 
郵箱:[email protected]
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章