Class: Formatron::AWS
- Inherits:
-
Object
show all
- Defined in:
- lib/formatron/aws.rb,
lib/formatron/aws/cloud_formation_stack.rb
Overview
shared AWS clients rubocop:disable Metrics/ClassLength
Defined Under Namespace
Classes: CloudFormationStack
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
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
-
#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.
49
50
51
52
53
54
55
56
|
# File 'lib/formatron/aws.rb', line 49
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
Returns the value of attribute region.
8
9
10
|
# File 'lib/formatron/aws.rb', line 8
def region
@region
end
|
Instance Method Details
#delete_file(bucket:, key:) ⇒ Object
75
76
77
78
79
80
|
# File 'lib/formatron/aws.rb', line 75
def delete_file(bucket:, key:)
@s3_client.delete_object(
bucket: bucket,
key: key
)
end
|
#delete_stack(stack_name:) ⇒ Object
130
131
132
133
134
135
136
|
# File 'lib/formatron/aws.rb', line 130
def delete_stack(stack_name:)
cloud_formation_stack = Formatron::AWS::CloudFormationStack.new(
stack_name: stack_name,
client: @cloudformation_client
)
cloud_formation_stack.delete if cloud_formation_stack.exists?
end
|
#deploy_stack(stack_name:, template_url:, parameters:) ⇒ Object
rubocop:disable Metrics/MethodLength
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
|
# File 'lib/formatron/aws.rb', line 98
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
cloud_formation_stack = Formatron::AWS::CloudFormationStack.new(
stack_name: stack_name,
client: @cloudformation_client
)
if !cloud_formation_stack.exists?
cloud_formation_stack.create(
template_url: template_url,
parameters: aws_parameters
)
else
cloud_formation_stack.update(
template_url: template_url,
parameters: aws_parameters
)
end
end
|
#download_file(bucket:, key:, path:) ⇒ Object
82
83
84
85
86
87
88
|
# File 'lib/formatron/aws.rb', line 82
def download_file(bucket:, key:, path:)
@s3_client.get_object(
bucket: bucket,
key: key,
response_target: path
)
end
|
#file_exists?(bucket:, key:) ⇒ Boolean
68
69
70
71
72
73
|
# File 'lib/formatron/aws.rb', line 68
def file_exists?(bucket:, key:)
@s3_client.list_objects(
bucket: bucket,
prefix: key
).contents.length > 0
end
|
#get_file(bucket:, key:) ⇒ Object
90
91
92
93
94
95
|
# File 'lib/formatron/aws.rb', line 90
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
124
125
126
127
128
|
# File 'lib/formatron/aws.rb', line 124
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
138
139
140
141
142
143
144
145
146
147
148
|
# File 'lib/formatron/aws.rb', line 138
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
150
151
152
153
154
155
156
|
# File 'lib/formatron/aws.rb', line 150
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
58
59
60
61
62
63
64
65
66
|
# File 'lib/formatron/aws.rb', line 58
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
|