ACS-ESS-RollingUpdateByRunCommandInScalingGroup

更新时间:
复制 MD 格式

Template name

Performs rolling updates on ECS instances in a scaling group by running commands.

Execute now

Template description

Performs rolling updates on ECS instances in a scaling group by running commands.

Template type

automation

Owner

Alibaba Cloud

Input parameters

Parameter

Description

Type

Required

Default

Constraints

invokeType

The type of operation. Set to invoke to run an update, or rollback to revert a previous update.

String

Yes

scalingGroupId

The ID of the scaling group containing the target ECS instances.

String

Yes

enterProcess

The scaling group processes to suspend before the update starts. Suspending these processes prevents Auto Scaling from interfering with the update.

List

No

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

exitProcess

The scaling group processes to resume after the update completes. Typically the same as enterProcess.

List

No

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

lifecycleState

The lifecycle state of the target instances. Specify InService for active instances or Protected for instances protected from scale-in.

List

No

InService

commandType

The type of script to run. Valid values: RunShellScript (Linux), RunBatScript (Windows), and RunPowerShellScript (Windows).

String

No

RunShellScript

invokeScript

The update script to run on each instance, such as a shell script that pulls the latest application code and restarts a service.

String

No

""

rollbackScript

The rollback script to run on each instance. This script should undo the changes made by invokeScript.

String

No

""

sourceExecutionId

The ID of the original OOS execution to roll back. Required only when invokeType is set to rollback.

String

No

""

batchPauseOption

The pause policy between batches. Automatic executes all batches without pausing. FirstBatchPause pauses the operation after the first batch. EveryBatchPause pauses the operation after every batch.

String

No

Automatic

batchNumber

The number of batches to divide the instances into. For example, if you have 10 instances and set this to 5, each batch processes 2 instances.

Number

No

5

OOSAssumeRole

The RAM role that OOS assumes to perform the update. If not specified, OOS uses the permissions of the current user.

String

No

""

Output parameters

Parameter

Description

Type

commandOutputs

The command outputs from each instance, corresponding to the operation specified by invokeType.

List

Required permission policy

{
    "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"
        }
    ]
}

Details

ACS-ESS-RollingUpdateByRunCommandInScalingGroup details

Template content

FormatVersion: OOS-2019-06-01
Description:
  en: Performs a rolling update on ECS instances in a scaling group by executing a command.
  zh-cn: 通过执行命令的方式滚动更新伸缩组内的ECS实例
  name-en: Perform Rolling Update in Scaling Group by Running Command
  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 target ECS instance.
      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: Selects the next task based on the value of invokeType.
      zh-cn: 根据执行类型选择任务
    Properties:
      DefaultTask: getInstance
      Choices:
        - When:
            'Fn::Equals':
              - rollback
              - '{{ invokeType }}'
          NextTask: getExecutionInstanceIds
  - Name: getInstance
    Description:
      en: Retrieves the ECS instances in the scaling group.
      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: Checks if the scaling group has any instances that match the specified state.
      zh-cn: 检查伸缩组内服务中的实例是否为空
    Properties:
      DefaultTask: whetherTosuspendProcess
      Choices:
        - When:
            'Fn::Equals':
              - [ ]
              - '{{ getInstance.instanceIds }}'
          NextTask: 'ACS::END'
  - Name: whetherTosuspendProcess
    Action: 'ACS::Choice'
    Description:
      en: Determines whether to suspend scaling processes based on the enterProcess parameter.
      zh-cn: 根据流程值判断是否暂停流程
    Properties:
      DefaultTask: suspendProcess
      Choices:
        - When:
            'Fn::Equals':
              - []
              - '{{ enterProcess }}'
          NextTask: invokeByRunCommand
  - Name: suspendProcess
    Action: 'ACS::ExecuteAPI'
    Description:
      en: Suspends the specified scaling group processes.
      zh-cn: 暂停伸缩组流程
    Properties:
      Service: ESS
      API: SuspendProcesses
      Parameters:
        ScalingGroupId: '{{ scalingGroupId }}'
        Process: '{{ enterProcess }}'
  - Name: invokeByRunCommand
    Action: 'ACS::ESS::RunCommand'
    Description:
      en: Runs the specified command on the target instances by using Cloud Assistant.
      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: Applies a tag to the updated ECS instances for tracking.
      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: Determines whether to resume scaling processes based on the exitProcess parameter.
      zh-cn: 根据流程值判断是否恢复流程
    Properties:
      DefaultTask: resumeProcesses
      Choices:
        - When:
            'Fn::Equals':
              - []
              - '{{ exitProcess }}'
          NextTask: 'ACS::END'
  - Name: resumeProcesses
    Action: 'ACS::ExecuteAPI'
    OnSuccess: 'ACS::END'
    Description:
      en: Resumes the specified scaling group processes.
      zh-cn: 恢复伸缩组流程
    Properties:
      Service: ESS
      API: ResumeProcesses
      Parameters:
        ScalingGroupId: '{{ scalingGroupId }}'
        Process: '{{ exitProcess }}'
  - Name: getExecutionInstanceIds
    Action: 'ACS::ExecuteAPI'
    Description:
      en: Retrieves the instance IDs from a previous execution for a rollback.
      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: Filters the list of instances to include only those that still exist.
      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: Retrieves the process parameters from the original execution.
      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: Suspends the scaling group processes for a rollback.
      zh-cn: 暂停伸缩组流程
    Properties:
      Service: ESS
      API: SuspendProcesses
      Parameters:
        ScalingGroupId: '{{ scalingGroupId }}'
        Process: '{{ getRollbackProcess.enterProcess }}'
  - Name: rollbackByRunCommand
    Action: 'ACS::ESS::RunCommand'
    Description:
      en: Runs the rollback command by using Cloud Assistant.
      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: Determines whether to resume scaling processes after a rollback.
      zh-cn: 根据流程值判断是否恢复流程
    Properties:
      DefaultTask: resumeProcessesWithRollback
      Choices:
        - When:
            'Fn::Equals':
              - []
              - '{{ getRollbackProcess.exitProcess }}'
          NextTask: 'ACS::END'
  - Name: resumeProcessesWithRollback
    Action: 'ACS::ExecuteAPI'
    Description:
      en: Resumes the scaling group processes after a rollback.
      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 }}'