Created
April 23, 2012 09:59
-
-
Save mpouleijn/2470000 to your computer and use it in GitHub Desktop.
Unicorn + Nginx
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
$:.unshift(File.expand_path('./lib', ENV['rvm_path'])) | |
require "rvm/capistrano" | |
set :rvm_ruby_string, 'ree-1.8.7@bed4you' | |
set :rvm_type, :user | |
require 'bundler/capistrano' | |
require 'capistrano/ext/multistage' | |
require 'capistrano_colors' | |
set :application, 'bed4you' | |
set :rake, 'bundle exec rake' | |
set(:backup_dir) { "#{deploy_to}/backups" } | |
set :deploy_via, :remote_cache | |
before "db:reset", "db:backup" | |
namespace :db do | |
task :backup, :roles => :db, :only => {:primary => true} do | |
filename = "#{backup_dir}/#{application}.dump.#{Time.now.to_f}.sql.bz2" | |
text = capture "cat #{release_path}/config/database.yml" | |
yaml = YAML::load(text) | |
on_rollback { run "rm #{filename}" } | |
run "mysqldump -u #{yaml['production']['username']} -p #{yaml['production']['database']} | bzip2 -c > #{filename}" do |ch, stream, out| | |
ch.send_data "#{yaml['production']['password']}\n" if out =~ /^Enter password:/ | |
end | |
end | |
task :reset, :roles => :db do | |
run "cd #{release_path} && bundle exec rake db:drop" | |
run "cd #{release_path} && bundle exec rake db:create" | |
run "cd #{release_path} && bundle exec rake db:migrate" | |
run "cd #{release_path} && bundle exec rake db:seed" | |
end | |
end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
set :scm, :git | |
set :repository, "[email protected]:mpouleijn/beddebkoning.git" | |
set :branch, "master" | |
set :migrate_target, :current | |
set :ssh_options, { :forward_agent => true } | |
set :rails_env, "production" | |
set :deploy_to, "/home/deploy/bed4you/production" | |
set :user, "deploy" | |
set :group, "deploy" | |
set :use_sudo, false | |
role :web, "bed4you.nl" | |
role :app, "bed4you.nl" | |
role :db, "bed4you.nl", :primary => true | |
set(:current_revision) { capture("cd #{current_path}; git rev-parse --short HEAD").strip } | |
set(:latest_revision) { capture("cd #{current_path}; git rev-parse --short HEAD").strip } | |
set(:previous_revision) { capture("cd #{current_path}; git rev-parse --short HEAD@{1}").strip } | |
default_environment["RAILS_ENV"] = 'production' | |
default_run_options[:pty] = true | |
after "deploy:finalize_update", "deploy:symlink_system" | |
after "deploy:finalize_update", "db:backup" | |
namespace :deploy do | |
desc "Zero-downtime restart of Unicorn" | |
task :restart, :except => { :no_release => true } do | |
run "kill -s USR2 `cat /tmp/unicorn.bed4you.pid`" | |
end | |
desc "Start unicorn" | |
task :start, :except => { :no_release => true } do | |
run "cd #{current_path} ; RAILS_ENV=production bundle exec unicorn_rails -c config/unicorn.rb -D" | |
end | |
desc "Stop unicorn" | |
task :stop, :except => { :no_release => true } do | |
run "kill -s QUIT `cat /tmp/unicorn.bed4you.pid`" | |
end | |
desc "Symlink the needed files" | |
task :symlink_system, :roles => :app do | |
run "ln -nfs #{deploy_to}/shared/config/database.yml #{release_path}/config/database.yml" | |
run "ln -nfs #{deploy_to}/public/photos #{release_path}/public/photos" | |
end | |
end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
if ENV['MY_RUBY_HOME'] && ENV['MY_RUBY_HOME'].include?('rvm') | |
begin | |
rvm_path = File.dirname(File.dirname(ENV['MY_RUBY_HOME'])) | |
rvm_lib_path = File.join(rvm_path, 'lib') | |
$LOAD_PATH.unshift rvm_lib_path | |
require 'rvm' | |
RVM.use_from_path! File.dirname(File.dirname(__FILE__)) | |
rescue LoadError | |
raise "The RVM Ruby library is not available." | |
end | |
end | |
env = ENV["RAILS_ENV"] || "production" | |
# See http://unicorn.bogomips.org/Unicorn/Configurator.html for complete | |
# documentation. | |
worker_processes 2 | |
listen "/tmp/unicorn.bed4you.sock" | |
pid "/tmp/unicorn.bed4you.pid" | |
# Preload our app for more speed | |
preload_app true | |
# nuke workers after 30 seconds instead of 60 seconds (the default) | |
timeout 30 | |
working_directory "/home/deploy/bed4you/production/current" | |
# feel free to point this anywhere accessible on the filesystem | |
user 'deploy', 'deploy' | |
shared_path = "/home/deploy/bed4you/production/shared" | |
stderr_path "#{shared_path}/log/unicorn.stderr.log" | |
stdout_path "#{shared_path}/log/unicorn.stdout.log" | |
before_fork do |server, worker| | |
# the following is highly recomended for Rails + "preload_app true" | |
# as there's no need for the master process to hold a connection | |
if defined?(ActiveRecord::Base) | |
ActiveRecord::Base.connection.disconnect! | |
end | |
# Before forking, kill the master process that belongs to the .oldbin PID. | |
# This enables 0 downtime deploys. | |
old_pid = "/tmp/unicorn.bed4you.pid.oldbin" | |
if File.exists?(old_pid) && server.pid != old_pid | |
begin | |
Process.kill("QUIT", File.read(old_pid).to_i) | |
rescue Errno::ENOENT, Errno::ESRCH | |
# someone else did our job for us | |
end | |
end | |
end | |
after_fork do |server, worker| | |
# the following is *required* for Rails + "preload_app true", | |
if defined?(ActiveRecord::Base) | |
ActiveRecord::Base.establish_connection | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment