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: Virtual Private Cloud執行個體ID
Description:
en: >-
Please search the ID starting with (vpc-xxx) from console-Virtual
Private Cloud
zh-cn: 現有Virtual Private Cloud絡的執行個體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: 可用性區域配置