Skip to content

Instantly share code, notes, and snippets.

@ivanvanderbyl
Created July 2, 2012 03:49
Show Gist options
  • Save ivanvanderbyl/3030936 to your computer and use it in GitHub Desktop.
Save ivanvanderbyl/3030936 to your computer and use it in GitHub Desktop.
Capsitrano deploy script for deploying to an 'ivanvanderbyl:application deployed' server.
production:
adapter: postgresql
encoding: unicode
database: my_application
pool: 5
host: 127.0.0.1
load 'deploy/assets'
set :bundle_roles, [:app] #{:except => {:no_release => true}} # e.g. [:app, :batch]
require 'bundler/capistrano'
set :application, "my_application_name"
set :repository, "git@github:username/my_application_name.git"
set :user, 'deploy'
set :use_sudo, false
set :scm, :git
set :branch, "master"
set :deploy_via, :remote_cache
set :git_shallow_clone, 1
# Deploy application to /home/deploy/my_application_name/current
set :deploy_to, "~/#{application}"
set :keep_releases, 3
role :web, "hostname_or_ip.com"
role :app, "hostname_or_ip.com"
role :db, "hostname_or_ip.com", :primary => true
set :application_env, 'production'
set :bundle_gemfile, "Gemfile"
set :bundle_dir, File.join(fetch(:shared_path), 'bundle')
set :bundle_flags, "--deployment --quiet"
set :bundle_without, [:development, :test]
set :bundle_cmd, "bundle" # e.g. "/opt/ruby/bin/bundle"
set :normalize_asset_timestamps, false
require 'capistrano-unicorn'
before 'deploy:finalize_update', 'deploy:symlink_sockets'
after 'deploy', 'deploy:cleanup'
before 'deploy', 'set_start_time'
after 'deploy', 'log_duration'
# This will symlink tmp/{sockets,pids}
# You may also want to symlink a production database.yml, unelss you are
# using postgres access, in which case you can remove the username and password
# keys from committed database.yml
namespace :deploy do
task :symlink_sockets, :roles => :web, :except => { :no_release => true } do
run <<-CMD
rm -rf #{latest_release}/tmp/sockets &&
ln -s #{shared_path}/sockets #{latest_release}/tmp/sockets &&
rm -rf #{latest_release}/tmp/pids &&
ln -s #{shared_path}/pids #{latest_release}/tmp/pids
CMD
end
end
task :set_start_time do
set :started_at, Time.now.to_f
end
# Output the deploy duration to console
task :log_duration do
logger.info "Deploy completed in %1.4f seconds" % (Time.now.to_f - started_at)
end
worker_processes 4# amount of unicorn workers to spin up
timeout 15 # restarts workers that hang for 15 seconds
# combine Ruby 2.0.0dev or REE with "preload_app true" for memory savings
# http://rubyenterpriseedition.com/faq.html#adapt_apps_for_cow
preload_app true
GC.respond_to?(:copy_on_write_friendly=) and
GC.copy_on_write_friendly = true
root = File.expand_path('../../..', __FILE__)
pid "#{root}/tmp/pids/unicorn.pid"
listen "#{root}/tmp/sockets/unicorn.socket", :backlog => 64
stderr_path "#{root}/log/unicorn.stderr.log"
stdout_path "#{root}/log/unicorn.stdout.log"
before_fork do |server, worker|
ActiveRecord::Base.connection.disconnect! if defined?(ActiveRecord::Base)
`cat #{server.pid}.oldbin`.scan(/^\d+$/).each {|ppid|
`kill -QUIT #{ppid}`
}
end
after_fork do |server, worker|
ActiveRecord::Base.establish_connection if defined?(ActiveRecord::Base)
end
@ivanvanderbyl
Copy link
Author

One you have this running, you may then want to add Unicorn to upstart so it comes online when you boot, and have upstart keep it alive.

I've written a babushka dep for this also:

babushka 'ivanvanderbyl:unicorn upstart'

It will ask you for a name, which is the name of the process you want to add to upstart. It will be prefixed with unicorn- e.g. Giving it testpilot will create an upstart process called unicorn-testpilot which can be controlled as follows:

sudo start unicorn-testpilot
sudo status unicorn-testpilot
sudo restart unicorn-testpilot
sudo stop unicorn-testpilot

Upstart will also respawn this process if it dies.

NOTE: You must save the unicorn configuration into config/unicorn/production.rb for this to work.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment