StackStorm分析(三)Action說明

StackStorm介紹

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

 


 

Action

Action是StackStrom事件觸發後的處理方式,也就是自動化任務的執行體。

 

內置Action

StackStorm內置了許多Action,可以通過命令行查詢:

# List all available actions (note that output may be lengthy)
$ st2 action list
 
# List all actions in "linux" pack
$ st2 action list -p linux

 

比較常用的Action,比如core.local就是在本地執行Linux命令,我們可以查詢Action的詳細說明:

$ st2 action get core.local
+-------------+--------------------------------------------------------------+
| Property    | Value                                                        |
+-------------+--------------------------------------------------------------+
| id          | 5afa49082b2556016fc96b82                                     |
| uid         | action:core:local                                            |
| ref         | core.local                                                   |
| pack        | core                                                         |
| name        | local                                                        |
| description | Action that executes an arbitrary Linux command on the       |
|             | localhost.                                                   |
| enabled     | True                                                         |
| entry_point |                                                              |
| runner_type | local-shell-cmd                                              |
| parameters  | {                                                            |
|             |     "cmd": {                                                 |
|             |         "required": true,                                    |
|             |         "type": "string",                                    |
|             |         "description": "Arbitrary Linux command to be        |
|             | executed on the local host."                                 |
|             |     },                                                       |
|             |     "sudo": {                                                |
|             |         "immutable": true                                    |
|             |     }                                                        |
|             | }                                                            |
| notify      |                                                              |
| tags        |                                                              |
+-------------+--------------------------------------------------------------+

 

  • pack: Action歸屬的Pack,StackStorm中Action和Workflow,Rule和Sensor都歸屬於Pack。可以通過st2 packlist查詢Pack。
  • entry_point Action的執行入口,類似DockerENTRYPOINT
  • runner_type:Action的Action Runner類型,core.local的Runner是local-shell-cmd
  • parameters : Action的參數信息,core.local的參數有cmd和sudo,其中cmd是必須的,即需要運行的命令

 

熟悉Action的詳情後,就可以執行Action:

$ st2 run core.local cmd="echo test"
id: 5afa92a22b2556015687cccd
status: succeeded
parameters: 
  cmd: echo test
result: 
  failed: false
  return_code: 0
  stderr: ''
  stdout: test
  succeeded: true

 

除了使用st2 run執行Action,也可以使用st2action execute執行,不同的是execute是屬於異步的方式:

$ st2 action execute core.http url="http://httpbin.org/get"
To get the results, execute:
 st2 execution get 5afa93352b2556015687ccd3
 
To view output in real-time, execute:
 st2 execution tail 5afa93352b2556015687ccd3
 
$ st2 execution get 5afa93352b2556015687ccd3
id: 5afa93352b2556015687ccd3
status: succeeded (1s elapsed)
parameters: 
  url: http://httpbin.org/get
result: 
  body:
    args: {}
    headers:
      Accept: '*/*'
      Accept-Encoding: gzip, deflate
      Connection: close
      Host: httpbin.org
      User-Agent: st2/v2.7.1
      X-Stanley-Action: http
    origin: 36.251.248.174
    url: http://httpbin.org/get
  headers:
    Access-Control-Allow-Credentials: 'true'
    Access-Control-Allow-Origin: '*'
    Connection: keep-alive
    Content-Length: '224'
    Content-Type: application/json
    Date: Tue, 15 May 2018 07:58:45 GMT
    Server: gunicorn/19.8.1
    Via: 1.1 vegur
  parsed: true
  status_code: 200

 

自定義Action

         StackStorm除了內置Action,有可以定製Action,一個Action的元數據包括:

  • name - Name of the action.
  • runner_type - The type of runner to execute the action.
  • enabled - Action cannot be invoked when disabled.
  • entry_point - Location of the action launch script relative to the /opt/stackstorm/packs/${pack_name}/actions/directory.
  • parameters - A dictionary of parameters and optional metadata describing type and default. The metadata is structured data following the JSON Schema specification draft 4. The common parameter types allowed are stringbooleannumber (whole numbers and decimal numbers - e.g. 1.013.3333, etc.), objectinteger (whole numbers only -11000, etc.) and array. If metadata is provided, input args are validated on action execution. Otherwise, validation is skipped.

 

        StackStorm提供了對Shell的支持,可以非常方便地將已有的腳本接入StackStorm。現在有一個Shell腳本script.sh
#!/usr/bin/env bash
 
MESSAGE=$1
echo ${MESSAGE}

 

然後創建Action的元數據文件my-action.yaml:

---
name: "my-action"
runner_type: "local-shell-script"
description: "Echo a message"
enabled: true
entry_point: "script.sh"
parameters:
    message:
        type: "string"
        description: "Message to write"
        required: true
        position: 0

 

創建Action:

$ st2 action create my-action.yaml 

+-------------+-------------------------------------------+

| Property    | Value                                     |

+-------------+-------------------------------------------+

| id          | 5afbe8f02b2556015687ce43                  |

| name        | my-action                                 |

| pack        | default                                   |

| description | Echo a message                            |

| enabled     | True                                      |

| entry_point | script.sh                                 |

| notify      |                                           |

| parameters  | {                                         |

|             |     "message": {                          |

|             |         "position": 0,                    |

|             |         "required": true,                 |

|             |         "type": "string",                 |

|             |         "description": "Message to write" |

|             |     }                                     |

|             | }                                         |

| ref         | default.my-action                         |

| runner_type | local-shell-script                        |

| tags        |                                           |

| uid         | action:default:my-action                  |

+-------------+-------------------------------------------+

 

默認的Pack是default,需要將shell腳本拷貝到/opt/stackstorm/packs/default/actions/script.sh。

 

然後執行這個Action:

$ st2 run default.my-action message="Hello1"

.

id: 5afbe9bc2b2556015687ce4e

status: succeeded

parameters:

  message: Hello1

result:

  failed: false

  return_code: 0

  stderr: ''

  stdout: Hello1

  succeeded: true

 

ActionRunner

       ActionRunner是Action的執行環境, Action Runner實際上就是一系列的工作進程,ActionRunner根據StackStorm調度來進行執行Action,可以通過命令查詢到進程:

$ st2ctl status
##### st2 components status #####
st2actionrunner PID: 93
st2actionrunner PID: 97
st2actionrunner PID: 100
st2actionrunner PID: 103
st2actionrunner PID: 109
st2actionrunner PID: 116
st2actionrunner PID: 122
st2actionrunner PID: 127
st2actionrunner PID: 130
st2actionrunner PID: 138
st2actionrunner PID: 143
st2actionrunner PID: 147

 

       StackStorm支持不同類型的執行方式,比如Shell腳本,Python腳本或者HTTP調用等等,不難理解這些是以插件的形式存在的,StackStorm內置了基本的一些Action Runner,如下表所示。除了StackStorm的內置類型ActionRunner,當然用戶也可以自定義ActionRunner。

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. 

inquirer

This runner provides the core logic of the :doc:`Inquiries </inquiries>` feature.

 

ActionChain

       Workflow是Action集合,Workflow能夠定義Action的執行順序和條件,組合一系列Action完成複雜的任務。StackStorm支持2種類型的Workflow,其中ActionChain是StackStorm定義的Action鏈。

      

創建ActionChain需要提供元數據文件echochain.meta.yaml:

---

# Action definition metadata

name: "echochain"

description: "Simple Action Chain workflow"

 

# `runner_type` has value `action-chain` to identify that action is an ActionChain.

runner_type: "action-chain"

 

# `entry_point` path to the ActionChain definition file, relative to the pack's action directory.

entry_point: "chains/echochain.yaml"

 

enabled: true

parameters:

  skip_notify:

    default:

      - c2

notify:

  on-complete:

    message: "\"@channel: Action succeeded.\""

    routes:

      - "slack"

 

       ActionChain本質也是一種Action,這個ActionChain元數據文件上也是就Action的規格文件,其中runner_type指定爲action-chain,entry_point指定的是ActionChain的規則文件echochain.yaml:

---

    chain:

        -

            name: "c1"

            ref: "core.local"

            parameters:

                cmd: "echo c1"

            on-success: "c2"

            on-failure: "c4"

        -

            name: "c2"

            ref: "core.local"

            parameters:

                cmd: "echo \"c2: parent exec is {{action_context.parent.execution_id}}.\""

            on-success: "c3"

            on-failure: "c4"

        -

            name: "c3"

            ref: "core.local"

            parameters:

                cmd: "echo c3&&exit 1"

            on-failure: "c4"

        -

            name: "c4"

            ref: "core.local"

            parameters:

                cmd: "echo fail c4"

    default: "c1"

 

ActionChain的規則文件定了4個子任務:c1,c2,c3和c4,包括每個子任務的運行類型和參數,然後還有成功或者失敗後的執行步驟,這樣一來就構成了工作流。

 

注意:默認創建的Pack是default, 對應的Pack目錄在/opt/stackstorm/packs/default,需要把echochain.yaml拷貝到

/opt/stackstorm/packs/default/actions/chains/echochain.yaml

 

創建ActionChain:

$ st2 action create echochain.meta.yaml

 

執行ActionChain:

$ st2 run default.echochain
...
id: 5afaab9c2b2556015687cd7b
action.ref: default.echochain
parameters: None
status: succeeded
result_task: c4
result: 
  failed: false
  return_code: 0
  stderr: ''
  stdout: fail c4
  succeeded: true
start_timestamp: Tue, 15 May 2018 09:42:52 UTC
end_timestamp: Tue, 15 May 2018 09:42:57 UTC
+--------------------------+------------------------+------+------------+-------------------------------+
| id                       | status                 | task | action     | start_timestamp               |
+--------------------------+------------------------+------+------------+-------------------------------+
| 5afaab9c2b2556007f0afa2b | succeeded (1s elapsed) | c1   | core.local | Tue, 15 May 2018 09:42:52 UTC |
| 5afaab9d2b2556007f0afa2d | succeeded (1s elapsed) | c2   | core.local | Tue, 15 May 2018 09:42:53 UTC |
| 5afaab9f2b2556007f0afa2f | failed (0s elapsed)    | c3   | core.local | Tue, 15 May 2018 09:42:55 UTC |
| 5afaaba02b2556007f0afa31 | succeeded (0s elapsed) | c4   | core.local | Tue, 15 May 2018 09:42:56 UTC |
+--------------------------+------------------------+------+------------+-------------------------------+

 

可以查詢子任務的運行情況:

$ st2 execution get 5afb88102b25560074358c7c   
id: 5afb88102b25560074358c7c
status: succeeded (0s elapsed)
parameters: 
  cmd: 'echo "c2: parent exec is 5afb880e2b2556015687cd7e."'
result: 
  failed: false
  return_code: 0
  stderr: ''
  stdout: 'c2: parent exec is 5afb880e2b2556015687cd7e.'
  succeeded: true

 

可以重跑失敗的子任務:

$ st2 execution re-run 5afb88112b25560074358c7e
.
id: 5afb8cf02b2556015687cd82
status: failed
parameters: 
  cmd: echo c3&&exit 1
result: 
  failed: true
  return_code: 1
  stderr: ''
  stdout: c3
  succeeded: false

 

 

參考


作者簡介

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