lib/rodeo_clown/elb.rb in rodeo_clown-0.0.1 vs lib/rodeo_clown/elb.rb in rodeo_clown-0.1.0
- old
+ new
@@ -1,52 +1,62 @@
require "forwardable"
module RodeoClown
- class ELB < Struct.new(:aws_elb)
- TIMEOUT = 10
+ class ELB < Struct.new(:elb)
+ def timeout
+ @timeout ||= (ENV["TIMEOUT"] || 60).to_i
+ end
+
extend Forwardable
- def_delegators :aws_elb, :availability_zones, :instances
+ def_delegators :elb, :availability_zones, :instances
def self.by_name(name)
new load_balancers[name]
end
def self.load_balancers
AWS::ELB.new.load_balancers
end
- #
- # Rotate servers given
- #
- def rotate(hsh)
- current_ec2, new_ec2 = hsh.first
-
- cur_instances = EC2.filter_instances(values: { values: current_ec2.to_s})
- new_instances = EC2.filter_instances(values: { values: new_ec2.to_s})
-
+ def register_and_wait(new_instances)
new_instances.each do |i|
begin
puts "...registering: #{i.id}"
instances.register(i.id)
rescue AWS::ELB::Errors::InvalidInstance
puts "Instance #{i.id} could not be registered to load balancer"
end
end
wait_for_state(instances, "InService")
+ end
- cur_instances.each do |i|
+ def deregister(ary_instances)
+ ary_instances.each do |i|
begin
puts "...deregistering: #{i.id}"
instances.deregister(i.id)
rescue AWS::ELB::Errors::InvalidInstance
puts "Instance #{i.id} currently not registered to load balancer"
end
end
end
#
+ # Rotate servers given
+ #
+ def rotate(hsh)
+ current_ec2, new_ec2 = hsh.first
+
+ cur_instances = EC2.by_tags("Name" => current_ec2.to_s)
+ new_instances = EC2.by_tags("Name" => new_ec2.to_s)
+
+ register_and_wait new_instances
+ deregister cur_instances
+ end
+
+ #
# Wait for all the instances to become InService
#
def wait_for_state(instances, exp_state)
time = 0
@@ -58,10 +68,10 @@
puts "#{i.id}: #{state}"
exp_state == state
end
- break if all_good || time > TIMEOUT
+ break if all_good || time > timeout
sleep 1
time += 1
end