Class: Formatron::AWS
- Inherits:
-
Object
- Object
- Formatron::AWS
- Defined in:
- lib/formatron/aws.rb
Overview
shared AWS clients rubocop:disable Metrics/ClassLength
Constant Summary collapse
- REGIONS =
{ 'us-east-1' => { ami: 'ami-ff02509a' }, 'us-west-2' => { ami: 'ami-8ee605bd' }, 'us-west-1' => { ami: 'ami-198a495d' }, 'eu-west-1' => { ami: 'ami-37360a40' }, 'eu-central-1' => { ami: 'ami-46272b5b' }, 'ap-southeast-1' => { ami: 'ami-42170410' }, 'ap-southeast-2' => { ami: 'ami-6d6c2657' }, 'ap-northeast-1' => { ami: 'ami-402e4c40' }, 'sa-east-1' => { ami: 'ami-1f4bda02' } }
- CAPABILITIES =
%w(CAPABILITY_IAM)
- STACK_READY_STATES =
%w( CREATE_COMPLETE UPDATE_COMPLETE UPDATE_ROLLBACK_COMPLETE ROLLBACK_COMPLETE )
Instance Attribute Summary collapse
-
#region ⇒ Object
readonly
Returns the value of attribute region.
Instance Method Summary collapse
- #delete_file(bucket:, key:) ⇒ Object
- #delete_stack(stack_name:) ⇒ Object
-
#deploy_stack(stack_name:, template_url:, parameters:) ⇒ Object
rubocop:disable Metrics/MethodLength.
- #download_file(bucket:, key:, path:) ⇒ Object
- #file_exists?(bucket:, key:) ⇒ Boolean
- #get_file(bucket:, key:) ⇒ Object
-
#hosted_zone_name(hosted_zone_id) ⇒ Object
rubocop:enable Metrics/MethodLength.
-
#initialize(credentials:) ⇒ AWS
constructor
A new instance of AWS.
- #stack_outputs(stack_name:) ⇒ Object
- #stack_ready!(stack_name:) ⇒ Object
- #upload_file(kms_key:, bucket:, key:, content:) ⇒ Object
Constructor Details
#initialize(credentials:) ⇒ AWS
Returns a new instance of AWS.
48 49 50 51 52 53 54 55 |
# File 'lib/formatron/aws.rb', line 48 def initialize(credentials:) @credentials = JSON.parse(File.read(credentials)) @region = @credentials['region'] _create_aws_credentials _create_s3_client _create_cloudformation_client _create_route53_client end |
Instance Attribute Details
#region ⇒ Object (readonly)
Returns the value of attribute region.
7 8 9 |
# File 'lib/formatron/aws.rb', line 7 def region @region end |
Instance Method Details
#delete_file(bucket:, key:) ⇒ Object
74 75 76 77 78 79 |
# File 'lib/formatron/aws.rb', line 74 def delete_file(bucket:, key:) @s3_client.delete_object( bucket: bucket, key: key ) end |
#delete_stack(stack_name:) ⇒ Object
140 141 142 143 144 |
# File 'lib/formatron/aws.rb', line 140 def delete_stack(stack_name:) @cloudformation_client.delete_stack( stack_name: stack_name ) end |
#deploy_stack(stack_name:, template_url:, parameters:) ⇒ Object
rubocop:disable Metrics/MethodLength
97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 |
# File 'lib/formatron/aws.rb', line 97 def deploy_stack(stack_name:, template_url:, parameters:) aws_parameters = parameters.map do |key, value| { parameter_key: key, parameter_value: value, use_previous_value: false } end @cloudformation_client.create_stack( stack_name: stack_name, template_url: template_url, capabilities: CAPABILITIES, on_failure: 'DO_NOTHING', parameters: aws_parameters ) rescue Aws::CloudFormation::Errors::AlreadyExistsException _update_stack( stack_name: stack_name, template_url: template_url, parameters: aws_parameters ) end |
#download_file(bucket:, key:, path:) ⇒ Object
81 82 83 84 85 86 87 |
# File 'lib/formatron/aws.rb', line 81 def download_file(bucket:, key:, path:) @s3_client.get_object( bucket: bucket, key: key, response_target: path ) end |
#file_exists?(bucket:, key:) ⇒ Boolean
67 68 69 70 71 72 |
# File 'lib/formatron/aws.rb', line 67 def file_exists?(bucket:, key:) @s3_client.list_objects( bucket: bucket, prefix: key ).contents.length > 0 end |
#get_file(bucket:, key:) ⇒ Object
89 90 91 92 93 94 |
# File 'lib/formatron/aws.rb', line 89 def get_file(bucket:, key:) @s3_client.get_object( bucket: bucket, key: key ).body.read end |
#hosted_zone_name(hosted_zone_id) ⇒ Object
rubocop:enable Metrics/MethodLength
121 122 123 124 125 |
# File 'lib/formatron/aws.rb', line 121 def hosted_zone_name(hosted_zone_id) @route53_client.get_hosted_zone( id: hosted_zone_id ).hosted_zone.name.chomp '.' end |
#stack_outputs(stack_name:) ⇒ Object
146 147 148 149 150 151 152 153 154 155 156 |
# File 'lib/formatron/aws.rb', line 146 def stack_outputs(stack_name:) description = @cloudformation_client.describe_stacks( stack_name: stack_name ).stacks[0] status = description.stack_status fail "CloudFormation stack, #{stack_name}, " \ "is not ready: #{status}" unless STACK_READY_STATES.include? status description.outputs.each_with_object({}) do |output, outputs| outputs[output.output_key] = output.output_value end end |
#stack_ready!(stack_name:) ⇒ Object
158 159 160 161 162 163 164 |
# File 'lib/formatron/aws.rb', line 158 def stack_ready!(stack_name:) status = @cloudformation_client.describe_stacks( stack_name: stack_name ).stacks[0].stack_status fail "CloudFormation stack, #{stack_name}, " \ "is not ready: #{status}" unless STACK_READY_STATES.include? status end |
#upload_file(kms_key:, bucket:, key:, content:) ⇒ Object
57 58 59 60 61 62 63 64 65 |
# File 'lib/formatron/aws.rb', line 57 def upload_file(kms_key:, bucket:, key:, content:) @s3_client.put_object( bucket: bucket, key: key, body: content, server_side_encryption: 'aws:kms', ssekms_key_id: kms_key ) end |