Created
August 31, 2010 20:39
-
-
Save SFEley/559710 to your computer and use it in GitHub Desktop.
Vlad config file with Hoptoad and Unicorn
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
set :application, "_____" | |
set :repository, "[email protected]:_____/#{application}.git" | |
begin | |
# Parsing the annoying multi-line output of 'git branch'... | |
set :branch, `git branch --no-color`.match(/\* (.+)/)[1] | |
rescue Exception => e | |
set :branch, '(none)' | |
end | |
set :scm, :git | |
# Or: `accurev`, `bzr`, `cvs`, `darcs`, `git`, `mercurial`, `perforce`, `subversion` or `none` | |
set :user, branch | |
set :rails_env, branch | |
set :domain, "#{user}@#{branch}.aarweb.org" | |
set :deploy_to, "/web/#{user}/#{application}" | |
set :revision, "origin/#{branch}" | |
set :unicorn, "/opt/local/bin/unicorn_rails" | |
namespace :vlad do | |
desc "Pushes to Github" | |
task :push do | |
puts "Sending to #{revision}" | |
`git push origin #{branch}` | |
end | |
desc "Sets the current Git branch" | |
remote_task :git_revision do | |
set :current_sha, run("cd #{File.join(scm_path, 'repo')}; git rev-parse #{revision}").strip | |
end | |
desc "Sets the current Git user" | |
task :git_user do | |
set :current_user, `git config --get user.name`.strip | |
end | |
desc "Pushes deployment status to Hoptoad" | |
task :notify_hoptoad => [:git_user, :git_revision] do | |
notify_command = "rake hoptoad:deploy TO=#{rails_env} REVISION=#{current_sha} REPO=#{repository} USER='#{current_user}'" | |
puts "Notifying Hoptoad of Deploy (#{notify_command})" | |
`#{notify_command}` | |
puts "Hoptoad Notification Complete." | |
end | |
desc "Starts a new Unicorn server" | |
remote_task :start_unicorn do | |
pidfile = "#{shared_path}/pids/unicorn.pid" | |
puts "Calling for a Unicorn..." | |
result = run("#{unicorn} -E #{rails_env} -D -c #{current_path}/config/unicorn.rb").strip | |
if result.empty? | |
sleep(1) # Shouldn't need time to catch up, but just in case... | |
if (pid = run("(test -f #{pidfile} && cat #{pidfile}) || echo 0").to_i) > 0 | |
puts "You have a Unicorn! His name is #{pid}. Take good care of him." | |
else | |
puts "No Unicorn came to your call. Something might be wrong." | |
end | |
else | |
"Something went wrong! The remote machine says: #{result}" | |
end | |
end | |
desc "Tells the Unicorn server to reload itself" | |
remote_task :reload_unicorn do | |
pidfile = "#{shared_path}/pids/unicorn.pid" | |
if (pid = run("(test -f #{pidfile} && cat #{pidfile}) || echo 0").to_i) > 0 | |
puts "Sending graceful reload to Unicorn #{pid}" | |
result = run("kill -HUP #{pid}").strip | |
if result.empty? | |
puts "Your Unicorn is refreshed." | |
else | |
puts "The remote machine says: #{result}" | |
puts "We'll try restarting..." | |
Rake::Task['vlad:start_unicorn'].invoke | |
end | |
else | |
puts "No Unicorn found." | |
Rake::Task['vlad:start_unicorn'].invoke | |
end | |
end | |
desc "Forces a terminate and restart of the Unicorn server" | |
remote_task :kick_unicorn do | |
pidfile = "#{shared_path}/pids/unicorn.pid" | |
if (pid = run("(test -f #{pidfile} && cat #{pidfile}) || echo 0").to_i) > 0 | |
puts "Telling Unicorn #{pid} to go away..." | |
run("kill -USR2 #{pid}") | |
sleep(3) | |
newpid = run("(test -f #{pidfile} && cat #{pidfile}) || echo 0").to_i | |
if newpid != pid and newpid > 0 | |
puts "Unicorn #{newpid} is on the scene! Putting #{pid} out to pasture." | |
run("kill -WINCH #{pid}") | |
puts "We'll wait a minute for you to ensure that the new one works. If it doesn't,\nCTRL-C this task and do 'rake vlad:rescue_unicorn' to bring the old one back." | |
sleep(60) | |
run("kill -QUIT #{pid}") | |
puts "Unicorn #{pid} is no more! Long live Unicorn #{newpid}." | |
else | |
puts "Something went wrong! The new PID is still #{newpid}. You'd better look into this." | |
end | |
else | |
puts "No Unicorn found." | |
Rake::Task['vlad:start_unicorn'].invoke | |
end | |
end | |
desc "Recovers from a failed Unicorn restart" | |
remote_task :rescue_unicorn do | |
newfile = "#{shared_path}/pids/unicorn.pid" | |
oldfile = newfile + '.oldbin' | |
newpid = run("(test -f #{newfile} && cat #{newfile}) || echo 0").to_i | |
oldpid = run("(test -f #{oldfile} && cat #{oldfile}) || echo 0").to_i | |
if oldpid == 0 then | |
puts "You have no old Unicorn! You'd better do this manually." and return | |
end | |
if newpid == 0 then | |
puts "There's no new Unicorn to quit! You'd better do this manually." and return | |
end | |
if newpid == oldpid then | |
puts "The new and old Unicorns are the same! Something went haywire. You'd better do this manually." and return | |
end | |
puts "Reviving the old trusty Unicorn #{oldpid}..." | |
run("kill -HUP #{oldpid}") | |
sleep(5) | |
puts "Unicorn #{oldpid} is back in the field!\nSending the usurping #{newpid} to ignominious doom." | |
run("kill -QUIT #{newpid}") | |
end | |
desc "Update gem bundles" | |
remote_task :bundle_install do | |
run("cd #{current_path} && /opt/local/bin/bundle install ~/bundle") | |
end | |
desc "Update server, cycle Unicorn, notify Hoptoad" | |
task :deploy => [:push, :update, :bundle_install, :reload_unicorn, :notify_hoptoad] | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment