aws-graph 0.0.4 → 0.0.5

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: e149eefa7e2a3cb4e32686669a74e5d53a6a1323
4
- data.tar.gz: 1f4d5efd58427441d0e87bd52cd847663e97ef4d
3
+ metadata.gz: 34764f061c1a14cd60b9bad3e1d518b94f94ef2e
4
+ data.tar.gz: 937bad8f4c61305a6942f1bec4a9263bbcb91e87
5
5
  SHA512:
6
- metadata.gz: 198e3f0b5fa42871d854579d760f92963186a55c82df3e27de44d954f7e41edc12c86fe57d0c814fa049e63b4c59d12dd49995bc5dd2bc274463fd5a17f35099
7
- data.tar.gz: 87ec6363dd2287af306c8524a61271cc01007d31d3cc09c98e13a1f1536ea5af493468b032844cd046381f9a8a023a10caf4a4faa68089cff13cb1cf9c093c08
6
+ metadata.gz: 760ef1a2d7ed9745a184d2b5e4af9e823cf1d1d0eae90c74ab365de3901f4a0188351c313cf8d6911184028818d17a096825e2f646611a067064d912bd9d7001
7
+ data.tar.gz: df0b653a9918730670b02710c362c408c7e4af050a716597ca25d7c4cb7780693ec9e809776cbd10150a3d3e33865b32e04315bbce0b6885e196538a3d0b1dd4
@@ -19,8 +19,6 @@ module AwsGraph
19
19
 
20
20
  protected
21
21
  def sg()
22
- gv = Gviz.new(:AWS, :digraph)
23
-
24
22
  ec2_instances = @ec2.instances # EC2 instances
25
23
  vpcs = @ec2.vpcs # VPC Collection
26
24
  security_groups = @ec2.security_groups # EC2 security groups
@@ -31,179 +29,198 @@ module AwsGraph
31
29
 
32
30
  secret = options[:secret]
33
31
 
34
- gv.graph do
35
- global layout:'fdp', overlap:false, compound:true, rankdir:'LR'
36
- edges lhead: '', ltail: ''
37
- nodes shape: 'box'
32
+ @formated = {}
33
+ sg_hash = {}
34
+ lb_sg = nil
38
35
 
39
- sg_hash = {}
36
+ # Create EC2 security group cluster
37
+ @formated[:security_groups] = {}
38
+ security_groups.each do | sg |
39
+ cluster_id = 'cluster' + sg.id.gsub(/[-\/]/,'')
40
+ sg_hash[sg.id] = cluster_id
41
+
42
+ if sg.vpc_id
43
+ print "v"
44
+ label = sg.name + '[' + sg.id + ']' + '[vpc]'
45
+ else
46
+ print "."
47
+ label = sg.name + '[' + sg.id + ']'
48
+ end
49
+
50
+ @formated[:security_groups][sg.id.to_sym] = {
51
+ label: label,
52
+ vpc_id: sg.vpc_id,
53
+ instances: {},
54
+ inbounds: {},
55
+ }
56
+ end
40
57
 
41
- # Create EC2 security group cluster
42
- security_groups.each do | sg |
43
- cluster_id = 'cluster' + sg.id.gsub(/[-\/]/,'')
44
- sg_hash[sg.id] = cluster_id
45
-
46
- if sg.vpc_id
47
- print "v"
48
- color = '#4B75B9'
49
- label = Util.new.label(sg.name + '[' + sg.id + ']', secret) + '[vpc]'
50
- style = 'rounded,bold'
51
- else
52
- print "."
53
- color = '#333333'
54
- label = Util.new.label(sg.name + '[' + sg.id + ']', secret)
55
- style = 'rounded,bold'
56
- end
57
-
58
- subgraph(cluster_id.to_sym) do
59
- global label: label, style: style, color: color
60
- end
58
+ # Create RDS security group cluster
59
+ db_security_groups[:db_security_groups].each do | db_sg |
60
+ print "."
61
+ if db_sg[:vpc_id]
62
+ print "v"
63
+ label = db_sg[:db_security_group_name] + '[vpc]'
64
+ else
65
+ print "."
66
+ label = db_sg[:db_security_group_name]
61
67
  end
68
+ @formated[:security_groups][db_sg[:db_security_group_name].to_sym] = {
69
+ label: label,
70
+ vpc_id: db_sg[:vpc_id],
71
+ instances: {},
72
+ inbounds: {},
73
+ }
74
+ end
62
75
 
63
- # Create RDS security group cluster
64
- db_security_groups[:db_security_groups].each do | db_sg |
76
+ # Append EC2 to EC2 security group
77
+ ec2_instances.each do | e |
78
+ e.security_groups.each do | sg |
65
79
  print "."
66
- cluster_id = 'cluster' + db_sg[:db_security_group_name].gsub(/[-\/]/,'')
67
- if db_sg[:vpc_id]
68
- print "v"
69
- color = '#4B75B9'
70
- label = Util.new.label(db_sg[:db_security_group_name], secret) + '[vpc]'
71
- style = 'rounded,bold'
72
- else
73
- print "."
74
- color = '#333333'
75
- label = Util.new.label(db_sg[:db_security_group_name], secret)
76
- style = 'rounded,bold'
77
- end
78
- subgraph(cluster_id.to_sym) do
79
- global label: label, style: style, color: color
80
+ label = '[' + e.id + ']'
81
+ e.tags.each do | t |
82
+ label = t[1] + '[' + e.id + ']' if t[0] == 'Name'
80
83
  end
84
+ @formated[:security_groups][sg.id.to_sym][:instances][e.id.to_sym] = {
85
+ label: label,
86
+ type: :ec2,
87
+ status: e.status,
88
+ }
81
89
  end
90
+ end
82
91
 
83
- # Append EC2 to EC2 security group
84
- ec2_instances.each do | e |
85
- if e.status == :running
86
- image_path = File.dirname(__FILE__) + '/ec2.png'
87
- else
88
- image_path = File.dirname(__FILE__) + '/ec2_disactive.png'
89
- end
92
+ # Append VPC EC2 to EC2 security group
93
+ vpcs.each do | vpc |
94
+ vpc.instances.each do | e |
90
95
  e.security_groups.each do | sg |
91
- print "."
92
- cluster_id = 'cluster' + sg.id.gsub(/[-\/]/,'')
93
- subgraph(cluster_id.to_sym) do
94
- node (sg.id + e.id).gsub(/[-\/]/, '').to_sym, label: Util.new.label(e.id, secret), shape: :none, image: image_path
96
+ print "v"
97
+ label = '[' + e.id + ']'
98
+ e.tags.each do | t |
99
+ label = t[1] + '[' + e.id + ']' if t[0] == 'Name'
95
100
  end
101
+ @formated[:security_groups][sg.id.to_sym][:instances][e.id.to_sym] = {
102
+ label: label,
103
+ type: :ec2,
104
+ status: e.status,
105
+ }
96
106
  end
97
107
  end
108
+ end
98
109
 
99
- # Append VPC EC2 to EC2 security group
100
- vpcs.each do | vpc |
101
- vpc.instances.each do | e |
102
- if e.status == :running
103
- image_path = File.dirname(__FILE__) + '/ec2.png'
104
- else
105
- image_path = File.dirname(__FILE__) + '/ec2_disactive.png'
106
- end
107
- e.security_groups.each do | sg |
108
- print "v"
109
- cluster_id = 'cluster' + sg.id.gsub(/[-\/]/,'')
110
- subgraph(cluster_id.to_sym) do
111
- node (sg.id + e.id).gsub(/[-\/]/, '').to_sym, label: Util.new.label(e.id, secret), shape: :none, image: image_path
112
- end
113
- end
114
- end
110
+ # Append RDS to RDS security group
111
+ db_instances[:db_instances].each do | r |
112
+ r[:db_security_groups].each do | db_sg |
113
+ print "."
114
+ @formated[:security_groups][db_sg[:db_security_group_name].to_sym][:instances][r[:db_instance_identifier].to_sym] = {
115
+ label: Util.new.label(r[:db_instance_identifier], secret),
116
+ type: :rds,
117
+ status: r[:db_instance_status],
118
+ }
115
119
  end
120
+ r[:vpc_security_groups].each do | sg |
121
+ print "v"
122
+ @formated[:security_groups][sg[:vpc_security_group_id].to_sym][:instances][r[:db_instance_identifier].to_sym] = {
123
+ label: Util.new.label(r[:db_instance_identifier], secret),
124
+ type: :rds,
125
+ status: r[:db_instance_status],
126
+ }
127
+ end
128
+ end
116
129
 
117
- # Append RDS to RDS security group
118
- db_instances[:db_instances].each do | r |
119
- r[:db_security_groups].each do | db_sg |
120
- print "."
121
- cluster_id = 'cluster' + db_sg[:db_security_group_name].gsub(/[-\/]/,'')
122
- image_path = File.dirname(__FILE__) + '/rds.png'
123
- subgraph(cluster_id.to_sym) do
124
- node (r[:db_instance_identifier]).gsub(/[-\/]/, '').to_sym, label: Util.new.label(r[:db_instance_identifier], secret), shape: :none, image: image_path
125
- end
126
- end
127
- r[:vpc_security_groups].each do | sg |
128
- print "v"
129
- cluster_id = 'cluster' + sg[:vpc_security_group_id].gsub(/[-\/]/,'')
130
- image_path = File.dirname(__FILE__) + '/rds.png'
131
- subgraph(cluster_id.to_sym) do
132
- node (r[:db_instance_identifier]).gsub(/[-\/]/, '').to_sym, label: Util.new.label(r[:db_instance_identifier], secret), shape: :none, image: image_path
130
+ # Add edges EC2 security group
131
+ security_groups.each do | sg |
132
+ ips = sg.ingress_ip_permissions # inbound permissions
133
+ ips.each do | ip |
134
+ # CDIR
135
+ ip.ip_ranges.each do | r |
136
+ unless @formated[:security_groups][sg.id.to_sym][:inbounds].has_key?(r.to_sym)
137
+ @formated[:security_groups][sg.id.to_sym][:inbounds][r.to_sym] = []
133
138
  end
139
+ @formated[:security_groups][sg.id.to_sym][:inbounds][r.to_sym].push({
140
+ port_range: ip.port_range,
141
+ protocol: ip.protocol,
142
+ })
134
143
  end
135
- end
136
144
 
137
- # Add edges EC2 security group
138
- security_groups.each do | sg |
139
- ips = sg.ingress_ip_permissions # inbound permissions
140
- ips.each do | ip |
141
-
142
- # EC2 security group -> EC2 security group
143
- ip.groups.each do | fromsg |
144
- next if fromsg.id == sg.id
145
- print "-"
146
- unless sg_hash[fromsg.id]
147
- # Unknown security group is amazon-elb/amazon-elb-sg
148
- cluster_id = 'cluster' + fromsg.id.gsub(/[-\/]/,'')
149
- sg_hash['amazon-elb/amazon-elb-sg'] = cluster_id
150
- subgraph(cluster_id.to_sym) do
151
- global label: Util.new.label('amazon-elb/amazon-elb-sg', false), style: 'rounded'
152
- end
153
- end
154
- from_cluster_id = 'cluster' + fromsg.id.gsub(/[-\/]/,'')
155
- to_cluster_id = 'cluster' + sg.id.gsub(/[-\/]/,'')
156
- route from_cluster_id.to_sym => to_cluster_id.to_sym
157
- edge (from_cluster_id + '_' + to_cluster_id).to_sym, label: Util.new.label(ip.port_range.to_s + '[' + ip.protocol.to_s + ']', secret)
145
+ # EC2 security group -> EC2 security group instances
146
+ ip.groups.each do | fromsg |
147
+ next if fromsg.id == sg.id
148
+ print "-"
149
+ unless sg_hash[fromsg.id]
150
+ # Unknown security group is amazon-elb/amazon-elb-sg
151
+ lb_sg = fromsg.id.to_sym
152
+ @formated[:security_groups][fromsg.id.to_sym] = {
153
+ label: 'amazon-elb/amazon-elb-sg',
154
+ instances: {},
155
+ inbounds: {},
156
+ }
158
157
  end
158
+ unless @formated[:security_groups][sg.id.to_sym][:inbounds].has_key?(fromsg.id.to_sym)
159
+ @formated[:security_groups][sg.id.to_sym][:inbounds][fromsg.id.to_sym] = []
160
+ end
161
+ @formated[:security_groups][sg.id.to_sym][:inbounds][fromsg.id.to_sym].push({
162
+ port_range: ip.port_range,
163
+ protocol: ip.protocol,
164
+ })
165
+ # route from_cluster_id.to_sym => to_cluster_id.to_sym
166
+ # edge (from_cluster_id + '_' + to_cluster_id).to_sym, color: '#005580', headlabel: Util.new.label(Util.new.ip_range(ip.port_range.to_s) + '[' + ip.protocol.to_s + ']', secret), fontcolor: '#005580'
159
167
  end
160
168
  end
169
+ end
161
170
 
162
- # EC2 security group -> RDS security group
163
- db_security_groups[:db_security_groups].each do | db_sg |
164
- print "-"
165
- db_sg[:ec2_security_groups].each do | sg |
166
- if sg[:ec2_security_group_id]
167
- from_cluster_id = 'cluster' + sg[:ec2_security_group_id].gsub(/[-\/]/,'')
168
- to_cluster_id = 'cluster' + db_sg[:db_security_group_name].gsub(/[-\/]/,'')
169
- route from_cluster_id.to_sym => to_cluster_id.to_sym
170
- edge (from_cluster_id + '_' + to_cluster_id).to_sym, label: 'RDS'
171
- else
172
- # なぜかdb_security_group_idが存在しないものがある
173
- security_groups.each do | s |
174
- if s.name == sg[:ec2_security_group_name]
175
- from_cluster_id = 'cluster' + s.id.gsub(/[-\/]/,'')
176
- to_cluster_id = 'cluster' + db_sg[:db_security_group_name].gsub(/[-\/]/,'')
177
- route from_cluster_id.to_sym => to_cluster_id.to_sym
178
- edge (from_cluster_id + '_' + to_cluster_id).to_sym, label: 'RDS'
171
+ # EC2 security group -> RDS security group
172
+ db_security_groups[:db_security_groups].each do | db_sg |
173
+ print "-"
174
+ db_sg[:ec2_security_groups].each do | sg |
175
+ if sg[:ec2_security_group_id]
176
+ unless @formated[:security_groups][db_sg[:db_security_group_name].to_sym][:inbounds].has_key?(sg[:ec2_security_group_id].to_sym)
177
+ @formated[:security_groups][db_sg[:db_security_group_name].to_sym][:inbounds][sg[:ec2_security_group_id].to_sym] = []
178
+ end
179
+ @formated[:security_groups][db_sg[:db_security_group_name].to_sym][:inbounds][sg[:ec2_security_group_id].to_sym].push({
180
+ port_range: 'RDS',
181
+ protocol: nil,
182
+ })
183
+ else
184
+ # なぜかdb_security_group_idが存在しないものがある
185
+ security_groups.each do | s |
186
+ if s.name == sg[:ec2_security_group_name]
187
+ unless @formated[:security_groups][db_sg[:db_security_group_name].to_sym][:inbounds].has_key?(s.id.to_sym)
188
+ @formated[:security_groups][db_sg[:db_security_group_name].to_sym][:inbounds][s.id.to_sym] = []
179
189
  end
190
+ @formated[:security_groups][db_sg[:db_security_group_name].to_sym][:inbounds][s.id.to_sym].push({
191
+ port_range: 'RDS',
192
+ protocol: nil,
193
+ })
180
194
  end
181
195
  end
182
196
  end
183
197
  end
198
+ end
184
199
 
185
- # Append ELB to ELB security group
186
- lbs.each do | lb |
187
- break unless sg_hash['amazon-elb/amazon-elb-sg']
188
- cluster_id = sg_hash['amazon-elb/amazon-elb-sg']
189
- image_path = File.dirname(__FILE__) + '/elb.png'
190
- subgraph(cluster_id.to_sym) do
191
- node lb.name.gsub('-', '').to_sym, label: Util.new.label(lb.name, secret), shape: :none, image: image_path
192
- end
193
- end
194
-
195
- puts ''
200
+ # Append ELB to ELB security group
201
+ lbs.each do | lb |
202
+ break unless lb_sg
203
+ @formated[:security_groups][lb_sg][:instances][lb.name.to_sym] = {
204
+ label: lb.name,
205
+ type: :elb,
206
+ status: nil,
207
+ }
196
208
  end
209
+
210
+ puts ''
197
211
  filename = File.basename options[:output], ".*"
198
- filepath = File.dirname options[:output]
212
+ dirpath = File.dirname options[:output]
199
213
  fileextname = File.extname options[:output]
200
214
  fileformat = fileextname.sub('.', '').to_sym
201
- unless fileformat == :dot
202
- gv.save(File.join(filepath, filename), fileformat)
203
- File.delete(File.join(filepath, "#{filename}.dot"))
215
+ case fileformat
216
+ when :dot
217
+ self.save_png File.join(dirpath, filename)
218
+ File.delete(File.join(dirpath, filename + '.png'))
219
+ when :png
220
+ self.save_png File.join(dirpath, filename)
221
+ File.delete(File.join(dirpath, filename + '.dot'))
204
222
  else
205
- gv.save(File.join(filepath, filename), :png)
206
- File.delete(File.join(filepath, "#{filename}.png"))
223
+
207
224
  end
208
225
  end
209
226
 
@@ -226,6 +243,66 @@ module AwsGraph
226
243
  :region => @config['aws_region'],
227
244
  )
228
245
  end
246
+
247
+ protected
248
+ def save_png(filepath)
249
+ secret = options[:secret]
250
+ f = @formated
251
+
252
+ gv = Gviz.new(:AWS, :digraph)
253
+ gv.graph do
254
+ global layout:'fdp', overlap:false, compound:true, rankdir:'LR'
255
+ edges lhead: '', ltail: ''
256
+ nodes shape: 'box'
257
+ f[:security_groups].each do | sg_id, sg |
258
+ cluster_id = 'cluster' + sg_id.to_s.gsub(/[-\/]/,'')
259
+ if sg[:vpc_id]
260
+ color = '#4B75B9'
261
+ label = Util.new.label(sg[:label], secret)
262
+ style = 'rounded,bold'
263
+ else
264
+ color = '#333333'
265
+ label = Util.new.label(sg[:label], secret)
266
+ style = 'rounded,bold'
267
+ end
268
+ subgraph(cluster_id.to_sym) do
269
+ global label: label, style: style, color: color
270
+ sg[:instances].each do | i_id, i |
271
+ case i[:type]
272
+ when :ec2
273
+ if i[:status] == :running
274
+ image_path = File.dirname(__FILE__) + '/ec2.png'
275
+ else
276
+ image_path = File.dirname(__FILE__) + '/ec2_disactive.png'
277
+ end
278
+ when :rds
279
+ image_path = File.dirname(__FILE__) + '/rds.png'
280
+ when :elb
281
+ image_path = File.dirname(__FILE__) + '/elb.png'
282
+ else
283
+ image_path = File.dirname(__FILE__) + '/ec2_disactive.png'
284
+ end
285
+ node (sg_id.to_s + i_id.to_s).gsub(/[-\/]/, '').to_sym, label: i[:label], shape: :none, image: image_path
286
+ end
287
+
288
+ sg[:inbounds].each do | ip, inbounds |
289
+
290
+ # Security Group -> Security Group
291
+ if /\Asg\-/.match(ip.to_s)
292
+ from_cluster_id = 'cluster' + ip.to_s.gsub(/[-\/]/,'')
293
+ route from_cluster_id.to_sym => cluster_id.to_sym
294
+ label = []
295
+ inbounds.each do | inbound |
296
+ label.push(Util.new.format_range(inbound[:port_range].to_s) + '(' + inbound[:protocol].to_s + ')')
297
+ end
298
+ edge (from_cluster_id + '_' + cluster_id).to_sym, color: '#005580', headlabel: label.join(','), fontcolor: '#005580'
299
+ end
300
+ end
301
+ end
302
+ end
303
+ end
304
+ gv.save(filepath, :png)
305
+ end
229
306
  end
230
307
 
231
308
  class Util
@@ -236,5 +313,12 @@ module AwsGraph
236
313
  return text
237
314
  end
238
315
  end
316
+ def format_range(ip_range)
317
+ if ip_range.sub(/\A[0-9]+\.\./,'') == ip_range.sub(/\.\.[0-9]+\z/,'')
318
+ return ip_range.sub(/\A[0-9]+\.\./,'')
319
+ else
320
+ return ip_range
321
+ end
322
+ end
239
323
  end
240
324
  end
@@ -1,3 +1,3 @@
1
1
  module AwsGraph
2
- VERSION = "0.0.4"
2
+ VERSION = "0.0.5"
3
3
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: aws-graph
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.4
4
+ version: 0.0.5
5
5
  platform: ruby
6
6
  authors:
7
7
  - k1LoW
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2014-04-02 00:00:00.000000000 Z
11
+ date: 2014-04-03 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler