Sha256: a39b2f386a2a069e16ff516a486743fc044c8b5bfc6e3769fd4574daa078e49f

Contents?: true

Size: 1.71 KB

Versions: 1

Compression:

Stored size: 1.71 KB

Contents

require "forwardable"
module RodeoClown
  class ELB < Struct.new(:aws_elb)
    TIMEOUT = 10

    extend Forwardable
      def_delegators :aws_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})

      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")

      cur_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

    #
    # Wait for all the instances to become InService
    #
    def wait_for_state(instances, exp_state)

      time = 0
      all_good = false

      loop do
        all_good = instances.all? do |i|
          state = i.elb_health[:state] 
          puts "#{i.id}: #{state}"

          exp_state == state
        end

        break if all_good || time > TIMEOUT

        sleep 1
        time += 1
      end

      # If timeout before all inservice, deregister and raise error
      unless all_good
        raise "Instances are out of service"
      end
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
rodeo_clown-0.0.1 lib/rodeo_clown/elb.rb