ROSTemplateFormatVersion: '2015-09-01'
Description:
en: Application deployed by docker
zh-cn: 使用docker部署单机应用
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
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
EcsInstanceType:
Type: String
Label:
en: Instance Type
zh-cn: 实例类型
AssociationProperty: ALIYUN::ECS::Instance::InstanceType
AssociationPropertyMetadata:
InstanceChargeType: ${PayType}
AllowedValues:
- ecs.g8i.large
- ecs.g6.large
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,必须包含三项(大写字母、小写字母、数字、 ()`~!@#$%^&*_-+=|{}[]:;'<>,.?/ 中的特殊符号)
AllowedPattern: '^[a-zA-Z0-9-\(\)\`\~\!\@\#\$\%\^\&\*\_\-\+\=\|\{\}\[\]\:\;\<\>\,\.\?\/]*$'
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,必须包含三项(大写字母、小写字母、数字、 ()`~!@#$%^&*_-+=|{}[]:;'<>,.?/ 中的特殊符号)
MinLength: 8
MaxLength: 30
AssociationProperty: ALIYUN::ECS::Instance::Password
ZoneId:
Type: String
Label:
en: Zone ID
zh-cn: 可用区ID
AssociationProperty: ALIYUN::ECS::Instance::ZoneId
VpcId:
Type: String
Label:
en: VPC ID
zh-cn: 专有网络VPC实例ID
Description:
en: >-
Please search the ID starting with (vpc-xxx) from console-Virtual
Private Cloud
zh-cn: 现有虚拟专有网络的实例ID
AssociationProperty: 'ALIYUN::ECS::VPC::VPCId'
VSwitchId:
Type: String
Label:
en: VSwitch ID
zh-cn: 交换机实例ID
Description:
en: >-
Instance ID of existing business network switches, console-Virtual
Private Cloud-VSwitches under query
zh-cn: 现有业务网络交换机的实例ID
Default: ''
AssociationProperty: 'ALIYUN::ECS::VSwitch::VSwitchId'
AssociationPropertyMetadata:
VpcId: VpcId
ZoneId: ZoneId
AdminPassword:
Type: String
AssociationProperty: ALIYUN::ECS::Instance::Password
Label: 管理员密码
NoEcho: True
Resources:
SecurityGroup:
Type: ALIYUN::ECS::SecurityGroup
Properties:
SecurityGroupName:
Ref: ALIYUN::StackName
VpcId:
Ref: VpcId
SecurityGroupIngress:
- PortRange: 80/80
Priority: 1
SourceCidrIp: 0.0.0.0/0
IpProtocol: tcp
NicType: internet
InstanceGroup:
Type: ALIYUN::ECS::InstanceGroup
Properties:
# 付费类型
InstanceChargeType:
Ref: PayType
PeriodUnit:
Ref: PayPeriodUnit
Period:
Ref: PayPeriod
VpcId:
Ref: VpcId
VSwitchId:
Ref: VSwitchId
SecurityGroupId:
Ref: SecurityGroup
ZoneId:
Ref: ZoneId
ImageId: centos_7
Password:
Ref: InstancePassword
InstanceType:
Ref: EcsInstanceType
SystemDiskCategory: cloud_essd
SystemDiskSize: 200
InternetMaxBandwidthOut: 5
IoOptimized: optimized
MaxAmount: 1
WaitCondition:
Type: ALIYUN::ROS::WaitCondition
Properties:
Count: 1
Handle:
Ref: WaitConditionHandle
Timeout: 300
WaitConditionHandle:
Type: ALIYUN::ROS::WaitConditionHandle
InstallPackage:
Type: ALIYUN::ECS::RunCommand
Properties:
InstanceIds:
Fn::GetAtt:
- InstanceGroup
- InstanceIds
Type: RunShellScript
Sync: true
Timeout: 300
CommandContent:
Fn::Sub:
- |
yum-config-manager --add-repo http://mirrors.aliyun.com/docker-ce/linux/centos/docker-ce.repo
yum makecache fast
yum -y install docker-ce jq
systemctl enable docker
systemctl start docker
sleep 10
dockerJson='{{ computenest::acr::dockerconfigjson }}'
decodeDockerJson=$(echo $dockerJson | base64 -d)
host=$(echo $decodeDockerJson | jq '.auths' | jq 'keys' | jq .[0])
username=$(echo $decodeDockerJson | jq ".auths.$host.username" | tr -d '"')
password=$(echo $decodeDockerJson | jq ".auths.$host.password" | tr -d '"')
host=$(echo $host | tr -d '"')
docker login $host --username=$username --password=$password
mkdir -p /home/admin/application
cat >/home/admin/application/docker-compose.yaml<<EOF
# docker-compose.yaml中可以引用参数
# 单个nginx服务实例
services:
# 服务名
nginx:
# Docker镜像
image: {{computenest::acrimage::nginx}}
# 端口映射
ports:
- 80:80
volumes:
- /home/admin/application/nginx/logs:/var/log/nginx/
EOF
cat > /etc/systemd/system/docker-compose-app.service <<EOF
[Unit]
Description=Docker Compose Application Service
Requires=docker.service
After=docker.service
[Service]
Type=oneshot
RemainAfterExit=yes
WorkingDirectory=/home/admin/application
ExecStart=/usr/bin/docker compose up -d
ExecStop=/usr/bin/docker compose down
TimeoutStartSec=0
[Install]
WantedBy=multi-user.target
EOF
systemctl enable docker-compose-app
# 在Docker Compose启动前执行此命令片段,命令中可以引用参数
echo "before docker compose starts"
mkdir -p /home/admin/application/nginx/logs
systemctl start docker-compose-app
sleep 10
# 在Docker Compose启动后执行此命令片段,命令中可以引用参数
echo "after docker compose starts"
echo ${AdminPassword}
# 执行成功回调WaitCondition结束waitCondition的等待
${CurlCli} -d "{\"Data\" : \"Success\", \"status\" : \"SUCCESS\"}"
- CurlCli:
Fn::GetAtt:
- WaitConditionHandle
- CurlCli
Outputs:
Endpoint:
Value:
Fn::Sub:
- http://${Address}:80
- Address:
Fn::Select:
- 0
- Fn::GetAtt:
- InstanceGroup
- PublicIps
Metadata:
ALIYUN::ROS::Interface:
ParameterGroups:
- Parameters:
- PayType
- PayPeriodUnit
- PayPeriod
Label:
default: 付费类型配置
- Parameters:
- EcsInstanceType
- InstancePassword
Label:
default: 资源配置
- Parameters:
- AdminPassword
Label:
en: Software Configuration
zh-cn: 软件配置
- Parameters:
- ZoneId
- VpcId
- VSwitchId
Label:
default: 可用区配置