From e46d5fee3c9d469b3e41575d3c624b8be843da16 Mon Sep 17 00:00:00 2001 From: Igor Rzegocki Date: Fri, 2 Sep 2016 18:11:13 +0200 Subject: [PATCH] refactor(appserver): Moved ENV declaration from configuration templates to service launcher template "Thin" appserver, does not support ruby-based configuration files - whole configuration is based on thin.yml file. This means, we can't set the `ENV` variables directly from the configuration as it was possible in "Unicorn" or "Puma". To solve that problem (and made templates more consistent), all `ENV` associations were moved to shared, `appserver.service` file. --- libraries/drivers_appserver_base.rb | 2 +- libraries/drivers_appserver_puma.rb | 3 +-- libraries/drivers_appserver_thin.rb | 3 +-- libraries/drivers_appserver_unicorn.rb | 3 +-- spec/unit/recipes/configure_spec.rb | 15 +++++++++------ templates/default/appserver.service.erb | 4 ++++ templates/default/puma.rb.erb | 4 ---- templates/default/unicorn.conf.erb | 4 ---- 8 files changed, 17 insertions(+), 21 deletions(-) diff --git a/libraries/drivers_appserver_base.rb b/libraries/drivers_appserver_base.rb index 0d89ebca..a09baa42 100644 --- a/libraries/drivers_appserver_base.rb +++ b/libraries/drivers_appserver_base.rb @@ -59,7 +59,7 @@ def manual_action(context, action) def add_appserver_service_script(context) opts = { deploy_dir: deploy_dir(app), app_shortname: app['shortname'], deploy_env: globals[:environment], - name: adapter, command: appserver_command(context) } + name: adapter, command: appserver_command(context), environment: app['environment'] } context.template File.join(opts[:deploy_dir], File.join('shared', 'scripts', "#{opts[:name]}.service")) do owner node['deployer']['user'] diff --git a/libraries/drivers_appserver_puma.rb b/libraries/drivers_appserver_puma.rb index d87735a6..b4d7e1af 100644 --- a/libraries/drivers_appserver_puma.rb +++ b/libraries/drivers_appserver_puma.rb @@ -7,8 +7,7 @@ class Puma < Drivers::Appserver::Base output filter: [:log_requests, :preload_app, :thread_max, :thread_min, :timeout, :worker_processes] def add_appserver_config(context) - opts = { environment: app['environment'], deploy_dir: deploy_dir(app), out: out, - deploy_env: globals[:environment] } + opts = { deploy_dir: deploy_dir(app), out: out, deploy_env: globals[:environment] } context.template File.join(opts[:deploy_dir], File.join('shared', 'config', 'puma.rb')) do owner node['deployer']['user'] diff --git a/libraries/drivers_appserver_thin.rb b/libraries/drivers_appserver_thin.rb index bb05e2b9..89a1b4ef 100644 --- a/libraries/drivers_appserver_thin.rb +++ b/libraries/drivers_appserver_thin.rb @@ -7,8 +7,7 @@ class Thin < Drivers::Appserver::Base output filter: [:max_connections, :max_persistent_connections, :timeout, :worker_processes] def add_appserver_config(context) - opts = { environment: app['environment'], deploy_dir: deploy_dir(app), out: out, - deploy_env: globals[:environment] } + opts = { deploy_dir: deploy_dir(app), out: out, deploy_env: globals[:environment] } context.template File.join(opts[:deploy_dir], File.join('shared', 'config', 'thin.yml')) do owner node['deployer']['user'] diff --git a/libraries/drivers_appserver_unicorn.rb b/libraries/drivers_appserver_unicorn.rb index b9310955..05418b5e 100644 --- a/libraries/drivers_appserver_unicorn.rb +++ b/libraries/drivers_appserver_unicorn.rb @@ -10,7 +10,6 @@ class Unicorn < Drivers::Appserver::Base def add_appserver_config(context) deploy_to = deploy_dir(app) - environment = app['environment'] output = out context.template File.join(deploy_to, File.join('shared', 'config', 'unicorn.conf')) do @@ -18,7 +17,7 @@ def add_appserver_config(context) group www_group mode '0644' source 'unicorn.conf.erb' - variables environment: environment, deploy_dir: deploy_to, out: output + variables deploy_dir: deploy_to, out: output end end diff --git a/spec/unit/recipes/configure_spec.rb b/spec/unit/recipes/configure_spec.rb index 3350c8be..d66f390c 100644 --- a/spec/unit/recipes/configure_spec.rb +++ b/spec/unit/recipes/configure_spec.rb @@ -65,9 +65,6 @@ end it 'creates proper unicorn.conf file' do - expect(chef_run) - .to render_file("/srv/www/#{aws_opsworks_app['shortname']}/shared/config/unicorn.conf") - .with_content('ENV[\'ENV_VAR1\'] = "test"') expect(chef_run) .to render_file("/srv/www/#{aws_opsworks_app['shortname']}/shared/config/unicorn.conf") .with_content('worker_processes 4') @@ -77,6 +74,9 @@ end it 'creates proper unicorn.service file' do + expect(chef_run) + .to render_file("/srv/www/#{aws_opsworks_app['shortname']}/shared/scripts/unicorn.service") + .with_content('ENV[\'ENV_VAR1\'] = "test"') expect(chef_run) .to render_file("/srv/www/#{aws_opsworks_app['shortname']}/shared/scripts/unicorn.service") .with_content("APP_NAME=\"#{aws_opsworks_app['shortname']}\"") @@ -323,9 +323,6 @@ end it 'creates proper puma.rb file' do - expect(chef_run) - .to render_file("/srv/www/#{aws_opsworks_app['shortname']}/shared/config/puma.rb") - .with_content('ENV[\'ENV_VAR1\'] = "test"') expect(chef_run) .to render_file("/srv/www/#{aws_opsworks_app['shortname']}/shared/config/puma.rb") .with_content('workers 4') @@ -341,6 +338,9 @@ end it 'creates proper puma.service file' do + expect(chef_run) + .to render_file("/srv/www/#{aws_opsworks_app['shortname']}/shared/scripts/puma.service") + .with_content('ENV[\'ENV_VAR1\'] = "test"') expect(chef_run) .to render_file("/srv/www/#{aws_opsworks_app['shortname']}/shared/scripts/puma.service") .with_content("APP_NAME=\"#{aws_opsworks_app['shortname']}\"") @@ -460,6 +460,9 @@ end it 'creates proper thin.service file' do + expect(chef_run) + .to render_file("/srv/www/#{aws_opsworks_app['shortname']}/shared/scripts/thin.service") + .with_content('ENV[\'ENV_VAR1\'] = "test"') expect(chef_run) .to render_file("/srv/www/#{aws_opsworks_app['shortname']}/shared/scripts/thin.service") .with_content("APP_NAME=\"#{aws_opsworks_app['shortname']}\"") diff --git a/templates/default/appserver.service.erb b/templates/default/appserver.service.erb index fb85e769..e958e80f 100644 --- a/templates/default/appserver.service.erb +++ b/templates/default/appserver.service.erb @@ -3,6 +3,10 @@ require 'etc' require 'digest/md5' +<% @environment.each do |key, value| %> +ENV['<%= key.to_s %>'] = "<%= value.to_s %>" +<% end %> + ROOT_PATH="<%= @deploy_dir %>" APP_NAME="<%= @app_shortname %>" PID_PATH="<%= @deploy_dir %>/shared/pids/<%= @name %>.pid" diff --git a/templates/default/puma.rb.erb b/templates/default/puma.rb.erb index 1a202876..3c0c92a4 100644 --- a/templates/default/puma.rb.erb +++ b/templates/default/puma.rb.erb @@ -1,9 +1,5 @@ #!/usr/bin/env puma -<% @environment.each do |key, value| %> -ENV['<%= key.to_s %>'] = "<%= value.to_s %>" -<% end %> - # The directory to operate out of. directory "<%= @deploy_dir %>/current" diff --git a/templates/default/unicorn.conf.erb b/templates/default/unicorn.conf.erb index 2e65dc5f..8f5ec237 100644 --- a/templates/default/unicorn.conf.erb +++ b/templates/default/unicorn.conf.erb @@ -19,10 +19,6 @@ pid "<%= @deploy_dir %>/shared/pids/unicorn.pid" stderr_path "<%= @deploy_dir %>/shared/log/unicorn.stderr.log" stdout_path "<%= @deploy_dir %>/shared/log/unicorn.stdout.log" -<% @environment.each do |key, value| %> -ENV['<%= key.to_s %>'] = "<%= value.to_s %>" -<% end %> - preload_app <%= @out[:preload_app] %> GC.copy_on_write_friendly = true if GC.respond_to?(:copy_on_write_friendly=)