全部产品
Search
文档中心

系统运维管理:ACS-ECS-UpdateImage

更新时间:Dec 17, 2024

模板名称

ACS-ECS-UpdateImage 更新镜像

立即执行

模板描述

通过运行命令更新一个已存在的ECS镜像后创建一个新ECS镜像,适用于需要对ECS镜像进行批量更新和管理的场景,例如软件安装和系统配置更改。在配置过程中,用户需要提供以下必填参数信息:地域ID(regionId):用于指定创建和更新镜像的操作区域,源镜像ID(sourceImageId):用于指定需要更新的原始ECS镜像。其他可在默认值基础上修改的参数包括:目标镜像名称(targetImageName):用于为新创建的ECS镜像命名,实例类型(instanceType):用于安装和配置新的镜像实例类型,以及更新方式(updateType):可选择运行命令或安装软件包。模板在执行后会返回新创建的镜像ID,方便用户验证和管理创建的ECS镜像。

模板类型

自动化

所有者

Alibaba Cloud

输入参数

参数名称

描述

类型

是否必填

默认值

约束

sourceImageId

源镜像ID

String

instanceType

实例类型

String

regionId

地域ID

String

{{ ACS::RegionId }}

zoneId

交换机可用区

String

“”

targetImageName

新镜像的名称

String

UpdateImage_from_{{sourceImageId}}on{{ACS::ExecutionId}}

whetherCreateVpc

是否新建VPC

Boolean

False

vpcCidrBlock

专有网络IPv4网段

String

192.168.0.0/16

vSwitchCidrBlock

交换机子网网段

String

192.168.1.0/24

securityGroupId

安全组ID

String

“”

vSwitchId

交换机ID

String

“”

internetMaxBandwidthOut

流量公网带宽

Number

0

ramRoleName

实例的RAM角色

String

“”

systemDiskCategory

系统盘的云盘种类

String

cloud_essd

commandType

云助手命令类型

String

RunShellScript

tags

镜像标签

Json

[]

commandContent

在ECS实例中执行的云助手命令

String

echo hello

timeout

超时时间

Number

600

updateType

更新方式

String

RunCommand

packageName

要配置的软件包名称

String

“”

packageVersion

要配置的软件包版本

String

“”

parameters

命令中的参数

Json

{}

OOSAssumeRole

OOS扮演的RAM角色

String

“”

输出参数

参数名称

描述

类型

imageId

String

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

{
    "Version": "1",
    "Statement": [
        {
            "Action": [
                "ecs:CreateImage",
                "ecs:DeleteInstance",
                "ecs:DescribeCloudAssistantStatus",
                "ecs:DescribeImages",
                "ecs:DescribeInstances",
                "ecs:DescribeInvocationResults",
                "ecs:DescribeInvocations",
                "ecs:InstallCloudAssistant",
                "ecs:RebootInstance",
                "ecs:RunCommand",
                "ecs:RunInstances",
                "ecs:StopInstance"
            ],
            "Resource": "*",
            "Effect": "Allow"
        },
        {
            "Action": [
                "ros:CreateStack",
                "ros:DeleteStack",
                "ros:GetStack"
            ],
            "Resource": "*",
            "Effect": "Allow"
        },
        {
            "Action": [
                "oos:GetTemplate",
                "oos:UpdateInstancePackageState"
            ],
            "Resource": "*",
            "Effect": "Allow"
        },
        {
            "Action": [
                "oss:GetObject"
            ],
            "Resource": "*",
            "Effect": "Allow"
        }
    ]
}

详情

ACS-ECS-UpdateImage详情

模板内容

FormatVersion: OOS-2019-06-01
Description:
  en: 'Update an existing ECS image by running commands and create a new ECS image. This is applicable to scenarios requiring batch updates and management of ECS images, such as software installation and system configuration changes. During configuration, users need to provide the following required parameters: Region ID (regionId), which specifies the region where the image update and creation operations occur, and Source Image ID (sourceImageId), which identifies the original ECS image that needs updating. Additional parameters that can be modified from their default values include: Target Image Name (targetImageName) for naming the newly created ECS image, Instance Type (instanceType) for the type of instance to install and configure the new image on, and Update Type (updateType), which can be selected to either run commands or install packages. Upon execution, the template will return the ID of the newly created image, facilitating the verification and management of the created ECS image.'
  zh-cn: 通过运行命令更新一个已存在的ECS镜像后创建一个新ECS镜像,适用于需要对ECS镜像进行批量更新和管理的场景,例如软件安装和系统配置更改。在配置过程中,用户需要提供以下必填参数信息:地域ID(regionId):用于指定创建和更新镜像的操作区域,源镜像ID(sourceImageId):用于指定需要更新的原始ECS镜像。其他可在默认值基础上修改的参数包括:目标镜像名称(targetImageName):用于为新创建的ECS镜像命名,实例类型(instanceType):用于安装和配置新的镜像实例类型,以及更新方式(updateType):可选择运行命令或安装软件包。模板在执行后会返回新创建的镜像ID,方便用户验证和管理创建的ECS镜像。
  name-en: Update Image
  name-zh-cn: 更新镜像
  categories:
    - image_manage
    - application_manage
Parameters:
  regionId:
    Type: String
    Label:
      en: RegionId
      zh-cn: 地域ID
    AssociationProperty: RegionId
    Default: '{{ ACS::RegionId }}'
  zoneId:
    Type: String
    Label:
      en: VSwitch Availability Zone
      zh-cn: 交换机可用区
    Default: ''
    AssociationProperty: ALIYUN::ECS::Instance::ZoneId
    AssociationPropertyMetadata:
      RegionId: regionId
  targetImageName:
    Label:
      en: TargetImageName
      zh-cn: 新镜像的名称
    Type: String
    Description:
      en: <p class="p">Note:</p> <ul class="ul"> <li class="li">Length is 2~128 English or Chinese characters</li> <li class="li"><font color='red'>must start with big or small letters or Chinese, not http:// and https://. </font></li> <li class="li">Can contain numbers, colons (:), underscores (_), or dashes (-). </li> </ul>
      zh-cn: <p class="p">注意:</p> <ul class="ul"> <li class="li">长度为2~128个英文或中文字符</li> <li class="li"><font color='red'>必须以大小字母或中文开头,不能以http://和https://开头。</font></li> <li class="li">可以包含数字、半角冒号(:)、下划线(_)或者短划线(-)。</li> </ul>
    Default: 'UpdateImage_from_{{sourceImageId}}_on_{{ACS::ExecutionId}}'
  sourceImageId:
    Label:
      en: SourceImageId
      zh-cn: 源镜像ID
    Type: String
    AssociationProperty: 'ALIYUN::ECS::Image::ImageId'
    AssociationPropertyMetadata:
      RegionId: regionId
      ImageOwnerAlias:
        - self
  instanceType:
    Label:
      en: InstanceType
      zh-cn: 实例类型
    Type: String
    AssociationProperty: 'ALIYUN::ECS::Instance::InstanceType'
    AssociationPropertyMetadata:
      RegionId: regionId
      ZoneId: zoneId
  whetherCreateVpc:
    Type: Boolean
    Label:
      en: WhetherCreateVpc
      zh-cn: 是否新建VPC
    Default: false
  vpcCidrBlock:
    Type: String
    Label:
      en: VPC CIDR IPv4 Block
      zh-cn: 专有网络IPv4网段
    Description:
      zh-cn: VPC的ip地址段范围,<br>您可以使用以下的ip地址段或其子网:<br><font color='green'>[10.0.0.0/8]</font><br><font color='green'>[172.16.0.0/12]</font><br><font color='green'>[192.168.0.0/16]</font>
      en: 'The ip address range of the VPC in the CidrBlock form; <br>You can use the following ip address ranges and their subnets: <br><font color=''green''>[10.0.0.0/8]</font><br><font color=''green''>[172.16.0.0/12]</font><br><font color=''green''>[192.168.0.0/16]</font>'
    Default: 192.168.0.0/16
    AssociationProperty: ALIYUN::VPC::VPC::CidrBlock
    AssociationPropertyMetadata:
      Visible:
        Condition:
          Fn::Equals:
            - ${whetherCreateVpc}
            - true
  vSwitchCidrBlock:
    Type: String
    Label:
      en: VSwitch CIDR Block
      zh-cn: 交换机子网网段
    Description:
      zh-cn: 必须属于VPC的子网段
      en: Must belong to the subnet segment of VPC
    Default: 192.168.1.0/24
    AssociationProperty: ALIYUN::VPC::VSwitch::CidrBlock
    AssociationPropertyMetadata:
      VpcCidrBlock: vpcCidrBlock
      Visible:
        Condition:
          Fn::Equals:
            - ${whetherCreateVpc}
            - true
  securityGroupId:
    Label:
      en: SecurityGroupId
      zh-cn: 安全组ID
    Type: String
    Default: ''
    AssociationProperty: 'ALIYUN::ECS::SecurityGroup::SecurityGroupId'
    AssociationPropertyMetadata:
      RegionId: regionId
      Visible:
        Condition:
          Fn::Equals:
            - ${whetherCreateVpc}
            - false
  vSwitchId:
    Label:
      en: VSwitchId
      zh-cn: 交换机ID
    Type: String
    Default: ''
    AssociationProperty: 'ALIYUN::VPC::VSwitch::VSwitchId'
    AssociationPropertyMetadata:
      RegionId: regionId
      ZoneId: zoneId
      Filters:
        - SecurityGroupId: securityGroupId
      Visible:
        Condition:
          Fn::Equals:
            - ${whetherCreateVpc}
            - false
  internetMaxBandwidthOut:
    Type: Number
    Label:
      zh-cn: 流量公网带宽
      en: Internet Max Bandwidth Out
    Description:
      zh-cn: 取值范围0-100, 0为不开公网ip
      en: no public ip if zero
    Default: 0
    MinValue: 0
    MaxValue: 100
  ramRoleName:
    Label:
      en: RamRoleName
      zh-cn: 实例的RAM角色
    Type: String
    AssociationProperty: ALIYUN::ECS::RAM::Role
    Default: ''
  systemDiskCategory:
    Label:
      en: SystemDiskCategory
      zh-cn: 系统盘的云盘种类
    Type: String
    AssociationProperty: ALIYUN::ECS::Disk::SystemDiskCategory
    AssociationPropertyMetadata:
      RegionId: regionId
      InstanceType: instanceType
    Default: cloud_essd
  commandType:
    Label:
      en: CommandType
      zh-cn: 云助手命令类型
    Type: String
    AssociationPropertyMetadata:
      LocaleKey: PricingCycle
      Visible:
        Condition:
          Fn::Equals:
            - ${updateType}
            - RunCommand
    AllowedValues:
      - RunBatScript
      - RunPowerShellScript
      - RunShellScript
    Default: RunShellScript
  tags:
    Label:
      en: Tags
      zh-cn: 镜像标签
    Type: Json
    AssociationProperty: Tags
    AssociationPropertyMetadata:
      ShowSystem: false
    Default: []
  commandContent:
    Label:
      en: CommandContent
      zh-cn: 在ECS实例中执行的云助手命令
    Type: String
    AssociationProperty: Code
    AssociationPropertyMetadata:
      Visible:
        Condition:
          Fn::Equals:
            - ${updateType}
            - RunCommand
    Default: 'echo hello'
  timeout:
    Label:
      en: Timeout
      zh-cn: 超时时间
    Type: Number
    AssociationPropertyMetadata:
      Visible:
        Condition:
          Fn::Equals:
            - ${updateType}
            - RunCommand
    Default: 600
  updateType:
    Label:
      en: UpdateType
      zh-cn: 更新方式
    Type: String
    AllowedValues:
      - RunCommand
      - InstallPackage
    Default: RunCommand
    AssociationPropertyMetadata:
      ValueLabelMapping:
        RunCommand:
          zh-cn: 运行命令
          en: RunCommand
        InstallPackage:
          zh-cn: 安装软件包
          en: InstallPackage
  packageName:
    Label:
      en: packageName
      zh-cn: 要配置的软件包名称
    Type: String
    Default: ''
    AssociationProperty: 'ALIYUN::OOS::Package::PackageName'
    AssociationPropertyMetadata:
      RegionId: regionId
      Visible:
        Condition:
          Fn::Equals:
            - ${updateType}
            - InstallPackage
  packageVersion:
    Label:
      en: packageVersion
      zh-cn: 要配置的软件包版本
    Type: String
    AssociationProperty: 'ALIYUN::OOS::Package::PackageVersion'
    AssociationPropertyMetadata:
      TemplateName: ${packageName}
      Visible:
        Condition:
          Fn::Equals:
            - ${updateType}
            - InstallPackage
    Default: ''
  parameters:
    Label:
      en: Parameters
      zh-cn: 命令中的参数
    Type: Json
    Default: {}
    AssociationProperty: TemplateParameter
    AssociationPropertyMetadata:
      TemplateName: packageName
      TemplateVersion: packageVersion
      TemplateType: Package
      Visible:
        Condition:
          Fn::Equals:
            - ${updateType}
            - InstallPackage
  OOSAssumeRole:
    Label:
      en: OOSAssumeRole
      zh-cn: OOS扮演的RAM角色
    Type: String
    Default: ''
RamRole: '{{ OOSAssumeRole }}'
Tasks:
- Name: checkNewImageName
  Action: ACS::CheckFor
  Description:
    en: Check image name is available
    zh-cn: 检查镜像名称是可用的
  Properties:
    Service: ECS
    API: DescribeImages
    Parameters:
      RegionId: '{{ regionId }}'
      ImageName: '{{ targetImageName }}'
    DesiredValues:
    - 0
    PropertySelector: TotalCount
- Name: createStack
  Action: 'ACS::ExecuteAPI'
  When:
    Fn::Equals:
      - '{{ whetherCreateVpc }}'
      - true
  Description:
    en: Create a resource stack
    zh-cn: 创建资源栈
  Properties:
    Service: ROS
    API: CreateStack
    Parameters:
      RegionId: '{{ regionId  }}'
      StackName: 'OOS-{{ACS::ExecutionId}}'
      TimeoutInMinutes: 10
      DisableRollback: false
      Parameters:
        - ParameterKey: ZoneId
          ParameterValue: '{{ zoneId }}'
        - ParameterKey: VpcCidrBlock
          ParameterValue: '{{ vpcCidrBlock  }}'
        - ParameterKey: VSwitchCidrBlock
          ParameterValue: '{{ vSwitchCidrBlock  }}'
      TemplateBody: |
        ROSTemplateFormatVersion: '2015-09-01'
        Parameters:
          ZoneId:
            Type: String
          VpcCidrBlock:
            Type: String
          VSwitchCidrBlock:
            Type: String
        Resources:
          EcsVpc:
            Type: ALIYUN::ECS::VPC
            Properties:
              CidrBlock:
                Ref: VpcCidrBlock
              VpcName:
                Ref: ALIYUN::StackName
          EcsVSwitch:
            Type: ALIYUN::ECS::VSwitch
            Properties:
              ZoneId:
                Ref: ZoneId
              VpcId:
                Ref: EcsVpc
              CidrBlock:
                Ref: VSwitchCidrBlock
          EcsSecurityGroup:
            Type: ALIYUN::ECS::SecurityGroup
            Properties:
              VpcId:
                Ref: EcsVpc
        Outputs:
          SecurityGroupId:
            Value:
              Ref: EcsSecurityGroup
          VSwitchId:
            Value:
              Ref: EcsVSwitch
  Outputs:
    StackId:
      Type: String
      ValueSelector: StackId
- Name: untilStackReady
  Action: 'ACS::WaitFor'
  When:
    Fn::Equals:
      - '{{ whetherCreateVpc }}'
      - true
  Description:
    en: Wait for the stack status CREATE_COMPLETE
    zh-cn: 等待资源栈至创建成功状态
  OnError: deleteStack
  Properties:
    Service: ROS
    API: GetStack
    Parameters:
      RegionId: '{{ regionId  }}'
      StackId: '{{createStack.StackId}}'
    DesiredValues:
      - CREATE_COMPLETE
    StopRetryValues:
      - CREATE_FAILED
      - CHECK_FAILED
      - ROLLBACK_FAILED
      - ROLLBACK_COMPLETE
      - CREATE_ROLLBACK_COMPLETE
    PropertySelector: Status
  Outputs:
    securityGroupId:
      Type: String
      ValueSelector: 'Outputs[0].OutputValue'
    vSwitchId:
      Type: String
      ValueSelector: 'Outputs[1].OutputValue'
- Name: runInstances
  Action: ACS::ECS::RunInstances
  Description:
    en: Create a ECS instance with source image
    zh-cn: 用源镜像创建一台ECS实例
  OnError: deleteStack
  Properties:
    regionId: '{{ regionId }}'
    imageId: '{{ sourceImageId }}'
    instanceType: '{{ instanceType }}'
    securityGroupId:
      Fn::If:
        - Fn::Equals:
            - '{{ whetherCreateVpc }}'
            - true
        - '{{ untilStackReady.securityGroupId }}'
        - '{{ securityGroupId }}'
    vSwitchId:
      Fn::If:
        - Fn::Equals:
            - '{{ whetherCreateVpc }}'
            - true
        - '{{ untilStackReady.vSwitchId }}'
        - '{{ vSwitchId }}'
    internetMaxBandwidthOut: '{{ internetMaxBandwidthOut }}'
    ramRoleName: '{{ ramRoleName }}'
    systemDiskCategory: '{{ systemDiskCategory }}'
  Outputs:
    instanceId:
      ValueSelector: instanceIds[0]
      Type: String
- Name: installCloudAssistant
  Action: ACS::ECS::InstallCloudAssistant
  Description:
    en: Install cloud assistant for ECS instance
    zh-cn: 给实例安装云助手
  OnError: deleteInstance
  Properties:
    regionId: '{{ regionId }}'
    instanceId: '{{ runInstances.instanceId }}'
- Name: runCommand
  Action: ACS::ECS::RunCommand
  When:
    Fn::Equals:
      - '{{ updateType }}'
      - RunCommand
  Description:
    en: Run cloud assistant command on ECS instance
    zh-cn: 在实例中运行云助手命令
  OnError: deleteInstance
  Properties:
    regionId: '{{ regionId }}'
    commandContent: '{{ commandContent }}'
    commandType: '{{ commandType }}'
    instanceId: '{{ runInstances.instanceId }}'
    timeout: '{{ timeout }}'
- Name: configurePackage
  Action: ACS::ECS::ConfigureOOSPackageWithTemporaryURL
  Description:
    en: Configure Package
    zh-cn: 配置软件包
  When:
    Fn::Equals:
      - '{{ updateType }}'
      - InstallPackage
  OnError: deleteInstance
  Properties:
    regionId: '{{ regionId }}'
    instanceId: '{{ runInstances.instanceId }}'
    packageName: '{{ packageName }}'
    packageVersion: '{{ packageVersion }}'
    parameters: '{{ parameters }}'
  Outputs:
    commandOutput:
      Type: String
      ValueSelector: .commandOutput
- Name: stopInstance
  Action: ACS::ECS::StopInstance
  Description:
    en: Stops the ECS instance
    zh-cn: 停止ECS实例
  Properties:
    regionId: '{{ regionId }}'
    instanceId: '{{ runInstances.instanceId }}'
- Name: createImage
  Action: ACS::ECS::CreateImage
  Description:
    en: Create new image with the specified image name and instance ID
    zh-cn: 通过指定实例ID和镜像名称创建新的镜像
  OnError: deleteInstance
  Properties:
    regionId: '{{ regionId }}'
    imageName: '{{ targetImageName }}'
    instanceId: '{{ runInstances.instanceId }}'
    tags: '{{tags}}'
  Outputs:
    imageId:
      ValueSelector: imageId
      Type: String
- Name: deleteInstance
  Action: ACS::ExecuteAPI
  Description:
    en: Deletes the ECS instance
    zh-cn: 删除ECS实例
  Properties:
    Service: ECS
    API: DeleteInstance
    Risk: Normal
    Parameters:
      RegionId: '{{ regionId }}'
      InstanceId: '{{ runInstances.instanceId }}'
      Force: true
- Name: deleteStack
  Action: 'ACS::ExecuteApi'
  When:
    Fn::Equals:
      - '{{ whetherCreateVpc }}'
      - true
  Description:
    en: Delete the stack
    zh-cn: 删除资源栈
  Properties:
    Service: ROS
    API: DeleteStack
    Parameters:
      RegionId: '{{ regionId  }}'
      StackId: '{{ createStack.StackId }}'
Outputs:
  imageId:
    Type: String
    Value: '{{ createImage.imageId }}'
Metadata:
  ALIYUN::OOS::Interface:
    ParameterGroups:
      - Parameters:
          - regionId
          - sourceImageId
        Label:
          default:
            zh-cn: 选择源镜像
            en: Select Origin Image
      - Parameters:
          - targetImageName
          - tags
        Label:
          default:
            zh-cn: 镜像设置
            en: Image Configure
      - Parameters:
          - zoneId
          - instanceType
          - whetherCreateVpc
          - vpcCidrBlock
          - vSwitchCidrBlock
          - securityGroupId
          - vSwitchId
          - internetMaxBandwidthOut
          - systemDiskCategory
          - ramRoleName
        Label:
          default:
            zh-cn: 配置中转实例
            en: ECS Instance Configure
      - Parameters:
          - updateType
          - packageName
          - packageVersion
          - parameters
          - commandType
          - commandContent
          - timeout
        Label:
          default:
            zh-cn: 更新镜像配置
            en: Update Image Configuration
      - Parameters:
          - OOSAssumeRole
        Label:
          default:
            zh-cn: 高级选项
            en: Control Options