This repository has been archived by the owner on Jan 19, 2022. It is now read-only.
/
fab_tasks.py
executable file
·987 lines (819 loc) · 32.4 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
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
#!/usr/bin/env python
import logging
import os
import re
import sys
import uuid
import boto.exception
import boto3
from fabric.api import env, task
from fabric.colors import green, red
from fabric.utils import abort
import pkg_resources
from bootstrap_cfn.autoscale import Autoscale
from bootstrap_cfn.cloudformation import Cloudformation
from bootstrap_cfn.config import ConfigParser, ProjectConfig
from bootstrap_cfn.elb import ELB
from bootstrap_cfn.errors import (ActiveTagExistConflictError, BootstrapCfnError,
CfnConfigError, CloudResourceNotFoundError, DNSRecordNotFoundError,
PublicELBNotFoundError, StackRecordNotFoundError, TagRecordExistConflictError,
TagRecordNotFoundError, UpdateDNSRecordError, UpdateDeployarnRecordError,
ZoneIDNotFoundError)
from bootstrap_cfn.iam import IAM
from bootstrap_cfn.r53 import R53
from bootstrap_cfn.utils import tail
from bootstrap_cfn.vpc import VPC
# 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')
env.setdefault('keyname', None)
# 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))
# Set up the logging
logging.basicConfig(level=logging.INFO)
logger = logging.getLogger("bootstrap-cfn")
logging.getLogger("requests").setLevel(logging.WARNING)
@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()
# Setup boto so we actually use this environment
boto3.setup_default_session(profile_name=env.aws,
region_name=env.aws_region)
@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 keyname(keyname):
"""
Sets the keyname to the keypair name on AWS
Sets the keyname to specific keypair name you created instead of "default"
Args:
keyname: the name of keypair on AWS
"""
env.keyname = str(keyname).lower()
@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, dry_run=False):
'''
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, dry_run=dry_run)
@task
def exit_maintenance(dry_run=False):
"""
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.get('logical_resource_id', x.get('LogicalResourceId', None)),
x.get('physical_resource_id', x.get('PhysicalResourceId', None)))
for x in elb_conn.cfn.get_stack_load_balancers(stack_name)])
if None in stack_elbs.keys():
raise BootstrapCfnError(
"Unable to retrieve logical resource IDs for a stack load balancer.\n"
"ELB Dict: ".format(stack_elbs))
if None in stack_elbs.values():
raise BootstrapCfnError(
"Unable to retrieve physical resource IDs for a stack load balancer.\n"
"ELB Dict: ".format(stack_elbs))
# 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, dry_run=dry_run)
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 new:
# For back-compatibility
set_stack_name()
try:
stack_tag = get_env_tag()
except AttributeError:
stack_tag = 'active'
env.tag = stack_tag
if not hasattr(env, 'stack_name'):
# get_config needs a stack_name so this is a hack because we don't
# know it yet...
env.stack_name = 'temp'
zone_name = get_zone_name()
zone_id = get_zone_id()
logger.info("fab_tasks::get_stack_name: Found master zone '%s' in config...", zone_name)
# get record name in the format of: stack.[stack_tag].[app]-[env]
record_name = get_tag_record_name(stack_tag)
dns_name = "{}.{}".format(record_name, zone_name)
r53_conn = get_connection(R53)
try:
# get stack id
stack_suffix = r53_conn.get_record(zone_name, zone_id, record_name, 'TXT').replace('"', "")
logger.info("fab_tasks::get_stack_name: Found stack suffix '%s' "
"for dns record '%s'... ", stack_suffix, dns_name)
legacy_name = get_legacy_name()
env.stack_name = "{0}-{1}".format(legacy_name, stack_suffix)
logger.info("fab_tasks::get_stack_name: Found stack name '%s'...", env.stack_name)
except Exception:
raise DNSRecordNotFoundError(dns_name)
return env.stack_name
def set_stack_name():
"""
Set 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'
We generate a new stack_name and create the
dns records to retreive it in the future.
"""
# create a stack id
r53_conn = get_connection(R53)
zone_name = get_zone_name()
zone_id = get_zone_id()
stack_suffix = uuid.uuid4().__str__()[-8:]
try:
stack_tag = get_env_tag()
if stack_tag == 'active':
# print red("'Active' tag is reserved, please change a tag. ")
raise ActiveTagExistConflictError()
elif r53_conn.hastag(zone_name, zone_id, get_tag_record_name(stack_tag)):
# print red("{} exists, please change a tag. ".format(env.tag))
raise TagRecordExistConflictError(stack_tag)
except AttributeError:
stack_tag = stack_suffix
env.tag = stack_tag
record = "{}.{}".format(get_tag_record_name(stack_tag), zone_name)
logger.info("fab_tasks::set_stack_name: "
"Creating stack suffix '%s' "
"for record '%s' "
"in zone id '%s'...", stack_suffix, record, zone_id)
# Let DNS update DNSServerError propogate
try:
r53_conn.update_dns_record(zone_id, record, 'TXT', '"{0}"'.format(stack_suffix))
env.stack_name = "{0}-{1}".format(get_legacy_name(), stack_suffix)
except Exception:
raise UpdateDNSRecordError
print green("Stack tag is set to {0}".format(stack_tag))
return env.stack_name
def get_zone_name():
try:
zone_name = get_basic_config()['master_zone']
except KeyError:
raise CfnConfigError("No master_zone in yaml, unable to create/find DNS records for stack name")
logger.info("fab_tasks::get_zone_id: Found master zone '%s' in config...", zone_name)
return zone_name
def get_zone_id():
zone_name = get_zone_name()
r53_conn = get_connection(R53)
try:
zone_id = r53_conn.get_hosted_zone_id(zone_name)
except Exception:
raise ZoneIDNotFoundError(zone_name)
logger.info("fab_tasks::get_zone_id: Found zone id '%s' "
"for zone name '%s'...", zone_id, zone_name)
return zone_id
def get_legacy_name():
legacy_name = "{0}-{1}".format(env.application, env.environment)
return legacy_name
def get_tag_record_name(stack_tag):
"""
Returns record name in the format of: stack.[tag].[app]-[env]
Args:
stack_tag: the tag of stack
Returns:
record name like stack.[tag].[app]-[env]
"""
legacy_name = get_legacy_name()
record_name = "stack.{0}.{1}".format(stack_tag, legacy_name)
return record_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_basic_config():
"""
Returns the basic unparsed configuration file for the project
"""
_validate_fabric_env()
project_config = ProjectConfig(
env.config,
env.environment,
passwords=env.stack_passwords)
return project_config.config
def get_config(called_by_cfn_create=False):
Parser = env.get('cloudformation_parser', ConfigParser)
basic_config = get_basic_config()
# keyname is mandatory in cfn_create, optional in others.
# keyname can be defined in fab or config while fab parameters has higher priority.
# otherwise not.
env_keyname = None
if called_by_cfn_create:
env_keyname = env.keyname
if env.keyname is None:
# if keyname is not defined in fab, check config file instead
print "[WARNING] keyname is not specified in fab command, checking config file..."
try:
env_keyname = basic_config['ec2']['parameters']['KeyName']
except KeyError:
sys.exit("[ERROR] KeyName is not defined in config file. "
"Please specify via fab e.g 'keyname:opskey' or config file")
print green("Creating stack with keyname: {0}").format(env_keyname)
cfn_config = Parser(get_basic_config(), get_stack_name(), environment=env.environment,
application=env.application, keyname=env_keyname)
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 for inactive stacks
Delete DNS records for active stacks
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)
r53_conn = get_connection(R53)
elb = get_first_public_elb()
stack_id = stack_name.split('-')[-1]
zone_name = get_zone_name()
zone_id = get_zone_id()
if not isactive():
# delete inactive stack
stack_tag = get_env_tag()
logger.info("Deleting '%s' inactive stack '%s'...".format(stack_tag, stack_name))
print green("\nSTACK {0} DELETING...\n").format(stack_name)
# delete Alias and TXT records
txt_tag_record = get_tag_record_name(stack_tag)
r53_conn.delete_record(zone_name, zone_id, elb, stack_id, stack_tag, txt_tag_record)
# Wait for stacks to delete
print 'Waiting for stack to delete.'
cfn.delete(stack_name)
if not env.blocking:
print 'Running in non blocking mode. Exiting.'
sys.exit(0)
tail(cfn, stack_name)
if cfn.stack_missing(stack_name):
print green("Stack successfully deleted")
else:
print red("Stack deletion was unsuccessful")
return False
# cleanup ssl if exists
# currently we read ssl from configuration file instead of from AWS
# this can cause some mismatch when local config file has been changed.s
try:
iam = get_connection(IAM)
iam.delete_ssl_certificate(cfn_config.ssl(), stack_name)
except AttributeError, boto.exception:
print green("ssl did not exist")
else:
# delete active dns records
stack_tag = 'active'
print green("\nDELETING ACTIVE DNS RECORDS...\n")
txt_tag_record = get_tag_record_name(stack_tag)
r53_conn.delete_record(zone_name, zone_id, elb, stack_id, stack_tag, txt_tag_record)
return True
def get_env_tag():
return env.tag
def get_env_application():
return env.application
def isactive():
try:
if env.tag == 'active':
return True
except AttributeError:
return False
@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.
"""
_validate_fabric_env()
stack_name = get_stack_name(new=True)
cfn_config = get_config(called_by_cfn_create=True)
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 Exception:
# 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
cfn_delete(True)
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 green('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))
return True
@task
def update_certs():
"""
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
"""
stack_name = get_stack_name()
cfn_config = get_config()
iam = get_connection(IAM)
# Upload any SSL certificates to our EC2 instances
if 'ssl' in cfn_config.data:
logger.info("Reloading SSL certificates...")
updated_count = iam.update_ssl_certificates(cfn_config.ssl(),
stack_name)
else:
logger.error("No ssl section found in cloud config file, aborting...")
sys.exit(1)
# Set the certificates on ELB's if we have any
if updated_count:
if 'elb' in cfn_config.data:
logger.info("Setting load balancer certificates...")
elb = get_connection(ELB)
replaced_certs = elb.set_ssl_certificates(updated_count,
stack_name,
max_retries=3,
retry_delay=10)
for cert_name in replaced_certs:
logger.info("Deleting replaced certificate '%s'..."
% (cert_name))
iam.delete_certificate(cert_name,
stack_name,
max_retries=3,
retry_delay=10)
else:
logger.error("No certificates updated so skipping "
"ELB certificate update...")
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,
"Bootstrap-cfn-Version": pkg_resources.require("bootstrap-cfn")[0].version
}
@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()
@task
def set_autoscaling_desired_capacity(capacity, block=True):
"""
Set the desired capacity the autoscaling group
Args:
capacity(int): Number of instances desired in
the autoscaling group.
block(bool): Wait for instances to become healthy
and in-service.
"""
asg = get_connection(Autoscale)
if not asg.group:
asg.set_autoscaling_group(get_stack_name())
asg.set_autoscaling_desired_capacity(capacity=int(capacity))
if block:
asg.wait_for_instances(int(capacity))
@task
def cycle_instances(delay=None):
"""
Cycle the instances in the autoscaling group
Args:
delay(int): Number of seconds between new instance
becoming healthy and killing the old one.
"""
asg = get_connection(Autoscale)
if not asg.group:
asg.set_autoscaling_group(get_stack_name())
if delay:
termination_delay = int(delay)
else:
termination_delay = None
asg.cycle_instances(termination_delay=termination_delay)
@task
def set_active_stack(stack_tag, force=False):
"""
Switch between stacks tagged differently
Update 'active' stacks' DNS records to the one specified.
Args:
stack_tag: the tag of stack to be active
force: if True, set it to active stack directly
"""
# helloworld.active.dsd.io
active_record = get_tag_record_name('active')
r53_conn = get_connection(R53)
zone_name = get_zone_name()
zone_id = get_zone_id()
tag_record = get_tag_record_name(stack_tag)
try:
tag_stack_id = r53_conn.get_record(zone_name, zone_id, tag_record, 'TXT')
except Exception:
raise TagRecordNotFoundError(tag_record)
if get_active_stack() and not force:
x = raw_input("Your stack is {}. Do you wanna change? (y/n)\n".format(tag_stack_id))
if x not in ['y', 'Y', 'Yes', 'yes']:
sys.exit(1)
# update TXT record
try:
r53_conn.update_dns_record(zone_id, "{}.{}".format(active_record, get_zone_name()), 'TXT',
'"{}"'.format(tag_stack_id))
logger.info("fab_tasks::set_active_stack: Successfully updated dns alias record")
except Exception:
raise UpdateDNSRecordError
# get the first public facing elb
elb = get_first_public_elb()
main_record_name = "{}.{}".format(elb, zone_name)
record_name = "{}-{}".format(elb, tag_stack_id)
try:
record_object = r53_conn.get_full_record(zone_name, zone_id, record_name, 'A')
record_value = [record_object.alias_hosted_zone_id,
record_object.alias_dns_name,
record_object.alias_evaluate_target_health]
# point [helloworld.dsd.io] to [helloworld-12345.dsd.io]'s ELB
try:
r53_conn.update_dns_record(zone_id, main_record_name, 'A', record_value, is_alias=True)
logger.info("fab_tasks::set_active_stack: Successfully updated dns alias record")
except Exception:
raise UpdateDNSRecordError
except Exception:
raise StackRecordNotFoundError(record_name)
try:
set_active_deployarn(stack_tag)
print green("Active stack is switched to {}".format(tag_record))
return True
except:
raise UpdateDeployarnRecordError
def set_active_deployarn(stack_tag):
"""
- Get the value of delpoyarn.stack_tag.x.x
- Set it to the value of deployarn.active.x.x
Args:
stack_tag: the stack that is set to be active
Returns:
(String) AWS arn value
"""
zone_name = get_zone_name()
tag_arn_record = arn_record_name(stack_tag, zone_name)
active_arn_record = arn_record_name('active', zone_name)
r53 = get_connection(R53)
try:
tag_arn_value = r53.get_deployarn_record(get_zone_id(), tag_arn_record, 'TXT')
if not tag_arn_value:
raise StackRecordNotFoundError(tag_arn_value)
print "Active deployarn was set to: {0}".format(tag_arn_value)
zone_id = get_zone_id()
try:
r53.update_dns_record(zone_id, active_arn_record, 'TXT', '"{0}"'.format(tag_arn_value))
except:
raise UpdateDeployarnRecordError
except:
raise StackRecordNotFoundError(tag_arn_record)
return tag_arn_value
def arn_record_name(stack_tag, zone_name):
tag_arn_record = 'deployarn.{0}.{1}.{2}.{3}.'.format(stack_tag,
env.environment,
env.application,
zone_name)
return tag_arn_record
@task
def get_active_stack():
"""
Returns stack id if active stack exists AND Alias record is set appropriately
"""
try:
active_record = get_tag_record_name('active')
r53_conn = get_connection(R53)
zone_name = get_zone_name()
zone_id = get_zone_id()
active_stack_id = r53_conn.get_record(zone_name, zone_id, active_record, 'TXT')
elb = get_first_public_elb()
dns_record_name = '{}-{}'.format(elb, active_stack_id)
dns_record_value = r53_conn.get_record(zone_name, zone_id, dns_record_name, 'A')
main_record_value = r53_conn.get_record(zone_name, zone_id, elb, 'A')
except Exception:
print green("No active stack exists.")
return
if active_stack_id and dns_record_value and dns_record_value == main_record_value:
logger.info("fab_tasks::get_active_stack: "
"Active stack id is: '%s'", active_stack_id)
print green("Active stack id is: {}".format(active_stack_id))
return active_stack_id
else:
print green("No active stack exists.")
return None
def get_all_elbs():
"""
Returns all internet-facing elbs from cloudformation configuration
"""
cfn_config = get_config()
elbs = [x.get('name') for x in cfn_config.data.get('elb', {}) if x.get('scheme') == 'internet-facing']
return elbs
def get_first_public_elb():
"""
Returns the first elb if exists.
"""
elbs = get_all_elbs()
if len(elbs) < 1:
raise PublicELBNotFoundError
elif len(elbs) == 1:
logger.info("fab_tasks::set_active_stack: Found one ELB '%s', "
"using it for public ELB... ", elbs[0])
else:
logger.info("fab_tasks::set_active_stack: Found multiple ELBs,"
"using the first one '%s' as public ELB", elbs[0])
return elbs[0]
@task
def get_stack_list():
'''
Returns all stacks in all environments
'''
r53_conn = get_connection(R53)
rrsets = r53_conn.get_all_resource_records(get_zone_id())
regex = "stack\.\w+\.{}.+".format(get_env_application())
stack_count = 0
for rr in rrsets:
if re.match(regex, rr.name):
stack_id = rr.resource_records[0][1:-1]
stack_count += 1
print green("DNS record name: {} stack id: {}".format(rr.name.ljust(40), stack_id.ljust(40)))
return stack_count