aws.rb 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  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 regions
  16. @cfg = NeoInfra::Config.new
  17. account = @cfg.accounts.first
  18. base_conf = {
  19. provider: 'AWS',
  20. aws_access_key_id: account[:key],
  21. aws_secret_access_key: account[:secret]
  22. }
  23. conn = Fog::Compute.new(base_conf)
  24. conn.describe_regions.data[:body]['regionInfo'].collect { |x| x['regionName'] }
  25. end
  26. def region_count
  27. Region.all.length
  28. end
  29. def az_count
  30. Az.all.length
  31. end
  32. def azs(region)
  33. @cfg = NeoInfra::Config.new
  34. account = @cfg.accounts.first
  35. base_conf = {
  36. provider: 'AWS',
  37. aws_access_key_id: account[:key],
  38. aws_secret_access_key: account[:secret],
  39. region: region
  40. }
  41. conn = Fog::Compute.new(base_conf)
  42. conn.describe_availability_zones.data[:body]['availabilityZoneInfo'].collect { |x| x['zoneName'] }
  43. end
  44. def load_regions
  45. @cfg = NeoInfra::Config.new
  46. neo4j_url = "http://#{@cfg.neo4j[:host]}:#{@cfg.neo4j[:port]}"
  47. Neo4j::Session.open(:server_db, neo4j_url)
  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. @cfg = NeoInfra::Config.new
  64. cw = NeoInfra::Cloudwatch.new
  65. neo4j_url = "http://#{@cfg.neo4j[:host]}:#{@cfg.neo4j[:port]}"
  66. Neo4j::Session.open(:server_db, neo4j_url)
  67. @cfg.accounts.each do |account|
  68. base_conf = {
  69. provider: 'AWS',
  70. aws_access_key_id: account[:key],
  71. aws_secret_access_key: account[:secret]
  72. }
  73. s = Fog::Storage.new(base_conf)
  74. s.directories.each do |bucket|
  75. next unless Bucket.where(name: bucket.key).empty?
  76. b = Bucket.new(
  77. name: bucket.key,
  78. size: cw.get_bucket_size(account[:key], account[:secret], bucket.location, bucket.key)
  79. )
  80. b.save
  81. BucketRegion.create(from_node: b, to_node: Region.where(region: bucket.location).first)
  82. BucketAccount.create(from_node: b, to_node: AwsAccount.where(name: account[:name]).first)
  83. end
  84. end
  85. end
  86. def load_rds
  87. @cfg = NeoInfra::Config.new
  88. neo4j_url = "http://#{@cfg.neo4j[:host]}:#{@cfg.neo4j[:port]}"
  89. Neo4j::Session.open(:server_db, neo4j_url)
  90. @cfg.accounts.each do |account|
  91. base_conf = {
  92. aws_access_key_id: account[:key],
  93. aws_secret_access_key: account[:secret]
  94. }
  95. s = Fog::AWS::RDS.new(base_conf)
  96. s.servers.each do |rds|
  97. next unless Rds.where(name: rds.id).empty?
  98. r = Rds.new(
  99. name: rds.id,
  100. size: rds.flavor_id,
  101. engine: rds.engine,
  102. engine_version: rds.engine_version,
  103. multi_az: rds.multi_az.to_s,
  104. endpoint: rds.endpoint['Address'],
  105. port: rds.endpoint['Port'],
  106. allocated_storage: rds.allocated_storage,
  107. )
  108. r.save
  109. RdsAz.create(from_node: r, to_node: Az.where(az: rds.availability_zone).first)
  110. RdsAccount.create(from_node: r, to_node: AwsAccount.where(name: account[:name]).first)
  111. end
  112. end
  113. end
  114. end
  115. end