public
Created

Unicorn / rvm /capistrano

  • Download Gist
deploy.rb
Ruby
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50
set :application, "quibbler"
set :repository, "git@tuxbox.whf.app:quibbler.git"
set :scm, :git
set :branch, 'cap'
ssh_options[:forward_agent] = true
set :deploy_via, :remote_cache
#set :git_enable_submodules, 1
set :user, 'root'
#default_run_options[:pty] = true
 
server = 'wwwtestvm.whf.app'
role :web, server # Your HTTP server, Apache/etc
role :app, server # This may be the same as your `Web` server
role :db, server, :primary => true # This is where Rails migrations will run
 
# RVM Setup
$:.unshift(File.expand_path('./lib', ENV['rvm_path'])) # Add RVM's lib directory to the load path.
require "bundler/capistrano"
require "rvm/capistrano"
set :rvm_ruby_string, 'ruby-1.9.2-p290@quibbler'
 
# Unicorn tasks from: http://blog.teachstreet.com/building-teachstreet/how-i-learned-to-stop-worrying-and-love-the-unicorn/
set :unicorn_pid, "#{current_path}/tmp/pids/unicorn.pid"
namespace :unicorn do
desc "start unicorn"
task :start, :roles => :app, :except => { :no_release => true } do
run "cd #{current_path} && bundle exec unicorn_rails -c #{current_path}/config/unicorn-#{rails_env}.rb -E #{rails_env} -D"
end
desc "stop unicorn"
task :stop, :roles => :app, :except => { :no_release => true } do
run " kill `cat #{unicorn_pid}`"
end
desc "graceful stop unicorn"
task :graceful_stop, :roles => :app, :except => { :no_release => true } do
run " kill -s QUIT `cat #{unicorn_pid}`"
end
desc "reload unicorn"
task :reload, :roles => :app, :except => { :no_release => true } do
run " kill -s USR2 `cat #{unicorn_pid}`"
end
#after "deploy:restart", "unicorn:reload"
end
 
namespace :rvm do
task :trust_rvmrc do
run "rvm rvmrc trust #{release_path}"
end
after "deploy", "rvm:trust_rvmrc"
end
unicorn-production.rb
Ruby
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72
# http://blog.teachstreet.com/building-teachstreet/how-i-learned-to-stop-worrying-and-love-the-unicorn/
worker_processes 6
preload_app true
timeout 180
 
APP_ROOT = File.expand_path(File.join(File.dirname(__FILE__), '..'))
 
# Use RVM
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! APP_ROOT
rescue LoadError
raise "RVM ruby lib is currently unavailable."
end
end
 
# Use Bundler
ENV['BUNDLE_GEMFILE'] = File.join(APP_ROOT, 'Gemfile')
puts "BUNDLER_GEMFILE=#{ENV['BUNDLE_GEMFILE']}"
require 'bundler/setup'
# Setup directories
working_directory APP_ROOT
listen APP_ROOT + "/tmp/sockets/unicorn.sock", :backlog => 64
pid APP_ROOT + "/tmp/pids/unicorn.pid"
stderr_path APP_ROOT + "/log/unicorn.stderr.log"
stdout_path APP_ROOT + "/log/unicorn.stdout.log"
after_fork do |server, worker|
ActiveRecord::Base.establish_connection
##
# Unicorn master is started as root, which is fine, but let's
# drop the workers to www-data:www-data
 
# temorarily forego permission dropping
# begin
# uid, gid = Process.euid, Process.egid
# user, group = 'www-data', 'www-data'
# target_uid = Etc.getpwnam(user).uid
# target_gid = Etc.getgrnam(group).gid
# worker.tmp.chown(target_uid, target_gid)
# if uid != target_uid || gid != target_gid
# Process.initgroups(user, target_gid)
# Process::GID.change_privilege(target_gid)
# Process::UID.change_privilege(target_uid)
# end
# rescue => e
# if RAILS_ENV == 'development'
# STDERR.puts "couldn't change user, oh well"
# else
# raise e
# end
# end
end
before_fork do |server, worker|
ActiveRecord::Base.connection.disconnect!
old_pid = RAILS_ROOT + '/tmp/pids/unicorn.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

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.