vpcs.rb 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  1. # frozen_string_literal: true
  2. require 'neoinfra'
  3. require 'vpc'
  4. require 'peers'
  5. require 'accounts'
  6. require 'fog-aws'
  7. require 'neo4j'
  8. # NeoInfra Account information
  9. module NeoInfra
  10. # Provide informations about the accounts available
  11. class Vpcs
  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 non_default_vpc_count
  18. Vpc.all.collect(&:default).select { |y| y == 'false' }.length
  19. end
  20. def default_vpc_count
  21. Vpc.all.collect(&:default).select { |y| y == 'true' }.length
  22. end
  23. def list_vpcs
  24. node_counts = Hash.new(0)
  25. Node.all.each do |x|
  26. begin
  27. node_counts[x.subnet.subnet.name] += 1
  28. rescue
  29. foo = "noop"
  30. end
  31. end
  32. Vpc.all.collect { |x| { 'nodes' => node_counts[x.name], 'vpc_id' => x.vpc_id, 'name' => x.name, 'region' => x.region.region, 'owner' => x.owned.name, 'cidr' => x.cidr, 'default' => x.default } }.select { |y| y['default'] == 'false' }.sort_by { |h| h['nodes'] }.reverse
  33. end
  34. def load_peers
  35. aws = NeoInfra::Aws.new
  36. @cfg.accounts.each do |account|
  37. base_conf = {
  38. provider: 'AWS',
  39. aws_access_key_id: account[:key],
  40. aws_secret_access_key: account[:secret]
  41. }
  42. aws.regions.each do |region|
  43. region_conf = { region: region }
  44. begin
  45. new_conn = Fog::Compute.new(region_conf.merge(base_conf))
  46. rescue StandardError
  47. puts "Error loading Peering in region: #{region}"
  48. next
  49. end
  50. new_conn.route_tables.each do |rt|
  51. rt.routes.select{ |x| not x["vpcPeeringConnectionId"].nil? }.each do |r|
  52. if Peer.where(peer_id: r["vpcPeeringConnectionId"]).empty?
  53. mypeer = Peer.new(
  54. peer_id: r['vpcPeeringConnectionId']
  55. )
  56. mypeer.save
  57. else
  58. mypeer = Peer.where(peer_id: r["vpcPeeringConnectionId"]).first
  59. end
  60. puts r["vpcPeeringConnectionId"]
  61. ### TODO: make this more efficient
  62. match_count = 0
  63. PeerVpc.all.each do |x|
  64. if x.from_node.peer_id == r["vpcPeeringConnectionId"] and x.to_node.vpc_id == rt.vpc_id
  65. puts "matched #{x.from_node.peer_id} and #{x.to_node.vpc_id}"
  66. match_count += 1
  67. end
  68. end
  69. if match_count < 1
  70. PeerVpc.create(from_node: mypeer, to_node: Vpc.where(vpc_id: rt.vpc_id).first)
  71. end
  72. end
  73. end
  74. end
  75. end
  76. end
  77. def load
  78. aws = NeoInfra::Aws.new
  79. @cfg.accounts.each do |account|
  80. base_conf = {
  81. provider: 'AWS',
  82. aws_access_key_id: account[:key],
  83. aws_secret_access_key: account[:secret]
  84. }
  85. aws.regions.each do |region|
  86. region_conf = { region: region }
  87. begin
  88. new_conn = Fog::Compute.new(region_conf.merge(base_conf))
  89. rescue StandardError
  90. puts "Error loading VPCs in region: #{region}"
  91. next
  92. end
  93. # Get VPCs
  94. new_conn.vpcs.all.each do |vpc|
  95. next unless Vpc.where(vpc_id: vpc.id).empty?
  96. vpc_name = if vpc.tags.empty?
  97. vpc.id
  98. elsif vpc.tags.key? 'Name'
  99. vpc.tags['Name']
  100. else
  101. vpc.id
  102. end
  103. vpc_id = Vpc.new(
  104. vpc_id: vpc.id,
  105. name: vpc_name,
  106. cidr: vpc.cidr_block,
  107. state: vpc.state,
  108. default: vpc.is_default.to_s
  109. )
  110. vpc_id.save
  111. AccountVpc.create(from_node: vpc_id, to_node: AwsAccount.where(name: account[:name]).first)
  112. VpcRegion.create(from_node: vpc_id, to_node: Region.where(region: region).first)
  113. end
  114. # Get all Subnets
  115. new_conn.subnets.all.each do |subnet|
  116. next unless Subnet.where(subnet_id: subnet.subnet_id).empty?
  117. subnet_name = if subnet.tag_set.empty?
  118. subnet.subnet_id
  119. elsif subnet.tag_set.key? 'Name'
  120. subnet.tag_set['Name']
  121. else
  122. subnet.subnet_id
  123. end
  124. sn = Subnet.new(
  125. subnet_id: subnet.subnet_id,
  126. cidr: subnet.cidr_block,
  127. name: subnet_name,
  128. ip_count: subnet.available_ip_address_count,
  129. state: subnet.state
  130. )
  131. sn.save
  132. begin
  133. VpcSubnet.create(from_node: sn, to_node: Vpc.where(vpc_id: subnet.vpc_id).first)
  134. SubnetAz.create(from_node: sn, to_node: Az.where(az: subnet.availability_zone).first)
  135. rescue Exception => e
  136. # Handle the case of hanging subnets
  137. puts "Account #{account[:name]} couldn't load the following subnet: #{e.message}"
  138. p subnet
  139. end
  140. end
  141. end
  142. end
  143. end
  144. end
  145. end