aws.rb 2.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  1. # frozen_string_literal: true
  2. require 'accounts'
  3. require 'regions'
  4. require 'mime-types'
  5. require 'fog'
  6. require 's3'
  7. require 'neo4j'
  8. require 'neoinfra/config'
  9. require 'neoinfra/cloudwatch'
  10. # NeoInfra Account information
  11. module NeoInfra
  12. # Provide informations about the accounts available
  13. class Aws
  14. def regions
  15. @cfg = NeoInfra::Config.new
  16. account = @cfg.accounts.first
  17. base_conf = {
  18. provider: 'AWS',
  19. aws_access_key_id: account[:key],
  20. aws_secret_access_key: account[:secret]
  21. }
  22. conn = Fog::Compute.new(base_conf)
  23. conn.describe_regions.data[:body]['regionInfo'].collect { |x| x['regionName'] }
  24. end
  25. def azs(region)
  26. @cfg = NeoInfra::Config.new
  27. account = @cfg.accounts.first
  28. base_conf = {
  29. provider: 'AWS',
  30. aws_access_key_id: account[:key],
  31. aws_secret_access_key: account[:secret],
  32. region: region
  33. }
  34. conn = Fog::Compute.new(base_conf)
  35. conn.describe_availability_zones.data[:body]['availabilityZoneInfo'].collect { |x| x['zoneName'] }
  36. end
  37. def load_regions
  38. @cfg = NeoInfra::Config.new
  39. neo4j_url = "http://#{@cfg.neo4j[:host]}:#{@cfg.neo4j[:port]}"
  40. Neo4j::Session.open(:server_db, neo4j_url)
  41. regions.each do |region|
  42. next unless Region.where(region: region).empty?
  43. r = Region.new(
  44. region: region
  45. )
  46. r.save
  47. azs(region).each do |az|
  48. next unless Az.where(az: az).empty?
  49. a = Az.new(az: az)
  50. a.save
  51. AzRegion.create(from_node: a, to_node: Region.where(region: region).first)
  52. end
  53. end
  54. end
  55. def load_buckets
  56. @cfg = NeoInfra::Config.new
  57. cw = NeoInfra::Cloudwatch.new
  58. neo4j_url = "http://#{@cfg.neo4j[:host]}:#{@cfg.neo4j[:port]}"
  59. Neo4j::Session.open(:server_db, neo4j_url)
  60. @cfg.accounts.each do |account|
  61. base_conf = {
  62. provider: 'AWS',
  63. aws_access_key_id: account[:key],
  64. aws_secret_access_key: account[:secret]
  65. }
  66. s = Fog::Storage.new(base_conf)
  67. s.directories.each do |bucket|
  68. next unless Bucket.where(name: bucket.key).empty?
  69. b = Bucket.new(
  70. name: bucket.key,
  71. size: cw.get_bucket_size(account[:key], account[:secret], bucket.location, bucket.key)
  72. )
  73. b.save
  74. BucketRegion.create(from_node: b, to_node: Region.where(region: bucket.location).first)
  75. BucketAccount.create(from_node: b, to_node: AwsAccount.where(name: account[:name]).first)
  76. end
  77. end
  78. end
  79. end
  80. end