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

Commit

Permalink
refactor: Replaced rails_env with deploy_env
Browse files Browse the repository at this point in the history
By changing `app['framework']['rails_env']` to `app['framework']['deploy_env']` the option itself is
made more general. Thanks to that, when `rails` framework is used, `RAILS_ENV` will be set. If
future frameworks are added to the stack, they can safely set their own configurations (`RACK_ENV`,
`SINATRA_ENV` etc.).

Related to #34
  • Loading branch information
Igor Rzegocki committed Sep 1, 2016
1 parent e2212b6 commit a3ee7d8
Show file tree
Hide file tree
Showing 9 changed files with 28 additions and 20 deletions.
6 changes: 3 additions & 3 deletions README.md
Expand Up @@ -143,10 +143,10 @@ Currently only `Rails` is supported.
* **Default:** `true`
* `app['framework']['assets_precompilation_command']`
* A command which will be invoked to precompile assets.
* `app['framework']['rails_env']`
* `app['framework']['deploy_env']`
* **Default:** `production`
* `rails` framework only. Sets the `RAILS_ENV` variable for all the ruby-related
actions in the project (server, worker, etc.)
* Sets the "environment" for all the app-related (for example `RAILS_ENV` in
Rails) actions in the project (server, worker, etc.)

### appserver

Expand Down
2 changes: 1 addition & 1 deletion attributes/default.rb
Expand Up @@ -75,6 +75,7 @@
## common

default['defaults']['framework']['adapter'] = 'rails'
default['defaults']['framework']['deploy_env'] = 'production'

## rails

Expand All @@ -84,7 +85,6 @@
'&& /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']['rails_env'] = 'production'

# worker
## common
Expand Down
7 changes: 5 additions & 2 deletions libraries/drivers_appserver_unicorn.rb
Expand Up @@ -46,19 +46,22 @@ def add_unicorn_config(context)
end
end

# rubocop:disable Metrics/MethodLength, Metrics/AbcSize
def add_unicorn_service_script(context)
deploy_to = deploy_dir(app)
app_shortname = app['shortname']
rails_env = app['attributes']['rails_env'] || 'production'
deploy_env = node['deploy'][app['shortname']].try(:[], 'framework').try(:[], 'deploy_env') ||
app['attributes']['rails_env'] || 'production'

context.template File.join(deploy_to, File.join('shared', 'scripts', 'unicorn.service')) do
owner node['deployer']['user']
group www_group
mode '0755'
source 'unicorn.service.erb'
variables app_shortname: app_shortname, deploy_dir: deploy_to, rails_env: rails_env
variables app_shortname: app_shortname, deploy_dir: deploy_to, deploy_env: deploy_env
end
end
# rubocop:enable Metrics/MethodLength, Metrics/AbcSize

def add_unicorn_service_context(context)
deploy_to = deploy_dir(app)
Expand Down
8 changes: 5 additions & 3 deletions libraries/drivers_db_base.rb
Expand Up @@ -17,21 +17,23 @@ def setup(context)
handle_packages(context)
end

# rubocop:disable Metrics/MethodLength, Metrics/AbcSize
def configure(context)
return unless applicable_for_configuration?

database = out
rails_env = app['attributes']['rails_env']
deploy_env = node['deploy'][app['shortname']].try(:[], 'framework').try(:[], 'deploy_env') ||
app['attributes']['rails_env']

context.template File.join(deploy_dir(app), 'shared', 'config', 'database.yml') do
source 'database.yml.erb'
mode '0660'
owner node['deployer']['user'] || 'root'
group www_group
variables(database: database, environment: rails_env)
variables(database: database, environment: deploy_env)
end
end

# rubocop:disable Metrics/AbcSize
def out
if configuration_data_source == :node_engine
return out_defaults.merge(
Expand Down
4 changes: 2 additions & 2 deletions libraries/drivers_framework_rails.rb
Expand Up @@ -6,12 +6,12 @@ class Rails < Drivers::Framework::Base
allowed_engines :rails
output filter: [
:migrate, :migration_command, :deploy_environment, :assets_precompile, :assets_precompilation_command,
:rails_env
:deploy_env
]
packages debian: 'zlib1g-dev', rhel: 'zlib-devel'

def raw_out
super.merge(deploy_environment: { 'RAILS_ENV' => super[:rails_env] })
super.merge(deploy_environment: { 'RAILS_ENV' => super[:deploy_env] })
end

def deploy_before_restart(context)
Expand Down
2 changes: 1 addition & 1 deletion spec/fixtures/node.rb
Expand Up @@ -43,7 +43,7 @@ def node(override = {})
},
framework: {
adapter: 'rails',
rails_env: 'staging',
deploy_env: 'staging',
migrate: false
},
worker: {
Expand Down
2 changes: 1 addition & 1 deletion spec/unit/libraries/drivers_framework_rails_spec.rb
Expand Up @@ -17,7 +17,7 @@
deploy_environment: { 'RAILS_ENV' => 'staging' },
migration_command: 'rake db:migrate',
migrate: false,
rails_env: 'staging'
deploy_env: 'staging'
)
end
end
15 changes: 9 additions & 6 deletions spec/unit/recipes/configure_spec.rb
Expand Up @@ -60,7 +60,7 @@
expect(db_config[:adapter]).to eq 'postgresql'
expect(chef_run)
.to render_file("/srv/www/#{aws_opsworks_app['shortname']}/shared/config/database.yml").with_content(
JSON.parse({ development: db_config, production: db_config }.to_json).to_yaml
JSON.parse({ development: db_config, production: db_config, staging: db_config }.to_json).to_yaml
)
end

Expand All @@ -85,7 +85,7 @@
.with_content("ROOT_PATH=\"/srv/www/#{aws_opsworks_app['shortname']}\"")
expect(chef_run)
.to render_file("/srv/www/#{aws_opsworks_app['shortname']}/shared/scripts/unicorn.service")
.with_content('unicorn_rails --env production')
.with_content('unicorn_rails --env staging')
end

it 'defines unicorn service' do
Expand Down Expand Up @@ -303,14 +303,14 @@

expect(chef_run)
.to render_file("/srv/www/#{aws_opsworks_app['shortname']}/shared/config/database.yml").with_content(
JSON.parse({ development: db_config, production: db_config }.to_json).to_yaml
JSON.parse({ development: db_config, production: db_config, staging: db_config }.to_json).to_yaml
)
end
end

context 'Sqlite3' do
let(:dummy_node) do
node(deploy: { dummy_project: { database: { adapter: 'sqlite3' } } })
node(deploy: { dummy_project: { database: { adapter: 'sqlite3' }, framework: { deploy_env: 'staging' } } })
end
let(:chef_run) do
ChefSpec::SoloRunner.new(platform: 'ubuntu', version: '14.04') do |solo_node|
Expand All @@ -329,7 +329,7 @@
expect(db_config[:database]).to eq 'db/data.sqlite3'
expect(chef_run)
.to render_file("/srv/www/#{aws_opsworks_app['shortname']}/shared/config/database.yml").with_content(
JSON.parse({ development: db_config, production: db_config }.to_json).to_yaml
JSON.parse({ development: db_config, production: db_config, staging: db_config }.to_json).to_yaml
)
end
end
Expand All @@ -344,6 +344,9 @@
password: 'password_936',
host: 'dummy-project.936.us-west-2.rds.amazon.com',
database: 'database_936'
},
framework: {
deploy_env: 'staging'
}
}
})
Expand All @@ -368,7 +371,7 @@
expect(db_config[:database]).to eq 'database_936'
expect(chef_run)
.to render_file("/srv/www/#{aws_opsworks_app['shortname']}/shared/config/database.yml").with_content(
JSON.parse({ development: db_config, production: db_config }.to_json).to_yaml
JSON.parse({ development: db_config, production: db_config, staging: db_config }.to_json).to_yaml
)
end
end
Expand Down
2 changes: 1 addition & 1 deletion templates/default/unicorn.service.erb
Expand Up @@ -37,7 +37,7 @@ def different_gemfile?
end

def start_unicorn
run_and_ignore_exitcode_and_print_command "cd #{ROOT_PATH}/current && /usr/local/bin/bundle exec unicorn_rails --env <%= @rails_env %> --daemonize -c #{ROOT_PATH}/shared/config/unicorn.conf"
run_and_ignore_exitcode_and_print_command "cd #{ROOT_PATH}/current && /usr/local/bin/bundle exec unicorn_rails --env <%= @deploy_env %> --daemonize -c #{ROOT_PATH}/shared/config/unicorn.conf"
end

def stop_unicorn
Expand Down

0 comments on commit a3ee7d8

Please sign in to comment.