nodes.rb 4.3 KB

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