class Infopark::AwsUtils::Env

Public Class Methods

new(profile_name = nil) click to toggle source
# File lib/infopark/aws_utils/env.rb, line 27
def initialize(profile_name = nil)
  @credentials = Aws::SharedCredentials.new(profile_name: profile_name)
  if @credentials.credentials.nil?
    raise "No credentials for AWS profile “#{profile_name}” found."\
        " Please provide them via ~/.aws/credentials."
  end

  region = Aws.shared_config.region(profile: profile_name)
  if region.nil?
    raise "No region for AWS profile “#{profile_name}” found."\
        " Please provide them via ~/.aws/config."
  end

  @clients = Hash.new do |clients, mod|
    clients[mod] = mod.const_get(:Client).new(
      credentials: @credentials,
      region: region,
    )
  end
end
profile(name) click to toggle source
# File lib/infopark/aws_utils/env.rb, line 17
def profile(name)
  env_var = "AWS_#{name.upcase}_PROFILE"
  profile_name = ENV[env_var] || name
  new(profile_name)
rescue Aws::Errors::NoSuchProfileError
  raise "AWS profile “#{profile_name}” not found."\
      " Please provide the #{name} profile via #{env_var}."
end

Public Instance Methods

aas() click to toggle source
# File lib/infopark/aws_utils/env.rb, line 72
def aas
  @clients[Aws::ApplicationAutoScaling]
end
account_type() click to toggle source
# File lib/infopark/aws_utils/env.rb, line 89
def account_type
  return "dev" if dev_account?
  return "prod" if prod_account?
  raise "Could not determine account type."
end
alb() click to toggle source
# File lib/infopark/aws_utils/env.rb, line 68
def alb
  @clients[Aws::ElasticLoadBalancingV2]
end
as() click to toggle source
# File lib/infopark/aws_utils/env.rb, line 64
def as
  @clients[Aws::AutoScaling]
end
cw() click to toggle source
# File lib/infopark/aws_utils/env.rb, line 76
def cw
  @clients[Aws::CloudWatch]
end
cwl() click to toggle source
# File lib/infopark/aws_utils/env.rb, line 80
def cwl
  @clients[Aws::CloudWatchLogs]
end
dev_account?() click to toggle source
# File lib/infopark/aws_utils/env.rb, line 95
def dev_account?
  account?(DEV_ACCOUNT_ID)
end
ec2() click to toggle source
# File lib/infopark/aws_utils/env.rb, line 60
def ec2
  @clients[Aws::EC2]
end
ecr() click to toggle source
# File lib/infopark/aws_utils/env.rb, line 56
def ecr
  @clients[Aws::ECR]
end
ecs() click to toggle source
# File lib/infopark/aws_utils/env.rb, line 52
def ecs
  @clients[Aws::ECS]
end
find_image_by_id(id) click to toggle source
# File lib/infopark/aws_utils/env.rb, line 135
def find_image_by_id(id)
  AwsUtils.gather_all(
    client: ec2,
    method: :describe_images,
    response_key: :image_details,
    image_ids: [id],
  ).first
end
find_image_by_name(name) click to toggle source
# File lib/infopark/aws_utils/env.rb, line 144
def find_image_by_name(name)
  AwsUtils.gather_all(
    client: ec2,
    method: :describe_images,
    response_key: :image_details,
    owners: [DEV_ACCOUNT_ID],
    filters: [{name: "name", values: [name]}],
  ).first
end
latest_base_image(root_device_type: :instance, reject_image_name_patterns: nil) click to toggle source
# File lib/infopark/aws_utils/env.rb, line 103
def latest_base_image(root_device_type: :instance, reject_image_name_patterns: nil)
  root_device_filter_value =
      case root_device_type
      when :instance
        ["instance-store"]
      when :ebs
        ["ebs"]
      else
        raise "invalid root_device_type: #{root_device_type}"
      end
  available_images = AwsUtils.gather_all(
    client: ec2,
    method: :describe_images,
    response_key: :image_details,
    owners: [AWS_AMI_OWNER],
    filters: [
      {name: "root-device-type", values: root_device_filter_value},
      {name: "ena-support", values: ["true"]},
      {name: "image-type", values: ["machine"]},
      {name: "virtualization-type", values: ["hvm"]},
    ],
  )
    .reject {|image| image.name.include?(".rc-") }
    .reject {|image| image.name.include?("-minimal-") }
    .reject {|image| image.name.include?("-test") }
    .reject {|image| image.name.include?("amzn-ami-vpc-nat-") }
  (reject_image_name_patterns || []).each do |pattern|
    available_images.reject! {|image| image.name =~ pattern }
  end
  available_images.sort_by(&:creation_date).last
end
prod_account?() click to toggle source
# File lib/infopark/aws_utils/env.rb, line 99
def prod_account?
  account?(PROD_ACCOUNT_ID)
end
profile_name() click to toggle source
# File lib/infopark/aws_utils/env.rb, line 48
def profile_name
  @credentials.profile_name
end
sts() click to toggle source
# File lib/infopark/aws_utils/env.rb, line 84
def sts
  @clients[Aws::STS]
end

Private Instance Methods

account?(account_id) click to toggle source
# File lib/infopark/aws_utils/env.rb, line 156
def account?(account_id)
  sts.get_caller_identity.account == account_id
end