Jelajahi Sumber

sample data with nodes

Chris Mague 7 tahun lalu
induk
melakukan
d4c3708543
2 mengubah file dengan 78 tambahan dan 7 penghapusan
  1. 34 3
      tasks/sample_data.rake
  2. 44 4
      tasks/sample_data.yaml

+ 34 - 3
tasks/sample_data.rake

@@ -1,6 +1,7 @@
 # frozen_string_literal: true
 
 require 'yaml'
+require 'pp'
 
 namespace :sample_data do
 
@@ -58,7 +59,7 @@ namespace :sample_data do
   task :subnets do
     puts 'loading subnets'
     sample_data['subnets'].each do |s|
-    	next unless Subnet.where(vpc_id: s['subnet_id']).empty?
+    	next unless Subnet.where(subnet_id: s['subnet_id']).empty?
       sn = Subnet.new(
         subnet_id: s['subnet_id'],
         cidr: s['cidr'],
@@ -76,11 +77,41 @@ namespace :sample_data do
     j = NeoInfra::Aws.new
   end
 
+  task :ssh_keys do
+    puts 'loading ssh_keys'
+    sample_data['ssh_keys'].each do |k|
+    	next unless SshKey.where(name: k['name']).empty?
+      s = SshKey.new(
+        name: k['name'],
+        account: k['account'],
+      )
+      s.save
+      SshKeyAccount.create(from_node: s, to_node: AwsAccount.where(name: k['account']).first)
+    end
+
+  end
+
   task :nodes do
     puts 'loading nodes'
-    #j = NeoInfra::Nodes.new
+    sample_data['nodes'].each do |n|
+    	next unless Node.where(node_id: n['node_id']).empty?
+      node = Node.new(
+        node_id: n['node_id'],
+        name:  n['name'],
+        ip:  n['ip'],
+        public_ip: n['public_ip'],
+        size: n['size'],
+        state: 'running',
+        ami: n['ami'],
+      )
+      node.save
+      NodeAccount.create(from_node: node, to_node: AwsAccount.where(name: n['account']).first)
+      NodeSubnet.create(from_node: node, to_node: Subnet.where(subnet_id: n['subnet_id']).first)
+      NodeAz.create(from_node: node, to_node: Az.where(az: n['az']).first)
+      NodeSshKey.create(from_node: node, to_node: SshKey.where(name: n['ssh_key']).first)
+    end
   end
 
   desc 'Load Sample Data'
-  task all: %i[accounts regions vpcs subnets buckets nodes]
+  task all: %i[accounts regions vpcs subnets ssh_keys buckets nodes]
 end

+ 44 - 4
tasks/sample_data.yaml

@@ -1,7 +1,7 @@
 ---
 accounts:
-  - :name: account1
-  - :name: account2
+  - :name: production
+  - :name: testing
 
 regions:
   - us-east-1:
@@ -14,6 +14,14 @@ regions:
     - us-west-1a
     - us-west-1c
 
+ssh_keys:
+  - name: prod1
+    account: production
+  - name: prod2
+    account: production
+  - name: test-key
+    account: testing
+
 vpcs:
   - vpc_id: vpc-21121
     name: prod-vpc-1
@@ -21,14 +29,14 @@ vpcs:
     default: false
     cidr: 192.168.0.0/24
     region: us-east-1
-    account: account1
+    account: production
   - vpc_id: vpc-21122
     name: test-vpc-1
     state: available
     default: false
     cidr: 192.168.5.0/24
     region: us-west-1
-    account: account2
+    account: testing
 
 subnets:
   - subnet_id: subnet-444444
@@ -66,3 +74,35 @@ subnets:
     state: avaliable
     az: us-east-1d
     vpc_id: vpc-21121
+
+nodes:
+  - node_id: i-1111111111
+    name: web1
+    ip: 192.168.5.2
+    public_ip: 8.8.8.8
+    size: m1.medium
+    ami: ami-12345678
+    subnet_id: subnet-444444
+    az: us-west-1a
+    ssh_key: test-key
+    account: testing
+  - node_id: i-1111111112
+    name: web2
+    ip: 192.168.5.3
+    public_ip: 8.8.8.4
+    size: m1.medium
+    ami: ami-12345678
+    subnet_id: subnet-444444
+    az: us-west-1a
+    ssh_key: test-key
+    account: testing
+  - node_id: i-1111111113
+    name: app1
+    ip: 192.168.5.201
+    public_ip: ""
+    size: m1.medium
+    ami: ami-12345678
+    subnet_id: subnet-444445
+    az: us-west-1a
+    ssh_key: test-key
+    account: testing