Class: SportNginAwsAuditor::Scripts::Audit
- Inherits:
-
Object
- Object
- SportNginAwsAuditor::Scripts::Audit
- Extended by:
- AWSWrapper
- Defined in:
- lib/sport_ngin_aws_auditor/scripts/audit.rb
Class Attribute Summary collapse
-
.options ⇒ Object
Returns the value of attribute options.
Attributes included from AWSWrapper
Class Method Summary collapse
- .colorize(key, value) ⇒ Object
- .execute(environment, options = nil, global_options = nil) ⇒ Object
- .gather_data(class_type, tag_name) ⇒ Object
- .header(type, length = 50) ⇒ Object
- .modify_tag_prints(key, value) ⇒ Object
- .print_data(slack, environment, data, class_type) ⇒ Object
- .print_discrepancies(discrepancy_hash, class_type, environment) ⇒ Object
- .print_to_slack(instances_hash, class_type, environment) ⇒ Object
Methods included from AWSWrapper
Class Attribute Details
.options ⇒ Object
Returns the value of attribute options.
10 11 12 |
# File 'lib/sport_ngin_aws_auditor/scripts/audit.rb', line 10 def @options end |
Class Method Details
.colorize(key, value) ⇒ Object
70 71 72 73 74 75 76 77 78 79 80 81 |
# File 'lib/sport_ngin_aws_auditor/scripts/audit.rb', line 70 def self.colorize(key, value) if key.include?(" with tag") key, value = modify_tag_prints(key, value) say "<%= color('#{key}: #{value}', :blue) %>" elsif value < 0 say "<%= color('#{key}: #{value}', :yellow) %>" elsif value == 0 say "<%= color('#{key}: #{value}', :green) %>" elsif value > 0 say "<%= color('#{key}: #{value}', :red) %>" end end |
.execute(environment, options = nil, global_options = nil) ⇒ Object
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 |
# File 'lib/sport_ngin_aws_auditor/scripts/audit.rb', line 13 def self.execute(environment, =nil, =nil) aws(environment, [:aws_roles]) @options = slack = [:slack] no_selection = !([:ec2] || [:rds] || [:cache]) if [:no_tag] tag_name = nil else tag_name = [:tag] end if !slack print "Gathering info, please wait..."; print "\r" else puts "Condensed results from this audit will print into Slack instead of directly to an output." end data = gather_data("EC2Instance", tag_name) if [:ec2] || no_selection print_data(slack, environment, data, "EC2Instance") if [:ec2] || no_selection data = gather_data("RDSInstance", tag_name) if [:rds] || no_selection print_data(slack, environment, data, "RDSInstance") if [:rds] || no_selection data = gather_data("CacheInstance", tag_name) if [:cache] || no_selection print_data(slack, environment, data, "CacheInstance") if [:cache] || no_selection end |
.gather_data(class_type, tag_name) ⇒ Object
41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 |
# File 'lib/sport_ngin_aws_auditor/scripts/audit.rb', line 41 def self.gather_data(class_type, tag_name) klass = SportNginAwsAuditor.const_get(class_type) if [:instances] instances = klass.get_instances(tag_name) instances_with_tag = klass.(instances) instances_without_tag = klass.(instances) instance_hash = klass.instance_count_hash(instances_without_tag) klass.add_instances_with_tag_to_hash(instances_with_tag, instance_hash) return instance_hash elsif [:reserved] return klass.instance_count_hash(klass.get_reserved_instances) else return klass.compare(tag_name) end end |
.header(type, length = 50) ⇒ Object
122 123 124 125 126 127 128 129 130 |
# File 'lib/sport_ngin_aws_auditor/scripts/audit.rb', line 122 def self.header(type, length = 50) type.upcase!.slice! "INSTANCE" half_length = (length - type.length)/2.0 - 1 [ "*" * length, "*" * half_length.floor + " #{type} " + "*" * half_length.ceil, "*" * length ].join("\n") end |
.modify_tag_prints(key, value) ⇒ Object
116 117 118 119 120 |
# File 'lib/sport_ngin_aws_auditor/scripts/audit.rb', line 116 def self.modify_tag_prints(key, value) key = key.dup # because key is a frozen string right now key.slice!(" with tag") return key, "*" << value.to_s end |
.print_data(slack, environment, data, class_type) ⇒ Object
58 59 60 61 62 63 64 65 66 67 68 |
# File 'lib/sport_ngin_aws_auditor/scripts/audit.rb', line 58 def self.print_data(slack, environment, data, class_type) if slack print_to_slack(data, class_type, environment) elsif [:reserved] || [:instances] puts header(class_type) data.each{ |key, value| say "<%= color('#{key}: #{value}', :white) %>" } else puts header(class_type) data.each{ |key, value| colorize(key, value) } end end |
.print_discrepancies(discrepancy_hash, class_type, environment) ⇒ Object
101 102 103 104 105 106 107 108 109 110 111 112 113 114 |
# File 'lib/sport_ngin_aws_auditor/scripts/audit.rb', line 101 def self.print_discrepancies(discrepancy_hash, class_type, environment) to_print = "Some #{class_type} instances for #{environment} are out of sync:\n" to_print << "#{header(class_type)}\n" discrepancy_hash.each do |key, value| if key.include?(" with tag") key, value = modify_tag_prints(key, value) end to_print << "#{key}: #{value}\n" end slack_job = NotifySlack.new(to_print) slack_job.perform end |
.print_to_slack(instances_hash, class_type, environment) ⇒ Object
83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 |
# File 'lib/sport_ngin_aws_auditor/scripts/audit.rb', line 83 def self.print_to_slack(instances_hash, class_type, environment) discrepancy_hash = Hash.new instances_hash.each do |key, value| if value != 0 discrepancy_hash[key] = value end end true_discrepancies = discrepancy_hash.dup.select{ |key, value| !key.include?(" with tag")} if true_discrepancies.empty? slack_job = NotifySlack.new("All #{class_type} instances for #{environment} are up to date.") slack_job.perform else print_discrepancies(discrepancy_hash, class_type, environment) end end |