This repository has been archived by the owner on Jul 21, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpyEC2Calc.py
101 lines (81 loc) · 2.71 KB
/
pyEC2Calc.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
__author__ = 'ihasn'
# Simple EC2 calculator for AWS.
# Imports
import csv
def osinput():
# Takes input as either Linux or Windows
os = raw_input("Linux/Windows OS: ")
return os
def testosinput(os):
if os != 'Linux' and os != 'Windows':
# Exits if not
print 'Exiting...'
exit()
print ""
def printinstances(os):
# Sets osfile name for loading of prices
osfile = 'Pricing' + os + '.csv'
# Opens osfile and reads out the instance types
with open(osfile) as csvfile:
reader = csv.DictReader(csvfile)
for row in reader:
print row['Name'] + ",",
print ""
return osfile
def assigninstancetype():
print ""
return raw_input("Instance Type: ")
def calccost(os, osfile, instance):
# Opens osfile and reads through rows
with open(osfile) as csvfile:
reader = csv.DictReader(csvfile)
for row in reader:
# If instance equals row:name
if instance == row['Name']:
# Set variables
instancetype = row['Name']
odhfloat = float(row['ODH'])
rhfloat = float(row['RH'])
# Print calculations
print "A " + os + " " + instancetype + " costs $" + str(odhfloat) + " an hour." + " $" + \
str(odhfloat * 24) + " a day." + " $" + str(odhfloat * 24 * 30) + " a month." + \
" $" + str(odhfloat * 24 * 30 * 12) + " a year."
print "Using Reserved pricing a " + os + " " + instancetype + " costs $" + str(rhfloat) + " an hour." + " $" + \
str(rhfloat * 24) + " a day." + " $" + str(rhfloat * 24 * 30) + " a month." + \
" $" + str(rhfloat * 24 * 30 * 12) + " a year."
print "With a savings of $" + str(odhfloat - rhfloat) + " an hour at " + \
str(round(rhfloat / odhfloat, 2) * 100) + "%."
print ""
# Test Function
def test():
print 'Testing Linux OS type'
os = 'Linux'
testosinput(os)
osfile = printinstances(os)
print 'Testing m3.medium instance type'
instance = 'm3.medium'
calccost(os, osfile, instance)
print 'Testing Windows OS type'
os = 'Windows'
testosinput(os)
osfile = printinstances(os)
print 'Testing m2.xlarge instance type'
instance = 'm2.xlarge'
calccost(os, osfile, instance)
print 'Testing faulty OS type'
os = 'Test'
testosinput(os)
# Main Function
def main():
os = osinput()
testosinput(os)
osfile = printinstances(os)
# Sets Instance type
instance = assigninstancetype()
calccost(os, osfile, instance)
# Rerun main?
main()
# Call main
if __name__ == '__main__':
test()
# main()