Skip to content
This repository has been archived by the owner on Dec 31, 2022. It is now read-only.

Commit

Permalink
fix(framework): Fixed envs_in_console for rails
Browse files Browse the repository at this point in the history
Up to this commit, `envs_in_console` was creating a `config/initializers/000_console.rb` file, which
basically overloaded `Rails.application.load_console` method, with new ENVs. However, this caused
problems, since console gets loaded pretty late in initialization stack. For example, if ENVs were
used in `application/secrets.yml` (which is common practice) they wasn't loaded, since secrets is
populated before loading the console. To avoid that, a new solution was implemented -
`config/application.rb` is updated, with the ENVs (while still maintaining console context). This
ensures, that all the variables are loaded as fast as possible, and visible to as many Rails
components as they need to be.

Signed-off-by: Igor Rzegocki <igor@rzegocki.pl>
  • Loading branch information
ajgon committed Sep 30, 2016
1 parent c1ed974 commit f8856c8
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 7 deletions.
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -186,6 +186,9 @@ ActiveRecord::Base.configurations[:production] = database_url || {
* **Default:** `false`
* If set to true, `rails console` will be invoked with all application-level
environment variables set.
* **WARNING!** This is highly unstable feature. If you experience any troubles
with deployments, and have this feature enabled, consider disabling it as a
first step in your debugging process.

### appserver

Expand Down
16 changes: 9 additions & 7 deletions libraries/drivers_framework_rails.rb
Original file line number Diff line number Diff line change
Expand Up @@ -45,14 +45,16 @@ def database_yml(db)

def setup_rails_console
return unless out[:envs_in_console]
deploy_to = deploy_dir(app)
env = environment
application_rb_path = File.join(deploy_dir(app), 'current', 'config', 'application.rb')

context.template File.join(deploy_to, 'current', 'config', 'initializers', '000_console.rb') do
owner node['deployer']['user']
group www_group
source 'rails_console_overload.rb.erb'
variables environment: env
if File.exist?(application_rb_path)
env_code = "if(defined?(Rails::Console))\n " +
environment.map { |key, value| "ENV['#{key}'] = #{value.inspect}" }.join("\n ") +
"\nend\n"

contents = File.read(application_rb_path).sub(/(^(?:module|class).*$)/, "#{env_code}\n\\1")

File.open(application_rb_path, 'w') { |file| file.write(contents) }
end
end

Expand Down

0 comments on commit f8856c8

Please sign in to comment.