Skip to content

Instantly share code, notes, and snippets.

@niaeashes
Last active November 1, 2015 23:04
Show Gist options
  • Save niaeashes/10270d1839df9b6dad9e to your computer and use it in GitHub Desktop.
Save niaeashes/10270d1839df9b6dad9e to your computer and use it in GitHub Desktop.
# config valid only for current version of Capistrano
lock '3.4.0'
set :application, 'application'
set :repo_url, 'repository'
# Default branch is :master
# ask :branch, `git rev-parse --abbrev-ref HEAD`.chomp
# Default deploy_to directory is /var/www/my_app_name
set :deploy_to, '/var/application'
# Default value for :scm is :git
# set :scm, :git
# Default value for :format is :pretty
# set :format, :pretty
# Default value for :log_level is :debug
# set :log_level, :debug
# Default value for :pty is false
set :pty, true
# Default value for :linked_files is []
set :linked_files, fetch(:linked_files, []).push('config/database.yml', 'config/secrets.yml', 'config/unicorn.rb')
# Default value for linked_dirs is []
set :linked_dirs, fetch(:linked_dirs, []).push('log', 'tmp/pids', 'tmp/cache', 'tmp/sockets', 'vendor/bundle', 'public/system')
# Default value for keep_releases is 5
# set :keep_releases, 5
namespace :deploy do
task :restart do
invoke "unicorn:restart"
end
after 'deploy:publishing', 'deploy:restart'
task :push_config do
next unless any? :linked_files
on release_roles :all do
fetch(:linked_files).each do |file|
unless test "[ -f #{shared_path.join file} ]"
upload! file, "#{shared_path.join file}"
end
end
end
end
before 'deploy:check:linked_files', 'deploy:push_config'
end
namespace :unicorn do
task :stop do
on roles(:web) do
execute :sudo, :systemctl, :stop, :unicorn
end
end
task :restart do
on roles(:web) do
invoke 'unicorn:stop'
invoke 'unicorn:start'
end
end
task :start do
on roles(:web) do
execute :sudo, :systemctl, :start, :unicorn
end
end
end
# Copyed from http://benjaminknofe.com/blog/2014/03/08/zero-downtime-deployment-with-unicorn-and-capistrano/
# Note: http://stackoverflow.com/questions/15111902/rails-mongoid-unicorn-config-for-heroku
root = "/var/application/current"
pid "#{root}/tmp/pids/unicorn.pid"
stderr_path "#{root}/log/unicorn_error.log"
stdout_path "#{root}/log/unicorn.log"
listen "#{root}/tmp/unicorn.socket", :backlog => 2048
preload_app true
working_directory root
worker_processes 4
timeout 30
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
defined?(ActiveRecord::Base) and ActiveRecord::Base.connection.disconnect!
##
# When sent a USR2, Unicorn will suffix its pidfile with .oldbin and
# immediately start loading up a new version of itself (loaded with a new
# version of our app). When this new Unicorn is completely loaded
# it will begin spawning workers. The first worker spawned will check to
# see if an .oldbin pidfile exists. If so, this means we've just booted up
# a new Unicorn and need to tell the old one that it can now die. To do so
# we send it a QUIT.
#
# Using this method we get 0 downtime deploys.
old_pid = "#{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
after_fork do |server, worker|
##
# Unicorn master loads the app then forks off workers - because of the way
# Unix forking works, we need to make sure we aren't using any of the parent's
# sockets, e.g. db connection
defined?(ActiveRecord::Base) and ActiveRecord::Base.establish_connection
# Redis and Memcached would go here but their connections are established
# on demand, so the master never opens a socket
##
# Unicorn master is started as root, which is fine, but let's
# drop the workers to deployer:deployer
begin
uid, gid = Process.euid, Process.egid
user, group = 'nia', 'nia'
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
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment