nodes.rb 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. # frozen_string_literal: true
  2. require 'neo4j'
  3. require 'neoinfra'
  4. require 'nodes'
  5. require 'accounts'
  6. require 'fog-aws'
  7. # NeoInfra Account information
  8. module NeoInfra
  9. # Provide informations about the accounts available
  10. class Nodes
  11. def initialize
  12. @cfg = NeoInfra::Config.new
  13. neo4j_url = "http://#{@cfg.neo4j[:host]}:#{@cfg.neo4j[:port]}"
  14. Neo4j::Session.open(:server_db, neo4j_url)
  15. end
  16. def display_node(node_id)
  17. n = Node.where(node_id: node_id).first
  18. return {
  19. "Name" => n.name,
  20. "IP" => n.ip,
  21. "State" => n.state,
  22. "AMI" => n.ami,
  23. "Public_IP" => n.public_ip,
  24. "AZ" => n.az.az,
  25. "Account" => n.account.name,
  26. "Size" => n.size,
  27. "Subnet" => n.subnet.name,
  28. "VPC" => n.subnet.subnet.name,
  29. "SSH-Key" => n.sshkey.name,
  30. "SecurityGroup" => n.node_sg.name,
  31. }
  32. end
  33. def search_nodes_by_ip(ip)
  34. display_node(Node.where(ip: ip).first.node_id)
  35. end
  36. def search_nodes_by_node_id(node_id)
  37. display_node(Node.where(node_id: node_id).first.node_id)
  38. end
  39. def load_nodes
  40. aws = NeoInfra::Aws.new
  41. @cfg.accounts.each do |account|
  42. base_conf = {
  43. provider: 'AWS',
  44. aws_access_key_id: account[:key],
  45. aws_secret_access_key: account[:secret]
  46. }
  47. aws.regions.each do |region|
  48. region_conf = { region: region }
  49. new_conn = Fog::Compute.new(region_conf.merge(base_conf))
  50. new_conn.servers.all.each do |ec2|
  51. if SshKey.where(name: ec2.key_name).empty?
  52. s = SshKey.new(
  53. name: ec2.key_name,
  54. account: account[:name]
  55. )
  56. s.save
  57. SshKeyAccount.create(from_node: s, to_node: AwsAccount.where(name: account[:name]).first)
  58. end
  59. next unless Node.where(node_id: ec2.id).empty?
  60. node_name = if ec2.tags.empty?
  61. ec2.id
  62. elsif ec2.tags.key? 'Name'
  63. ec2.tags['Name']
  64. else
  65. ec2.id
  66. end
  67. n = Node.new(
  68. name: node_name,
  69. node_id: ec2.id,
  70. ip: ec2.private_ip_address,
  71. public_ip: ec2.public_ip_address,
  72. size: ec2.flavor_id,
  73. state: ec2.state,
  74. ami: ec2.image_id
  75. )
  76. n.save
  77. NodeAccount.create(from_node: n, to_node: AwsAccount.where(name: account[:name]).first)
  78. NodeSubnet.create(from_node: n, to_node: Subnet.where(subnet_id: ec2.subnet_id).first)
  79. NodeAz.create(from_node: n, to_node: Az.where(az: ec2.availability_zone).first)
  80. NodeSshKey.create(from_node: n, to_node: SshKey.where(name: ec2.key_name).first)
  81. ec2.network_interfaces.select{|x| x.length > 0 }.each do |i|
  82. if i.has_key? 'groupIds'
  83. i['groupIds'].each do |g|
  84. begin
  85. NodeSecurityGroup.create(from_node: n, to_node: SecurityGroup.where(sg_id: g).first)
  86. rescue
  87. puts "Security Groups: #{account[:name]}/#{region} couldn't get the following to work:"
  88. p ec2
  89. p g
  90. end
  91. end
  92. end
  93. end
  94. end
  95. end
  96. end
  97. end
  98. end
  99. end