Skip to content

Instantly share code, notes, and snippets.

@charliemcelfresh
Created August 12, 2013 17:19
Show Gist options
  • Save charliemcelfresh/6213005 to your computer and use it in GitHub Desktop.
Save charliemcelfresh/6213005 to your computer and use it in GitHub Desktop.
# 1. Use .erb templates to create custom .conf files based on each server’s config
# Config is held inside capistrano deploy files, like
/config/deploy/staging.rb
set :rails_env, "staging"
namespace :nginx do
# The /etc/init.d/nginx script points to /home/travel/apps/ShortTrips/shared/config/nginx.conf
# To deploy changes, run:
# cap nginx:configure
task :configure do
template_configure "nginx.conf” # makes all capistrano config params available to nginx.conf.erb
end
[:stop, :start, :status, :restart, :reload, :configtest].each do | command_sym |
# /sbin/service runs same command as /etc/init.d
task command_sym do
run "sudo /sbin/service nginx #{command_sym}"
end
end
end
# here's the template_configure method in lib/capistrano/utils.rb
# A class with utility methods for Capistrano.
class Capistrano::Configuration
# Offers a means of creating a config file e.g. resque.yml
# from an erb file so that the config file can be loaded
# with dynamic environment-specific data (such as a hostname) at runtime.
def template_configure(config_file, options = {})
# create the config directory and all requisite directories along the path
run "mkdir -p #{shared_path}/config"
#grab the erb template
configuration = ERB.new File.read("#{rails_root}/config/#{config_file}.erb")
# the binding method method (passed as an argument) is offered to every ruby object and offers up the variables
# available in the current scope to the erb instance. therefore, shared_path, config_file, etc
# will be available to the erb template. See http://www.stuartellis.eu/articles/erb/ for more info.
# The put operation writes the value of the rendered erb template into the config file.
put configuration.result(binding), "#{shared_path}/config/#{config_file}", options
# link the config file to the latest release.
run "ln -nfs #{shared_path}/config/#{config_file} #{latest_release}/config/#{config_file}; true"
end
end
# 2. run a remote rails task
namespace :rails_cache do
desc "Flushes rails cache for current env"
task :flush, :roles => [:app] do
run("cd #{current_path} && RAILS_ENV=#{rails_env} bundle exec rake rails_cache:flush")
end
end
# 3) on local box, get list of all commits since last deploy 2) pipe that list into rake task *on the server*, 3) email that list to whoever wants it, using mailer in the Rails app
desc "Show currently deployed revision on server."
task :email_revisions_to_dev, :roles => :app do
begin
require "base64"
output = []
current, latest = current_revision[0,7], real_revision[0,7] # “current” is last rev on server, “real” is rev deploying now
output.push "===== Prior Revision: #{current}"
output.push "=== Current Revision: #{latest}"
# Show difference between last two deploys.
# this is run on local dev box
diff = `git log #{current}..#{latest} --date=rfc`
output.push "=== Difference between #{current} revision and #{latest} revision:"
output = output + diff.split("\n")
encoded_output = Base64.encode64(output.join("\n"))
# this is run on the server
run "cd #{release_path} && export RAILS_ENV=#{rails_env} && bundle exec rake output=#{encoded_output} post_deploy_change_log:notify_team"
rescue
puts "------------------------ deploy:email_revisions_to_dev failed ----------------------------"
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment