Browse Source

load up nodes

Chris Mague 7 years ago
parent
commit
60880b1d01
3 changed files with 139 additions and 1 deletions
  1. 12 1
      Rakefile
  2. 68 0
      lib/neoinfra/nodes.rb
  3. 59 0
      models/nodes.rb

+ 12 - 1
Rakefile

@@ -16,6 +16,7 @@ require 'rspec/core/rake_task'
 require 'neoinfra/accounts'
 require 'neoinfra/vpcs'
 require 'neoinfra/aws'
+require 'neoinfra/nodes'
 
 RuboCop::RakeTask.new(:rubocop) do |t|
   t.options = ['--display-cop-names']
@@ -28,29 +29,39 @@ end
 
 desc 'Load accounts into the neo4j container'
 task :load_accounts do
+  puts 'loading accounts'
   j = NeoInfra::Accounts.new
   j.load
 end
 
 desc 'Load VPCs into the neo4j container'
 task :load_vpcs do
+  puts 'loading vpcs'
   j = NeoInfra::Vpcs.new
   j.load
 end
 
 desc 'Load Region and Availability Zone information'
 task :load_regions do
+  puts 'loading regions'
   j = NeoInfra::Aws.new
   j.load_regions
 end
 
 desc 'Load S3 Buckets'
 task :load_buckets do
+  puts 'loading buckets'
   j = NeoInfra::Aws.new
   j.load_buckets
 end
 
+desc 'Load Nodes'
+task :load_nodes do
+  puts 'loading nodes'
+  j = NeoInfra::Nodes.new
+  j.load_nodes
+end
 
 desc 'Load Everything'
-task load_all: %i[load_accounts load_regions load_vpcs load_buckets]
+task load_all: %i[load_accounts load_regions load_vpcs load_buckets load_nodes]
 task full_test: %i[rubocop spec]

+ 68 - 0
lib/neoinfra/nodes.rb

@@ -0,0 +1,68 @@
+# frozen_string_literal: true
+
+require 'nodes'
+require 'accounts'
+require 'fog'
+require 'neo4j'
+require 'neoinfra/aws'
+
+# NeoInfra Account information
+module NeoInfra
+  # Provide informations about the accounts available
+  class Nodes
+    def load_nodes
+      aws = NeoInfra::Aws.new
+      @cfg = NeoInfra::Config.new
+      neo4j_url = "http://#{@cfg.neo4j[:host]}:#{@cfg.neo4j[:port]}"
+      Neo4j::Session.open(:server_db, neo4j_url)
+
+      @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 }
+          new_conn = Fog::Compute.new(region_conf.merge(base_conf))
+          new_conn.servers.all.each do |ec2|
+
+            if SshKey.where(name: ec2.key_name).empty?
+              s = SshKey.new(
+                name: ec2.key_name,
+                account: account[:name]
+              )
+              s.save
+              SshKeyAccount.create(from_node: s, to_node: AwsAccount.where(name: account[:name]).first)
+            end
+
+            next unless Node.where(node_id: ec2.id).empty?
+            node_name = if ec2.tags.empty?
+                         ec2.id
+                       else
+                         if ec2.tags.key? 'Name'
+                           ec2.tags['Name']
+                         else
+                           ec2.id
+                         end
+                       end
+            n = Node.new(
+              name: node_name,
+              node_id: ec2.id,
+              ip: ec2.private_ip_address,
+              public_ip: ec2.public_ip_address,
+              size: ec2.flavor_id,
+              state: ec2.state,
+              ami: ec2.image_id,
+            )
+            n.save
+            NodeAccount.create(from_node: n, to_node: AwsAccount.where(name: account[:name]).first)
+            NodeSubnet.create(from_node: n, to_node: Subnet.where(subnet_id: ec2.subnet_id).first)
+            NodeAz.create(from_node: n, to_node: Az.where(az: ec2.availability_zone).first)
+            NodeSshKey.create(from_node: n, to_node: SshKey.where(name: ec2.key_name).first)
+          end
+        end
+      end
+    end
+  end
+end

+ 59 - 0
models/nodes.rb

@@ -0,0 +1,59 @@
+# frozen_string_literal: true
+
+require 'neo4j'
+
+class Node
+  include Neo4j::ActiveNode
+  property :node_id, constraint: :unique
+  property :name
+  property :ip
+  property :public_ip
+  property :size
+  property :state
+  property :ami
+  has_one :out, :subnet, rel_class: :NodeSubnet
+  has_one :out, :az, rel_class: :NodeAz
+  has_one :out, :sshkey, rel_class: :NodeSshKey
+  has_one :out, :account, rel_class: :NodeAccount
+end
+
+class SshKey
+  include Neo4j::ActiveNode
+  property :name
+  property :account
+end
+
+class NodeSubnet
+  include Neo4j::ActiveRel
+  from_class :Node
+  to_class :Subnet
+  type :subnet
+end
+
+class NodeAz
+  include Neo4j::ActiveRel
+  from_class :Node
+  to_class :Az
+  type :az
+end
+
+class NodeSshKey
+  include Neo4j::ActiveRel
+  from_class :Node
+  to_class :SshKey
+  type :sshkey
+end
+
+class NodeAccount
+  include Neo4j::ActiveRel
+  from_class :Node
+  to_class :AwsAccount
+  type :owner
+end
+
+class SshKeyAccount
+  include Neo4j::ActiveRel
+  from_class :SshKey
+  to_class :AwsAccount
+  type :owner
+end