Skip to content

Instantly share code, notes, and snippets.

@jptoto
Forked from r38y/deploy.rb
Created March 25, 2009 18:41
Show Gist options
  • Save jptoto/85628 to your computer and use it in GitHub Desktop.
Save jptoto/85628 to your computer and use it in GitHub Desktop.
# this is for multistage deployments... to deploy to staging, do "cap staging deploy"
# this is also set up for passenger deployments
# change stuff where it says CHANGEME
# set stage-specific stuff in the task named after the stage below
set :domain, "CHANGME"
role :web, domain
role :app, domain
role :db, domain, :primary => true
set :application, "CHANGEME"
set :deploy_to, lambda{"/home/deploy/public_html/#{application}/#{environment}"}
set :user, 'deploy'
set :group, 'users'
set :repository, "git@github.com:CHANGEME/CHANGEME.git"
set :scm, "git"
set :deploy_via, :remote_cache
set :repository_cache, "git_cache"
set :git_enable_submodules, true
set :branch, "master"
ssh_options[:keys] = %w(/Users/CHANGEME/.ssh/id_rsa)
ssh_options[:port] = CHANGEME
ssh_options[:forward_agent] = true
task :staging do
set :environment, "staging"
end
task :production do
set :environment, "production"
end
task :after_update_code, :roles => :app do
run "ln -s #{shared_path}/database.yml #{release_path}/config/database.yml"
end
task :after_symlink, :roles => :app do
restart_fetcher
restart_delayed_job
end
task :after_symlink, :roles => :web do
run "date > #{current_release}/DATE"
end
task :restart_fetcher do
sudo "god restart isfeasting_#{environment}_email_fetcher"
end
task :restart_delayed_job do
sudo "god restart isfeasting-#{environment}-dj"
end
after "deploy", "deploy:cleanup"
after "deploy:migrations", "deploy:cleanup"
namespace :deploy do
desc "Restarting mod_rails with restart.txt"
task :restart, :roles => :app, :except => { :no_release => true } do
run "touch #{current_path}/tmp/restart.txt"
end
[:start, :stop].each do |t|
desc "#{t} task is a no-op with mod_rails"
task t, :roles => :app do ; end
end
end
namespace :db do
task :backup_name, :roles => :db, :only => { :primary => true } do
now = Time.now
run "mkdir -p #{shared_path}/db_backups"
backup_time = [now.year,now.month,now.day,now.hour,now.min,now.sec].join('-')
set :backup_file, "#{shared_path}/db_backups/CHANGEME(APPNAME)_CHANGEME(ENV)-snapshot-#{backup_time}.sql"
end
desc "Dump the production database and import into the staging database"
task :dump_prod_to_staging, :roles => :db, :only => {:primary => true} do
backup_name
on_rollback { run "rm -f #{backup_file}" }
run "mysql -uCHANGEME -pCHANGEME -e 'drop database CHANGEME_staging; create database CHANGEME_staging'"
# schema first
run "mysqldump --add-drop-table --no-data -uCHANGEME -pCHANGEME CHANGEME_production > #{backup_file}"
# data second minus big tables
run "mysqldump -uCHANGEME -pCHANGEME CHANGEME_production >> #{backup_file}"
run "mysql -uCHANGEME -pCHANGEME CHANGEME_staging < #{backup_file}"
run "rm -f #{backup_file}"
end
desc "Dump Staging Database to Local Machine Except (will take a long time to SCP)"
task :dump_staging_to_local, :roles => :db, :only => { :primary => true } do
backup_name
gz_backup_file = "#{backup_name}.gz"
base_backup_file = backup_file.split("/").last
base_gz_backup_file = gz_backup_file.split("/").last
on_rollback { run "rm -f #{backup_file}" }
logger.debug "Dump the staging database."
run "mysqldump --single-transaction --complete-insert --add-drop-table -uCHANGEME -pCHANGEME CHANGEME_staging > #{backup_file}"
logger.debug "Gzip it up."
run "gzip -f #{backup_file}"
logger.debug "SCP file to your desktop, this will take a while depending on what tables you exclude."
puts `scp -P CHANGEME(port) CHANGEME@CHANGEME:#{gz_backup_file} ~/Desktop`
logger.debug "Delete backup from staging server."
run "rm -f #{gz_backup_file}"
logger.debug "Unzip staging dump at ~/Desktop"
`gunzip ~/Desktop/#{base_gz_backup_file}`
logger.debug "Load staging dump into dev db"
db_config = YAML.load_file(File.dirname(__FILE__) + "/database.yml")['development']
`mysql -u #{db_config['username']} -p#{db_config['password']} #{db_config['database']} < ~/Desktop/#{base_backup_file}`
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment