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 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
#aas ⇒ Object
72 73 74 |
# File 'lib/infopark/aws_utils/env.rb', line 72 def aas @clients[Aws::ApplicationAutoScaling] end |
#account_type ⇒ Object
89 90 91 92 93 |
# 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 ⇒ Object
68 69 70 |
# File 'lib/infopark/aws_utils/env.rb', line 68 def alb @clients[Aws::ElasticLoadBalancingV2] end |
#as ⇒ Object
64 65 66 |
# File 'lib/infopark/aws_utils/env.rb', line 64 def as @clients[Aws::AutoScaling] end |
#cw ⇒ Object
76 77 78 |
# File 'lib/infopark/aws_utils/env.rb', line 76 def cw @clients[Aws::CloudWatch] end |
#cwl ⇒ Object
80 81 82 |
# File 'lib/infopark/aws_utils/env.rb', line 80 def cwl @clients[Aws::CloudWatchLogs] end |
#dev_account? ⇒ Boolean
95 96 97 |
# File 'lib/infopark/aws_utils/env.rb', line 95 def dev_account? account?(DEV_ACCOUNT_ID) end |
#ec2 ⇒ Object
60 61 62 |
# File 'lib/infopark/aws_utils/env.rb', line 60 def ec2 @clients[Aws::EC2] end |
#ecr ⇒ Object
56 57 58 |
# File 'lib/infopark/aws_utils/env.rb', line 56 def ecr @clients[Aws::ECR] end |
#ecs ⇒ Object
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
99 100 101 |
# File 'lib/infopark/aws_utils/env.rb', line 99 def prod_account? account?(PROD_ACCOUNT_ID) end |
#profile_name ⇒ Object
48 49 50 |
# File 'lib/infopark/aws_utils/env.rb', line 48 def profile_name @credentials.profile_name end |
#sts ⇒ Object
84 85 86 |
# File 'lib/infopark/aws_utils/env.rb', line 84 def sts @clients[Aws::STS] end |