Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add puma cookbook for 11.10 branch #307

Open
wants to merge 2 commits into
base: release-chef-11.10
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion deploy/attributes/deploy.rb
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@
when 'debian','ubuntu'
default[:opsworks][:deploy_user][:group] = 'www-data'
when 'centos','redhat','fedora','amazon'
default[:opsworks][:deploy_user][:group] = node['opsworks']['rails_stack']['name'] == 'nginx_unicorn' ? 'nginx' : 'apache'
default[:opsworks][:deploy_user][:group] = node['opsworks']['rails_stack']['name'].include?('nginx') ? 'nginx' : 'apache'
end

default[:opsworks][:rails][:ignore_bundler_groups] = ['test', 'development']
Expand Down
5 changes: 5 additions & 0 deletions deploy/attributes/rails_stack.rb
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,11 @@
normal[:opsworks][:rails_stack][:needs_reload] = true
normal[:opsworks][:rails_stack][:service] = 'unicorn'
normal[:opsworks][:rails_stack][:restart_command] = '../../shared/scripts/unicorn clean-restart'
when "nginx_puma"
normal[:opsworks][:rails_stack][:recipe] = "puma::rails"
normal[:opsworks][:rails_stack][:needs_reload] = true
normal[:opsworks][:rails_stack][:service] = 'puma'
normal[:opsworks][:rails_stack][:restart_command] = '../../shared/scripts/puma clean-restart'
else
raise "Unknown stack: #{node[:opsworks][:rails_stack][:name].inspect}"
end
Expand Down
6 changes: 6 additions & 0 deletions deploy/definitions/opsworks_deploy.rb
Original file line number Diff line number Diff line change
Expand Up @@ -177,6 +177,12 @@
deploy deploy
end

when 'nginx_puma'
puma_web_app do
application application
deploy deploy
end

else
raise "Unsupport Rails stack"
end
Expand Down
1 change: 1 addition & 0 deletions deploy/metadata.rb
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
depends "opsworks_agent_monit"
depends "passenger_apache2"
depends "unicorn"
depends "puma"
depends "opsworks_java"
depends "php"
depends "mysql"
Expand Down
25 changes: 25 additions & 0 deletions deploy/recipes/rails-undeploy.rb
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,31 @@
action :run
end

when 'nginx_puma'
include_recipe 'nginx::service'

link "/etc/nginx/sites-enabled/#{application}" do
action :delete
only_if do
::File.exists?("/etc/nginx/sites-enabled/#{application}")
end
notifies :restart, "service[nginx]"
end

file "/etc/nginx/sites-available/#{application}" do
action :delete
only_if do
::File.exists?("/etc/nginx/sites-available/#{application}")
end
end

execute 'stop puma and restart nginx' do
command "sleep #{deploy[:sleep_before_restart]} && \
/srv/www/#{application}/shared/scripts/puma stop"
notifies :restart, "service[nginx]"
action :run
end

else
raise 'Unsupported Rails stack'
end
Expand Down
2 changes: 2 additions & 0 deletions opsworks_ganglia/recipes/client.rb
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,8 @@
include_recipe 'opsworks_ganglia::monitor-apache'
when 'nginx_unicorn'
include_recipe 'opsworks_ganglia::monitor-nginx'
when 'nginx_puma'
include_recipe 'opsworks_ganglia::monitor-nginx'
end

end
Expand Down
2 changes: 1 addition & 1 deletion opsworks_ganglia/templates/default/conf.php.erb
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ $conf['optional_graphs'] = array();
<% if (node[:opsworks][:layers].has_key?("rails-app") && node[:opsworks][:rails_stack][:name] == 'apache_passenger' && !node[:opsworks][:layers]['rails-app']['instances'].empty? ) -%>
array_push($conf['optional_graphs'], 'passenger_memory_stats', 'passenger_status');
<% end -%>
<% if node[:opsworks][:layers].has_key?("rails-app") && node[:opsworks][:rails_stack][:name] == 'nginx_unicorn' && !node[:opsworks][:layers]['rails-app']['instances'].empty? -%>
<% if node[:opsworks][:layers].has_key?("rails-app") && node[:opsworks][:rails_stack][:name].include?('nginx') && !node[:opsworks][:layers]['rails-app']['instances'].empty? -%>
array_push($conf['optional_graphs'], 'nginx_status');
<% end -%>
<% if node[:opsworks][:layers].has_key?("web") && !node[:opsworks][:layers]['web']['instances'].empty? -%>
Expand Down
2 changes: 1 addition & 1 deletion opsworks_ganglia/templates/default/host_view_json.erb
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
if node[:opsworks][:rails_stack][:name] == 'apache_passenger'
reports << 'passenger_memory_stats_report'
reports << 'passenger_status_report'
elsif node[:opsworks][:rails_stack][:name] == 'nginx_unicorn'
elsif node[:opsworks][:rails_stack][:name].include?('nginx')
reports << 'nginx_status_report'
else
end
Expand Down
2 changes: 1 addition & 1 deletion passenger_apache2/metadata.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
license "Apache 2.0"
version "1.0.0"

%w{ packages gem_support apache2 nginx unicorn rails opsworks_initial_setup }.each do |cb|
%w{ packages gem_support apache2 nginx unicorn puma rails opsworks_initial_setup }.each do |cb|
depends cb
end

Expand Down
1 change: 1 addition & 0 deletions passenger_apache2/recipes/rails.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
unless node[:opsworks][:skip_uninstall_of_other_rails_stack]
include_recipe "nginx::uninstall"
include_recipe "unicorn::stop"
include_recipe "puma::stop"
end

include_recipe "apache2"
Expand Down
Empty file added puma/attributes/customize.rb
Empty file.
28 changes: 28 additions & 0 deletions puma/attributes/default.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
###
# Do not use this file to override the puma cookbook's default
# attributes. Instead, please use the customize.rb attributes file,
# which will keep your adjustments separate from the AWS OpsWorks
# codebase and make it easier to upgrade.
#
# However, you should not edit customize.rb directly. Instead, create
# "puma/attributes/customize.rb" in your cookbook repository and
# put the overrides in YOUR customize.rb file.
#
# Do NOT create an 'puma/attributes/default.rb' in your cookbooks. Doing so
# would completely override this file and might cause upgrade issues.
#
# See also: http://docs.aws.amazon.com/opsworks/latest/userguide/customizing.html
###

include_attribute 'rails::rails'

# Usually this should be a dynamic value but we are using threads in order to scale properly
default[:puma][:workers] = 4
default[:puma][:preload_app] = true
default[:puma][:version] = '2.11.3'
default[:puma][:threads_min] = node[:rails][:max_pool_size] ? node[:rails][:max_pool_size]/8 : 1
default[:puma][:threads_max] = node[:rails][:max_pool_size] ? node[:rails][:max_pool_size]/4 : 4

include_attribute "puma::customize"


17 changes: 17 additions & 0 deletions puma/definitions/puma_web_app.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
define :puma_web_app do
deploy = params[:deploy]
application = params[:application]

nginx_web_app deploy[:application] do
docroot deploy[:absolute_document_root]
server_name deploy[:domains].first
server_aliases deploy[:domains][1, deploy[:domains].size] unless deploy[:domains][1, deploy[:domains].size].empty?
rails_env deploy[:rails_env]
mounted_at deploy[:mounted_at]
ssl_certificate_ca deploy[:ssl_certificate_ca]
cookbook 'puma'
deploy deploy
template "nginx_puma_web_app.erb"
application deploy
end
end
9 changes: 9 additions & 0 deletions puma/metadata.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
name "puma"
description "Manage puma"
maintainer "AWS OpsWorks"
license "Apache 2.0"
version "1.0.0"

depends "apache2"
depends "nginx"
depends "rails"
5 changes: 5 additions & 0 deletions puma/recipes/default.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
ruby_block "ensure only our puma version is installed by deinstalling any other version" do
block do
ensure_only_gem_version('puma', node[:puma][:version])
end
end
48 changes: 48 additions & 0 deletions puma/recipes/rails.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
unless node[:opsworks][:skip_uninstall_of_other_rails_stack]
include_recipe "apache2::uninstall"
end

include_recipe 'nginx'
include_recipe 'puma'

# setup Unicorn service per app
node[:deploy].each do |application, deploy|
if deploy[:application_type] != 'rails'
Chef::Log.debug("Skipping puma::rails application #{application} as it is not an Rails app")
next
end

opsworks_deploy_user do
deploy_data deploy
end

opsworks_deploy_dir do
user deploy[:user]
group deploy[:group]
path deploy[:deploy_to]
end

template "#{deploy[:deploy_to]}/shared/scripts/puma" do
mode '0755'
owner deploy[:user]
group deploy[:group]
source 'puma.service.erb'
variables(:deploy => deploy, :application => application)
end

service "puma_#{application}" do
start_command "#{deploy[:deploy_to]}/shared/scripts/puma start"
stop_command "#{deploy[:deploy_to]}/shared/scripts/puma stop"
restart_command "#{deploy[:deploy_to]}/shared/scripts/puma restart"
status_command "#{deploy[:deploy_to]}/shared/scripts/puma status"
action :nothing
end

template "#{deploy[:deploy_to]}/shared/config/puma.conf" do
mode '0644'
owner deploy[:user]
group deploy[:group]
source 'puma.conf.erb'
variables(:deploy => deploy, :application => application)
end
end
14 changes: 14 additions & 0 deletions puma/recipes/stop.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
# stop Puma service per app
node[:deploy].each do |application, deploy|
if deploy[:application_type] != 'rails'
Chef::Log.debug("Skipping puma::rails application #{application} as it is not an Rails app")
next
end

execute "stop puma" do
command "#{deploy[:deploy_to]}/shared/scripts/puma stop"
only_if do
File.exists?("#{deploy[:deploy_to]}/shared/scripts/puma")
end
end
end
10 changes: 10 additions & 0 deletions puma/specs/default_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
require 'minitest/spec'

describe_recipe 'puma::default' do
include MiniTest::Chef::Resources
include MiniTest::Chef::Assertions

it 'installs puma' do
assert system("#{node[:dependencies][:gem_binary]} list | grep puma | grep '#{node[:puma][:version]}'")
end
end
7 changes: 7 additions & 0 deletions puma/specs/rails_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
require 'minitest/spec'

describe_recipe 'puma::rails' do
include MiniTest::Chef::Resources
include MiniTest::Chef::Assertions

end
7 changes: 7 additions & 0 deletions puma/specs/stop_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
require 'minitest/spec'

describe_recipe 'puma::stop' do
include MiniTest::Chef::Resources
include MiniTest::Chef::Assertions

end
80 changes: 80 additions & 0 deletions puma/templates/default/nginx_puma_web_app.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
upstream puma_<%= @application[:domains].first %> {
server unix:<%= @application[:deploy_to]%>/shared/sockets/puma.sock fail_timeout=0;
}

server {
listen 80;
server_name <%= @application[:domains].join(" ") %> <%= node[:hostname] %>;
access_log <%= node[:nginx][:log_dir] %>/<%= @application[:domains].first %>.access.log;
root <%= @application[:absolute_document_root] %>;

keepalive_timeout 5;

location /nginx_status {
stub_status on;
access_log off;
allow 127.0.0.1;
deny all;
}

<% if @application[:nginx] && @application[:nginx][:client_max_body_size] %>
client_max_body_size <%= @application[:nginx][:client_max_body_size] %>;
<% end %>

location / {
try_files $uri @puma;
}

location @puma {
proxy_set_header X-Forwarded-Proto $scheme;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header Host $http_host;
proxy_redirect off;
proxy_pass http://puma_<%= @application[:domains].first %>;
}

error_page 500 502 503 504 /500.html;
location = /500.html {
root <%= @application[:absolute_document_root] %>;
}

}

<% if @application[:ssl_support] %>
server {
listen 443;
server_name <%= @application[:domains].join(" ") %> <%= node[:hostname] %>;
access_log <%= node[:nginx][:log_dir] %>/<%= @application[:domains].first %>-ssl.access.log;
root <%= @application[:absolute_document_root] %>;

keepalive_timeout 5;

ssl on;
ssl_certificate /etc/nginx/ssl/<%= @application[:domains].first %>.crt;
ssl_certificate_key /etc/nginx/ssl/<%= @application[:domains].first %>.key;
<% if @application[:ssl_certificate_ca] -%>
ssl_client_certificate /etc/nginx/ssl/<%= @application[:domains].first %>.ca;
<% end -%>

<% if @application[:nginx] && @application[:nginx][:client_max_body_size] %>
client_max_body_size <%= @application[:nginx][:client_max_body_size] %>;
<% end %>

location / {
try_files $uri @puma;
}

location @puma {
proxy_set_header X-Forwarded-Proto $scheme;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header Host $http_host;
proxy_redirect off;
proxy_pass http://puma_<%= @application[:domains].first %>;
}

error_page 500 502 503 504 /500.html;
location = /500.html {
root <%= @application[:absolute_document_root] %>;
}
}
<% end %>
Loading