Sha256: 47d4d2e335b3bcad8246de8d7719b231b8c85085595295ed2399563da26de5b5

Contents?: true

Size: 1.42 KB

Versions: 1

Compression:

Stored size: 1.42 KB

Contents

class AWS::ELB::InstanceCollection
  def rotate(options = {})

    new_instances = start_new_instances(options)
    old_instances = stop_current_instances

    deregister old_instances
    register   new_instances
  end

  private

  def wait_for_status(status, interval = 1, collection = self)
    until collection.all? { |ec2| ec2.status == status } 
      puts "Waiting for ALL instances to be #{status}..."
      collection.each { |ec2| print_status(ec2) }
      sleep interval
    end

    collection
  end

  def print_status(ec2)
    puts "#{ec2.id}\t#{ec2.status}"
  end

  def start_new_instances(options = {})
    new_instances = map do |instance| 
      create_instance(instance_attributes(instance).merge(options))
    end

    wait_for_status(:running, 3, new_instances)

    new_instances
  end

  def stop_current_instances(interval = 3)
    old_instances = map { |instance| stop_instance(instance) }

    wait_for_status(:stopped, interval, old_instance)
    old_instances 
  end
  
  def instance_attributes(instance)
    {
      security_groups:    instance.security_groups.map(&:name),
      image_id:           instance.image_id,
      instance_type:      instance.instance_type,
      availability_zone:  instance.availability_zone,
    }
  end

  def create_instance(options)
    ec2.instances.create(options)
  end

  def stop_instance(instance)
    instance.stop
    instance
  end

  def ec2
    @ec2 ||= AWS::EC2.new
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
rodeo_clown-0.1.0 lib/rodeo_clown/ext/aws/elb/instance_collection.rb