This repository has been archived by the owner on Feb 10, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathyamlcf.py
executable file
·246 lines (197 loc) · 7.84 KB
/
yamlcf.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
#!/usr/bin/env python3
#
# Simple cloudformation cli tool
#
import argparse
import botocore
import botocore.session
import botocore.exceptions
import yaml
import json
import time
class GenericCompound:
def __init__(self, type):
self._type = type
@staticmethod
def to_yaml(dumper, data):
return data._type
def default_constructor(loader, tag_suffix, node):
return GenericCompound(node)
yaml.add_multi_constructor('', default_constructor, Loader=yaml.SafeLoader)
yaml.add_representer(GenericCompound, GenericCompound.to_yaml, Dumper=yaml.SafeDumper)
def name_from_file(yaml_file):
"""
Takes a yaml file name and generates a stack-name from it.
"""
for suffix in [".cf.yaml", ".cf.json", ".yaml", ".json"]:
if yaml_file.endswith(suffix):
end_index = len(yaml_file) - len(suffix)
return yaml_file[:end_index]
return yaml_file
def log(line):
"""
:param line: a line that is printed to stdout
"""
print(line)
def load(file_name, parsing=True):
"""
Loads the given file name as yaml document
:param parsing:
:param file_name:
:return: a yaml document as string with resolved aliases
"""
with open(file_name, 'r') as f:
if parsing:
log("parsing YAML file")
yaml_doc = yaml.safe_load(f)
yaml.Dumper.ignore_aliases = lambda *ignored_args: True
return yaml.safe_dump(yaml_doc, default_flow_style=False, allow_unicode=True)
else:
return f.read()
def retrieve_events(stack_name, last_shown_event_id=None, limit=None):
try:
r = cf_client.describe_stack_events(StackName=stack_name)
events = r['StackEvents']
if limit:
events = events[0:limit]
# filter already shown events
if last_shown_event_id:
for i in range(0, len(events)):
if events[i]['EventId'] == last_shown_event_id:
events = events[0:i]
break
events.reverse()
return events
except botocore.exceptions.ClientError:
return []
def retrieve_last_event_id(_stack_name):
_events = retrieve_events(_stack_name, None)
if len(_events) > 0:
return _events[-1]['EventId']
else:
return None
def show_summary(stack_name):
r = cf_client.describe_stacks(StackName=stack_name)
outputs = r['Stacks'][0].get('Outputs')
if outputs:
for o in outputs:
print("{}: {}".format(o['OutputKey'], o['OutputValue']))
def check_finished(_waiter, stack_name):
r = _waiter._operation_method(StackName=stack_name)
acceptors = list(_waiter.config.acceptors)
current_state = 'waiting'
for acceptor in acceptors:
if acceptor.matcher_func(r):
current_state = acceptor.state
break
else:
# If none of the acceptors matched, we should
# transition to the failure state if an error
# response was received.
if 'Error' in r:
# Transition to the failure state, which we can
# just handle here by raising an exception.
raise ValueError(r['Error'].get('Message', 'Unknown'))
if current_state == 'success':
return True
if current_state == 'failure':
raise ValueError('Waiter encountered a terminal failure state')
def wait_for(waiter_name, stack_name, e_id):
waiter = cf_client.get_waiter(waiter_name)
while waiter:
events = retrieve_events(stack_name, e_id)
print_events(events)
if len(events) > 0:
e_id = events[-1]['EventId']
if check_finished(waiter, stack_name):
log("----------------\nsuccessful")
waiter = None
time.sleep(1)
def print_events(events):
for event in events:
description = event.get('ResourceStatusReason', '')
log("{t} {ResourceType:<30} {LogicalResourceId:<20} {ResourceStatus:<20} {description}".format(t=event['Timestamp'].strftime('%Y-%m-%d %H:%M:%S'), description=description, **event))
def create_update_policy(allow_replace, allow_delete):
actions = ['Update:Modify']
if allow_delete:
actions.append('Update:Delete')
if allow_replace:
actions.append('Update:Replace')
return json.dumps({
"Statement": [{
"Effect": "Allow",
"Action": actions,
"Principal": "*",
"Resource": "*"
}]
})
parser = argparse.ArgumentParser(description='Yet another cloudformation tool')
parser.add_argument('command', metavar='command', help='create, update, delete, dump')
parser.add_argument('yaml_file', metavar='stack.cf.yaml', help='stack json or yaml file')
parser.add_argument('--stack-name', metavar='stack name', required=False, help='use the given stack name, don\'t guess from filename')
parser.add_argument('--on-failure', default='ROLLBACK', help='behavior for create failures: ROLLBACK, DELETE or DO_NOTHING')
parser.add_argument('--allow-update-replace', default=False, action='store_true', help='allows replacement of resources on update')
parser.add_argument('--allow-update-delete', default=False, action='store_true', help='allows deletion of resources on update')
parser.add_argument('--force', '-f', default=False, action='store_true', help='force deletion or replacement on update or delete without asking')
parser.add_argument('-n', '--no-parsing', dest='no_parsing', help='don\'t parse yaml file: Aliases are not resolved')
parser.add_argument('-p', '--parameter', dest='parameters', nargs='*', help='optional stack parameters as key=value')
args = parser.parse_args()
# AWS configuration
session = botocore.session.get_session()
cf_client = session.create_client('cloudformation')
# Stack name detection
if args.stack_name:
stack_name = args.stack_name
else:
stack_name = name_from_file(args.yaml_file)
log("Stack name: {}".format(stack_name))
last_shown_event_id = retrieve_last_event_id(stack_name)
parameters = []
if args.parameters:
for p in args.parameters:
key, value = p.split('=', 1)
parameters.append({'ParameterKey': key, 'ParameterValue': value})
stack_document = None
if args.command == "delete":
cf_client.delete_stack(StackName=stack_name)
wait_for('stack_delete_complete', stack_name, last_shown_event_id)
elif args.command == "info":
response = cf_client.describe_stacks(StackName=stack_name)
stack = response['Stacks'][0]
log("Description: {}".format(stack['Description']))
log("Created at: {}".format(stack['CreationTime']))
log("Status: {}".format(stack['StackStatus']))
log("")
show_summary(stack_name)
elif args.command == "update":
stack_document = load(args.yaml_file, (not args.no_parsing))
try:
response = cf_client.update_stack(
StackName=stack_name,
TemplateBody=stack_document,
Parameters=parameters,
StackPolicyDuringUpdateBody=create_update_policy(args.force or args.allow_update_replace, args.force or args.allow_update_delete),
Capabilities=[
'CAPABILITY_IAM',
])
wait_for('stack_update_complete', stack_name, last_shown_event_id)
except botocore.exceptions.ClientError as e:
message = e.response.get('Error', {}).get('Message')
if message == "No updates are to be performed.":
log(message)
else:
raise e
elif args.command == "create":
stack_document = load(args.yaml_file, (not args.no_parsing))
response = cf_client.create_stack(
StackName=stack_name,
TemplateBody=stack_document,
Parameters=parameters,
OnFailure=args.on_failure,
Capabilities=[
'CAPABILITY_IAM',
])
wait_for('stack_create_complete', stack_name, last_shown_event_id)
show_summary(stack_name)
elif args.command == "dump":
log(load(args.yaml_file, (not args.no_parsing)))