nodes.rb 2.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  1. # frozen_string_literal: true
  2. require 'nodes'
  3. require 'accounts'
  4. require 'fog'
  5. require 'neo4j'
  6. require 'neoinfra/aws'
  7. # NeoInfra Account information
  8. module NeoInfra
  9. # Provide informations about the accounts available
  10. class Nodes
  11. def load_nodes
  12. aws = NeoInfra::Aws.new
  13. @cfg = NeoInfra::Config.new
  14. neo4j_url = "http://#{@cfg.neo4j[:host]}:#{@cfg.neo4j[:port]}"
  15. Neo4j::Session.open(:server_db, neo4j_url)
  16. @cfg.accounts.each do |account|
  17. base_conf = {
  18. provider: 'AWS',
  19. aws_access_key_id: account[:key],
  20. aws_secret_access_key: account[:secret]
  21. }
  22. aws.regions.each do |region|
  23. region_conf = { region: region }
  24. new_conn = Fog::Compute.new(region_conf.merge(base_conf))
  25. new_conn.servers.all.each do |ec2|
  26. if SshKey.where(name: ec2.key_name).empty?
  27. s = SshKey.new(
  28. name: ec2.key_name,
  29. account: account[:name]
  30. )
  31. s.save
  32. SshKeyAccount.create(from_node: s, to_node: AwsAccount.where(name: account[:name]).first)
  33. end
  34. next unless Node.where(node_id: ec2.id).empty?
  35. node_name = if ec2.tags.empty?
  36. ec2.id
  37. elsif ec2.tags.key? 'Name'
  38. ec2.tags['Name']
  39. else
  40. ec2.id
  41. end
  42. n = Node.new(
  43. name: node_name,
  44. node_id: ec2.id,
  45. ip: ec2.private_ip_address,
  46. public_ip: ec2.public_ip_address,
  47. size: ec2.flavor_id,
  48. state: ec2.state,
  49. ami: ec2.image_id
  50. )
  51. n.save
  52. NodeAccount.create(from_node: n, to_node: AwsAccount.where(name: account[:name]).first)
  53. NodeSubnet.create(from_node: n, to_node: Subnet.where(subnet_id: ec2.subnet_id).first)
  54. NodeAz.create(from_node: n, to_node: Az.where(az: ec2.availability_zone).first)
  55. NodeSshKey.create(from_node: n, to_node: SshKey.where(name: ec2.key_name).first)
  56. end
  57. end
  58. end
  59. end
  60. end
  61. end