lib/aly/app.rb in aly-0.2.2 vs lib/aly/app.rb in aly-0.2.3
- old
+ new
@@ -61,10 +61,11 @@
selected = selected.select do |item|
item.values_at('AllocationId', 'InstanceId', 'InstanceType', 'IpAddress').compact.any? { |e| query.any? { |q| e.include?(q) } }
end
end
+
if options['detail']
puts JSON.pretty_generate(selected)
else
net_intefraces = exec('ecs', 'DescribeNetworkInterfaces', '--pager', **options)['NetworkInterfaceSets']['NetworkInterfaceSet'].each_with_object({}) do |item, result|
result[item['NetworkInterfaceId']] = item
@@ -92,10 +93,14 @@
def slb(*args, **options)
raw_out = exec('slb', 'DescribeLoadBalancers', '--pager', **options)
selected = raw_out['LoadBalancers']['LoadBalancer'] || []
+ eips = exec('vpc', 'DescribeEipAddresses', "--PageSize=#{selected.size}", **options)['EipAddresses']['EipAddress'].each_with_object({}) do |item, result|
+ result[item['InstanceId']] = item['IpAddress']
+ end
+
listeners = (exec('slb', 'DescribeLoadBalancerListeners', '--pager', 'path=Listeners', **options)['Listeners'] || []).each_with_object({}) do |listener, result|
instance_id = listener['LoadBalancerId']
result[instance_id] ||= []
result[instance_id] << listener
end
@@ -138,36 +143,38 @@
{
Id: row['LoadBalancerId'],
Name: row['LoadBalancerName'],
Address: row['Address'],
+ Eip: eips[row['LoadBalancerId']] || '',
Listeners: listeners
}
end
puts selected.table&.to_s
end
end
def slb_contains_host?(host)
- @slb.any? { |lb| lb['Address'] == host }
+ @slb.any? { |lb| lb['Address'] == host || lb['Eip'] == host }
end
def ecs_contains_host?(host)
@ecs.any? { |item| item['AllIPs'].include?(host) }
end
def show_slb(host, **options)
@listeners ||= exec('slb', 'DescribeLoadBalancerListeners', '--pager', 'path=Listeners', **options)['Listeners'] || []
- lb = @slb.find { |e| e['Address'] == host }
+ lb = @slb.find { |e| e['Address'] == host || e['Eip'] == host }
listeners = @listeners.select { |e| e['LoadBalancerId'] == lb['LoadBalancerId'] }
background_servers = exec('slb', 'DescribeLoadBalancerAttribute', "--LoadBalancerId=#{lb['LoadBalancerId']}", **options)['BackendServers']['BackendServer']
puts 'LoadBalancers:'
puts([{
Id: lb['LoadBalancerId'],
Name: lb['LoadBalancerName'],
Address: lb['Address'],
+ Eip: lb['Eip'],
Listeners: listeners.size
}].table.to_s)
puts
if background_servers && background_servers.size > 0
@@ -255,10 +262,16 @@
def show(*args, **options)
@slb ||= exec('slb', 'DescribeLoadBalancers', '--pager', **options)['LoadBalancers']['LoadBalancer'] || []
@eip ||= exec('vpc', 'DescribeEipAddresses', '--PageSize=100', **options)['EipAddresses']['EipAddress'] || []
+
+ eip_map = @eip.each_with_object({}) { |eip, h| h[eip['InstanceId']] = eip['IpAddress'] }
+ @slb.each do |slb|
+ slb['Eip'] = eip_map[slb['LoadBalancerId']]
+ end
+
unless @ecs
@ecs = exec('ecs', 'DescribeInstances', '--pager', **options)['Instances']['Instance'] || []
@ecs.each do |item|
item['PrivateIP'] = (item['NetworkInterfaces']['NetworkInterface'] || []).map { |ni| ni['PrimaryIpAddress'] }
item['PublicIP'] = []
@@ -280,10 +293,10 @@
if slb_contains_host?(host)
show_slb(host, **options)
elsif ecs_contains_host?(host)
show_ecs(host)
elsif eip_contains_host?(host)
- eip(host)
+ eip(host, **options)
else
puts "Not found: #{host}"
end
end