class Aerosol::AutoScaling

Public Class Methods

latest_for_tag(key, value) click to toggle source
# File lib/aerosol/auto_scaling.rb, line 141
def self.latest_for_tag(key, value)
  all.select  { |group| group.tags[key] == value }
     .sort_by { |group| group.created_time }.last
end
new(options={}, &block) click to toggle source
Calls superclass method Aerosol::AWSModel::new
# File lib/aerosol/auto_scaling.rb, line 18
def initialize(options={}, &block)
  tag = options.delete(:tag)
  super(options, &block)

  tags.merge!(tag) unless tag.nil?

  tags["GitSha"] ||= Aerosol::Util.git_sha
  tags["Deploy"] ||= namespaced_name
end
request_all() click to toggle source
# File lib/aerosol/auto_scaling.rb, line 128
def self.request_all
  next_token = nil
  asgs = []

  begin
    new_asgs = request_all_for_token(next_token)
    asgs.concat(new_asgs.auto_scaling_groups)
    next_token = new_asgs.next_token
  end until next_token.nil?

  asgs
end
request_all_for_token(next_token) click to toggle source
# File lib/aerosol/auto_scaling.rb, line 123
def self.request_all_for_token(next_token)
  options = next_token.nil? ? {} : { next_token: next_token }
  Aerosol::AWS.auto_scaling.describe_auto_scaling_groups(options)
end

Public Instance Methods

all_instances() click to toggle source
# File lib/aerosol/auto_scaling.rb, line 93
def all_instances
  conn.describe_auto_scaling_groups(auto_scaling_group_names: [*auto_scaling_group_name])
      .auto_scaling_groups.first
      .instances.map { |instance| Aerosol::Instance.from_hash(instance.to_hash) }
end
auto_scaling_group_name(arg = nil) click to toggle source
# File lib/aerosol/auto_scaling.rb, line 28
def auto_scaling_group_name(arg = nil)
  if arg
    raise "You cannot set the auto_scaling_group_name directly" unless from_aws
    @auto_scaling_group_name = arg
  else
    @auto_scaling_group_name || default_identifier
  end
end
create!() click to toggle source
# File lib/aerosol/auto_scaling.rb, line 46
def create!
  ensure_present! :max_size, :min_size
  raise 'availability_zones or vpc_zone_identifier must be set' if [availability_zones, vpc_zone_identifier].none?
  raise 'launch_configuration or launch_template must be set' unless launch_details

  info "creating auto scaling group"
  launch_details = create_launch_details

  info self.inspect

  conn.create_auto_scaling_group({
    auto_scaling_group_name: auto_scaling_group_name,
    availability_zones: [*availability_zones],
    max_size: max_size,
    min_size: min_size
  }
    .merge(create_options)
    .merge(launch_details)
  )

  conn.wait_until(:group_in_service, auto_scaling_group_names: [auto_scaling_group_name]) do |waiter|
    waiter.before_wait do |attempt_count, _response|
      info "Wait count #{attempt_count} of #{waiter.max_attempts} for #{auto_scaling_group_name} to be in service"
    end
  end
end
deleting?() click to toggle source
# File lib/aerosol/auto_scaling.rb, line 85
def deleting?
  asgs = conn.describe_auto_scaling_groups(auto_scaling_group_names: [auto_scaling_group_name]).auto_scaling_groups

  return true if asgs.empty?

  asgs.first.status.to_s.include?('Delete')
end
destroy!() click to toggle source
# File lib/aerosol/auto_scaling.rb, line 73
def destroy!
  info self.inspect
  conn.delete_auto_scaling_group(auto_scaling_group_name: auto_scaling_group_name, force_delete: true)
  begin
    (0..2).each { break if deleting?; sleep 1 }
    launch_details.destroy
  rescue => ex
    info "Launch Details: #{launch_details} for #{auto_scaling_group_name} were not deleted."
    info ex.message
  end
end
exists?() click to toggle source
Calls superclass method Aerosol::AWSModel#exists?
# File lib/aerosol/auto_scaling.rb, line 37
def exists?
  info "auto_scaling: needed?: #{namespaced_name}: " +
       "checking for auto scaling group: #{auto_scaling_group_name}"
  exists = super
  info "auto scaling: needed?: #{namespaced_name}: " +
       "#{exists ? 'found' : 'did not find'} auto scaling group: #{auto_scaling_group_name}"
  exists
end
launch_details() click to toggle source
# File lib/aerosol/auto_scaling.rb, line 99
def launch_details
  launch_configuration || launch_template
end
tag(val) click to toggle source
# File lib/aerosol/auto_scaling.rb, line 103
def tag(val)
  tags.merge!(val)
end
tags(ary=nil) click to toggle source
# File lib/aerosol/auto_scaling.rb, line 107
def tags(ary=nil)
  if !ary.nil?
    if ary.is_a? Hash
      ary.each do |key, value|
        tag key => value
      end
    else
      ary.each do |struct|
        tag struct[:key] => struct[:value]
      end
    end
  else
    @tags ||= {}
  end
end
to_s() click to toggle source
# File lib/aerosol/auto_scaling.rb, line 146
  def to_s
    %{Aerosol::AutoScaling { \
"auto_scaling_group_name" => "#{auto_scaling_group_name}", \
"availability_zones" => "#{availability_zones}", \
"min_size" => "#{min_size}", \
"max_size" => "#{max_size}", \
"default_cooldown" => "#{default_cooldown}", \
"desired_capacity" => "#{desired_capacity}", \
"health_check_grace_period" => "#{health_check_grace_period}", \
"health_check_type" => "#{health_check_type}", \
"load_balancer_names" => "#{load_balancer_names}", \
"placement_group" => "#{placement_group}", \
"tags" => #{tags.to_s}, \
"created_time" => "#{created_time}" \
"target_group_arns" => "#{target_group_arns}" \
}}
  end

Private Instance Methods

conn() click to toggle source
# File lib/aerosol/auto_scaling.rb, line 165
def conn
  Aerosol::AWS.auto_scaling
end
create_launch_details() click to toggle source
# File lib/aerosol/auto_scaling.rb, line 169
def create_launch_details
  if launch_configuration
    launch_configuration.create
    { launch_configuration_name: launch_configuration.launch_configuration_name }
  elsif launch_template
    launch_template.create
    {
      launch_template:
      {
        launch_template_name: launch_template.launch_template_name,
        version: '$Latest'
      }
    }
  end
end
create_options() click to toggle source
# File lib/aerosol/auto_scaling.rb, line 185
def create_options
  {
    default_cooldown: default_cooldown,
    desired_capacity: desired_capacity,
    health_check_grace_period: health_check_grace_period,
    health_check_type: health_check_type,
    load_balancer_names: load_balancer_names,
    placement_group: placement_group,
    tags: tags_to_array,
    vpc_zone_identifier: vpc_zone_identifier,
    target_group_arns: target_group_arns
  }.reject { |k, v| v.nil? }
end
tags_to_array() click to toggle source
# File lib/aerosol/auto_scaling.rb, line 199
def tags_to_array
  tags.map do |key, value|
    { key: key, value: value }
  end
end