Skip to content

Instantly share code, notes, and snippets.

@scottraio
Forked from fuse/deploy.rb
Created March 10, 2012 08:15
Show Gist options
  • Save scottraio/2010805 to your computer and use it in GitHub Desktop.
Save scottraio/2010805 to your computer and use it in GitHub Desktop.
Capistrano configuration
require "bundler/capistrano"
# allowing shell interactions
default_run_options[:pty] = true
# multistaging
set :stages, %w(staging production)
set :default_stage, "staging"
require 'capistrano/ext/multistage'
set :application, "fake"
set :user, "synbioz"
set :repository, "ssh://user@server/git/#{application}.git"
set :ssh_options, { :forward_agent => true }
set :deploy_to, "/var/www/#{application}"
set :scm, :git
set :deploy_via, :remote_cache
# number of releases we want to keep
set :keep_releases, 3
set :use_sudo, false
# default rails_env, should be overrided in config/deploy/#{environnement}.rb
set :rails_env, "staging"
# unicorn informations
set :unicorn_config, "#{current_path}/config/unicorn.rb"
set :unicorn_pid, "#{shared_path}/pids/unicorn.pid"
set :unicorn_bin, "#{current_path}/bin/unicorn"
# useful for rbenv
set :default_environment, {
'PATH' => "/home/synbioz/.rbenv/shims:/home/synbioz/.rbenv/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin"
}
namespace :maintenance do
task :start do
run "ln -nfs #{shared_path}/system/_maintenance.html #{shared_path}/system/maintenance.html"
end
task :stop do
run "rm -f #{shared_path}/system/maintenance.html"
end
end
namespace :deploy do
task :start, :roles => :app, :except => { :no_release => true } do
run <<-CMD
cd #{current_path} && #{unicorn_bin} -c #{unicorn_config} -E #{rails_env} -D
CMD
end
task :force_stop, :roles => :app, :except => { :no_release => true } do
run <<-CMD
if [ -e #{unicorn_pid} ]; then
kill -9 $(cat #{unicorn_pid});
fi
CMD
end
task :stop, :roles => :app, :except => { :no_release => true } do
run <<-CMD
if [ -e #{unicorn_pid} ]; then
kill $(cat #{unicorn_pid});
fi
CMD
end
task :graceful_stop, :roles => :app, :except => { :no_release => true } do
run <<-CMD
if [ -e #{unicorn_pid} ]; then
kill -s QUIT $(cat #{unicorn_pid});
fi
CMD
end
task :reload, :roles => :app, :except => { :no_release => true } do
run <<-CMD
if [ -e #{unicorn_pid} ]; then
kill -s USR2 $(cat #{unicorn_pid});
fi
CMD
end
task :restart, :roles => :app, :except => { :no_release => true } do
run <<-CMD
if [ -e #{unicorn_pid} ]; then
kill -9 $(cat #{unicorn_pid});
sleep 5;
cd #{current_path} && #{unicorn_bin} -c #{unicorn_config} -E #{rails_env} -D;
fi
CMD
end
end
desc "Create shared folders."
after 'deploy:setup', :roles => :app do
# for unicorn
run "mkdir -p #{shared_path}/sockets"
run "mkdir -p #{shared_path}/config"
# only for sqlite
run "mkdir -p #{shared_path}/db"
end
desc "Link db.yml."
after 'deploy:update_code', :roles => :app do
run "ln -nfs #{shared_path}/config/database.yml #{release_path}/config/database.yml"
end
desc "Seed datas when cold deploying."
after 'deploy:migrate', :roles => :app, :only => { :no_release => true } do
run "cd #{release_path} && bin/rake db:seed RAILS_ENV=#{rails_env}"
end
desc "Link maintenance, rbenv and precompile assets."
after 'deploy:symlink', :roles => [:web, :app] do
run "ln -nfs #{shared_path}/config/.rbenv-version #{release_path}/config/.rbenv-version"
run "cp #{release_path}/public/_maintenance.html #{shared_path}/system/"
run "cd #{release_path}; RAILS_ENV=#{rails_env} bin/rake assets:precompile"
end
desc "remove unused releases."
after "deploy", "deploy:cleanup"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment