Skip to content

Instantly share code, notes, and snippets.

@kennethkalmer
Created October 4, 2012 10:16
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save kennethkalmer/3832729 to your computer and use it in GitHub Desktop.
Save kennethkalmer/3832729 to your computer and use it in GitHub Desktop.
Capistrano config for near-zero downtime deployments
#
# Based heavily on the deployment recipe dicussed in the article at
# http://ariejan.net/2011/09/14/lighting-fast-zero-downtime-deployments-with-git-capistrano-nginx-and-unicorn
# but tweaked to fit our setup...
#
# NO WARRANTY, IMPLIED OR OTHERWISE
#
# Multistage setup
set :stages, %w(production staging)
set :default_stage, "staging"
require 'capistrano/ext/multistage'
# RVM setup
set :rvm_ruby_string, 'ruby-1.9.3-p194'
set :rvm_type, :system
require "rvm/capistrano"
require 'bundler/capistrano'
require 'capistrano/campfire'
load 'deploy/assets'
# Branch to deploy
set :branch, ENV['BRANCH'] || 'origin/master'
set :scm, :git
set :repository, "git@github.com:super/awesome.git"
set :branch, fetch(:branch)
set :migrate_target, :current
set :ssh_options, { :forward_agent => true }
set :deploy_to, "/apps/super/awesome"
set :normalize_asset_timestamps, false
set :user, "deploy"
set :group, "deploy"
set :use_sudo, false
set(:latest_release) { fetch(:current_path) }
set(:release_path) { fetch(:current_path) }
set(:current_release) { fetch(:current_path) }
set(:current_revision) { capture("cd #{current_path}; git rev-parse --short HEAD").strip }
set(:latest_revision) { capture("cd #{current_path}; git rev-parse --short HEAD").strip }
set(:previous_revision) { capture("cd #{current_path}; git rev-parse --short HEAD@{1}").strip }
#default_run_options[:shell] = 'bash'
default_run_options[:pty] = true
# More application configs
set :config_files, %w{ database.yml unicorn.rb }
# Callbacks
before "deploy:symlink", "deploy:check_pending_migrations"
after "deploy:update_code", "deploy:shared_config_files"
before 'deploy:update_code', 'sesame:authorize'
before 'deploy:update_code', 'campfire:started'
before 'airbrake:deploy', 'campfire:finished'
after 'airbrake:deploy', 'sesame:revoke'
# Change capistrano behaviour here (ie, symlink these into the shared path)
set :shared_children, %w( log tmp )
# Campfire options
set :campfire_options, :account => 'SUPER', :room => 'AWESOME', :token => 'xxx', :ssl => true
namespace :deploy do
desc "Deploy your application"
task :default do
update
restart
end
desc "Setup your git-based deployment app"
task :setup, :except => { :no_release => true } do
dirs = [deploy_to, shared_path]
dirs += shared_children.map { |d| File.join(shared_path, d) }
run "#{try_sudo} mkdir -p #{dirs.join(' ')} && #{try_sudo} chmod g+w #{dirs.join(' ')}"
run "git clone #{repository} #{current_path}"
end
task :cold do
update
migrate
end
task :update do
transaction do
update_code
end
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}"
finalize_update
end
desc "Update the database (overwritten to avoid symlink)"
task :migrations do
transaction do
update_code
end
migrate
restart
end
task :finalize_update, :except => { :no_release => true } do
run "chmod -R g+w #{latest_release}" if fetch(:group_writable, true)
# mkdir -p is making sure that the directories are there for some SCM's that don't
# save empty folders
run <<-CMD
rm -rf #{latest_release}/log #{latest_release}/public/system #{latest_release}/tmp &&
mkdir -p #{latest_release}/public &&
ln -s #{shared_path}/log #{latest_release}/log &&
ln -s #{shared_path}/system #{latest_release}/public/system &&
ln -s #{shared_path}/tmp #{latest_release}/tmp
CMD
if fetch(:normalize_asset_timestamps, true)
stamp = Time.now.utc.strftime("%Y%m%d%H%M.%S")
asset_paths = fetch(:public_children, %w(images stylesheets javascripts)).map { |p| "#{latest_release}/public/#{p}" }.join(" ")
run "find #{asset_paths} -exec touch -t #{stamp} {} ';'; true", :env => { "TZ" => "UTC" }
end
end
desc "Zero-downtime restart of Unicorn"
task :restart, :except => { :no_release => true } do
run "kill -s USR2 `cat #{shared_path}/tmp/unicorn.pid`"
end
desc "Start unicorn"
task :start, :except => { :no_release => true } do
run "cd #{current_path} ; bundle exec unicorn_rails -c config/unicorn.rb -D"
end
desc "Stop unicorn"
task :stop, :except => { :no_release => true } do
run "kill -s QUIT `cat #{shared_path}/tmp/unicorn.pid`"
end
namespace :rollback do
desc "Moves the repo back to the previous version of HEAD"
task :repo, :except => { :no_release => true } do
set :branch, "HEAD@{1}"
deploy.default
end
desc "Rewrite reflog so HEAD@{1} will continue to point to at the next previous release."
task :cleanup, :except => { :no_release => true } do
run "cd #{current_path}; git reflog delete --rewrite HEAD@{1}; git reflog delete --rewrite HEAD@{1}"
end
desc "Rolls back to the previously deployed version."
task :default do
rollback.repo
rollback.cleanup
end
end
desc "Create links to config files stored in shared config directory.
Specify which config files to link using the following:
set :config_files, 'database.yml'"
task :shared_config_files do
config_path = "#{current_release}/config"
shared_config_path = "#{shared_path}/config"
config_files.each do |file_path|
begin
run "rm -f #{config_path}/#{file_path} ; ln -nfs #{shared_config_path}/#{file_path} #{config_path}/#{file_path}"
rescue
puts "Problem linking to #{file_path}. Be sure file already exists in #{shared_config_path}."
end
end if config_files
end
desc "Bail if we have pending migrations"
task :check_pending_migrations, :once => true do
rake = fetch(:rake, "rake")
run "cd #{release_path} && #{rake} RAILS_ENV=#{rails_env} db:abort_if_pending_migrations"
end
namespace :assets do
task :precompile, :roles => :web, :except => { :no_release => true } do
#from = source.next_revision(current_revision)
from = previous_revision
if capture( "cd #{latest_release} && #{source.local.log(from)} vendor/assets/ app/assets/ lib/assets Gemfile.lock | wc -l" ).to_i > 0
logger.info "Compiling assets locally and performing rsync"
run_locally "rm -rf public/assets/*"
run_locally "rake assets:precompile"
servers = find_servers_for_task(current_task)
port_option = "" #port ? " -e 'ssh -p #{port}' " : ''
servers.each do |server|
run_locally("rsync --recursive --times --rsh=ssh --compress --human-readable #{port_option} --progress public/assets #{user}@#{server}:#{shared_path}")
end
run_locally "rm -rf public/assets/*"
else
logger.info "Skipping asset pre-compilation because there were no asset changes"
end
end
end
end
def run_rake(cmd)
run "cd #{current_path}; #{rake} #{cmd}"
end
namespace :campfire do
task :ping do
campfire_room.speak "[CAP] Ping from #{ENV['USER']}... :)"
end
task :started do
campfire_room.speak "[CAP] Deployment of #{branch} to #{rails_env} started by #{ENV['USER']}"
campfire_room.speak "[CAP] Deploying the following changes: https://github.com/super/awesome/compare/#{current_revision}...#{branch.split('/').pop}"
end
task :finished do
campfire_room.speak "[CAP] Deployment of #{branch} to #{rails_env} finished"
end
end
namespace :sesame do
task :setup do
require 'aws'
AWS.config( YAML.load_file( File.expand_path('~/.thingy') )['capistrano'] )
end
desc "Open port 22 on the security groups used by this application"
task :authorize do
setup
collection = AWS::EC2::SecurityGroupCollection.new
security_groups.each do |id|
begin
puts "Authoring SSH access to #{id}"
collection[ id ].authorize_ingress( :tcp, 22, "0.0.0.0/0" )
rescue AWS::EC2::Errors::InvalidPermission::Duplicate
end
end
end
desc "Close port 22 on the security groups used by this application"
task :revoke do
setup
collection = AWS::EC2::SecurityGroupCollection.new
security_groups.each do |id|
begin
puts "Revoking SSH access to #{id}"
collection[ id ].revoke_ingress( :tcp, 22, "0.0.0.0/0" )
rescue AWS::EC2::Errors::InvalidPermission::Duplicate
end
end
end
end
require 'airbrake/capistrano'
# Why is this here? See https://github.com/capistrano/capistrano/issues/168
Capistrano::Configuration::Namespaces::Namespace.class_eval do
def capture(*args)
parent.capture *args
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment