stackstorm 14.编写stackstorm的mistral

1 mistral动作流基础

因为workflows也是属于action,因此workflows在actions下面
因为mistral工作流比较特殊,它和之前执行的actions中对应的shell脚本或者python脚本不同,
它本身是一个yaml文件。
必须的参数:
type
input
output
tasks:
    task1:
    ......
     task2:
    ......同样的MistralWorkflow跟ActionChainWorkflow一样都需要提供元数据文件和脚本文件。

Pack examples的
元数据文件是在/opt/stackstorm/packs/examples/actions目录下,
脚本文件是在/opt/stackstorm/packs/examples/actions/workflows目录下.
实际在/opt/stackstorm/packs//actions/workflows下创建文件

2 创建mistral脚本文件


第一行: version版本
第二行: mistral工作流名称
第三行: input(用- 来表示多个参数), output, tasks(下面是多个task,每个task包含:
    action, publish(中包含stdout, stderr))
注意: 缩进都是4个空格
文件名称如下:
multi_task_mistral.yaml
内容如下:
version: '2.0'
default.multi_task_mistral:
    desctiption: "milti task mistral desctiption"
    type: direct
    input:
        - cmd
        - timeout
    output:
        stdout: <% $.stdout %>
    tasks:
        task1:
            action: core.local cmd=<% $.cmd %> timeout=<% $.timeout %>
            publish:
                result1: '<% task(task1).result.stdout %>'
                stdout: <% task(task1).result.stdout %>
                stderr: <% task(task1).result.stderr %>
            on-success:
                - task2
            on-error:
                - task4
        task2:
            action: core.local cmd='llll'
            publish:
                stdout: <% task(task2).result.stdout %>
                stderr: <% task(task2).result.stderr %>
            on-success:
                - task3
            on-error:
                - task4
        task3:
            action: core.local cmd='echo task3'
            publish:
                stdout: <% task(task3).result.stdout %>
                stderr: <% task(task3).result.stderr %>
            on-error:
                - task4
        task4:
            action: core.local cmd='echo task4'
            publish:
                stdout: <% task(task4).result.stdout %>
                stderr: <% task(task4).result.stderr %>


将该文件放入到:
/opt/stackstorm/packs/default/actions/workflows
目录下


3 创建mistral元数据文件


该元数据文件包含脚本位置,输入参数等
创建一个文件:
multi_task_mistral_meta.yaml
具体内容如下:
---
name: 'multi_task_mistral'
description: 'multi task mistral description'
enabled: true
runner_type: 'mistral-v2'
entry_point: 'workflows/multi_task_mistral.yaml'
parameters:
  cmd:
    required: true
    type: string
  timeout:
    type: integer
    default: 60


将该文件放入到:
/opt/stackstorm/packs/default/actions
目录下

4 创建mistral


执行如下命令
st2 action create /opt/stackstorm/packs/default/actions/multi_task_mistral_meta.yaml

输出结果如下:
+---------------+-----------------------------------+
| Property      | Value                             |
+---------------+-----------------------------------+
| id            | 5c6bdb089dc6d6211c117fb5          |
| name          | multi_task_mistral                |
| pack          | default                           |
| description   | multi task mistral description    |
| enabled       | True                              |
| entry_point   | workflows/multi_task_mistral.yaml |
| metadata_file |                                   |
| notify        |                                   |
| output_schema |                                   |
| parameters    | {                                 |
|               |     "cmd": {                      |
|               |         "required": true,         |
|               |         "type": "string"          |
|               |     },                            |
|               |     "timeout": {                  |
|               |         "default": 60,            |
|               |         "type": "integer"         |
|               |     }                             |
|               | }                                 |
| ref           | default.multi_task_mistral        |
| runner_type   | mistral-v2                        |
| tags          |                                   |
| uid           | action:default:multi_task_mistral |
+---------------+-----------------------------------+


查看创建的mistral:
[root@localhost actions]# st2 action list|grep default.multi_task_mistral
| default.multi_task_mistral                         | default  | multi task mistral description                     |


5 执行mistral


即执行如下命令:
st2 run default.multi_task_mistral cmd=date -a

输出结果如下:
To get the results, execute:
 st2 execution get 5c6bdbae9dc6d6211c117fb7

To view output in real-time, execute:
 st2 execution tail 5c6bdbae9dc6d6211c117fb7


不加 -a的结果,直接看到
[root@localhost workflows]# st2 run default.multi_task_mistral cmd=date
..
id: 5c6be37e9dc6d6211c117fc3
action.ref: default.multi_task_mistral
parameters: 
  cmd: date
status: succeeded
result_task: task4
result: 
  failed: false
  return_code: 0
  stderr: ''
  stdout: task4
  succeeded: true
start_timestamp: Tue, 19 Feb 2019 11:07:42 UTC
end_timestamp: Tue, 19 Feb 2019 11:07:45 UTC
+--------------------------+------------------------+-------+------------+-----------------+
| id                       | status                 | task  | action     | start_timestamp |
+--------------------------+------------------------+-------+------------+-----------------+
| 5c6be37f9dc6d6211c117fc6 | succeeded (0s elapsed) | task1 | core.local | Tue, 19 Feb     |
|                          |                        |       |            | 2019 11:07:43   |
|                          |                        |       |            | UTC             |
| 5c6be37f9dc6d6211c117fc8 | failed (1s elapsed)    | task2 | core.local | Tue, 19 Feb     |
|                          |                        |       |            | 2019 11:07:43   |
|                          |                        |       |            | UTC             |
| 5c6be3809dc6d6211c117fca | succeeded (1s elapsed) | task4 | core.local | Tue, 19 Feb     |
|                          |                        |       |            | 2019 11:07:44   |
|                          |                        |       |            | UTC             |
+--------------------------+------------------------+-------+------------+-----------------+

查看具体的执行结果:
[root@localhost action]# st2 execution get 5c6bdd229dc6d6211c117fba
id: 5c6bdd229dc6d6211c117fba
action.ref: default.multi_task_mistral
parameters: 
  cmd: date
status: succeeded (4s elapsed)
result_task: task4
result: 
  failed: false
  return_code: 0
  stderr: ''
  stdout: task4
  succeeded: true
start_timestamp: Tue, 19 Feb 2019 10:40:34 UTC
end_timestamp: Tue, 19 Feb 2019 10:40:38 UTC
+--------------------------+------------------------+-------+------------+--------------------------+
| id                       | status                 | task  | action     | start_timestamp          |
+--------------------------+------------------------+-------+------------+--------------------------+
| 5c6bdd239dc6d6211c117fbd | succeeded (0s elapsed) | task1 | core.local | Tue, 19 Feb 2019         |
|                          |                        |       |            | 10:40:35 UTC             |
| 5c6bdd249dc6d6211c117fbf | failed (0s elapsed)    | task2 | core.local | Tue, 19 Feb 2019         |
|                          |                        |       |            | 10:40:36 UTC             |
| 5c6bdd249dc6d6211c117fc1 | succeeded (1s elapsed) | task4 | core.local | Tue, 19 Feb 2019         |
|                          |                        |       |            | 10:40:36 UTC             |
+--------------------------+------------------------+-------+------------+--------------------------+

分析:
执行task2的时候,故意设置了执行的命令是 'llll',果然task2失败,然后直接跳转到task4,
与预期符合。


6 总结mistral


创建一个mistral工作流的主要过程就是:
1) 先编写一个mistral工作流的脚本文件,是yaml文件,包含: tasks, input, output, type等参数
2) 然后编写一个mistral工作流的元数据文件,是yaml文件,包含: entry_point, parameters, runner_type等。
3) 然后执行 st2 action create ,来创建mistral的action
4) 最后执行 st2 run 来运行mistral的action。

验证动作的正确性
st2 run -a
-a告诉命令无需等待工作流完成即可返回。

查看任务执行情况:
st2 execution list

查看各个子任务执行情况:
st2 execution get --show-tasks

关于mistral相关文件的存放位置:
mistral和action一样,也需要提供元数据文件和脚本文件。
只不过mistral的脚本文件比较特殊,是一个yaml文件。
将mistral的元数据文件放在/opt/stackstorm/packs//actions目录下,
将mistral的脚本文件是放在/opt/stackstorm/packs//actions/workflows目录下。


参考:
[1] https://docs.stackstorm.com/mistral.html
[2] https://blog.csdn.net/wlhdo71920145/article/details/80338954 
[3] https://blog.csdn.net/wlhdo71920145/article/details/80338279

你可能感兴趣的:(stackstorm)