-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathcommon.py
More file actions
166 lines (142 loc) · 5.22 KB
/
Copy pathcommon.py
File metadata and controls
166 lines (142 loc) · 5.22 KB
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
import os
import sys
import json
import re
import copy
from jinja2 import Template
#
# This file contains common functions for generating AWS CloudFormation templates
#
# http://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/mappings-section-structure.html
MAPPINGS = {
# TODO: we should build our own AMIs in all cases
# amzn-ami-minimal-hvm-2015.03.rc-1.x86_64-s3
# https://aws.amazon.com/amazon-linux-ami/
"MinimalAmazonLinuxAMI": {
"us-east-1": {"instance": "ami-dc6aa9b1", "ebs": "ami-6869aa05"}
}
}
def get_file_tokens(file_name, service_fullname=None):
""" Returns a local file as a series of tokens and CloudFormation variables/functions """
with open(file_name, "r") as f:
cnt = f.read()
if service_fullname:
rendered = Template(cnt).render(service_fullname=service_fullname)
tokens = rendered.split("|||")
tokens = [json.loads(token) if re.match(r"^\{.*\}$", token) else token for token in tokens]
return tokens
else:
return [cnt]
def get_file(*args):
""" Returns the CloudFormation-compatible content of a local file interpreted as a jinja2 template """
return {"Fn::Join": [
"",
get_file_tokens(*args)
]}
def get_launch_configuration(service_name, service_fullname, storage_type, commands=None, packages=None, files=None, services=None):
""" Returns the LaunchConfiguration section of a CloudFormation template """
commands = copy.deepcopy(commands or {})
packages = copy.deepcopy(packages or {})
files = copy.deepcopy(files or {})
services = copy.deepcopy(services or {})
files["/etc/cfn/cfn-hup.conf"] = {
"content": {"Fn::Join": ["", [
"[main]\n",
"stack=", {"Ref": "AWS::StackId"}, "\n",
"region=", {"Ref": "AWS::Region"}, "\n"
"interval=1\n"
]]},
"mode": "000400",
"owner": "root",
"group": "root"
}
files["/etc/cfn/hooks.d/cfn-auto-reloader.conf"] = {
"content": {"Fn::Join": ["", [
"[cfn-auto-reloader-hook]\n",
"triggers=post.update\n",
"path=Resources.%sLaunchConfiguration.Metadata.AWS::CloudFormation::Init\n" % service_fullname,
"action=/opt/aws/bin/cfn-init -v ",
" --stack ", {"Ref": "AWS::StackName"},
" --resource %sLaunchConfiguration " % service_fullname,
" --region ", {"Ref": "AWS::Region"}, "\n",
"runas=root\n"
]]}
}
services["cfn-hup"] = {
"enabled": "true",
"ensureRunning": "true",
"files": [
"/etc/cfn/cfn-hup.conf",
"/etc/cfn/hooks.d/cfn-auto-reloader.conf"
]
}
# Parse templates to file contents
for f in files.itervalues():
if f.get("_template"):
f["content"] = get_file(f["_template"], service_fullname)
del f["_template"]
launchconfiguration = {
"Type": "AWS::AutoScaling::LaunchConfiguration",
"Metadata": {
"AWS::CloudFormation::Init": {
"config": {
"commands": commands,
"packages": packages,
"files": files,
"services": {
"sysvinit": services
}
}
}
},
"Properties": {
"ImageId": {"Fn::FindInMap": ["MinimalAmazonLinuxAMI", {"Ref": "AWS::Region"}, storage_type]},
"InstanceType": {"Ref": "%sInstanceType" % service_fullname},
"AssociatePublicIpAddress": "true", # TODO
"IamInstanceProfile": {"Ref": "EC2InstanceProfile"},
"SecurityGroups": [{"Ref": "EC2InternalSecurityGroup"}, {"Ref": "%sSecurityGroup" % service_name}],
"KeyName": {"Ref": "KeyName"},
"UserData": {"Fn::Base64": get_file("aws/%s/ec2-bootstrap.sh" % service_name.lower(), service_fullname)}
}
}
# TODO: add other groups for spot instances
# if service_fullname == "ElasticsearchData":
# launchconfiguration["Properties"]["SpotPrice"] = {"Ref": "ElasticsearchDataSpotBid"}
return launchconfiguration
def get_auto_scaling_group(service_name, service_fullname):
""" Returns the AutoScalingGroup section of a CloudFormation template """
asg = {
"Type": "AWS::AutoScaling::AutoScalingGroup",
"Properties": {
"VPCZoneIdentifier": {"Ref": "SubnetID"},
"LaunchConfigurationName": {"Ref": "%sLaunchConfiguration" % service_fullname},
"MinSize": {"Ref": "%sMinSize" % service_fullname},
"MaxSize": {"Ref": "%sMaxSize" % service_fullname},
"Cooldown": "60",
"DesiredCapacity": {"Ref": "%sMinSize" % service_fullname},
"Tags": [
{"Key": "Name", "Value": service_fullname, "PropagateAtLaunch": "true"},
{"Key": "service_name", "Value": service_name.lower(), "PropagateAtLaunch": "true"},
{"Key": "service_fullname", "Value": service_fullname.lower(), "PropagateAtLaunch": "true"}
]
},
"CreationPolicy": {
"ResourceSignal": {
"Timeout": "PT15M"
}
},
"UpdatePolicy": {
"AutoScalingRollingUpdate": {
"MinInstancesInService": "1",
"MaxBatchSize": "1",
"PauseTime": "PT15M",
"WaitOnResourceSignals": "true"
}
}
}
if service_fullname == "Frontend":
asg["Properties"]["LoadBalancerNames"] = [{"Ref": "FrontendLoadBalancer"}]
# TODO "HealthCheckType": "ELB", ?
elif service_fullname == "ElasticsearchLb":
asg["Properties"]["LoadBalancerNames"] = [{"Ref": "ElasticsearchLoadBalancer"}]
return asg