Skip to content

Instantly share code, notes, and snippets.

@vierarb
Created January 3, 2014 20:32
Show Gist options
  • Save vierarb/8246042 to your computer and use it in GitHub Desktop.
Save vierarb/8246042 to your computer and use it in GitHub Desktop.
Capistrano 3
require 'capistrano/setup'
require 'capistrano/deploy'
require 'capistrano/rvm'
require 'capistrano/bundler'
# Loads custom tasks from `lib/capistrano/tasks' if you have any defined.
Dir.glob('lib/capistrano/tasks/*.cap').each { |r| import r }
set :application, "#{fetch(:stage)}.EXAMPLE.com"
set :deploy_to, "/home/USER/apps/#{fetch(:application)}"
set :rvm_path, '~/.rvm'
set :rvm_default_path, '~/.rvm'
set :rvm_ruby_version, '2.1.0@EXAMPLE'
set :rvm_type, :user
server 'coachonrails.com', user: 'USER', roles: %w{web app db assets}
set :repo_url, '[email protected]:USER/EXAMPLE.git'
set :copy_exclude, ['.git', '.gitignore']
set :scm, :git
set :use_sudo, false
set :linked_files, %w{config/database.yml config/secrets.yml}
set :linked_dirs, %w{bin}
set :keep_releases, 10
set :normalize_asset_timestamps, false
set :pty, true
namespace :deploy do
def unicorn_config
"#{fetch(:app_path)}/config/unicorn.rb"
end
def unicorn_pid
"#{fetch(:app_path)}/tmp/pids/unicorn.pid"
end
def unicorn_old_pid
"#{unicorn_pid}.oldbin"
end
def unicorn_get_pid(pid_file = unicorn_pid)
"`cat #{pid_file}`"
end
def unicorn_get_old_pid
unicorn_get_pid(unicorn_old_pid)
end
def unicorn_is_running?
remote_process_exists? unicorn_pid
end
def unicorn_old_is_running?
remote_process_exists?(unicorn_old_pid)
end
def remote_process_exists?(pid_file)
"[ -e #{pid_file} ] && kill -0 `cat #{pid_file}` > /dev/null 2>&1"
end
def unicorn_send_signal(signal, pid = unicorn_get_pid)
"kill -s #{signal} #{pid}"
end
def unicorn_start
if test "[ -e '#{unicorn_pid}' ]"
if test "kill -0 `cat #{unicorn_pid}` > /dev/null 2>&1"
execute :echo, 'Unicorn is already running'
execute :exit, 0
end
execute :rm, unicorn_pid
end
within fetch(:app_path) do
with rails_env: fetch(:rails_env) do
execute :echo, 'Starting Unicorn'
execute :bundle, "exec unicorn_rails -c #{unicorn_config} -D -E #{fetch(:rails_env)}"
end
end
end
def unicorn_kill(signal)
if test unicorn_is_running?
execute :echo, 'Stopping Unicorn...'
execute unicorn_send_signal(signal)
else
execute :echo, 'Unicorn is not running.'
end
end
def unicorn_duplicate
unicorn_is_running? ? unicorn_send_signal('USR2') : unicorn_start
end
desc 'Start Unicorn master process'
task :start do
on roles(:app) do
unicorn_start
end
end
desc 'Stop Unicorn'
task :stop do
on roles(:app) do
unicorn_kill('QUIT')
end
end
desc 'Restart Unicorn'
task :restart do
on roles(:app) do
unicorn_duplicate
execute :sleep, 2
if unicorn_old_is_running?
unicorn_send_signal('QUIT', unicorn_get_old_pid)
end
end
end
desc 'Immediately shutdown Unicorn'
task :shutdown do
on roles(:app) do
unicorn_kill('TERM')
end
end
task :migrate do
on primary(:db) do
within release_path do
with rails_env: fetch(:rails_env) do
execute :bundle, 'exec rake db:migrate'
end
end
end
end
namespace :assets do
task :precompile do
on roles(:web) do
within release_path do
with rails_env: fetch(:rails_env) do
execute :bundle, 'exec rake assets:precompile'
end
end
end
end
end
before 'assets:precompile', 'bower:install'
after :updated, 'deploy:assets:precompile'
after :updated, 'deploy:migrate'
after :finishing, 'deploy:cleanup'
end
namespace :bower do
task :install do
on roles(:app) do
within release_path do
with rails_env: fetch(:rails_env) do
execute :bower, 'install --quiet'
end
end
end
end
task :prune do
on roles(:app) do
within release_path do
with rails_env: fetch(:rails_env) do
execute :bower, 'prune'
end
end
end
end
after :install, 'bower:prune'
end
set :rails_env, :staging
set :stage, :staging
set :branch, :staging
set :application, "#{fetch(:stage)}.EXAMPLE.com"
set :deploy_to, "/home/USER/apps/#{fetch(:application)}"
set :app_path, "#{fetch(:deploy_to)}/current"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment