Class: SportNginAwsAuditor::Scripts::Audit

Inherits:
Object
  • Object
show all
Extended by:
AWSWrapper
Defined in:
lib/sport_ngin_aws_auditor/scripts/audit.rb

Class Attribute Summary collapse

Attributes included from AWSWrapper

#account_id, #aws

Class Method Summary collapse

Methods included from AWSWrapper

get_account_id

Class Attribute Details

.optionsObject

Returns the value of attribute options.



10
11
12
# File 'lib/sport_ngin_aws_auditor/scripts/audit.rb', line 10

def options
  @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, options=nil, global_options=nil)
  aws(environment, global_options[:aws_roles])
  @options = options
  slack = options[:slack]
  no_selection = !(options[:ec2] || options[:rds] || options[:cache])

  if options[:no_tag]
    tag_name = nil
  else
    tag_name = options[: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 options[:ec2] || no_selection
  print_data(slack, environment, data, "EC2Instance") if options[:ec2] || no_selection

  data = gather_data("RDSInstance", tag_name) if options[:rds] || no_selection
  print_data(slack, environment, data, "RDSInstance") if options[:rds] || no_selection

  data = gather_data("CacheInstance", tag_name) if options[:cache] || no_selection
  print_data(slack, environment, data, "CacheInstance") if options[: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 options[:instances]
    instances = klass.get_instances(tag_name)
    instances_with_tag = klass.filter_instances_with_tags(instances)
    instances_without_tag = klass.filter_instance_without_tags(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 options[: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


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 options[:reserved] || options[: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


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


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