Skip to content

Instantly share code, notes, and snippets.

@bbg-deploy
Created May 17, 2012 19:47
Show Gist options
  • Save bbg-deploy/2721190 to your computer and use it in GitHub Desktop.
Save bbg-deploy/2721190 to your computer and use it in GitHub Desktop.
set :whenever_environment, defer { stage }
require 'capistrano/ext/multistage'
require 'bundler/capistrano'
require 'yaml'
set :whenever_command, "bundle exec whenever"
require 'whenever/capistrano'
$:.unshift(File.expand_path('./lib', ENV['rvm_path']))
require "rvm"
require "rvm/capistrano"
set :rvm_ruby_string, 'ruby-1.9.2-p290'
set :rvm_type, :system # Copy the exact line. I really mean :user here
set :rvm_path, "/usr/local/rvm"
set :rvm_bin_path, "#{rvm_path}/bin"
set :rvm_lib_path, "#{rvm_path}/lib"
set :keep_releases, 5
set :default_environment, {
#'PATH' => "/usr/local/rvm/gems/ruby-1.9.2-p180/bin:/usr/local/rvm/bin:/usr/local/rvm/rubies/ruby-1.9.2-p290/bin:$PATH",
#'PATH' => "/usr/local/rvm/bin:/usr/local/rvm/rubies/ruby-1.9.2-p290/bin:$PATH",
#'PATH' => "/usr/local/rvm/bin:$PATH",
'RUBY_VERSION' => 'ruby-1.9.2-p290@global',
'GEM_HOME' => '/usr/local/rvm/gems/ruby-1.9.2-p290@global',
'GEM_PATH' => '/usr/local/rvm/gems/ruby-1.9.2-p290@global',
'BUNDLE_PATH' => '/usr/local/rvm/gems/ruby-1.9.2-p290@global' # If you are using bundler.
}
set :user, "deploy"
set :application, "youbuy"
set :repository, 'git@github.com:youbeauty/youbuy.git'
set :scm, :git
set :scm_verbose, true
set :deploy_via, :remote_cache
set :keep_releases, 5
set :use_sudo, false
set :stages, %w(staging production)
set :default_stage, 'staging'
set :normalize_asset_timestamps, false
set :shared_children, %w(public/system log tmp/pids doc)
default_run_options[:pty] = true
ssh_options[:forward_agent] = true
set :runner, user
set :whenever_identifier, defer { "#{application}_#{stage}" }
after "deploy:restart", "deploy:cleanup"
# after "deploy:update_code", "deploy:precompile_assets"
namespace :deploy do
desc "Copy config files"
#
after "deploy:update_code" do
run "export RAILS_ENV=staging"
run "ln -nfs #{shared_path}/public/assets #{release_path}/public/assets"
run "ln -nfs #{shared_path}/public/spree #{release_path}/public/spree"
# run "ln -nfs #{shared_path}/public/labels_shipment #{release_path}/public/labels_shipment"
# run "ln -nfs #{shared_path}/public/shipment_labels #{release_path}/public"
# run "ln -nfs #{shared_path}/public/product_data #{release_path}/public/product_data"
# run "ln -nfs #{shared_path}/public/invoice #{release_path}/public/invoice"
# run "ln -nfs #{shared_path}/doc/product_data #{release_path}/doc/product_data"
# run "ln -nfs #{shared_path}/doc/product_data/ #{release_path}/doc/product_data/import_logs/"
run "ln -nfs #{shared_path}/log #{release_path}/log"
run "ln -nfs #{shared_path}/tmp #{release_path}/tmp"
run "cp -R #{shared_path}/config/database.yml #{release_path}/config/"
run "cp -R #{shared_path}/config/environments/staging.rb #{release_path}/config/environments/staging.rb"
sudo "chmod -R 0777 #{release_path}/tmp/"
# sudo "chmod -R 0777 #{release_path}/public/labels_shipment"
# sudo "chmod -R 0777 #{release_path}/public/spree/products"
# sudo "chmod -R 0777 #{release_path}/public/product_data"
# sudo "chmod -R 0777 #{release_path}/public/invoice"
# sudo "chmod -R 0777 #{release_path}/doc/product_data"
end
task :restart, :roles => :app, :except => { :no_release => true } do
run "touch #{File.join(current_path,'tmp','restart.txt')}"
end
desc "Seed the database"
task :seed, :roles => :db do
# on_rollback { deploy.db.restore }
run "cd #{current_path}"
run "rake db:seed RAILS_ENV=staging"
run "rake spree_sample:load RAILS_ENV=staging"
end
desc 'run bundle install'
task :bundle_install, :roles => :app do
run "cd #{current_path} && bundle install --deployment --path #{shared_path}/bundle"
end
desc "Reset the database"
task :reset, :roles => :db do
# on_rollback { deploy.db.restore }
run "cd #{current_path}"
run "rake db:migrate:reset RAILS_ENV=staging"
end
# task :cleanup do
# #do nothing
# end
desc "Compile all the assets named in config.assets.precompile."
task :precompile_assets do
raise "Rails environment not set" unless rails_env
task = "assets:precompile"
run "cd #{release_path} && bundle exec rake #{task} RAILS_ENV=#{rails_env}"
end
end
after "deploy:create_symlink", "deploy:bundle_install"
namespace :bundler do
task :create_symlink, :roles => :app do
shared_dir = File.join(shared_path, 'bundle')
release_dir = File.join(current_release, '.bundle')
run("mkdir -p #{shared_dir} && ln -s #{shared_dir} #{release_dir}")
end
task :bundle_new_release, :roles => :app do
bundler.create_symlink
run "cd #{release_path} && source $HOME/.bash_profile && bundle install"
end
end
#namespace :rvm do
# task :trust_rvmrc do
# run( "rvm rvmrc trust #{release_path}")
# end
#end
desc "Dumps target database into development db"
namespace :db do
task :clone_to_local, :roles => [:db], :only => {:primary => true} do
file = "#{application}.sql.bz2"
database = YAML::load_file('config/database.yml')
db_user = database[rails_env]['username']
db_name = database[rails_env]['database']
local_db_user = database['development']['username']
local_db_dev = database['development']['database']
run "pg_dump --clean --no-owner --no-privileges -U#{db_user} #{db_name} | bzip2 > #{file}" do |ch, stream, out|
ch.send_data "#{db_password}\n" if out =~ /^Password:/
puts out
end
puts rsync = "rsync #{user}@#{db_host}:#{file} tmp"
`#{rsync}`
puts depackage = "bzcat tmp/#{file} | psql #{local_db_dev} -U#{local_db_user}"
`#{depackage}`
end
end
after 'deploy:finalize_update', 'bundler:bundle_new_release'
#after "deploy", "rvm:trust_rvmrc"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment