forked from vantage-sh/ec2instances.info
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrender.py
106 lines (97 loc) · 3.06 KB
/
render.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
import mako.template
import mako.exceptions
import json
import datetime
def pretty_name(inst):
pieces = inst['instance_type'].split('.')
family = pieces[0]
short = pieces[1]
family_names = {
'r3': 'R3 High-Memory',
'c3': 'C3 High-CPU',
'm3': 'M3 General Purpose',
'i3': 'I3 High I/O',
'cc1': 'Cluster Compute',
'cg1': 'Cluster GPU',
'cc2': 'Cluster Compute',
'cr1': 'High Memory Cluster',
'hs1': 'High Storage',
'c1' : 'C1 High-CPU',
'hi1': 'HI1. High I/O',
'm2' : 'M2 High Memory',
'm1' : 'M1 General Purpose'
}
prefix = family_names.get(family, family.upper())
extra = None
if short.startswith('8x'):
extra = 'Eight'
elif short.startswith('4x'):
extra = 'Quadruple'
elif short.startswith('2x'):
extra = 'Double'
elif short.startswith('x'):
extra = ''
bits = [prefix]
if extra is not None:
bits.extend([extra, 'Extra'])
short = 'Large'
bits.append(short.capitalize())
return ' '.join([b for b in bits if b])
def network_sort(inst):
perf = inst['network_performance']
if perf == 'Very Low':
sort = 0
elif perf == 'Low':
sort = 1
elif perf == 'Moderate':
sort = 2
elif perf == 'High':
sort = 3
elif perf == '10 Gigabit':
sort = 4
else: ## unknown value
sort = 5
sort *= 2
if inst['ebs_optimized']:
sort += 1
return sort
def add_cpu_detail(i):
if i['instance_type'] in ('cc1.4xlarge', 'cg1.4xlarge'):
i['cpu_details'] = {
'cpus': 2,
'type': 'Xeon X5570',
'note': 'Quad-core Nehalem architecture'
}
elif i['instance_type'] in ('hi1.4xlarge', 'hs1.8xlarge'):
i['cpu_details'] = {
'cpus': 2,
'type': 'Xeon E5-2650',
'note': 'Eight-core Sandy Bridge architecture'
}
elif i['instance_type'] in ('cc2.8xlarge', 'cr1.8xlarge'):
i['cpu_details'] = {
'cpus': 2,
'type': 'Xeon E5-2670',
'note': 'Eight-core Sandy Bridge architecture'
}
def add_render_info(i):
i['network_sort'] = network_sort(i)
i['pretty_name'] = pretty_name(i)
add_cpu_detail(i)
def render(data_file, template_file, destination_file):
"""Build the HTML content from scraped data"""
template = mako.template.Template(filename=template_file)
print "Loading data from %s..." % data_file
with open(data_file) as f:
instances = json.load(f)
for i in instances:
add_render_info(i)
print "Rendering to %s..." % destination_file
generated_at = datetime.datetime.utcnow().strftime('%Y-%m-%d %H:%M:%S UTC')
with open(destination_file, 'w') as fh:
try:
fh.write(template.render(instances=instances, generated_at=generated_at))
except:
print mako.exceptions.text_error_template().render()
if __name__ == '__main__':
render('www/instances.json', 'in/index.html.mako', 'www/index.html')