Class: Jets::Application

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

Defined Under Namespace

Modules: Defaults

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',
}

Instance Method Summary collapse

Methods included from Defaults

#default_autoload_paths, #default_config, #default_ignore_paths

Methods included from Middleware

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

Instance Method Details

#awsObject



234
235
236
# File 'lib/jets/application.rb', line 234

def aws
  Jets::AwsInfo.new
end

#configObject



53
54
55
# File 'lib/jets/application.rb', line 53

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

#configs!Object



21
22
23
24
25
26
27
# File 'lib/jets/application.rb', line 21

def configs!
  load_environments_config
  load_db_config
  set_iam_policy # relies on dependent values, must be called afterwards
  set_time_zone
  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

#deprecated_configs_messageObject



93
94
95
96
97
98
99
100
101
102
# File 'lib/jets/application.rb', line 93

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

#each_app_autoload_path(expression) ⇒ Object



129
130
131
132
133
134
# File 'lib/jets/application.rb', line 129

def each_app_autoload_path(expression)
  Dir.glob(expression).each do |p|
    p.sub!('./','')
    yield(p) unless exclude_autoload_path?(p)
  end
end

#exclude_autoload_path?(path) ⇒ Boolean

Returns:

  • (Boolean)


136
137
138
139
140
# File 'lib/jets/application.rb', line 136

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

#finish!Object



38
39
40
41
42
43
44
45
46
47
# File 'lib/jets/application.rb', line 38

def finish!
  deprecated_configs_message
  load_inflections
  load_routes

  Jets::Controller::Rendering::RackRenderer.setup! # Sets up ActionView etc
  # 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



142
143
144
145
146
147
148
149
150
151
# File 'lib/jets/application.rb', line 142

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_config_applicationObject



80
81
82
83
# File 'lib/jets/application.rb', line 80

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

#load_db_config(database_yml = "#{Jets.root}/config/database.yml") ⇒ Object



200
201
202
203
204
205
206
207
208
209
210
211
# File 'lib/jets/application.rb', line 200

def load_db_config(database_yml="#{Jets.root}/config/database.yml")
  config.database = {}

  Jets::Dotenv.load!
  if File.exist?(database_yml)
    require "active_record/database_configurations" # lazy require
    text = Jets::Erb.result(database_yml)
    db_configs = Jets::Util::Yamler.load(text)
    configurations = ActiveRecord::DatabaseConfigurations.new(db_configs)
    config.database = configurations
  end
end

#load_default_configObject



72
73
74
75
76
77
78
# File 'lib/jets/application.rb', line 72

def load_default_config
  @config = default_config # sets Jets.config.project_name by calling parse_project_name
  set_computed_configs! # things like project_namespace that need project_name
  Jets::Dotenv.load! # needs Jets.config.project_name when using ssm in dotenv files
  Jets.config.project_name = parse_project_name # Must set again because JETS_PROJECT_NAME is possible
  load_config_application # this overwrites Jets.config.project_name
end

#load_environments_configObject



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

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, env_file)
  end
end

#load_inflectionsObject



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

def load_inflections
  Jets::Inflections.load!
end

#load_routes(refresh: false) ⇒ Object



222
223
224
225
226
227
228
229
230
231
232
# File 'lib/jets/application.rb', line 222

def load_routes(refresh: false)
  @router = nil if refresh # clear_routes

  routes_file = "#{Jets.root}/config/routes.rb"
  return unless File.exist?(routes_file)
  if refresh
    load routes_file # always evaluate
  else
    require routes_file # evaluate once
  end
end

#main_loaderObject



104
105
106
# File 'lib/jets/application.rb', line 104

def main_loader
  Jets::Autoloaders.main
end

#main_loader_setupObject



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

def main_loader_setup
  main_loader.enable_reloading if Jets.env.development?
  main_loader.setup # only respected on the first call
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.



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

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.



64
65
66
67
68
69
70
# File 'lib/jets/application.rb', line 64

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.*=/ && l !~ /^\s+#/ }
  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.



32
33
34
35
36
# File 'lib/jets/application.rb', line 32

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.



218
219
220
# File 'lib/jets/application.rb', line 218

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

#set_computed_configs!Object



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

def set_computed_configs!
  # env var JETS_EXTRA higher precedence than config.extra
  config.extra = Jets.extra
  # IE: With extra: project-dev-1
  #     Without extra: project-dev
  config.short_env = ENV_MAP[Jets.env.to_sym] || Jets.env
  # table_namespace does not have the 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



177
178
179
180
181
182
# File 'lib/jets/application.rb', line 177

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

#set_time_zoneObject



184
185
186
# File 'lib/jets/application.rb', line 184

def set_time_zone
  Time.zone_default = Time.find_zone!(config.time_zone)
end

#setup!Object



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

def setup!
  load_default_config
  setup_autoload_paths
  setup_ignore_paths
  main_loader_setup
end

#setup_autoload_pathsObject



108
109
110
111
112
113
114
# File 'lib/jets/application.rb', line 108

def setup_autoload_paths
  autoload_paths = default_autoload_paths + config.autoload_paths
  autoload_paths.each do |path|
    next unless File.exist?(path)
    main_loader.push_dir(path)
  end
end

#setup_ignore_pathsObject

Allow use to add config.ignore_paths just in case there’s some case Jets hasn’t considered



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

def setup_ignore_paths
  ignore_paths = default_ignore_paths + config.ignore_paths
  ignore_paths.each do |path|
    main_loader.ignore("#{Jets.root}/#{path}")
  end
end