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
# File 'lib/infopark/aws_utils/env.rb', line 27

def initialize(profile_name = nil)
  @credentials = Aws::SharedCredentials.new(profile_name: profile_name)
  @clients = Hash.new do |clients, mod|
    clients[mod] = mod.const_get(:Client).new(credentials: @credentials, region: 'eu-west-1')
  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


58
59
60
# File 'lib/infopark/aws_utils/env.rb', line 58

def aas
  @clients[Aws::ApplicationAutoScaling]
end

#account_typeObject


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

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

#albObject


54
55
56
# File 'lib/infopark/aws_utils/env.rb', line 54

def alb
  @clients[Aws::ElasticLoadBalancingV2]
end

#asObject


50
51
52
# File 'lib/infopark/aws_utils/env.rb', line 50

def as
  @clients[Aws::AutoScaling]
end

#cwObject


62
63
64
# File 'lib/infopark/aws_utils/env.rb', line 62

def cw
  @clients[Aws::CloudWatch]
end

#cwlObject


66
67
68
# File 'lib/infopark/aws_utils/env.rb', line 66

def cwl
  @clients[Aws::CloudWatchLogs]
end

#dev_account?Boolean

Returns:

  • (Boolean)

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

def dev_account?
  account?(DEV_ACCOUNT_ID)
end

#ec2Object


46
47
48
# File 'lib/infopark/aws_utils/env.rb', line 46

def ec2
  @clients[Aws::EC2]
end

#ecrObject


42
43
44
# File 'lib/infopark/aws_utils/env.rb', line 42

def ecr
  @clients[Aws::ECR]
end

#ecsObject


38
39
40
# File 'lib/infopark/aws_utils/env.rb', line 38

def ecs
  @clients[Aws::ECS]
end

#find_image_by_id(id) ⇒ Object


117
118
119
# File 'lib/infopark/aws_utils/env.rb', line 117

def find_image_by_id(id)
  AwsUtils.gather_all(ec2, :describe_images, image_ids: [id]).first
end

#find_image_by_name(name) ⇒ Object


121
122
123
124
# File 'lib/infopark/aws_utils/env.rb', line 121

def find_image_by_name(name)
  AwsUtils.gather_all(ec2, :describe_images, owners: [DEV_ACCOUNT_ID],
      filters: [{name: "name", values: [name]}]).first
end

#latest_base_image(root_device_type: :instance, reject_image_name_patterns: nil) ⇒ Object


89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
# File 'lib/infopark/aws_utils/env.rb', line 89

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(ec2, :describe_images,
      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)

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

def prod_account?
  account?(PROD_ACCOUNT_ID)
end

#profile_nameObject


34
35
36
# File 'lib/infopark/aws_utils/env.rb', line 34

def profile_name
  @credentials.profile_name
end

#stsObject


70
71
72
# File 'lib/infopark/aws_utils/env.rb', line 70

def sts
  @clients[Aws::STS]
end