audit.rb 1.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243
  1. # frozen_string_literal: true
  2. require 'nodes'
  3. require 'accounts'
  4. require 'fog-aws'
  5. require 'neoinfra'
  6. # NeoInfra Account information
  7. module NeoInfra
  8. # Provide informations about the accounts available
  9. class Audit
  10. def audit_nodes
  11. results = Hash.new { |h, k| h[k] = {} }
  12. aws = NeoInfra::Aws.new
  13. @cfg = NeoInfra::Config.new
  14. @cfg.accounts.each do |account|
  15. base_conf = {
  16. provider: 'AWS',
  17. aws_access_key_id: account[:key],
  18. aws_secret_access_key: account[:secret]
  19. }
  20. aws.regions.each do |region|
  21. region_conf = { region: region }
  22. #Get Instances
  23. new_conn = Fog::Compute.new(region_conf.merge(base_conf))
  24. new_conn.servers.all.each do |ec2|
  25. %i[required recommended]. each do |a|
  26. next if (ec2.tags.keys.sort & @cfg.tag_policy[a].sort) == @cfg.tag_policy[a].sort
  27. results[ec2.id].merge!("#{a}_missing_tags" => @cfg.tag_policy[a].sort - ec2.tags.keys.sort,
  28. 'tags' => ec2.tags.keys.sort,
  29. 'account' => account[:name],
  30. 'launched' => ec2.created_at,
  31. 'region' => region,
  32. 'ssh_key' => ec2.key_name)
  33. end
  34. end
  35. end
  36. end
  37. results
  38. end
  39. end
  40. end