-
Notifications
You must be signed in to change notification settings - Fork 35
/
ecs-with-ruby-enviroment.yml
322 lines (315 loc) · 10 KB
/
ecs-with-ruby-enviroment.yml
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
ROSTemplateFormatVersion: '2015-09-01'
Description:
zh-cn: 创建CentOS-7 ECS实例,配置Ruby on Rails环境与本地MySQL数据库,部署简易应用。
en: Create a CentOS-7 ECS instance, configure the Ruby on Rails environment with
a local MySQL database, and deploy a simple application.
Parameters:
VpcName:
Type: String
Label: Vpc Name
Description: Name of VPC
ConstraintDescription: '[2, 128] English or Chinese characters'
Default: MyVpcName
MinLength: 2
MaxLength: 128
DBRootPassword:
Type: String
Label: DB Root Password
Description: Root password for MySQL
ConstraintDescription: must contain only alphanumeric characters.
AllowedPattern: '[a-zA-Z0-9]*'
MinLength: '1'
MaxLength: '41'
NoEcho: true
Confirm: true
ZoneId:
Type: String
Label: The ECS Available Zone ID
Description: The ECS Available Zone ID, Notice:Whether cloned ecs Type is supported
in the available zone.<a href='#/product/cn-beijing/list/zoneList' target='_blank'>View
zoneid info</a>
VSwitchCidrBlock:
Type: String
Label: VSwitch CidrBlock
Description: CIDR Block of created VSwitch, It must belong to itself VPC CIDR
block.
Default: 10.0.10.0/24
InstanceType:
Type: String
Label: Ecs Instance type
Description: WebServer ECS instance type, The ECS instance type, <a href='#/product/cn-beijing/list/typeList'
target='_blank'>View instance types</a>
ConstraintDescription: Must be a valid ECS instance type.
Default: ecs.c5.large
AllowedValues:
- ecs.c5.large
- ecs.c5.xlarge
- ecs.g5.large
- ecs.g5.xlarge
IoOptimized:
Type: String
Label: Io Optimized
Description: The 'optimized' instance can provide better IO performance. Support
'none' and 'optimized' only.
Default: optimized
AllowedValues:
- none
- optimized
DBPassword:
Type: String
Label: DB Password
Description: Password MySQL database access
ConstraintDescription: Must contain only alphanumeric characters.
AllowedPattern: '[a-zA-Z0-9]*'
MinLength: '1'
MaxLength: '41'
NoEcho: true
Confirm: true
VpcCidrBlock:
Type: String
Label: The VPC Cidrblock
Description: |-
The IP address range of the VPC in the CIDR block form. You can use the following IP address ranges and their subnets:
10.0.0.0/8
172.16.0.0/12 (Default)
192.168.0.0/16
Default: 10.0.0.0/8
AllowedValues:
- 192.168.0.0/16
- 172.16.0.0/12
- 10.0.0.0/8
InstancePassword:
Type: String
Label: Instance Password
Description: The login password of ECS instances
ConstraintDescription: Consist of 8 to 41 characters of alphanumeric characters
AllowedPattern: '[a-zA-Z][a-zA-Z0-9]*'
MinLength: '8'
MaxLength: '41'
NoEcho: true
Confirm: true
SystemDiskCategory:
Type: String
Label: System Disk Category
Description: 'System disk category: average cloud disk(cloud), efficient cloud
disk(cloud_efficiency) or SSD cloud disk(cloud_ssd)'
Default: cloud_ssd
AllowedValues:
- cloud
- cloud_efficiency
- cloud_ssd
DBName:
Type: String
Label: DB Name
Description: MySQL database name
ConstraintDescription: Must begin with a letter and contain only alphanumeric
characters.
Default: MyDatabase
AllowedPattern: '[a-zA-Z][a-zA-Z0-9]*'
MinLength: '1'
MaxLength: '64'
InstanceName:
Type: String
Label: Instance Name
Description: The name of ECS instance
Default: MyEcsInstance
DBUser:
Type: String
Label: DB Username
Description: Username for MySQL database access
ConstraintDescription: Must begin with a letter and contain only alphanumeric
characters.
AllowedPattern: '[a-zA-Z][a-zA-Z0-9]*'
MinLength: '1'
MaxLength: '16'
Resources:
Vpc:
Type: ALIYUN::ECS::VPC
Properties:
CidrBlock:
Ref: VpcCidrBlock
VpcName:
Ref: VpcName
VSwitch:
Type: ALIYUN::ECS::VSwitch
Properties:
ZoneId:
Ref: ZoneId
VpcId:
Fn::GetAtt:
- Vpc
- VpcId
CidrBlock:
Ref: VSwitchCidrBlock
WebServerSecurityGroup:
Type: ALIYUN::ECS::SecurityGroup
Properties:
VpcId:
Ref: Vpc
SecurityGroupIngress:
- SourceCidrIp: 0.0.0.0/0
IpProtocol: all
NicType: internet
PortRange: -1/-1
Priority: 1
SecurityGroupEgress:
- IpProtocol: all
DestCidrIp: 0.0.0.0/0
NicType: internet
PortRange: -1/-1
Priority: 1
WebServer:
Type: ALIYUN::ECS::Instance
Properties:
VpcId:
Fn::GetAtt:
- Vpc
- VpcId
VSwitchId:
Ref: VSwitch
SecurityGroupId:
Ref: WebServerSecurityGroup
ImageId: centos_7
IoOptimized:
Ref: IoOptimized
Password:
Ref: InstancePassword
SystemDiskCategory:
Ref: SystemDiskCategory
UserData:
Fn::Replace:
- ros-notify:
Fn::GetAtt:
- WebServerConditionHandle
- CurlCli
- Fn::Join:
- ''
- - '#!/bin/bash
'
- dbname=
- Ref: DBName
- '
'
- dbuser=
- Ref: DBUser
- '
'
- dbpassword=
- Ref: DBPassword
- '
'
- dbrootpassword=
- Ref: DBRootPassword
- " \n"
- 'export HOME=/root
'
- 'export PATH=$PATH:/usr/local/bin
'
- "curl -sSL https://get.rvm.io | bash \n"
- "source /etc/profile.d/rvm.sh \n"
- "rvm install 2.3.1 \n"
- "source /etc/profile.d/rvm.sh \n"
- "rvm --default use 2.3.1 \n"
- "gem update --system \n"
- "gem install rails \n"
- " \n"
- "# install mysql in centos-7 \n"
- "yum -y install mysql mysql-server mysql-devel mysql-libs \n"
- "yum -y install mariadb-server mariadb-client \n"
- "systemctl start mariadb.service \n"
- "systemctl enable mariadb.service \n"
- " \n"
- "# set mysql root password \n"
- "mysqladmin -u root password \"$dbrootpassword\" \n"
- "$(mysql $dbname -u root --password=\"$dbrootpassword\" >/dev/null 2>&1\
\ </dev/null); (( $? != 0 )) \n"
- " \n"
- "# create database \n"
- "touch /tmp/setup.mysql \n"
- "echo CREATE USER \"$dbuser\"@\"localhost\" IDENTIFIED BY \"'$dbpassword'\"\
\\; >> /tmp/setup.mysql \n"
- "echo GRANT ALL ON $dbname.* TO \"$dbuser\"@\"localhost\"\\; >> /tmp/setup.mysql\
\ \n"
- "echo FLUSH PRIVILEGES\\; >> /tmp/setup.mysql \n"
- "chmod 400 /tmp/setup.mysql \n"
- " \n"
- "mysql -u root --password=\"$dbrootpassword\" < /tmp/setup.mysql \n"
- "$(mysql $dbname -u root --password=\"$dbrootpassword\" >/dev/null 2>&1\
\ </dev/null); (( $? != 0 )) \n"
- " \n"
- "# install application \n"
- "touch /tmp/database.yml \n"
- "echo development: >> /tmp/database.yml \n"
- "echo \" adapter: mysql2\" >> /tmp/database.yml \n"
- "echo \" encoding: utf8\" >> /tmp/database.yml \n"
- "echo \" reconnect: false\" >> /tmp/database.yml \n"
- "echo \" pool: 5\" >> /tmp/database.yml \n"
- "echo \" database: $dbname\" >> /tmp/database.yml \n"
- "echo \" username: $dbuser\" >> /tmp/database.yml \n"
- "echo \" password: $dbpassword\" >> /tmp/database.yml \n"
- "echo \" socket: /var/lib/mysql/mysql.sock\" >> /tmp/database.yml\
\ \n"
- "chmod 400 /tmp/database.yml \n"
- " \n"
- "source /etc/profile.d/rvm.sh \n"
- "rvm use 2.3.1 \n"
- "/usr/local/rvm/scripts/rvm use 2.3.1 \n"
- "cd /root \n"
- " \n"
- "# Kill the rails server if it is running to allow update \n"
- "if pgrep ruby &> /dev/null ; then pkill -TERM ruby ; fi \n"
- " \n"
- "# Create a new application, with therubyracer javascript library \n"
- "rails new sample -d mysql --skip-spring --skip-bundle --force \n"
- "cd /root/sample \n"
- "sed -i 's/^# \\(.*therubyracer.*$\\)/\\1/' Gemfile \n"
- "bundle install \n"
- " \n"
- "yum -y install nodejs \n"
- "rails generate scaffold Note title:string body:text --force \n"
- " \n"
- "cd config \n"
- "mv database.yml database.yml.bak \n"
- "cd .. \n"
- "mv /tmp/database.yml config \n"
- "rake db:create db:migrate \n"
- " \n"
- "# Startup the application \n"
- "source /etc/profile.d/rvm.sh \n"
- "rvm use 2.3.1 \n"
- "/usr/local/rvm/scripts/rvm use 2.3.1 \n"
- " \n"
- "cd /root/sample \n"
- " \n"
- "rails server --binding 0.0.0.0 -p 80 -d \n"
- " \n"
- "# cleanup \n"
- "# rm -f /tmp/setup.mysql \n"
- 'ros-notify -d ''{"data" : "Install Ruby on rails."}''
'
InstanceName:
Ref: InstanceName
InstanceType:
Ref: InstanceType
WebServerConditionHandle:
Type: ALIYUN::ROS::WaitConditionHandle
Properties: {}
WebServerGroupWaitCondition:
Type: ALIYUN::ROS::WaitCondition
Properties:
Handle:
Ref: WebServerConditionHandle
Timeout: 3600
Count: 1
DependsOn: WebServer
Outputs:
WebsiteURL:
Description: URL for newly created Ruby on Rails application
Value:
Fn::Join:
- ''
- - http://
- Fn::GetAtt:
- WebServer
- PublicIp
- /notes