This repository has been archived by the owner on Aug 5, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 17
/
Copy pathpricing_table.rb
executable file
·129 lines (98 loc) · 3.58 KB
/
pricing_table.rb
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
#!/usr/bin/env ruby
require 'net/http'
require 'execjs'
require 'json'
# http://aws-assets-pricing-prod.s3.amazonaws.com/pricing/ec2/linux-od.js
# http://aws-assets-pricing-prod.s3.amazonaws.com/pricing/linux-od.js
# http://aws-assets-pricing-prod.s3.amazonaws.com/pricing/ec2/linux-ri-light.js
# http://aws-assets-pricing-prod.s3.amazonaws.com/pricing/ec2/linux-ri-medium.js
# http://aws-assets-pricing-prod.s3.amazonaws.com/pricing/ec2/linux-ri-heavy.js
# http://aws-assets-pricing-prod.s3.amazonaws.com/pricing/ec2/mswin-od.js
# http://aws-assets-pricing-prod.s3.amazonaws.com/pricing/ec2/mswin-ri-light.js
# http://aws-assets-pricing-prod.s3.amazonaws.com/pricing/ec2/mswin-ri-medium.js
# http://aws-assets-pricing-prod.s3.amazonaws.com/pricing/ec2/mswin-ri-heavy.js
BASE_URL = 'http://a0.awsstatic.com/pricing/1/ec2'
def conv_region(region)
{
'us-east' => 'us-east-1',
'us-west-2' => 'us-west-2',
'us-west' => 'us-west-1',
'us-west-1' => 'us-west-1',
'eu-ireland' => 'eu-west-1',
'eu-west-1' => 'eu-west-1',
'apac-sin' => 'ap-southeast-1',
'ap-southeast-1' => 'ap-southeast-1',
'apac-tokyo' => 'ap-northeast-1',
'ap-northeast-1' => 'ap-northeast-1',
'apac-syd' => 'ap-southeast-2',
'ap-southeast-2' => 'ap-southeast-2',
'sa-east-1' => 'sa-east-1',
}.fetch(region)
end
def pricing_table(os, type)
type = type.to_s.gsub('_', '-')
url = "#{BASE_URL}/#{os}-#{type}.min.js"
url = URI.parse(url)
body = Net::HTTP.start(url.host, url.port) {|http| http.get(url.path).body }
ExecJS.eval(body.gsub(/\A.*callback\s*\((.*)\)\s*;?\s*\Z/m) { $1 })
end
require 'pp'
def ondemand_sheets(os)
buf = {}
pricing_table(os, :od)['config']['regions'].each do |region_h|
region = conv_region(region_h['region'])
instance_types = region_h['instanceTypes']
instance_types.each do |instance_type_h|
instance_type_h['sizes'].each do |size_h|
size = size_h['size']
buf[region] ||= {}
buf[region][size] = size_h['valueColumns'].first['prices']['USD'].to_f
end
end
end
return buf
end
def ondemand_sheets_js(varname, os)
buf = ondemand_sheets(os)
"var #{varname} = " + JSON.pretty_generate(buf).strip + ';'
end
def ri_sheets(os, weight)
buf = {}
pricing_table(:linux, "ri_#{weight}")['config']['regions'].each do |region_h|
region = conv_region(region_h['region'])
instance_types = region_h['instanceTypes']
instance_types.each do |instance_type_h|
instance_type_h['sizes'].each do |size_h|
size = size_h['size']
buf[region] ||= {}
buf[region][size] = []
size_h['valueColumns'].map do |value_columns_h|
buf[region][size] << value_columns_h['prices']['USD'].to_f
end
buf[region][size] = "%" + buf[region][size].inspect + "%"
end
end
end
return buf
end
def ri_sheets_js(varname, os, weight)
buf = ri_sheets(os, weight)
buf = JSON.pretty_generate(buf).strip
buf = buf.gsub(/"%(\[[^%]+\])%"/) { $1 }
JSON.parse(buf)
"var #{varname} = " + buf + ';'
end
puts <<-EOS
// On-Demand Instances
#{ondemand_sheets_js '__calcLinuxMonthlyAmount__rateSheets', :linux}
#{ondemand_sheets_js '__calcWindowsMonthlyAmount__rateSheets', :mswin}
EOS
%w(Light Medium Heavy).each do |weight|
linux_varname = "__calc#{weight}RILinuxMonthlyAmount__rateSheets"
mswin_varname = "__calc#{weight}RIWindowsMonthlyAmount__rateSheets"
puts <<-EOS
// Reserved Instances (#{weight})
#{ri_sheets_js linux_varname, :linux, weight.downcase}
#{ri_sheets_js mswin_varname, :mswin, weight.downcase}
EOS
end