工作流

工作流是 Mistral 工作流语言的主要构建块,也是该项目存在的原因。工作流代表一个可以用多种方式描述并能为最终用户提供有用功能的流程。每个工作流都由任务(至少一个)组成,这些任务描述了工作流执行期间应采取的具体步骤。

YAML 示例

---
version: '2.0'
create_vm:
  description: Simple workflow sample
  type: direct
  input: # Input parameter declarations
    - vm_name
    - image_ref
    - flavor_ref
  output: # Output definition
    vm_id: <% $.vm_id %>
  tasks:
    create_server:
      action: nova.servers_create name=<% $.vm_name %> image=<% $.image_ref %> flavor=<% $.flavor_ref %>
      publish:
        vm_id: <% task().result.id %>
      on-success:
        - wait_for_instance
    wait_for_instance:
      action: nova.servers_find id=<% $.vm_id %> status='ACTIVE'
      retry:
        delay: 5
        count: 15

工作流类型

Mistral 工作流语言 v2 引入了不同的工作流类型,并且每种工作流类型的结构根据其语义而异。目前,Mistral 提供两种工作流类型

有关详细信息,请参阅相应的章节。

直接工作流

直接工作流由以图形方式组合的任务组成,每个后续任务在根据产生的结果之后启动。因此,直接工作流具有转换的概念。如果不再有可用于跳转到下一个任务的转换,则认为直接工作流已完成。

../../_images/direct_workflow.png

直接工作流的 YAML 示例

---
version: '2.0'
create_vm_and_send_email:
  type: direct
  input:
    - vm_name
    - image_id
    - flavor_id
  output:
    result: <% $.vm_id %>
  tasks:
    create_vm:
      action: nova.servers_create name=<% $.vm_name %> image=<% $.image_id %> flavor=<% $.flavor_id %>
      publish:
        vm_id: <% task().result.id %>
      on-error:
        - send_error_email
      on-success:
        - send_success_email
    send_error_email:
      action: send_email to='admin@mysite.org' body='Failed to create a VM'
      on-complete:
        - fail
    send_success_email:
      action: send_email to='admin@mysite.org' body='Vm is successfully created and its id is <% $.vm_id %>'

反向工作流

在反向工作流中,工作流任务图中的所有关系都是依赖关系。为了运行这种类型的工作流,我们需要指定需要完成的任务,可以将其约定称为“目标任务”。当 Mistral Engine 启动工作流时,它会递归地识别需要首先完成的所有依赖项。

../../_images/reverse_workflow.png

下图解释了反向工作流的工作方式。在示例中,任务 T1 被选为目标任务。因此,当工作流启动时,Mistral 将仅运行任务 T7T8T5T6T2T1,顺序为指定顺序(从没有依赖项的任务开始)。任务 T3T4 不会成为此工作流的一部分,因为从 T1T3T4 的有向图中没有路径。

反向工作流的 YAML 示例

---
version: '2.0'
create_vm_and_send_email:
  type: reverse
  input:
    - vm_name
    - image_id
    - flavor_id
  output:
    result: <% $.vm_id %>
  tasks:
    create_vm:
      action: nova.servers_create name=<% $.vm_name %> image=<% $.image_id %> flavor=<% $.flavor_id %>
      publish:
        vm_id: <% task().result.id %>
    search_for_ip:
      action: nova.floating_ips_findall instance_id=null
      publish:
        vm_ip: <% task().result[0].ip %>
    associate_ip:
      action: nova.servers_add_floating_ip server=<% $.vm_id %> address=<% $.vm_ip %>
      requires: [search_for_ip]
    send_email:
      action: send_email to='admin@mysite.org' body='Vm is created and id <% $.vm_id %> and ip address <% $.vm_ip %>'
      requires: [create_vm, associate_ip]

有关 Mistral 工作流语言本身的更多详细信息,请参阅 Mistral 工作流语言规范