Module: Capistrano::Configuration::LoadBalancers

Included in:
Capistrano::Configuration
Defined in:
lib/cap-elb.rb

Instance Method Summary collapse

Instance Method Details

#loadbalancer(named_load_balancer, *args) ⇒ Object

This will give you the list of hosts behind the load balancer that meet the criteria. % cap ec2:list

Raises:

  • (Exception)


90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
# File 'lib/cap-elb.rb', line 90

def loadbalancer (named_load_balancer, *args)

 require_arglist = args[1][:require] rescue {}
 exclude_arglist = args[1][:exclude] rescue {}
 named_region = fetch(:aws_params)[:region] rescue 'us-east-1'

 # can't have a EC2_URL env var if region has been provided
 # otherwise the RightScale gem will overresolve the url and region and form a bad endpoint
 # in the RightAWS::Ec2.new class
 # this undefine only lasts the extent of the cap task, doesn't affect the parent process.
 ENV['EC2_URL'] = nil if !named_region.nil?

 # list of all the instances assoc'ed with this account
 @ec2_api ||= RightAws::Ec2.new(fetch(:aws_access_key_id), fetch(:aws_secret_access_key), fetch(:aws_params, {}))

 # fetch a raw list all the load balancers
 @elb_api ||= RightAws::ElbInterface.new(fetch(:aws_access_key_id), fetch(:aws_secret_access_key), :region => named_region)
 
 # only get the named load balancer
 named_elb = @elb_api.describe_load_balancers.delete_if{ |instance| instance[:load_balancer_name] != named_load_balancer.to_s }

 # must exit if no load balancer on record for this account by given name in cap config file
 raise Exception, "No load balancer found named: #{named_load_balancer.to_s} for aws account with this access key: #{fetch(:aws_access_key_id)} in this region: #{named_region}" if named_elb.nil?
 # probe for the load balancer ec2 instance set, if this raises Exception, load balancer can't be found
 named_elb[0] rescue raise Exception, "No load balancer found named: #{named_load_balancer.to_s} for aws account with this access key: #{fetch(:aws_access_key_id)} in this region: #{named_region}" 
 raise Exception, "No instances within this load balancer: #{named_load_balancer.to_s} for aws account with this access key: #{fetch(:aws_access_key_id)} in this region: #{named_region}" if  named_elb[0][:instances].count == 0

 elb_ec2_instances = named_elb[0][:instances] rescue {}

 # get the full instance list for account, this is necessary to subsquently fish out the :dns_name for the instances that survive our reduction steps
  = @ec2_api.describe_instances

 # reduce to only the instances in the named ELB
 .delete_if { |i| ! elb_ec2_instances.include?(i[:aws_instance_id]) }

 # reduce against 'require' args, if an instance doesnt have the args in require_arglist, remove
 .delete_if { |i| ! all_args_within_instance(i, require_arglist) }  unless require_arglist.nil? or require_arglist.empty?

 # reduce against 'exclude_arglist', if an instance has any of the args in exclude_arglist, remove
 .delete_if { |i|   any_args_within_instance(i, exclude_arglist) }  unless exclude_arglist.nil? or exclude_arglist.empty?

 # finally load the derived instances into the serverlist used by capistrano tasks
 .each do |instance|
  hostname = instance[:dns_name] 
  hostname = instance[:ip_address] if hostname.empty?   # if host in a VPC, there will be no DNS name, use ip_address instead
  server(hostname, *args)
 end
end