This repository has been archived by the owner on Mar 22, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathSingleWEBrick.json
243 lines (243 loc) · 11 KB
/
SingleWEBrick.json
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
{
"AWSTemplateFormatVersion": "2010-09-09",
"Outputs": {
"SSH": {
"Description": "AppServer SSH connect string",
"Value": {
"Fn::Join": [
"",
[
"ssh -i ",
{
"Ref": "TeamName"
},
".pem ec2-user@",
{
"Fn::GetAtt": [
"AppServer",
"PublicDnsName"
]
}
]
]
}
},
"URL": {
"Description": "The URL to the rails application.",
"Value": {
"Fn::Join": [
"",
[
"http://",
{
"Fn::GetAtt": [
"AppServer",
"PublicDnsName"
]
}
]
]
}
}
},
"Parameters": {
"AppInstanceType": {
"AllowedValues": [
"t1.micro",
"m1.small",
"m1.medium",
"m1.large",
"m1.xlarge",
"m2.xlarge",
"m2.2xlarge",
"m2.4xlarge",
"m3.medium",
"m3.large",
"m3.xlarge",
"m3.2xlarge",
"c1.medium",
"c1.xlarge",
"c3.large",
"c3.xlarge",
"c3.2xlarge",
"c3.4xlarge"
],
"Default": "t1.micro",
"Description": "The AppServer instance type.",
"Type": "String"
},
"Branch": {
"Default": "master",
"Description": "The git branch to deploy.",
"Type": "String"
},
"TeamName": {
"AllowedValues": [
"demo",
"victorious-Secret",
"ScalableMaster",
"Atticus",
"Questing-Adventurer",
"yam",
"AirBooks",
"TEAM",
"GiftHub",
"admin-InstanceSecurityGroup-1E5XQRZ0OCH97",
"MapKeep",
"Fantastic4",
"Gattlestar-Balactica",
"RubyCoders",
"Michelangelo",
"Arpeggio",
"Zijun",
"whatsup",
"demo5",
"Quotopia",
"Team1024",
"Newbie",
"U1F44D",
"Yeap"
],
"ConstraintDescription": "Must exactly match your team name as shown in your Github URL.",
"Description": "Your team name.",
"Type": "String"
}
},
"Resources": {
"AppServer": {
"CreationPolicy": {
"ResourceSignal": {
"Timeout": "PT20M"
}
},
"Metadata": {
"AWS::CloudFormation::Init": {
"app": {
"services": {
"sysvinit": {
"mysqld": {
"enabled": true,
"ensureRunning": true
}
}
},
"sources": {
"/home/ec2-user/app": {
"Fn::Join": [
"",
[
"https://github.com/scalableinternetservices/",
{
"Ref": "TeamName"
},
"/tarball/",
{
"Ref": "Branch"
}
]
]
}
}
},
"configSets": {
"default": [
"packages",
"app",
"perms",
"user"
]
},
"packages": {
"packages": {
"yum": {
"gcc-c++": [],
"git": [],
"make": [],
"mysql-devel": [],
"mysql-server": [],
"ruby21-devel": []
}
}
},
"perms": {
"commands": {
"update_permissions": {
"command": "chown -R ec2-user:ec2-user .",
"cwd": "/home/ec2-user/"
}
}
},
"user": {
"files": {
"/home/ec2-user/app/config/database.yml": {
"content": "production:\n adapter: mysql2\n database: rails_app\n",
"group": "ec2-user",
"owner": "ec2-user"
}
}
}
}
},
"Properties": {
"IamInstanceProfile": {
"Ref": "TeamName"
},
"ImageId": "ami-55a7ea65",
"InstanceType": {
"Ref": "AppInstanceType"
},
"KeyName": {
"Ref": "TeamName"
},
"SecurityGroups": [
{
"Ref": "TeamName"
}
],
"UserData": {
"Fn::Base64": {
"Fn::Join": [
"",
[
"#!/bin/bash -v\nyum update -y aws-cfn-bootstrap\nyum update\n# Helper function\nfunction error_exit {",
"\n /opt/aws/bin/cfn-signal -e 1 -r \"$1\" --stack ",
{
"Ref": "AWS::StackName"
},
" --resource AppServer --region ",
{
"Ref": "AWS::Region"
},
"\n exit 1\n}",
"\n# Run cfn-init (see AWS::CloudFormation::Init)\n/opt/aws/bin/cfn-init -s ",
{
"Ref": "AWS::StackName"
},
" -r AppServer --region ",
{
"Ref": "AWS::Region"
},
" || error_exit 'Failed to run cfn-init'\n# Don't require tty to run sudo\nsed -i 's/ requiretty/ !requiretty/' /etc/sudoers\nfunction user_sudo {",
"\n sudo -u ec2-user bash -lc \"$*\"\n}",
"\n",
"# Update alternatives\nalternatives --set ruby /usr/bin/ruby2.1 || error_exit 'Failed ruby2.1 default'\n# Install bundler only after the alternatives have been set.\ngem install bundle || error_exit 'Failed to install bundle'\n# Update user's path if it hasn't been set already\necho \"export PATH=/usr/local/bin:\\$PATH\" >> /home/ec2-user/.bashrc\n",
"# Change to the app directory\ncd /home/ec2-user/app\n# Add environment variables to ec2-user's .bashrc\nexport RAILS_ENV=production\necho \"export RAILS_ENV=production\" >> ../.bashrc\necho \"export SECRET_KEY_BASE=b801783afb83bb8e614b32ccf6c05c855a927116d92062a75c6ffa61d58c58e62f13eb60cf1a31922c44b7e6a3e8f1809934a93llask938bl\" >> ../.bashrc\n\n# Redirect port 80 to port 3000 (ec2-user cannot bind port 80)\niptables -t nat -A PREROUTING -p tcp --dport 80 -j REDIRECT --to-port 3000\n\n# Run the app specific ec2 initialization\nif [ -f .ec2_initialize ]; then\n sudo -u ec2-user bash -l .ec2_initialize || error_exit 'Failed to run .ec2_initialize'\nfi\n\n# Add gems needed on production\necho -e \"\ngem 'therubyracer', platforms: :ruby \" >> Gemfile\necho -e \"\ngem 'mysql2', platforms: :ruby \" >> Gemfile\necho -e \"\ngem 'therubyrhino', platforms: :jruby \" >> Gemfile\necho -e \"\ngem 'activerecord-jdbc-adapter', platforms: :jruby \" >> Gemfile\n\n# Run the remaining commands as the ec2-user in the app directory\nuser_sudo bundle install --without test development || error_exit 'Failed to install bundle'\n# Create the database and run the migrations (try up to 10x)\nloop=10\nwhile [ $loop -gt 0 ]; do\n user_sudo rake db:create db:migrate\n if [ $? -eq 0 ]; then\n loop=-1\n else\n sleep 6\n loop=$(expr $loop - 1)\n fi\ndone\nif [ $loop -eq 0 ]; then\n error_exit 'Failed to execute database migration'\nfi\n# Run the app specific ec2 initialization\nif [ -f .rails_initialize ]; then\n sudo -u ec2-user bash -l .rails_initialize || error_exit 'Failed to run .rails_initialize'\nfi\n# Fix multi_json gem version (>1.7.8 has issues precompiling assets)\necho -e \"\ngem 'multi_json', '1.7.8'\" >> Gemfile\nuser_sudo bundle update multi_json || error_exit 'Failed to update multi_json'\n# Generate static assets\nuser_sudo rake assets:precompile || error_exit 'Failed to precompile static assets'\n",
"# Configure the app to serve static assets\n# Start up WEBrick (or whatever server is installed)\nuser_sudo RAILS_SERVE_STATIC_FILES=true rails server -d -b 0.0.0.0 || error_exit 'Failed to start rails server'\n",
"# All is well so signal success\n/opt/aws/bin/cfn-signal -e 0 --stack ",
{
"Ref": "AWS::StackName"
},
" --resource AppServer --region ",
{
"Ref": "AWS::Region"
},
"\n"
]
]
}
}
},
"Type": "AWS::EC2::Instance"
}
}
}