Class: Jets::Application
- Inherits:
-
Object
- Object
- Jets::Application
- Extended by:
- Memoist
- Includes:
- DefaultConfig, Middleware, Singleton
- Defined in:
- lib/jets/application.rb,
lib/jets/application/default_config.rb
Defined Under Namespace
Modules: DefaultConfig
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
- #aws ⇒ Object
- #config ⇒ Object
- #configs! ⇒ Object
- #configure(&block) ⇒ Object
-
#default_autoload_paths ⇒ Object
Essentially folders under app folder will be the default_autoload_paths.
- #deprecated_configs_message ⇒ Object
- #each_app_autoload_path(expression) ⇒ Object
- #exclude_autoload_path?(path) ⇒ Boolean
- #finish! ⇒ Object
- #internal_autoload_paths ⇒ Object
- #load_config_application ⇒ Object
- #load_db_config ⇒ Object
- #load_default_config ⇒ Object
- #load_environments_config ⇒ Object
- #load_inflections ⇒ Object
- #load_routes(refresh: false) ⇒ Object
-
#normalize_env_vars! ⇒ Object
It is pretty easy to attempt to set environment variables without the correct AWS Environment.Variables path struture.
-
#parse_project_name ⇒ Object
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.
-
#reload_configs! ⇒ Object
After the mimimal template gets build, we need to reload it for the full stack creation.
-
#routes ⇒ Object
Naming it routes because config/routes.rb requires.
- #set_computed_configs! ⇒ Object
- #set_iam_policy ⇒ Object
- #setup! ⇒ Object
- #setup_auto_load_paths(autoload_paths = default_autoload_paths) ⇒ Object
Methods included from DefaultConfig
Methods included from Middleware
#build_stack, #call, #config_middleware, #default_stack, #endpoint, #middlewares
Class Method Details
.default_iam_policy ⇒ Object
192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 |
# File 'lib/jets/application.rb', line 192 def self.default_iam_policy project_namespace = Jets.project_namespace logs = { action: ["logs:*"], effect: "Allow", resource: "arn:aws:logs:#{Jets.aws.region}:#{Jets.aws.account}: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.account}:stack/#{project_namespace}*", } policies << cloudformation end policies end |
Instance Method Details
#aws ⇒ Object
268 269 270 |
# File 'lib/jets/application.rb', line 268 def aws Jets::AwsInfo.new end |
#config ⇒ Object
51 52 53 |
# File 'lib/jets/application.rb', line 51 def config @config ||= ActiveSupport::OrderedOptions.new # dont use memoize since we reset @config later end |
#configs! ⇒ Object
20 21 22 23 24 25 |
# File 'lib/jets/application.rb', line 20 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_paths ⇒ Object
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
122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 |
# File 'lib/jets/application.rb', line 122 def default_autoload_paths paths = [] each_app_autoload_path("#{Jets.root}/app/*") do |path| paths << path end # Handle concerns folders each_app_autoload_path("#{Jets.root}/app/**/concerns") do |path| paths << path end paths << "#{Jets.root}/app/shared/resources" paths << "#{Jets.root}/app/shared/extensions" paths end |
#deprecated_configs_message ⇒ Object
90 91 92 93 94 95 96 97 98 99 |
# File 'lib/jets/application.rb', line 90 def 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
138 139 140 141 142 143 |
# File 'lib/jets/application.rb', line 138 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
145 146 147 148 149 |
# File 'lib/jets/application.rb', line 145 def exclude_autoload_path?(path) path =~ %r{app/javascript} || path =~ %r{app/views} || path =~ %r{/functions} # app and shared end |
#finish! ⇒ Object
36 37 38 39 40 41 42 43 44 45 |
# File 'lib/jets/application.rb', line 36 def finish! 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_paths ⇒ Object
151 152 153 154 155 156 157 158 159 160 |
# File 'lib/jets/application.rb', line 151 def internal_autoload_paths internal = File.("../internal", __FILE__) paths = %w[ app/controllers app/helpers app/jobs app/models ] paths.map { |path| "#{internal}/#{path}" } end |
#load_config_application ⇒ Object
77 78 79 80 |
# File 'lib/jets/application.rb', line 77 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 ⇒ Object
235 236 237 238 239 240 241 242 243 244 245 |
# File 'lib/jets/application.rb', line 235 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_config ⇒ Object
70 71 72 73 74 75 |
# File 'lib/jets/application.rb', line 70 def load_default_config @config = default_config set_computed_configs! # things like project_namespace that need project_name load_config_application # 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_config ⇒ Object
82 83 84 85 86 87 88 |
# File 'lib/jets/application.rb', line 82 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_inflections ⇒ Object
47 48 49 |
# File 'lib/jets/application.rb', line 47 def load_inflections Jets::Inflections.load! end |
#load_routes(refresh: false) ⇒ Object
256 257 258 259 260 261 262 263 264 265 266 |
# File 'lib/jets/application.rb', line 256 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 |
#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.
226 227 228 229 230 231 232 233 |
# File 'lib/jets/application.rb', line 226 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_name ⇒ Object
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.
62 63 64 65 66 67 68 |
# File 'lib/jets/application.rb', line 62 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.
30 31 32 33 34 |
# File 'lib/jets/application.rb', line 30 def reload_configs! # Tricky: reset only the things that depends on the minimal stack @config.iam_policy = nil configs! end |
#routes ⇒ Object
Naming it routes because config/routes.rb requires
Jets.application.routes.draw do
for scaffolding to work.
252 253 254 |
# File 'lib/jets/application.rb', line 252 def routes @router ||= Jets::Router.new end |
#set_computed_configs! ⇒ Object
174 175 176 177 178 179 180 181 182 183 184 185 |
# File 'lib/jets/application.rb', line 174 def set_computed_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_policy ⇒ Object
187 188 189 190 |
# File 'lib/jets/application.rb', line 187 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 18 |
# File 'lib/jets/application.rb', line 14 def setup! load_default_config autoload_paths = config.autoload_paths + config.extra_autoload_paths setup_auto_load_paths(autoload_paths) end |
#setup_auto_load_paths(autoload_paths = default_autoload_paths) ⇒ Object
101 102 103 104 105 106 107 108 109 |
# File 'lib/jets/application.rb', line 101 def setup_auto_load_paths(autoload_paths=default_autoload_paths) loader = Jets::Autoloaders.main autoload_paths.each do |path| next unless File.exist?(path) loader.push_dir(path) end loader.enable_reloading if Jets.env.development? loader.setup end |