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' => {
'ubuntu' => 'ami-fce3c696',
'windows' => 'ami-3586ac5f'
},
'us-west-1' => {
'ubuntu' => 'ami-06116566',
'windows' => 'ami-95fd8bf5'
},
'us-west-2' => {
'ubuntu' => 'ami-9abea4fb',
'windows' => 'ami-df8767bf'
},
'eu-west-1' => {
'ubuntu' => 'ami-f95ef58a',
'windows' => 'ami-8519a9f6'
},
'eu-central-1' => {
'ubuntu' => 'ami-87564feb',
'windows' => 'ami-5dd2c931'
},
'ap-northeast-1' => {
'ubuntu' => 'ami-a21529cc',
'windows' => 'ami-14b8bc7a'
},
'ap-northeast-2' => {
'ubuntu' => 'ami-09dc1267',
'windows' => 'ami-d31dd3bd'
},
'ap-southeast-1' => {
'ubuntu' => 'ami-25c00c46',
'windows' => 'ami-9801cffb'
},
'ap-southeast-2' => {
'ubuntu' => 'ami-6c14310f',
'windows' => 'ami-db0a2db8'
},
'sa-east-1' => {
'ubuntu' => 'ami-0fb83963',
'windows' => 'ami-828e0dee'
}
}
- 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.
62
63
64
65
66
67
68
69
|
# File 'lib/formatron/aws.rb', line 62
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
88
89
90
91
92
93
|
# File 'lib/formatron/aws.rb', line 88
def delete_file(bucket:, key:)
@s3_client.delete_object(
bucket: bucket,
key: key
)
end
|
#delete_stack(stack_name:) ⇒ Object
143
144
145
146
147
148
149
|
# File 'lib/formatron/aws.rb', line 143
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
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
|
# File 'lib/formatron/aws.rb', line 111
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
95
96
97
98
99
100
101
|
# File 'lib/formatron/aws.rb', line 95
def download_file(bucket:, key:, path:)
@s3_client.get_object(
bucket: bucket,
key: key,
response_target: path
)
end
|
#file_exists?(bucket:, key:) ⇒ Boolean
81
82
83
84
85
86
|
# File 'lib/formatron/aws.rb', line 81
def file_exists?(bucket:, key:)
@s3_client.list_objects(
bucket: bucket,
prefix: key
).contents.length > 0
end
|
#get_file(bucket:, key:) ⇒ Object
103
104
105
106
107
108
|
# File 'lib/formatron/aws.rb', line 103
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
137
138
139
140
141
|
# File 'lib/formatron/aws.rb', line 137
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
151
152
153
154
155
156
157
158
159
160
161
|
# File 'lib/formatron/aws.rb', line 151
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
163
164
165
166
167
168
169
|
# File 'lib/formatron/aws.rb', line 163
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
71
72
73
74
75
76
77
78
79
|
# File 'lib/formatron/aws.rb', line 71
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
|