Skip to content

Commit

Permalink
First pass at adding haproxy awareness to the recipe.
Browse files Browse the repository at this point in the history
  • Loading branch information
wyhaines committed May 7, 2010
1 parent 1489b4e commit 2f680b2
Show file tree
Hide file tree
Showing 2 changed files with 156 additions and 126 deletions.
280 changes: 155 additions & 125 deletions cookbooks/varnish/recipes/default.rb
Expand Up @@ -5,130 +5,160 @@

require 'etc'

# This needs to be in keywords: www-servers/varnish ~x86
# This makes sure that it is.

execute "unmask-varnish" do
command %Q{
echo "www-servers/varnish ~amd64 ~x86" >> /etc/portage/package.keywords/ec2
}
not_if "grep www-servers/varnish /etc/portage/package.keywords/ec2"
end

# Install varnish.
package "www-servers/varnish" do
action :install
end


#####
#
# These are generic tuning parameters for each instance size. You may want to
# tune them if they prove inadequate.
#
#####

CACHE_DIR = '/var/lib/varnish'
size = `curl -s http://instance-data.ec2.internal/latest/meta-data/instance-type`
case size
when /m1.small/ # 1.7G RAM, 1 ECU, 32-bit, 1 core
THREAD_POOLS=1
THREAD_POOL_MAX=1000
OVERFLOW_MAX=2000
CACHE="file,#{CACHE_DIR},1GB"
when /m1.large/ # 7.5G RAM, 4 ECU, 64-bit, 2 cores
THREAD_POOLS=2
THREAD_POOL_MAX=2000
OVERFLOW_MAX=4000
CACHE="file,#{CACHE_DIR},1GB"
when /m1.xlarge/ # 15G RAM, 8 ECU, 64-bit, 4 cores
THREAD_POOLS=4
THREAD_POOL_MAX=4000
OVERFLOW_MAX=8000
CACHE="file,#{CACHE_DIR},1GB"
when /c1.medium/ # 1.7G RAM, 5 ECU, 32-bit, 2 cores
THREAD_POOLS=2
THREAD_POOL_MAX=2000
OVERFLOW_MAX=4000
CACHE="file,#{CACHE_DIR},1GB"
when /c1.xlarge/ # 7G RAM, 20 ECU, 64-bit, 8 cores
THREAD_POOLS=8
THREAD_POOL_MAX=8000 # This might be too much.
OVERFLOW_MAX=16000
CACHE="file,#{CACHE_DIR},1GB"
when /m2.xlarge/ # 17.1G RAM, 6.5 ECU, 64-bit, 2 cores
THREAD_POOLS=2
THREAD_POOL_MAX=2000
OVERFLOW_MAX=4000
CACHE="file,#{CACHE_DIR},1GB"
when /m2.2xlarge/ # 34.2G RAM, 13 ECU, 64-bit, 4 cores
THREAD_POOLS=4
THREAD_POOL_MAX=4000
OVERFLOW_MAX=8000
CACHE="file,#{CACHE_DIR},1GB"
when /m2.4xlarge/ # 68.4G RAM, 26 ECU, 64-bit, 8 cores
THREAD_POOLS=8
THREAD_POOL_MAX=8000 # This might be too much.
OVERFLOW_MAX=16000
CACHE="file,#{CACHE_DIR},1GB"
else # This shouldn't happen, but do something rational if it does.
THREAD_POOLS=1
THREAD_POOL_MAX=2000
OVERFLOW_MAX=2000
CACHE="file,#{CACHE_DIR},1GB"
end


# Install the varnish monit file.
template '/etc/monit.d/varnishd.monitrc' do
owner node[:owner_name]
group node[:owner_name]
source 'varnishd.monitrc.erb'
variables({
:thread_pools => THREAD_POOLS,
:thread_pool_max => THREAD_POOL_MAX,
:overflow_max => OVERFLOW_MAX,
:cache => CACHE
})
end

# Install the app VCL file.
template '/etc/varnish/app.vcl' do
owner node[:owner_name]
group node[:owner_name]
source 'app.vcl.erb'
end

# Make sure the cache directory exists.
unless FileTest.exist? CACHE_DIR
user = Etc::getpwnam(node[:owner_name])
Dir.mkdir(CACHE_DIR)
File.chown(user.uid,user.gid,CACHE_DIR)
end


# Edit nginx config to change it off of port 80; we may or may not want the recipe to do this?
# The idea is that for the typical simple deployment, nginx will be listening in some config to port 80.
# We want to change that to port 81, so that the default varnish config can listen on port 80 and forward to 81.

execute "Edit the config files inline to change nginx from listening on port 80 to listening on port 81" do
command %Q{
bash -c 'for k in `grep -l "listen 80;" /etc/nginx/servers/*.conf`; do perl -p -i -e"s{listen 80;}{listen 81;}" $k; mv $k "/etc/nginx/servers/keep.`basename $k`"; done'
}
end

# Restart nginx

execute "Restart nginx" do
command %Q{
/etc/init.d/nginx restart
}
end

# Start/restart varnish
if ['solo','app_master'].include?(node[:instance_role])

# This needs to be in keywords: www-servers/varnish ~x86
# This makes sure that it is.

execute "unmask-varnish" do
command %Q{
echo "www-servers/varnish ~amd64 ~x86" >> /etc/portage/package.keywords/ec2
}
not_if "grep www-servers/varnish /etc/portage/package.keywords/ec2"
end

# Install varnish.
package "www-servers/varnish" do
action :install
end


#####
#
# These are generic tuning parameters for each instance size. You may want to
# tune them if they prove inadequate.
#
#####

CACHE_DIR = '/var/lib/varnish'
size = `curl -s http://instance-data.ec2.internal/latest/meta-data/instance-type`
case size
when /m1.small/ # 1.7G RAM, 1 ECU, 32-bit, 1 core
THREAD_POOLS=1
THREAD_POOL_MAX=1000
OVERFLOW_MAX=2000
CACHE="file,#{CACHE_DIR},1GB"
when /m1.large/ # 7.5G RAM, 4 ECU, 64-bit, 2 cores
THREAD_POOLS=2
THREAD_POOL_MAX=2000
OVERFLOW_MAX=4000
CACHE="file,#{CACHE_DIR},1GB"
when /m1.xlarge/ # 15G RAM, 8 ECU, 64-bit, 4 cores
THREAD_POOLS=4
THREAD_POOL_MAX=4000
OVERFLOW_MAX=8000
CACHE="file,#{CACHE_DIR},1GB"
when /c1.medium/ # 1.7G RAM, 5 ECU, 32-bit, 2 cores
THREAD_POOLS=2
THREAD_POOL_MAX=2000
OVERFLOW_MAX=4000
CACHE="file,#{CACHE_DIR},1GB"
when /c1.xlarge/ # 7G RAM, 20 ECU, 64-bit, 8 cores
THREAD_POOLS=8
THREAD_POOL_MAX=8000 # This might be too much.
OVERFLOW_MAX=16000
CACHE="file,#{CACHE_DIR},1GB"
when /m2.xlarge/ # 17.1G RAM, 6.5 ECU, 64-bit, 2 cores
THREAD_POOLS=2
THREAD_POOL_MAX=2000
OVERFLOW_MAX=4000
CACHE="file,#{CACHE_DIR},1GB"
when /m2.2xlarge/ # 34.2G RAM, 13 ECU, 64-bit, 4 cores
THREAD_POOLS=4
THREAD_POOL_MAX=4000
OVERFLOW_MAX=8000
CACHE="file,#{CACHE_DIR},1GB"
when /m2.4xlarge/ # 68.4G RAM, 26 ECU, 64-bit, 8 cores
THREAD_POOLS=8
THREAD_POOL_MAX=8000 # This might be too much.
OVERFLOW_MAX=16000
CACHE="file,#{CACHE_DIR},1GB"
else # This shouldn't happen, but do something rational if it does.
THREAD_POOLS=1
THREAD_POOL_MAX=2000
OVERFLOW_MAX=2000
CACHE="file,#{CACHE_DIR},1GB"
end

# Figure out if this environment is using haproxy. If haproxy exists on
# this environment, then we'll want to edit its config to point it at Varnish.
# If haproxy isn't in use, then Varnish will go on port 80. In either case,
# nginx should be on port 81.

HAS_HAPROXY = FileTest.exist?('/etc/haproxy.cfg')

# Install the varnish monit file.
template '/etc/monit.d/varnishd.monitrc' do
owner node[:owner_name]
group node[:owner_name]
source 'varnishd.monitrc.erb'
variables({
:thread_pools => THREAD_POOLS,
:thread_pool_max => THREAD_POOL_MAX,
:overflow_max => OVERFLOW_MAX,
:cache => CACHE,
:varnish_port => HAS_HAPROXY ? 82 : 80
})
end

# Install the app VCL file.
template '/etc/varnish/app.vcl' do
owner node[:owner_name]
group node[:owner_name]
source 'app.vcl.erb'
end

# Make sure the cache directory exists.
unless FileTest.exist? CACHE_DIR
user = Etc::getpwnam(node[:owner_name])
Dir.mkdir(CACHE_DIR)
File.chown(user.uid,user.gid,CACHE_DIR)
end


if HAS_HAPROXY
# Edit the haproxy config to point at Varnish instead of nginx.

execute "Edit the haproxy.cfg file to point it at varnish instead of nginx" do
command %Q{
perl -p -i -e's{(server\s+[\w\-]+\s+[\w\-\.]+):81}{$1:82}' /etc/haproxy.cfg
}
end

# Restart haproxy

execute "Restarting haproxy" do
command %Q{
/etc/init.d/haproxy restart
}
end
else
# Edit nginx config to change it off of port 80; we may or may not want the recipe to do this?
# The idea is that for the typical simple deployment, nginx will be listening in some config to port 80.
# We want to change that to port 81, so that the default varnish config can listen on port 80 and forward to 81.

execute "Edit the config files inline to change nginx from listening on port 80 to listening on port 81" do
command %Q{
bash -c 'for k in `grep -l "listen 80;" /etc/nginx/servers/*.conf`; do perl -p -i -e"s{listen 80;}{listen 81;}" $k; mv $k "/etc/nginx/servers/keep.`basename $k`"; done'
}
end

# Restart nginx

execute "Restart nginx" do
command %Q{
/etc/init.d/nginx restart
}
end
end


# Start/restart varnish

execute "Stop Varnish and bounce monit" do
command %Q{
sleep 20 ; pkill -9 monit && telinit q ; sleep 10 && monit
}
end

execute "Stop Varnish and bounce monit" do
command %Q{
monit quit
}
end
2 changes: 1 addition & 1 deletion cookbooks/varnish/templates/default/varnishd.monitrc.erb
@@ -1,4 +1,4 @@
check process varnish_80
with pidfile /var/run/varnish.80.pid
start program = "/usr/sbin/varnishd -a :80 -T 127.0.0.1:6082 -s <%= @cache %> -f /etc/varnish/app.vcl -P /var/run/varnish.80.pid -u nobody -g nobody -p obj_workspace=4096 -p sess_workspace=262144 -p listen_depth=2048 -p overflow_max=<%= @overflow_max %> -p ping_interval=2 -p log_hashstring=off -h classic,5000009 -p thread_pool_max=<%= @thread_pool_max %> -p lru_interval=60 -p esi_syntax=0x00000003 -p sess_timeout=10 -p thread_pools=<%= @thread_pools %> -p thread_pool_min=100 -p shm_workspace=32768 -p thread_pool_add_delay=1"
start program = "/usr/sbin/varnishd -a :<%= @varnish_port %> -T 127.0.0.1:6082 -s <%= @cache %> -f /etc/varnish/app.vcl -P /var/run/varnish.80.pid -u nobody -g nobody -p obj_workspace=4096 -p sess_workspace=262144 -p listen_depth=2048 -p overflow_max=<%= @overflow_max %> -p ping_interval=2 -p log_hashstring=off -h classic,5000009 -p thread_pool_max=<%= @thread_pool_max %> -p lru_interval=60 -p esi_syntax=0x00000003 -p sess_timeout=10 -p thread_pools=<%= @thread_pools %> -p thread_pool_min=100 -p shm_workspace=32768 -p thread_pool_add_delay=1"
stop program = "/usr/bin/pkill varnish"

0 comments on commit 2f680b2

Please sign in to comment.