来更新动作。","marks":[{"type":"color","value":"#4d4d4d"},{"type":"backgroundColor","value":"rgb(255, 255, 255)"},{"type":"fontFamily","value":"SimSun"}]}]}],"state":{}},{"type":"block","id":"PxZV-1660466226756","name":"paragraph","data":{},"nodes":[{"type":"text","id":"2yXW-1660466226754","leaves":[{"text":"或者,通过 ","marks":[{"type":"color","value":"#4d4d4d"},{"type":"backgroundColor","value":"rgb(255, 255, 255)"},{"type":"fontFamily","value":"SimSun"}]},{"text":"st2ctl reload --register-all","marks":[{"type":"color","value":"#F33232"},{"type":"backgroundColor","value":"rgb(255, 255, 255)"},{"type":"fontFamily","value":"SimSun"}]},{"text":" 将会所所有改变进行再一次加载。","marks":[{"type":"color","value":"#4d4d4d"},{"type":"backgroundColor","value":"rgb(255, 255, 255)"},{"type":"fontFamily","value":"SimSun"}]}]}],"state":{}},{"type":"block","id":"EdAm-1659541516821","name":"paragraph","data":{},"nodes":[{"type":"text","id":"XNEU-1659541516819","leaves":[{"text":"","marks":[{"type":"fontFamily","value":"SimSun"}]}]}],"state":{}},{"type":"block","id":"f6B6-1659541516914","name":"paragraph","data":{},"nodes":[{"type":"text","id":"dJ9A-1659541516912","leaves":[{"text":"","marks":[{"type":"fontFamily","value":"SimSun"}]}]}],"state":{}},{"type":"block","id":"nDVN-1659541517021","name":"paragraph","data":{},"nodes":[{"type":"text","id":"ZzVi-1659541517020","leaves":[{"text":"","marks":[{"type":"fontFamily","value":"SimSun"}]}]}],"state":{}},{"type":"block","id":"6yYs-1659541517120","name":"paragraph","data":{},"nodes":[{"type":"text","id":"aNMy-1659541517118","leaves":[{"text":"","marks":[{"type":"fontFamily","value":"SimSun"}]}]}],"state":{}},{"type":"block","id":"4X6P-1659541517220","name":"paragraph","data":{},"nodes":[{"type":"text","id":"EOWl-1659541517218","leaves":[{"text":"","marks":[{"type":"fontFamily","value":"SimSun"}]}]}],"state":{}},{"type":"block","id":"cXiS-1659541517326","name":"paragraph","data":{},"nodes":[{"type":"text","id":"LTem-1659541517324","leaves":[{"text":"","marks":[{"type":"fontFamily","value":"SimSun"}]}]}],"state":{}},{"type":"block","id":"QmGI-1659541517421","name":"paragraph","data":{},"nodes":[{"type":"text","id":"m0Hn-1659541517420","leaves":[{"text":"","marks":[{"type":"fontFamily","value":"SimSun"}]}]}],"state":{}},{"type":"block","id":"eZxM-1659541517517","name":"paragraph","data":{},"nodes":[{"type":"text","id":"r9CN-1659541517516","leaves":[{"text":"","marks":[{"type":"fontFamily","value":"SimSun"}]}]}],"state":{}},{"type":"block","id":"pYit-1659541517618","name":"paragraph","data":{},"nodes":[{"type":"text","id":"gFCT-1659541517617","leaves":[{"text":"","marks":[{"type":"fontFamily","value":"SimSun"}]}]}],"state":{}},{"type":"block","id":"olku-1659541517712","name":"paragraph","data":{},"nodes":[{"type":"text","id":"noMP-1659541517710","leaves":[{"text":"","marks":[{"type":"fontFamily","value":"SimSun"}]}]}],"state":{}},{"type":"block","id":"IzFI-1659541517812","name":"paragraph","data":{},"nodes":[{"type":"text","id":"T73H-1659541517811","leaves":[{"text":"","marks":[{"type":"fontFamily","value":"SimSun"}]}]}],"state":{}},{"type":"block","id":"VouA-1659541517908","name":"paragraph","data":{},"nodes":[{"type":"text","id":"22jD-1659541517906","leaves":[{"text":"","marks":[{"type":"fontFamily","value":"SimSun"}]}]}],"state":{}},{"type":"block","id":"sgTo-1659541518011","name":"paragraph","data":{},"nodes":[{"type":"text","id":"LTVT-1659541518009","leaves":[{"text":"","marks":[{"type":"fontFamily","value":"SimSun"}]}]}],"state":{}},{"type":"block","id":"5PYf-1659541518114","name":"paragraph","data":{},"nodes":[{"type":"text","id":"AbA6-1659541518113","leaves":[{"text":"","marks":[{"type":"fontFamily","value":"SimSun"}]}]}],"state":{}},{"type":"block","id":"qYNF-1659541518210","name":"paragraph","data":{},"nodes":[{"type":"text","id":"MXL9-1659541518208","leaves":[{"text":"","marks":[{"type":"fontFamily","value":"SimSun"}]}]}],"state":{}},{"type":"block","id":"GDji-1659541518306","name":"paragraph","data":{},"nodes":[{"type":"text","id":"ndHD-1659541518305","leaves":[{"text":"","marks":[{"type":"fontFamily","value":"SimSun"}]}]}],"state":{}},{"type":"block","id":"ar4Q-1659541518414","name":"paragraph","data":{},"nodes":[{"type":"text","id":"uIHf-1659541518412","leaves":[{"text":"","marks":[{"type":"fontFamily","value":"SimSun"}]}]}],"state":{}},{"type":"block","id":"WQP1-1659541518512","name":"paragraph","data":{},"nodes":[{"type":"text","id":"CWMc-1659541518510","leaves":[{"text":"","marks":[{"type":"fontFamily","value":"SimSun"}]}]}],"state":{}},{"type":"block","id":"qN7S-1659541518618","name":"paragraph","data":{},"nodes":[{"type":"text","id":"Jgkt-1659541518616","leaves":[{"text":"","marks":[{"type":"fontFamily","value":"SimSun"}]}]}],"state":{}},{"type":"block","id":"Olvu-1659541518702","name":"paragraph","data":{},"nodes":[{"type":"text","id":"Gup6-1659541518700","leaves":[{"text":"","marks":[{"type":"fontFamily","value":"SimSun"}]}]}],"state":{}}]'>
stackstrom action
Action是StackStrom事件触发后的处理方式,也就是自动化任务的执行体。
相关的action命令:st2 action arg
positional arguments:
{list,get,create,update,delete,enable,disable,execute}
List of commands for managing actions.
list Get the list of actions.
get Get individual action.
create Create a new action.
update Updating an existing action.
delete Delete an existing action.
enable Enable an existing action.
disable Disable an existing action.
execute Invoke an action manually.
2 内置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
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的详情后,就可以执行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 62f8542978884ba53a7c9697
To view output in real-time, execute:
st2 execution tail 62f8542978884ba53a7c9697
# st2 execution get 62f8542978884ba53a7c9697
id: 62f8542978884ba53a7c9697
action.ref: core.http
context.user: st2admin
parameters:
status: succeeded (2s elapsed)
start_timestamp: Sun, 14 Aug 2022 01:47:21 UTC
end_timestamp: Sun, 14 Aug 2022 01:47:23 UTC
log:
- status: requested timestamp: '2022-08-14T01:47:21.711000Z'
- status: scheduled timestamp: '2022-08-14T01:47:22.115000Z'
- status: running timestamp: '2022-08-14T01:47:22.406000Z'
- status: succeeded timestamp: '2022-08-14T01:47:23.500000Z' result: body: args: {} headers: Accept: '/' Accept-Encoding: gzip, deflate Host: httpbin.org User-Agent: st2/v3.7.0 X-Amzn-Trace-Id: Root=1-62f8542c-0fb9534d6dee32813b13ae23 X-Stanley-Action: http origin: 120.231.220.31 url: http://httpbin.org/get headers: Access-Control-Allow-Credentials: 'true' Access-Control-Allow-Origin: '*' Connection: keep-alive Content-Length: '328' Content-Type: application/json Date: Sun, 14 Aug 2022 01:47:24 GMT Server: gunicorn/19.9.0 parsed: true status_code: 200
3 自定义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 string, boolean, number (whole numbers and decimal numbers - e.g. 1.0, 1, 3.3333, etc.), object, integer (whole numbers only -1, 1000, etc.) and array. If metadata is provided, input args are validated on action execution. Otherwise, validation is skipped.
默认的Pack是default,action需要用到脚本放在/opt/stackstorm/packs/default/actions/script.sh。
vi /opt/stackstorm/packs/default/actions/script.sh
#!/usr/bin/env bash
MESSAGE=$1
echo ${MESSAGE}
创建action元数据文件:
vi /opt/stackstorm/packs/default/my-message.yaml
---
name: "my-action"
runner_type: "local-shell-script"
description: "Echo a message"
enabled: true
entry_point: "message.sh"
parameters:
message:
type: "string"
description: "Message to write"
required: true
position: 0
position属性(0用于传进来的第1个变量,1用于传进来的第2个变量),
创建action:
st2 action create my-message.yaml
+---------------+--------------------------------------------+
| Property | Value |
+---------------+--------------------------------------------+
| id | 62f85cae78884ba53a7c969f |
| name | my-action |
| pack | default |
| description | Echo a message |
| enabled | True |
| entry_point | message.sh |
| metadata_file | |
| notify | |
| output_schema | |
| parameters | { |
| | "message": { |
| | "type": "string", |
| | "description": "Message to write", |
| | "required": true, |
| | "position": 0 |
| | } |
| | } |
| ref | default.my-action |
| runner_type | local-shell-script |
| tags | |
| uid | action:default:my-action |
+---------------+--------------------------------------------+
执行action:
st2 run default.my-action message="this is test action"
.id: 62f85d8b78884ba53a7c96a4
action.ref: default.my-action
context.user: st2admin
parameters:
message: this is test action
status: succeeded
start_timestamp: Sun, 14 Aug 2022 02:27:23 UTC
end_timestamp: Sun, 14 Aug 2022 02:27:24 UTC
result:
failed: false
return_code: 0
stderr: ''
stdout: ''
succeeded: true
如果重命名或者添加参数,需要通过
st2 action update 来更新动作。
或者,通过 st2ctl reload --register-all 将会所所有改变进行再一次加载。