StackStorm分析(一)StackStorm介紹

StackStorm介紹

       StackStorm是一個強大的自動化平臺,結合DevOpsChatOps,提供可擴展、靈活和健壯的工具鏈用於應用、服務和工作流的自動化能力。

 


 

StackStorm核心概念

 

StackStorm的工作步驟大體如下:

       1. StackStorm Sensor感應並觸發事件。

       2. Rules Engine對事件進行規則匹配,如果匹配產生任務。

       3. StackStorm Worker執行任務,一般是調用到外部系統。

       4. StackStorm記錄審計任務執行的細節。

       5.任務執行結果返回給Rules Engine進行進一步處理。


 

       可以看出StackStorm是個以事件驅動的系統,爲此抽象出一系列概念來分解事件從產生、觸發、規則匹配到執行的整個生命週期事件,具體包含核心概論如下:

Sensor感應器

       Sensor是一系列的感應器用於接受或者監測事件,當事件發生的時候,Sensor將會通知Trigger提交事件到StackStorm。

       Sensor是Python插件實現,只要實現StackStorm定義的接口,然後配置元數據YAML註冊到StackStorm:

---
  class_name:"SampleSensor"
  entry_point:"sample_sensor.py"
  description:"Sample sensor that emits triggers."
  trigger_types:
    -
      name:"event"
      description:"An example trigger."
      payload_schema:
        type:"object"
        properties:
          executed_at:
            type:"string"
            format:"date-time"
            default:"2014-07-30 05:04:24.578325"

 

 

Trigger觸發器

       Trigger代表事件,一般事件是由外部系統產生,比如監控告警,JIRA問題更新等等,另外也有通用的事件觸發器,比如定時器或者WebHook。

       在StackStorm系統中,Trigger只是String類型的對象,由Sensor註冊,用戶可以在Sensor插件自定義新的Trigger。

 

Action 動作/任務

Action是事件觸發後的處理方式,一般是由外部系統執行,包括:

  • 重啓服務
  • 創建雲服務
  • 發生郵件
  • 啓動Docker容器
  • 製作VM快照

 

       Action可以是通用的執行方式,比如SSH,REST API調用,也能夠集成Openstack、Docker/Kubernetes等系統實現。Action Runner是Action的執行環境,StackStorm的內置Action Runner

Action Runner

Description

local-shell-cmd

This is the local runner. This runner executes a Linux command on the same host where StackStorm components are running.

local-shell-script

 This is the local runner. Actions are implemented as scripts. They are executed on the same hosts where StackStorm components are running.

remote-shell-cmd

This is a remote runner. This runner executes a Linux command on one or more remote hosts provided by the user.

remote-shell-script

This is a remote runner. Actions are implemented as scripts. They run on one or more remote hosts provided by the user.

python-script

This is a Python runner. Actions are implemented as Python classes with arun method. They run locally on the same machine where StackStorm components are running.

http-request

HTTP client which performs HTTP requests for running HTTP actions.

action-chain

This runner supports executing simple linear work-flows.

mistral-v2

Those runners are built on top of the Mistral OpenStack project and support executing complex work-flows. 

cloudslang

This runner is built on top of the CloudSlang project and supports executing complex workflows. 

 

       通過ActionRunner用戶可以自定義Action的實現,以下是一個python-script類型的Action用於發送SMS:

---

name:"send_sms"

runner_type:"python-script"

description:"ThissendsanSMSusingtwilio."

enabled:true

entry_point:"send_sms.py"

parameters:

    from_number:

        type:"string"

        description:"Yourtwilio'from'numberinE.164format.Example+14151234567."

        required:true

        position:0

    to_number:

        type:"string"

        description:"RecipientnumberinE.164format.Example+14151234567."

        required:true

        position:1

        secret:true

    body:

        type:"string"

        description:"Bodyofthemessage."

        required:true

        position:2

        default:"Hello{%ifsystem.user%}{{system.user}}{%else%}dude{%endif%}!"

 

Workflow 工作流

       Workflow是Action集合,Workflow能夠定義Action的執行順序和條件,組合一系列Action完成複雜的任務。Workflow可以認爲是廣義意義上的Action。

 

StackStorm支持2種類型的Workflow:

  • ActionChain:通過簡單的語法定義Action鏈
---
    chain:
        -  name: "c1"
            ref: "core.local"
            parameters:
                cmd: "echoc1"
            on-success: "c2"
            on-failure: "c4"
        - name:"c2"
            ref: "core.local"
            parameters:
                cmd: "echo\"c2:parentexecis{{action_context.parent.execution_id}}.\""
            on-success: "c3"
            on-failure: "c4"
        - name:"c3"
            ref: "core.local"
            parameters:
                cmd: "echoc3"
            on-failure: "c4"
        - name:"c4"
            ref: "core.local"
            parameters:
                cmd: "echofailc4"
    default: "c1"

 

  • Mistral :Openstack的工作流組件,可以同Stackstorm集成,支持複雜的工作流配置。
version: '2.0'
               examples.mistral-join:
                   description: >
                       A sample workflow that demonstrates how to join parallel branches.
                   type: direct
                   tasks:
                       a:
                           action: core.local
                           input:
                               cmd: "echo 'a'"
                           on-success:
                               - b
                               - c
                               - d
                       b:
                           action: core.local
                           input:
                               cmd: "echo 'b'"
                           on-success:
                               - e
                       c:
                           action: core.local
                           input:
                               cmd: "echo 'c'"
                           on-success:
                               - e
                       d:
                           action: core.local
                           input:
                               cmd: "echo 'd'"
                           on-success:
                               - e
                       e:
                           join: all
                           action: core.local
                           input:
                               cmd: "echo 'e'"

 

Rule 規則

       Rule是映射Trigger到Action(或者Workflow),即當事件觸發後,通過Rule定義的標準(Criteria)進行匹配,當匹配成功將執行Action(或者Workflow)。

 

 Rule的定義格式:

---
    name: "rule_name"                      # required
    pack: "examples"                       # optional
    description: "Ruledescription."       # 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}}"

 

      

Audit 審計

Audit是用來跟蹤和記錄Action的執行細節,用於查詢定位:

{
    "status": "succeeded",
    "start_timestamp": "2014-10-31T02:00:46.679000Z",
    "parameters": {
        "cmd": "ifconfig"
    },
    "callback": {},
    "result": {
        ...
    },
    "context": {
        "user": "stanley"
    },
    "action": "core.local",
    "id": "5452ed4e0640fd6b59e75908"
}

 

ChatOps

       ChatOps是一種新的DevOps方法,ChatOps是誕生於GitHub的一種基於會話驅動的協作開發方法,過去團隊之間的通訊和開發操作是兩層皮,導致各種不透明和低效率。ChatOps將開發工具帶入開發者聊天室,通過定製的插件和腳本,一個聊天機器人能夠執行聊天中輸入的各種命令,實現在聊天平臺上的團隊協作開發自動化,把團隊溝通和執行統一整合到一個可視化更高的聊天環境中,“聊着天就把事情辦了”。

       目前流行的ChatOps聊天機器人主要有HubotGitHubbot,用CoffeeScriptNode.js開發)、Lita(用Ruby開發)和Err(用Python開發)三種,都是開源軟件,而且可以整合到開發團隊在工作中經常會使用一些聊天工具例如HipChatSlackFlowdockCampfire等。

       StackStorm中集成了Hubot作爲聊天機器人提供ChatOps,同時提供Action Alias Notifications 機制實現更好的體驗,如下圖所示:

 

 

StackStorm ChatOps的流程如下:

Phase 1Bot Initialiazation

聊天機器人會調用StackStorm API下載Action Alias信息進行初始化。

Phase 2Alias Execution

初始化完成後,聊天機器人將服務在聊天頻道等待命令,一旦接受到命令,將Action Alias轉化爲Action,併發送給StackStorm進行處理。

Phase 3: ActionExecution

StackStorm處理執行Action,將執行輸出發送給ActionRunner,並轉發給Notifications子系統。

Phase 4: ActionCompetition

Action執行完成後,將返回Notifications

Phase 5: NotificationsReply

聊天機器人收到Notifications進行回覆。


示例:


參考:https://docs.stackstorm.com/_images/chatops_demo.gif

 

 


 

參考

  • https://docs.stackstorm.com/index.html


作者簡介

吳龍輝,現任網宿科技雲計算架構師,致力於雲計算PaaS的研究和實踐,《Kubernetes實戰》作者,活躍於CloudFoundry,Docker,Kubernetes等開源社區,貢獻代碼和撰寫技術文檔。 
郵箱:[email protected]
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章