-
Notifications
You must be signed in to change notification settings - Fork 41
/
fabfile.py
385 lines (318 loc) · 12.5 KB
/
fabfile.py
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
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
import errno
import os, json
from tempfile import mkdtemp
from contextlib import contextmanager
from fabric.operations import open_shell, put
from fabric.api import env, local, sudo, run, cd, prefix, task, settings, execute
from fabric.colors import green as _green, yellow as _yellow
from fabric.context_managers import hide, show, lcd
import boto
import boto.ec2
import time
# -----SETTINGS-----------
# Read in configurable settings.
deploy_settings = {
'aws_access_key_id': None,
'aws_secret_access_key': None,
'aws_default_region': None,
'aws_security_group_name': None,
'aws_instance_type': None,
'aws_ami_id': None,
'aws_ssh_key_name': None,
'aws_ssh_port': None,
}
for name, value in deploy_settings.items():
env_value = os.getenv(name.upper())
env[name] = env_value
if not env_value:
raise Exception("Please make sure to enter your AWS keys/info in your deploy/environment file before running fab scripts. {} is current set to {}".format(name, value))
# Define non-configurable settings.
env.root_directory = os.path.dirname(os.path.realpath(__file__))
env.deploy_directory = os.path.join(env.root_directory, 'deploy')
env.app_settings_file = os.path.join(env.deploy_directory, 'settings.json')
env.ssh_directory = os.path.join(env.deploy_directory, 'ssh')
env.aws_ssh_key_extension = '.pem'
env.aws_ssh_key_path = os.path.join(
env.ssh_directory,
''.join([env.aws_ssh_key_name, env.aws_ssh_key_extension]))
#-----FABRIC TASKS-----------
@task
def setup_aws_account():
prep_paths(env.ssh_directory, env.deploy_directory)
ec2 = connect_to_ec2()
# Check to see if specified keypair already exists.
# If we get an InvalidKeyPair.NotFound error back from EC2,
# it means that it doesn't exist and we need to create it.
try:
key_name = env.aws_ssh_key_name
key = ec2.get_all_key_pairs(keynames=[key_name])[0]
print "key name {} already exists".format(key_name)
except ec2.ResponseError, e:
if e.code == 'InvalidKeyPair.NotFound':
print 'Creating keypair: %s' % env.aws_ssh_key_name
# Create an SSH key to use when logging into instances.
key = ec2.create_key_pair(env.aws_ssh_key_name)
# AWS will store the public key but the private key is
# generated and returned and needs to be stored locally.
# The save method will also chmod the file to protect
# your private key.
key.save(directory_path=env.ssh_directory)
else:
raise
# Check to see if specified security group already exists.
# If we get an InvalidGroup.NotFound error back from EC2,
# it means that it doesn't exist and we need to create it.
try:
group = ec2.get_all_security_groups(groupnames=[env.aws_security_group_name])[0] # noqa
except ec2.ResponseError, e:
if e.code == 'InvalidGroup.NotFound':
print 'Creating Security Group: %s' % env.aws_security_group_name
# Create a security group to control access to instance via SSH.
group = ec2.create_security_group(env.aws_security_group_name,
'A group that allows SSH access')
else:
raise
# Add a rule to the security group to authorize SSH traffic
# on the specified port.
for port in ["80", env.aws_ssh_port]:
try:
group.authorize('tcp', port, port, "0.0.0.0/0")
except ec2.ResponseError, e:
if e.code == 'InvalidPermission.Duplicate':
print 'Security Group: %s already authorized' % env.aws_security_group_name # noqa
else:
raise
# postgres authorization
try:
group.authorize('tcp', 5432, 5432, src_group=group)
except ec2.ResponseError, e:
if e.code == 'InvalidPermission.Duplicate':
print 'Security Group: %s already authorized' % env.aws_security_group_name # noqa
else:
raise
@task
def create_instance(name, tag=None):
"""
Launch an instance and wait for it to start running.
Returns a tuple consisting of the Instance object and the CmdShell
object, if request, or None.
tag A name that will be used to tag the instance so we can
easily find it later.
"""
prep_paths(env.ssh_directory, env.deploy_directory)
print(_green("Started creating {}...".format(name)))
print(_yellow("...Creating EC2 instance..."))
conn = connect_to_ec2()
try:
key = conn.get_all_key_pairs(keynames=[env.aws_ssh_key_name])[0]
group = conn.get_all_security_groups(groupnames=[env.aws_security_group_name])[0] # noqa
except conn.ResponseError, e:
setup_aws_account()
reservation = conn.run_instances(
env.aws_ami_id,
key_name=env.aws_ssh_key_name,
security_groups=[env.aws_security_group_name],
instance_type=env.aws_instance_type)
instance = reservation.instances[0]
conn.create_tags([instance.id], {"Name":name})
if tag:
instance.add_tag(tag)
while instance.state != u'running':
print(_yellow("Instance state: %s" % instance.state))
time.sleep(10)
instance.update()
print(_green("Instance state: %s" % instance.state))
print(_green("Public dns: %s" % instance.public_dns_name))
host_data = {
'host_string': instance.public_dns_name,
'port': '22',
'user': 'ubuntu',
'key_filename': env.aws_ssh_key_path,
}
with open(os.path.join(env.ssh_directory, ''.join([name, '.json'])), 'w') as f: # noqa
json.dump(host_data, f)
f = open("deploy/fab_hosts/{}.txt".format(name), "w")
f.write(instance.public_dns_name)
f.close()
return instance.public_dns_name
@task
def terminate_instance(name):
"""
Terminates all servers with the given name
"""
print(_green("Started terminating {}...".format(name)))
conn = connect_to_ec2()
filters = {"tag:Name": name}
for reservation in conn.get_all_instances(filters=filters):
for instance in reservation.instances:
if "terminated" in str(instance._state):
print "instance {} is already terminated".format(instance.id)
continue
else:
print instance._state
print (instance.id, instance.tags['Name'])
if raw_input("terminate? (y/n) ").lower() == "y":
print(_yellow("Terminating {}".format(instance.id)))
conn.terminate_instances(instance_ids=[instance.id])
os.remove(os.path.join(env.ssh_directory, ''.join([name, '.json']))) # noqa
print(_yellow("Terminated"))
@task
def ssh(name):
"""SSH into an instance."""
with open(os.path.join(env.ssh_directory, ''.join([name, '.json'])), 'r') as f: # noqa
host_data = json.load(f)
f = open("deploy/fab_hosts/{}.txt".format(name))
env.host_string = "ubuntu@{}".format(f.readline().strip())
with settings(**host_data):
open_shell()
@task
def bootstrap(name, no_install=False):
"""
Bootstrap the specified server. Install chef then run chef solo.
:param name: The name of the node to be bootstrapped
:param no_install: Optionally skip the Chef installation
since it takes time and is unneccesary after the first run
:return:
"""
print(_green("--BOOTSTRAPPING {}--".format(name)))
f = open("deploy/fab_hosts/{}.txt".format(name))
env.host_string = "ubuntu@{}".format(f.readline().strip())
if not no_install:
install_chef()
run_chef(name)
@task
def associate_ip(name, no_install=False):
"""
Bootstrap the specified server. Install chef then run chef solo.
:param name: The name of the node to be bootstrapped
:param no_install: Optionally skip the Chef installation
since it takes time and is unneccesary after the first run
:return:
"""
print(_green("--BOOTSTRAPPING {}--".format(name)))
f = open("deploy/fab_hosts/{}.txt".format(name))
env.host_string = "ubuntu@{}".format(f.readline().strip())
allocate_and_assign_ip(name)
@task
def deploy(name):
"""
Deploy the github repo to your node.
:param name: The name of the node to be bootstrapped
:param no_install: Optionally skip the Chef installation
since it takes time and is unneccesary after the first run
:return:
"""
print(_green("--DEPLOYING {}--".format(name)))
f = open("deploy/fab_hosts/{}.txt".format(name))
env.host_string = "ubuntu@{}".format(f.readline().strip())
deploy_app(name)
@task
def restart(name):
"""
Reload nginx/gunicorn
"""
with settings(warn_only=True):
with open(env.app_settings_file) as f:
app_settings = json.load(f)
env.key_filename = env.aws_ssh_key_path
sudo("sudo supervisorctl restart {app_name}".format(app_name=app_settings["APP_NAME"]), shell=False)
sudo('sudo /etc/init.d/nginx reload', shell=False)
#----------HELPER FUNCTIONS-----------
def prep_paths(ssh_directory, deploy_directory):
try:
os.makedirs(ssh_directory)
except OSError as exception:
if exception.errno == errno.EEXIST and os.path.isdir(ssh_directory):
pass
else:
raise
os.chmod(deploy_directory, 0700)
os.chmod(ssh_directory, 0700)
@contextmanager
def _virtualenv():
with prefix(env.activate):
yield
def connect_to_ec2():
"""
return a connection given credentials imported from config
"""
ec2_connection = boto.ec2.connect_to_region(
env.aws_default_region,
aws_access_key_id=env.aws_access_key_id,
aws_secret_access_key=env.aws_secret_access_key)
if not ec2_connection:
raise Exception("We're having a problem connecting to your AWS account. Are you sure you entered your credentials correctly?")
return ec2_connection
def install_chef():
"""
Install chef-solo on the server.
"""
print(_yellow("--INSTALLING CHEF--"))
local("knife solo prepare -i {key_file} {host}".format(
key_file=env.aws_ssh_key_path,
host=env.host_string))
def run_chef(name):
"""
Read configuration from the appropriate node file and bootstrap
the node
:param name:
:return:
"""
print(_yellow("--RUNNING CHEF--"))
node = "./nodes/{name}_node.json".format(name=name)
with lcd('chef_files'):
local("knife solo cook -i {key_file} {host} {node}".format(
key_file=env.aws_ssh_key_path,
host=env.host_string,
node=node))
def deploy_app(name):
print(_yellow("--RUNNING CHEF--"))
node = "./nodes/deploy_node.json".format(name=name)
with lcd('chef_files'):
try:
# skip updating the Berkshelf cookbooks to save time
try:
os.rename("chef_files/Berksfile", "chef_files/hold_Berksfile")
except OSError:
pass
local("knife solo cook -i {key_file} {host} {node}".format(
key_file=env.aws_ssh_key_path,
host=env.host_string,
node=node))
restart(name)
except Exception as e:
print e
finally:
try:
os.rename("chef_files/hold_Berksfile", "chef_files/Berksfile")
except OSError:
pass
def allocate_and_assign_ip(name):
"""
"""
print(_green("Assigning an elastic IP to {}...".format(name)))
conn = connect_to_ec2()
filters = {"tag:Name": name}
ip = None
for reservation in conn.get_all_instances(filters=filters):
for instance in reservation.instances:
ip = conn.allocate_address()
ip.associate(instance.id)
break
break
if ip:
with open("deploy/fab_hosts/{}.txt".format(name), "w") as f:
f.write(ip.public_ip)
# update ssh address
with open(os.path.join(env.ssh_directory, ''.join([name, '.json'])), 'r') as f: # noqa
host_data = json.load(f)
host_data['host_string'] = ip.public_ip
with open(os.path.join(env.ssh_directory, ''.join([name, '.json'])), 'w') as f: # noqa
json.dump(host_data, f)
# update node address
with open("deploy/settings.json", 'r') as f: # noqa
settings_json = json.load(f)
settings_json['EC2_DNS'] = ip.public_ip
with open("deploy/settings.json", 'w') as f: # noqa
json.dump(settings_json, f)
print(_green("Your new elastic IP is {}.".format(ip.public_ip)))