class Elasticsearch::Drain::AutoScaling

Attributes

asg[R]

@attribute [r] EC2 AutoScaling Group name

region[R]

@attribute [r] AWS region

Public Class Methods

new(asg, region) click to toggle source
# File lib/elasticsearch/drain/autoscaling.rb, line 14
def initialize(asg, region)
  @asg = asg
  @region = region
  @instances = nil
  @instance_ids = nil
end

Public Instance Methods

asg_client() click to toggle source
# File lib/elasticsearch/drain/autoscaling.rb, line 21
def asg_client
  Aws::AutoScaling::Client.new(region: region)
end
describe_autoscaling_group() click to toggle source

Describe an AutoScaling Group

@return [Struct] AutoScaling Group

# File lib/elasticsearch/drain/autoscaling.rb, line 58
def describe_autoscaling_group
  groups = asg_client.describe_auto_scaling_groups(
    auto_scaling_group_names: [asg]
  )
  groups.auto_scaling_groups.first
end
describe_instances() click to toggle source

Get instances in an AutoScaling Group

@return [Array<Aws::EC2::Types::Instance>] EC2 Instance objects

# File lib/elasticsearch/drain/autoscaling.rb, line 44
def describe_instances
  instances = []
  find_instances_in_asg if @instance_ids.nil?
  return [] if @instance_ids.empty?
  ec2_client.describe_instances(instance_ids: @instance_ids).each do |page|
    instances << page.reservations.map(&:instances)
  end
  instances.flatten!
  @instances = instances
end
desired_capacity() click to toggle source

Gets the DesiredCapacity of an AutoScalingGroup

@return [Integer] Value of DesiredCapacity of an AutoScalingGroup

# File lib/elasticsearch/drain/autoscaling.rb, line 111
def desired_capacity
  group = describe_autoscaling_group
  group.desired_capacity
end
detach_instance(instance_id) click to toggle source
# File lib/elasticsearch/drain/autoscaling.rb, line 116
def detach_instance(instance_id)
  current_desired_capacity = desired_capacity
  asg_client.detach_instances(
    instance_ids: [instance_id],
    auto_scaling_group_name: asg,
    should_decrement_desired_capacity: true
  )
  wait_until(current_desired_capacity - 1) do
    desired_capacity
  end
end
ec2_client() click to toggle source
# File lib/elasticsearch/drain/autoscaling.rb, line 25
def ec2_client
  Aws::EC2::Client.new(region: region)
end
find_instances_in_asg() click to toggle source
# File lib/elasticsearch/drain/autoscaling.rb, line 29
def find_instances_in_asg
  instances = []
  asg_client.describe_auto_scaling_instances.each do |page|
    instances << page.auto_scaling_instances.map do |i|
      i.instance_id if i.auto_scaling_group_name == asg
    end
  end
  instances.flatten!
  instances.compact!
  @instance_ids = instances
end
find_private_ips() click to toggle source
# File lib/elasticsearch/drain/autoscaling.rb, line 65
def find_private_ips
  instances = describe_instances.clone
  return [] if instances.nil?
  instances.map!(&:private_ip_address)
  instances.flatten!
  instances
end
instance(ipaddress) click to toggle source
# File lib/elasticsearch/drain/autoscaling.rb, line 78
def instance(ipaddress)
  describe_instances if @instances.nil?
  instances = @instances.clone
  instance = instances.find { |i| i.private_ip_address == ipaddress }
  fail Errors::NodeNotFound if instance.nil?
  instance
end
instances() click to toggle source
# File lib/elasticsearch/drain/autoscaling.rb, line 73
def instances
  find_instances_in_asg
  find_private_ips
end
min_size() click to toggle source

Gets the MinSize of an AutoScalingGroup

@return [Integer] Value of MinSize of an AutoScalingGroup

# File lib/elasticsearch/drain/autoscaling.rb, line 103
def min_size
  group = describe_autoscaling_group
  group.min_size
end
min_size=(count = 0) click to toggle source

Sets the MinSize of an AutoScalingGroup

@option [FixNum] count (0) The new MinSize of the AutoScalingGroup @return [Struct] Empty response from the sdk

# File lib/elasticsearch/drain/autoscaling.rb, line 90
def min_size=(count = 0)
  asg_client.update_auto_scaling_group(
    auto_scaling_group_name: asg,
    min_size: count
  )
  wait_until(count) do
    min_size
  end
end