123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152 |
- # frozen_string_literal: true
- require 'neoinfra'
- require 'vpc'
- require 'peers'
- require 'accounts'
- require 'fog-aws'
- require 'neo4j'
- # NeoInfra Account information
- module NeoInfra
- # Provide informations about the accounts available
- class Vpcs
- def initialize
- @cfg = NeoInfra::Config.new
- neo4j_url = "http://#{@cfg.neo4j[:host]}:#{@cfg.neo4j[:port]}"
- Neo4j::Session.open(:server_db, neo4j_url)
- end
- def non_default_vpc_count
- Vpc.all.collect(&:default).select { |y| y == 'false' }.length
- end
- def default_vpc_count
- Vpc.all.collect(&:default).select { |y| y == 'true' }.length
- end
- def list_vpcs
- node_counts = Hash.new(0)
- Node.all.each do |x|
- begin
- node_counts[x.subnet.subnet.name] += 1
- rescue
- foo = "noop"
- end
- end
- 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
- end
-
- def load_peers
- aws = NeoInfra::Aws.new
- @cfg.accounts.each do |account|
- base_conf = {
- provider: 'AWS',
- aws_access_key_id: account[:key],
- aws_secret_access_key: account[:secret]
- }
- aws.regions.each do |region|
- region_conf = { region: region }
- begin
- new_conn = Fog::Compute.new(region_conf.merge(base_conf))
- rescue StandardError
- puts "Error loading Peering in region: #{region}"
- next
- end
- new_conn.route_tables.each do |rt|
- rt.routes.select{ |x| not x["vpcPeeringConnectionId"].nil? }.each do |r|
- if Peer.where(peer_id: r["vpcPeeringConnectionId"]).empty?
- mypeer = Peer.new(
- peer_id: r['vpcPeeringConnectionId']
- )
- mypeer.save
- else
- mypeer = Peer.where(peer_id: r["vpcPeeringConnectionId"]).first
- end
- puts r["vpcPeeringConnectionId"]
- ### TODO: make this more efficient
- match_count = 0
- PeerVpc.all.each do |x|
- if x.from_node.peer_id == r["vpcPeeringConnectionId"] and x.to_node.vpc_id == rt.vpc_id
- puts "matched #{x.from_node.peer_id} and #{x.to_node.vpc_id}"
- match_count += 1
- end
- end
- if match_count < 1
- PeerVpc.create(from_node: mypeer, to_node: Vpc.where(vpc_id: rt.vpc_id).first)
- end
- end
- end
- end
- end
- end
- def load
- aws = NeoInfra::Aws.new
- @cfg.accounts.each do |account|
- base_conf = {
- provider: 'AWS',
- aws_access_key_id: account[:key],
- aws_secret_access_key: account[:secret]
- }
- aws.regions.each do |region|
- region_conf = { region: region }
- begin
- new_conn = Fog::Compute.new(region_conf.merge(base_conf))
- rescue StandardError
- puts "Error loading VPCs in region: #{region}"
- next
- end
- # Get VPCs
- new_conn.vpcs.all.each do |vpc|
- next unless Vpc.where(vpc_id: vpc.id).empty?
- vpc_name = if vpc.tags.empty?
- vpc.id
- elsif vpc.tags.key? 'Name'
- vpc.tags['Name']
- else
- vpc.id
- end
- vpc_id = Vpc.new(
- vpc_id: vpc.id,
- name: vpc_name,
- cidr: vpc.cidr_block,
- state: vpc.state,
- default: vpc.is_default.to_s
- )
- vpc_id.save
- AccountVpc.create(from_node: vpc_id, to_node: AwsAccount.where(name: account[:name]).first)
- VpcRegion.create(from_node: vpc_id, to_node: Region.where(region: region).first)
- end
- # Get all Subnets
- new_conn.subnets.all.each do |subnet|
- next unless Subnet.where(subnet_id: subnet.subnet_id).empty?
- subnet_name = if subnet.tag_set.empty?
- subnet.subnet_id
- elsif subnet.tag_set.key? 'Name'
- subnet.tag_set['Name']
- else
- subnet.subnet_id
- end
- sn = Subnet.new(
- subnet_id: subnet.subnet_id,
- cidr: subnet.cidr_block,
- name: subnet_name,
- ip_count: subnet.available_ip_address_count,
- state: subnet.state
- )
- sn.save
- begin
- VpcSubnet.create(from_node: sn, to_node: Vpc.where(vpc_id: subnet.vpc_id).first)
- SubnetAz.create(from_node: sn, to_node: Az.where(az: subnet.availability_zone).first)
- rescue Exception => e
- # Handle the case of hanging subnets
- puts "Account #{account[:name]} couldn't load the following subnet: #{e.message}"
- p subnet
- end
- end
- end
- end
- end
- end
- end
|