Skip to content

Instantly share code, notes, and snippets.

@dallasmarlow
Created June 22, 2012 22:11
Show Gist options
  • Save dallasmarlow/2975488 to your computer and use it in GitHub Desktop.
Save dallasmarlow/2975488 to your computer and use it in GitHub Desktop.
unicorn cap deploy
#
# a simple cap recipe for deploying rack apps (such as sinatra or rails) using unicorn
# via scp using a dedicated deploy user with sudo access to run commands as the daemon user
#
# app options
set :application, 'app'
set :repository, '.' # deploy from the current working directory
# deploy options
set :scm, :none # use flat files, no git/svn/etc
set :deploy_via, :copy # copy via scp to remote nodes
set :deploy_to, '/var/www/app' # this directory will need to exist
# or it's parent will need to be writeable by the deploy user
# user options
set :user, 'deploy' # ssh as the deploy user
set :use_sudo, false # do not use sudo unless specified
# ssh options
ssh_options[:keys] = ['~/.ssh/deploy.pem'] # use a dedicated private key for the deploy user
ssh_options[:forward_agent] = true
# misc options
default_run_options[:pty] = true
set :normalize_asset_timestamps, false # for apps without the following dirs: public/images, public/javascripts, public/stylesheets
# unicorn options
set :unicorn, '/opt/ruby-1.9.2/bin/unicorn' # path to the unicorn binary if it's not in the deploy user's path
set :unicorn_pid, '/var/run/unicorn.pid' # path to the configured location of unicorn's pid set in unicorn.rb
set :unicorn_conf, File.join(deploy_to, 'current', 'config', 'unicorn.rb') # path to unicorn.rb
# nodes
role :app, 'node.domain.tld'
# tasks
namespace :deploy do
# reload by default instead of restart
task :default do
update
reload
end
# start unicorn as the daemon user
task :start do
sudo "#{unicorn} -E production -c #{unicorn_conf} -D", :as => :daemon
end
# kill unicorn as the daemon user if pid exists
task :stop do
pid = capture "if [ -f #{unicorn_pid} ]; then cat #{unicorn_pid}; fi"
if pid
sudo "kill #{pid}", :as => :daemon
end
end
# reload unicorn as the daemon user if pid exists
task :reload do
pid = capture "if [ -f #{unicorn_pid} ]; then cat #{unicorn_pid}; fi"
if pid
sudo "kill -USR2 #{pid}", :as => :daemon
end
end
task :restart do
stop
start
end
end
# clean up old deploys after a new deploy
after 'deploy:update', 'deploy:cleanup'
# unicorn settings
preload_app true
timeout 60
worker_processes 10
# unicorn paths
pid '/var/run/unicorn.pid'
listen '/var/run/unicorn.sock', :backlog => 2048
# app paths
working_directory '/var/www/app/current'
stderr_path 'log/unicorn.log'
# reload logic
before_fork do |server, worker|
demoted_server_pid = "#{server.config[:pid]}.oldbin"
unless demoted_server_pid == server.pid
begin
signal = case
when (worker.nr + 1) >= server.worker_processes
:QUIT
else
:TTOU
end
Process.kill signal, File.read(demoted_server_pid).to_i
rescue Errno::ENOENT, Errno::ESRCH
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment