asg-detailer 0.1.1 → 0.2.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 10d9c882e5169f6ce73a101f18168054c77f9002
4
- data.tar.gz: d94614d43bc22ea9d7468026c456f2ed18de5466
3
+ metadata.gz: 3f09748d2340e88c932e8a0ae9947cd9d41672aa
4
+ data.tar.gz: 62cf1c28aafb326da200b4a275897f967d3e248e
5
5
  SHA512:
6
- metadata.gz: ed68bd2e0b20caf16fbaa427197b9f58086ed6965fee437ac92bda0c2ac9a86a4b26e294c039d4eee9bb1276776a2f22b60a0c93a1920dc69c8557109f99fca4
7
- data.tar.gz: 29d51c7b48e8519d8c1e6116f47f9c2d9dff0ebd16c2e166add8b3a8351f7b4d702f45ba287c511ed8ebb7c142fb54bcc24b26f7770bff5db4f8743b9f517653
6
+ metadata.gz: c62520b9f68f20369e65dc33abebd1eebb8952c6d18048e8d2ae572fcae7e71c2ebe9a4c4395f2cac53b1ae81b639e1aa5cbd7d387461f1fed3fe7d929a87165
7
+ data.tar.gz: 081535680f07650d211aad16ddd342fb4316cb5eca385c9c03b009c78665145f40dcca2b10acb0f96463d727731e696ea0ea6a4c31407e4769ff9b3fab8e82c3
data/bin/asg-detailer CHANGED
@@ -1,12 +1,6 @@
1
1
  #!/usr/bin/env ruby
2
2
 
3
- require "bundler/setup"
4
- require "asg-detailer"
3
+ require 'bundler/setup'
4
+ require 'asg-detailer'
5
5
 
6
- if ARGV.count < 1
7
- puts "Usage: example <asg name>"
8
- exit 1
9
- end
10
-
11
- name = ARGV[0]
12
- Detailer.new(name).run
6
+ AsgDetailer::CLI.new(ARGV)
data/bin/console CHANGED
@@ -1,7 +1,7 @@
1
1
  #!/usr/bin/env ruby
2
2
 
3
- require "bundler/setup"
4
- require "asg-detailer"
3
+ require 'bundler/setup'
4
+ require 'asg-detailer'
5
5
  include AsgDetailer
6
6
 
7
7
  # You can add fixtures and/or initialization code here to make experimenting
@@ -11,5 +11,5 @@ include AsgDetailer
11
11
  # require "pry"
12
12
  # Pry.start
13
13
 
14
- require "irb"
14
+ require 'irb'
15
15
  IRB.start
data/lib/asg-detailer.rb CHANGED
@@ -1 +1,2 @@
1
- require_relative "asg-detailer/detailer.rb"
1
+ require_relative 'asg-detailer/detailer.rb'
2
+ require_relative 'asg-detailer/cli.rb'
@@ -0,0 +1,49 @@
1
+ require 'optparse'
2
+
3
+ include AsgDetailer
4
+
5
+ module AsgDetailer
6
+ class CLI
7
+ attr_accessor :conf
8
+
9
+ def initialize(args)
10
+ options = {}
11
+ opt_parser = OptionParser.new do |opts|
12
+ opts.banner = 'Usage: asg-detailer [ option ... ]'
13
+
14
+ opts.separator ''
15
+ opts.separator 'Specific options:'
16
+
17
+ opts.on('-a', '--asg STRING', String, 'The ASG name') do |asg|
18
+ options[:asg] = asg
19
+ end
20
+
21
+ opts.on('-j', '--json', 'Output data as JSON') do |json|
22
+ options[:json] = true
23
+ end
24
+
25
+ opts.on('-p', '--pretty-json', 'Output data as pretty_generated JSON') do |pretty|
26
+ options[:pretty] = true
27
+ end
28
+ end
29
+
30
+ opt_parser.parse!(args)
31
+ if options[:asg].nil?
32
+ puts opt_parser
33
+ exit
34
+ end
35
+
36
+ self.run(options)
37
+ end
38
+
39
+ def run(options)
40
+ if options[:pretty]
41
+ Detailer.new(options[:asg]).json_pretty
42
+ elsif options[:json]
43
+ puts Detailer.new(options[:asg]).json
44
+ else
45
+ Detailer.new(options[:asg]).print
46
+ end
47
+ end
48
+ end
49
+ end
@@ -1,13 +1,15 @@
1
- require_relative "infra.rb"
1
+ require_relative 'infra.rb'
2
2
 
3
3
  include AsgDetailer
4
4
 
5
5
  module AsgDetailer
6
6
  class Detailer
7
7
  attr_reader :name
8
+ attr_reader :data
8
9
 
9
10
  def initialize(name, options = {})
10
11
  @name = name
12
+ @data = nil
11
13
  @infra = Infra.new
12
14
  end
13
15
 
@@ -15,56 +17,67 @@ module AsgDetailer
15
17
  detail_asg
16
18
  end
17
19
 
20
+ def print
21
+ self.run
22
+ print_details
23
+ end
24
+
25
+ def json
26
+ self.run
27
+ @data ? @data.to_json : nil
28
+ end
29
+
30
+ def json_pretty
31
+ self.run
32
+ puts JSON.pretty_generate(@data)
33
+ end
34
+
18
35
  private
19
36
 
20
37
  def detail_asg
38
+ @data ||= Hash.new
39
+ @data[:auto_scaling_groups] = []
21
40
  resp = @infra.query_asg(@name)
22
41
 
23
42
  # should be only one, but returns array...
24
43
  resp[:auto_scaling_groups].each do |a|
25
- puts "Autoscaling Group: #{a[:auto_scaling_group_name]}"
26
- puts "min size: #{a[:min_size]}"
27
- puts "max size: #{a[:max_size]}"
28
- puts "desired capacity: #{a[:desired_capacity]}"
29
- puts "subnets: #{a[:vpc_zone_identifier]}"
30
-
31
- detail_lc(a[:launch_configuration_name])
32
- detail_lbs(a)
44
+ asg = Hash.new
45
+ asg[:name] = a[:auto_scaling_group_name]
46
+ asg[:min_size] = a[:min_size]
47
+ asg[:max_size] = a[:max_size]
48
+ asg[:desired_capacity] = a[:desired_capacity]
49
+ asg[:subnets] = a[:vpc_zone_identifier]
50
+ asg[:lc] = detail_lc(a[:launch_configuration_name])
51
+ asg[:instances] = detail_instances(a)
52
+ asg[:load_balancers] = detail_lbs(a, asg[:instances])
53
+ @data[:auto_scaling_groups] << asg
33
54
  end
34
55
  end
35
56
 
36
57
  def detail_lc(lc)
37
- puts
38
- puts "Launch Configuration: #{lc}"
39
-
40
- resp = @infra.query_lc(lc)
41
-
42
- # should be only one, but returns array...
43
- resp[:launch_configurations].each do |l|
44
- puts "instance type: #{l[:instance_type]}"
45
- puts "AMI: #{l[:image_id]}"
46
- puts "security groups: #{l[:security_groups]}"
47
- end
48
- end
49
-
50
- # returns the instance health for any of the instances in the LB
51
- def detail_lbs(a)
52
- instance_health = Hash.new
53
- a[:load_balancer_names].each do |n|
54
- puts
55
- puts "Load Balancer: #{n}"
56
-
57
- resp = @infra.query_instance_health(n)
58
- resp.instance_states.each do |i|
59
- instance_health[i[:instance_id]] = i[:state]
58
+ lc_hash = {
59
+ name: 'N/A',
60
+ instance_type: 'N/A',
61
+ image_id: 'N/A',
62
+ security_groups: 'N/A'
63
+ }
64
+ unless lc.empty?
65
+ resp = @infra.query_lc(lc)
66
+
67
+ # should be only one, but returns array...
68
+ resp[:launch_configurations].each do |l|
69
+ lc_hash[:name] = l[:launch_configuration_name]
70
+ lc_hash[:instance_type] = l[:instance_type]
71
+ lc_hash[:image_id] = l[:image_id]
72
+ lc_hash[:security_groups] = l[:security_groups]
60
73
  end
61
74
  end
62
- detail_instances(a, instance_health)
75
+ return lc_hash
63
76
  end
64
77
 
65
- def detail_instances(a, instance_health)
66
- puts " Instances:"
67
- ids = []
78
+ def detail_instances(a)
79
+ instances = Hash.new
80
+ ids = Array.new
68
81
  a[:instances].map { |i| ids.push i[:instance_id] }
69
82
 
70
83
  unless ids.empty?
@@ -73,14 +86,86 @@ module AsgDetailer
73
86
 
74
87
  resp[:reservations].each do |r|
75
88
  r[:instances].each do |i|
76
- ip = i[:private_ip_address].empty? ? 'IP is N/A' : i[:private_ip_address]
77
- health = instance_health[i[:instance_id]] ? instance_health[i[:instance_id]] : "State is N/A (Not in LB)"
78
- puts " InstanceID: #{i[:instance_id]} : #{ip} : #{health}"
89
+ instance = Hash.new
90
+ if i[:private_ip_address]
91
+ instance[:ip] = i[:private_ip_address].empty? ? 'IP is N/A' : i[:private_ip_address]
92
+ else
93
+ instance[:ip] = 'IP is N/A'
94
+ end
95
+ instance[:image_id] = i[:image_id]
96
+ instances[i[:instance_id]] = instance
79
97
  end
80
98
  end
81
99
  rescue Aws::EC2::Errors::InvalidInstanceIDNotFound
82
100
  end
83
101
  end
102
+ return instances
103
+ end
104
+
105
+ # returns the instance health for any of the instances in the LB
106
+ def detail_lbs(a, instances)
107
+ lbs = Hash.new
108
+ a[:load_balancer_names].each do |n|
109
+ lb = Hash.new
110
+ resp = @infra.query_instance_health(n)
111
+ resp.instance_states.each do |i|
112
+ if instances.has_key?(i[:instance_id])
113
+ lb[i[:instance_id]] = { 'state': i[:state] }
114
+ end
115
+ end
116
+ lbs[n] = lb
117
+ end
118
+ return lbs
119
+ end
120
+
121
+ def print_details
122
+ # should be only one, but returns array...
123
+ @data[:auto_scaling_groups].each do |a|
124
+ puts "Autoscaling Group: #{a[:name]}"
125
+ puts "min size: #{a[:min_size]}"
126
+ puts "max size: #{a[:max_size]}"
127
+ puts "desired capacity: #{a[:desired_capacity]}"
128
+ puts "subnets: #{a[:subnets]}"
129
+
130
+ print_lc(a[:lc])
131
+ print_instances(a[:instances])
132
+ print_lbs(a)
133
+ end
134
+ end
135
+
136
+ def print_lc(lc)
137
+ puts
138
+ puts "Launch Configuration: #{lc[:name]}"
139
+
140
+ puts "instance type: #{lc[:instance_type]}"
141
+ puts "AMI: #{lc[:image_id]}"
142
+ puts "security groups: #{lc[:security_groups]}"
143
+ end
144
+
145
+ def print_instances(instances)
146
+ puts
147
+ puts 'Instances:'
148
+ instances.each do |k, v|
149
+ puts " InstanceID: #{k} : #{v[:image_id]} : #{v[:ip]}"
150
+ end
151
+ end
152
+
153
+ # returns the instance health for any of the instances in the LB
154
+ def print_lbs(a)
155
+ a[:load_balancers].each do |k, v|
156
+ puts
157
+ puts "Load Balancer: #{k}"
158
+ print_lb_instances(a, v)
159
+ end
160
+ end
161
+
162
+ def print_lb_instances(a, instances)
163
+ puts ' Instances:'
164
+ instances.each do |k, v|
165
+ if a[:instances].has_key?(k)
166
+ puts " InstanceID: #{k} : #{a[:instances][k][:ip]} : #{v[:state]}"
167
+ end
168
+ end
84
169
  end
85
170
  end
86
171
  end
@@ -1,4 +1,4 @@
1
- require "aws-sdk"
1
+ require 'aws-sdk'
2
2
 
3
3
  module AsgDetailer
4
4
  class Infra
@@ -1,3 +1,3 @@
1
1
  module AsgDetailer
2
- VERSION = "0.1.1"
2
+ VERSION = '0.2.0'
3
3
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: asg-detailer
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.1
4
+ version: 0.2.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Chris Horton
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2017-01-23 00:00:00.000000000 Z
11
+ date: 2017-02-24 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler
@@ -111,6 +111,7 @@ files:
111
111
  - bin/console
112
112
  - bin/setup
113
113
  - lib/asg-detailer.rb
114
+ - lib/asg-detailer/cli.rb
114
115
  - lib/asg-detailer/detailer.rb
115
116
  - lib/asg-detailer/infra.rb
116
117
  - lib/asg-detailer/version.rb