aws.rb 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. # frozen_string_literal: true
  2. require 'accounts'
  3. require 'regions'
  4. require 'mime-types'
  5. require 'fog-aws'
  6. require 's3'
  7. require 'neo4j'
  8. require 'rds'
  9. require 'neoinfra/config'
  10. require 'neoinfra/cloudwatch'
  11. # NeoInfra Account information
  12. module NeoInfra
  13. # Provide informations about the accounts available
  14. class Aws
  15. def initialize
  16. @cfg = NeoInfra::Config.new
  17. neo4j_url = "http://#{@cfg.neo4j[:host]}:#{@cfg.neo4j[:port]}"
  18. Neo4j::Session.open(:server_db, neo4j_url)
  19. end
  20. def regions
  21. account = @cfg.accounts.first
  22. base_conf = {
  23. provider: 'AWS',
  24. aws_access_key_id: account[:key],
  25. aws_secret_access_key: account[:secret]
  26. }
  27. conn = Fog::Compute.new(base_conf)
  28. conn.describe_regions.data[:body]['regionInfo'].collect { |x| x['regionName'] }
  29. end
  30. def region_count
  31. Region.all.length
  32. end
  33. def az_count
  34. Az.all.length
  35. end
  36. def azs(region)
  37. account = @cfg.accounts.first
  38. base_conf = {
  39. provider: 'AWS',
  40. aws_access_key_id: account[:key],
  41. aws_secret_access_key: account[:secret],
  42. region: region
  43. }
  44. conn = Fog::Compute.new(base_conf)
  45. conn.describe_availability_zones.data[:body]['availabilityZoneInfo'].collect { |x| x['zoneName'] }
  46. end
  47. def load_regions
  48. regions.each do |region|
  49. next unless Region.where(region: region).empty?
  50. r = Region.new(
  51. region: region
  52. )
  53. r.save
  54. azs(region).each do |az|
  55. next unless Az.where(az: az).empty?
  56. a = Az.new(az: az)
  57. a.save
  58. AzRegion.create(from_node: a, to_node: Region.where(region: region).first)
  59. end
  60. end
  61. end
  62. def load_buckets
  63. cw = NeoInfra::Cloudwatch.new
  64. @cfg.accounts.each do |account|
  65. base_conf = {
  66. provider: 'AWS',
  67. aws_access_key_id: account[:key],
  68. aws_secret_access_key: account[:secret]
  69. }
  70. s = Fog::Storage.new(base_conf)
  71. s.directories.each do |bucket|
  72. next unless Bucket.where(name: bucket.key).empty?
  73. b = Bucket.new(
  74. name: bucket.key,
  75. size: cw.get_bucket_size(account[:key], account[:secret], bucket.location, bucket.key)
  76. )
  77. b.save
  78. BucketRegion.create(from_node: b, to_node: Region.where(region: bucket.location).first)
  79. BucketAccount.create(from_node: b, to_node: AwsAccount.where(name: account[:name]).first)
  80. end
  81. end
  82. end
  83. def load_rds
  84. @cfg.accounts.each do |account|
  85. base_conf = {
  86. aws_access_key_id: account[:key],
  87. aws_secret_access_key: account[:secret]
  88. }
  89. s = Fog::AWS::RDS.new(base_conf)
  90. s.servers.each do |rds|
  91. next unless Rds.where(name: rds.id).empty?
  92. r = Rds.new(
  93. name: rds.id,
  94. size: rds.flavor_id,
  95. engine: rds.engine,
  96. engine_version: rds.engine_version,
  97. multi_az: rds.multi_az.to_s,
  98. endpoint: rds.endpoint['Address'],
  99. port: rds.endpoint['Port'],
  100. allocated_storage: rds.allocated_storage,
  101. )
  102. r.save
  103. RdsAz.create(from_node: r, to_node: Az.where(az: rds.availability_zone).first)
  104. RdsAccount.create(from_node: r, to_node: AwsAccount.where(name: account[:name]).first)
  105. end
  106. end
  107. end
  108. end
  109. end