Skip to content

Instantly share code, notes, and snippets.

@twinge
Created February 22, 2011 16:30
Show Gist options
  • Save twinge/838919 to your computer and use it in GitHub Desktop.
Save twinge/838919 to your computer and use it in GitHub Desktop.
require 'hoptoad_notifier/capistrano'
# This defines a deployment "recipe" that you can feed to capistrano
# (http://manuals.rubyonrails.com/read/book/17). It allows you to automate
# (among other things) the deployment of your application.
# =============================================================================
# REQUIRED VARIABLES
# =============================================================================
# You must always specify the application and repository for every recipe. The
# repository must be the URL of the repository you want this recipe to
# correspond to. The deploy_to path must be the path on each machine that will
# form the root of the application path.
set :application, "mpd"
# set :repository, "http://svn.uscm.org/#{application}/trunk"
set :repository, "git://github.com/twinge/mpd_tool.git"
# set :checkout, 'co'
set :keep_releases, '3'
# =============================================================================
# ROLES
# =============================================================================
# You can define any number of roles, each of which contains any number of
# machines. Roles might include such things as :web, or :app, or :db, defining
# what the purpose of each machine is. You can also specify options that can
# be used to single out a specific subset of boxes in a particular role, like
# :primary => true.
set :target, ENV['target'] || ENV['TARGET'] || 'dev'
default_run_options[:pty] = true
set :scm, "git"
case target
when 'dev'
role :app, "hart-w025.uscm.org"
role :db, "hart-w025.uscm.org", :primary => true
set :environment, 'development'
set :deploy_to, "/var/www/html/integration/#{application}"
set :user, 'deploy'
set :password, 'alt60m'
set :rails_env, 'development'
when 'prod'
# role :app, "hart-w025.uscm.org"
role :app, "hart-w025.uscm.org"
set :environment, 'production'
set :deploy_to, "/var/www/html/production/#{application}"
set :user, 'deploy'
set :password, 'alt60m'
set :rails_env, 'production'
# set :deploy_via, :copy
# set :copy_cache, true
# set :copy_exclude, [".git","coverage"]
# set :scm_passphrase, "p@ssw0rd" #This is your custom users password
end
# define the restart task
desc "Restart the web server"
deploy.task :restart, :roles => :app do
run "touch #{deploy_to}/current/tmp/restart.txt"
end
# =============================================================================
# OPTIONAL VARIABLES
# =============================================================================
# set :scm, :darcs # defaults to :subversion
# set :svn, "/path/to/svn" # defaults to searching the PATH
# set :darcs, "/path/to/darcs" # defaults to searching the PATH
# set :cvs, "/path/to/cvs" # defaults to searching the PATH
# set :gateway, "gate.host.com" # default to no gateway
# =============================================================================
# SSH OPTIONS
# =============================================================================
# ssh_options[:keys] = %w(/path/to/my/key /path/to/another/key)
ssh_options[:forward_agent] = true
ssh_options[:port] = 40022
set :branch, "master"
set :deploy_via, :remote_cache
set :git_enable_submodules, 1
# =============================================================================
# TASKS
# =============================================================================
# Define tasks that run on all (or only some) of the machines. You can specify
# a role (or set of roles) that each task should be executed on. You can also
# narrow the set of servers to a subset of a role by specifying options, which
# must match the options given for the servers to select (like :primary => true)
desc "Add linked files after deploy and set permissions"
task :after_update_code, :roles => :app do
begin
sudo "ln -s #{shared_path}/coverage #{release_path}/public/coverage"
rescue; end;
begin
# run "ln -s #{shared_path}/system/payment.yml #{release_path}/config/payment.yml"
rescue; end;
run <<-CMD
ln -s #{shared_path}/bundle #{release_path}/vendor/bundle &&
export LD_LIBRARY_PATH=/opt/oracle/instantclient_10_2 &&
cd #{release_path} && bundle install --deployment --without development &&
ln -s #{shared_path}/config/database.yml #{release_path}/config/database.yml &&
ln -s #{shared_path}/config/facebook.yml #{release_path}/config/facebook.yml
CMD
# db mappings
run "ln -s #{shared_path}/config/mappings.yml #{release_path}/config/mappings.yml"
run "ln -s #{shared_path}/config/amazon_s3.yml #{release_path}/config/amazon_s3.yml"
# configs
run "ln -s #{shared_path}/config/initializers/email.rb #{release_path}/config/initializers/email.rb"
run "ln -s #{shared_path}/config/initializers/errbit.rb #{release_path}/config/initializers/errbit.rb"
run "ln -s #{shared_path}/config/initializers/ga.rb #{release_path}/config/initializers/ga.rb"
# run "ln -s #{shared_path}/config/initializers/exception_notification.rb #{release_path}/config/initializers/exception_notification.rb"
#plugins
run "ln -s #{shared_path}/plugins/common_engine #{release_path}/vendor/plugins/common_engine"
run "ln -s #{shared_path}/plugins/enforce_schema_rules #{release_path}/vendor/plugins/enforce_schema_rules"
run "ln -s #{shared_path}/plugins/acts_as_state_machine #{release_path}/vendor/plugins/acts_as_state_machine"
#create the tmp folders
run "mkdir -p -m 770 #{shared_path}/tmp/{cache,sessions,sockets,pids}"
run "rm -Rf #{release_path}/tmp"
run "ln -s #{shared_path}/tmp #{release_path}/tmp"
end
desc <<DESC
An imaginary backup task. (Execute the 'show_tasks' task to display all
available tasks.)
DESC
task :backup, :roles => :db, :only => { :primary => true } do
# the on_rollback handler is only executed if this task is executed within
# a transaction (see below), AND it or a subsequent task fails.
on_rollback { delete "/tmp/dump.sql" }
run "mysqldump -u theuser -p thedatabase > /tmp/dump.sql" do |ch, stream, out|
ch.send_data "thepassword\n" if out =~ /^Enter password:/
end
end
# You can use "transaction" to indicate that if any of the tasks within it fail,
# all should be rolled back (for each task that specifies an on_rollback
# handler).
desc "A task demonstrating the use of transactions."
task :long_deploy do
transaction do
deploy.update_code
# deploy.disable_web
deploy.symlink
deploy.migrate
end
deploy.restart
# deploy.enable_web
end
desc "Automatically run cleanup"
task :after_deploy, :roles => :app do
deploy.cleanup
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment