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]

猜你喜欢

转载自blog.csdn.net/wlhdo71920145/article/details/80338798
今日推荐