ROSTemplateFormatVersion: '2015-09-01'
Description:
  zh-cn: 创建一个ECS实例与RDS MySQL 5.7数据库，支持付费类型选择，ECS实例配置（类型、系统盘、密码等），RDS配置（类别、规格、存储、账号）及网络设置。
  en: Create an ECS instance and RDS MySQL 5.7 database, supporting payment type selection, ECS instance configuration (type, system disk, password, etc.), RDS configuration (category, specifications, storage, account) and network settings.
Parameters:
  PayType:
    Type: String
    Label:
      en: ECS Instance Charge Type
      zh-cn: 付费类型
    Default: PostPaid
    AllowedValues:
      - PostPaid
      - PrePaid
    AssociationProperty: ChargeType
    AssociationPropertyMetadata:
      LocaleKey: InstanceChargeType
  PayPeriodUnit:
    Type: String
    Label:
      en: Pay Period Unit
      zh-cn: 购买资源时长周期
    Default: Month
    AllowedValues:
      - Month
      - Year
    AssociationProperty: PayPeriodUnit
    AssociationPropertyMetadata:
      Visible:
        Condition:
          Fn::Not:
            Fn::Equals:
              - ${PayType}
              - PostPaid
  PayPeriod:
    Type: Number
    Description:
      en: When the resource purchase duration is Month, the value of Period ranges from 1 to 9, 12, 24, 36, 48, or 60. <br><b><font color='red'> When ECS instance types are PrePaid valid </b></font>
      zh-cn: 当购买资源时长为Month时，Period取值：1~9 <br><b><font color='red'>当ECS实例类型为PrePaid有效</b></font>
    Label:
      en: Period
      zh-cn: 购买资源时长
    Default: 1
    AllowedValues:
      - 1
      - 2
      - 3
      - 4
      - 5
      - 6
      - 7
      - 8
      - 9
    AssociationProperty: PayPeriod
    AssociationPropertyMetadata:
      Visible:
        Condition:
          Fn::Not:
            Fn::Equals:
              - ${PayType}
              - PostPaid
  ZoneId:
    Type: String
    AssociationProperty: ALIYUN::ECS::Instance::ZoneId
    Label:
      en: VSwitch Availability Zone
      zh-cn: 交换机可用区
  VpcId:
    AssociationProperty: ALIYUN::ECS::VPC::VPCId
    Type: String
    Description:
      en: Please search the ID starting with (vpc-xxx) from console-Virtual Private Cloud
      zh-cn: 现有虚拟专有网络的实例ID
    Label:
      en: VPC ID
      zh-cn: 专有网络VPC实例ID
  VSwitchId:
    AssociationProperty: ALIYUN::ECS::VSwitch::VSwitchId
    AssociationPropertyMetadata:
      VpcId: ${VpcId}
      ZoneId: ${ZoneId}
    Type: String
    Description:
      en: Instance ID of existing business network switches, console-Virtual Private Cloud-VSwitches under query
      zh-cn: 现有业务网络交换机的实例ID
    Label:
      en: VSwitch ID
      zh-cn: 交换机实例ID
  EcsInstanceType:
    Type: String
    Label:
      en: Instance Type
      zh-cn: 实例类型
    AssociationProperty: ALIYUN::ECS::Instance::InstanceType
    AssociationPropertyMetadata:
      InstanceChargeType: ${PayType}
  SystemDiskCategory:
    Type: String
    AllowedValues:
      - cloud_efficiency
      - cloud_ssd
      - cloud
      - cloud_essd
    Label:
      en: System Disk Category
      zh-cn: 系统盘类型
    Default: cloud_essd
    AssociationPropertyMetadata:
      LocaleKey: DiskCategory
  SystemDiskSize:
    Default: 40
    Type: Number
    Label:
      zh-cn: 系统盘空间 (GB)
      en: System Disk Space (GB)
  InstancePassword:
    NoEcho: true
    Type: String
    Description:
      en: Server login password, Length 8-30, must contain three(Capital letters, lowercase letters, numbers, ()`~!@#$%^&*_-+=|{}[]:;<>,.?/ Special symbol in)
      zh-cn: 服务器登录密码,长度8-30，必须包含三项（大写字母、小写字母、数字、 ()`~!@#$%^&*_-+=|{}[]:;<>,.?/ 中的特殊符号）
    Label:
      en: Instance Password
      zh-cn: 实例密码
    ConstraintDescription:
      en: Length 8-30, must contain three(Capital letters, lowercase letters, numbers, ()`~!@#$%^&*_-+=|{}[]:;<>,.?/ Special symbol in)
      zh-cn: 长度8-30，必须包含三项（大写字母、小写字母、数字、 ()`~!@#$%^&*_-+=|{}[]:;<>,.?/ 中的特殊符号）
    AssociationProperty: ALIYUN::ECS::Instance::Password
    AllowedPattern: '^[a-zA-Z0-9-\(\)\`\~\!\@\#\$\%\^\&\*\_\-\+\=\|\{\}\[\]\:\;\<\>\,\.\?\/]*$'
    MinLength: 8
    MaxLength: 30
  InstanceCount:
    Type: Number
    Label:
      en: Instance Count
      zh-cn: Node节点数量
    Default: 1
  DBCategory:
    Label:
      zh-cn: 实例系列
      en: DB Category
    Type: String
    AllowedValues:
      - Basic
      - HighAvailability
      - AlwaysOn
      - Finance
    AssociationPropertyMetadata:
      LocaleKey: RDSCategory
  DBInstanceClass:
    Label:
      zh-cn: 实例规格
      en: DB Instance Class
    AssociationProperty: ALIYUN::RDS::Instance::InstanceType
    AssociationPropertyMetadata:
      Engine: MySQL
      EngineVersion: 5.7
      Category: ${DBCategory}
    Type: String
    Description:
      zh-cn: 根据数据库引擎的类型和可用的区域支持选择实例规格；<br>请参见详细信息：<a href='https://help.aliyun.com/document_detail/26312.html' target='_blank'><b><font color='blue'>实例规格表</font></b></a>
      en: 'Select the instance specification based on the type of database engine and the available area support;<br>see detail: <a href=''https://www.alibabacloud.com/help/doc-detail/26312.html'' target=''_blank''><b><font color=''blue''>Instance specification sheet</font></b></a>'
    Default: null
  DBInstanceStorage:
    Label:
      zh-cn: 实例存储
      en: Storage
    Type: Number
    Description:
      zh-cn: RDS实例大小范围为20-2000，每5个增量，单位为GB
      en: The size range of RDS instances is 20 - 2000, Incrementing in every 5, unit GB
    MinValue: 20
    MaxValue: 2000
    ConstraintDescription:
      zh-cn: RDS实例大小范围为20-2000，每5个增量，单位为GB
      en: The size range of RDS instances is 20 - 2000, Incrementing in every 5, unit GB
    Default: 200
  DBInstanceStorageType:
    Label:
      zh-cn: 实例存储类型
      en: Storage Type
    Type: String
    Description:
      zh-cn: 实例存储类型；<br>请参见详细信息：<a href='https://help.aliyun.com/document_detail/69795.html' target='_blank'><b><font color='blue'>存储类型</font></b></a>
      en: The storage type of DB instance; see detail <a href='https://www.alibabacloud.com/help/en/apsaradb-for-rds/latest/storage-types' target='_blank'><b><font color=''blue''>Storage types</font></b></a>'
    AllowedValues:
        - local_ssd
        - cloud_ssd
        - cloud_essd
        - cloud_essd2
        - cloud_essd3
  DBMasterUsername:
    Type: String
    Description:
      en: Primary account name of the database instance.
      zh-cn: 数据库实例的主账号名称。
    ConstraintDescription:
      en: Consist of 2 to 16 characters of lowercase letters, underline. Must begin with a letter and be end with an alphanumeric character
      zh-cn: 由 2 到 16 个小写字母组成，下划线。必须以字母开头，以字母数字字符结尾
    Label:
      zh-cn: 数据库账号名称
      en: DB Username
    Default: rdsuser
    MaxLength: 16
    MinLength: 2
  DBMasterPassword:
    NoEcho: true
    Type: String
    Description:
      en: Server login password, Length 8-30, must contain three(Capital letters, lowercase letters, numbers, ()`~!@#$%^&*_-+=|{}[]:;<>,.?/ Special symbol in)
      zh-cn: 服务器登录密码,长度8-30，必须包含三项（大写字母、小写字母、数字、 ()`~!@#$%^&*_-+=|{}[]:;<>,.?/ 中的特殊符号）
    Label:
      en: Instance Password
      zh-cn: 实例密码
    ConstraintDescription:
      en: Length 8-30, must contain three(Capital letters, lowercase letters, numbers, ()`~!@#$%^&*_-+=|{}[]:;<>,.?/ Special symbol in)
      zh-cn: 长度8-30，必须包含三项（大写字母、小写字母、数字、 ()`~!@#$%^&*_-+=|{}[]:;<>,.?/ 中的特殊符号）
    AllowedPattern: '^[a-zA-Z0-9-\(\)\`\~\!\@\#\$\%\^\&\*\_\-\+\=\|\{\}\[\]\:\;\<\>\,\.\?\/]*$'
    MinLength: 8
    MaxLength: 30
# 资源配置
Resources:
  EcsSecurityGroup:
    Type: ALIYUN::ECS::SecurityGroup
    Properties:
      SecurityGroupName:
        Ref: ALIYUN::StackName
      VpcId:
        Ref: VpcId
      SecurityGroupEgress:
        - PortRange: '-1/-1'
          Priority: 1
          IpProtocol: all
          DestCidrIp: 0.0.0.0/0
          NicType: intranet
  Database:
    Type: ALIYUN::RDS::DBInstance
    Properties:
      ZoneId:
        Ref: ZoneId
      VpcId:
        Ref: VpcId
      VSwitchId:
        Ref: VSwitchId
      Engine: MySQL
      EngineVersion: '5.7'
      DBInstanceClass:
        Ref: DBInstanceClass
      DBInstanceStorage:
        Ref: DBInstanceStorage
      DBInstanceStorageType:
        Ref: DBInstanceStorageType
      Category:
        Ref: DBCategory
      DBInstanceNetType: Intranet
      MasterUserPassword:
        Ref: DBMasterPassword
      MasterUsername:
        Ref: DBMasterUsername
      PayType:
        Ref: PayType
      PeriodType:
        Ref: PayPeriodUnit
      Period:
        Ref: PayPeriod
      SecurityIPList:
        Fn::Join:
          - ','
          - Fn::GetAtt:
              - InstanceGroup
              - PrivateIps
  InstanceGroup:
    Type: ALIYUN::ECS::InstanceGroup
    Properties:
      # 实例名
      InstanceName:
        Fn::Join:
          - '-'
          - - Ref: ALIYUN::StackName
            - '[1,4]'
      IoOptimized: optimized
      # 付费类型
      InstanceChargeType:
        Ref: PayType
      PeriodUnit:
        Ref: PayPeriodUnit
      Period:
        Ref: PayPeriod
      # 网络配置
      ZoneId:
        Ref: ZoneId
      VpcId:
        Ref: VpcId
      VSwitchId:
        Ref: VSwitchId
      SecurityGroupId:
        Ref: EcsSecurityGroup
      # 磁盘类型和大小
      SystemDiskCategory:
        Ref: SystemDiskCategory
      SystemDiskSize:
        Ref: SystemDiskSize
      MaxAmount:
        Ref: InstanceCount
      # 镜像
      ImageId: centos_7
      # 实例类型
      InstanceType:
        Ref: EcsInstanceType
      Password:
        Ref: InstancePassword
      # 公网关闭
      AllocatePublicIP: false
      # 启动脚本
      UserData: -|
        #!/bin/bash
  # 定义waitCondition和waitConditionHandle来等待跳板机命令执行完毕部署成功
  WaitCondition:
    Type: ALIYUN::ROS::WaitCondition
    DependsOn:
      - Database
      - InstanceGroup
    Properties:
      Count: 1
      Handle:
        Ref: WaitConditionHandle
      Timeout: 300
  WaitConditionHandle:
    Type: ALIYUN::ROS::WaitConditionHandle
  # 到机器上执行命令
  InstanceRunCommand:
    Type: ALIYUN::ECS::RunCommand
    Properties:
      CommandContent:
        Fn::Sub:
          # 将master的ip输入到/root/conf.txt
          # 可以在后续的步骤中通过conf.txt里的内容完成slave的初始化
          - |
            #!/bin/bash
            cat >> /root/conf.txt << "EOF"
            DBMasterUsername=${DBMasterUsername}
            DBMasterPassword=${DBMasterPassword}
            DBConnectString=${DBConnectString}
            EOF
            # 继续进行初始化 在这里补上自己的代码 数据库的初始化可以用shell mysqldump完成
            # init.sh /root/conf.txt
            # init.sh ${DBMasterUsername} ${DBMasterPassword} ${DBConnectString} 也是可以的

            # 执行成功回调WaitCondition结束waitCondition的等待
            ${CurlCli} -d "{\"Data\" : \"Success\", \"status\" : \"SUCCESS\"}"
          - DBMasterUsername:
              Ref: DBMasterUsername
            # 从用户输入中拿到用户名和密码
            DBMasterPassword:
              Ref: DBMasterPassword
            # 从Database的Output中拿到内网连接串
            DBConnectString:
              Fn::GetAtt:
                - Database
                - InnerConnectionString
            # 获取到waitConditionHandle的地址放到 ${CurlCli}变量里
            CurlCli:
              Fn::GetAtt:
                - WaitConditionHandle
                - CurlCli
      Type: RunShellScript
      InstanceIds:
        Fn::GetAtt:
          - InstanceGroup
          - InstanceIds
      # 超时时间
      Timeout: '300'
Metadata:
  ALIYUN::ROS::Interface:
    # 分组信息
    ParameterGroups:
      - Parameters:
          - PayType
          - PayPeriodUnit
          - PayPeriod
        Label:
          default:
            en: PayType Configuration
            zh-cn: 付费类型配置
      - Parameters:
          - EcsInstanceType
          - SystemDiskCategory
          - SystemDiskSize
          - InstancePassword
          - InstanceCount
        Label:
          default:
            en: Instance
            zh-cn: ECS实例配置
      - Parameters:
          - DBCategory
          - DBInstanceClass
          - DBInstanceStorage
          - DBInstanceStorageType
          - DBMasterUsername
          - DBMasterPassword
        Label:
          default:
            en: Database
            zh-cn: 数据库配置
      - Parameters:
          - ZoneId
        Label:
          default:
            zh-cn: 可用区配置
            en: Zone Configuration
      - Parameters:
          - VpcId
          - VSwitchId
        Label:
          default:
            zh-cn: 选择已有基础资源配置
            en: Choose existing Infrastructure Configuration
