aws-rikanjo 0.0.8 → 0.0.9

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: f5befef7edf0fddafadcdc3beaa309c6d0f71df8
4
- data.tar.gz: fa83ff4aed6413b0585a65da199529ae84627585
3
+ metadata.gz: 8953135308c2709a6b820c95d653585028303427
4
+ data.tar.gz: 79cbab1f693c606da4383cd12a0b377c5a038d5e
5
5
  SHA512:
6
- metadata.gz: d4c6e04838a15bd76a5c08ebeabddbd35a8734c29826e2475483faff98bd1f89feae8b06b570fd0c53c53dbdccae3224c2cad436091b4d596b92c19c01a0bf70
7
- data.tar.gz: c98edc88c7502955c1fee87ce792b2b9411a883b4a5c8bb58fa66f3057ae12574e6190222d0cfd1fbbfa819e56b5830d2212c079606fd2da19194360e826bbf4
6
+ metadata.gz: 890ff68ab047bfc5f827551766c9b88af6ce2f3bb003a8ea440fab619a1fd60ea47bb17d36553177b1109f5461110f2cb479b84d107d50beee373a98208f938e
7
+ data.tar.gz: 7a762a60a45a02b354c9ef0acb68032e1b529795acfec1187692e34562d9a06147465cb1ad02f79417514a07b9951242be080e15ef247d9e587858e303ab9448
@@ -0,0 +1,6 @@
1
+ LineLength:
2
+ Description: 'Limit lines to 130 characters.'
3
+ Max: 130
4
+
5
+ RedundantBegin
6
+ Enabled: false
@@ -0,0 +1,8 @@
1
+ language: ruby
2
+
3
+ rvm:
4
+ - 2.0.0
5
+ - 2.1.1
6
+
7
+ gemfile:
8
+ - Gemfile
data/Gemfile CHANGED
@@ -2,9 +2,3 @@ source 'https://rubygems.org'
2
2
 
3
3
  # Specify your gem's dependencies in aws-rikanjo.gemspec
4
4
  gemspec
5
-
6
- gem 'net/http'
7
- gem 'uri'
8
- gem 'yajl'
9
- gem 'date'
10
- gem 'rspec'
data/README.md CHANGED
@@ -1,5 +1,4 @@
1
- # Aws::Rikanjo
2
-
1
+ # Aws::Rikanjo [![Build Status](https://travis-ci.org/kenjiskywalker/aws-rikanjo.svg?branch=master)](https://travis-ci.org/kenjiskywalker/aws-rikanjo)
3
2
  RI Cost Calc Tool( Only new price ).
4
3
 
5
4
  ## Installation
@@ -23,20 +22,22 @@ example
23
22
  ```
24
23
  require "aws/rikanjo"
25
24
 
26
- a = Aws::RiKanjoo.new(region = "ap-northeast-1", instance_type = "m3.large", ri_util = "medium")
25
+ a = Aws::RiKanjoo::Base.new('ec2', 'ap-northeast-1', 'm3.xlarge', 'medium')
27
26
  a.total_cost_year
28
- # "region" : ap-northeast-1
29
- # "instance_type" : m3.large
30
- # "ri_util" : medium
31
- # "discont percent (percent)" : 35.09
32
- # "ondemand hour price (doller)" : 0.203
33
- # "reserved hour price (doller)" : 0.086
34
- # "ondemand year price (doller)" : 1778.28
35
- # "reserved year price (doller)" : 1154.36
36
- # "reserved upfront (doller)" : 401
37
- # "sweet spot day (day)" : 142
38
- # "sweet spot date (date)" : 2014-09-19
39
- # "sweet spot price (doller)" : 696.15
27
+ {
28
+ "sweet spot price (doller)": "269.08",
29
+ "sweet spot date (date)": "2015-02-17",
30
+ "sweet spot day (day)": "254",
31
+ "reserved upfront (doller)": "61",
32
+ "region": "us-east-1",
33
+ "instance_type": "m1.small",
34
+ "ri_util": "light",
35
+ "discont percent (percent)": "6.9",
36
+ "ondemand hour price (doller)": "0.044",
37
+ "reserved hour price (doller)": "0.034",
38
+ "ondemand year price (doller)": "385.44",
39
+ "reserved year price (doller)": "358.84"
40
+ }
40
41
  ```
41
42
 
42
43
  ## CLI Usage
@@ -51,6 +52,7 @@ Usage: rikanjo ec2/rds [options]
51
52
  -r, --region=VALUE specify aws-region (us-east-1/us-west-1/us-west-2/eu-west-1/ap-southeast-1/ap-northeast-1/ap-southeast-2/sa-east-1)
52
53
  -t, --instance_type=VALUE specify ec2-instance-type
53
54
  -u, --ri_util=VALUE specify ri-util (light/medium/heavy)
55
+ -j, --output_json specify output_json flg
54
56
  -h, --help
55
57
  ```
56
58
 
@@ -71,6 +73,7 @@ Usage: rikanjo ec2/rds [options]
71
73
  -r, --region=VALUE specify aws-region (us-east-1/us-west-1/us-west-2/eu-west-1/ap-southeast-1/ap-northeast-1/ap-southeast-2/sa-east-1)
72
74
  -t, --instance_type=VALUE specify ec2-instance-type
73
75
  -u, --ri_util=VALUE specify ri-util (light/medium/heavy)
76
+ -j, --output_json specify output_json flg
74
77
  -h, --help
75
78
  ```
76
79
 
@@ -4,22 +4,25 @@ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
4
  require 'aws/rikanjo/version'
5
5
 
6
6
  Gem::Specification.new do |spec|
7
- spec.name = "aws-rikanjo"
8
- spec.version = "0.0.8"
9
- spec.authors = ["kenjiskywalker"]
10
- spec.email = ["git@kenjiskywalker.org"]
7
+ spec.name = 'aws-rikanjo'
8
+ spec.version = '0.0.9'
9
+ spec.authors = ['kenjiskywalker']
10
+ spec.email = ['git@kenjiskywalker.org']
11
11
  spec.description = %q{AWS RI Cost Calc Tool}
12
12
  spec.summary = %q{calc 1 year cost. check sweet spot}
13
- spec.homepage = "https://github.com/kenjiskywalker/aws-rikanjo"
14
- spec.license = "MIT"
13
+ spec.homepage = 'https://github.com/kenjiskywalker/aws-rikanjo'
14
+ spec.license = 'MIT'
15
15
 
16
16
  spec.files = `git ls-files`.split($/)
17
17
  spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
18
18
  spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
19
- spec.require_paths = ["lib"]
19
+ spec.require_paths = ['lib']
20
20
 
21
- spec.add_development_dependency "bundler", "~> 1.3"
22
- spec.add_development_dependency "rake"
21
+ spec.add_development_dependency 'bundler', '~> 1.3'
22
+ spec.add_development_dependency 'rake'
23
+ spec.add_development_dependency 'rspec', '~> 2.0'
24
+ spec.add_dependency 'rake'
25
+ spec.add_dependency 'yajl-ruby'
23
26
 
24
27
  spec.bindir = 'bin'
25
28
  spec.executables << 'rikanjo'
@@ -1,78 +1,6 @@
1
1
  #!/usr/bin/env ruby
2
+ $LOAD_PATH.unshift File.join(File.dirname(__FILE__), '..', 'lib')
2
3
 
3
- $:.unshift(File.join(File.dirname(File.dirname(__FILE__)), 'lib'))
4
+ require 'aws/rikanjo/cli'
4
5
 
5
- require 'rubygems'
6
- require 'optparse'
7
-
8
- options = { }
9
-
10
- optparse = OptionParser.new do |opts|
11
-
12
- opts.banner = "Usage: rikanjo ec2/rds [options]"
13
-
14
- # subcommand
15
- mode = ARGV.shift
16
- if mode == 'ec2'
17
- elsif mode == 'rds'
18
- opts.on("--multiaz", "enable multi-az") do |value|
19
- options[:multiaz] = value
20
- end
21
- else
22
- $stderr.puts "no such subcommand: #{mode}"
23
- puts opts
24
- exit 1
25
- end
26
- options[:mode] = mode
27
-
28
- region_values = %w[us-east-1 us-west-1 us-west-2 eu-west-1 ap-southeast-1 ap-northeast-1 ap-southeast-2 sa-east-1 ]
29
- opts.on("-r", "--region=VALUE", region_values, "specify aws-region (#{region_values.join('/')})") do |value|
30
- options[:region] = value
31
- end
32
-
33
- opts.on("-t", "--instance_type=VALUE", "specify ec2-instance-type") do |value|
34
- options[:instance_type] = value
35
- end
36
-
37
- ri_util_values = %w[light medium heavy]
38
- opts.on("-u", "--ri_util=VALUE", ri_util_values, "specify ri-util (#{ri_util_values.join('/')})") do |value|
39
- options[:ri_util] = value
40
- end
41
-
42
- opts.on("-h", "--help") do
43
- puts opts
44
- exit
45
- end
46
-
47
- end
48
-
49
-
50
-
51
- # validation
52
- begin
53
- optparse.parse!
54
- require_args = [:region, :instance_type, :ri_util]
55
- error_args = require_args.select{ |param| options[param].nil? }
56
- if not error_args.empty?
57
- puts "require arguments: #{error_args.join(', ')}"
58
- puts
59
- puts optparse
60
- exit
61
- end
62
- rescue
63
- puts $!.to_s
64
- puts
65
- puts optparse
66
- exit
67
- end
68
-
69
- # rikanjo
70
- require "aws/rikanjo"
71
- a = Aws::RiKanjoo.new(
72
- mode = options[:mode],
73
- region = options[:region],
74
- instance_type = options[:instance_type],
75
- ri_util = options[:ri_util],
76
- multiaz = options[:multiaz],
77
- )
78
- a.total_cost_year
6
+ Aws::RiKanjoo::CLI.new.start
@@ -1,133 +1,13 @@
1
1
  #!/usr/bin/env ruby
2
2
 
3
- require 'net/http'
4
- require 'uri'
5
- require 'yajl'
6
- require 'date'
7
- require_relative 'rikanjo/mode/ec2'
8
- require_relative 'rikanjo/mode/rds'
3
+ require 'aws/rikanjo/base'
4
+ require 'aws/rikanjo/ec2'
5
+ require 'aws/rikanjo/rds'
6
+ require 'aws/rikanjo/calc'
9
7
 
10
8
  module Aws
11
- class RiKanjoo
12
- attr_reader :region, :instance_type, :ri_util
13
-
14
- def initialize(mode='ec2', region, instance_type, ri_util, multiaz)
15
- if mode == 'ec2'
16
- @mode_class = Aws::RiKanjoo::Mode::Ec2.new(region, instance_type, ri_util)
17
- elsif mode == 'rds'
18
- @mode_class = Aws::RiKanjoo::Mode::Rds.new(region, instance_type, ri_util, multiaz)
19
- end
20
- @region = region
21
- @instance_type = instance_type
22
- @ri_util = ri_util
23
- @om_info = Hash.new
24
- @ri_info = Hash.new
25
- end
26
-
27
- def om_get_hr_price
28
- # TODO: merge om and ri
29
- uri = URI.parse("#{@mode_class.price_url}/#{@mode_class.om_price_file}")
30
- contents = Net::HTTP.get(uri)
31
-
32
- # parse om info
33
- @om_info = @mode_class.om_price_from_contents(contents)
34
- end
35
-
36
- def ri_get_hr_price_and_upfront
37
- uri = URI.parse("#{@mode_class.price_url}/#{@mode_class.ri_price_file}")
38
- contents = Net::HTTP.get(uri)
39
-
40
- # parse ri info
41
- @ri_info = @mode_class.ri_price_from_contents(contents)
9
+ module RiKanjoo
10
+ class Base
42
11
  end
43
-
44
- def calc_year_cost
45
-
46
- om_hr_price = @om_info[:hr_price].to_f
47
- ri_hr_price = @ri_info[@ri_util][:hr_price].to_f
48
- upfront = @ri_info[@ri_util][:upfront].to_f
49
-
50
- om_day_paid = (om_hr_price * 24).to_f
51
- ri_day_paid = (ri_hr_price * 24).to_f
52
-
53
- sum_om_price = om_day_paid
54
- sum_ri_price = ri_day_paid + upfront
55
- 365.times.each do |d|
56
- d = d + 1
57
- sum_om_price = sum_om_price + om_day_paid
58
- sum_ri_price = sum_ri_price + ri_day_paid
59
-
60
- if sum_ri_price < sum_om_price
61
- @ri_info[@ri_util].store(:sweet_spot_price, sum_ri_price.round(2))
62
- @ri_info[@ri_util].store(:sweet_spot_start_day, d)
63
- break
64
- end
65
- end
66
- total_om_price = (om_day_paid * 365).round(2)
67
- @om_info.store(:yr_price, total_om_price)
68
- total_ri_price = ((ri_day_paid * 365) + upfront).round(2)
69
- @ri_info[@ri_util].store(:yr_price, total_ri_price)
70
-
71
- # exp. @om_info = {:hr_price=>"0.350", :yr_price=>3066.0}
72
- # exp. @ri_info = {"ri_util"=>{:upfront=>"NNN", :hr_price=>"NNN",
73
- # :sweet_spot_price=>NNN, :sweet_spot_start_day=>78, :yr_price=>2074.56}}
74
- return @om_info, @ri_info
75
- end
76
-
77
- def total_cost_year
78
- om_get_hr_price
79
- ri_get_hr_price_and_upfront
80
- calc_year_cost
81
-
82
- discount_per = (100 - ((@ri_info[@ri_util][:yr_price] / @om_info[:yr_price]) * 100)).round(2)
83
-
84
- # If the Reserved Instance does not become cheaper than the On-demand Instance.
85
- if @ri_info[@ri_util][:sweet_spot_start_day].nil?
86
- puts "\"Price of the Reserved Instance is higher than the price of On-Demand Instances.\""
87
- puts "\"region\" : #{@region}"
88
- puts "\"instance_type\" : #{@instance_type}"
89
- puts "\"ri_util\" : #{@ri_util}"
90
- puts "\"discont percent (percent)\" : #{discount_per}"
91
- puts "\"ondemand hour price (doller)\" : #{@om_info[:hr_price]}"
92
- puts "\"reserved hour price (doller)\" : #{@ri_info[@ri_util][:hr_price]}"
93
- puts "\"ondemand year price (doller)\" : #{@om_info[:yr_price]}"
94
- puts "\"reserved year price (doller)\" : #{@ri_info[@ri_util][:yr_price]}"
95
- puts "\"reserved upfront (doller)\" : #{@ri_info[@ri_util][:upfront]}"
96
-
97
- exit 1
98
- end
99
-
100
- sweet_spot_date = Date.today + @ri_info[@ri_util][:sweet_spot_start_day]
101
-
102
- puts "\"region\" : #{@region}"
103
- puts "\"instance_type\" : #{@instance_type}"
104
- puts "\"ri_util\" : #{@ri_util}"
105
- puts "\"discont percent (percent)\" : #{discount_per}"
106
- puts "\"ondemand hour price (doller)\" : #{@om_info[:hr_price]}"
107
- puts "\"reserved hour price (doller)\" : #{@ri_info[@ri_util][:hr_price]}"
108
- puts "\"ondemand year price (doller)\" : #{@om_info[:yr_price]}"
109
- puts "\"reserved year price (doller)\" : #{@ri_info[@ri_util][:yr_price]}"
110
- puts "\"reserved upfront (doller)\" : #{@ri_info[@ri_util][:upfront]}"
111
- puts "\"sweet spot day (day)\" : #{@ri_info[@ri_util][:sweet_spot_start_day]}"
112
- puts "\"sweet spot date (date)\" : #{sweet_spot_date}"
113
- puts "\"sweet spot price (doller)\" : #{@ri_info[@ri_util][:sweet_spot_price]}"
114
- end
115
-
116
12
  end
117
13
  end
118
-
119
- # m = Aws::RiKanjoo.new(region = "ap-northeast-1", instance_type = "m3.large", ri_util = "medium")
120
- # m.total_cost_year
121
- #
122
- # "region" : ap-northeast-1
123
- # "instance_type" : m3.large
124
- # "ri_util" : medium
125
- # "discont percent (percent)" : 35.09
126
- # "ondemand hour price (doller)" : 0.203
127
- # "reserved hour price (doller)" : 0.086
128
- # "ondemand year price (doller)" : 1778.28
129
- # "reserved year price (doller)" : 1154.36
130
- # "reserved upfront (doller)" : 401
131
- # "sweet spot day (day)" : 142
132
- # "sweet spot date (date)" : 2014-09-19
133
- # "sweet spot price (doller)" : 696.15
@@ -0,0 +1,40 @@
1
+ require 'aws/rikanjo/ec2'
2
+ require 'aws/rikanjo/rds'
3
+ require 'aws/rikanjo/calc'
4
+
5
+ module Aws
6
+ module RiKanjoo
7
+ class Base < Calc
8
+ attr_reader :region, :instance_type, :ri_util, :multiaz
9
+
10
+ def initialize(mode = 'ec2', region, instance_type, ri_util, multiaz)
11
+ if mode == 'ec2'
12
+ @mode = Aws::RiKanjoo::Ec2.new(region, instance_type, ri_util)
13
+ elsif mode == 'rds'
14
+ @mode = Aws::RiKanjoo::Rds.new(region, instance_type, ri_util, multiaz)
15
+ end
16
+ @region = region
17
+ @instance_type = instance_type
18
+ @ri_util = ri_util
19
+ @om_info = {}
20
+ @ri_info = {}
21
+ end
22
+ end
23
+ end
24
+ end
25
+
26
+ # m = Aws::RiKanjoo.new(region = "ap-northeast-1", instance_type = "m3.large", ri_util = "medium")
27
+ # m.total_cost_year
28
+ #
29
+ # "region" : ap-northeast-1
30
+ # "instance_type" : m3.large
31
+ # "ri_util" : medium
32
+ # "discont percent (percent)" : 35.09
33
+ # "ondemand hour price (doller)" : 0.203
34
+ # "reserved hour price (doller)" : 0.086
35
+ # "ondemand year price (doller)" : 1778.28
36
+ # "reserved year price (doller)" : 1154.36
37
+ # "reserved upfront (doller)" : 401
38
+ # "sweet spot day (day)" : 142
39
+ # "sweet spot date (date)" : 2014-09-19
40
+ # "sweet spot price (doller)" : 696.15
@@ -0,0 +1,103 @@
1
+ require 'net/http'
2
+ require 'uri'
3
+ require 'yajl'
4
+ require 'date'
5
+ require 'json'
6
+
7
+ module Aws
8
+ module RiKanjoo
9
+ class Calc
10
+ def om_get_hr_price
11
+ # TODO: merge om and ri
12
+ uri = URI.parse("#{@mode.price_url}/#{@mode.om_price_file}")
13
+ contents = Net::HTTP.get(uri)
14
+
15
+ # parse om info
16
+ @om_info = @mode.om_price_from_contents(contents)
17
+ end
18
+
19
+ def ri_get_hr_price_and_upfront
20
+ uri = URI.parse("#{@mode.price_url}/#{@mode.ri_price_file}")
21
+ contents = Net::HTTP.get(uri)
22
+
23
+ # parse ri info
24
+ @ri_info = @mode.ri_price_from_contents(contents)
25
+ end
26
+
27
+ def calc_year_cost
28
+ om_hr_price = @om_info[:hr_price].to_f
29
+ ri_hr_price = @ri_info[@ri_util][:hr_price].to_f
30
+ upfront = @ri_info[@ri_util][:upfront].to_f
31
+
32
+ om_day_paid = (om_hr_price * 24).to_f
33
+ ri_day_paid = (ri_hr_price * 24).to_f
34
+
35
+ sum_om_price = om_day_paid
36
+ sum_ri_price = ri_day_paid + upfront
37
+ 365.times.each do |d|
38
+ d += 1
39
+ sum_om_price += om_day_paid
40
+ sum_ri_price += ri_day_paid
41
+
42
+ if sum_ri_price < sum_om_price
43
+ @ri_info[@ri_util].store(:sweet_spot_price, sum_ri_price.round(2))
44
+ @ri_info[@ri_util].store(:sweet_spot_start_day, d)
45
+ break
46
+ end
47
+ end
48
+ total_om_price = (om_day_paid * 365).round(2)
49
+ @om_info.store(:yr_price, total_om_price)
50
+ total_ri_price = ((ri_day_paid * 365) + upfront).round(2)
51
+ @ri_info[@ri_util].store(:yr_price, total_ri_price)
52
+
53
+ # exp. @om_info = {:hr_price=>"0.350", :yr_price=>3066.0}
54
+ # exp. @ri_info = {"ri_util"=>{:upfront=>"NNN", :hr_price=>"NNN",
55
+ # :sweet_spot_price=>NNN, :sweet_spot_start_day=>78, :yr_price=>2074.56}}
56
+ return @om_info, @ri_info
57
+ end
58
+
59
+ def total_cost_year
60
+ om_get_hr_price
61
+ ri_get_hr_price_and_upfront
62
+ calc_year_cost
63
+
64
+ discount_per = (100 - ((@ri_info[@ri_util][:yr_price] / @om_info[:yr_price]) * 100)).round(2)
65
+
66
+ # If the Reserved Instance does not become cheaper than the On-demand Instance.
67
+ if @ri_info[@ri_util][:sweet_spot_start_day].nil?
68
+ message = {
69
+ "Price of the Reserved Instance is higher than the price of On-Demand Instances." => nil,
70
+ "region" => "#{@region}",
71
+ "instance_type" => "#{@instance_type}",
72
+ "ri_util" => "#{@ri_util}",
73
+ "discont percent (percent)" => "#{discount_per}",
74
+ "ondemand hour price (doller)" => "#{@om_info[:hr_price]}",
75
+ "reserved hour price (doller)" => "#{@ri_info[@ri_util][:hr_price]}",
76
+ "ondemand year price (doller)" => "#{@om_info[:yr_price]}",
77
+ "reserved year price (doller)" => "#{@ri_info[@ri_util][:yr_price]}",
78
+ "reserved upfront (doller)" => "#{@ri_info[@ri_util][:upfront]}",
79
+ }
80
+ puts message.to_json
81
+ exit 1
82
+ end
83
+
84
+ sweet_spot_date = Date.today + @ri_info[@ri_util][:sweet_spot_start_day]
85
+ message = {
86
+ "region" => "#{@region}",
87
+ "instance_type" => "#{@instance_type}",
88
+ "ri_util" => "#{@ri_util}",
89
+ "discont percent (percent)" => "#{discount_per}",
90
+ "ondemand hour price (doller)" => "#{@om_info[:hr_price]}",
91
+ "reserved hour price (doller)" => "#{@ri_info[@ri_util][:hr_price]}",
92
+ "ondemand year price (doller)" => "#{@om_info[:yr_price]}",
93
+ "reserved year price (doller)" => "#{@ri_info[@ri_util][:yr_price]}",
94
+ "reserved upfront (doller)" => "#{@ri_info[@ri_util][:upfront]}",
95
+ "sweet spot day (day)" => "#{@ri_info[@ri_util][:sweet_spot_start_day]}",
96
+ "sweet spot date (date)" => "#{sweet_spot_date}",
97
+ "sweet spot price (doller)" => "#{@ri_info[@ri_util][:sweet_spot_price]}",
98
+ }
99
+ return message
100
+ end
101
+ end
102
+ end
103
+ end