Class: Jets::Cfn::Ship
Constant Summary
Constants included
from Timing
Timing::RECORD_LOG_PATH
Instance Method Summary
collapse
#cfn, #lambda, #s3, #s3_resource, #stack_exists?, #stack_in_progress?, #sts
Methods included from Timing
clear, #record_data, #record_log, report
Constructor Details
#initialize(options) ⇒ Ship
Returns a new instance of Ship.
Instance Method Details
#capabilities ⇒ Object
133
134
135
136
137
138
139
|
# File 'lib/jets/cfn/ship.rb', line 133
def capabilities
["CAPABILITY_IAM", "CAPABILITY_NAMED_IAM"] end
|
#command_with_iam(capabilities) ⇒ Object
129
130
131
|
# File 'lib/jets/cfn/ship.rb', line 129
def command_with_iam(capabilities)
"#{File.basename($0)} #{ARGV.join(' ')} --capabilities #{capabilities}"
end
|
#create_stack ⇒ Object
51
52
53
54
55
|
# File 'lib/jets/cfn/ship.rb', line 51
def create_stack
template_body = IO.read(@template_path)
cfn.create_stack(stack_options)
end
|
#pretty_time(total_seconds) ⇒ Object
170
171
172
173
174
175
176
177
178
|
# File 'lib/jets/cfn/ship.rb', line 170
def pretty_time(total_seconds)
minutes = (total_seconds / 60) % 60
seconds = total_seconds % 60
if total_seconds < 60
"#{seconds.to_i}s"
else
"#{minutes.to_i}m #{seconds.to_i}s"
end
end
|
#prompt_for_iam(capabilities) ⇒ Object
121
122
123
124
125
126
127
|
# File 'lib/jets/cfn/ship.rb', line 121
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)"
confirm = $stdin.gets
end
|
#run ⇒ Object
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
|
# File 'lib/jets/cfn/ship.rb', line 15
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).colorize(:green)}"
retry
else
puts "Exited"
exit 1
end
end
wait_for_stack
prewarm
show_api_endpoint
end
|
#save_stack ⇒ Object
43
44
45
46
47
48
49
|
# File 'lib/jets/cfn/ship.rb', line 43
def save_stack
if stack_exists?(@parent_stack_name)
update_stack
else
create_stack
end
end
|
#show_api_endpoint ⇒ Object
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
|
# File 'lib/jets/cfn/ship.rb', line 97
def show_api_endpoint
return unless @options[:stack_type] == :full return if Jets::Router.routes.empty?
resp = cfn.describe_stack_resources(stack_name: @parent_stack_name)
resources = resp.stack_resources
api_gateway = resources.find { |resource| resource.logical_resource_id == "ApiGateway" }
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
|
#stack_options ⇒ Object
options common to both create_stack and update_stack
69
70
71
72
73
74
75
76
|
# File 'lib/jets/cfn/ship.rb', line 69
def stack_options
{
stack_name: @parent_stack_name,
template_body: IO.read(@template_path),
capabilities: capabilities, }
end
|
#stack_status ⇒ Object
115
116
117
118
119
|
# File 'lib/jets/cfn/ship.rb', line 115
def stack_status
resp = cfn.describe_stacks(stack_name: @parent_stack_name)
status = resp.stacks[0].stack_status
[resp, status]
end
|
#update_stack ⇒ Object
58
59
60
61
62
63
64
65
|
# File 'lib/jets/cfn/ship.rb', line 58
def update_stack
begin
cfn.update_stack(stack_options)
rescue Aws::CloudFormation::Errors::ValidationError => e
puts "ERROR: #{e.message}".red
error = true
end
end
|
#upload_to_s3 ⇒ Object
Upload both code and child templates to s3
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
|
# File 'lib/jets/cfn/ship.rb', line 142
def upload_to_s3
raise "Did not specify @options[:s3_bucket] #{@options[:s3_bucket].inspect}" unless @options[:s3_bucket]
bucket_name = @options[:s3_bucket]
puts "Uploading child CloudFormation templates to S3"
expression = "#{Jets::Naming.template_path_prefix}-*"
Dir.glob(expression).each do |path|
next unless File.file?(path)
key = "jets/cfn-templates/#{File.basename(path)}"
obj = s3_resource.bucket(bucket_name).object(key)
obj.upload_file(path)
end
md5_code_zipfile = Jets::Naming.md5_code_zipfile
file_size = number_to_human_size(File.size(md5_code_zipfile))
puts "Uploading #{md5_code_zipfile} (#{file_size}) to S3"
start_time = Time.now
key = Jets::Naming.code_s3_key
obj = s3_resource.bucket(bucket_name).object(key)
obj.upload_file(md5_code_zipfile)
puts "Time to upload code to s3: #{pretty_time(Time.now-start_time).colorize(:green)}"
end
|
#wait_for_stack ⇒ Object
check for /(_COMPLETE|_FAILED)$/ status
79
80
81
|
# File 'lib/jets/cfn/ship.rb', line 79
def wait_for_stack
Jets::Cfn::Status.new(@options).wait
end
|