sport_ngin_aws_auditor 3.4.1 → 3.5.0

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
2
  SHA1:
3
- metadata.gz: d049590c2efaa67e2070d04f7882d6a615040e91
4
- data.tar.gz: 6107c00771383ba9886aef453c96e5f823057c14
3
+ metadata.gz: 49a159bfcd78d9a4084f11225a8a8eabf9161d5f
4
+ data.tar.gz: 8fdabe907c57537bb552cbfc974b0e6305da54f2
5
5
  SHA512:
6
- metadata.gz: 9d503ffdeb1338db137c930e9cdfb2e8b73e17f7be293134c22bdfdb9528e10d8af6119a4217f4ced95978fcb2a61c00011ca0755d17995231de1a0c33d7aadf
7
- data.tar.gz: 27f60395d9b9e72bd4abd51ce19e45e2cb90edbfb163bab1e81b3d21e55242a73ebc9e35d113a97fb8715b35693ea123ad107234617197e1654924c951953a91
6
+ metadata.gz: 95a2838c5ba80c0740b31f3875598ecaf085df1e2e8c59c583c3873f34c75c0a1c1b9ea2429f0b734cf2e67e82bbaa6f45a46468645277c2931a024b690c64f0
7
+ data.tar.gz: 2479ae3cc84a567bb5b6ca79c0eadb50863399a870fba1821c529e917f8dc675f77f175da8622da9dbc65c160c40ce4a8b4d798d65798b74c31469cf9b970445
data/CHANGELOG.markdown CHANGED
@@ -1,3 +1,8 @@
1
+ #### v3.5.0
2
+ * Cleaning up slack printouts with the audit command
3
+
4
+ > Emma Sax: Brian Bergstrom: https://github.com/sportngin/sport_ngin_aws_auditor/pull/15
5
+
1
6
  #### v3.4.1
2
7
  #### v3.4.0
3
8
  * Add other RDS engine types
data/README.md CHANGED
@@ -62,7 +62,7 @@ To find discrepancies between number of running instances and purchased instance
62
62
 
63
63
  $ sport-ngin-aws-auditor audit account1
64
64
 
65
- Any running instances that are not matched with a reserved instance with show up as yellow (with the negative number indicating the amount), the reserved instances that are not matched with an running instance will show up in red (with the positive number indicating the amount), and any reserved instances and running instances that match will show up in green. Any instances in blue with asteriks have a special tag that can either be specified in the audit command or will be defaulted to `no-reserved-instance`.
65
+ Any running instances that are not matched with a reserved instance with show up as yellow, the reserved instances that are not matched with a running instance will show up in red, and any reserved instances and running instances that match will show up in green. Any instances in blue with asteriks have a special tag that can either be specified in the audit command or will be defaulted to `no-reserved-instance`.
66
66
 
67
67
  To specify your own tag name, run:
68
68
 
@@ -3,6 +3,7 @@ require_relative 'sport_ngin_aws_auditor/convenience_wrappers'
3
3
  require_relative 'sport_ngin_aws_auditor/ec2_instance'
4
4
  require_relative 'sport_ngin_aws_auditor/rds_instance'
5
5
  require_relative 'sport_ngin_aws_auditor/cache_instance'
6
+ require_relative 'sport_ngin_aws_auditor/instance'
6
7
  require_relative 'sport_ngin_aws_auditor/stack'
7
8
  require_relative 'sport_ngin_aws_auditor/google_sheet'
8
9
  require_relative 'sport_ngin_aws_auditor/output'
@@ -0,0 +1,44 @@
1
+ require_relative './instance_helper'
2
+
3
+ module SportNginAwsAuditor
4
+ class Instance
5
+ extend InstanceHelper
6
+
7
+ attr_accessor :name, :count, :type
8
+ def initialize(name, count)
9
+ if name.include?(" with tag")
10
+ name = name.dup # because name is a frozen string right now
11
+ name.slice!(" with tag")
12
+ self.name = name
13
+ self.type = "tagged"
14
+ else
15
+ self.name = name
16
+ if count < 0
17
+ self.type = "running"
18
+ elsif count == 0
19
+ self.type = "matched"
20
+ elsif count > 0
21
+ self.type = "reserved"
22
+ end
23
+ end
24
+
25
+ self.count = count.abs
26
+ end
27
+
28
+ def tagged?
29
+ self.type == "tagged"
30
+ end
31
+
32
+ def reserved?
33
+ self.type == "reserved"
34
+ end
35
+
36
+ def running?
37
+ self.type == "running"
38
+ end
39
+
40
+ def matched?
41
+ self.type == "matched"
42
+ end
43
+ end
44
+ end
@@ -1,11 +1,12 @@
1
- require 'slack-notifier'
1
+ require 'httparty'
2
2
 
3
3
  module SportNginAwsAuditor
4
4
  class NotifySlack
5
- attr_accessor :text, :channel, :webhook, :username, :icon_url, :icon_emoji
5
+ attr_accessor :text, :channel, :webhook, :username, :icon_url, :icon_emoji, :attachments
6
6
 
7
7
  def initialize(text)
8
8
  self.text = text
9
+ self.attachments = []
9
10
  if SportNginAwsAuditor::Config.slack
10
11
  self.channel = SportNginAwsAuditor::Config.slack[:channel]
11
12
  self.username = SportNginAwsAuditor::Config.slack[:username]
@@ -18,13 +19,14 @@ module SportNginAwsAuditor
18
19
 
19
20
  def perform
20
21
  if SportNginAwsAuditor::Config.slack
21
- options = {webhook: webhook,
22
+ options = {text: text,
23
+ webhook: webhook,
22
24
  channel: channel,
23
25
  username: username,
24
26
  icon_url: icon_url,
25
- http_options: {open_timeout: 10}
27
+ attachments: attachments
26
28
  }
27
- Slack::Notifier.new(webhook, options).ping(text)
29
+ HTTParty.post(options[:webhook], :body => "payload=#{options.to_json}")
28
30
  end
29
31
  end
30
32
  end
@@ -1,5 +1,6 @@
1
1
  require 'highline/import'
2
2
  require_relative "../notify_slack"
3
+ require_relative "../instance"
3
4
 
4
5
  module SportNginAwsAuditor
5
6
  module Scripts
@@ -47,76 +48,79 @@ module SportNginAwsAuditor
47
48
  instances_without_tag = klass.filter_instance_without_tags(instances)
48
49
  instance_hash = klass.instance_count_hash(instances_without_tag)
49
50
  klass.add_instances_with_tag_to_hash(instances_with_tag, instance_hash)
50
- return instance_hash
51
51
  elsif options[:reserved]
52
- return klass.instance_count_hash(klass.get_reserved_instances)
52
+ instance_hash = klass.instance_count_hash(klass.get_reserved_instances)
53
53
  else
54
- return klass.compare(tag_name)
54
+ instance_hash = klass.compare(tag_name)
55
55
  end
56
+
57
+ compared_array = []
58
+ instance_hash.each do |key, value|
59
+ compared_array.push(Instance.new(key, value))
60
+ end
61
+ compared_array
56
62
  end
57
63
 
58
64
  def self.print_data(slack, environment, data, class_type)
65
+ data.sort_by! { |instance| [instance.type, instance.name] }
66
+
59
67
  if slack
60
68
  print_to_slack(data, class_type, environment)
61
69
  elsif options[:reserved] || options[:instances]
62
70
  puts header(class_type)
63
- data.each{ |key, value| say "<%= color('#{key}: #{value}', :white) %>" }
71
+ data.each{ |instance| say "<%= color('#{instance.name}: #{instance.count}', :white) %>" }
64
72
  else
65
73
  puts header(class_type)
66
- data.each{ |key, value| colorize(key, value) }
74
+ data.each{ |instance| colorize(instance) }
67
75
  end
68
76
  end
69
77
 
70
- def self.colorize(key, value)
71
- if key.include?(" with tag")
72
- key, value = modify_tag_prints(key, value)
73
- say "<%= color('#{key}: #{value}', :blue) %>"
74
- elsif value < 0
75
- say "<%= color('#{key}: #{value}', :yellow) %>"
76
- elsif value == 0
77
- say "<%= color('#{key}: #{value}', :green) %>"
78
- elsif value > 0
79
- say "<%= color('#{key}: #{value}', :red) %>"
80
- end
78
+ def self.colorize(instance)
79
+ name = instance.name
80
+ count = instance.count
81
+ color, rgb = color_chooser(instance)
82
+ say "<%= color('#{name}: #{count}', :#{color}) %>"
81
83
  end
82
84
 
83
- def self.print_to_slack(instances_hash, class_type, environment)
84
- discrepancy_hash = Hash.new
85
- instances_hash.each do |key, value|
86
- if value != 0
87
- discrepancy_hash[key] = value
85
+ def self.print_to_slack(instances_array, class_type, environment)
86
+ discrepancy_array = []
87
+ instances_array.each do |instance|
88
+ unless instance.matched?
89
+ discrepancy_array.push(instance)
88
90
  end
89
91
  end
90
92
 
91
- true_discrepancies = discrepancy_hash.dup.select{ |key, value| !key.include?(" with tag")}
93
+ true_discrepancies = discrepancy_array.reject{ |instance| instance.tagged? }
92
94
 
93
- if true_discrepancies.empty?
94
- slack_job = NotifySlack.new("All #{class_type} instances for #{environment} are up to date.")
95
- slack_job.perform
96
- else
97
- print_discrepancies(discrepancy_hash, class_type, environment)
95
+ unless true_discrepancies.empty?
96
+ print_discrepancies(discrepancy_array, class_type, environment)
98
97
  end
99
98
  end
100
99
 
101
- def self.print_discrepancies(discrepancy_hash, class_type, environment)
102
- to_print = "Some #{class_type} instances for #{environment} are out of sync:\n"
103
- to_print << "#{header(class_type)}\n"
100
+ def self.print_discrepancies(discrepancy_array, class_type, environment)
101
+ title = "Some #{class_type} discrepancies for #{environment} exist:\n"
102
+ slack_job = NotifySlack.new(title)
104
103
 
105
- discrepancy_hash.each do |key, value|
106
- if key.include?(" with tag")
107
- key, value = modify_tag_prints(key, value)
108
- end
109
- to_print << "#{key}: #{value}\n"
104
+ discrepancy_array.each do |discrepancy|
105
+ name = discrepancy.name
106
+ count = discrepancy.count
107
+ color, rgb = color_chooser(discrepancy)
108
+ slack_job.attachments.push({"color" => rgb, "text" => "#{name}: #{count}", "mrkdwn_in" => ["text"]})
110
109
  end
111
110
 
112
- slack_job = NotifySlack.new(to_print)
113
111
  slack_job.perform
114
112
  end
115
113
 
116
- def self.modify_tag_prints(key, value)
117
- key = key.dup # because key is a frozen string right now
118
- key.slice!(" with tag")
119
- return key, "*" << value.to_s
114
+ def self.color_chooser(instance)
115
+ if instance.tagged?
116
+ return "blue", "#0000CC"
117
+ elsif instance.running?
118
+ return "yellow", "#FFD700"
119
+ elsif instance.matched?
120
+ return "green", "#32CD32"
121
+ elsif instance.reserved?
122
+ return "red", "#BF1616"
123
+ end
120
124
  end
121
125
 
122
126
  def self.header(type, length = 50)
@@ -1,3 +1,3 @@
1
1
  module SportNginAwsAuditor
2
- VERSION = "3.4.1"
2
+ VERSION = "3.5.0"
3
3
  end
data/spec/spec_helper.rb CHANGED
@@ -1,5 +1,4 @@
1
1
  require 'aws-sdk'
2
- require 'slack-notifier'
3
2
 
4
3
  # This file was generated by the `rspec --init` command. Conventionally, all
5
4
  # specs live under a `spec` directory, which RSpec adds to the `$LOAD_PATH`.
@@ -0,0 +1,30 @@
1
+ require "sport_ngin_aws_auditor"
2
+
3
+ module SportNginAwsAuditor
4
+ describe Instance do
5
+ it "should make a reserved instance with proper attributes" do
6
+ instance = Instance.new("Windows VPC us-east-1e m1.large", 4)
7
+ expect(instance).to be_an_instance_of(Instance)
8
+ expect(instance.type).to eq("reserved")
9
+ expect(instance.count).to eq(4)
10
+ expect(instance.tagged?).to eq(false)
11
+ expect(instance.reserved?).to eq(true)
12
+ end
13
+
14
+ it "should make a running instance with proper attributes" do
15
+ instance = Instance.new("Windows VPC us-east-1e m1.large", -1)
16
+ expect(instance).to be_an_instance_of(Instance)
17
+ expect(instance.type).to eq("running")
18
+ expect(instance.count).to eq(-1.abs)
19
+ end
20
+
21
+ it "should make an instance with a tag with proper attributes" do
22
+ instance = Instance.new("Windows VPC us-east-1e m1.large with tag", 4)
23
+ expect(instance).to be_an_instance_of(Instance)
24
+ expect(instance.type).to eq("tagged")
25
+ expect(instance.count).to eq(4)
26
+ expect(instance.tagged?).to eq(true)
27
+ expect(instance.running?).to eq(false)
28
+ end
29
+ end
30
+ end
@@ -14,9 +14,8 @@ module SportNginAwsAuditor
14
14
  end
15
15
 
16
16
  it 'should ping Slack Notifier' do
17
- notifier = double('notifier', ping: true)
18
- allow(Slack::Notifier).to receive(:new).and_return(notifier)
19
- expect(notifier).to receive(:ping).and_return(true)
17
+ notifier = double('notifier')
18
+ expect(HTTParty).to receive(:post)
20
19
  message = NotifySlack.new("Test message")
21
20
  message.perform
22
21
  end
@@ -24,9 +24,9 @@ Gem::Specification.new do |spec|
24
24
  spec.add_dependency 'highline', '~> 1.6'
25
25
  spec.add_dependency 'google_drive', '~> 1.0.0.pre2'
26
26
  spec.add_dependency 'google-api-client', '~> 0.8.6'
27
- spec.add_dependency 'slack-notifier', '~> 1.5.1'
28
27
  spec.add_dependency 'rack', '~> 1.3.0'
29
28
  spec.add_dependency 'activesupport', '~> 3.2'
29
+ spec.add_dependency 'httparty'
30
30
 
31
31
  spec.add_development_dependency "bundler", "~> 1.7"
32
32
  spec.add_development_dependency "rake", "~> 10.0"
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: 3.4.1
4
+ version: 3.5.0
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: 2016-07-22 00:00:00.000000000 Z
13
+ date: 2016-07-25 00:00:00.000000000 Z
14
14
  dependencies:
15
15
  - !ruby/object:Gem::Dependency
16
16
  name: aws-sdk
@@ -97,47 +97,47 @@ dependencies:
97
97
  - !ruby/object:Gem::Version
98
98
  version: 0.8.6
99
99
  - !ruby/object:Gem::Dependency
100
- name: slack-notifier
100
+ name: rack
101
101
  requirement: !ruby/object:Gem::Requirement
102
102
  requirements:
103
103
  - - "~>"
104
104
  - !ruby/object:Gem::Version
105
- version: 1.5.1
105
+ version: 1.3.0
106
106
  type: :runtime
107
107
  prerelease: false
108
108
  version_requirements: !ruby/object:Gem::Requirement
109
109
  requirements:
110
110
  - - "~>"
111
111
  - !ruby/object:Gem::Version
112
- version: 1.5.1
112
+ version: 1.3.0
113
113
  - !ruby/object:Gem::Dependency
114
- name: rack
114
+ name: activesupport
115
115
  requirement: !ruby/object:Gem::Requirement
116
116
  requirements:
117
117
  - - "~>"
118
118
  - !ruby/object:Gem::Version
119
- version: 1.3.0
119
+ version: '3.2'
120
120
  type: :runtime
121
121
  prerelease: false
122
122
  version_requirements: !ruby/object:Gem::Requirement
123
123
  requirements:
124
124
  - - "~>"
125
125
  - !ruby/object:Gem::Version
126
- version: 1.3.0
126
+ version: '3.2'
127
127
  - !ruby/object:Gem::Dependency
128
- name: activesupport
128
+ name: httparty
129
129
  requirement: !ruby/object:Gem::Requirement
130
130
  requirements:
131
- - - "~>"
131
+ - - ">="
132
132
  - !ruby/object:Gem::Version
133
- version: '3.2'
133
+ version: '0'
134
134
  type: :runtime
135
135
  prerelease: false
136
136
  version_requirements: !ruby/object:Gem::Requirement
137
137
  requirements:
138
- - - "~>"
138
+ - - ">="
139
139
  - !ruby/object:Gem::Version
140
- version: '3.2'
140
+ version: '0'
141
141
  - !ruby/object:Gem::Dependency
142
142
  name: bundler
143
143
  requirement: !ruby/object:Gem::Requirement
@@ -214,6 +214,7 @@ files:
214
214
  - lib/sport_ngin_aws_auditor/ec2_instance.rb
215
215
  - lib/sport_ngin_aws_auditor/google.rb
216
216
  - lib/sport_ngin_aws_auditor/google_sheet.rb
217
+ - lib/sport_ngin_aws_auditor/instance.rb
217
218
  - lib/sport_ngin_aws_auditor/instance_helper.rb
218
219
  - lib/sport_ngin_aws_auditor/notify_slack.rb
219
220
  - lib/sport_ngin_aws_auditor/output.rb
@@ -228,6 +229,7 @@ files:
228
229
  - spec/sport_ngin_aws_auditor/cache_instance_spec.rb
229
230
  - spec/sport_ngin_aws_auditor/config_spec.rb
230
231
  - spec/sport_ngin_aws_auditor/ec2_instance_spec.rb
232
+ - spec/sport_ngin_aws_auditor/instance_spec.rb
231
233
  - spec/sport_ngin_aws_auditor/notify_slack_spec.rb
232
234
  - spec/sport_ngin_aws_auditor/rds_instance_spec.rb
233
235
  - sport_ngin_aws_auditor.gemspec
@@ -261,5 +263,6 @@ test_files:
261
263
  - spec/sport_ngin_aws_auditor/cache_instance_spec.rb
262
264
  - spec/sport_ngin_aws_auditor/config_spec.rb
263
265
  - spec/sport_ngin_aws_auditor/ec2_instance_spec.rb
266
+ - spec/sport_ngin_aws_auditor/instance_spec.rb
264
267
  - spec/sport_ngin_aws_auditor/notify_slack_spec.rb
265
268
  - spec/sport_ngin_aws_auditor/rds_instance_spec.rb