Permalink
Browse files

use EC2 tags for what servers should be in ELB

  • Loading branch information...
1 parent 50817ba commit 64f93f2f7e1063c00929745789e92bbb90cc3dca @thattommyhall committed Jan 16, 2012
Showing with 24 additions and 34 deletions.
  1. +2 −9 README.md
  2. +21 −19 lib/capistrano-elb.rb
  3. +0 −5 lib/capistrano-elb/tasks.rb
  4. +1 −1 lib/capistrano-elb/version.rb
View
11 README.md
@@ -18,6 +18,8 @@ then just
This will instantiate an instance of the CapELB class and add hooks to remove/readd before/after deploys
+As of version 0.4.0 it looks for an 'elb' tag with value of the name of a loadbalancer to decide when to remove/readd servers.
+
(Equivalent to having the following in your deploy.rb)
require "capistrano-elb"
@@ -37,10 +39,6 @@ This will instantiate an instance of the CapELB class and add hooks to remove/re
capELB.add servers
end
- task :save do
- capELB.save_config
- end
-
task :check do
puts capELB.check_config
end
@@ -49,11 +47,6 @@ This will instantiate an instance of the CapELB class and add hooks to remove/re
before "deploy", "elb:remove"
after "deploy", "elb:add"
-The first time you run it a record of the ELB setup is saved to config/lbs.yaml, you can check/update this with
- cap elb:check
- cap elb:save
-
-
You can just require capistrano-elb and do whatever you want inside your deploy scripts of course
If you want to hook after deploy but before the elb:add you can target
View
40 lib/capistrano-elb.rb
@@ -3,12 +3,17 @@
require 'capistrano'
require 'pp'
+class Hash
+ def diff(h2)
+ dup.delete_if { |k, v| h2[k] == v }.merge!(h2.dup.delete_if { |k, v| has_key?(k) })
+ end
+end
+
class CapELB
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"]}
- @regions -= ['us-west-2']
@compute = {}
@regions.each do |region|
@compute.merge!(region => Fog::Compute.new(ec2credentials.merge({:provider=>'AWS',:region=>region})))
@@ -19,40 +24,37 @@ def initialize(configdir=File.join(Dir.pwd, 'config'))
@elb.merge!(region => Fog::AWS::ELB.new(ec2credentials.merge(:region=>region)))
end
- @lbsfile = File.join(configdir, 'lbs.yaml')
-
- @lbs = load_config
+ @lbs = config_from_tags
end
- def config_from_aws
+ def config_from_tags
lbs = {}
@regions.each do |region|
- loadBalancerDescriptions =
- @elb[region].describe_load_balancers.body["DescribeLoadBalancersResult"]["LoadBalancerDescriptions"]
- loadBalancerDescriptions.each do |lb|
- lbs.merge!({region => {lb["LoadBalancerName"] => lb["Instances"]}})
+ @elb[region].load_balancers.each do |lb|
+ lbs.merge!({region => {lb.id => get_instances(region,lb.id)}})
end
end
lbs
end
- def save_config
- File.open( @lbsfile, 'w' ) do |file|
- YAML.dump( config_from_aws, file )
+ 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
- def load_config
- unless File.exists? @lbsfile
- save_config
- end
- YAML::load(File.open(@lbsfile))
+ def get_instances(region,lbname)
+ @compute[region].tags.select{|tag| tag.key == 'elb' and tag.value==lbname}.map(&:resource_id)
end
def check_config
- current = config_from_aws
+ current = config_from_lb
errors = []
- load_config.each_pair do |region,lbs|
+ @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
View
5 lib/capistrano-elb/tasks.rb
@@ -14,16 +14,11 @@
capELB.add servers
end
- task :save do
- capELB.save_config
- end
-
task :check do
puts capELB.check_config
end
end
before "deploy", "elb:remove"
after "deploy", "elb:add"
-
end
View
2 lib/capistrano-elb/version.rb
@@ -1,5 +1,5 @@
module Capistrano
module Elb
- VERSION = "0.3.8"
+ VERSION = "0.4.0"
end
end

0 comments on commit 64f93f2

Please sign in to comment.