aws.rb 2.4 KB

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