forked from NeCTAR-RC/heat-templates
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwordpress_with_ha.yaml
295 lines (269 loc) · 10.9 KB
/
wordpress_with_ha.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
# http://docs.openstack.org/developer/heat/template_guide/hot_spec.html#heat-template-version
heat_template_version: 2014-10-16
description: >
This template creates a sigle instance running wordpress, and a monitor that will kill and start a new instance if
it finds that the running instance is not being responsive enough. Note that all resources that are dependent
on the instance are also killed and rebuilt!
Note that it uses a deprecated resource type (HARestarter).
parameters:
# First off, the parameters that affect the environment
key_name:
description: Name of an existing KeyPair to enable SSH access to the instances
type: string
instance_type:
description: The NeCTAR flavour the webserver is to run on
type: string
default: m2.xsmall
constraints:
- allowed_values: [m2.xsmall, m2.small, m1.small]
description:
Must be a valid NeCTAR flavour with 10G drive, limited to the smaller ones available
image_id:
description: A NeCTAR fedora image
type: string
default: d3bae233-a0cd-4533-a710-d7aa9de0a4b7
# Then the installation specific parameters
db_name:
default: wordpress
description: The WordPress database name
type: string
constraints:
- length: { min: 6, max: 16}
description: The database name must be between 6 to 16 characters in length.
- allowed_pattern: '[a-zA-Z][a-zA-Z0-9]*'
description: Must begin with a letter and contain only alphanumeric characters.
db_username:
description: The WordPress database admin account username
default: bestAdminEver
type: string
constraints:
- length: { min: 6, max: 16}
description: The user name must be between 6 to 16 characters in length.
- allowed_pattern: '[a-zA-Z][a-zA-Z0-9]*'
description: Must begin with a letter and contain only alphanumeric characters.
hup_poll_interval:
default: '1'
type: string
description: Interval for cfn-hup
resources:
# http://docs.openstack.org/developer/heat/template_guide/openstack.html#OS::Heat::RandomString
db_password:
type: OS::Heat::RandomString
# http://docs.openstack.org/developer/heat/template_guide/openstack.html#OS::Heat::RandomString
db_root_password:
type: OS::Heat::RandomString
# http://docs.openstack.org/developer/heat/template_guide/cfn.html#AWS::IAM::User
cfn_user:
type: AWS::IAM::User
properties:
Policies: [ { get_resource: access_policy }]
# http://docs.openstack.org/developer/heat/template_guide/cfn.html#AWS::IAM::AccessKey
web_server_access_key:
type: AWS::IAM::AccessKey
properties:
UserName: { get_resource: cfn_user }
# Deprecated!
# http://docs.openstack.org/hot-reference/content/OS__Heat__HARestarter.html
web_server_restart_policy:
type: OS::Heat::HARestarter
properties:
InstanceId: { get_resource: wordpress_site }
cpu_alarm_high:
type: OS::Ceilometer::Alarm
properties:
description: Restart the vm if the cpu_utilization is over 90 for 6 minutes.
meter_name: cpu_util
statistic: avg
period: 330
evaluation_periods: 1
threshold: 90
alarm_actions:
- { get_attr: [web_server_restart_policy, AlarmUrl] }
comparison_operator: gt
http_failure_alarm:
# http://docs.openstack.org/developer/heat/template_guide/cfn.html#AWS::CloudWatch::Alarm
type: AWS::CloudWatch::Alarm
properties:
AlarmDescription: Restart the wordpress_site if httpd fails > 3 times in 5 minutes
MetricName: ServiceFailure
# ServiceFailure
# MemoryUtilization
# MemoryUsed
# MemoryAvailable
# Heartbeat
# SwapUtilization
# SwapUsed
# DiskSpaceUtilization
# DiskSpaceUsed
# DiskSpaceAvailable
# CPUUtilization
Statistic: SampleCount
Period: 300
EvaluationPeriods: 1
Threshold: 2
AlarmActions:
- { get_attr: [web_server_restart_policy, AlarmUrl] }
ComparisonOperator: GreaterThanThreshold
wordpress_site:
# http://docs.openstack.org/developer/heat/template_guide/cfn.html# AWS::EC2::Instance
type: AWS::EC2::Instance
metadata:
# See http://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/aws-resource-init.html
# This will be placed as JSON at /var/lib/cloud/data/cfn-init-data on the instance
# heat-cfntools will then unpack and act on it.
# For more see: https://wiki.openstack.org/wiki/Heat/ApplicationDeployment
AWS::CloudFormation::Init:
config:
files:
# required to communicate with OpenStack
/etc/cfn/cfn-credentials:
content:
str_replace:
template: |
AWSAccessKeyId=WebAccessKey
AWSSecretKey=WebSecretKey
params:
WebAccessKey: { get_resource: web_server_access_key }
WebSecretKey: { get_attr: [ web_server_access_key, SecretAccessKey ] }
mode: '000400'
owner: root
group: root
# configuration for the process that will install and monitor services, and call the hooks.
/etc/cfn/cfn-hup.conf:
content:
str_replace:
template: |
[main]
stack=StackName
credential-file=/etc/cfn/cfn-credentials
region=ap-southeast-1
interval=Interval
params:
StackName: { get_param: 'OS::stack_name' }
Interval: { get_param: hup_poll_interval }
mode: '000400'
owner: root
group: root
# The hook that will report a service failure
/etc/cfn/notify-on-httpd-restarted:
content: |
#!/bin/sh
/opt/aws/bin/cfn-push-stats --service-failure
mode: '000700'
owner: root
group: root
# call the process at a regular interval (for monitoring and possible hook calling)
/tmp/cfn-hup-crontab.txt:
content: |
MAIL=""
* * * * * /opt/aws/bin/cfn-hup -f
mode: '000600'
owner: root
group: root
# a file required to configure mysql
/tmp/setup.mysql:
content:
str_replace:
template: |
CREATE DATABASE db_name;
GRANT ALL PRIVILEGES ON db_name .* TO 'db_username'@'localhost' IDENTIFIED BY 'db_password';
FLUSH PRIVILEGES;
EXIT
params:
db_name: { get_param: db_name }
db_password: { get_resource: db_password }
db_username: { get_param: db_username }
mode: '000644'
owner: root
group: root
# The hook to call if the any of the services are restarted.
/etc/cfn/hooks.conf:
content: |
[cfn-http-restarted]
triggers=service.restarted
path=Resources.wordpress_site.Metadata
action=/etc/cfn/notify-on-httpd-restarted
runas=root
mode: '000400'
owner: root
group: root
# the following packages will be installed
packages:
yum:
cronie: []
mariadb: []
mariadb-server: []
httpd: []
wordpress: []
# and the following services will be run
services:
systemd:
mysqld: { enabled: true, ensureRunning: true }
httpd: { enabled: true, ensureRunning: true }
crond: { enabled: true, ensureRunning: true }
properties:
ImageId: { get_param: image_id }
InstanceType: { get_param: instance_type }
KeyName: { get_param: key_name }
UserData:
str_replace:
template: |
#!/bin/bash -v
# Helper function
function error_exit
{
/opt/aws/bin/cfn-signal -e 1 -r "$1" 'wait_handle'
exit 1
}
echo "stack_name wordpress_site stack_region" > /root/args.txt
/opt/aws/bin/cfn-init -s stack_name -r wordpress_site --access-key access_key --secret-key secret_key --region stack_region || error_exit 'Failed to run cfn-init'
# Setup database root password and create a user
mysqladmin -u root password db_root_password || error_exit 'Failed to initialize root password'
mysql -u root --password=db_root_password < /tmp/setup.mysql || error_exit 'Failed to create database.'
sed -i "/Deny from All/d" /etc/httpd/conf.d/wordpress.conf
sed -i "s/Require local/Require all granted/" /etc/httpd/conf.d/wordpress.conf
sed -i s/database_name_here/db_name/ /etc/wordpress/wp-config.php
sed -i s/username_here/db_user/ /etc/wordpress/wp-config.php
sed -i s/password_here/db_password/ /etc/wordpress/wp-config.php
systemctl restart httpd.service
# install cfn-hup crontab
crontab /tmp/cfn-hup-crontab.txt
/opt/aws/bin/cfn-signal -e 0 -r "Wiki server setup complete" 'wait_handle'
params:
stack_name: { get_param: 'OS::stack_name' }
stack_region: ap-southeast-1
access_key: { get_resource: web_server_access_key }
secret_key: { get_attr: [web_server_access_key, SecretAccessKey] }
wait_handle: { get_resource: wait_handle }
db_root_password: { get_resource: db_root_password }
db_password: { get_resource: db_password }
db_user: { get_param: db_username }
db_name: { get_param: db_name }
access_policy:
# http://docs.openstack.org/developer/heat/template_guide/openstack.html#OS::Heat::AccessPolicy
type: OS::Heat::AccessPolicy
properties:
AllowedResources: [ "wordpress_site" ]
# http://docs.openstack.org/developer/heat/template_guide/cfn.html#AWS::CloudFormation::WaitConditionHandle
wait_handle:
type: 'AWS::CloudFormation::WaitConditionHandle'
# http://docs.openstack.org/developer/heat/template_guide/cfn.html#AWS::CloudFormation::WaitCondition
wait_condition:
type: AWS::CloudFormation::WaitCondition
depends_on: wordpress_site
properties:
Handle: { get_resource: wait_handle }
Count: 1
# Give it 11 minutes: there's a lot going on...
Timeout: 700
outputs:
restart_url:
description: The Restart URL
value: { get_attr: [web_server_restart_policy, AlarmUrl] }
url:
description: The URL of the website
value:
str_replace:
template: http://ip_address/wordpress
params:
ip_address: { get_attr: [wordpress_site, PublicIp] }