sport_ngin_aws_auditor 4.3.1 → 4.3.2

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
- SHA256:
3
- metadata.gz: 272236aaef0f07666be8ffc21084d25f1a3dfebb0cd8e45c7561ac39cb29287c
4
- data.tar.gz: bc5d6bc2324288813bed74beb87aba0bd1de7950282425efff7a42aab2b5c26d
2
+ SHA1:
3
+ metadata.gz: 8749be5c2a1fd6c12d3578215ce3c8bae4f5e223
4
+ data.tar.gz: 38356de4c0dfa296977b9aa8fa0b63bcec51e182
5
5
  SHA512:
6
- metadata.gz: a5546d693f3454601d91584d63e76de0f9e9843bfc45c87976164c1ed27b5e6404c2700b56f93e6ca5bb0d6e126f0d2b773a3df080482eab0bac498e9c7358a0
7
- data.tar.gz: b286a24a59db4dc48fe8a61c508ed4f759c0cead17d051b72d4bdf0f0c5a366b38e09e3b23405e36abd56ff8b30a8bc5a62e702838b878239e6c0c58c8483e68
6
+ metadata.gz: 0566cda91b09fc46124367b4de7fe033b01ee2d3d7ffc16457cfa2e7cee6ef703b985fd1520c3279ce7e6a1b2558e45ce784889ca5e08eba7dd066bdfdc913b0
7
+ data.tar.gz: 28c45d84a058af478ddf9dcbff170fd92e6e1e3989c9fb8049ad92c5f416a1acf382cdbed59b54fa51ff6c58dd03e6296ac08cca8edcf5824d6c5c080eedf5d8
@@ -24,42 +24,12 @@ module SportNginAwsAuditor
24
24
  instance_hash
25
25
  end
26
26
 
27
- def add_region_ris_to_hash(ris_region, differences, klass)
28
- ris_region.each do |ri|
29
- differences.each do |key, value|
30
- # if key = 'Linux VPC us-east-1a t2.medium'...
31
- my_match = key.match(/(\w*\s*\w*\s*)\w{2}-\w{2,}-\w{2}(\s*\S*)/)
32
-
33
- # then platform = 'Linux VPC'...
34
- platform = my_match[1] if my_match
35
- platform[platform.length - 1] = ''
36
-
37
- # and size = 't2.medium'
38
- size = my_match[2] if my_match
39
- size[0] = ''
40
-
41
- if compare_platforms_based_on_klass(klass, platform, ri.platform) &&
42
- (size == ri.instance_type) &&
43
- (value[:count] < 0)
44
- until (ri.count == 0) || (value[:count] == 0)
45
- value[:count] = value[:count] + 1
46
- ri.count = ri.count - 1
47
- end
48
- end
49
- end
50
- end
51
-
52
- ris_region.each do |ri|
53
- differences[ri.to_s] = {:count => ri.count, :region_based => true}
54
- end
55
- end
56
-
57
27
  def add_additional_instances_to_hash(instances_to_add, instance_hash, extra_string)
58
28
  instances_to_add.each do |instance|
59
29
  next if instance.nil?
60
30
  key = "#{instance.to_s.dup}#{extra_string}#{instance.name})"
61
31
  instance_result = {}
62
-
32
+
63
33
  if instance_hash.has_key?(instance.to_s) && instance_hash[instance.to_s][:count] > 0
64
34
  current_val = instance_hash[instance.to_s][:count]
65
35
  val = current_val - instance.count
@@ -109,18 +79,86 @@ module SportNginAwsAuditor
109
79
  return ris_region, ris_hash
110
80
  end
111
81
 
112
- def measure_differences(instance_hash, ris_hash)
82
+ def measure_differences(instance_hash, ris_hash, ris_region, klass)
113
83
  differences = Hash.new()
84
+
85
+ # Setup and zone-based RI calculation
114
86
  instance_hash.keys.concat(ris_hash.keys).uniq.each do |key|
115
87
  instance_count = instance_hash.has_key?(key) ? instance_hash[key][:count] : 0
116
88
  ris_count = ris_hash.has_key?(key) ? ris_hash[key][:count] : 0
117
- differences[key] = {:count => ris_count - instance_count, :region_based => false}
89
+ # positive count => more ris, negative count => more instances
90
+ differences[key] = { count: ris_count - instance_count, region_based: false }
118
91
  end
92
+
93
+ # Region-based RI calculation
94
+ ris_region_processor(differences, ris_region) unless ris_region.empty?
95
+
119
96
  differences
120
97
  end
121
98
 
99
+ # Assuming zone based information is already added and differences hash is initialized
100
+ private def ris_region_processor(differences, ris_region)
101
+ # Group all the same size RIs
102
+ # Ex: ri_group_arr = [{ instance_type: "t2.medium", platform: "Linux VPC", count: 7 }, ...]
103
+ ri_group_arr = group_ris_region(ris_region)
104
+
105
+ # Process each ri_group determined by instance_type and platform
106
+ ri_group_arr.each do |ri_group|
107
+ target_instance_type = ri_group[:instance_type]
108
+ target_platform = ri_group[:platform]
109
+ ri_count = ri_group[:count]
110
+ instances_left = differences.select do |k,v|
111
+ # Ex: k,v = "Linux VPC us-east-1d t2.small", {:count=>-15, :region_based=>false}
112
+ Regexp.new("^#{target_platform}.*#{target_instance_type}$") =~ k
113
+ end
114
+
115
+ instances_left_count = - instances_left.reduce(0) do | previous_count, actual_instance |
116
+ previous_count += actual_instance[1][:count]
117
+ end
118
+
119
+ if instances_left_count > 0
120
+ # Actual instances left > zone RIs
121
+ # Use regional instances
122
+ instances_left.each_value do |v|
123
+ if ri_count >= -v[:count]
124
+ ri_count += v[:count]
125
+ v[:count] = 0
126
+ else
127
+ # No more RIs left
128
+ v[:count] = ri_count + v[:count]
129
+ ri_count = 0
130
+ break;
131
+ end
132
+ end
133
+ end
134
+ differences["#{target_platform} #{target_instance_type}"] = { count: ri_count, region_based: true }
135
+ end
136
+ end
137
+
138
+ private def group_ris_region(ris_region)
139
+ ri_group_arr = []
140
+ ris_region.each do |ec2_ri_obj|
141
+ selected_ri_group = ri_group_arr.select do |ri_group|
142
+ ri_group[:instance_type] == ec2_ri_obj.instance_type && ri_group[:platform] == ec2_ri_obj.platform
143
+ end
144
+ if selected_ri_group.count == 1
145
+ selected_ri_group = selected_ri_group.first
146
+ selected_ri_group[:count] += ec2_ri_obj.count
147
+ elsif selected_ri_group.empty?
148
+ selected_ri_group = {
149
+ instance_type: ec2_ri_obj.instance_type,
150
+ platform: ec2_ri_obj.platform,
151
+ count: ec2_ri_obj.count
152
+ }
153
+ ri_group_arr << selected_ri_group
154
+ else
155
+ raise "More than one group with the same instance size and platform detected: #{selected_ri_group}"
156
+ end
157
+ end
158
+ ri_group_arr
159
+ end
160
+
122
161
  def add_additional_data(ris_region, instances_with_tag, ignored_instances, differences, klass)
123
- add_region_ris_to_hash(ris_region, differences, klass)
124
162
  add_additional_instances_to_hash(instances_with_tag, differences, " with tag (")
125
163
  add_additional_instances_to_hash(ignored_instances, differences, " ignored (")
126
164
  return differences
@@ -129,7 +167,8 @@ module SportNginAwsAuditor
129
167
  def compare(instances, ignore_instances_regexes, client, klass)
130
168
  ignored_instances, instances_with_tag, instance_hash = sort_through_instances(instances, ignore_instances_regexes)
131
169
  ris_region, ris_hash = sort_through_RIs(client)
132
- differences = measure_differences(instance_hash, ris_hash)
170
+ # ris_region is empty for rds and cache, and ris_hash is empty for ec2 (by default)
171
+ differences = measure_differences(instance_hash, ris_hash, ris_region, klass)
133
172
  add_additional_data(ris_region, instances_with_tag, ignored_instances, differences, klass)
134
173
  differences
135
174
  end
@@ -152,12 +191,12 @@ module SportNginAwsAuditor
152
191
  end
153
192
  end
154
193
 
155
- # this gathers all RIs except the region-based RIs
194
+ # this gathers all RIs except the region-based RIs (For RDS and cache)
156
195
  def filter_ris_availability_zone(ris)
157
196
  ris.reject { |ri| ri.scope == 'Region' }
158
197
  end
159
198
 
160
- # this filters all of the region-based RIs
199
+ # this filters all of the region-based RIs (For EC2 by default)
161
200
  def filter_ris_region_based(ris)
162
201
  ris.select { |ri| ri.scope == 'Region' }
163
202
  end
@@ -185,7 +224,7 @@ module SportNginAwsAuditor
185
224
  # this returns a hash of all instances that have retired between 1 week ago and today
186
225
  def get_retired_tags(instances)
187
226
  return_array = []
188
-
227
+
189
228
  instances.select do |instance|
190
229
  value = gather_instance_tag_date(instance)
191
230
  one_week_ago = (Date.today - 7).to_s
@@ -193,7 +232,7 @@ module SportNginAwsAuditor
193
232
  return_array << RecentlyRetiredTag.new(value.to_s, instance.to_s, instance.name, instance.tag_reason)
194
233
  end
195
234
  end
196
-
235
+
197
236
  return_array
198
237
  end
199
238
 
@@ -208,8 +247,8 @@ module SportNginAwsAuditor
208
247
 
209
248
  #################### HELPER METHODS ####################
210
249
 
211
- # If the klass is EC2, then just make sure the instance platform includes the RI platform because
212
- # classic RIs (non-VPC) are used on any instance.
250
+ # If the klass is EC2, then just make sure the instance platform includes the RI platform because
251
+ # classic RIs (non-VPC) are used on any instance.
213
252
  #
214
253
  # Instance | RI | Used?
215
254
  # ----------|-----------|------
@@ -1,3 +1,3 @@
1
1
  module SportNginAwsAuditor
2
- VERSION = "4.3.1"
2
+ VERSION = "4.3.2"
3
3
  end
@@ -79,7 +79,7 @@ module SportNginAwsAuditor
79
79
  allow(@ec2_instance1).to receive(:tag_value).and_return(nil)
80
80
  allow(@ec2_instance2).to receive(:tag_value).and_return(nil)
81
81
  allow(@reserved_ec2_instance1).to receive(:count).and_return(2)
82
- allow(@reserved_ec2_instance2).to receive(:count).and_return(2)
82
+ allow(@reserved_ec2_instance2).to receive(:count).and_return(4)
83
83
  allow(@reserved_ec2_instance1).to receive(:to_s).and_return('Linux VPC us-east-1b t2.small')
84
84
  allow(@reserved_ec2_instance2).to receive(:to_s).and_return('Windows us-east-1b t2.medium')
85
85
  allow(@region_reserved_ec2_instance1).to receive(:platform).and_return('Linux VPC')
@@ -88,8 +88,8 @@ module SportNginAwsAuditor
88
88
  allow(@region_reserved_ec2_instance2).to receive(:platform).and_return('Windows')
89
89
  allow(@region_reserved_ec2_instance2).to receive(:instance_type).and_return('t2.medium')
90
90
  allow(@region_reserved_ec2_instance2).to receive(:count).and_return(4)
91
- allow(@region_reserved_ec2_instance1).to receive(:to_s).and_return('Linux VPC t2.small')
92
- allow(@region_reserved_ec2_instance2).to receive(:to_s).and_return('Windows t2.medium')
91
+ allow(@region_reserved_ec2_instance1).to receive(:to_s).and_return('Linux VPC t2.small')
92
+ allow(@region_reserved_ec2_instance2).to receive(:to_s).and_return('Windows t2.medium')
93
93
  allow(@region_reserved_ec2_instance1).to receive(:count_remaining).and_return(2)
94
94
  allow(@region_reserved_ec2_instance2).to receive(:count_remaining).and_return(2)
95
95
  allow(@region_reserved_ec2_instance1).to receive(:count_remaining=).and_return(2)
@@ -129,27 +129,21 @@ module SportNginAwsAuditor
129
129
  allow(@region_reserved_ec2_instance2).to receive(:count=)
130
130
  instance_hash = klass.instance_count_hash(@ec2_instances)
131
131
  ris = klass.instance_count_hash(@reserved_instances)
132
- differences = Hash.new()
133
- instance_hash.keys.concat(ris.keys).uniq.each do |key|
134
- instance_count = instance_hash.has_key?(key) ? instance_hash[key][:count] : 0
135
- ris_count = ris.has_key?(key) ? ris[key][:count] : 0
136
- differences[key] = {count: ris_count - instance_count, region_based: false}
137
- end
138
- result = klass.add_region_ris_to_hash(@region_reserved_instances, differences, "EC2")
139
- expect(differences).to eq({"Linux VPC us-east-1b t2.small"=>{count: 0, region_based: false}, "Windows us-east-1b t2.medium"=>{count: 0, region_based: false},
140
- "Linux VPC t2.small" => {count: 2, region_based: true}, "Windows t2.medium" => {count: 4, region_based: true}})
132
+ result = klass.measure_differences(instance_hash, ris, @region_reserved_instances, klass)
133
+ expect(result).to eq({"Linux VPC us-east-1b t2.small"=>{count: -1, region_based: false}, "Windows us-east-1b t2.medium"=>{count: 0, region_based: false},
134
+ "Linux VPC t2.small" => {count: 0, region_based: true}, "Windows t2.medium" => {count: 3, region_based: true}})
141
135
  end
142
136
 
143
137
  it 'should factor in the region based RIs into the counting when there are no zone specific RIs' do
144
138
  klass = SportNginAwsAuditor::EC2Instance
145
- allow(@ec2_instance1).to receive(:count).and_return(-2)
139
+ allow(@ec2_instance1).to receive(:count).and_return(2)
146
140
  allow(@ec2_instance2).to receive(:count).and_return(5)
147
141
  allow(@region_reserved_ec2_instance1).to receive(:count=)
148
142
  allow(@region_reserved_ec2_instance2).to receive(:count=)
149
143
  instance_hash = klass.instance_count_hash(@ec2_instances)
150
- result = klass.add_region_ris_to_hash(@region_reserved_instances, instance_hash, "EC2")
151
- expect(instance_hash).to eq({"Linux VPC us-east-1b t2.small"=>{count: 0, region_based: false}, "Windows us-east-1b t2.medium"=>{count: 5, region_based: false},
152
- "Linux VPC t2.small" => {count: 2, region_based: true}, "Windows t2.medium" => {count: 4, region_based: true}})
144
+ result = klass.measure_differences(instance_hash, {}, @region_reserved_instances, klass)
145
+ expect(result).to eq({"Linux VPC us-east-1b t2.small"=>{count: 0, region_based: false}, "Windows us-east-1b t2.medium"=>{count: -1, region_based: false},
146
+ "Linux VPC t2.small" => {count: 0, region_based: true}, "Windows t2.medium" => {count: 0, region_based: true}})
153
147
  end
154
148
  end
155
149
 
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: sport_ngin_aws_auditor
3
3
  version: !ruby/object:Gem::Version
4
- version: 4.3.1
4
+ version: 4.3.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - Elliot Hursh
@@ -10,7 +10,7 @@ authors:
10
10
  autorequire:
11
11
  bindir: bin
12
12
  cert_chain: []
13
- date: 2018-06-19 00:00:00.000000000 Z
13
+ date: 2018-10-23 00:00:00.000000000 Z
14
14
  dependencies:
15
15
  - !ruby/object:Gem::Dependency
16
16
  name: aws-sdk
@@ -285,7 +285,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
285
285
  version: '0'
286
286
  requirements: []
287
287
  rubyforge_project:
288
- rubygems_version: 2.7.5
288
+ rubygems_version: 2.6.14
289
289
  signing_key:
290
290
  specification_version: 4
291
291
  summary: AWS configuration as code