Class: Awful::Elb
  
  
  
  
    
      Constant Summary
      collapse
    
    
      
        - COLORS =
          
        
 
        {
  InService:    :green,
  OutOfService: :red,
} 
      
    
  
  
    
      Instance Method Summary
      collapse
    
    
  
  
  
  
  
  
  
  
  
  Methods inherited from Cli
  #initialize, #ll, #version
  
  Constructor Details
  
    This class inherits a constructor from Awful::Cli
  
 
  
    Instance Method Details
    
      
  
  
    #create(name)  ⇒ Object 
  
  
  
  
    
      
102
103
104
105
106
107
108
109
110
111
112 
     | 
    
      # File 'lib/awful/elb.rb', line 102
def create(name)
  whitelist = %i[load_balancer_name listeners availability_zones subnets security_groups scheme tags]
  opt = load_cfg
  opt[:load_balancer_name] = name
  opt[:listeners] = opt.fetch(:listener_descriptions, []).map { |l| l[:listener] }
  opt.delete(:availability_zones) unless opt.fetch(:subnets, []).empty?
  opt = remove_empty_strings(opt)
  opt = only_keys_matching(opt, whitelist)
  elb.create_load_balancer(opt).map(&:dns_name).flatten.output { |dns| puts dns }
  health_check(name) if opt[:health_check]
end
     | 
  
 
    
      
  
  
    #delete(name)  ⇒ Object 
  
  
  
  
    
      
129
130
131
132
133 
     | 
    
      # File 'lib/awful/elb.rb', line 129
def delete(name)
  if yes? "really delete ELB #{name}?", :yellow
    elb.delete_load_balancer(load_balancer_name: name)
  end
end
     | 
  
 
    
      
  
  
    #deregister(name, *instances)  ⇒ Object 
  
  
  
  
    
      
141
142
143 
     | 
    
      # File 'lib/awful/elb.rb', line 141
def deregister(name, *instances)
  elb.deregister_instances_from_load_balancer(load_balancer_name: name, instances: instance_ids(*instances))
end 
     | 
  
 
    
      
  
  
    #dns(name)  ⇒ Object 
  
  
  
  
    
      
95
96
97
98
99 
     | 
    
      # File 'lib/awful/elb.rb', line 95
def dns(name)
  all_matching_elbs(name).map(&:dns_name).output do |dns_names|
    puts dns_names
  end
end
     | 
  
 
    
      
  
  
    #dump(name)  ⇒ Object 
  
  
  
  
    
      
68
69
70
71
72
73
74 
     | 
    
      # File 'lib/awful/elb.rb', line 68
def dump(name)
  all_matching_elbs(name).map(&:to_hash).output do |elbs|
    elbs.each do |elb|
      puts YAML.dump(stringify_keys(elb))
    end
  end
end
     | 
  
 
    
      
  
  
    #health_check(name)  ⇒ Object 
  
  
  
  
    
      
120
121
122
123
124
125
126 
     | 
    
      # File 'lib/awful/elb.rb', line 120
def health_check(name)
  opt = load_cfg.merge(options.reject(&:nil?))
  hc = elb.configure_health_check(load_balancer_name: name, health_check: opt[:health_check])
  hc.map(&:health_check).flatten.first.output do |h|
    print_table h.to_hash
  end
end
     | 
  
 
    
      
  
  
    #instances(name)  ⇒ Object 
  
  
  
  
    
      
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65 
     | 
    
      # File 'lib/awful/elb.rb', line 46
def instances(name)
  instances = all_matching_elbs(name).map do |e|
    elb.describe_instance_health(load_balancer_name: e.load_balancer_name).map(&:instance_states)
  end.flatten
  if instances.empty?
    instances.output { puts 'no instances' }
  else
    instances_by_id = instances.inject({}) { |hash,instance| hash[instance.instance_id] = instance; hash }
    if options[:long]
      ec2.describe_instances(instance_ids: instances_by_id.keys).map(&:reservations).flatten.map(&:instances).flatten.map do |instance|
        health = instances_by_id[instance.instance_id]
        instance_name = tag_name(instance) || '-'
        [ instance.instance_id, instance_name, instance.public_ip_address, color(health.state), health.reason_code, health.description ]
      end.output { |list| print_table list }
    else
      instances_by_id.keys.output { |list| puts list }
    end
  end
end
     | 
  
 
    
      
  
  
    #ls(name = /./)  ⇒ Object 
  
  
  
  
    
      
32
33
34
35
36
37
38
39
40
41
42 
     | 
    
      # File 'lib/awful/elb.rb', line 32
def ls(name = /./)
  all_matching_elbs(name).output do |elbs|
    if options[:long]
      print_table elbs.map { |e|
        [e.load_balancer_name, e.instances.length, e.availability_zones.join(','), e.dns_name]
      }.sort
    else
      puts elbs.map(&:load_balancer_name).sort
    end
  end
end
     | 
  
 
    
      
  
  
    #register(name, *instances)  ⇒ Object 
  
  
  
  
    
      
136
137
138 
     | 
    
      # File 'lib/awful/elb.rb', line 136
def register(name, *instances)
  elb.register_instances_with_load_balancer(load_balancer_name: name, instances: instance_ids(*instances))
end 
     | 
  
 
    
      
  
  
    #state(name, *instances)  ⇒ Object 
  
  
  
  
    
      
147
148
149
150
151
152
153
154
155 
     | 
    
      # File 'lib/awful/elb.rb', line 147
def state(name, *instances)
  elb.describe_instance_health(load_balancer_name: name, instances: instance_ids(*instances)).instance_states.output do |list|
    if options[:long]
      print_table list.map { |i| [ i.instance_id, color(i.state), i.reason_code, i.description ] }
    else
      puts list.map(&:state)
    end
  end
end
     | 
  
 
    
      
  
  
    #tag(name, key)  ⇒ Object 
  
  
  
  
    
      
86
87
88
89
90
91
92 
     | 
    
      # File 'lib/awful/elb.rb', line 86
def tag(name, key)
  elb.describe_tags(load_balancer_names: [name]).tag_descriptions.first.tags.find do |tag|
    tag.key == key
  end.output do |tag|
    puts tag.value
  end
end
     | 
  
 
    
      
  
  
    
      
77
78
79
80
81
82
83 
     | 
    
      # File 'lib/awful/elb.rb', line 77
def tags(*names)
  elb.describe_tags(load_balancer_names: names).tag_descriptions.output do |tags|
    tags.each do |tag|
      puts YAML.dump(stringify_keys(tag.to_hash))
    end
  end
end
     |