Class: Infopark::AwsUtils::Env

Inherits:
Object
  • Object
show all
Defined in:
lib/infopark/aws_utils/env.rb

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(profile_name = nil) ⇒ Env

Returns a new instance of Env.



27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
# 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

Class Method Details

.profile(name) ⇒ Object



17
18
19
20
21
22
23
24
# 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

Instance Method Details

#aasObject



72
73
74
# File 'lib/infopark/aws_utils/env.rb', line 72

def aas
  @clients[Aws::ApplicationAutoScaling]
end

#account_typeObject



89
90
91
92
93
# File 'lib/infopark/aws_utils/env.rb', line 89

def 
  return "dev" if dev_account?
  return "prod" if prod_account?
  raise "Could not determine account type."
end

#albObject



68
69
70
# File 'lib/infopark/aws_utils/env.rb', line 68

def alb
  @clients[Aws::ElasticLoadBalancingV2]
end

#asObject



64
65
66
# File 'lib/infopark/aws_utils/env.rb', line 64

def as
  @clients[Aws::AutoScaling]
end

#cwObject



76
77
78
# File 'lib/infopark/aws_utils/env.rb', line 76

def cw
  @clients[Aws::CloudWatch]
end

#cwlObject



80
81
82
# File 'lib/infopark/aws_utils/env.rb', line 80

def cwl
  @clients[Aws::CloudWatchLogs]
end

#dev_account?Boolean

Returns:

  • (Boolean)


95
96
97
# File 'lib/infopark/aws_utils/env.rb', line 95

def dev_account?
  account?(DEV_ACCOUNT_ID)
end

#ec2Object



60
61
62
# File 'lib/infopark/aws_utils/env.rb', line 60

def ec2
  @clients[Aws::EC2]
end

#ecrObject



56
57
58
# File 'lib/infopark/aws_utils/env.rb', line 56

def ecr
  @clients[Aws::ECR]
end

#ecsObject



52
53
54
# File 'lib/infopark/aws_utils/env.rb', line 52

def ecs
  @clients[Aws::ECS]
end

#find_image_by_id(id) ⇒ Object



135
136
137
138
139
140
141
142
# 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) ⇒ Object



144
145
146
147
148
149
150
151
152
# 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) ⇒ Object



103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
# 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?Boolean

Returns:

  • (Boolean)


99
100
101
# File 'lib/infopark/aws_utils/env.rb', line 99

def prod_account?
  account?(PROD_ACCOUNT_ID)
end

#profile_nameObject



48
49
50
# File 'lib/infopark/aws_utils/env.rb', line 48

def profile_name
  @credentials.profile_name
end

#stsObject



84
85
86
# File 'lib/infopark/aws_utils/env.rb', line 84

def sts
  @clients[Aws::STS]
end