Class: Infopark::AwsUtils::Env
- Inherits:
-
Object
- Object
- Infopark::AwsUtils::Env
- Defined in:
- lib/infopark/aws_utils/env.rb
Class Method Summary collapse
Instance Method Summary collapse
- #aas ⇒ Object
- #account_type ⇒ Object
- #alb ⇒ Object
- #as ⇒ Object
- #cw ⇒ Object
- #cwl ⇒ Object
- #dev_account? ⇒ Boolean
- #ec2 ⇒ Object
- #ecr ⇒ Object
- #ecs ⇒ Object
- #find_image_by_id(id) ⇒ Object
- #find_image_by_name(name) ⇒ Object
-
#initialize(profile_name = nil) ⇒ Env
constructor
A new instance of Env.
- #latest_base_image(root_device_type: :instance, reject_image_name_patterns: nil) ⇒ Object
- #prod_account? ⇒ Boolean
- #profile_name ⇒ Object
- #sts ⇒ Object
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
#aas ⇒ Object
58 59 60 |
# File 'lib/infopark/aws_utils/env.rb', line 58 def aas @clients[Aws::ApplicationAutoScaling] end |
#account_type ⇒ Object
75 76 77 78 79 |
# File 'lib/infopark/aws_utils/env.rb', line 75 def account_type return "dev" if dev_account? return "prod" if prod_account? raise "Could not determine account type." end |
#alb ⇒ Object
54 55 56 |
# File 'lib/infopark/aws_utils/env.rb', line 54 def alb @clients[Aws::ElasticLoadBalancingV2] end |
#as ⇒ Object
50 51 52 |
# File 'lib/infopark/aws_utils/env.rb', line 50 def as @clients[Aws::AutoScaling] end |
#cw ⇒ Object
62 63 64 |
# File 'lib/infopark/aws_utils/env.rb', line 62 def cw @clients[Aws::CloudWatch] end |
#cwl ⇒ Object
66 67 68 |
# File 'lib/infopark/aws_utils/env.rb', line 66 def cwl @clients[Aws::CloudWatchLogs] end |
#dev_account? ⇒ Boolean
81 82 83 |
# File 'lib/infopark/aws_utils/env.rb', line 81 def dev_account? account?(DEV_ACCOUNT_ID) end |
#ec2 ⇒ Object
46 47 48 |
# File 'lib/infopark/aws_utils/env.rb', line 46 def ec2 @clients[Aws::EC2] end |
#ecr ⇒ Object
42 43 44 |
# File 'lib/infopark/aws_utils/env.rb', line 42 def ecr @clients[Aws::ECR] end |
#ecs ⇒ Object
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
85 86 87 |
# File 'lib/infopark/aws_utils/env.rb', line 85 def prod_account? account?(PROD_ACCOUNT_ID) end |
#profile_name ⇒ Object
34 35 36 |
# File 'lib/infopark/aws_utils/env.rb', line 34 def profile_name @credentials.profile_name end |
#sts ⇒ Object
70 71 72 |
# File 'lib/infopark/aws_utils/env.rb', line 70 def sts @clients[Aws::STS] end |