-
Notifications
You must be signed in to change notification settings - Fork 0
/
cfn.py
148 lines (113 loc) · 4.45 KB
/
cfn.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
#!/usr/bin/env python
import argparse
import boto
import boto.s3
import boto.cloudformation
from configuration import (
stack_base_name,
region_name,
)
def cfn_connect(region_name_to_connect):
return boto.cloudformation.connect_to_region(region_name_to_connect)
def cfn_update(cfn_conn, stack_name, template_body, capabilities=None):
cfn_conn.update_stack(stack_name, template_body=template_body, capabilities=capabilities)
def cfn_create(cfn_conn, stack_name, template_body, capabilities=None):
cfn_conn.create_stack(stack_name, template_body=template_body, capabilities=capabilities)
def update(template_name):
cfn_conn = cfn_connect(region_name)
stack_name = stack_base_name + '-' + template_name
if template_name == 'assets':
print("Updating stack: {0}".format(stack_name))
from templates import assets as template
cfn_update(cfn_conn, stack_name, template.get())
return 0
if template_name == 'assets-https-enabled':
print("Updating stack: {0}".format(stack_name))
from templates import assets_https_enabled as template
cfn_update(cfn_conn, stack_name, template.get())
return 0
if template_name == 'ecr':
print("Updating stack: {0}".format(stack_name))
from templates import ecr as template
cfn_update(cfn_conn, stack_name, template.get())
return 0
if template_name == 'network':
print("Updating stack: {0}".format(stack_name))
from templates import network as template
cfn_update(cfn_conn, stack_name, template.get())
return 0
if template_name == 'rds':
print("Updating stack: {0}".format(stack_name))
from templates import rds as template
cfn_update(cfn_conn, stack_name, template.get())
return 0
if template_name == 'cluster':
print("Updating stack: {0}".format(stack_name))
from templates import cluster as template
cfn_update(cfn_conn, stack_name, template.get(), capabilities=['CAPABILITY_IAM'])
return 0
if template_name == 'ecs':
print("Updating stack: {0}".format(stack_name))
from templates import ecs as template
cfn_update(cfn_conn, stack_name, template.get(), capabilities=['CAPABILITY_IAM'])
return 0
return 1
def create(template_name):
cfn_conn = cfn_connect(region_name)
stack_name = stack_base_name + '-' + template_name
print("Creating stack: {0}".format(stack_name))
if template_name == 'assets':
from templates import assets as template
cfn_create(cfn_conn, stack_name, template.get())
return 0
if template_name == 'assets-https-enabled':
from templates import assets_https_enabled as template
cfn_create(cfn_conn, stack_name, template.get())
return 0
if template_name == 'ecr':
from templates import ecr as template
cfn_create(cfn_conn, stack_name, template.get())
return 0
if template_name == 'network':
from templates import network as template
cfn_create(cfn_conn, stack_name, template.get())
return 0
if template_name == 'rds':
from templates import rds as template
cfn_create(cfn_conn, stack_name, template.get())
return 0
if template_name == 'cluster':
from templates import cluster as template
cfn_create(cfn_conn, stack_name, template.get(), capabilities=['CAPABILITY_IAM'])
return 0
if template_name == 'ecs':
from templates import ecs as template
cfn_create(cfn_conn, stack_name, template.get(), capabilities=['CAPABILITY_IAM'])
return 0
return 1
def parse_args():
parser = argparse.ArgumentParser(
description='Creates or updates a CloudFormation stack')
parser.add_argument('-c', '--create', action='store_true')
parser.add_argument('-t', '--template',
choices=[
'assets',
'assets-https-enabled',
'ecr',
'network',
'rds',
'cluster',
'ecs',
],
required=True,
type=str)
return parser.parse_args()
def main():
args = parse_args()
if args.create:
create(args.template)
return 0
update(args.template)
return 0
if __name__ == "__main__":
main()