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

Commit

Permalink
refactor(appserver): Moved ENV declaration from configuration templat…
Browse files Browse the repository at this point in the history
…es 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.
  • Loading branch information
Igor Rzegocki committed Sep 2, 2016
1 parent 9667939 commit e46d5fe
Show file tree
Hide file tree
Showing 8 changed files with 17 additions and 21 deletions.
2 changes: 1 addition & 1 deletion libraries/drivers_appserver_base.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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']
Expand Down
3 changes: 1 addition & 2 deletions libraries/drivers_appserver_puma.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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']
Expand Down
3 changes: 1 addition & 2 deletions libraries/drivers_appserver_thin.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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']
Expand Down
3 changes: 1 addition & 2 deletions libraries/drivers_appserver_unicorn.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,15 +10,14 @@ 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
owner node['deployer']['user']
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

Expand Down
15 changes: 9 additions & 6 deletions spec/unit/recipes/configure_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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')
Expand All @@ -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']}\"")
Expand Down Expand Up @@ -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')
Expand All @@ -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']}\"")
Expand Down Expand Up @@ -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']}\"")
Expand Down
4 changes: 4 additions & 0 deletions templates/default/appserver.service.erb
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
4 changes: 0 additions & 4 deletions templates/default/puma.rb.erb
Original file line number Diff line number Diff line change
@@ -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"

Expand Down
4 changes: 0 additions & 4 deletions templates/default/unicorn.conf.erb
Original file line number Diff line number Diff line change
Expand Up @@ -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=)

Expand Down

0 comments on commit e46d5fe

Please sign in to comment.