StackStorm介绍
StackStorm是一个强大的自动化平台,结合DevOps和ChatOps,提供可扩展、灵活和健壮的工具链用于应用、服务和工作流的自动化能力。
Inquiry
在自动化任务执行的过程中,往往会需要等待确认的步骤,比如需要人工审核,需要外部系统的确认。为此StackStorm提供一种Action的交互机制Inquiry,通过执行core.ask发起Inquiry,进入pending挂起状态,直到Inquiry收到回复才继续执行。
下面是一个Mistral Workflow的规格文件examples.mistral-ask-basic.yaml:
version: '2.0' examples.mistral-ask-basic: description: A basic Mistral workflow illustrating the use of Inquiries type: direct output: result: <% task(task1).result.response %> tasks: task1: action: core.ask input: route: developers schema: type: object properties: secondfactor: type: string description: Please enter second factor for authenticating to "foo" service required: True on-success: - task2 task2: action: core.local input: cmd: echo "We can now authenticate to 'foo' service with <% task(task1).result.response.secondfactor %>" |
task1的action就是core.ask,core.ask需要提供的入参就是Inquiry的规格:
Parameter |
Description |
schema |
A JSON schema that will be used to validate the response data. A basic schema will be provided by default, or you can provide one here. Only valid responses will cause the action to succeed, and the workflow to continue. |
ttl |
Time (in minutes) until an unacknowledged Inquiry is garbage-collected. Set to 0 to disable garbage collection for this Inquiry. NOTE - Inquiry garbage collection is not enabled by default, so this field does nothing unless it is turned on. |
roles |
A list of RBAC roles that are permitted to respond to the action. Defaults to empty list, which permits all roles. This requires enterprise features |
users |
A list of users that are permitted to respond to the action. Defaults to empty list, which permits all users. |
route |
An arbitrary string that can be used to filter different Inquiries inside rules. This can be helpful for deciding who to notify of an incoming Inquiry. |
执行Workflow,task1就会挂起:
$ st2 run examples.mistral-ask-basic . id: 5afbd3802b2556015687ce09 action.ref: examples.mistral-ask-basic parameters: None status: paused start_timestamp: Wed, 16 May 2018 06:45:20 UTC end_timestamp: +--------------------------+---------+-------+----------+-------------------------------+ | id | status | task | action | start_timestamp | +--------------------------+---------+-------+----------+-------------------------------+ | 5afbd3802b2556015687ce0c | pending | task1 | core.ask | Wed, 16 May 2018 06:45:20 UTC | +--------------------------+---------+-------+----------+-------------------------------+ |
这时候就会产生一个Inquiry:
$ st2 inquiry list +--------------------------+-------+-------+------------+------+ | id | roles | users | route | ttl | +--------------------------+-------+-------+------------+------+ | 5afbd3802b2556015687ce0c | | | developers | 1440 | +--------------------------+-------+-------+------------+------+ |
然后进行回复:
$ st2 inquiry respond -r '{"secondfactor": "bar"}' 5afbd3802b2556015687ce0c |
那么Workflow就会继续执行:
$ st2 execution get 5afbd3802b2556015687ce09 id: 5afbd3802b2556015687ce09 action.ref: examples.mistral-ask-basic parameters: None status: succeeded (450s elapsed) result_task: task2 result: failed: false return_code: 0 stderr: '' stdout: We can now authenticate to 'foo' service with bar succeeded: true start_timestamp: Wed, 16 May 2018 06:45:20 UTC end_timestamp: Wed, 16 May 2018 06:52:50 UTC +--------------------------+------------------------+-------+------------+-------------------------------+ | id | status | task | action | start_timestamp | +--------------------------+------------------------+-------+------------+-------------------------------+ | 5afbd3802b2556015687ce0c | succeeded (1s elapsed) | task1 | core.ask | Wed, 16 May 2018 06:45:20 UTC | | 5afbd5402b2556015687ce0e | succeeded (0s elapsed) | task2 | core.local | Wed, 16 May 2018 06:52:48 UTC | +--------------------------+------------------------+-------+------------+-------------------------------+ |
参考
- https://github.com/StackStorm/st2docs/blob/master/docs/source/inquiries.rst
作者简介
吴龙辉,现任网宿科技云计算架构师,致力于云计算PaaS的研究和实践,《Kubernetes实战》作者,活跃于CloudFoundry,Docker,Kubernetes等开源社区,贡献代码和撰写技术文档。邮箱: [email protected]