All Products
Search
Document Center

CloudOps Orchestration Service:ACS-ESS-RollingUpdateByRunCommandInScalingGroup

Last Updated:Jan 13, 2025

Template name

ACS-ESS-RollingUpdateByRunCommandInScalingGroup

Execute Now

Template description

Runs commands to perform rolling updates on the Elastic Compute Service (ECS) instances in a scaling group.

Template type

Automated

Owner

Alibaba Cloud

Input parameters

Parameter

Description

Type

Required

Default value

Limit

invokeType

The operation type.

String

Yes

scalingGroupId

The scaling group ID.

String

Yes

enterProcess

The scaling processes to be suspended before execution.

List

No

['ScaleIn', 'ScaleOut', 'HealthCheck', 'AlarmNotification', 'ScheduledAction']

exitProcess

The scaling processes to be resumed after the execution.

List

No

['ScaleIn', 'ScaleOut', 'HealthCheck', 'AlarmNotification', 'ScheduledAction']

lifecycleState

The lifecycle state.

List

No

InService

commandType

The command type.

String

No

RunShellScript

invokeScript

The script used to perform updates.

String

No

""

rollbackScript

The script used to perform rollback.

String

No

""

sourceExecutionId

The ID of the CloudOps Orchestration Service (OOS) execution to be rolled back.

String

No

""

batchPauseOption

The pause policy.

String

No

Automatic

batchNumber

The number of batches.

Number

No

5

OOSAssumeRole

The Resource Access Management (RAM) role that is assumed by OOS.

String

No

""

Output parameters

Parameter

Description

Type

commandOutputs

List

Policy that is required to execute the template

{
    "Version": "1",
    "Statement": [
        {
            "Action": [
                "ess:DescribeScalingInstances",
                "ess:EnterStandby",
                "ess:ExitStandBy",
                "ess:ResumeProcesses",
                "ess:SetInstancesProtection",
                "ess:SuspendProcesses"
            ],
            "Resource": "*",
            "Effect": "Allow"
        },
        {
            "Action": [
                "ecs:DescribeInstances",
                "ecs:DescribeInvocationResults",
                "ecs:DescribeInvocations",
                "ecs:RunCommand",
                "ecs:TagResources"
            ],
            "Resource": "*",
            "Effect": "Allow"
        },
        {
            "Action": [
                "oos:ListExecutions"
            ],
            "Resource": "*",
            "Effect": "Allow"
        }
    ]
}

References

ACS-ESS-RollingUpdateByRunCommandInScalingGroup

Template content

FormatVersion: OOS-2019-06-01
Description:
  en: Rolling update ECS instances that in scaling group by run command
  zh-cn: 通过执行命令的方式滚动更新伸缩组内的ECS实例
  name-en: ACS-ESS-RollingUpdateByRunCommandInScalingGroup
  name-zh-cn: 通过执行命令的方式滚动更新伸缩组内的ECS实例
  categories:
    - elastic_manage
Parameters:
  invokeType:
    Label:
      en: InvokeType
      zh-cn: 操作类型
    Type: String
    AllowedValues:
      - invoke
      - rollback
  scalingGroupId:
    Label:
      en: ScalingGroupId
      zh-cn: 伸缩组ID
    Type: String
  enterProcess:
    Label:
      en: EnterProcess
      zh-cn: 执行开始要暂停的伸缩组流程
    Type: List
    Default:
      - ScaleIn
      - ScaleOut
      - HealthCheck
      - AlarmNotification
      - ScheduledAction
  exitProcess:
    Label:
      en: ExitProcess
      zh-cn: 执行结束要恢复的伸缩组流程
    Type: List
    Default:
      - ScaleIn
      - ScaleOut
      - HealthCheck
      - AlarmNotification
      - ScheduledAction
  lifecycleState:
    Label:
      en: LifecycleState
      zh-cn: 生命周期状态
    Description:
      en: The lifecycle state of the ECS instance to be upgraded
      zh-cn: 要升级的ECS实例所处的生命周期状态
    Type: List
    AllowedValues:
      - InService
      - Protected
    Default: InService
  commandType:
    Label:
      en: CommandType
      zh-cn: 命令类型
    Type: String
    AllowedValues:
      - RunBatScript
      - RunPowerShellScript
      - RunShellScript
    Default: RunShellScript
  invokeScript:
    Label:
      en: InvokeScript
      zh-cn: 用于执行的脚本
    Type: String
    Default: ''
  rollbackScript:
    Label:
      en: RollbackScript
      zh-cn: 用于回滚的脚本
    Type: String
    Default: ''
  sourceExecutionId:
    Label:
      en: SourceExecutionId
      zh-cn: 用于回滚的OOS执行ID
    Type: String
    Default: ''
  batchPauseOption:
    Label:
      en: BatchPauseOption
      zh-cn: 分批模式
    Type: String
    AllowedValues:
      - Automatic
      - FirstBatchPause
      - EveryBatchPause
    Default: Automatic
  batchNumber:
    Label:
      en: BatchNumber
      zh-cn: 分批执行的批次
    Type: Number
    MinValue: 1
    MaxValue: 100
    Default: 5
  OOSAssumeRole:
    Label:
      en: OOSAssumeRole
      zh-cn: OOS扮演的RAM角色
    Type: String
    Default: ''
RamRole: '{{ OOSAssumeRole }}'
Tasks:
  - Name: choiceNextTask
    Action: 'ACS::Choice'
    Description:
      en: Choose next task by invoke type
      zh-cn: 根据执行类型选择任务
    Properties:
      DefaultTask: getInstance
      Choices:
        - When:
            'Fn::Equals':
              - rollback
              - '{{ invokeType }}'
          NextTask: getExecutionInstanceIds
  - Name: getInstance
    Description:
      en: Views the ECS instances
      zh-cn: 获取ECS实例
    Action: 'ACS::ExecuteApi'
    Properties:
      Service: ESS
      API: DescribeScalingInstances
      Parameters:
        ScalingGroupId: '{{ scalingGroupId }}'
        LifecycleStates: '{{ lifecycleState }}'
        CreationTypes:
          - AutoCreated
          - Attached
    Outputs:
      instanceIds:
        Type: List
        ValueSelector: 'ScalingInstances.ScalingInstance | map({"InstanceId": (.InstanceId), "LifecycleState": (.LifecycleState)})[]'
  - Name: whetherTheScalingGroupInstanceIsEmpty
    Action: 'ACS::Choice'
    Description:
      en: Check whether the instances in the services in the scaling group are empty
      zh-cn: 检查伸缩组内服务中的实例是否为空
    Properties:
      DefaultTask: whetherTosuspendProcess
      Choices:
        - When:
            'Fn::Equals':
              - [ ]
              - '{{ getInstance.instanceIds }}'
          NextTask: 'ACS::END'
  - Name: whetherTosuspendProcess
    Action: 'ACS::Choice'
    Description:
      en: Choose next task by process
      zh-cn: 根据流程值判断是否暂停流程
    Properties:
      DefaultTask: suspendProcess
      Choices:
        - When:
            'Fn::Equals':
              - []
              - '{{ enterProcess }}'
          NextTask: invokeByRunCommand
  - Name: suspendProcess
    Action: 'ACS::ExecuteAPI'
    Description:
      en: Suspend process
      zh-cn: 暂停伸缩组流程
    Properties:
      Service: ESS
      API: SuspendProcesses
      Parameters:
        ScalingGroupId: '{{ scalingGroupId }}'
        Process: '{{ enterProcess }}'
  - Name: invokeByRunCommand
    Action: 'ACS::ESS::RunCommand'
    Description:
      en: Execute cloud assistant command
      zh-cn: 执行云助手命令
    Properties:
      commandContent: '{{ invokeScript }}'
      instanceId:
        Fn::Select:
          - 'InstanceId'
          - '{{ ACS::TaskLoopItem }}'
      lifecycleState:
        Fn::Select:
          - 'LifecycleState'
          - '{{ ACS::TaskLoopItem }}'
      commandType: '{{ commandType }}'
      scalingGroupId: '{{ scalingGroupId }}'
    Loop:
      Items: '{{ getInstance.instanceIds }}'
      BatchPauseOption: '{{ batchPauseOption }}'
      Concurrency:
        'Fn::CalculateBatch':
          - '{{ batchNumber }}'
          - '{{ getInstance.instanceIds }}'
      Outputs:
        commandOutputs:
          AggregateType: 'Fn::ListJoin'
          AggregateField: commandOutput
    Outputs:
      commandOutput:
        Type: String
        ValueSelector: invocationOutput
  - Name: tagInstance
    Action: ACS::ExecuteApi
    Description:
      en: Creates and attaches tag to ECS instance
      zh-cn: 创建并挂载标签到ECS实例
    Properties:
      Service: ECS
      API: TagResources
      Parameters:
        ResourceIds:
          - Fn::Select:
              - 'InstanceId'
              - '{{ ACS::TaskLoopItem }}'
        ResourceType: Instance
        Tags:
        - Key: 'ess:rollingupdate'
          Value: 'oos:{{ ACS::ExecutionId }}'
    Loop:
      Items: '{{ getInstance.instanceIds }}'
      Mode: Concurrency
      MaxErrors: 0
      Concurrency: 10
  - Name: whetherToResumeProcesses
    Action: 'ACS::Choice'
    Description:
      en: Choose next task by process
      zh-cn: 根据流程值判断是否恢复流程
    Properties:
      DefaultTask: resumeProcesses
      Choices:
        - When:
            'Fn::Equals':
              - []
              - '{{ exitProcess }}'
          NextTask: 'ACS::END'
  - Name: resumeProcesses
    Action: 'ACS::ExecuteAPI'
    OnSuccess: 'ACS::END'
    Description:
      en: Resume process
      zh-cn: 恢复伸缩组流程
    Properties:
      Service: ESS
      API: ResumeProcesses
      Parameters:
        ScalingGroupId: '{{ scalingGroupId }}'
        Process: '{{ exitProcess }}'
  - Name: getExecutionInstanceIds
    Action: 'ACS::ExecuteAPI'
    Description:
      en: Get the instance that has been executed
      zh-cn: 获取已经执行过执行脚本的实例
    Properties:
      Service: OOS
      API: ListExecutions
      Parameters:
        ParentExecutionId: '{{ sourceExecutionId }}'
    Outputs:
      instanceIds:
        Type: List
        ValueSelector: 'Executions[].Parameters.instanceId'
      lifecycleStates:
        Type: List
        ValueSelector: '.Executions | map({"InstanceId": (.Parameters.instanceId), "LifecycleState": (.Parameters.lifecycleState)})[]'
  - Name: getExistInstanceIds
    Description:
      en: Views the ECS instances
      zh-cn: 获取ECS实例
    Action: ACS::SelectTargets
    Properties:
      ResourceType: ALIYUN::ECS::Instance
      Filters:
        - Type: ResourceIds
          ResourceIds: '{{ getExecutionInstanceIds.instanceIds }}'
    Outputs:
      instanceIds:
        Type: List
        ValueSelector: Instances.Instance[].InstanceId
  - Name: getRollbackProcess
    Action: 'ACS::ExecuteAPI'
    Description:
      en: Views resource parameters
      zh-cn: 获取源执行的参数
    Properties:
      Service: OOS
      API: ListExecutions
      Parameters:
        ExecutionId: '{{ sourceExecutionId }}'
    Outputs:
      enterProcess:
        Type: List
        ValueSelector: 'Executions[].Parameters.enterProcess[]'
      exitProcess:
        Type: List
        ValueSelector: 'Executions[].Parameters.exitProcess[]'
  - Name: suspendProcessWithRollBack
    Action: 'ACS::ExecuteAPI'
    Description:
      en: Suspend process
      zh-cn: 暂停伸缩组流程
    Properties:
      Service: ESS
      API: SuspendProcesses
      Parameters:
        ScalingGroupId: '{{ scalingGroupId }}'
        Process: '{{ getRollbackProcess.enterProcess }}'
  - Name: rollbackByRunCommand
    Action: 'ACS::ESS::RunCommand'
    Description:
      en: Execute cloud assistant command
      zh-cn: 执行云助手命令
    Properties:
      commandContent: '{{ rollbackScript }}'
      instanceId: '{{ ACS::TaskLoopItem }}'
      lifecycleState:
        Fn::Jq:
          - First
          - '.[] | select(.InstanceId == "{{ ACS::TaskLoopItem }}") | .LifecycleState'
          - '{{ getExecutionInstanceIds.lifecycleStates }}'
      commandType: RunShellScript
      scalingGroupId: '{{ scalingGroupId }}'
      rollBack: 'true'
    Loop:
      Items: '{{ getExistInstanceIds.instanceIds }}'
      BatchPauseOption: '{{ batchPauseOption }}'
      Concurrency:
        'Fn::CalculateBatch':
          - '{{ batchNumber }}'
          - '{{ getExistInstanceIds.instanceIds }}'
      Outputs:
        commandOutputs:
          AggregateType: 'Fn::ListJoin'
          AggregateField: commandOutput
    Outputs:
      commandOutput:
        Type: String
        ValueSelector: invocationOutput
  - Name: whetherToResumeProcessesWithRollback
    Action: 'ACS::Choice'
    Description:
      en: Choose next task by process
      zh-cn: 根据流程值判断是否恢复流程
    Properties:
      DefaultTask: resumeProcessesWithRollback
      Choices:
        - When:
            'Fn::Equals':
              - []
              - '{{ getRollbackProcess.exitProcess }}'
          NextTask: 'ACS::END'
  - Name: resumeProcessesWithRollback
    Action: 'ACS::ExecuteAPI'
    Description:
      en: Resume process
      zh-cn: 恢复伸缩组流程
    Properties:
      Service: ESS
      API: ResumeProcesses
      Parameters:
        ScalingGroupId: '{{ scalingGroupId }}'
        Process: '{{ getRollbackProcess.exitProcess }}'
Outputs:
  commandOutputs:
    Type: List
    Value:
      'Fn::If':
        - 'Fn::Equals':
            - invoke
            - '{{ invokeType }}'
        - '{{ invokeByRunCommand.commandOutputs }}'
        - '{{ rollbackByRunCommand.commandOutputs }}'