Class: Jets::Cfn::Ship

Inherits:
Object
  • Object
show all
Extended by:
Memoist
Includes:
AwsServices
Defined in:
lib/jets/cfn/ship.rb

Instance Method Summary collapse

Methods included from AwsServices

#apigateway, #aws_lambda, #aws_options, #cfn, #dynamodb, #logs, #s3, #s3_resource, #sns, #sqs, #sts

Methods included from AwsServices::StackStatus

#lookup, #stack_exists?, #stack_in_progress?

Methods included from AwsServices::GlobalMemoist

included

Constructor Details

#initialize(options) ⇒ Ship

Returns a new instance of Ship.



6
7
8
9
# File 'lib/jets/cfn/ship.rb', line 6

def initialize(options)
  @options = options
  @parent_stack_name = Jets::Names.parent_stack_name
end

Instance Method Details

#api_gatewayObject

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.



129
130
131
132
133
# File 'lib/jets/cfn/ship.rb', line 129

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

#capabilitiesObject



183
184
185
# File 'lib/jets/cfn/ship.rb', line 183

def capabilities
  ["CAPABILITY_IAM", "CAPABILITY_NAMED_IAM"]
end

#clean_deploy_logsObject



115
116
117
# File 'lib/jets/cfn/ship.rb', line 115

def clean_deploy_logs
  Jets::Commands::Clean::Log.new(@options).clean_deploys
end

#command_with_iam(capabilities) ⇒ Object



179
180
181
# File 'lib/jets/cfn/ship.rb', line 179

def command_with_iam(capabilities)
  "#{File.basename($0)} #{ARGV.join(' ')} --capabilities #{capabilities}"
end

#create_deployment_recordObject



49
50
51
52
53
54
55
56
# File 'lib/jets/cfn/ship.rb', line 49

def create_deployment_record
  return if @options[:stack_type] == :minimal
  resp = Jets::Cfn::Deployment.new(@options.merge(stack_name: @parent_stack_name)).create
  if resp
    version = resp["version"]
    Jets::Cfn::Upload.new.upload_cfn_templates(version) if version
  end
end

#create_stackObject



70
71
72
73
# File 'lib/jets/cfn/ship.rb', line 70

def create_stack
  # parent stack template is on filesystem and child stacks templates is on s3
  cfn.create_stack(stack_options)
end

#endpoint_available?Boolean

Returns:

  • (Boolean)


136
137
138
# File 'lib/jets/cfn/ship.rb', line 136

def endpoint_available?
  !endpoint_unavailable?
end

#endpoint_unavailable?Boolean

Returns:

  • (Boolean)


119
120
121
122
123
124
125
# File 'lib/jets/cfn/ship.rb', line 119

def endpoint_unavailable?
  return true unless @options[:stack_type] == :full # s3 bucket is available
  return true if Jets::Router.no_routes?
  _, status = stack_status
  return true if status.include?("ROLLBACK")
  return true unless api_gateway
end

#prewarmObject



102
103
104
105
106
107
108
109
110
111
112
113
# File 'lib/jets/cfn/ship.rb', line 102

def prewarm
  if ENV['SKIP_PREWARMING']
    puts "Skipping prewarming" # useful for testing
    return
  end
  return unless @options[:stack_type] == :full # s3 bucket is available
  return unless Jets.config.prewarm.enable
  return unless Jets.gem_layer?

  puts "Prewarming application."
  Jets::PreheatJob.prewarm!
end

#prompt_for_iam(capabilities) ⇒ Object



171
172
173
174
175
176
177
# File 'lib/jets/cfn/ship.rb', line 171

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 # confirm
end

#runObject



11
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
# File 'lib/jets/cfn/ship.rb', line 11

def run
  # s3 bucket is available only when stack_type is full
  upload_to_s3 if @options[:stack_type] == :full

  stack_in_progress?(@parent_stack_name)

  puts "Deploying CloudFormation stack with jets app!"
  begin
    set_resource_tags
    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

  # waits for /(_COMPLETE|_FAILED)$/ status
  cfn_status = Jets::Cfn::Status.new
  success = cfn_status.wait
  unless success
    cfn_status.failure_message!
  end

  save_apigw_state
  prewarm
  clean_deploy_logs
  show_api_endpoint
  show_custom_domain
  create_deployment_record
end

#save_apigw_stateObject



98
99
100
# File 'lib/jets/cfn/ship.rb', line 98

def save_apigw_state
  Jets::Router::State.save_apigw_state
end

#save_stackObject



62
63
64
65
66
67
68
# File 'lib/jets/cfn/ship.rb', line 62

def save_stack
  if stack_exists?(@parent_stack_name)
    update_stack
  else
    create_stack
  end
end

#set_resource_tagsObject



58
59
60
# File 'lib/jets/cfn/ship.rb', line 58

def set_resource_tags
  @tags = Jets.config.cfn.build.resource_tags.map { |key, value| { key: key, value: value } }
end

#show_api_endpointObject



140
141
142
143
144
145
146
147
148
149
150
# File 'lib/jets/cfn/ship.rb', line 140

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_domainObject



152
153
154
155
156
157
158
159
160
161
# File 'lib/jets/cfn/ship.rb', line 152

def show_custom_domain
  return unless endpoint_available? && Jets.custom_domain? && Jets.config.domain.route53

  domain_name = Jets::Cfn::Resource::ApiGateway::DomainName.new
  # Looks funny but its right.
  # domain_name is a method on the Jets::Cfn::Resource::ApiGateway::Domain instance
  url = "https://#{domain_name.domain_name}"
  puts "Custom Domain: #{url}"
  puts "App Domain: https://#{Jets.config.app.domain}" if Jets.config.app.domain
end

#stack_optionsObject

options common to both create_stack and update_stack



85
86
87
88
89
90
91
92
# File 'lib/jets/cfn/ship.rb', line 85

def stack_options
  {
    stack_name: @parent_stack_name,
    capabilities: capabilities, # ["CAPABILITY_IAM", "CAPABILITY_NAMED_IAM"]
    # disable_rollback: !@options[:rollback],
    tags: @tags,
  }.merge!(template.stack_option)
end

#stack_statusObject



165
166
167
168
169
# File 'lib/jets/cfn/ship.rb', line 165

def stack_status
  resp = cfn.describe_stacks(stack_name: @parent_stack_name)
  status = resp.stacks[0].stack_status
  [resp, status]
end

#templateObject



94
95
96
# File 'lib/jets/cfn/ship.rb', line 94

def template
  @template ||= Template.new(Jets::Names.parent_template_path, @options)
end

#update_stackObject



75
76
77
78
79
80
81
82
# File 'lib/jets/cfn/ship.rb', line 75

def update_stack
  begin
    cfn.update_stack(stack_options)
  rescue Aws::CloudFormation::Errors::ValidationError => e
    puts "ERROR: #{e.message}".color(:red)
    true # error
  end
end

#upload_to_s3Object

Upload both code and child templates to s3



188
189
190
# File 'lib/jets/cfn/ship.rb', line 188

def upload_to_s3
  Upload.new.upload
end