Class: SportNginAwsAuditor::Scripts::Export

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

Constant Summary collapse

CLASS_TYPES =
%w[EC2Instance RDSInstance CacheInstance]

Class Attribute Summary collapse

Attributes included from GoogleWrapper

#google

Attributes included from AWSWrapper

#account_id, #aws

Class Method Summary collapse

Methods included from AWSWrapper

get_account_id

Class Attribute Details

.cache_instancesObject

Returns the value of attribute cache_instances.



11
12
13
# File 'lib/sport_ngin_aws_auditor/scripts/export.rb', line 11

def cache_instances
  @cache_instances
end

.ec2_instancesObject

Returns the value of attribute ec2_instances.



11
12
13
# File 'lib/sport_ngin_aws_auditor/scripts/export.rb', line 11

def ec2_instances
  @ec2_instances
end

.environmentObject

Returns the value of attribute environment.



11
12
13
# File 'lib/sport_ngin_aws_auditor/scripts/export.rb', line 11

def environment
  @environment
end

.fileObject

Returns the value of attribute file.



11
12
13
# File 'lib/sport_ngin_aws_auditor/scripts/export.rb', line 11

def file
  @file
end

.keys_hashObject

Returns the value of attribute keys_hash.



11
12
13
# File 'lib/sport_ngin_aws_auditor/scripts/export.rb', line 11

def keys_hash
  @keys_hash
end

.optionsObject

Returns the value of attribute options.



11
12
13
# File 'lib/sport_ngin_aws_auditor/scripts/export.rb', line 11

def options
  @options
end

.rds_instancesObject

Returns the value of attribute rds_instances.



11
12
13
# File 'lib/sport_ngin_aws_auditor/scripts/export.rb', line 11

def rds_instances
  @rds_instances
end

Class Method Details

.all_keys_hash(name = nil, value = nil) ⇒ Object



67
68
69
70
71
72
# File 'lib/sport_ngin_aws_auditor/scripts/export.rb', line 67

def self.all_keys_hash(name = nil, value = nil)
  return @keys_hash if @keys_hash && @keys_hash[:name] == name
  @keys_hash = {:name => name}
  get_all_keys.each{ |key| @keys_hash[key] = value }
  @keys_hash
end

.cache_arrayObject



95
96
97
98
99
100
101
# File 'lib/sport_ngin_aws_auditor/scripts/export.rb', line 95

def self.cache_array
  instance_array = [{name: "CACHE"}]
  cache_instances.each do |cache_name, cache|
    instance_array << Hash({:name => cache_name, "#{cache.to_s}" => "#{cache.count}"})
  end
  instance_array
end

.cache_reserved_instancesObject



140
141
142
# File 'lib/sport_ngin_aws_auditor/scripts/export.rb', line 140

def self.cache_reserved_instances
  @cache_reserved_instances ||= CacheInstance.reserved_instance_hash
end

.counts(options = {:instance => false, :reserved => false, :compare => false }) ⇒ Object



110
111
112
113
114
115
116
117
118
# File 'lib/sport_ngin_aws_auditor/scripts/export.rb', line 110

def self.counts(options = {:instance => false, :reserved => false, :compare => false })
  CLASS_TYPES.map do |class_type|
    klass = SportNginAwsAuditor.const_get(class_type)
    instances = klass.instance_count_hash(klass.get_instances) if options[:instance]
    instances = klass.instance_count_hash(klass.get_reserved_instances) if options[:reserved]
    instances = klass.compare if options[:compare]
    instances
  end.inject(:merge)
end

.create_csv(keys, info) ⇒ Object



29
30
31
32
33
34
35
36
37
38
# File 'lib/sport_ngin_aws_auditor/scripts/export.rb', line 29

def self.create_csv(keys, info)
  CSV.open("#{environment}.csv", "wb") do |csv|
    csv << ["name",keys].flatten
    info.each do |hash|
      csv << all_keys_hash.merge(hash).values
    end
  end

  `open "#{environment}.csv"`
end

.ec2_arrayObject



79
80
81
82
83
84
85
# File 'lib/sport_ngin_aws_auditor/scripts/export.rb', line 79

def self.ec2_array
  instance_array = [{name: "OPSWORKS"}]
  EC2Instance.bucketize.map do |stack_name, stack_instances|
    instance_array << {:name => stack_name}.merge(EC2Instance.instance_count_hash(stack_instances))
  end
  instance_array
end

.ec2_reserved_instancesObject



124
125
126
# File 'lib/sport_ngin_aws_auditor/scripts/export.rb', line 124

def self.ec2_reserved_instances
  @ec2_reserved_instances ||= EC2Instance.reserved_instance_hash
end

.execute(environment, options = nil, global_options = nil) ⇒ Object



16
17
18
19
20
21
22
23
24
25
26
27
# File 'lib/sport_ngin_aws_auditor/scripts/export.rb', line 16

def self.execute(environment, options = nil, global_options = nil)
  @environment = environment
  (puts "Must specify either --drive or --csv"; exit) unless options[:csv] || options[:drive]
  aws(environment, global_options[:aws_roles])
  print "Gathering info, please wait..."
  all_keys = get_all_keys
  all_info = prepare
  print "\r" + " " * 50 + "\r"

  create_csv(all_keys,all_info) if options[:csv]
  upload_to_drive(all_keys,all_info) if options[:drive]
end

.get_all_arraysObject



74
75
76
77
# File 'lib/sport_ngin_aws_auditor/scripts/export.rb', line 74

def self.get_all_arrays
  return @all_array if @all_array
  @all_array = [ec2_array,rds_array,cache_array].flatten
end

.get_all_countsObject



103
104
105
106
107
108
# File 'lib/sport_ngin_aws_auditor/scripts/export.rb', line 103

def self.get_all_counts
  total_array = [{:name => "TOTALS"}]
  total_array << all_keys_hash("Running Instances").merge(counts(:instance => true))
  total_array << all_keys_hash("Reserved Instances", 0).merge(counts(:reserved => true))
  total_array << all_keys_hash("Differences").merge(counts(:compare => true))
end

.get_all_keysObject



58
59
60
61
62
63
64
65
# File 'lib/sport_ngin_aws_auditor/scripts/export.rb', line 58

def self.get_all_keys
  return @keys if @keys
  @keys = [
    [ec2_reserved_instances.values,ec2_instances.values].flatten.map{ |x| x.to_s }.uniq.sort! { |a,b| a.downcase <=> b.downcase },
    [rds_reserved_instances.values,rds_instances.values].flatten.map{ |x| x.to_s }.uniq.sort! { |a,b| a.downcase <=> b.downcase },
    [cache_reserved_instances.values,cache_instances.values].flatten.map{ |x| x.to_s }.uniq.sort! { |a,b| a.downcase <=> b.downcase }
  ].flatten
end

.prepareObject



54
55
56
# File 'lib/sport_ngin_aws_auditor/scripts/export.rb', line 54

def self.prepare
  [get_all_arrays,get_all_counts].flatten
end

.rds_arrayObject



87
88
89
90
91
92
93
# File 'lib/sport_ngin_aws_auditor/scripts/export.rb', line 87

def self.rds_array
  instance_array = [{name: "RDS"}]
  rds_instances.each do |db_name, db|
    instance_array << Hash({:name => db_name, "#{db.to_s}" => "#{db.count}"})
  end
  instance_array
end

.rds_reserved_instancesObject



132
133
134
# File 'lib/sport_ngin_aws_auditor/scripts/export.rb', line 132

def self.rds_reserved_instances
  @rds_reserved_instances ||= RDSInstance.reserved_instance_hash
end

.upload_to_drive(keys, info) ⇒ Object



40
41
42
43
44
45
46
47
48
49
50
51
52
# File 'lib/sport_ngin_aws_auditor/scripts/export.rb', line 40

def self.upload_to_drive(keys, info)
  @file = GoogleSheet.new(Google.file[:name], Google.file[:path], environment)
  print "Exporting to Google Drive, please wait..."
  file.write_header(keys)
  info.each do |value_hash|
    response = file.worksheet.list.push(value_hash)
    puts response unless response.is_a? GoogleDrive::ListRow
  end
  file.worksheet.save
  print "\r" + " " * 50 + "\r"
  puts "Exporting Complete."
  `open #{file.sheet.human_url}`
end