Class: CapELB
- Inherits:
-
Object
- Object
- CapELB
- Defined in:
- lib/capistrano-elb.rb
Instance Method Summary collapse
- #add(serverlist) ⇒ Object
- #check_config ⇒ Object
- #config_from_lb ⇒ Object
- #config_from_tags ⇒ Object
- #get_instances(region, lbname) ⇒ Object
-
#initialize(configdir = File.join(Dir.pwd, 'config')) ⇒ CapELB
constructor
A new instance of CapELB.
- #remove(serverlist) ⇒ Object
Constructor Details
#initialize(configdir = File.join(Dir.pwd, 'config')) ⇒ CapELB
Returns a new instance of CapELB.
13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 |
# File 'lib/capistrano-elb.rb', line 13 def initialize(configdir=File.join(Dir.pwd, 'config')) ec2credentials = YAML::load(File.open(File.join(configdir, 'ec2credentials.yaml'))) aws = Fog::Compute.new(ec2credentials.merge({:provider=>'AWS'})) @regions = aws.describe_regions.body["regionInfo"].map {|region| region["regionName"]} @compute = {} @regions.each do |region| @compute.merge!(region => Fog::Compute.new(ec2credentials.merge({:provider=>'AWS',:region=>region}))) end @elb = {} @regions.each do |region| @elb.merge!(region => Fog::AWS::ELB.new(ec2credentials.merge(:region=>region))) end @lbs = end |
Instance Method Details
#add(serverlist) ⇒ Object
68 69 70 71 72 73 74 75 76 77 78 79 |
# File 'lib/capistrano-elb.rb', line 68 def add(serverlist) @lbs.each_pair do |region, lbs| lbs.each_pair do |lbname, target_instances| server_ids = @compute[region].servers.select{|server| serverlist.include? server.dns_name}.map{|server| server.id} to_change = server_ids.select{|server_id| target_instances.include? server_id} unless to_change.empty? puts "Adding #{to_change} to LB #{lbname} in #{region}" @elb[region].register_instances_with_load_balancer(to_change, lbname) end end end end |
#check_config ⇒ Object
54 55 56 57 58 59 60 61 62 63 64 65 66 |
# File 'lib/capistrano-elb.rb', line 54 def check_config current = config_from_lb errors = [] @lbs.each_pair do |region,lbs| lbs.each_pair do |lbname, target_instances| missing = target_instances - current[region][lbname] extra = current[region][lbname] - target_instances errors << "#{missing} are missing from #{region}/#{lbname}" unless missing.empty? errors << "#{extra} should not be in #{region}/#{lbname}" unless extra.empty? end end (errors.empty? ? "ELB config correct" : errors) end |
#config_from_lb ⇒ Object
40 41 42 43 44 45 46 47 48 |
# File 'lib/capistrano-elb.rb', line 40 def config_from_lb lbs = {} @regions.each do |region| @elb[region].load_balancers.each do |lb| lbs.merge!({region => {lb.id => lb.instances}}) end end lbs end |
#config_from_tags ⇒ Object
30 31 32 33 34 35 36 37 38 |
# File 'lib/capistrano-elb.rb', line 30 def lbs = {} @regions.each do |region| @elb[region].load_balancers.each do |lb| lbs.merge!({region => {lb.id => get_instances(region,lb.id)}}) end end lbs end |
#get_instances(region, lbname) ⇒ Object
50 51 52 |
# File 'lib/capistrano-elb.rb', line 50 def get_instances(region,lbname) @compute[region]..select{|tag| tag.key == 'elb' and tag.value==lbname}.map(&:resource_id) end |
#remove(serverlist) ⇒ Object
81 82 83 84 85 86 87 88 89 90 91 92 |
# File 'lib/capistrano-elb.rb', line 81 def remove(serverlist) @lbs.each_pair do |region, lbs| lbs.each_pair do |lbname, target_instances| server_ids = @compute[region].servers.select{|server| serverlist.include? server.dns_name}.map{|server| server.id} to_change = server_ids.select{|server_id| target_instances.include? server_id} unless to_change.empty? puts "Removing #{to_change} from LB #{lbname} in #{region}" @elb[region].deregister_instances_from_load_balancer(to_change, lbname) end end end end |