aly 0.1.3 → 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
  SHA256:
3
- metadata.gz: 9259e511caf4ff304c7f0e3abb279ee2a72bdededc55f289a52b9e85b65d7d7c
4
- data.tar.gz: e4ba5cd7522b8bf53c02cca5eead898451fefdb344f56d31d09fc91e838be185
3
+ metadata.gz: a67b93ee1a1850b5f9af5afaffbd14d0d989f2394571f09c7a5d22382a128535
4
+ data.tar.gz: 837c13099835afd7b34f2e7db2143fb96ad1c208083ba84ee2bda0fd15eb1ed7
5
5
  SHA512:
6
- metadata.gz: 63289e66578eaca5b6c8be92e6416e939370fd7b2b6c2d0e1603fa6455740683db5320bc45a69309a36d1cf7e747ef48e41b42337f60d8267c2be8f8ebd483a6
7
- data.tar.gz: aca415bbc279e4e08069eb791b776487f5ac72680fa57989a3bdcb438e5172948f4e624a9080c3c4bc489b8e9141adc18cc28c498276b1ba061f94b4215be197
6
+ metadata.gz: 480f9262bdc049a8b6028fd3cbaca0b3ca7e6be60506464be6a51e65cb227add6e74447d43ded3bccb8870294c8874f0736522c2ed0ae9bae243c50e09d417c5
7
+ data.tar.gz: 93bd2d071c76d4eb30552e7512e061e583906470debe1cd1b59d9a4ee70f1bc3a1397099b33bf19a9fc443fe297624771c405492ad6981fee56847f0d398875a
data/.gitignore CHANGED
@@ -9,3 +9,4 @@
9
9
 
10
10
  # rspec failure tracking
11
11
  .rspec_status
12
+ /.idea/
data/Gemfile.lock CHANGED
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- aly (0.1.2)
4
+ aly (0.2.0)
5
5
  terminal-table (~> 1.8.0)
6
6
  thor (~> 0.20.0)
7
7
 
@@ -39,6 +39,7 @@ GEM
39
39
 
40
40
  PLATFORMS
41
41
  x86_64-darwin-20
42
+ x86_64-darwin-21
42
43
 
43
44
  DEPENDENCIES
44
45
  aly!
data/lib/aly/app.rb CHANGED
@@ -1,5 +1,7 @@
1
1
  require 'json'
2
2
  require 'terminal-table'
3
+ require 'socket'
4
+
3
5
 
4
6
  class Array
5
7
  def table
@@ -51,6 +53,7 @@ module Aly
51
53
  end
52
54
 
53
55
  def eip(*args, **options)
56
+ puts 'EIPs:'
54
57
  raw_out = exec('vpc', 'DescribeEipAddresses', '--PageSize=100')
55
58
  selected = raw_out['EipAddresses']['EipAddress']
56
59
 
@@ -144,6 +147,142 @@ module Aly
144
147
  end
145
148
  end
146
149
 
150
+ def slb_contains_host?(host)
151
+ @slb.any? { |lb| lb['Address'] == host }
152
+ end
153
+
154
+ def ecs_contains_host?(host)
155
+ @ecs.any? { |item| item['AllIPs'].include?(host) }
156
+ end
157
+
158
+ def show_slb(host)
159
+ @listeners ||= exec('slb', 'DescribeLoadBalancerListeners', '--pager', 'path=Listeners')['Listeners']
160
+ lb = @slb.find { |e| e['Address'] == host }
161
+ listeners = @listeners.select { |e| e['LoadBalancerId'] == lb['LoadBalancerId'] }
162
+ background_servers = exec('slb', 'DescribeLoadBalancerAttribute', "--LoadBalancerId=#{lb['LoadBalancerId']}")['BackendServers']['BackendServer']
163
+
164
+ puts 'LoadBalancers:'
165
+ puts([{
166
+ Id: lb['LoadBalancerId'],
167
+ Name: lb['LoadBalancerName'],
168
+ Address: lb['Address'],
169
+ Listeners: listeners.size,
170
+ BackendServers: background_servers.map { |e| e['ServerId'] }.join(', ')
171
+ }].table.to_s)
172
+ puts
173
+
174
+ listeners_info = listeners.map do |listener|
175
+ {
176
+ Port: listener['ListenerPort'],
177
+ Protocol: listener['ListenerProtocol'],
178
+ Status: listener['ListenerStatus'],
179
+ BackendServerPort: listener['BackendServerPort'],
180
+ ForwardPort: listener.dig('HTTPListenerConfig', 'ForwardPort'),
181
+ VServerGroup: listener['VServerGroupId']
182
+ }
183
+ end
184
+
185
+ puts 'Configured Listeners:'
186
+ puts listeners_info.table.to_s
187
+ puts
188
+
189
+ listeners_info.each do |listener|
190
+ if listener[:VServerGroup]
191
+ vserver_group = exec('slb', 'DescribeVServerGroupAttribute', "--VServerGroupId=#{listener[:VServerGroup]}")["BackendServers"]["BackendServer"]
192
+ puts "VServerGroup #{listener[:VServerGroup]}:"
193
+ puts(vserver_group.map { |e|
194
+ {
195
+ EcInstanceId: e['ServerId'],
196
+ Port: e['Port'],
197
+ Weight: e['Weight'],
198
+ Type: e['Type']
199
+ }
200
+ }.table.to_s)
201
+ puts
202
+ end
203
+ end
204
+
205
+ ecs_ids = background_servers.map { |e| e['ServerId'] }
206
+ ecs_ids += listeners_info.flat_map { |e|
207
+ if e[:VServerGroup]
208
+ exec('slb', 'DescribeVServerGroupAttribute', "--VServerGroupId=#{e[:VServerGroup]}")["BackendServers"]["BackendServer"].map { |e| e['ServerId'] }
209
+ else
210
+ []
211
+ end
212
+ }
213
+ ecs_ids.uniq!
214
+
215
+ ecss = @ecs.select { |e| ecs_ids.include?(e['InstanceId']) }
216
+
217
+ puts "Referenced ECS Instances:"
218
+
219
+ puts ecss.map { |row|
220
+ {
221
+ Id: row['InstanceId'],
222
+ Name: row['InstanceName'],
223
+ PrivateIP: row['PrivateIP'].join(', '),
224
+ PublicIP: row['PublicIP'].join(', '),
225
+ CPU: row['Cpu'],
226
+ RAM: "#{row['Memory'] / 1024.0} GB"
227
+ }
228
+ }.table.to_s
229
+
230
+ end
231
+
232
+ def show_ecs(host)
233
+ selected = @ecs.select { |e| e['AllIPs'].include?(host) }.map do |row|
234
+ {
235
+ Id: row['InstanceId'],
236
+ Name: row['InstanceName'],
237
+ PrivateIP: row['PrivateIP'].join(', '),
238
+ PublicIP: row['PublicIP'].join(', '),
239
+ CPU: row['Cpu'],
240
+ RAM: "#{row['Memory'] / 1024.0} GB"
241
+ }
242
+ end
243
+ puts 'ECS Instances:'
244
+ puts selected.table&.to_s
245
+ end
246
+
247
+ def eip_contains_host?(host)
248
+ @eip.any? { |eip| eip['IpAddress'] == host }
249
+ end
250
+
251
+ def show(*args, **options)
252
+ @slb ||= exec('slb', 'DescribeLoadBalancers', '--pager')['LoadBalancers']['LoadBalancer']
253
+
254
+ @eip ||= exec('vpc', 'DescribeEipAddresses', '--PageSize=100')['EipAddresses']['EipAddress']
255
+ unless @ecs
256
+ @ecs = exec('ecs', 'DescribeInstances', '--pager')['Instances']['Instance']
257
+ @ecs.each do |item|
258
+ item['PrivateIP'] = (item['NetworkInterfaces']['NetworkInterface'] || []).map { |ni| ni['PrimaryIpAddress'] }
259
+ item['PublicIP'] = []
260
+ if ip = item['EipAddress']['IpAddress']
261
+ item['PublicIP'] << ip
262
+ end
263
+ if ips = item['PublicIpAddress']['IpAddress']
264
+ item['PublicIP'] += ips
265
+ end
266
+ item['AllIPs'] = item['PrivateIP'] + item['PublicIP']
267
+ end
268
+ end
269
+
270
+ host = IPSocket::getaddress(args.first)
271
+
272
+ puts "Host: #{args.first} resolves to #{host}" if host != args.first
273
+ puts
274
+
275
+ if slb_contains_host?(host)
276
+ show_slb(host)
277
+ elsif ecs_contains_host?(host)
278
+ show_ecs(host)
279
+ elsif eip_contains_host?(host)
280
+ eip(host)
281
+ else
282
+ puts "Not found: #{host}"
283
+ end
284
+ end
285
+
147
286
  def exec(command, sub_command, *args)
148
287
  JSON.parse(`aliyun #{command} #{sub_command} #{args.join(' ')}`)
149
288
  end
data/lib/aly/cli.rb CHANGED
@@ -20,6 +20,11 @@ module Aly
20
20
  App.new.start(options: options, command: :slb, args: [query])
21
21
  end
22
22
 
23
+ desc 'show', 'show resource information of host'
24
+ def show(host = nil)
25
+ App.new.start(options: options, command: :show, args: [host])
26
+ end
27
+
23
28
  class << self
24
29
  def main(args)
25
30
  start(args)
data/lib/aly/version.rb CHANGED
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module Aly
4
- VERSION = "0.1.3"
4
+ VERSION = "0.2.0"
5
5
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: aly
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.3
4
+ version: 0.2.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Liu Xiang
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2021-10-22 00:00:00.000000000 Z
11
+ date: 2022-03-10 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: thor
@@ -95,7 +95,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
95
95
  - !ruby/object:Gem::Version
96
96
  version: '0'
97
97
  requirements: []
98
- rubygems_version: 3.2.3
98
+ rubygems_version: 3.3.3
99
99
  signing_key:
100
100
  specification_version: 4
101
101
  summary: A simple wrapper for aliyun cli