Jamal的博客

StackStorm Learning - Actions

这个系列是翻译的StackStrom官方文档。

因为工具不太方便加之前写过的系统稳定头,所以只做追加。
系列文章
总览
安装
Actios

概述

Action是一个动作,用来执行一些自动化的任务,例如重启机器、创建一个新机器、重启一个镜像等,st2内置了很多action,我们也可以使用任意的语言自定义自己的action。
在st2中,action是trigger被触发后,通过rule规则判断后执行的动作,action可以是一个单一的动作(例如重启机器),也可以多个action合起来成为一个workflow(例如先检查当前业务受损情况,然后重启机器,然后观察业务恢复情况这样一个流程),workflow本质上也是一个action。action可以通过cli、api和web ui来触发。

内置action

通过简单的命令查询action:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
root@5b0c7bc6be62:/# st2 action list -p core
+-------------------+------+-----------------------------------------------------------+
| ref | pack | description |
+-------------------+------+-----------------------------------------------------------+
| core.announcement | core | Action that broadcasts the announcement to all stream |
| | | consumers. |
| core.ask | core | Action for initiating an Inquiry (usually in a workflow) |
| core.echo | core | Action that executes the Linux echo command on the |
| | | localhost. |
| core.http | core | Action that performs an http request. |
| core.local | core | Action that executes an arbitrary Linux command on the |
| | | localhost. |
| core.local_sudo | core | Action that executes an arbitrary Linux command on the |
| | | localhost. |
| core.noop | core | Action that does nothing |
| core.pause | core | Action to pause current thread of workflow/sub-workflow. |
| core.remote | core | Action to execute arbitrary linux command remotely. |
| core.remote_sudo | core | Action to execute arbitrary linux command remotely. |
| core.sendmail | core | This sends an email |
| core.uuid | core | Generate a new UUID (default uuid4) |
| core.windows_cmd | core | Action to execute arbitrary Windows command remotely. |
+-------------------+------+-----------------------------------------------------------+

-p代表的是core这个pack的action。

查看action详情

看一下core.local这个action的详细描述:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
root@5b0c7bc6be62:/# st2 action get core.local
+-------------+--------------------------------------------------------------+
| Property | Value |
+-------------+--------------------------------------------------------------+
| id | 5b8a4832feeadc010e616fd1 |
| 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的执行入口,类似Docker的ENTRYPOINT。
  • runner_type:Action的Action Runner类型,core.local的Runner是local-shell-cmd
  • parameters : Action的参数信息,core.local的参数有cmd和sudo,其中cmd是必须的,即需要运行的命令
执行具体的action
1
2
3
4
5
6
7
8
9
10
11
12
root@5b0c7bc6be62:/# st2 run core.local cmd="echo test"
.
id: 5b95ee1ec8daae032cbb3c85
status: succeeded
parameters:
cmd: echo test
result:
failed: false
return_code: 0
stderr: ''
stdout: test
succeeded: true

我们还可以查看具体每个action的执行详情:

1
2
3
4
5
6
7
8
9
10
11
root@5b0c7bc6be62:/# st2 execution get 5b95ee1ec8daae032cbb3c85
id: 5b95ee1ec8daae032cbb3c85
status: succeeded (1s elapsed)
parameters:
cmd: echo test
result:
failed: false
return_code: 0
stderr: ''
stdout: test
succeeded: true

在action详情中我们说到了runner_type,runner_type决定了action跑在什么环境中,目前st2提供了以下几个runner:

  • local-shell-cmd 是一个local runner,表示在当前服务器上执行shell命令
  • local-shell-script 表示在本地执行shell脚本
  • remote-shell-cmd 远程执行shell命令
  • remote-shell-script 远程执行shell脚本
  • python-script python脚本,实现action的run方法就可以,是一个本地跑的脚本
  • http-request:http请求
  • action-chain: action执行链,st2自己的workflow,只支持线性workflow
  • mistral-v2:openstack的一个流程引擎,支持复杂的workflow规则
  • cloudslang:另一个流程引擎,java编写,详情可以看下cloudslang
  • inquirer:st2的core.ask的一层封装,主要用来通知外部

自定义action

st2提供了强大的自定义能力,我们可以写自定义的action脚本。
一个action分为两部分组成:

  1. 一个yaml的metadata文件,描述了action的基本信息和入参
  2. 一个脚本,实现具体的action逻辑
    脚本可以用任意语言实现,只要遵循以下的规则:
  3. 脚本执行完成的时候返回0表示脚本执行成功,非0表示脚本执行失败
  4. 所有的日志信息打印在标准错误输出中
action metadata

action meta信息用来描述一个action,一般用yaml来编写。有以下的参数需要在meta文件中体现:

  • name:action名字
  • runner_type:执行action的runner类型
  • enabled:是否生效
  • entry_point:执行入口,类似Docker的ENTRYPOINT,一般脚本放在/opt/stackstorm/packs/${pack_name}/actions/下面
  • parameters:入参。目前支持string、boolean、number、object、integer、array,遵循JSON Schema 定义
  • tags

我们来看一个实例,