class CfDeployer::Driver::AutoScalingGroup

Attributes

group[R]
group_name[R]

Public Class Methods

new(name, timeout = CfDeployer::Defaults::Timeout) click to toggle source
# File lib/cf_deployer/driver/auto_scaling_group.rb, line 10
def initialize name, timeout = CfDeployer::Defaults::Timeout
  @group_name = name
  @timeout = timeout
end

Public Instance Methods

cool_down() click to toggle source
# File lib/cf_deployer/driver/auto_scaling_group.rb, line 37
def cool_down
  Log.info "Cooling down #{group_name}"
  CfDeployer::Driver::DryRun.guard "Skipping ASG cooldown" do
    aws_group.update :min_size => 0, :max_size => 0
    aws_group.set_desired_capacity 0
  end
end
describe() click to toggle source
# File lib/cf_deployer/driver/auto_scaling_group.rb, line 15
def describe
  { desired: aws_group.desired_capacity, min: aws_group.min_size, max: aws_group.max_size }
end
desired_capacity_reached?() click to toggle source
# File lib/cf_deployer/driver/auto_scaling_group.rb, line 61
def desired_capacity_reached?
  healthy_instance_count >= desired_capacity
end
healthy_instance_count() click to toggle source
# File lib/cf_deployer/driver/auto_scaling_group.rb, line 87
def healthy_instance_count
  AWS.memoize do
    instances = healthy_instance_ids
    instances &= in_service_instance_ids unless load_balancers.empty?
    Log.info "Healthy instance count: #{instances.count}"
    instances.count
  end
rescue => e
  Log.error "Unable to determine healthy instance count due to error: #{e.message}"
  -1
end
healthy_instance_ids() click to toggle source
# File lib/cf_deployer/driver/auto_scaling_group.rb, line 65
def healthy_instance_ids
  instances = auto_scaling_instances.select do |instance|
    'HEALTHY'.casecmp(instance.health_status) == 0
  end
  instances.map(&:id)
end
in_service_instance_ids() click to toggle source
# File lib/cf_deployer/driver/auto_scaling_group.rb, line 72
def in_service_instance_ids
  elbs = load_balancers
  return [] if elbs.empty?

  ids = elbs.collect(&:instances)
            .collect(&:health)
            .to_a
            .collect { |elb_healths|
               elb_healths.select { |health| health[:state] == 'InService' }
                          .map { |health| health[:instance].id }
            }

  ids.inject(:&)
end
instance_statuses() click to toggle source
# File lib/cf_deployer/driver/auto_scaling_group.rb, line 45
def instance_statuses
  instance_info = {}
  ec2_instances.each do |instance|
    instance_info[instance.id] = CfDeployer::Driver::Instance.new(instance).status
  end
  instance_info
end
wait_for_desired_capacity() click to toggle source
# File lib/cf_deployer/driver/auto_scaling_group.rb, line 53
def wait_for_desired_capacity
  Timeout::timeout(@timeout){
    until desired_capacity_reached?
      sleep 15
    end
  }
end
warm_up(desired) click to toggle source
# File lib/cf_deployer/driver/auto_scaling_group.rb, line 19
def warm_up desired
  return if desired < aws_group.min_size
  desired = aws_group.max_size if desired > aws_group.max_size
  Log.info "warming up auto scaling group #{group_name} to #{desired}"

  CfDeployer::Driver::DryRun.guard "Skipping ASG warmup" do
    aws_group.set_desired_capacity desired
    wait_for_desired_capacity
  end
end
warm_up_cooled_group(options) click to toggle source
# File lib/cf_deployer/driver/auto_scaling_group.rb, line 30
def warm_up_cooled_group options
  CfDeployer::Driver::DryRun.guard 'Skipping update of ASG min & max instance count' do
    aws_group.update :min_size => options[:min], :max_size => options[:max]
  end
  warm_up options[:desired]
end

Private Instance Methods

aws_group() click to toggle source
# File lib/cf_deployer/driver/auto_scaling_group.rb, line 101
def aws_group
  @my_group ||= AWS::AutoScaling.new.groups[group_name]
end