aws-rikanjo 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: db2d3d01c61253d1f8db3b929ed67d9a93109e28
4
+ data.tar.gz: 78ca9a220637d4d719eea99fea9a4c7ca9f62d61
5
+ SHA512:
6
+ metadata.gz: d83cb8bf705f0696673e4327266cc1dfb3c02899818ad537ff93b5ba0a44bcd9ac45a798cd45a6ed2fa248dfa8ab7504839b41d132f55dac0978a8cc9381deb4
7
+ data.tar.gz: e1681fb5fbb41a63f5c86758e40cc43a09be31f7524a7b68370d66166058269e7181162c3c21f91ce2d9e7e8bc9656c4061cf783adbdb89f65ac8cbb19f4d5cf
data/.gitignore ADDED
@@ -0,0 +1,17 @@
1
+ *.gem
2
+ *.rbc
3
+ .bundle
4
+ .config
5
+ .yardoc
6
+ Gemfile.lock
7
+ InstalledFiles
8
+ _yardoc
9
+ coverage
10
+ doc/
11
+ lib/bundler/man
12
+ pkg
13
+ rdoc
14
+ spec/reports
15
+ test/tmp
16
+ test/version_tmp
17
+ tmp
data/Gemfile ADDED
@@ -0,0 +1,9 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in aws-rikanjo.gemspec
4
+ gemspec
5
+
6
+ gem 'net/http'
7
+ gem 'uri'
8
+ gem 'yajl'
9
+ gem 'date'
data/LICENSE.txt ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2014 kenjiskywalker
2
+
3
+ MIT License
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining
6
+ a copy of this software and associated documentation files (the
7
+ "Software"), to deal in the Software without restriction, including
8
+ without limitation the rights to use, copy, modify, merge, publish,
9
+ distribute, sublicense, and/or sell copies of the Software, and to
10
+ permit persons to whom the Software is furnished to do so, subject to
11
+ the following conditions:
12
+
13
+ The above copyright notice and this permission notice shall be
14
+ included in all copies or substantial portions of the Software.
15
+
16
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,49 @@
1
+ # Aws::Rikanjo
2
+
3
+ RI Cost Calc Tool.
4
+
5
+ ## Installation
6
+
7
+ Add this line to your application's Gemfile:
8
+
9
+ gem 'aws-rikanjo'
10
+
11
+ And then execute:
12
+
13
+ $ bundle
14
+
15
+ Or install it yourself as:
16
+
17
+ $ gem install aws-rikanjo
18
+
19
+ ## Usage
20
+
21
+ example
22
+
23
+ ```
24
+ require "./aws-rikanjo/lib/aws/rikanjo"
25
+
26
+ a = Aws::RiKanjoo.new(region = "ap-northeast-1", instance_type = "m1.large", ri_util = "light")
27
+ a.total_cost_year
28
+
29
+ # "region" : ap-northeast-1
30
+ # "instance_type" : m1.large
31
+ # "ri_util" : light
32
+ # "discont percent (percent)" : 32.34
33
+ # "ondemand hour price (doller)" : 0.350
34
+ # "reserved hour price (doller)" : 0.206
35
+ # "ondemand year price (doller)" : 3066.0
36
+ # "reserved year price (doller)" : 2074.56
37
+ # "reserved upfront (doller)" : 270
38
+ # "sweet spot day (day)" : 78
39
+ # "sweet spot date (date)" : 2014-07-16
40
+ # "sweet spot price (doller)" : 660.58
41
+ ```
42
+
43
+ ## Contributing
44
+
45
+ 1. Fork it
46
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
47
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
48
+ 4. Push to the branch (`git push origin my-new-feature`)
49
+ 5. Create new Pull Request
data/Rakefile ADDED
@@ -0,0 +1 @@
1
+ require "bundler/gem_tasks"
@@ -0,0 +1,23 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'aws/rikanjo/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "aws-rikanjo"
8
+ spec.version = "0.0.1"
9
+ spec.authors = ["kenjiskywalker"]
10
+ spec.email = ["git@kenjiskywalker.org"]
11
+ spec.description = %q{AWS RI Cost Calc Tool}
12
+ spec.summary = %q{calc 1 year cost. check sweet spot}
13
+ spec.homepage = "https://github.com/kenjiskywalker/aws-rikanjo"
14
+ spec.license = "MIT"
15
+
16
+ spec.files = `git ls-files`.split($/)
17
+ spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
18
+ spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
19
+ spec.require_paths = ["lib"]
20
+
21
+ spec.add_development_dependency "bundler", "~> 1.3"
22
+ spec.add_development_dependency "rake"
23
+ end
@@ -0,0 +1,202 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require 'net/http'
4
+ require 'uri'
5
+ require 'yajl'
6
+ require 'date'
7
+
8
+ module Aws
9
+ class RiKanjoo
10
+ attr_reader :region, :instance_type
11
+
12
+ def initialize(region="us-west", instance_type="t1.micro", ri_util="medium")
13
+ @region = region
14
+ @instance_type = instance_type
15
+ @ri_util = ri_util
16
+ @om_info = Hash.new
17
+ @ri_info = Hash.new
18
+ end
19
+
20
+ def om_get_hr_price
21
+ region = @region
22
+ instance_type = @instance_type
23
+ json = nil
24
+ ondemandjson_data = nil
25
+
26
+ # not same ri region
27
+ case region
28
+ when "us-west-1"
29
+ region = "us-west"
30
+ when "eu-west-1"
31
+ region = "eu-ireland"
32
+ when "ap-southeast-1"
33
+ region = "apac-sin"
34
+ when "ap-northeast-1"
35
+ region = "apac-tokyo"
36
+ when "ap-southeast-2"
37
+ region = "apac-syd"
38
+ when "sa-east-1"
39
+ region = "sa-east-1"
40
+ end
41
+
42
+ # TODO: merge om and ri
43
+ uri = URI.parse('http://aws-assets-pricing-prod.s3.amazonaws.com/pricing/ec2/linux-od.js')
44
+ Net::HTTP.start(uri.host, uri.port) do |http|
45
+ response = http.get(uri.request_uri)
46
+ json = response.body
47
+ end
48
+
49
+ # parse
50
+ json = json.gsub(/^callback\(\n /,'').gsub(/\n\)/, '')
51
+ ondemandjson_data = Yajl::Parser.parse(json)
52
+
53
+ # select 'region' and 'instance_type'
54
+ ondemandjson_data["config"]["regions"].each do |r|
55
+ # r = {"region"=>"us-east", "instanceTypes"=>[{"type"=>"generalCurrentGen", ...
56
+ next unless r["region"] == region
57
+ r["instanceTypes"].each do |type|
58
+ # type = {"type"=>"generalCurrentGen", "sizes"=>[{"size"=>"m3.medium", "vCPU"=>"1" ...
59
+ type["sizes"].each do |i|
60
+ next unless i["size"] == instance_type
61
+ @om_info = {:hr_price => i["valueColumns"][0]["prices"]["USD"] }
62
+ end
63
+ end
64
+ end
65
+
66
+ # :hr_price => price
67
+ return @om_info
68
+ end
69
+
70
+ def ri_get_hr_price_and_upfront
71
+ region = @region
72
+
73
+ # not same om region
74
+ case region
75
+ when "us-west"
76
+ region = "us-west-1"
77
+ when "eu-ireland"
78
+ region = "eu-west-1"
79
+ when "apac-sin"
80
+ region = "ap-southeast-1"
81
+ when "apac-tokyo"
82
+ region = "ap-northeast-1"
83
+ when "apac-syd"
84
+ region = "ap-southeast-2"
85
+ when "sa-east-1"
86
+ region = "sa-east-1"
87
+ end
88
+
89
+ get_ri_price
90
+ end
91
+
92
+ def get_ri_price
93
+ ri_util = @ri_util
94
+
95
+ region = @region
96
+ instance_type = @instance_type
97
+ json = nil
98
+ reservedjson_data = nil
99
+
100
+ uri = URI.parse("http://aws-assets-pricing-prod.s3.amazonaws.com/pricing/ec2/linux-ri-#{ri_util}.js")
101
+ Net::HTTP.start(uri.host, uri.port) do |http|
102
+ response = http.get(uri.request_uri)
103
+ json = response.body
104
+ end
105
+
106
+ json = json.gsub(/callback\(\n/,'').gsub(/\n\)/, '')
107
+ reservedjson_data = Yajl::Parser.parse(json)
108
+
109
+ reservedjson_data["config"]["regions"].each do |r|
110
+ next unless r["region"] == region
111
+ r["instanceTypes"].each do |type|
112
+ type["sizes"].each do |i|
113
+ next unless i["size"] == instance_type
114
+
115
+ i["valueColumns"].each do |y|
116
+ case y["name"]
117
+ when "yrTerm1"
118
+ @ri_info[ri_util] = {:upfront => y["prices"]["USD"]}
119
+ when "yrTerm1Hourly"
120
+ @ri_info[ri_util].store(:hr_price, y["prices"]["USD"])
121
+ end
122
+ end
123
+ end
124
+ end
125
+ end
126
+ # exp. {"light"=>{:upfront=>"270", :hr_price=>"0.206"}}
127
+ return @ri_info
128
+ end
129
+
130
+ def calc_year_cost
131
+
132
+ om_hr_price = @om_info[:hr_price].to_f
133
+ ri_hr_price = @ri_info[@ri_util][:hr_price].to_f
134
+ upfront = @ri_info[@ri_util][:upfront].to_f
135
+
136
+ om_day_paid = (om_hr_price * 24).to_f
137
+ ri_day_paid = (ri_hr_price * 24).to_f
138
+
139
+ sum_om_price = om_day_paid
140
+ sum_ri_price = ri_day_paid + upfront
141
+ 365.times.each do |d|
142
+ d = d + 1
143
+ sum_om_price = sum_om_price + om_day_paid
144
+ sum_ri_price = sum_ri_price + ri_day_paid
145
+
146
+ if sum_ri_price < sum_om_price
147
+ @ri_info[@ri_util].store(:sweet_spot_price, sum_ri_price.round(2))
148
+ @ri_info[@ri_util].store(:sweet_spot_start_day, d)
149
+ break
150
+ end
151
+ end
152
+ total_om_price = (om_day_paid * 365).round(2)
153
+ @om_info.store(:yr_price, total_om_price)
154
+ total_ri_price = ((ri_day_paid * 365) + upfront).round(2)
155
+ @ri_info[@ri_util].store(:yr_price, total_ri_price)
156
+
157
+ # exp. @om_info = {:hr_price=>"0.350", :yr_price=>3066.0}
158
+ # exp. @ri_info = {"ri_util"=>{:upfront=>"NNN", :hr_price=>"NNN",
159
+ # :sweet_spot_price=>NNN, :sweet_spot_start_day=>78, :yr_price=>2074.56}}
160
+
161
+ return @om_info, @ri_info
162
+ end
163
+
164
+ def total_cost_year
165
+ om_get_hr_price
166
+ ri_get_hr_price_and_upfront
167
+ calc_year_cost
168
+
169
+ discount_per = (100 - ((@ri_info[@ri_util][:yr_price] / @om_info[:yr_price]) * 100)).round(2)
170
+ sweet_spot_date = Date.today + @ri_info[@ri_util][:sweet_spot_start_day]
171
+
172
+ puts "\"region\" : #{@region}"
173
+ puts "\"instance_type\" : #{@instance_type}"
174
+ puts "\"ri_util\" : #{@ri_util}"
175
+ puts "\"discont percent (percent)\" : #{discount_per}"
176
+ puts "\"ondemand hour price (doller)\" : #{@om_info[:hr_price]}"
177
+ puts "\"reserved hour price (doller)\" : #{@ri_info[@ri_util][:hr_price]}"
178
+ puts "\"ondemand year price (doller)\" : #{@om_info[:yr_price]}"
179
+ puts "\"reserved year price (doller)\" : #{@ri_info[@ri_util][:yr_price]}"
180
+ puts "\"reserved upfront (doller)\" : #{@ri_info[@ri_util][:upfront]}"
181
+ puts "\"sweet spot day (day)\" : #{@ri_info[@ri_util][:sweet_spot_start_day]}"
182
+ puts "\"sweet spot date (date)\" : #{sweet_spot_date}"
183
+ puts "\"sweet spot price (doller)\" : #{@ri_info[@ri_util][:sweet_spot_price]}"
184
+ end
185
+ end
186
+ end
187
+
188
+ # m = Aws::RiKanjoo.new(region = "ap-northeast-1", instance_type = "m1.large", ri_util = "light")
189
+ # m.total_cost_year
190
+ #
191
+ # "region" : ap-northeast-1
192
+ # "instance_type" : m1.large
193
+ # "ri_util" : light
194
+ # "discont percent (percent)" : 32.34
195
+ # "ondemand hour price (doller)" : 0.350
196
+ # "reserved hour price (doller)" : 0.206
197
+ # "ondemand year price (doller)" : 3066.0
198
+ # "reserved year price (doller)" : 2074.56
199
+ # "reserved upfront (doller)" : 270
200
+ # "sweet spot day (day)" : 78
201
+ # "sweet spot date (date)" : 2014-07-17
202
+ # "sweet spot price (doller)" : 660.58
@@ -0,0 +1,5 @@
1
+ module Aws
2
+ module Rikanjo
3
+ VERSION = "0.0.1"
4
+ end
5
+ end
metadata ADDED
@@ -0,0 +1,80 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: aws-rikanjo
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - kenjiskywalker
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2014-04-29 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: bundler
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ~>
18
+ - !ruby/object:Gem::Version
19
+ version: '1.3'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ~>
25
+ - !ruby/object:Gem::Version
26
+ version: '1.3'
27
+ - !ruby/object:Gem::Dependency
28
+ name: rake
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - '>='
32
+ - !ruby/object:Gem::Version
33
+ version: '0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - '>='
39
+ - !ruby/object:Gem::Version
40
+ version: '0'
41
+ description: AWS RI Cost Calc Tool
42
+ email:
43
+ - git@kenjiskywalker.org
44
+ executables: []
45
+ extensions: []
46
+ extra_rdoc_files: []
47
+ files:
48
+ - .gitignore
49
+ - Gemfile
50
+ - LICENSE.txt
51
+ - README.md
52
+ - Rakefile
53
+ - aws-rikanjo.gemspec
54
+ - lib/aws/rikanjo.rb
55
+ - lib/aws/rikanjo/version.rb
56
+ homepage: https://github.com/kenjiskywalker/aws-rikanjo
57
+ licenses:
58
+ - MIT
59
+ metadata: {}
60
+ post_install_message:
61
+ rdoc_options: []
62
+ require_paths:
63
+ - lib
64
+ required_ruby_version: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - '>='
67
+ - !ruby/object:Gem::Version
68
+ version: '0'
69
+ required_rubygems_version: !ruby/object:Gem::Requirement
70
+ requirements:
71
+ - - '>='
72
+ - !ruby/object:Gem::Version
73
+ version: '0'
74
+ requirements: []
75
+ rubyforge_project:
76
+ rubygems_version: 2.1.11
77
+ signing_key:
78
+ specification_version: 4
79
+ summary: calc 1 year cost. check sweet spot
80
+ test_files: []