Class: Shards::IpSearch
- Inherits:
-
Object
- Object
- Shards::IpSearch
- Defined in:
- lib/shards/ip_search.rb
Instance Attribute Summary collapse
-
#stage ⇒ Object
readonly
Returns the value of attribute stage.
Instance Method Summary collapse
- #all_servers_file ⇒ Object
- #base_path ⇒ Object
- #file_list ⇒ Object
-
#initialize(stage) ⇒ IpSearch
constructor
A new instance of IpSearch.
- #instances ⇒ Object
- #ips ⇒ Object
- #lb1_instances ⇒ Object
- #lb2_arns ⇒ Object
- #lb2_client ⇒ Object
- #lb2_instances ⇒ Object
- #lbs ⇒ Object
- #load_balancers ⇒ Object
- #target_group_instances ⇒ Object
- #target_groups_arns ⇒ Object
Constructor Details
#initialize(stage) ⇒ IpSearch
Returns a new instance of IpSearch.
11 12 13 |
# File 'lib/shards/ip_search.rb', line 11 def initialize stage @stage=stage end |
Instance Attribute Details
#stage ⇒ Object (readonly)
Returns the value of attribute stage.
9 10 11 |
# File 'lib/shards/ip_search.rb', line 9 def stage @stage end |
Instance Method Details
#all_servers_file ⇒ Object
23 24 25 |
# File 'lib/shards/ip_search.rb', line 23 def all_servers_file File.join base_path , 'all_servers.yml' end |
#base_path ⇒ Object
15 16 17 |
# File 'lib/shards/ip_search.rb', line 15 def base_path File.join ENV['ENGINEERING_ROOT_PATH'], ENV['PATH_TO_ENGINEERING_YAML'] , stage.engineering_dirname end |
#file_list ⇒ Object
19 20 21 |
# File 'lib/shards/ip_search.rb', line 19 def file_list Dir.glob( File.join base_path, "*production*.yml" ) end |
#instances ⇒ Object
92 93 94 |
# File 'lib/shards/ip_search.rb', line 92 def instances ( lb1_instances + lb2_instances ).uniq end |
#ips ⇒ Object
96 97 98 99 100 101 102 103 |
# File 'lib/shards/ip_search.rb', line 96 def ips client = Aws::EC2::Client.new region: lbs[:region] client.describe_instances({ instance_ids: instances }).reservations.map do |r| r.instances.map do |i| i.private_ip_address end end.flatten.uniq end |
#lb1_instances ⇒ Object
48 49 50 51 52 53 54 55 56 |
# File 'lib/shards/ip_search.rb', line 48 def lb1_instances client=Aws::ElasticLoadBalancing::Client.new region: lbs[:region] lbs[:list1].map do |name| client.describe_instance_health( { load_balancer_name: name }).instance_states.select do |state| state.state=='InService' end.map { |x| x.instance_id } end.flatten.uniq end |
#lb2_arns ⇒ Object
70 71 72 73 74 |
# File 'lib/shards/ip_search.rb', line 70 def lb2_arns lb2_client.describe_load_balancers({ names: lbs[:list2] }).load_balancers.map do |lb| lb.load_balancer_arn end end |
#lb2_client ⇒ Object
66 67 68 |
# File 'lib/shards/ip_search.rb', line 66 def lb2_client @lb2_client||=Aws::ElasticLoadBalancingV2::Client.new( region: lbs[:region] ) end |
#lb2_instances ⇒ Object
58 59 60 61 62 63 64 |
# File 'lib/shards/ip_search.rb', line 58 def lb2_instances if lbs[:list2].empty? [] else target_group_instances end end |
#lbs ⇒ Object
44 45 46 |
# File 'lib/shards/ip_search.rb', line 44 def lbs @lbs||= load_balancers end |
#load_balancers ⇒ Object
27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 |
# File 'lib/shards/ip_search.rb', line 27 def load_balancers region=nil list1=[] list2=[] file_list.each do |file| yaml=YAML.load_file file all_yaml=YAML.load_file all_servers_file yaml=all_yaml.merge(yaml) list1+= (yaml['loadbalancer_list'] || [] ) list2+= (yaml['loadbalancer2_list'] || [] ) region||= yaml['loadbalancer_region'] end { region: region, list1: list1.uniq, list2: list2.uniq } end |
#target_group_instances ⇒ Object
84 85 86 87 88 89 90 |
# File 'lib/shards/ip_search.rb', line 84 def target_group_instances target_groups_arns.map do |arn| lb2_client.describe_target_health({ target_group_arn: arn }).target_health_descriptions.select do |d| d.target_health.state='healthy' end.map { |d| d.target.id } end.flatten.uniq end |
#target_groups_arns ⇒ Object
76 77 78 79 80 81 82 |
# File 'lib/shards/ip_search.rb', line 76 def target_groups_arns lb2_arns.map do |arn| lb2_client.describe_target_groups({ load_balancer_arn: arn }).target_groups.map do |tg| tg.target_group_arn end end.flatten.uniq end |