This repository has been archived by the owner on Jan 19, 2022. It is now read-only.
/
fab_tasks.py
executable file
·591 lines (477 loc) · 18.7 KB
/
fab_tasks.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
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
#!/usr/bin/env python
import logging
import os
import sys
import time
import uuid
from boto.route53.exception import DNSServerError
from fabric.api import env, task
from fabric.colors import green, red
from fabric.utils import abort
from bootstrap_cfn.cloudformation import Cloudformation
from bootstrap_cfn.config import ConfigParser, ProjectConfig
from bootstrap_cfn.elb import ELB
from bootstrap_cfn.errors import BootstrapCfnError
from bootstrap_cfn.errors import CloudResourceNotFoundError
from bootstrap_cfn.iam import IAM
from bootstrap_cfn.r53 import R53
from bootstrap_cfn.utils import tail
# Default fab config. Set via the tasks below or --set
env.setdefault('application')
env.setdefault('environment')
env.setdefault('aws')
env.setdefault('config')
env.setdefault('stack_passwords')
env.setdefault('blocking', True)
env.setdefault('aws_region', 'eu-west-1')
# GLOBAL VARIABLES
TIMEOUT = 3600
RETRY_INTERVAL = 10
# This is needed because pkgutil wont pick up modules
# imported in a fabfile.
path = env.real_fabfile or os.getcwd()
sys.path.append(os.path.dirname(path))
@task
def aws(profile_name):
"""
Set the AWS account to use
Sets the environment variable 'aws' to the name of the
account to use in the AWS config file (~/.aws/credentials.yaml)
Args:
profile_name(string): The string to set the environment
variable to
"""
env.aws = str(profile_name).lower()
@task
def environment(environment_name):
"""
Set the environment section to be read from the project config
file
Sets the environment variable 'environment'.
The named section will be read from the project's YAML file
Args:
environment_name(string): The string to set the
variable to
"""
env.environment = str(environment_name).lower()
@task
def application(application_name):
"""
Set the application name
Sets the environment variable 'application' to
an application name. Which is just a name to
associate with Cloudformation stack
Args:
application_name(string): The string to set the
variable to
"""
env.application = str(application_name).lower()
@task
def tag(tag):
"""
Set a tag for the stack
Sets the environment variable 'tag'
This gets used to store a DNS entry to identify
multiple stacks with the same name.
e.g. you can tag a stack as active, or inactive,
green or blue etc.
Args:
tag(string): The string to set the
variable to
"""
env.tag = str(tag).lower()
@task
def config(config_file):
"""
Set the location of the project's YAML file
Sets the environment variable 'config' to be
the location of the project's YAML config
file
Args:
config_file(string): The string to set the
variable to
"""
env.config = str(config_file).lower()
@task
def passwords(passwords_file):
"""
Set the path to the project's password YAML config file
Set the environment variable 'stack_passwords' to the
path of the project's password file. This will be used
to load in a dictionary of passwords to use with the
project's components
Args:
passwords_file(string): The string to set the
variable to
"""
env.stack_passwords = str(passwords_file).lower()
@task
def blocking(block):
"""
Set to block while waiting for stack creation or deletion to complete
Sets the environment variable 'blocking' to True to wait on stack
creation or deletion to complete before returning from the script.
If false the cloudformation task will be started and the script
will immediately exit
Args:
block(string): The string to set the
variable to. Must be one of yes, true,
t or 1
"""
env.blocking = str(block).lower() in ("yes", "true", "t", "1")
@task
def user(username):
"""
Sets the username to use for ssh to created instances
Sets the environment variable 'user' to the ssh username
to use when trying to connect to a remote instance
Args:
username(string): The string to set the
variable to.
"""
env.user = username
@task
def swap_tags(tag1, tag2):
"""
Swap two tagged stacks.
i.e. update the DNS text record which defines the
random suffix associated with a stack tag.
"""
cfn_config = get_config()
r53_conn = get_connection(R53)
zone_name = cfn_config.data['master_zone']
zone_id = r53_conn.get_hosted_zone_id(zone_name)
legacy_name = "{0}-{1}".format(env.application, env.environment)
record1 = "stack.{0}.{1}".format(tag1, legacy_name)
record2 = "stack.{0}.{1}".format(tag2, legacy_name)
stack_suffix1 = r53_conn.get_record(zone_name, zone_id, record1, 'TXT')
stack_suffix2 = r53_conn.get_record(zone_name, zone_id, record2, 'TXT')
fqdn1 = "{0}.{1}".format(record1, zone_name)
fqdn2 = "{0}.{1}".format(record2, zone_name)
r53_conn.update_dns_record(zone_id, fqdn1, 'TXT', '"{0}"'.format(stack_suffix2))
r53_conn.update_dns_record(zone_id, fqdn2, 'TXT', '"{0}"'.format(stack_suffix1))
def apply_maintenance_criteria(elb):
'''
Applies maintenance criteria to elb
Returns True if the maintenance should continue
'''
return elb['scheme'] == 'internet-facing'
@task
def enter_maintenance(maintenance_ip):
'''
Puts stack into maintenance mode
Sets all internet facing elb hostnames to resolve to given maintenance_ip
'''
cfn_config = get_config()
r53_conn = get_connection(R53)
cached_zone_ids = {}
for elb in cfn_config.data['elb']:
if not apply_maintenance_criteria(elb):
continue
record = "{name}.{hosted_zone}".format(**elb)
zone_id = get_cached_zone_id(r53_conn, cached_zone_ids, elb['hosted_zone'])
print green("Attempting to update: \"{0}\":\"{1}\"".format(record, maintenance_ip))
r53_conn.update_dns_record(zone_id, record, 'A', maintenance_ip)
@task
def exit_maintenance():
"""
Exit maintenance mode
Sets internet-facing elbs hostnames
back to the ELB DNS alias
"""
r53_conn = get_connection(R53)
elb_conn = get_connection(ELB)
cfn_config = get_config()
stack_name = get_stack_name()
# In order to traverse from config yaml all the way to the DNS alias for the ELB
# it is required to construct a logical to physical naming for the elbs. So first
# get all elbs for this stack from AWS cloudformation, to be used as a
# filter on the next step
# Note: if stack does not exist this will throw a BotoServerError
stack_elbs = dict([
(x.logical_resource_id, x.physical_resource_id)
for x in elb_conn.cfn.get_stack_load_balancers(stack_name)])
# filter stack related load balancers (as opposed to all stack elbs in the account)
full_load_balancers = elb_conn.conn_elb.get_all_load_balancers(
load_balancer_names=stack_elbs.values())
cached_zone_ids = {}
# loop through elb config entries and change internet facing ones
for elb in cfn_config.data['elb']:
if not apply_maintenance_criteria(elb):
continue
record = "{name}.{hosted_zone}".format(**elb)
# obtain physical name from dict lookup, by converting elb name into safe name
# into logical name
phys_name = stack_elbs[mold_to_safe_elb_name(elb['name'])]
dns_name = [x.dns_name for x in full_load_balancers if x.name == phys_name]
if len(dns_name) == 1:
dns_name = dns_name[0]
else:
raise BootstrapCfnError(
"Lookup for elb with physical name \"{0}\" returned {1} load balancers, "
"while only exactly 1 was expected".format(phys_name, len(dns_name)))
zone_id = get_cached_zone_id(r53_conn, cached_zone_ids, elb['hosted_zone'])
# For record_value provide list of params as needed by function set_alias
# http://boto.readthedocs.org/en/latest/ref/route53.html#boto.route53.record.Record.set_alias
record_value = [
# alias_hosted_zone_id
R53.AWS_ELB_ZONE_ID[env.aws_region],
# alias_dns_name
dns_name,
# alias_evaluate_target_health (True/False)
False
]
print green("Attempting to update: \"{0}\":{1}".format(record, record_value))
r53_conn.update_dns_record(zone_id, record, 'A', record_value, is_alias=True)
def get_cached_zone_id(r53_conn, zone_dict, zone_name):
'''
Gets and cache zone id from route53
If we are looping through ELBs we may just have different hostnames in same zone,
so feel free to cache it (and drink a shot because I said 'cache')
raises CloudResourceNotFoundError if zone is not found
'''
if zone_name not in zone_dict:
# not found, look it up, cache it up ..
lookup_zone = r53_conn.get_hosted_zone_id(zone_name)
if not lookup_zone:
raise CloudResourceNotFoundError("Zone ID not found for zone: {}".format(zone_name))
zone_dict[zone_name] = lookup_zone
return zone_dict[zone_name]
def mold_to_safe_elb_name(elb_name):
'''
Molds the elb_name to match cloudformation naming of ELBs
'''
return 'ELB' + elb_name.replace('-', '').replace('.', '').replace('_', '')
def get_stack_name(new=False):
"""
Get the name of the stack
The name of the stack is a combination
of the application and environment names
and a randomly generated suffix.
The env.tag dictates which randomly generated suffix
the default env.tag is 'active'
If new=True we generate a new stack_name and create the
dns records to retreive it in the future.
"""
if hasattr(env, 'tag'):
tag = env.tag
else:
tag = 'active'
env.tag = tag
if not hasattr(env, 'stack_name') or new:
legacy_name = "{0}-{1}".format(env.application, env.environment)
# get_config needs a stack_name so this is a hack because we don't
# know it yet...
env.stack_name = 'temp'
cfn_config = get_config()
try:
r53_conn = get_connection(R53)
try:
zone_name = cfn_config.data['master_zone']
except KeyError:
logging.warn("No master_zone in yaml, unable to create/find DNS records for "
"stack name, will fallback to legacy stack names: "
"application-environment")
env.stack_name = legacy_name
zone_id = r53_conn.get_hosted_zone_id(zone_name)
record_name = "stack.{0}.{1}".format(tag, legacy_name)
if new:
stack_suffix = uuid.uuid4().__str__()[-8:]
record = "{0}.{1}".format(record_name, zone_name)
r53_conn.update_dns_record(zone_id, record, 'TXT', '"{0}"'.format(stack_suffix))
else:
stack_suffix = r53_conn.get_record(zone_name, zone_id, record_name, 'TXT')
if stack_suffix:
env.stack_name = "{0}-{1}".format(legacy_name, stack_suffix)
else:
env.stack_name = legacy_name
except DNSServerError:
logging.warn("Couldn't find/create DNS entry for stack suffix, "
"stack name, will fallback to legacy stack names: "
"application-environment")
env.stack_name = legacy_name
return env.stack_name
def _validate_fabric_env():
if env.aws is None:
sys.exit("\n[ERROR] Please specify an AWS account, e.g 'aws:dev'")
if env.environment is None:
sys.exit("\n[ERROR] Please specify an environment, e.g 'environment:dev'")
if env.application is None:
sys.exit("\n[ERROR] Please specify an application, e.g 'application:peoplefinder'")
if env.config is None:
sys.exit("\n[ERROR] Please specify a config file, e.g 'config:/tmp/sample-application.yaml'")
elif not os.path.isfile(env.config):
sys.exit("\n[ERROR] Config file %s does not exist" % str(env.config))
if env.stack_passwords is not None and not os.path.exists(env.stack_passwords):
print >> sys.stderr, "\n[ERROR] Passwords file '{0}' doesn't exist!".format(env.stack_passwords)
sys.exit(1)
def get_config():
_validate_fabric_env()
project_config = ProjectConfig(
env.config,
env.environment,
passwords=env.stack_passwords)
Parser = env.get('cloudformation_parser', ConfigParser)
cfn_config = Parser(project_config.config, get_stack_name(), environment=env.environment, application=env.application)
return cfn_config
def get_connection(klass):
_validate_fabric_env()
return klass(env.aws, env.aws_region)
@task
def cfn_delete(force=False, pre_delete_callbacks=None):
"""
Delete the AWS Cloudformation stack
Deletes the stack and the associated SSL certificates
Args:
force(bool): True to destroy the stack without any further
input, False to require confirmation before deletion
pre_delete_callbacks(list of callables): callable to invoke before
trying to run the DeleteStack call. Each callback is called with
kwargs of ``stack_name``, and ``config``. (Python only, not setable from
command line)
"""
stack_name = get_stack_name()
if not force:
x = raw_input("Are you really sure you want to blow away the whole stack for {}!? (y/n)\n".format(stack_name))
if x not in ['y', 'Y', 'Yes', 'yes']:
sys.exit(1)
cfn_config = get_config()
cfn = get_connection(Cloudformation)
if pre_delete_callbacks is not None:
for callback in pre_delete_callbacks:
callback(stack_name=stack_name, config=cfn_config)
print green("\nSTACK {0} DELETING...\n").format(stack_name)
cfn.delete(stack_name)
if not env.blocking:
print 'Running in non blocking mode. Exiting.'
sys.exit(0)
# Wait for stacks to delete
print 'Waiting for stack to delete.'
tail(cfn, stack_name)
if cfn.stack_missing(stack_name):
print green("Stack successfully deleted")
else:
print red("Stack deletion was unsuccessfull")
if 'ssl' in cfn_config.data:
iam = get_connection(IAM)
iam.delete_ssl_certificate(cfn_config.ssl(), stack_name)
@task
def cfn_create(test=False):
"""
Create the AWS cloudformation stack.
Using the configuration files, a full cloudformation
specification will be generated and used to create a
stack on AWS.
"""
stack_name = get_stack_name(new=True)
cfn_config = get_config()
cfn = get_connection(Cloudformation)
if test:
print cfn_config.process()
return
# Upload any SSL certs that we may need for the stack.
if 'ssl' in cfn_config.data:
print green("Uploading SSL certificates to stack")
iam = get_connection(IAM)
iam.upload_ssl_certificate(cfn_config.ssl(), stack_name)
# Useful for debug
# print cfn_config.process()
# Inject security groups in stack template and create stacks.
try:
stack = cfn.create(stack_name, cfn_config.process(), tags=get_cloudformation_tags())
except:
# cleanup ssl certificates if any
if 'ssl' in cfn_config.data:
print red("Deleting SSL certificates from stack")
iam.delete_ssl_certificate(cfn_config.ssl(), stack_name)
import traceback
abort(red("Failed to create: {error}".format(error=traceback.format_exc())))
print green("\nSTACK {0} CREATING...\n").format(stack_name)
if not env.blocking:
print 'Running in non blocking mode. Exiting.'
sys.exit(0)
tail(cfn, stack_name)
stack_evt = cfn.get_last_stack_event(stack)
if stack_evt.resource_status == 'CREATE_COMPLETE':
print 'Successfully built stack {0}.'.format(stack)
else:
# So delete the SSL cert that we uploaded
if 'ssl' in cfn_config.data:
iam.delete_ssl_certificate(cfn_config.ssl(), stack_name)
abort('Failed to create stack: {0}'.format(stack))
@task
def update_certs(delete_replaced_certificates=True):
"""
Update the ssl certificates
This will read in the certificates from the config
file, update them in AWS Iam, and then also handle
setting the certificates on ELB's. By default, replaced
SSL certs will be deleted.
Args:
delete_replaced_certificates: Delete the certificates we have replaced
"""
stack_name = get_stack_name()
cfn_config = get_config()
# Upload any SSL certificates to our EC2 instances
if 'ssl' in cfn_config.data:
logging.info("update_certs: Updating SSL certificates...")
iam = get_connection(IAM)
updated_certs = iam.update_ssl_certificates(cfn_config.ssl(),
stack_name)
else:
logging.error("update_certs: No ssl section found in cloud config file, aborting...")
sys.exit(1)
# Set the certificates on ELB's if we have any
if len(updated_certs) <= 0:
logging.error("No certificates updated so skipping "
"ELB certificate update...")
if 'elb' in cfn_config.data:
logging.info("update_certs: Setting load balancer certificates...")
elb = get_connection(ELB)
replaced_certificates = elb.set_ssl_certificates(updated_certs, stack_name, max_retries=3)
if delete_replaced_certificates:
for replaced_certificate in replaced_certificates:
iam.delete_certificate(replaced_certificate, stack_name, max_retries=3)
def get_cloudformation_tags():
"""
Get a top-level set of tags for the stack, these will propagate
down so that many of the created resources will be tagged in
addition. Notable omissions are EBS volumes and route53 resources
"""
return {
"Env": env.environment,
"Application": env.application
}
@task
def display_elb_dns_entries():
"""
Prints out the ELB name(s) and the corresponding DNS name(s) for every ELB
in the environment provided.
"""
stack_name = get_stack_name()
elb = get_connection(ELB)
elb_dns_list = elb.list_domain_names(stack_name)
for elb_dns in elb_dns_list:
print "\n\nELB name: {0} DNS: {1}".format(elb_dns['elb_name'], elb_dns['dns_name'])
@task
def enable_vpc_peering():
"""
Enables vpc peering to stacks named in the cloudformation config.
"""
# peer vpc
cfg = get_config()
vpc_cfg = cfg.data.get('vpc', False)
if vpc_cfg:
vpc_obj = VPC(cfg.data, get_stack_name())
vpc_obj.enable_peering()
@task
def disable_vpc_peering():
"""
Disables vpc peering to stacks named in the cloudformation config.
"""
# peer vpc
cfg = get_config()
vpc_cfg = cfg.data.get('vpc', False)
if vpc_cfg:
vpc_obj = VPC(cfg.data, get_stack_name())
vpc_obj.disable_peering()