nodes.rb 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  1. # frozen_string_literal: true
  2. require 'neo4j'
  3. require 'neoinfra'
  4. require 'nodes'
  5. require 'node_stats'
  6. require 'accounts'
  7. require 'fog-aws'
  8. # NeoInfra Account information
  9. module NeoInfra
  10. # Provide informations about the accounts available
  11. class Nodes
  12. def initialize
  13. @cfg = NeoInfra::Config.new
  14. neo4j_url = "http://#{@cfg.neo4j[:host]}:#{@cfg.neo4j[:port]}"
  15. Neo4j::Session.open(:server_db, neo4j_url)
  16. end
  17. def display_node(node_id)
  18. n = Node.where(node_id: node_id).first
  19. {
  20. 'Name' => n.name,
  21. 'IP' => n.ip,
  22. 'State' => n.state,
  23. 'AMI' => n.ami,
  24. 'Public_IP' => n.public_ip,
  25. 'AZ' => n.az.az,
  26. 'Account' => n.account.name,
  27. 'Size' => n.size,
  28. 'Subnet' => n.subnet.name,
  29. 'VPC' => n.subnet.subnet.name,
  30. 'SSH-Key' => n.sshkey.name,
  31. 'SecurityGroup' => n.node_sg.name
  32. }
  33. end
  34. def search_nodes_by_name(name)
  35. results = {:nodes => [], :errors => []}
  36. if !Node.where(name: name).empty?
  37. Node.where(name: name).each do |k|
  38. results[:nodes] << display_node(k.node_id)
  39. end
  40. else
  41. results[:errors] << "Could not find a node with name: #{name}"
  42. end
  43. return results
  44. end
  45. def search_nodes_by_ip(ip)
  46. if !Node.where(ip: ip).empty?
  47. display_node(Node.where(ip: ip).first.node_id)
  48. else
  49. display_node(Node.where(public_ip: ip).first.node_id)
  50. end
  51. end
  52. def search_nodes_by_node_id(node_id)
  53. display_node(Node.where(node_id: node_id).first.node_id)
  54. end
  55. def load_nodes
  56. aws = NeoInfra::Aws.new
  57. @cfg.accounts.each do |account|
  58. base_conf = {
  59. provider: 'AWS',
  60. aws_access_key_id: account[:key],
  61. aws_secret_access_key: account[:secret]
  62. }
  63. aws.regions.each do |region|
  64. region_conf = { region: region }
  65. begin
  66. new_conn = Fog::Compute.new(region_conf.merge(base_conf))
  67. rescue StandardError
  68. puts "Error loading nodes in region: #{region}"
  69. next
  70. end
  71. new_conn.servers.all.each do |ec2|
  72. next if ec2.state == 'terminated'
  73. if SshKey.where(name: ec2.key_name).empty?
  74. s = SshKey.new(
  75. name: ec2.key_name,
  76. account: account[:name]
  77. )
  78. s.save
  79. SshKeyAccount.create(from_node: s, to_node: AwsAccount.where(name: account[:name]).first)
  80. end
  81. next unless Node.where(node_id: ec2.id).empty?
  82. node_name = if ec2.tags.empty?
  83. ec2.id
  84. elsif ec2.tags.key? 'Name'
  85. ec2.tags['Name']
  86. else
  87. ec2.id
  88. end
  89. n = Node.new(
  90. name: node_name,
  91. node_id: ec2.id,
  92. ip: ec2.private_ip_address,
  93. public_ip: ec2.public_ip_address,
  94. size: ec2.flavor_id,
  95. state: ec2.state,
  96. ami: ec2.image_id
  97. )
  98. n.save
  99. cw = NeoInfra::Cloudwatch.new
  100. stats = cw.get_instance_cpu(account[:key], account[:secret], region, ec2.id)
  101. s = NodeStats.new(
  102. node_id: ec2.id,
  103. cpu_max: stats[:cpu_max],
  104. cpu_avg: stats[:cpu_avg],
  105. )
  106. s.save
  107. begin
  108. NodeAccount.create(from_node: n, to_node: AwsAccount.where(name: account[:name]).first)
  109. NodeAz.create(from_node: n, to_node: Az.where(az: ec2.availability_zone).first)
  110. NodeSshKey.create(from_node: n, to_node: SshKey.where(name: ec2.key_name).first)
  111. NodeSubnet.create(from_node: n, to_node: Subnet.where(subnet_id: ec2.subnet_id).first)
  112. Node2Stats.create(from_node: n, to_node: s)
  113. rescue Exception => e
  114. puts "Account #{account[:name]} couldn't load the following node: #{e.message}"
  115. p n
  116. end
  117. ec2.network_interfaces.reject(&:empty?).each do |i|
  118. next unless i.key? 'groupIds'
  119. i['groupIds'].each do |g|
  120. begin
  121. NodeSecurityGroup.create(from_node: n, to_node: SecurityGroup.where(sg_id: g).first)
  122. rescue StandardError
  123. puts "Security Groups: #{account[:name]}/#{region} couldn't get the following to work:"
  124. p ec2
  125. p g
  126. end
  127. end
  128. end
  129. end
  130. end
  131. end
  132. end
  133. end
  134. end