nodes.rb 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  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. if Node.where(ip: ip).length > 0
  35. display_node(Node.where(ip: ip).first.node_id)
  36. else
  37. display_node(Node.where(public_ip: ip).first.node_id)
  38. end
  39. end
  40. def search_nodes_by_node_id(node_id)
  41. display_node(Node.where(node_id: node_id).first.node_id)
  42. end
  43. def load_nodes
  44. aws = NeoInfra::Aws.new
  45. @cfg.accounts.each do |account|
  46. base_conf = {
  47. provider: 'AWS',
  48. aws_access_key_id: account[:key],
  49. aws_secret_access_key: account[:secret]
  50. }
  51. aws.regions.each do |region|
  52. region_conf = { region: region }
  53. begin
  54. new_conn = Fog::Compute.new(region_conf.merge(base_conf))
  55. rescue
  56. puts "Error loading nodes in region: #{region}"
  57. next
  58. end
  59. new_conn.servers.all.each do |ec2|
  60. if SshKey.where(name: ec2.key_name).empty?
  61. s = SshKey.new(
  62. name: ec2.key_name,
  63. account: account[:name]
  64. )
  65. s.save
  66. SshKeyAccount.create(from_node: s, to_node: AwsAccount.where(name: account[:name]).first)
  67. end
  68. next unless Node.where(node_id: ec2.id).empty?
  69. node_name = if ec2.tags.empty?
  70. ec2.id
  71. elsif ec2.tags.key? 'Name'
  72. ec2.tags['Name']
  73. else
  74. ec2.id
  75. end
  76. n = Node.new(
  77. name: node_name,
  78. node_id: ec2.id,
  79. ip: ec2.private_ip_address,
  80. public_ip: ec2.public_ip_address,
  81. size: ec2.flavor_id,
  82. state: ec2.state,
  83. ami: ec2.image_id
  84. )
  85. n.save
  86. NodeAccount.create(from_node: n, to_node: AwsAccount.where(name: account[:name]).first)
  87. NodeSubnet.create(from_node: n, to_node: Subnet.where(subnet_id: ec2.subnet_id).first)
  88. NodeAz.create(from_node: n, to_node: Az.where(az: ec2.availability_zone).first)
  89. NodeSshKey.create(from_node: n, to_node: SshKey.where(name: ec2.key_name).first)
  90. ec2.network_interfaces.select{|x| x.length > 0 }.each do |i|
  91. if i.has_key? 'groupIds'
  92. i['groupIds'].each do |g|
  93. begin
  94. NodeSecurityGroup.create(from_node: n, to_node: SecurityGroup.where(sg_id: g).first)
  95. rescue
  96. puts "Security Groups: #{account[:name]}/#{region} couldn't get the following to work:"
  97. p ec2
  98. p g
  99. end
  100. end
  101. end
  102. end
  103. end
  104. end
  105. end
  106. end
  107. end
  108. end