diff --git a/README.md b/README.md index f91c7aa4..5f8e07ad 100644 --- a/README.md +++ b/README.md @@ -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 diff --git a/attributes/default.rb b/attributes/default.rb index 06fa0601..ef5becdc 100644 --- a/attributes/default.rb +++ b/attributes/default.rb @@ -75,6 +75,7 @@ ## common default['defaults']['framework']['adapter'] = 'rails' +default['defaults']['framework']['deploy_env'] = 'production' ## rails @@ -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 diff --git a/libraries/drivers_appserver_unicorn.rb b/libraries/drivers_appserver_unicorn.rb index 42730732..7b71904e 100644 --- a/libraries/drivers_appserver_unicorn.rb +++ b/libraries/drivers_appserver_unicorn.rb @@ -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) diff --git a/libraries/drivers_db_base.rb b/libraries/drivers_db_base.rb index e1269aa1..980ae31b 100644 --- a/libraries/drivers_db_base.rb +++ b/libraries/drivers_db_base.rb @@ -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( diff --git a/libraries/drivers_framework_rails.rb b/libraries/drivers_framework_rails.rb index b0e5a14e..67e2745c 100644 --- a/libraries/drivers_framework_rails.rb +++ b/libraries/drivers_framework_rails.rb @@ -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) diff --git a/spec/fixtures/node.rb b/spec/fixtures/node.rb index 18cb7e40..543f847b 100644 --- a/spec/fixtures/node.rb +++ b/spec/fixtures/node.rb @@ -43,7 +43,7 @@ def node(override = {}) }, framework: { adapter: 'rails', - rails_env: 'staging', + deploy_env: 'staging', migrate: false }, worker: { diff --git a/spec/unit/libraries/drivers_framework_rails_spec.rb b/spec/unit/libraries/drivers_framework_rails_spec.rb index 1309ce35..89a61354 100644 --- a/spec/unit/libraries/drivers_framework_rails_spec.rb +++ b/spec/unit/libraries/drivers_framework_rails_spec.rb @@ -17,7 +17,7 @@ deploy_environment: { 'RAILS_ENV' => 'staging' }, migration_command: 'rake db:migrate', migrate: false, - rails_env: 'staging' + deploy_env: 'staging' ) end end diff --git a/spec/unit/recipes/configure_spec.rb b/spec/unit/recipes/configure_spec.rb index 6a55fd18..3ef3b625 100644 --- a/spec/unit/recipes/configure_spec.rb +++ b/spec/unit/recipes/configure_spec.rb @@ -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 @@ -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 @@ -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| @@ -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 @@ -344,6 +344,9 @@ password: 'password_936', host: 'dummy-project.936.us-west-2.rds.amazon.com', database: 'database_936' + }, + framework: { + deploy_env: 'staging' } } }) @@ -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 diff --git a/templates/default/unicorn.service.erb b/templates/default/unicorn.service.erb index 36e151b5..b9028d74 100644 --- a/templates/default/unicorn.service.erb +++ b/templates/default/unicorn.service.erb @@ -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