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

Commit

Permalink
feat(framework): Environemnt variables in rails console
Browse files Browse the repository at this point in the history
  • Loading branch information
Igor Rzegocki committed Sep 14, 2016
1 parent 1ca5b0b commit 89252b3
Show file tree
Hide file tree
Showing 6 changed files with 36 additions and 2 deletions.
5 changes: 5 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -157,6 +157,11 @@ Currently only `Rails` is supported.
* **Default:** `true`
* `app['framework']['assets_precompilation_command']`
* A command which will be invoked to precompile assets.
* `app['framework']['envs_in_console']`
* **Supported values:** `true`, `false`
* **Default:** `false`
* If set to true, `rails console` will be invoked with all application-level
environment variables set.

### appserver

Expand Down
1 change: 1 addition & 0 deletions attributes/default.rb
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,7 @@
'&& /usr/local/bin/bundle exec rake db:migrate || /usr/local/bin/bundle exec rake db:setup'
default['defaults']['framework']['assets_precompile'] = true
default['defaults']['framework']['assets_precompilation_command'] = '/usr/local/bin/bundle exec rake assets:precompile'
default['defaults']['framework']['envs_in_console'] = false

# worker
## common
Expand Down
20 changes: 19 additions & 1 deletion libraries/drivers_framework_rails.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@ class Rails < Drivers::Framework::Base
adapter :rails
allowed_engines :rails
output filter: [
:migrate, :migration_command, :deploy_environment, :assets_precompile, :assets_precompilation_command
:migrate, :migration_command, :deploy_environment, :assets_precompile, :assets_precompilation_command,
:envs_in_console
]
packages debian: 'zlib1g-dev', rhel: 'zlib-devel'

Expand All @@ -27,6 +28,23 @@ def deploy_before_restart(context)
end if out[:assets_precompile]
end

def deploy_after_restart(context)
setup_rails_console(context)
end

def setup_rails_console(context)
return unless out[:envs_in_console]
deploy_to = deploy_dir(app)
env = environment

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

def environment
app['environment'].merge(out[:deploy_environment])
end
Expand Down
3 changes: 2 additions & 1 deletion spec/fixtures/node.rb
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,8 @@ def node(override = {})
},
framework: {
adapter: 'rails',
migrate: false
migrate: false,
envs_in_console: true
},
worker: {
adapter: 'sidekiq',
Expand Down
1 change: 1 addition & 0 deletions spec/unit/libraries/drivers_framework_rails_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
expect(described_class.new(aws_opsworks_app, node).out).to eq(
assets_precompile: true,
assets_precompilation_command: 'bundle exec rake assets:precompile',
envs_in_console: true,
deploy_environment: { 'RAILS_ENV' => 'staging' },
migration_command: 'rake db:migrate',
migrate: false
Expand Down
8 changes: 8 additions & 0 deletions templates/default/rails_console_overload.rb.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
Rails.application.class.send(:define_method, :load_console) do |app = Rails.application|

<% @environment.each do |key, value| %>
ENV['<%= key.to_s %>'] = "<%= value.to_s %>"
<% end %>

super(app)
end

0 comments on commit 89252b3

Please sign in to comment.