class Dogscaler::AwsClient

Constants

NoAutoscaleNameFound
NoResultsError
TooManyResultsError

Public Class Methods

new() click to toggle source
# File lib/dogscaler/awsclient.rb, line 10
def initialize
  @credentials = Aws::SharedCredentials.new(profile_name: Settings.aws['profile'])
  @region = Settings.aws['region']
end

Public Instance Methods

asg_client() click to toggle source
# File lib/dogscaler/awsclient.rb, line 15
def asg_client
  @asg_client ||= Aws::AutoScaling::Client.new(credentials: @credentials, :region => @region)
end
autoscalegroups() click to toggle source
# File lib/dogscaler/awsclient.rb, line 39
def autoscalegroups
  @@asgs ||= get_autoscale_groups
end
ec2_client() click to toggle source
# File lib/dogscaler/awsclient.rb, line 19
def ec2_client
  @ec2_client ||= Aws::EC2::Client.new(credentials: @credentials, :region => @region)
end
get_asg(asg_name=nil, asg_tag_filters = {}) click to toggle source
# File lib/dogscaler/awsclient.rb, line 43
def get_asg(asg_name=nil, asg_tag_filters = {})
  raise NoAutoscaleNameFound if asg_name.nil? and asg_tag_filters.empty?

  if not asg_tag_filters.empty?
    asg_name = autoscalegroups.select do |group|
      validate_tags(group.tags, asg_tag_filters)
    end
  else
    asg_name = autoscalegroups.select do |group|
      group.auto_scaling_group_name == asg_name
    end
  end

  asg_name.select! do |group|
    group.desired_capacity > 0 and
    group.max_size > 0
  end

  raise TooManyResultsError if asg_name.count > 1
  if asg_name.empty?
    logger.error "No results found for asg_name #{asg_name}, filters: #{asg_tag_filters}"
    raise NoResultsError
  end
  return asg_name.first
end
get_autoscale_groups() click to toggle source
# File lib/dogscaler/awsclient.rb, line 23
def get_autoscale_groups
  next_token = nil
  autoscalegroups = []
  loop do
    body = {next_token: next_token}
    resp = asg_client.describe_auto_scaling_groups(body)
    asgs =  resp.auto_scaling_groups
    asgs.each do |instance|
      autoscalegroups << instance
    end
    next_token = resp.next_token
    break if next_token.nil?
  end
  return autoscalegroups
end
get_capacity(asg_name) click to toggle source
# File lib/dogscaler/awsclient.rb, line 86
def get_capacity(asg_name)
  asg_client.describe_auto_scaling_groups({auto_scaling_group_names: \
    [asg_name] }).auto_scaling_groups.first.desired_capacity
end
set_capacity(instance, options) click to toggle source
# File lib/dogscaler/awsclient.rb, line 91
def set_capacity(instance, options)
  template = {
    auto_scaling_group_name: instance.autoscalegroupname,
    desired_capacity: instance.change
  }

  logger.debug template
  asg_client.update_auto_scaling_group(template)
end
validate_tags(tags, filters) click to toggle source
# File lib/dogscaler/awsclient.rb, line 69
def validate_tags(tags, filters)
  values = []
  filters.each do |key, value|
    trueness = false
    logger.debug "Checking: #{key} for: #{value}"
    tags.each do |tag|
      if tag['key'] == key && tag['value'] == value
        trueness = true
        break
      end
    end
    values << trueness
  end
  # we're good if the results are all good
  values.all?
end