def origin_branches `git branch -a`. split("\n"). reject{|line| line =~ /HEAD/}. reject{|line| line =~ /master/}. select{|line| line =~ /remotes\/origin\//}. map{|line| line.sub(/.*remotes\/origin\//, '')} end def instance_deploy_tasks(prefix, instance, &block) namespace instance do namespace :deploy do origin_branches.each do |branch| desc "Deploy '#{prefix}_#{instance}' from branch '#{branch}'" task branch do set :revision, branch set :deploy_to, "/opt/www/#{prefix}_#{instance}" set :instance, instance set :prefix, prefix deploy do block.call(prefix, instance) end invoke :__stop queue 'sleep 5' invoke :__start invoke 'deploy:cleanup' end end end desc "Deploy '#{prefix}_#{instance}' from master" task :deploy do set :revision, 'master' set :deploy_to, "/opt/www/#{prefix}_#{instance}" set :instance, instance set :prefix, prefix deploy do block.call(prefix, instance) end invoke :__stop queue 'sleep 5' invoke :__start invoke 'deploy:cleanup' end end end def instance_setup_task(prefix, instance) namespace instance do desc "Setup '#{prefix}_#{instance}'" task :setup do set :revision, 'master' set :deploy_to, "/opt/www/#{prefix}_#{instance}" set :instance, instance set :prefix, prefix invoke :setup end end end def instance_manage_tasks(prefix, instance) namespace instance do desc "Start '#{prefix}_#{instance}'" task :start do set :deploy_to, "/opt/www/#{prefix}_#{instance}" set :instance, instance set :prefix, prefix invoke :__start end desc "Restart '#{prefix}_#{instance}'" task :restart do set :deploy_to, "/opt/www/#{prefix}_#{instance}" set :instance, instance set :prefix, prefix invoke :__stop queue 'sleep 5' invoke :__start end desc "Stop '#{prefix}_#{instance}'" task :stop do set :deploy_to, "/opt/www/#{prefix}_#{instance}" set :instance, instance set :prefix, prefix invoke :__stop end desc "Tail Log '#{prefix}_#{instance}'" task :log do set :deploy_to, "/opt/www/#{prefix}_#{instance}" set :instance, instance set :prefix, prefix invoke :__log end end end task :__git_clone do queue %{ echo "-----> Cloning the Git repository" #{echo_cmd %[git clone "#{repository!}" .]} } if revision != 'master' queue %{ echo "-----> Using revision #{revision}" && #{echo_cmd %[git checkout "#{revision}"]} } end end task :__start do queue %{ cd #{deploy_to}/current && bundle exec unicorn_rails -c /opt/unicorn-conf/#{prefix}_#{instance}.rb -E production -D && echo "-----> #{instance} Started" } end task :__stop do queue %{ kill -QUIT `cat /opt/unicorn-pids/#{prefix}_#{instance}.pid`; true echo "-----> #{instance} Stopped" } end task :__log do queue %{ cd #{deploy_to}/current && tail -400 log/production.log } end