Skip to content

Instantly share code, notes, and snippets.

@davidchua
Last active December 11, 2015 07:29
Show Gist options
  • Save davidchua/4567097 to your computer and use it in GitHub Desktop.
Save davidchua/4567097 to your computer and use it in GitHub Desktop.
Default Capistrano Script to get Started - Git - RVM System
load 'deploy/assets'
require "bundler/capistrano"
set :application, "some_web"
set :repository, "git@somedomain.com:whatever.git"
set :deploy_to, "/home/ubuntu/some_web"
set :user, 'ubuntu'
set :use_sudo, false
set :rvm_type, :system
set :scm, :git # You can set :scm explicitly or Capistrano will make an intelligent guess based on known version control directory names
# Or: `accurev`, `bzr`, `cvs`, `darcs`, `git`, `mercurial`, `perforce`, `subversion` or `none`
role :web, "<IP>" # Your HTTP server, Apache/etc
role :app, "<IP>" # This may be the same as your `Web` server
role :db, "<IP>", :primary => true # This is where Rails migrations will run
# if you want to clean up old releases on each deploy uncomment this:
# after "deploy:restart", "deploy:cleanup"
# created symlink to database.yml
after 'deploy:update_code', 'deploy:symlink_shared'
before 'deploy:db:symlink_db', 'deploy:setup_config'
before 'deploy:assets:precompile', 'deploy:db:symlink_db'
# if you're still using the script/reaper helper you will need
# these http://github.com/rails/irs_process_scripts
# helper method to determine if a remote_file exists on record
def remote_file_exists?(full_path)
'true' == capture("if [ -e #{full_path} ]; then echo 'true'; fi").strip
end
namespace :deploy do
task :start do ; end
task :stop do ; end
task :restart, :roles => :app, :except => { :no_release => true } do
run "#{try_sudo} touch #{File.join(current_path,'tmp','restart.txt')}"
end
  task :symlink_shared do
    run "ln -nfs #{deploy_to}/shared/assets #{current_release}/public/assets"
  end
task :setup_config do
run "mkdir -p #{deploy_to}/shared/config"
run "mkdir -p #{deploy_to}/shared/log"
if remote_file_exists?("#{deploy_to}/shared/config/database.yml")
puts "database.yml exists, continuing on ..."
else
run "cd #{current_release}; cp -u #{current_release}/config/database.yml.example #{deploy_to}/shared/config/database.yml"
end
end
# symlink the database.yml
desc "Symlinks the database.yml"
task :symlink_db, :roles => :app do
run "ln -nfs #{deploy_to}/shared/config/database.yml #{release_path}/config/database.yml"
end
task :reset, :roles => [:web, :db] do
run "cd #{current_release}; bundle exec rake db:drop RAILS_ENV=#{rails_env}"
run "cd #{current_release}; bundle exec rake db:create RAILS_ENV=#{rails_env}"
end
end
# assets precompile speedup
namespace :assets do
task :precompile, :roles => :web, :except => { :no_release => true } do
# add check to prevent fresh deploy where there's no CURRENT_REVISION from stopping whole deploy
begin
from = source.next_revision(current_revision)
rescue
err_no = true
end
if err_no || capture("cd #{latest_release} && #{source.local.log(from)} vendor/assets/ app/assets/ | wc -l").to_i > 0
run %Q{cd #{latest_release} && #{rake} RAILS_ENV=#{rails_env} #{asset_env} assets:precompile}
else
logger.info "Skipping asset pre-compilation because there were no asset changes"
end
end
end
namespace :rails do
desc "Open the rails console on one of the remote servers"
task :console, :roles => :app do
puts "You're about to enter PRODUCTION level console..."
puts "Please exercise caution ..."
run_interactively "bundle exec rails console #{rails_env}"
end
desc "tail production log files"
task :tail_logs, :roles => :app do
trap("INT") { puts 'Interupted'; exit 0; }
run "tail -f #{shared_path}/log/production.log" do |channel, stream, data|
puts # for an extra line break before the host name
puts "#{channel[:host]}: #{data}"
break if stream == :err
end
end
end
require "rvm/capistrano"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment