Skip to content

Instantly share code, notes, and snippets.

@natew
Last active December 16, 2015 17:39
Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save natew/5471784 to your computer and use it in GitHub Desktop.
Save natew/5471784 to your computer and use it in GitHub Desktop.
deploy.rb rolling restart with unicorn deploy.rb in your rails app /config/deploy.rb on your server: /etc/unicorn/site.conf /etc/init.d/unicorn /etc/nginx/sites-available/sitename.conf
require "bundler/capistrano"
load 'deploy/assets'
# Pretty colors
require 'capistrano_colors'
# rbenv and ssh forwarding
set :default_environment, { 'PATH' => "$HOME/.rbenv/shims:$HOME/.rbenv/bin:$PATH" }
set :default_run_options, { :pty => true, :shell => '/bin/zsh' }
set :ssh_options, { :forward_agent => true }
set :scm_verbose, true
set :scm, :git
set :branch, 'live'
set :keep_releases, 2
set :use_sudo, false
set :deploy_via, :remote_cache
set :user, ENV['OBTVSE_USER']
set :application, ENV['OBTVSE_APP']
set :domain, ENV['OBTVSE_DOMAIN']
set :repository, "ssh://#{user}@#{domain}/var/git/#{application}.git"
set :deploy_to, "/var/www/#{application}.com/web"
set :rails_env, 'production'
set :unicorn_config, "#{current_path}/config/unicorn/production.rb"
set :unicorn_binary, "bundle exec unicorn_rails -c #{unicorn_config} -E #{rails_env} -D"
set :unicorn_pid, "#{current_path}/tmp/pids/unicorn.pid"
role :web, domain
role :app, domain
role :db, domain, :primary => true # This is where Rails migrations will run
after 'deploy:symlink', 'deploy:symlink_database'
after 'deploy:update', 'deploy:cleanup'
namespace :deploy do
task :restart, :roles => :app, :except => { :no_release => true } do
run "if [ -f #{unicorn_pid} ]; then kill -s USR2 `cat #{unicorn_pid}`; fi"
end
task :force_restart, :roles => :app, :except => { :no_release => true } do
stop
start
end
task :stop, :roles => :app, :except => { :no_release => true } do
run "if [ -f #{unicorn_pid} ]; then kill -s QUIT `cat #{unicorn_pid}`; fi"
end
task :start, :roles => :app do
run "cd #{current_path} && #{unicorn_binary}"
end
task :symlink_database, :roles => :app do
run "cd #{current_path}/config && ln -s #{current_path}/../../shared/config/database.yml ."
end
end
RAILS_ENV=production
RAILS_ROOT=/var/www/site.com
APP_USER=username
upstream unicorn {
server unix:/var/www/example.com/web/current/tmp/sockets/example.sock;
}
server {
listen 80 default;
server_name example.com www.example.com;
root /var/www/example.com/web/current/public;
location ~ ^/(assets|fonts)/ {
root /var/www/example.com/web/current/public;
expires max;
break;
}
location / {
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_redirect off;
proxy_pass http://unicorn;
}
}
#!/bin/bash
### BEGIN INIT INFO
# Provides: unicorn
# Required-Start: $local_fs $remote_fs $network $syslog
# Required-Stop: $local_fs $remote_fs $network $syslog
# Default-Start: 2 3 4 5
# Default-Stop: 0 1 6
# Short-Description: Start the unicorns at boot
# Description: Enable Rails applications with unicorn at boot time.
### END INIT INFO
# This is /etc/init.d/unicorn (without .sh)
# init.d script for single or multiple unicorn installations. Expects at least one .conf
# file in /etc/unicorn
#
# Modified again by http://github.com/natew
# Modified by http://github.com/ganta
# based on modified version by http://github.com/zewelor
# which is based on:
# http://gist.github.com/2623205 by http://github.com/zewelor
# http://gist.github.com/504875 by http://github.com/jaygooby
# http://gist.github.com/308216 by http://github.com/mguterl
#
## A sample /etc/unicorn/my_app.conf
##
## RAILS_ENV=production
## RAILS_ROOT=/var/apps/www/my_app/current
## UNICORN="/opt/rbenv/shims/unicorn_rails"
## APP_USER="www-data"
#
# This configures a unicorn master for your app at /opt/my_app/current running in
# production mode. It will read config/unicorn.rb for further set up.
#
# You should ensure different ports or sockets are set in each config/unicorn.rb if
# you are running more than one master concurrently.
#
# If you call this script without any config parameters, it will attempt to run the
# init command for all your unicorn configurations listed in /etc/unicorn/*.conf
#
# /etc/init.d/unicorn start # starts all unicorns
#
# If you specify a particular config, it will only operate on that one
#
# /etc/init.d/unicorn start my_app
RBENV_FILE_TO_SOURCE=/etc/profile.d/rbenv.sh
sig () {
test -s "$PID" && kill -$1 `cat "$PID"`
}
oldsig () {
test -s "$OLD_PID" && kill -$1 `cat "$OLD_PID"`
}
cmd () {
case $1 in
start)
sig 0 && echo >&2 "Already running" && return 0
su - $APP_USER -c "$CMD"
echo "Starting"
;;
stop)
sig QUIT && echo "Stopping" && return 0
echo >&2 "Not running"
;;
force-stop)
sig TERM && echo "Forcing a stop" && return 0
echo >&2 "Not running"
;;
restart|reload)
sig USR2 && sleep 5 && oldsig QUIT && echo "Killing old master" `cat $OLD_PID` && return 0
echo >&2 "Couldn't reload, starting '$CMD' instead"
eval $CMD
;;
upgrade)
sig USR2 && echo Upgraded && return 0
echo >&2 "Couldn't upgrade, starting '$CMD' instead"
eval $CMD
;;
rotate)
sig USR1 && echo rotated logs OK && return 0
echo >&2 "Couldn't rotate logs" && return 1
;;
*)
echo >&2 "Usage: $0 <start|stop|restart|upgrade|rotate|force-stop>"
exit 1
;;
esac
}
setup () {
CONFIG=$1
export APP_NAME=`basename $1 .conf`
echo "Launching ${APP_NAME}"
if [ -z $APP_USER ]; then
if [ `getent passwd | awk -F: '{ print $1 }' | grep "^${APP_NAME}$" | wc -l` -eq 1 ]; then
export APP_USER=$APP_NAME
else
echo "The is no user defined in ${CONFIG}, and the is no user in the system called ${APP_NAME}"
return 1
fi
fi
if [ -z $RAILS_ROOT ]; then
if [ -d "/home/${APP_USER}/www/current" ]; then
export RAILS_ROOT="/home/${APP_USER}/www/current"
else
echo "Rails root is not defined in ${CONFIG}"
return 1
fi
fi
if [ -z $RAILS_ENV ]; then
export RAILS_ENV="production"
fi
# If unicorn binary was not defined in config
if [ -z $UNICORN ]; then
UNICORN="unicorn_rails"
fi
cd $RAILS_ROOT || return 1
export PID=$RAILS_ROOT/tmp/pids/unicorn.pid
export OLD_PID="$PID.oldbin"
CMD="cd ${RAILS_ROOT} && /home/nwienert/.rbenv/shims/bundle exec unicorn_rails -E ${RAILS_ENV} c ${RAILS_ROOT}/config/unicorn.rb -D"
return
}
# either run the start/stop/reload/etc command for every config under /etc/unicorn
# or just do it for a specific one
# $1 contains the start/stop/etc command
# $2 if it exists, should be the specific config we want to act on
source $RBENV_FILE_TO_SOURCE
if [ $2 ]; then
. /etc/unicorn/$2.conf
setup "/etc/unicorn/$2.conf"
if [ $? -eq 1 ]; then
exit 1
fi
cmd $1
else
for CONFIG in /etc/unicorn/*.conf; do
# clean variables from prev configs
unset APP_USER
unset RAILS_ROOT
unset RAILS_ENV
unset UNICORN
# import the variables
. $CONFIG
setup $CONFIG
if [ $? -eq 1 ]; then
continue
fi
# run the start/stop/etc command
cmd $1
done
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment