module PrimaAwsClient

Public Instance Methods

asg_client() click to toggle source
# File lib/prima_aws_client.rb, line 9
def asg_client
  @asg ||= Aws::AutoScaling::Client.new
end
cf_client() click to toggle source
# File lib/prima_aws_client.rb, line 5
def cf_client
  @cf ||= Aws::CloudFormation::Client.new
end
describe_auto_scaling_groups(auto_scaling_group_names, max_records) click to toggle source
# File lib/prima_aws_client.rb, line 136
def describe_auto_scaling_groups(auto_scaling_group_names, max_records)
  resp = asg_client.describe_auto_scaling_groups({
                                                   auto_scaling_group_names: auto_scaling_group_names,
                                                   max_records: max_records
                                                 })
rescue Aws::CloudFormation::Errors::Throttling => e
  output 'Throttling, retrying in 15 seconds'.red
  sleep 15
  resp = describe_auto_scaling_groups(auto_scaling_group_names, max_records)
end
get_autoscaling_capacity(asg_name) click to toggle source
# File lib/prima_aws_client.rb, line 147
def get_autoscaling_capacity(asg_name)
  resp = asg_client.describe_auto_scaling_groups(auto_scaling_group_names: [asg_name])
  resp.auto_scaling_groups[0].desired_capacity
end
get_stack_outputs(name) click to toggle source
# File lib/prima_aws_client.rb, line 111
def get_stack_outputs(name)
  resp = cf_client.describe_stacks(stack_name: name)
rescue Aws::CloudFormation::Errors::Throttling => e
  output 'Throttling, retrying in 15 seconds'.red
  sleep 15
  get_stack_outputs(name)
else
  resp.stacks[0].outputs
end
get_stack_parameters(name) click to toggle source
# File lib/prima_aws_client.rb, line 101
def get_stack_parameters(name)
  resp = cf_client.describe_stacks(stack_name: name)
rescue Aws::CloudFormation::Errors::Throttling => e
  output 'Throttling, retrying in 15 seconds'.red
  sleep 15
  get_stack_parameters(name)
else
  resp.stacks[0].parameters
end
list_exports() click to toggle source
# File lib/prima_aws_client.rb, line 13
def list_exports
  exports = []
  next_token = ''
  loop do
    print '.'.yellow; STDOUT.flush
    options = next_token != '' ? { next_token: next_token } : {}
    begin
      resp = cf_client.list_exports(options)
    rescue Aws::CloudFormation::Errors::Throttling => e
      output 'Throttling, retrying in 15 seconds'.red
      sleep 15
      resp = cf_client.list_exports(options)
    end
    exports += resp.exports
    break unless resp.next_token

    next_token = resp.next_token
  end
  puts '.'.yellow; STDOUT.flush
  exports
end
stack_ready?(stack_name, failed_statuses = %w[CREATE_FAILED ROLLBACK_IN_PROGRESS ROLLBACK_FAILED DELETE_IN_PROGRESS DELETE_FAILED DELETE_COMPLETE UPDATE_ROLLBACK_FAILED UPDATE_ROLLBACK_COMPLETE_CLEANUP_IN_PROGRESS]) click to toggle source
# File lib/prima_aws_client.rb, line 121
def stack_ready?(stack_name, failed_statuses = %w[CREATE_FAILED ROLLBACK_IN_PROGRESS ROLLBACK_FAILED DELETE_IN_PROGRESS DELETE_FAILED DELETE_COMPLETE UPDATE_ROLLBACK_FAILED UPDATE_ROLLBACK_COMPLETE_CLEANUP_IN_PROGRESS])
  begin
    resp = cf_client.describe_stacks(
      stack_name: stack_name
    )
    stack_status = resp.stacks[0].stack_status
  rescue Aws::CloudFormation::Errors::Throttling => e
    print 'Throttling'.red; STDOUT.flush
    return false
  end
  raise "The stack #{stack_name} errored out" if failed_statuses.include? stack_status

  %w[CREATE_COMPLETE UPDATE_COMPLETE UPDATE_ROLLBACK_COMPLETE ROLLBACK_COMPLETE].include? stack_status
end
update_stack(stack_name, template_body, parameters = [], tags = [], role = nil) click to toggle source
# File lib/prima_aws_client.rb, line 35
def update_stack(stack_name, template_body, parameters = [], tags = [], role = nil)
  cf_args = {
    stack_name: stack_name,
    template_body: template_body,
    parameters: parameters,
    tags: tags,
    capabilities: ['CAPABILITY_IAM']
  }

  cf_args.merge!(role_arn: role) unless role.nil?

  begin
    cf_client.update_stack(cf_args)
  rescue Aws::CloudFormation::Errors::Throttling => e
    output 'Throttling, retrying in 15 seconds'.red
    sleep 15
    update_stack(stack_name, template_body, parameters = [], tags = [])
  rescue Aws::CloudFormation::Errors::ValidationError => e
    raise e
  else
    output "CloudFormation stack #{stack_name} update started".green
  end
end
update_stack_reuse_template(stack_name, parameters = [], tags = [], role = nil) click to toggle source
# File lib/prima_aws_client.rb, line 59
def update_stack_reuse_template(stack_name, parameters = [], tags = [], role = nil)
  cf_args = {
    stack_name: stack_name,
    use_previous_template: true,
    parameters: parameters,
    tags: tags,
    capabilities: ['CAPABILITY_IAM']
  }

  cf_args.merge!(role_arn: role) unless role.nil?

  begin
    cf_client.update_stack(cf_args)
  rescue Aws::CloudFormation::Errors::Throttling => e
    output 'Throttling, retrying in 15 seconds'.red
    sleep 15
    update_stack(stack_name, template_body, parameters = [], tags = [])
  rescue Aws::CloudFormation::Errors::ValidationError => e
    raise e
  else
    output "CloudFormation stack #{stack_name} update started".green
  end
end
wait_for_stack_ready(stack_name, failed_statuses = %w[CREATE_FAILED ROLLBACK_IN_PROGRESS ROLLBACK_FAILED DELETE_IN_PROGRESS DELETE_FAILED DELETE_COMPLETE UPDATE_ROLLBACK_FAILED UPDATE_ROLLBACK_COMPLETE_CLEANUP_IN_PROGRESS]) click to toggle source
# File lib/prima_aws_client.rb, line 83
def wait_for_stack_ready(stack_name, failed_statuses = %w[CREATE_FAILED ROLLBACK_IN_PROGRESS ROLLBACK_FAILED DELETE_IN_PROGRESS DELETE_FAILED DELETE_COMPLETE UPDATE_ROLLBACK_FAILED UPDATE_ROLLBACK_COMPLETE_CLEANUP_IN_PROGRESS])
  ready = false
  sleep_seconds = 13
  output "Waiting for stack #{stack_name}...\n".yellow
  until ready
    ready = true if stack_ready?(stack_name, failed_statuses)
    seconds_elapsed = 0
    loop do
      break if seconds_elapsed >= sleep_seconds

      print '.'.yellow; STDOUT.flush
      sleep 1
      seconds_elapsed += 1
    end
  end
  output "\nStack #{stack_name} ready!\n".green
end