Class: Jets::Cfn::Ship
- Inherits:
-
Object
show all
- Extended by:
- Memoist
- Includes:
- AwsServices
- Defined in:
- lib/jets/cfn/ship.rb
Instance Method Summary
collapse
#apigateway, #aws_lambda, #cfn, #dynamodb, #logs, #s3, #s3_resource, #sns, #sqs, #sts
#lookup, #stack_exists?, #stack_in_progress?
included
Constructor Details
#initialize(options) ⇒ Ship
Returns a new instance of Ship.
Instance Method Details
#api_gateway ⇒ Object
Do not memoize this because on first stack run it will be nil It only gets called one more time so just let it get called.
116
117
118
119
120
|
# File 'lib/jets/cfn/ship.rb', line 116
def api_gateway
resp = cfn.describe_stack_resources(stack_name: @parent_stack_name)
resources = resp.stack_resources
resources.find { |resource| resource.logical_resource_id == "ApiGateway" }
end
|
#capabilities ⇒ Object
169
170
171
172
173
174
175
|
# File 'lib/jets/cfn/ship.rb', line 169
def capabilities
["CAPABILITY_IAM", "CAPABILITY_NAMED_IAM"] end
|
#clean_deploy_logs ⇒ Object
#command_with_iam(capabilities) ⇒ Object
165
166
167
|
# File 'lib/jets/cfn/ship.rb', line 165
def command_with_iam(capabilities)
"#{File.basename($0)} #{ARGV.join(' ')} --capabilities #{capabilities}"
end
|
#create_stack ⇒ Object
60
61
62
63
|
# File 'lib/jets/cfn/ship.rb', line 60
def create_stack
cfn.create_stack(stack_options)
end
|
#endpoint_available? ⇒ Boolean
123
124
125
|
# File 'lib/jets/cfn/ship.rb', line 123
def endpoint_available?
!endpoint_unavailable?
end
|
#endpoint_unavailable? ⇒ Boolean
106
107
108
109
110
111
112
|
# File 'lib/jets/cfn/ship.rb', line 106
def endpoint_unavailable?
return true unless @options[:stack_type] == :full return true if Jets::Router.routes.empty?
_, status = stack_status
return true if status.include?("ROLLBACK")
return true unless api_gateway
end
|
#prewarm ⇒ Object
89
90
91
92
93
94
95
96
97
98
99
100
|
# File 'lib/jets/cfn/ship.rb', line 89
def prewarm
if ENV['SKIP_PREWARMING']
puts "Skipping prewarming" return
end
return unless @options[:stack_type] == :full return unless Jets.config.prewarm.enable
return if Jets.poly_only?
puts "Prewarming application."
Jets::PreheatJob.prewarm!
end
|
#prompt_for_iam(capabilities) ⇒ Object
157
158
159
160
161
162
163
|
# File 'lib/jets/cfn/ship.rb', line 157
def prompt_for_iam(capabilities)
puts "This stack will create IAM resources. Please approve to run the command again with #{capabilities} capabilities."
puts " #{command_with_iam(capabilities)}"
puts "Please confirm (y/n)"
$stdin.gets end
|
#run ⇒ Object
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
|
# File 'lib/jets/cfn/ship.rb', line 12
def run
upload_to_s3 if @options[:stack_type] == :full
stack_in_progress?(@parent_stack_name)
puts "Deploying CloudFormation stack with jets app!"
begin
save_stack
rescue Aws::CloudFormation::Errors::InsufficientCapabilitiesException => e
capabilities = e.message.match(/\[(.*)\]/)[1]
confirm = prompt_for_iam(capabilities)
if confirm =~ /^y/
@options.merge!(capabilities: [capabilities])
puts "Re-running: #{command_with_iam(capabilities).color(:green)}"
retry
else
puts "Exited"
exit 1
end
end
success = wait_for_stack
unless success
puts <<~EOL
The Jets application failed to deploy. Jets creates a few CloudFormation stacks to deploy your application.
The logs above show the CloudFormation parent stack events and points to the stack with the error.
Please go to the CloudFormation console and look for the specific stack with the error.
The specific child stack usually shows more detailed information and can be used to resolve the issue.
Example of checking the CloudFormation console: https://rubyonjets.com/docs/debugging/cloudformation/
EOL
exit 1
end
prewarm
clean_deploy_logs
show_api_endpoint
show_custom_domain
end
|
#save_stack ⇒ Object
52
53
54
55
56
57
58
|
# File 'lib/jets/cfn/ship.rb', line 52
def save_stack
if stack_exists?(@parent_stack_name)
update_stack
else
create_stack
end
end
|
#show_api_endpoint ⇒ Object
127
128
129
130
131
132
133
134
135
136
137
|
# File 'lib/jets/cfn/ship.rb', line 127
def show_api_endpoint
return unless endpoint_available?
stack_id = api_gateway["physical_resource_id"]
resp = cfn.describe_stacks(stack_name: stack_id)
stack = resp["stacks"].first
output = stack["outputs"].find { |o| o["output_key"] == "RestApiUrl" }
endpoint = output["output_value"]
puts "API Gateway Endpoint: #{endpoint}"
end
|
#show_custom_domain ⇒ Object
139
140
141
142
143
144
145
146
147
|
# File 'lib/jets/cfn/ship.rb', line 139
def show_custom_domain
return unless endpoint_available? && Jets.custom_domain? && Jets.config.domain.route53
domain_name = Jets::Resource::ApiGateway::DomainName.new
url = "https://#{domain_name.domain_name}"
puts "Custom Domain: #{url}"
end
|
#stack_options ⇒ Object
options common to both create_stack and update_stack
75
76
77
78
79
80
81
82
|
# File 'lib/jets/cfn/ship.rb', line 75
def stack_options
{
stack_name: @parent_stack_name,
template_body: IO.read(@template_path),
capabilities: capabilities, }
end
|
#stack_status ⇒ Object
151
152
153
154
155
|
# File 'lib/jets/cfn/ship.rb', line 151
def stack_status
resp = cfn.describe_stacks(stack_name: @parent_stack_name)
status = resp.stacks[0].stack_status
[resp, status]
end
|
#update_stack ⇒ Object
65
66
67
68
69
70
71
72
|
# File 'lib/jets/cfn/ship.rb', line 65
def update_stack
begin
cfn.update_stack(stack_options)
rescue Aws::CloudFormation::Errors::ValidationError => e
puts "ERROR: #{e.message}".color(:red)
true end
end
|
#upload_to_s3 ⇒ Object
Upload both code and child templates to s3
178
179
180
181
182
183
|
# File 'lib/jets/cfn/ship.rb', line 178
def upload_to_s3
raise "Did not specify @options[:s3_bucket] #{@options[:s3_bucket].inspect}" unless @options[:s3_bucket]
uploader = Upload.new(@options[:s3_bucket])
uploader.upload
end
|
#wait_for_stack ⇒ Object
check for /(_COMPLETE|_FAILED)$/ status
85
86
87
|
# File 'lib/jets/cfn/ship.rb', line 85
def wait_for_stack
Jets::Cfn::Status.new(@options).wait
end
|