-
Notifications
You must be signed in to change notification settings - Fork 1
/
timeline_build.py
executable file
·77 lines (69 loc) · 2.35 KB
/
timeline_build.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
#!/usr/bin/env python3
"""
Build timeline for a CFN build
and "draw" a diagram of it
"""
import json
import sys
import arrow
def print_help():
"""
Usage printer for any error
"""
print(
'''
Usage:
./timeline_build.py events.json
hint: to generate the events.json run
aws cloudformation describe-stack-events --stack-name mystack > events.json
''')
sys.exit(1)
# depending the screen size and whole build time
SCALE = 0.18 # potential improvment, calc this based on terminal size and max time
# Set it to -1 to show all entries
TOSMALL = 1
RESOURCES = []
START_TIMES = {}
END_TIMES = {}
try:
EF = sys.argv[1]
if EF == "help":
print_help()
with open(EF) as json_file:
DATA = json.load(json_file)
for event in DATA['StackEvents']:
if event['ResourceStatus'] in ("CREATE_IN_PROGRESS", "CREATE_COMPLETE"):
r = event['LogicalResourceId']
if event['ResourceStatus'] == "CREATE_IN_PROGRESS":
try:
if START_TIMES[r] < arrow.get(event['Timestamp']).timestamp():
START_TIMES[r] = arrow.get(event['Timestamp']).timestamp()
except KeyError:
START_TIMES[r] = arrow.get(event['Timestamp']).timestamp()
else:
END_TIMES[r] = arrow.get(event['Timestamp']).timestamp()
if r not in RESOURCES:
RESOURCES.append(r)
except IndexError:
print("[ERROR] You forgot to specify the event file location!")
print("to check usage run ./timeline_build.py help")
sys.exit(1)
except FileNotFoundError:
print("[ERROR] Can't find the file, make sure it exist and readable!")
print("to check usage run ./timeline_build.py help")
sys.exit(1)
except ValueError:
print("[ERROR] That does not look like a well formated JSON!")
print("to check usage run ./timeline_build.py help")
sys.exit(1)
FIRST = min(START_TIMES, key=START_TIMES.get)
FIRST_T = START_TIMES[FIRST]
RESOURCES.sort(key=START_TIMES.get)
for res in RESOURCES:
start = START_TIMES[res] - FIRST_T
end = END_TIMES[res] - FIRST_T
timesum = end - start
if timesum > TOSMALL:
print(" "*(int(start*SCALE)) +
"»"*(int(timesum*SCALE)) +
" - {} ({}s)".format(res, timesum))