文档

ACS-ESS-RollingUpdateByDownloadFileAndRunCommand

更新时间:

模板名称

ACS-ESS-RollingUpdateByDownloadFileAndRunCommand 通过下载文件并执行命令的方式滚动更新伸缩组内的ECS实例

立即执行

模板描述

通过下载文件并执行命令的方式滚动更新伸缩组内的ECS实例

模板类型

自动化

所有者

Alibaba Cloud

输入参数

参数名称

描述

类型

是否必填

默认值

约束

invokeType

操作类型

String

scalingGroupId

伸缩组ID

String

invokeSourcePath

用于升级的文件URL

String

invokeDestinationDir

用于升级的文件存储目录

String

rollbackSourcePath

用于回滚的文件URL

String

rollbackDestinationDir

用于回滚的文件存储目录

String

enterProcess

执行开始要暂停的伸缩组流程

List

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

exitProcess

执行结束要恢复的伸缩组流程

List

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

invokeScript

用于升级的脚本

String

""

rollbackScript

用于回滚的脚本

String

""

sourceExecutionId

用于回滚的OOS执行ID

String

""

whetherSaveToFile

是否将内容下载到特定文件

Boolean

True

batchPauseOption

分批模式

String

Automatic

batchNumber

分批执行的批次

Number

5

OOSAssumeRole

OOS扮演的RAM角色

String

""

输出参数

参数名称

描述

类型

commandOutputs

List

执行此模板需要的权限策略

{
    "Version": "1",
    "Statement": [
        {
            "Action": [
                "ess:CreateLifecycleHook",
                "ess:DescribeLifecycleHooks",
                "ess:DescribeScalingInstances",
                "ess:ModifyLifecycleHook",
                "ess:ResumeProcesses",
                "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"
        }
    ]
}

详情

ACS-ESS-RollingUpdateByDownloadFileAndRunCommand详情

模板内容

FormatVersion: OOS-2019-06-01
Description:
  en: Rolling update ECS instances that in scaling group by download file and run command
  zh-cn: 通过下载文件并执行命令的方式滚动更新伸缩组内的ECS实例
  name-en: ACS-ESS-RollingUpdateByDownloadFileAndRunCommand
  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
  invokeSourcePath:
    Type: String
    Label:
      en: InvokeSourcePath
      zh-cn: 用于升级的文件URL
  invokeDestinationDir:
    Label:
      en: InvokeDestinationDir
      zh-cn: 用于升级的文件存储目录
    Type: String
  invokeScript:
    Label:
      en: InvokeScript
      zh-cn: 用于升级的脚本
    Type: String
    AssociationProperty: Code
    Default: ''
  rollbackSourcePath:
    Type: String
    Label:
      en: InvokeSourcePath
      zh-cn: 用于回滚的文件URL
  rollbackDestinationDir:
    Label:
      en: InvokeDestinationDir
      zh-cn: 用于回滚的文件存储目录
    Type: String
  rollbackScript:
    Label:
      en: RollbackScript
      zh-cn: 用于回滚的脚本
    Type: String
    AssociationProperty: Code
    Default: ''
  sourceExecutionId:
    Label:
      en: SourceExecutionId
      zh-cn: 用于回滚的OOS执行ID
    Type: String
    Default: ''
  whetherSaveToFile:
    Label:
      en: whetherSaveToFile
      zh-cn: 是否将内容下载到特定文件
    Type: Boolean
    Default: true
  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: getLifecycleHook
    Action: ACS::ExecuteAPI
    Description:
      en: Get lifecycle hook
      zh-cn: 获取生命周期挂钩
    Properties:
      Service: ESS
      API: DescribeLifecycleHooks
      Parameters:
        ScalingGroupId: '{{ scalingGroupId }}'
        LifecycleHookName: ACS-ESS-LifeCycleDownloadFileAndRunCommand
    Outputs:
      totalCount:
        Type: String
        ValueSelector: .TotalCount
  - 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 }}'
        LifecycleState: InService
        CreationTypes:
          - AutoCreated
          - Attached
    Outputs:
      instanceIds:
        Type: List
        ValueSelector: 'ScalingInstances.ScalingInstance[].InstanceId'
  - Name: createLifecycleHookOfUpgrade
    When:
      Fn::Equals:
        - '{{ getLifecycleHook.totalCount }}'
        - 0
    Action: ACS::ExecuteAPI
    Description:
      en: Create lifecycle hook of upgrade
      zh-cn: 创建用于升级的生命周期挂钩
    Properties:
      Service: ESS
      API: CreateLifecycleHook
      Parameters:
        ScalingGroupId: '{{ scalingGroupId }}'
        NotificationMetadata: '{"sourcePath":"{{ invokeSourcePath }}","destinationDir":"{{ invokeDestinationDir }}","commandContent":"{{ invokeScript }}","timeout":"600","regionId":"${regionId}","instanceIds":"${instanceIds}","lifecycleHookId":"${lifecycleHookId}","lifecycleActionToken":"${lifecycleActionToken}","rateControl":"{\"Mode\":\"Concurrency\",\"MaxErrors\":0,\"Concurrency\":10}","OOSAssumeRole":"OOSServiceRole"}'
        LifecycleTransition: SCALE_OUT
        LifecycleHookName: ACS-ESS-LifeCycleDownloadFileAndRunCommand
        NotificationArn: acs:oos:{{ ACS::RegionId }}:{{ACS::AccountId}}:template/ACS-ESS-LifeCycleDownloadFileAndRunCommand
  - Name: modifyLifecycleHookOfUpgrade
    When:
      Fn::Equals:
        - '{{ getLifecycleHook.totalCount }}'
        - 1
    Action: ACS::ExecuteAPI
    Description:
      en: Modify lifecycle hook of upgrade
      zh-cn: 修改用于升级的生命周期挂钩
    Properties:
      Service: ESS
      API: ModifyLifecycleHook
      Parameters:
        ScalingGroupId: '{{ scalingGroupId }}'
        NotificationMetadata: '{"sourcePath":"{{ invokeSourcePath }}","destinationDir":"{{ invokeDestinationDir }}","commandContent":"{{ invokeScript }}","timeout":"600","regionId":"${regionId}","instanceIds":"${instanceIds}","lifecycleHookId":"${lifecycleHookId}","lifecycleActionToken":"${lifecycleActionToken}","rateControl":"{\"Mode\":\"Concurrency\",\"MaxErrors\":0,\"Concurrency\":10}","OOSAssumeRole":"OOSServiceRole"}'
        LifecycleTransition: SCALE_OUT
        LifecycleHookName: ACS-ESS-LifeCycleDownloadFileAndRunCommand
  - 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: invokeByDownloadFileAndRunCommand
  - Name: suspendProcess
    Action: 'ACS::ExecuteAPI'
    Description:
      en: Suspend process
      zh-cn: 暂停伸缩组流程
    Properties:
      Service: ESS
      API: SuspendProcesses
      Parameters:
        ScalingGroupId: '{{ scalingGroupId }}'
        Process: '{{ enterProcess }}'
  - Name: invokeByDownloadFileAndRunCommand
    Action: ACS::ESS::DownloadFileAndRunCommand
    Description:
      en: Upgrade by downloading files to the instance and executing commands
      zh-cn: 通过下载文件到实例并执行命令的方式进行升级
    Properties:
      instanceId: '{{ ACS::TaskLoopItem }}'
      sourceType: 'https'
      sourcePath: '{{ invokeSourcePath }}'
      destinationDir: '{{ invokeDestinationDir }}'
      commandContent: '{{ invokeScript }}'
      whetherSaveToFile: '{{ whetherSaveToFile }}'
    Loop:
      Items: '{{ getInstance.instanceIds }}'
      BatchPauseOption: '{{ batchPauseOption }}'
      Concurrency:
        'Fn::CalculateBatch':
          - '{{ batchNumber }}'
          - '{{ getInstance.instanceIds }}'
      Outputs:
        commandOutputs:
          AggregateType: 'Fn::ListJoin'
          AggregateField: commandOutput
    Outputs:
      commandOutput:
        Type: String
        ValueSelector: runCommandOutput
  - Name: tagInstance
    Action: ACS::ExecuteApi
    Description:
      en: Creates and attaches tag to ECS instance
      zh-cn: 创建并挂载标签到ECS实例
    Properties:
      Service: ECS
      API: TagResources
      Parameters:
        ResourceIds:
          - '{{ 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'
  - 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: rollbackByDownloadFileAndRunCommand
    Action: ACS::ESS::DownloadFileAndRunCommand
    Description:
      en: Rollback by downloading files to the instance and executing commands
      zh-cn: 通过下载文件到实例并执行命令的方式进行回滚
    Properties:
      instanceId: '{{ ACS::TaskLoopItem }}'
      sourceType: 'https'
      sourcePath: '{{ rollbackSourcePath }}'
      destinationDir: '{{ rollbackDestinationDir }}'
      commandContent: '{{ rollbackScript }}'
      whetherSaveToFile: '{{ whetherSaveToFile }}'
    Loop:
      Items: '{{ getExistInstanceIds.instanceIds }}'
      BatchPauseOption: '{{ batchPauseOption }}'
      Concurrency:
        'Fn::CalculateBatch':
          - '{{ batchNumber }}'
          - '{{ getExistInstanceIds.instanceIds }}'
      Outputs:
        commandOutputs:
          AggregateType: 'Fn::ListJoin'
          AggregateField: commandOutput
    Outputs:
      commandOutput:
        Type: String
        ValueSelector: runCommandOutput
  - Name: modifyLifecycleHookOfRollback
    When:
      Fn::Equals:
        - '{{ getLifecycleHook.totalCount }}'
        - 1
    Action: ACS::ExecuteAPI
    Description:
      en: Modify lifecycle hook of rollback
      zh-cn: 修改用于回滚的生命周期挂钩
    Properties:
      Service: ESS
      API: ModifyLifecycleHook
      Parameters:
        ScalingGroupId: '{{ scalingGroupId }}'
        NotificationMetadata: '{"sourcePath":"{{ rollbackSourcePath }}","destinationDir":"{{ rollbackDestinationDir }}","commandContent":"{{ rollbackScript }}","timeout":"600","regionId":"${regionId}","instanceIds":"${instanceIds}","lifecycleHookId":"${lifecycleHookId}","lifecycleActionToken":"${lifecycleActionToken}","rateControl":"{\"Mode\":\"Concurrency\",\"MaxErrors\":0,\"Concurrency\":10}","OOSAssumeRole":"OOSServiceRole"}'
        LifecycleTransition: SCALE_OUT
        LifecycleHookName: ACS-ESS-LifeCycleDownloadFileAndRunCommand
  - 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 }}'
        - '{{ invokeByDownloadFileAndRunCommand.commandOutputs }}'
        - '{{ rollbackByDownloadFileAndRunCommand.commandOutputs }}'