全部產品
Search
文件中心

CloudOps Orchestration Service:選擇ACS::Choice

更新時間:Jun 30, 2024

用途

根據條件判斷結果來選擇接下來將執行的任務。比如對一個執行個體重啟前,要查詢該執行個體的狀態,根據狀態結果選擇將執行的任務,若該執行個體為運行中,那麼執行對執行個體重啟的任務;若該執行個體為已停止,執行對執行個體啟動的任務,若以上狀態均不是,則執行查詢執行個體狀態的任務。

文法

Tasks:
- Name: chooseNextTaskToRun # 任務名稱
  Action: ACS::Choice
  Properties:
    DefaultTask:  task1  #執行完該chooseNextTaskToRun任務,若無Choices中條件滿足時,預設將執行名稱為task1的任務。task1不允許是對參數引用擷取的任務名,其必須是確定的字串格式的任務名稱。
    Choices:
      - When: condition1  # 待判斷的條件1。
        NextTask: task2  # 當condition1為true時,執行完該chooseNextTaskToRun任務,將執行名稱為task2的任務。task2不允許是對參數引用擷取的任務名,其必須是確定的字串格式的任務名稱。
      - When: condition2 # 待判斷的條件2。
        NextTask: task3 # 當condition2為true時,執行完該chooseNextTaskToRun任務,將執行名稱為task3的任務。task3不允許是對參數引用擷取的任務名,其必須是確定的字串格式的任務名稱。
                # 如有需要,Choices中還可有多個When判斷條件。
                #...
{
    "Tasks": [
        {
            "Name": "chooseNextTaskToRun",
            "Action": "ACS::Choice",
            "Properties": {
                "DefaultTask": "task1",
                "Choices": [
                    {
                        "When": "condition1",
                        "NextTask": "task2"
                    },
                    {
                        "When": "condition2",
                        "NextTask": "task3"
                    }
                ]
            }
        }
    ]
}

樣本

FormatVersion: OOS-2019-06-01
Description:
  en: Bulky restarts the ECS instances.
  zh-cn: 大量重啟ECS執行個體。
  name-en: ACS-ECS-BulkyRebootInstances
  name-zh-cn: 大量重啟ECS執行個體
Parameters:
  targets:
    Type: Json
    AssociationProperty: Targets
    AssociationPropertyMetadata:
      ResourceType: 'ALIYUN::ECS::Instance::InstanceId'
  OOSAssumeRole:
    Description:
      en: The RAM role to be assumed by OOS.
      zh-cn: OOS扮演的RAM角色。
    Type: String
    Default: OOSServiceRole
RamRole: '{{ OOSAssumeRole }}'
Tasks:
  - Name: getInstance
    Description:
      en: Views the ECS instances.
      zh-cn: 擷取ECS執行個體。
    Action: 'ACS::SelectTargets'
    Properties:
      ResourceType: 'ALIYUN::ECS::Instance::InstanceId'
      Filters:
        - '{{ targets }}'
    Outputs:
      instanceIds:
        Type: List
        ValueSelector: 'Instances.Instance[].InstanceId'
      status:
        Type: String
        ValueSelector: Instances.Instance[].Status
  - Name: whetherStatusRunning
    Action: 'ACS::Choice'
    Description:
      en: Starts the ECS instances
      zh-cn: 啟動執行個體
    Properties:
      DefaultTask: rebootInstance
      Choices:
        - When:
            'Fn::Equals':
              - Stopped
              - '{{ describeInstances.status }}'
          NextTask: startInstance
  - Name: startInstance
    Action: 'ACS::ECS::StartInstance'
    Description:
      en: Starts the ECS instances
      zh-cn: 啟動執行個體
    Properties:
      instanceId: '{{ ACS::TaskLoopItem }}'
    Loop:
      RateControl: '{{ rateControl }}'
      Items: '{{ getInstance.instanceIds }}'
  - Name: rebootInstance
    Action: 'ACS::ECS::RebootInstance'
    Description:
      en: Restarts the ECS instances
      zh-cn: 重啟執行個體
    Properties:
      instanceId: '{{ ACS::TaskLoopItem }}'
    Loop:
      RateControl: '{{ rateControl }}'
      Items: '{{ getInstance.instanceIds }}'
{
    "FormatVersion": "OOS-2019-06-01",
    "Description": {
        "en": "Bulky restarts the ECS instances.",
        "zh-cn": "大量重啟ECS執行個體。",
        "name-en": "ACS-ECS-BulkyRebootInstances",
        "name-zh-cn": "大量重啟ECS執行個體"
    },
    "Parameters": {
        "targets": {
            "Type": "Json",
            "AssociationProperty": "Targets",
            "AssociationPropertyMetadata": {
                "ResourceType": "ALIYUN::ECS::Instance"
            }
        },
        "rateControl": {
            "Description": {
                "en": "Concurrency ratio of task execution.",
                "zh-cn": "任務執行的並發比率。"
            },
            "Type": "Json",
            "AssociationProperty": "RateControl"
        },
        "OOSAssumeRole": {
            "Description": {
                "en": "The RAM role to be assumed by OOS.",
                "zh-cn": "OOS扮演的RAM角色。"
            },
            "Type": "String",
            "Default": "OOSServiceRole"
        }
    },
    "RamRole": "{{ OOSAssumeRole }}",
    "Tasks": [
        {
            "Name": "getInstance",
            "Description": {
                "en": "Views the ECS instances.",
                "zh-cn": "擷取ECS執行個體。"
            },
            "Action": "ACS::SelectTargets",
            "Properties": {
                "ResourceType": "ALIYUN::ECS::Instance",
                "Filters": [
                    "{{ targets }}"
                ]
            },
            "Outputs": {
                "instanceIds": {
                    "Type": "List",
                    "ValueSelector": "Instances.Instance[].InstanceId"
                }
            }
        },
        {
            "Name": "describeInstances",
            "Action": "ACS::ExecuteAPI",
            "Description": "Views A ECS instances Status.",
            "Properties": {
                "Service": "ECS",
                "API": "DescribeInstances",
                "Parameters": {
                    "InstanceIds": "{{ getInstance.instanceIds }}"
                }
            },
            "Outputs": {
                "status": {
                    "Type": "String",
                    "ValueSelector": "Instances.Instance[].Status"
                }
            }
        },
        {
            "Name": "whetherStatusRunning",
            "Action": "ACS::Choice",
            "Properties": {
                "DefaultTask": "describeInstancesFinally",
                "Choices": [
                    {
                        "When": {
                            "Fn::Equals": [
                                "Running",
                                "{{ describeInstances.status }}"
                            ]
                        },
                        "NextTask": "rebootInstance"
                    },
                    {
                        "When": {
                            "Fn::Equals": [
                                "Stopped",
                                "{{ describeInstances.status }}"
                            ]
                        },
                        "NextTask": "startInstance"
                    }
                ]
            }
        },
        {
            "Name": "startInstance",
            "Action": "ACS::ECS::StartInstance",
            "Description": {
                "en": "Starts the ECS instances.",
                "zh-cn": "啟動執行個體。"
            },
            "Properties": {
                "instanceId": "{{ ACS::TaskLoopItem }}"
            },
            "Loop": {
                "RateControl": "{{ rateControl }}",
                "Items": "{{ getInstance.instanceIds }}"
            }
        },
        {
            "Name": "rebootInstance",
            "Action": "ACS::ECS::RebootInstance",
            "Description": {
                "en": "Restarts the ECS instances.",
                "zh-cn": "重啟執行個體。"
            },
            "Properties": {
                "instanceId": "{{ ACS::TaskLoopItem }}"
            },
            "Loop": {
                "RateControl": "{{ rateControl }}",
                "Items": "{{ getInstance.instanceIds }}"
            }
        },
        {
            "Name": "describeInstancesFinally",
            "Action": "ACS::ExecuteAPI",
            "Description": "Views the ECS instances Status.",
            "Properties": {
                "Service": "ECS",
                "API": "DescribeInstances",
                "Parameters": {
                    "InstanceIds": "{{ getInstance.instanceIds }}"
                }
            },
            "Outputs": {
                "status": {
                    "Type": "String",
                    "ValueSelector": "Instances.Instance[].Status"
                }
            }
        }
    ],
    "Outputs": {
        "instanceIds": {
            "Type": "List",
            "Value": "{{ getInstance.instanceIds }}"
        }
    }
}