nodes.rb 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  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. new_conn = Fog::Compute.new(region_conf.merge(base_conf))
  54. new_conn.servers.all.each do |ec2|
  55. if SshKey.where(name: ec2.key_name).empty?
  56. s = SshKey.new(
  57. name: ec2.key_name,
  58. account: account[:name]
  59. )
  60. s.save
  61. SshKeyAccount.create(from_node: s, to_node: AwsAccount.where(name: account[:name]).first)
  62. end
  63. next unless Node.where(node_id: ec2.id).empty?
  64. node_name = if ec2.tags.empty?
  65. ec2.id
  66. elsif ec2.tags.key? 'Name'
  67. ec2.tags['Name']
  68. else
  69. ec2.id
  70. end
  71. n = Node.new(
  72. name: node_name,
  73. node_id: ec2.id,
  74. ip: ec2.private_ip_address,
  75. public_ip: ec2.public_ip_address,
  76. size: ec2.flavor_id,
  77. state: ec2.state,
  78. ami: ec2.image_id
  79. )
  80. n.save
  81. NodeAccount.create(from_node: n, to_node: AwsAccount.where(name: account[:name]).first)
  82. NodeSubnet.create(from_node: n, to_node: Subnet.where(subnet_id: ec2.subnet_id).first)
  83. NodeAz.create(from_node: n, to_node: Az.where(az: ec2.availability_zone).first)
  84. NodeSshKey.create(from_node: n, to_node: SshKey.where(name: ec2.key_name).first)
  85. ec2.network_interfaces.select{|x| x.length > 0 }.each do |i|
  86. if i.has_key? 'groupIds'
  87. i['groupIds'].each do |g|
  88. begin
  89. NodeSecurityGroup.create(from_node: n, to_node: SecurityGroup.where(sg_id: g).first)
  90. rescue
  91. puts "Security Groups: #{account[:name]}/#{region} couldn't get the following to work:"
  92. p ec2
  93. p g
  94. end
  95. end
  96. end
  97. end
  98. end
  99. end
  100. end
  101. end
  102. end
  103. end