class AwsAuditor::Scripts::Export

Constants

CLASS_TYPES

Attributes

cache_instances[RW]
ec2_instances[RW]
environment[RW]
file[RW]
keys_hash[RW]
options[RW]
rds_instances[RW]

Public Class Methods

all_keys_hash(name = nil, value = nil) click to toggle source
# File lib/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_array() click to toggle source
# File lib/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_instances() click to toggle source
# File lib/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 }) click to toggle source
# File lib/aws_auditor/scripts/export.rb, line 110
def self.counts(options = {:instance => false, :reserved => false, :compare => false })
  CLASS_TYPES.map do |class_type|
    klass = AwsAuditor.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) click to toggle source
# File lib/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_array() click to toggle source
# File lib/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_instances() click to toggle source
# File lib/aws_auditor/scripts/export.rb, line 124
def self.ec2_reserved_instances
  @ec2_reserved_instances ||= EC2Instance.reserved_instance_hash
end
execute(environment, options = nil) click to toggle source
# File lib/aws_auditor/scripts/export.rb, line 16
def self.execute(environment, options = nil)
  @environment = environment
  (puts "Must specify either --drive or --csv"; exit) unless options[:csv] || options[:drive]
  aws(environment)
  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_arrays() click to toggle source
# File lib/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_counts() click to toggle source
# File lib/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_keys() click to toggle source
# File lib/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
prepare() click to toggle source
# File lib/aws_auditor/scripts/export.rb, line 54
def self.prepare
  [get_all_arrays,get_all_counts].flatten
end
rds_array() click to toggle source
# File lib/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_instances() click to toggle source
# File lib/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) click to toggle source
# File lib/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