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



205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
# File 'lib/jets/application.rb', line 205

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



274
275
276
# File 'lib/jets/application.rb', line 274

def aws
  Jets::AwsInfo.new
end

#configObject



28
29
30
# File 'lib/jets/application.rb', line 28

def config
  @config ||= ActiveSupport::OrderedOptions.new # dont use memoize since we reset @config later
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


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

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

#default_config(project_name = nil) ⇒ Object



32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
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
# File 'lib/jets/application.rb', line 32

def default_config(project_name=nil)
  config = ActiveSupport::OrderedOptions.new
  config.project_name = project_name
  config.cors = true
  config.autoload_paths = default_autoload_paths
  config.extra_autoload_paths = []

  # 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 = [
    'https://gems.lambdagems.com'
  ]

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

  config.assets = ActiveSupport::OrderedOptions.new
  config.assets.folders = %w[public]
  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.ruby.lazy_load = true # also set in config/environments files

  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.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
end

#eval_app_configObject



112
113
114
115
# File 'lib/jets/application.rb', line 112

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)


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

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

#finish!Object



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

def finish!
  load_inflections
  setup_auto_load_paths
  load_routes
end

#internal_autoload_pathsObject



165
166
167
168
169
170
171
172
173
# File 'lib/jets/application.rb', line 165

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

#load_app_configObject



102
103
104
105
106
107
108
109
110
# File 'lib/jets/application.rb', line 102

def load_app_config
  project_name = parse_project_name
  @config = default_config(project_name)
  set_dependent_configs! # things like project_namespace that need project_name
  eval_app_config

  set_iam_policy # relies on dependent values, must be called afterwards
  normalize_env_vars!
end

#load_configsObject



132
133
134
135
136
# File 'lib/jets/application.rb', line 132

def load_configs
  load_app_config
  load_db_config
  load_environments_config
end

#load_db_configObject



248
249
250
251
252
253
254
255
256
257
258
# File 'lib/jets/application.rb', line 248

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_environments_configObject



124
125
126
127
128
129
130
# File 'lib/jets/application.rb', line 124

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



24
25
26
# File 'lib/jets/application.rb', line 24

def load_inflections
  Jets::Inflections.load!
end

#load_routesObject



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

def load_routes
  routes_file = "#{Jets.root}config/routes.rb"
  require routes_file if File.exist?(routes_file)
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.



239
240
241
242
243
244
245
246
# File 'lib/jets/application.rb', line 239

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.



96
97
98
99
100
# File 'lib/jets/application.rb', line 96

def parse_project_name
  lines = IO.readlines("#{Jets.root}config/application.rb")
  project_name_line = lines.find { |l| l =~ /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.



120
121
122
# File 'lib/jets/application.rb', line 120

def reload_configs!
  load_configs
end

#routesObject

Naming it routes because config/routes.rb requires

Jets.application.routes.draw do

for scaffolding to work.



265
266
267
# File 'lib/jets/application.rb', line 265

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

#set_dependent_configs!Object



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

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



200
201
202
203
# File 'lib/jets/application.rb', line 200

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

#setup!Object



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

def setup!
  load_configs
end

#setup_auto_load_pathsObject



138
139
140
141
142
143
# File 'lib/jets/application.rb', line 138

def setup_auto_load_paths
  autoload_paths = config.autoload_paths + config.extra_autoload_paths
  # internal_autoload_paths are last
  autoload_paths += internal_autoload_paths
  ActiveSupport::Dependencies.autoload_paths += autoload_paths
end