Class: Jets::Application

Inherits:
Object
  • Object
show all
Extended by:
Memoist
Includes:
Middleware, Singleton
Defined in:
lib/jets/application.rb

Constant Summary collapse

ENV_MAP =

Use the shorter name in stack names, but use the full name when it comes to checking for the env.

Example:

Jets.env: 'development'
Jets.config.project_namespace: 'demo-dev'
{
  development: 'dev',
  production: 'prod',
  staging: 'stag',
}

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Middleware

#build_stack, #call, #config_middleware, #default_stack, #endpoint, #middlewares

Class Method Details

.default_iam_policyObject



275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
# File 'lib/jets/application.rb', line 275

def self.default_iam_policy
  project_namespace = Jets.project_namespace
  logs = {
    action: ["logs:*"],
    effect: "Allow",
    resource: "arn:aws:logs:#{Jets.aws.region}:#{Jets.aws.}:log-group:/aws/lambda/#{project_namespace}-*",
  }
  s3_bucket = Jets.aws.s3_bucket
  s3_readonly = {
    action: ["s3:Get*", "s3:List*"],
    effect: "Allow",
    resource: "arn:aws:s3:::#{s3_bucket}*",
  }
  s3_bucket = {
    action: ["s3:ListAllMyBuckets", "s3:HeadBucket"],
    effect: "Allow",
    resource: "arn:aws:s3:::*", # scoped to all buckets
  }
  policies = [logs, s3_readonly, s3_bucket]

  if Jets::Stack.has_resources?
    cloudformation = {
      action: ["cloudformation:DescribeStacks"],
      effect: "Allow",
      resource: "arn:aws:cloudformation:#{Jets.aws.region}:#{Jets.aws.}:stack/#{project_namespace}*",
    }
    policies << cloudformation
  end
  policies
end

Instance Method Details

#awsObject



351
352
353
# File 'lib/jets/application.rb', line 351

def aws
  Jets::AwsInfo.new
end

#configObject



48
49
50
# File 'lib/jets/application.rb', line 48

def config
  @config ||= ActiveSupport::OrderedOptions.new # dont use memoize since we reset @config later
end

#configs!Object



19
20
21
22
23
24
# File 'lib/jets/application.rb', line 19

def configs!
  load_environments_config
  load_db_config
  set_iam_policy # relies on dependent values, must be called afterwards
  normalize_env_vars!
end

#configure(&block) ⇒ Object



10
11
12
# File 'lib/jets/application.rb', line 10

def configure(&block)
  instance_eval(&block) if block
end

#default_autoload_pathsObject

Essentially folders under app folder will be the default_autoload_paths. Example:

app/controllers
app/helpers
app/jobs
app/models
app/rules
app/shared/resources

Also include:

app/models/concerns
app/controllers/concerns


212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
# File 'lib/jets/application.rb', line 212

def default_autoload_paths
  paths = []
  Dir.glob("#{Jets.root}/app/*").each do |p|
    p.sub!('./','')
    paths << p unless exclude_autoload_path?(p)
  end
  # Handle concerns folders
  Dir.glob("#{Jets.root}/app/**/concerns").each do |p|
    p.sub!('./','')
    paths << p unless exclude_autoload_path?(p)
  end

  paths << "#{Jets.root}/app/shared/resources"
  paths << "#{Jets.root}/app/shared/extensions"

  paths
end

#default_configObject



52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
# File 'lib/jets/application.rb', line 52

def default_config
  config = ActiveSupport::OrderedOptions.new
  config.project_name = parse_project_name # must set early because other configs requires this
  config.cors = false
  config.autoload_paths = default_autoload_paths
  config.extra_autoload_paths = []
  config.logger = Jets::Logger.new($stderr)

  # function properties defaults
  config.function = ActiveSupport::OrderedOptions.new
  config.function.timeout = 30
  # default memory setting based on:
  # https://medium.com/epsagon/how-to-make-lambda-faster-memory-performance-benchmark-be6ebc41f0fc
  config.function.memory_size = 1536

  config.prewarm = ActiveSupport::OrderedOptions.new
  config.prewarm.enable = true
  config.prewarm.rate = '30 minutes'
  config.prewarm.concurrency = 2
  config.prewarm.public_ratio = 3
  config.prewarm.rack_ratio = 5

  config.gems = ActiveSupport::OrderedOptions.new
  config.gems.sources = [
    Jets.default_gems_source
  ]

  config.inflections = ActiveSupport::OrderedOptions.new
  config.inflections.irregular = {}

  config.assets = ActiveSupport::OrderedOptions.new
  config.assets.folders = %w[assets images packs]
  config.assets.base_url = nil # IE: https://cloudfront.com/my/base/path
  config.assets.max_age = 3600
  config.assets.cache_control = nil # IE: public, max-age=3600 , max_age is a shorter way to set cache_control.

  config.ruby = ActiveSupport::OrderedOptions.new

  config.middleware = Jets::Middleware::Configurator.new

  config.session = ActiveSupport::OrderedOptions.new
  config.session.store = Rack::Session::Cookie # note when accessing it use session[:store] since .store is an OrderedOptions method
  config.session.options = {}

  config.api = ActiveSupport::OrderedOptions.new
  config.api.authorization_type = "NONE"
  config.api.cors_authorization_type = nil # nil so ApiGateway::Cors#cors_authorization_type handles
  config.api.binary_media_types = ['multipart/form-data']
  config.api.endpoint_type = 'EDGE' # PRIVATE, EDGE, REGIONAL

  config.domain = ActiveSupport::OrderedOptions.new
  # config.domain.name = "#{Jets.project_namespace}.coolapp.com" # Default is nil
  # config.domain.cert_arn = "..."
  config.domain.endpoint_type = "REGIONAL" # EDGE or REGIONAL. Default to EDGE because CloudFormation update is faster
  config.domain.route53 = true # controls whether or not to create the managed route53 record.
    # Useful to disable this when user wants to manage the route themself like pointing
    # it to CloudFront for blue-green deployments instead.

  # Custom user lambda layers
  config.lambda = ActiveSupport::OrderedOptions.new
  config.lambda.layers = []

  # Only used for Jets Afterburner, Mega Mode currently. This is a fallback default
  # encoding.  Usually, the Rails response will return a content-type header and
  # the encoding in there is used when possible. Example Content-Type header:
  #   Content-Type    text/html; charset=utf-8
  config.encoding = ActiveSupport::OrderedOptions.new
  config.encoding.default = "utf-8"

  config.s3_event = ActiveSupport::OrderedOptions.new
  # These notification_configuration properties correspond to the ruby aws-sdk
  #   s3.put_bucket_notification_configuration
  # in jets/s3_bucket_config.rb, not the CloudFormation Bucket properties. The CloudFormation
  # bucket properties have a similiar structure but is slightly different so it can be confusing.
  #
  #   Ruby aws-sdk S3 Docs: https://amzn.to/2N7m5Lr
  config.s3_event.configure_bucket = true
  config.s3_event.notification_configuration = {
    topic_configurations: [
      {
        events: ["s3:ObjectCreated:*"],
        topic_arn: "!Ref SnsTopic", # must use this logical id
      },
    ],
  }

  # So tried to defined this in the jets/mailer.rb Turbine only but jets new requires it
  # config.action_mailer = ActiveSupport::OrderedOptions.new

  config
end

#deprecated_configs_messageObject



179
180
181
182
183
184
185
186
187
188
# File 'lib/jets/application.rb', line 179

def deprecated_configs_message
  unless config.ruby.lazy_load.nil?
    puts "Detected config.ruby.lazy_load = #{config.ruby.lazy_load.inspect}".color(:yellow)
    puts "Deprecated: config.ruby.lazy_load".color(:yellow)
    puts "Gems are now bundled with with Lambda Layer and there's no need to lazy load them."
    puts "Please remove the config in your config/application.rb or config/environments files."
    puts "You can have Jets automatically do this by running:"
    puts "  jets upgrade"
  end
end

#eval_app_configObject



166
167
168
169
# File 'lib/jets/application.rb', line 166

def eval_app_config
  app_config = "#{Jets.root}/config/application.rb"
  load app_config # use load instead of require so reload_configs! works
end

#exclude_autoload_path?(path) ⇒ Boolean

Returns:

  • (Boolean)


230
231
232
# File 'lib/jets/application.rb', line 230

def exclude_autoload_path?(path)
  path =~ %r{app/javascript} || path =~ %r{app/views}
end

#finish!Object



35
36
37
38
39
40
41
42
# File 'lib/jets/application.rb', line 35

def finish!
  deprecated_configs_message
  load_inflections
  load_routes
  # Load libraries at the end to trigger onload so we can defined options in any order.
  # Only action_mailer library have been used properly this way so far.
  require 'action_mailer'
end

#internal_autoload_pathsObject



234
235
236
237
238
239
240
241
242
243
# File 'lib/jets/application.rb', line 234

def internal_autoload_paths
  internal = File.expand_path("../internal", __FILE__)
  paths = %w[
    app/controllers
    app/helpers
    app/jobs
    app/models
  ]
  paths.map { |path| "#{internal}/#{path}" }
end

#load_db_configObject



318
319
320
321
322
323
324
325
326
327
328
# File 'lib/jets/application.rb', line 318

def load_db_config
  config.database = {}

  Jets::Dotenv.load!
  database_yml = "#{Jets.root}/config/database.yml"
  if File.exist?(database_yml)
    text = Jets::Erb.result(database_yml)
    db_config = YAML.load(text)
    config.database = db_config
  end
end

#load_default_configObject



159
160
161
162
163
164
# File 'lib/jets/application.rb', line 159

def load_default_config
  @config = default_config
  set_dependent_configs! # things like project_namespace that need project_name
  eval_app_config # this overwrites Jets.config.project_name
  Jets.config.project_name = parse_project_name # Must set again because JETS_PROJECT_NAME is possible
end

#load_environments_configObject



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

def load_environments_config
  env_file = "#{Jets.root}/config/environments/#{Jets.env}.rb"
  if File.exist?(env_file)
    code = IO.read(env_file)
    instance_eval(code)
  end
end

#load_inflectionsObject



44
45
46
# File 'lib/jets/application.rb', line 44

def load_inflections
  Jets::Inflections.load!
end

#load_routesObject



339
340
341
342
343
344
345
346
347
348
349
# File 'lib/jets/application.rb', line 339

def load_routes
  reload = Jets.env.development?
  @router = nil if reload # clear_routes

  routes_file = "#{Jets.root}/config/routes.rb"
  if reload
    load routes_file # always evaluate
  else
    require routes_file # evaluate once
  end
end

#normalize_env_vars!Object

It is pretty easy to attempt to set environment variables without the correct AWS Environment.Variables path struture. Auto-fix it for convenience.



309
310
311
312
313
314
315
316
# File 'lib/jets/application.rb', line 309

def normalize_env_vars!
  environment = config.function.environment
  if environment and !environment.to_h.key?(:variables)
    config.function.environment = {
      variables: environment.to_h
    }
  end
end

#parse_project_nameObject

Double evaling config/application.rb causes subtle issues:

* double loading of shared resources: Jets::Stack.subclasses will have the same
class twice when config is called when declaring a function
* forces us to rescue all exceptions, which is a big hammer

Lets parse for the project name instead for now.



151
152
153
154
155
156
157
# File 'lib/jets/application.rb', line 151

def parse_project_name
  return ENV['JETS_PROJECT_NAME'] if ENV['JETS_PROJECT_NAME'] # override

  lines = IO.readlines("#{Jets.root}/config/application.rb")
  project_name_line = lines.find { |l| l =~ /config\.project_name.*=/ }
  project_name_line.gsub(/.*=/,'').strip.gsub(/["']/,'') # project_name
end

#reload_configs!Object

After the mimimal template gets build, we need to reload it for the full stack creation. This allows us to reference IAM policies configs that depend on the creation of the s3 bucket.



29
30
31
32
33
# File 'lib/jets/application.rb', line 29

def reload_configs!
  # Tricky: reset only the things that depends on the minimal stack
  @config.iam_policy = nil
  configs!
end

#routesObject

Naming it routes because config/routes.rb requires

Jets.application.routes.draw do

for scaffolding to work.



335
336
337
# File 'lib/jets/application.rb', line 335

def routes
  @router ||= Jets::Router.new
end

#set_dependent_configs!Object



257
258
259
260
261
262
263
264
265
266
267
268
# File 'lib/jets/application.rb', line 257

def set_dependent_configs!
  # env_extra can be also be set with JETS_ENV_EXTRA.
  # JETS_ENV_EXTRA higher precedence than config.env_extra
  config.env_extra = ENV['JETS_ENV_EXTRA'] if ENV['JETS_ENV_EXTRA']
  # IE: With env_extra: project-dev-1
  #     Without env_extra: project-dev
  config.short_env = ENV_MAP[Jets.env.to_sym] || Jets.env
  # table_namespace does not have the env_extra, more common case desired.
  config.table_namespace = [config.project_name, config.short_env].compact.join('-')

  config.project_namespace = Jets.project_namespace
end

#set_iam_policyObject



270
271
272
273
# File 'lib/jets/application.rb', line 270

def set_iam_policy
  config.iam_policy ||= self.class.default_iam_policy
  config.managed_policy_definitions ||= [] # default empty
end

#setup!Object



14
15
16
17
# File 'lib/jets/application.rb', line 14

def setup!
  load_default_config
  setup_auto_load_paths
end

#setup_auto_load_pathsObject



190
191
192
193
194
195
196
197
198
199
# File 'lib/jets/application.rb', line 190

def setup_auto_load_paths
  autoload_paths = config.autoload_paths + config.extra_autoload_paths
  autoload_paths += internal_autoload_paths # internal_autoload_paths are last
  autoload_paths.each do |path|
    next unless File.exist?(path)
    Jets.loader.push_dir(path)
  end
  Jets.loader.enable_reloading if Jets.env.development?
  Jets.loader.setup
end