Class: Awful::Elb

Inherits:
Cli show all
Defined in:
lib/awful/elb.rb

Constant Summary collapse

COLORS =
{
  InService:    :green,
  OutOfService: :red,
}

Instance Method Summary collapse

Methods inherited from Cli

#initialize

Constructor Details

This class inherits a constructor from Awful::Cli

Instance Method Details

#create(name) ⇒ Object



106
107
108
109
110
111
112
113
114
115
116
# File 'lib/awful/elb.rb', line 106

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



133
134
135
136
137
# File 'lib/awful/elb.rb', line 133

def delete(name)
  if yes? "really delete ELB #{name}?", :yellow
    elb.delete_load_balancer(load_balancer_name: name)
  end
end

#deregister(name, *instances) ⇒ Object



145
146
147
# File 'lib/awful/elb.rb', line 145

def deregister(name, *instances)
  elb.deregister_instances_from_load_balancer(load_balancer_name: name, instances: instance_ids(*instances))
end

#dns(name) ⇒ Object



99
100
101
102
103
# File 'lib/awful/elb.rb', line 99

def dns(name)
  all_matching_elbs(name).map(&:dns_name).output do |dns_names|
    puts dns_names
  end
end

#dump(name) ⇒ Object



72
73
74
75
76
77
78
# File 'lib/awful/elb.rb', line 72

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



124
125
126
127
128
129
130
# File 'lib/awful/elb.rb', line 124

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



50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
# File 'lib/awful/elb.rb', line 50

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



36
37
38
39
40
41
42
43
44
45
46
# File 'lib/awful/elb.rb', line 36

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



140
141
142
# File 'lib/awful/elb.rb', line 140

def register(name, *instances)
  elb.register_instances_with_load_balancer(load_balancer_name: name, instances: instance_ids(*instances))
end

#state(name, *instances) ⇒ Object



151
152
153
154
155
156
157
158
159
# File 'lib/awful/elb.rb', line 151

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



90
91
92
93
94
95
96
# File 'lib/awful/elb.rb', line 90

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

#tags(*names) ⇒ Object



81
82
83
84
85
86
87
# File 'lib/awful/elb.rb', line 81

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