Skip to content

Instantly share code, notes, and snippets.

@mm53bar
Created October 7, 2011 21:05
Show Gist options
  • Star 24 You must be signed in to star a gist
  • Fork 7 You must be signed in to fork a gist
  • Save mm53bar/1271350 to your computer and use it in GitHub Desktop.
Save mm53bar/1271350 to your computer and use it in GitHub Desktop.
My capistrano deployment
require File.join(File.dirname(__FILE__), 'deploy/nginx')
require File.join(File.dirname(__FILE__), 'deploy/log')
default_run_options[:pty] = true
set :ssh_options, { :forward_agent => true }
set :application, "appname"
set :repository, "git@giturl"
set :scm, :git
set :user, "deploy"
set :deploy_to, "/var/applications/appname"
set :rails_env, "production"
set :branch, "master"
server "appname.com", :web, :app, :db, :primary => true
namespace :deploy do
desc "Deploy the MFer"
task :default do
update
restart
cleanup
end
desc "Setup a GitHub-style deployment."
task :setup, :except => { :no_release => true } do
run "git clone #{repository} #{current_path}"
update
run "mkdir -p #{current_path}/tmp/sockets && mkdir -p #{current_path}/tmp/pids"
db.setup
migrate
db.seed
start
cleanup
nginx.start
end
desc "Update the deployed code."
task :update_code, :except => { :no_release => true } do
#run "cd #{current_path}; git fetch origin; git reset --hard #{branch}"
run "cd #{current_path}; git reset --hard; git pull origin master"
end
desc "Deploy and run migrations"
task :migrations, :except => { :no_release => true } do
update
migrate
restart
cleanup
end
desc "Run pending migrations on already deployed code"
task :migrate, :only => {:primary => true}, :except => { :no_release => true } do
run "cd #{current_path}; RAILS_ENV=#{rails_env} bundle exec rake db:migrate"
end
task :symlink, :roles => :app, :except => { :no_release => true } do
#nothing yet - only exists to wipe out symlink task from super
end
namespace :db do
desc "Seed the database on already deployed code"
task :seed, :only => {:primary => true}, :except => { :no_release => true } do
run "cd #{current_path}; RAILS_ENV=#{rails_env} bundle exec rake db:seed"
end
desc "Setup application schema"
task :setup do
run "cd #{current_path}; RAILS_ENV=#{rails_env} bundle exec rake db:create"
end
desc "Wipe tables then rerun all migrations and seed database"
task :remigrate, :only => {:primary => true}, :except => { :no_release => true } do
run "cd #{current_path}; RAILS_ENV=#{rails_env} bundle exec rake db:remigrate"
end
end
namespace :rollback do
desc "Rollback"
task :default, :except => { :no_release => true } do
code
end
desc "Rollback a single commit."
task :code, :except => { :no_release => true } do
set :branch, "HEAD^"
default
end
end
task :cleanup, :roles => :app, :except => { :no_release => true } do
# nothing yet
end
# override default tasks to make capistrano happy
desc "Start unicorn"
task :start, :roles => :app do
run "cd #{current_path} && bundle exec unicorn -c #{current_path}/config/unicorn.rb -E #{rails_env} -D"
end
desc "Kick unicorn"
task :restart, :roles => :app do
run "kill -USR2 `cat #{current_path}/tmp/pids/unicorn.pid`"
end
desc "Kill a unicorn"
task :stop, :roles => :app do
run "kill -QUIT `cat #{current_path}/tmp/pids/unicorn.pid`"
end
end
namespace :restore do
task :db, :roles => :db do
# from http://www.thegeekstuff.com/2009/01/how-to-backup-and-restore-postgres-database-using-pg_dump-and-psql/
#
# psql -U {user-name} -d {desintation_db}-f {dumpfilename.sql}
end
end
namespace :backup do
desc "Backup the database"
task :db, :roles => :db do
run "mkdir -p #{current_path}/backups"
run "cd #{current_path}; pg_dump -U #{user} #{application}_production -f backups/#{Time.now.utc.strftime('%Y%m%d%H%M%S')}.sql"
end
desc "Backup the database and download the script"
task :download, :roles => :app do
db
timestamp = Time.now.utc.strftime('%Y%m%d%H%M%S')
run "mkdir -p backups"
run "cd #{current_path}; tar -cvzpf #{timestamp}_backup.tar.gz backups"
get "#{current_path}/#{timestamp}_backup.tar.gz", "#{timestamp}_backup.tar.gz"
end
end
namespace :assets do
task :precompile, :roles => :web do
run "cd #{current_path}; RAILS_ENV=#{rails_env} bundle exec rake assets:precompile"
end
task :clean, :roles => :web do
run "cd #{current_path}; RAILS_ENV=#{rails_env} bundle exec rake assets:clean"
end
end
namespace :bundler do
task :bundle_new_release, :roles => :app do
run "cd #{current_path} && bundle install --deployment --quiet --without development test cucumber"
end
end
after 'deploy:update_code', 'bundler:bundle_new_release'
after 'deploy:update_code', 'assets:precompile'
Capistrano::Configuration.instance.load do
namespace :log do
desc "Tail all application log files"
task :tail, :roles => :app do
run "tail -f #{current_path}/log/*.log" do |channel, stream, data|
puts "#{channel[:host]}: #{data}"
break if stream == :err
end
end
end
end
Capistrano::Configuration.instance.load do
namespace :nginx do
desc "Restart nginx"
task :restart, :roles => :app , :except => { :no_release => true } do
sudo "/etc/init.d/nginx restart"
end
desc "Stop nginx"
task :stop, :roles => :app , :except => { :no_release => true } do
sudo "/etc/init.d/nginx stop"
end
desc "Start nginx"
task :start, :roles => :app , :except => { :no_release => true } do
sudo "/etc/init.d/nginx start"
end
desc "Show nginx status"
task :status, :roles => :app , :except => { :no_release => true } do
sudo "/etc/init.d/nginx status"
end
end
end
@danman01
Copy link

Hi, would you mind posting your unicorn.rb as well? I'm trying to wrap my head around unicorn continuous deploys. The migrate task you have: is it possible that some users will see the old unicorn process and the old version of code, and some will see the new process and new updated code? Then, assuming your before_fork block kills the old unicorn in your unicorn.rb, things will finally sync up?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment