-
Notifications
You must be signed in to change notification settings - Fork 2
/
wp-centos-autoscale.yaml
311 lines (287 loc) · 11.1 KB
/
wp-centos-autoscale.yaml
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
heat_template_version: 2015-04-30
description: AutoScaling Wordpress
parameters:
image:
type: string
description: Image used for servers
default: CentOS-7-x86_64
key:
type: string
description: SSH key to connect to the servers
default: cloudkey1
flavor:
type: string
description: flavor used by the web servers
constraints:
- allowed_values: [m1.small, m1.medium]
database_flavor:
type: string
description: flavor used by the db server
constraints:
- allowed_values: [m1.small, m1.medium]
network:
type: string
description: Network used by the server
default: private_network
subnet_id:
type: string
description: subnet on which the load balancer will be located
default: private_subnet
database_name:
type: string
description: Name of the wordpress DB
default: wordpress
database_user:
type: string
description: Name of the wordpress user
default: wordpress
public_network:
type: string
description: UUID of a Neutron external network
default: public_network
resources:
database_password:
type: OS::Heat::RandomString
database_root_password:
type: OS::Heat::RandomString
db:
type: OS::Nova::Server
properties:
security_groups:
- db_security_group
flavor: {get_param: database_flavor}
image: {get_param: image}
key_name: {get_param: key}
networks: [{network: {get_param: network} }]
user_data_format: RAW
user_data:
str_replace:
template: |
#!/bin/bash -v
setenforce 0
yum -y install mariadb mariadb-server
systemctl enable mariadb.service
systemctl start mariadb.service
firewall-cmd --permanent --add-port=3306/tcp
firewall-cmd --reload
mysqladmin -u root password $db_rootpassword
cat << EOF | mysql -u root --password=$db_rootpassword
CREATE DATABASE $db_name;
GRANT ALL PRIVILEGES ON $db_name.* TO "$db_user"@"%"
IDENTIFIED BY "$db_password";
FLUSH PRIVILEGES;
EXIT
EOF
params:
$db_rootpassword: {get_attr: [database_root_password, value]}
$db_name: {get_param: database_name}
$db_user: {get_param: database_user}
$db_password: {get_attr: [database_password, value]}
asg:
type: OS::Heat::AutoScalingGroup
properties:
min_size: 1
max_size: 5
resource:
type: https://raw.githubusercontent.com/thaiopen/privatecloud/master/lb-wpenv.yaml
properties:
flavor: {get_param: flavor}
image: {get_param: image}
key_name: {get_param: key}
network: {get_param: network}
pool_id: {get_resource: pool}
metadata: {"metering.stack": {get_param: "OS::stack_id"}}
user_data:
str_replace:
template: |
#!/bin/bash -v
setenforce 0
setsebool -P httpd_can_network_connect_db=1
yum -y install php php-gd php-mysql httpd wget tmux vim unzip
systemctl enable httpd.service
systemctl start httpd.service
# download wordpress
cd /root
wget http://wordpress.org/latest.tar.gz
rm /var/www/html/index.html
tar -xzf latest.tar.gz
cp wordpress/wp-config-sample.php wordpress/wp-config.php
sed -i 's/database_name_here/$db_name/' wordpress/wp-config.php
sed -i 's/username_here/$db_user/' wordpress/wp-config.php
sed -i 's/password_here/$db_password/' wordpress/wp-config.php
sed -i 's/localhost/$db_host/' wordpress/wp-config.php
cp -avR wordpress/* /var/www/html/
mkdir /var/www/html/wp-content/uploads
firewall-cmd --permanent --add-port=80/tcp
firewall-cmd --reload
yum -y install epel-release
yum -y install nodejs npm vim tmux git
yum -y install golang
mkdir /root/gopath
export GOPATH="/root/gopath/"
go get github.com/ncw/rclone
sleep 5
cp /root/gopath/bin/rclone /usr/bin
npm install fsmonitor
npm install -g fsmonitor
npm install forever
npm install -g forever
cat << RCLONE > /root/.rclone.conf
[remote]
type = swift
user = demo
key = isyl]y[openstack
auth = http://103.27.200.234:5000/v2.0
tenant = demo
region = RegionOne
RCLONE
rclone mkdir remote:uploads
rclone mkdir remote:themes
rclone sync remote:uploads /var/www/html/wp-content/uploads
rclone sync remote:themes /var/www/html/wp-content/themes
cat << BACKUP > backup.js
var sys = require('sys');
var exec = require('child_process').exec;
function reports(error, stdout, stderr) {sys.puts(stdout)};
fsmonitor = require('fsmonitor');
fsmonitor.watch('/var/www/html/wp-content/uploads', null, function(change) {
console.log("Change detected:\n" + change);
console.log("Added files: %j", change.addedFiles);
console.log("Modified files: %j", change.modifiedFiles);
console.log("Removed files: %j", change.removedFiles);
console.log("Added folders: %j", change.addedFolders);
console.log("Modified folders: %j", change.modifiedFolders);
console.log("Removed folders: %j", change.removedFolders);
exec('rclone sync /var/www/html/wp-content/uploads remote:uploads',reports)
});
fsmonitor2 = require('fsmonitor');
fsmonitor2.watch('/var/www/html/wp-content/themes', null, function(change) {
console.log("Change detected:\n" + change);
console.log("Added files: %j", change.addedFiles);
console.log("Modified files: %j", change.modifiedFiles);
console.log("Removed files: %j", change.removedFiles);
console.log("Added folders: %j", change.addedFolders);
console.log("Modified folders: %j", change.modifiedFolders);
console.log("Removed folders: %j", change.removedFolders);
exec('rclone sync /var/www/html/wp-content/themes remote:themes', reports)
});
BACKUP
forever start backup.js
params:
$db_name: {get_param: database_name}
$db_user: {get_param: database_user}
$db_password: {get_attr: [database_password, value]}
$db_host: {get_attr: [db, first_address]}
web_server_scaleup_policy:
type: OS::Heat::ScalingPolicy
properties:
adjustment_type: change_in_capacity
auto_scaling_group_id: {get_resource: asg}
cooldown: 60
scaling_adjustment: 1
web_server_scaledown_policy:
type: OS::Heat::ScalingPolicy
properties:
adjustment_type: change_in_capacity
auto_scaling_group_id: {get_resource: asg}
cooldown: 60
scaling_adjustment: -1
cpu_alarm_high:
type: OS::Ceilometer::Alarm
properties:
description: Scale-up if the average CPU > 50% for 1 minute
meter_name: cpu_util
statistic: avg
period: 60
evaluation_periods: 1
threshold: 50
alarm_actions:
- {get_attr: [web_server_scaleup_policy, alarm_url]}
matching_metadata: {'metadata.user_metadata.stack': {get_param: "OS::stack_id"}}
comparison_operator: gt
cpu_alarm_low:
type: OS::Ceilometer::Alarm
properties:
description: Scale-down if the average CPU < 15% for 10 minutes
meter_name: cpu_util
statistic: avg
period: 60
evaluation_periods: 1
threshold: 15
alarm_actions:
- {get_attr: [web_server_scaledown_policy, alarm_url]}
matching_metadata: {'metadata.user_metadata.stack': {get_param: "OS::stack_id"}}
comparison_operator: lt
monitor:
type: OS::Neutron::HealthMonitor
properties:
type: TCP
delay: 5
max_retries: 5
timeout: 5
pool:
type: OS::Neutron::Pool
properties:
protocol: HTTP
monitors: [{get_resource: monitor}]
subnet_id: {get_param: subnet_id}
lb_method: ROUND_ROBIN
vip:
protocol_port: 80
lb:
type: OS::Neutron::LoadBalancer
properties:
protocol_port: 80
pool_id: {get_resource: pool}
# assign a floating ip address to the load balancer
# pool.
lb_floating:
type: OS::Neutron::FloatingIP
properties:
floating_network_id: {get_param: public_network}
port_id: {get_attr: [pool, vip, port_id]}
outputs:
scale_up_url:
description: >
This URL is the webhook to scale up the autoscaling group. You
can invoke the scale-up operation by doing an HTTP POST to this
URL; no body nor extra headers are needed.
value: {get_attr: [web_server_scaleup_policy, alarm_url]}
scale_dn_url:
description: >
This URL is the webhook to scale down the autoscaling group.
You can invoke the scale-down operation by doing an HTTP POST to
this URL; no body nor extra headers are needed.
value: {get_attr: [web_server_scaledown_policy, alarm_url]}
pool_ip_address:
value: {get_attr: [pool, vip, address]}
description: The IP address of the load balancing pool
website_url:
value:
str_replace:
template: http://host/
params:
host: { get_attr: [lb_floating, floating_ip_address] }
description: >
This URL is the "external" URL that can be used to access the
Wordpress site.
ceilometer_query:
value:
str_replace:
template: >
ceilometer statistics -m cpu_util
-q metadata.user_metadata.stack=stackval -p 60 -a avg
params:
stackval: { get_param: "OS::stack_id" }
description: >
This is a Ceilometer query for statistics on the cpu_util meter
Samples about OS::Nova::Server instances in this stack. The -q
parameter selects Samples according to the subject's metadata.
When a VM's metadata includes an item of the form metering.X=Y,
the corresponding Ceilometer resource has a metadata item of the
form user_metadata.X=Y and samples about resources so tagged can
be queried with a Ceilometer query term of the form
metadata.user_metadata.X=Y. In this case the nested stacks give
their VMs metadata that is passed as a nested stack parameter,
and this stack passes a metadata of the form metering.stack=Y,
where Y is this stack's ID.