Hi guys i have problem to deploy my rails app to production server. Actually i have install bundle in server via gem install bundler. Unfortunately when i typed cap production deploy it becomes like this:
** [out :: server url] bash: bundle: command not found
command finished in 772ms
*** [deploy:update_code] rolling back
* executing "rm -rf /home/deployer/crawler/releases/20180326063414; true"
servers: ["server url"]
[server url] executing command
command finished in 833ms
failed: "rvm_path=$HOME/.rvm/ $HOME/.rvm/bin/rvm-shell 'ruby-2.2.1' -c 'cd /home/deployer/crawler/releases/20180326063414 && bundle install --gemfile /home/deployer/crawler/releases/20180326063414/Gemfile --path /home/deployer/crawler/shared/bundle --deployment --quiet --without development test'" on server url
it says bash: bundle: command not found. I typed bundle --version the output is:
Bundler version 1.16.1
here is my gemfile
group :development do
gem 'capistrano', '~>2.15.4'
gem 'rvm-capistrano', '1.4.1', require: false
gem 'invoker'
gem 'regularity', require: nil
gem 'pry-rails'
end
and here is my deploy.rb:
require "bundler/capistrano"
require "rvm/capistrano"
require 'capistrano/ext/multistage'
require 'rollbar/capistrano'
#set :rollbar_token, '3dc5b293a0c74c3e8b98ab26f07bea74'
set :rollbar_token, 'de21e333f1d5469ea521c2ef3a29bd3d'
set :application, "crawler"
# set :repository, "git#bitbucket.org:terbang-ventures/linkedin-scraper.git"
set :repository, "git#bitbucket.org:terbang-ventures/linkedin-scraper.git"
set :rails_env, "production"
set :branch, "master"
set :scm, :git
set :default_shell, '/bin/bash -l'
# set :whenever_environment, defer { stage }
# require "whenever/capistrano"
set :stages, %w(production staging)
set :user, 'deployer'
set :rvm_type, :user
set :rvm_ruby_string, 'ruby-2.2.1'
set :rvm_binary, '~/.rvm/bin/rvm'
$:.unshift(File.expand_path('./lib', ENV['rvm_path']))
set :deploy_to, "/home/#{user}/#{application}"
set :use_sudo, false
set :deploy_via, :remote_cache
default_run_options[:pty] = true
ssh_options[:forward_agent] = true
namespace :deploy do
desc 'Load environment variables'
task :load_env_vars do
run "ln -s /home/#{user}/env.yml #{current_release}/config/env.yml"
end
desc "Migrate DB1"
task :migrate_db1 do
# rake db:migrate_db1 RAILS_ENV=staging
run "cd #{current_path} && bundle exec rake db:migrate_db1 RAILS_ENV=#{rails_env}"
end
desc 'Re-establish mongoid.yml'
task :set_database_symlink do
run "ln -s /home/#{user}/mongoid.yml #{current_release}/config/mongoid.yml"
end
desc 'Re-establish database.yml'
task :set_database_symlink_active_record do
run "ln -s /home/#{user}/database.yml #{current_release}/config/database.yml"
end
desc 'run bundle install'
task :install_bundle do
run "cd #{current_path} && bundle install"
end
task :restart, :roles => :app, :except => { :no_release => true } do
run "ps axf | grep puma | grep -v grep | awk '{print \"kill -9 \" $1}' | sh"
run "cd #{current_path} && bundle exec puma -e #{rails_env} -p 9292 -d"
run "cd #{current_path} && bundle exec puma -e #{rails_env} -p 9293 -d"
end
task :stop, :roles => :app, :except => { :no_release => true } do
run "ps axf | grep puma | grep -v grep | awk '{print \"kill -9 \" $1}' | sh"
end
task :start, :roles => :app, :except => { :no_release => true } do
run "cd #{current_path} && bundle exec puma -e #{rails_env} -p 9292 -d"
run "cd #{current_path} && bundle exec puma -e #{rails_env} -p 9293 -d"
end
end
namespace :searchkick do
# reindex
namespace :reindex do
# Reindex Contact
task :contact, except: {no_release: true} do
run "cd #{current_path} && bundle exec rake searchkick:reindex CLASS=Contact RAILS_ENV=#{rails_env}"
end
# Reindex Contact
task :company, except: {no_release: true} do
run "cd #{current_path} && bundle exec rake searchkick:reindex CLASS=Company RAILS_ENV=#{rails_env}"
end
task :user, except: {no_release: true} do
run "cd #{current_path} && bundle exec rake searchkick:reindex CLASS=User RAILS_ENV=#{rails_env}"
end
task :all, except: {no_release: true} do
run "cd #{current_path} && bundle exec rake searchkick:reindex:all RAILS_ENV=#{rails_env}"
end
end
end
namespace :importer do
task :department, roles: :app, except: {no_release: true} do
run "cd #{current_path} && bundle exec rake importer:import_department RAILS_ENV=#{rails_env}"
end
task :staff_level, roles: :app, except: {no_release: true} do
run "cd #{current_path} && bundle exec rake importer:import_staff_level RAILS_ENV=#{rails_env}"
end
task :email_format, roles: :app, except: {no_release: true} do
run "cd #{current_path} && bundle exec rake importer:import_email_format RAILS_ENV=#{rails_env}"
end
task :combined_emails, roles: :app, except: {no_release: true} do
run "cd #{current_path} && bundle exec rake importer:import_combined_emails RAILS_ENV=#{rails_env}"
end
task :late_address, roles: :app, except: {no_release: true} do
run "cd #{current_path} && bundle exec rake importer:import_late_address RAILS_ENV=#{rails_env}"
end
end
namespace :rails do
desc "Remote console"
task :console, roles: :app, except: { no_release: true } do
run_interactively "bundle exec rails c -e #{rails_env}"
end
desc "Remote dbconsole"
task :dbconsole, roles: :app, except: { no_release: true } do
run_interactively "bundle exec rails dbconsole -e #{rails_env}"
end
# Short aliases
task :c, roles: :app do
console
end
task :dbc, roles: :app do
dbconsole
end
desc "Monitor log"
task :log, roles: :app, except: { no_release: true } do
run_interactively "tail -f log/#{rails_env}.log"
end
def run_interactively(command)
server ||= find_servers_for_task(current_task).first
puts " running `#{command}` as #{user}##{server}"
exec %Q(ssh #{user}##{server} -t "bash --login -c 'cd #{current_path} && #{command}'")
end
end
after "deploy", "deploy:cleanup"
after "deploy", "deploy:migrate"
after "deploy:migrate", "deploy:migrate_db1"
after "deploy:start", "sidekiq:start"
after "deploy", "sidekiq:restart"
before "deploy:assets:precompile", "deploy:load_env_vars"
before "deploy:assets:precompile", "deploy:set_database_symlink"
before "deploy:assets:precompile", "deploy:set_database_symlink_active_record"
before "deploy:assets:precompile", "deploy:assets:clean"
before "deploy:migrate", "deploy:install_bundle"
My question is, why that capistrano still asking my bundle command not found if i trace it failed in rvm_path=$HOME/.rvm/ $HOME/.rvm/bin/rvm-shell 'ruby-2.2.1' -c 'cd /home/deployer/crawler/releases/20180326065919 && bundle install --gemfile /home/deployer/crawler/releases/20180326065919/Gemfile --path /home/deployer/crawler/shared/bundle --deployment --quiet --without development test'?
Related
I am deploying with Capistrano to my new VPS. After the first deploy(cap deploy) everything was OK (site was running), but the second deploy failed on assets:precompile error.
I am running rails 3.2.13, ruby 2.0.0, rvm.
error:
* executing "cd -- /home/rails/releases/20140116121250 && RAILS_ENV=production RAILS_GROUPS=assets bundle exec rake assets:precompile"
servers: ["IP"]
[IP] executing command
*** [err :: IP] bash: line 1: 23406 Killed RAILS_ENV=production RAILS_GROUPS=assets bundle exec rake assets:precompile
command finished in 84187ms
*** [deploy:update_code] rolling back
* executing "rm -rf /home/rails/releases/20140116121250; true"
servers: ["IP"]
[IP] executing command
command finished in 519ms
failed: "rvm_path=/usr/local/rvm /usr/local/rvm/bin/rvm-shell 'default' -c 'cd -- /home/rails/releases/20140116121250 && RAILS_ENV=production RAILS_GROUPS=assets bundle exec rake assets:precompile'" on IP
deploy.rb file:
set :application, "app_name"
set :repository, "git_repository"
role :web, "IP"
role :app, "IP"
role :db, "IP", :primary => true
set :user, "rails"
set :password, "password"
set :use_sudo, false
set :deploy_to, "/home/rails/"
set :deploy_via, :copy
set :normalize_asset_timestamps, false
require 'bundler/capistrano'
require "rvm/capistrano"
set :rvm_type, :system
Capfile
load 'deploy'
# Uncomment if you are using Rails' asset pipeline
load 'deploy/assets'
load 'config/deploy' # remove this line to skip loading any of the default tasks
I am quite new to Capistrano, so please try to explain the solution clearly. Thank You for your support!
It seems as you have low operation memory size on your vpn server. (Vpn supplies without memory-swap for now) thus operation system kills your deploy process.
The solution is to compile assets locally (on your development machine)
Add deploy:assets:precompile task to your deploy.rb file (this is for Capistrano 2)
namespace :deploy do
. . .
namespace :assets do
task :precompile, :roles => :web do
from = source.next_revision(current_revision)
if capture("cd #{latest_release} && #{source.local.log(from)} vendor/assets/ lib/assets/ app/assets/ | wc -l").to_i > 0
run_locally("rake assets:clean && rake assets:precompile")
run_locally "cd public && tar -jcf assets.tar.bz2 assets"
top.upload "public/assets.tar.bz2", "#{shared_path}", :via => :scp
run "cd #{shared_path} && tar -jxf assets.tar.bz2 && rm assets.tar.bz2"
run_locally "rm public/assets.tar.bz2"
run_locally("rake assets:clean")
else
logger.info "Skipping asset precompilation because there were no asset changes"
end
end
end
end
Then just redeploy you app
$ bundle exec cap deploy
wish it helps
"rake assets:precompile" and "cap deploy" consume extremely high amount of time whenever I call it (usually more then an hour). Sometimes deploy hang at all.
I use Rails 3.2.13, Ruby 1.9.3, OSX and Backbone's Chaplin Framework for frontend.
How can I speed up assets precompile and deploy task and what can cause such freezing?
My deploy.rb config:
# -*- encoding : utf-8 -*-
require 'bundler/capistrano'
load 'deploy/assets'
set :application, "eyelashes"
set :rails_env, "production"
set :repository, "git#github.com:eyelasher/repo.git"
set :scm, :git
set :deploy_via, :checkout
set :ssh_options, { :forward_agent => true }
default_run_options[:pty] = true
server "75.223.145.3", :app, :web, :db, :primary => true
set :bundle_without, [:test]
set :user, 'deployer'
set :deploy_to, "/home/deployer/eyelasher"
set :branch, "master" unless exists?(:branch)
set :use_sudo, false
set :rvm_type, :user
set :rvm_ruby_string, :local
set :deploy_via, :checkout
before 'deploy:setup', 'rvm:create_gemset'
set :unicorn_conf, "#{deploy_to}/current/config/unicorn.rb"
set :unicorn_pid, "#{deploy_to}/shared/pids/unicorn.pid"
after "deploy", "deploy:migrate"
after "deploy", "deploy:cleanup"
require 'rvm/capistrano'
require 'thinking_sphinx/deploy/capistrano'
#after 'deploy:update_code', :roles => :app do
#run "rm -f #{current_release}/config/database.yml"
#run "ln -s #{deploy_to}/shared/config/database.yml #{current_release}/config/database.yml"
#end
namespace :deploy do
task :restart do
run "if [ -f #{unicorn_pid} ]; then kill -USR2 `cat #{unicorn_pid}`; else cd #{deploy_to}/current && bundle exec unicorn -c #{unicorn_conf} -E #{rails_env} -D; fi"
end
task :start do
run "cd #{deploy_to}/current && bundle exec unicorn -c #{unicorn_conf} -E #{rails_env} -D;"
end
task :stop do
run "if [ -f #{unicorn_pid} ] && [ -e /proc/$(cat #{unicorn_pid}) ]; then kill -QUIT `cat #{unicorn_pid}`; fi"
end
end
# Thinking Sphinx typing shortcuts
namespace :ts do
task :conf do
thinking_sphinx.configure
end
task :in do
thinking_sphinx.index
end
task :start do
thinking_sphinx.start
end
task :stop do
thinking_sphinx.stop
end
task :restart do
thinking_sphinx.restart
end
task :rebuild do
thinking_sphinx.rebuild
end
end
# http://github.com/jamis/capistrano/blob/master/lib/capistrano/recipes/deploy.rb
# :default -> update, restart
# :update -> update_code, symlink
namespace :deploy do
desc "Link up Sphinx's indexes."
task :symlink_sphinx_indexes, :roles => [:app] do
run "ln -nfs #{shared_path}/db/sphinx #{release_path}/db/sphinx"
end
task :activate_sphinx, :roles => [:app] do
symlink_sphinx_indexes
thinking_sphinx.configure
#thinking_sphinx.stop
#thinking_sphinx.start
end
task :copy_images do
run "cp -R #{shared_path}/public/images #{release_path}/public/images"
end
before 'deploy:update_code', 'thinking_sphinx:stop'
after 'deploy:update_code', 'deploy:activate_sphinx'
after 'deploy:update_code', 'deploy:copy_images'
end
Maybe I can speedup somehow by making changes in deploy configuration?
If you are deploying to a memory constrained VPS that sounds plausible. I'd recommend precompiling the assets on a a faster machine and checking them in to your repository.
There are various Gems and packages for speeding up asset precompilation by parallesing them, however in your situation that will likely make things MUCH worse!
on cap deploy:cold ro cap deploy, is triggered an error
[out :: xxx.xxx.xxx.xxx] Killed
command finished in 9020ms
*** [deploy:update_code] rolling back
* executing [#<Capistrano::Command::Tree::ElseBranch:0x00000100dc5478 #condition="else", #command="rm -rf /home/yasinishyn/apps/mkv/releases/20130506084016; true", #callback=#<Proc:0x00000100dd5da0#/usr/local/rvm/gems/ruby-2.0.0-p0/gems/capistrano-2.15.3/lib/capistrano/configuration/actions/invocation.rb:13>, #options={}, #skip=false>]
servers: ["xxx.xxx.xxx.xxx"]
[xxx.xxx.xxx.xxx] executing command
command finished in 386ms
failed: "sh -c 'cd -- /home/yasinishyn/apps/mkv/releases/20130506084016 && bundle exec rake RAILS_ENV=production RAILS_GROUPS=assets assets:precompile'" on xxx.xxx.xxx.xxx
I have tryed many advises from stack, but nosing works for me.
my deploy.rb
deploy.rb:
require "bundler/capistrano"
server "xxx.xxx.xxx.xxx", :web, :app, :db, primary: true
set :application, "app"
set :user, "user"
set :deploy_to, "/home/#{user}/apps/#{application}"
set :deploy_via, :remote_cache
set :use_sudo, false
set :scm, "git"
set :repository, "git#github.com:git_user/#{application}.git"
set :branch, "master"
default_run_options[:pty] = true
ssh_options[:forward_agent] = true
after "deploy", "deploy:cleanup" # keep only the last 5 releases
namespace :deploy do
%w[start stop restart].each do |command|
desc "#{command} unicorn server"
task command, roles: :app, except: {no_release: true} do
run "/etc/init.d/unicorn_#{application} #{command}"
end
end
task :setup_config, roles: :app do
sudo "ln -nfs #{current_path}/config/nginx.conf /etc/nginx/sites-enabled/#{application}"
sudo "ln -nfs #{current_path}/config/unicorn_init.sh /etc/init.d/unicorn_#{application}"
run "mkdir -p #{shared_path}/config"
put File.read("config/database.example.yml"), "#{shared_path}/config/database.yml"
puts "Now edit the config files in #{shared_path}."
end
after "deploy:setup", "deploy:setup_config"
task :symlink_config, roles: :app do
run "ln -nfs #{shared_path}/config/database.yml #{release_path}/config/database.yml"
end
after "deploy:finalize_update", "deploy:symlink_config"
desc "Make sure local git is in sync with remote."
task :check_revision, roles: :web do
unless `git rev-parse HEAD` == `git rev-parse origin/master`
puts "WARNING: HEAD is not the same as origin/master"
puts "Run `git push` to sync changes."
exit
end
end
before "deploy", "deploy:check_revision"
#rake seed task
desc "Seed the database on already deployed code"
task :seed, :only => {:primary => true}, :except => { :no_release => true } do
run "cd #{current_path}; RAILS_ENV=#{rails_env} bundle exec rake db:seed"
end
desc "Seed the database on already deployed code"
task :drop, :only => {:primary => true}, :except => { :no_release => true } do
run "cd #{current_path}; RAILS_ENV=#{rails_env} bundle exec rake db:drop:all"
run "cd #{current_path}; RAILS_ENV=#{rails_env} bundle exec rake db:create:all"
run "cd #{current_path}; RAILS_ENV=#{rails_env} bundle exec rake db:migrate"
end
end
in my production.rb I have
config.assets.compress = true
and my capfile
load 'deploy'
# Uncomment if you are using Rails' asset pipeline
load 'deploy/assets'
Dir['vendor/gems/*/recipes/*.rb','vendor/plugins/*/recipes/*.rb'].each { |plugin| load(plugin) }
load 'config/deploy' # remove this line to skip loading any of the default tasks
local precompile with "bundle exec rake assets:precompile RAILS_ENV=production --trace" executes without any errors.
Where is my mistake? Hot to debug it? Or maybe somewhere is some log file for capistano in which a can look for more details?
I find an answer by my own
delete "load 'deploy/assets'" from capfile, and run
cap deploy:cold
this will work without an error, but only on initial deploy. Then as usual "sudo service nginx restart" on server, and add back deleted snippet.
And BAMM!! It works :)
You are out of RAM by the sounds of it, this question and answer helped me out - I increased the swap on digital ocean server and everything worked fine: deploy with capistrano failing
For anyone who face the same problem and Andrey answer don't work stop the server "sudo service nginx stop" then cap deploy and then start the server again "sudo service nginx start". It worked for me.
I'm trying to deploy the first rails application using Capistrano.
I'm getting an error while capistrano tries to run rake db:migrate.
To be more clear this is the traceback of the exception:
[ip] executing command
*** [err :: ip] bash: rake: command not found
command finished in 124ms
failed: "env RAILS_ENV=production bash -c 'cd /home/deployer/website/current && rake RAILS_ENV=production db:migrate'" on ip
And this is the config/deploy.rb I'm using actually. On the server I've installed RVM (single user installation).
# config/deploy.rb
$:.unshift(File.expand_path('./lib', ENV['rvm_path']))
require "rvm/capistrano"
set :scm, :git
set :repository, "git#bitbucket.org:user/project.git"
set :branch, "origin/master"
set :migrate_target, :current
set :ssh_options, { :forward_agent => true }
set :rails_env, "production"
set :deploy_to, "/home/deployer/website"
set :normalize_asset_timestamps, false
set :rvm_type, :user # Don't use system-wide RVM
set :rvm_ruby_string, "ruby-1.9.3-p194#global"
set :user, "deployer"
set :group, "staff"
set :use_sudo, false
role :web, "IP"
role :app, "IP"
role :db, "IP2", :primary => true
set(:latest_release) { fetch(:current_path) }
set(:release_path) { fetch(:current_path) }
set(:current_release) { fetch(:current_path) }
set(:current_revision) { capture("cd #{current_path}; git rev-parse --short HEAD").strip }
set(:latest_revision) { capture("cd #{current_path}; git rev-parse --short HEAD").strip }
set(:previous_revision) { capture("cd #{current_path}; git rev-parse --short HEAD#{1}").strip }
default_environment["RAILS_ENV"] = 'production'
default_run_options[:shell] = 'bash'
namespace :deploy do
desc "Deploy your application"
task :default do
update
restart
end
desc "Setup your git-based deployment app"
task :setup, :except => { :no_release => true } do
dirs = [deploy_to, shared_path]
dirs += shared_children.map { |d| File.join(shared_path, d) }
run "#{try_sudo} mkdir -p #{dirs.join(' ')} && #{try_sudo} chmod g+w #{dirs.join(' ')}"
run "git clone #{repository} #{current_path}"
end
task :cold do
update
migrate
end
task :update do
transaction do
update_code
end
end
desc "Update the deployed code."
task :update_code, :except => { :no_release => true } do
run "cd #{current_path}; git fetch origin; git reset --hard #{branch}"
finalize_update
end
desc "Update the database (overwritten to avoid symlink)"
task :migrations do
transaction do
update_code
end
migrate
restart
end
task :finalize_update, :except => { :no_release => true } do
run "chmod -R g+w #{latest_release}" if fetch(:group_writable, true)
# mkdir -p is making sure that the directories are there for some SCM's that don't
# save empty folders
run <<-CMD
rm -rf #{latest_release}/log #{latest_release}/public/system #{latest_release}/tmp/pids &&
mkdir -p #{latest_release}/public &&
mkdir -p #{latest_release}/tmp &&
ln -s #{shared_path}/log #{latest_release}/log &&
ln -s #{shared_path}/system #{latest_release}/public/system &&
ln -s #{shared_path}/pids #{latest_release}/tmp/pids &&
ln -sf #{shared_path}/database.yml #{latest_release}/config/database.yml
CMD
if fetch(:normalize_asset_timestamps, true)
stamp = Time.now.utc.strftime("%Y%m%d%H%M.%S")
asset_paths = fetch(:public_children, %w(images stylesheets javascripts)).map { |p| "#{latest_release}/public/#{p}" }.join(" ")
run "find #{asset_paths} -exec touch -t #{stamp} {} ';'; true", :env => { "TZ" => "UTC" }
end
end
desc "Zero-downtime restart of Unicorn"
task :restart, :except => { :no_release => true } do
run "kill -s USR2 `cat /tmp/unicorn.my_site.pid`"
end
desc "Start unicorn"
task :start, :except => { :no_release => true } do
run "cd #{current_path} ; bundle exec unicorn_rails -c config/unicorn.rb -D"
end
desc "Stop unicorn"
task :stop, :except => { :no_release => true } do
run "kill -s QUIT `cat /tmp/unicorn.my_site.pid`"
end
namespace :rollback do
desc "Moves the repo back to the previous version of HEAD"
task :repo, :except => { :no_release => true } do
set :branch, "HEAD#{1}"
deploy.default
end
desc "Rewrite reflog so HEAD#{1} will continue to point to at the next previous release."
task :cleanup, :except => { :no_release => true } do
run "cd #{current_path}; git reflog delete --rewrite HEAD#{1}; git reflog delete --rewrite HEAD#{1}"
end
desc "Rolls back to the previously deployed version."
task :default do
rollback.repo
rollback.cleanup
end
end
end
def run_rake(cmd)
run "cd #{current_path}; #{rake} #{cmd}"
end
How can I make this run properly?
Of course I already tried to run rake on the server, and it is found properly.
Thanks
you break rvm/capistrano integration by adding this line:
default_run_options[:shell] = 'bash'
You need to remove it.
Also I see a lot of over-engineering in your script, please check my example script which I run from time to time to confirm RVM works as expected:
https://github.com/mpapis/ad/blob/master/config/deploy.rb
I followed the docs on cap + nginx + unicorn but have some problems to understand how to do the database deployment correctly.
/config/database.yml should not be in the git repo ( preferable )
on production server in /shared/database.yml you place the database.yml
Problem is that on deploy it still looks for /config/database.yml
How can I make my deploy.rb grab the /shared/database.yml instead?
search high and low for this to no avail :(
deploy.rb
# config/deploy.rb
require "bundler/capistrano"
set :scm, :git
set :repository, "root#109.etc:/srv/paintings.git"
set :branch, "origin/master"
set :migrate_target, :current
set :ssh_options, {:forward_agent => true}
set :rails_env, "production"
set :deploy_to, "/srv/paintings"
set :normalize_asset_timestamps, false
set :user, "root"
set :group, ""
set :use_sudo, true
default_run_options[:pty] = true
set :port, 5984
ssh_options[:port] = 5984
role :web, "109.etc"
role :app, "109.etc"
role :db, "109.etc", :primary => true
set(:latest_release) { fetch(:current_path) }
set(:release_path) { fetch(:current_path) }
set(:current_release) { fetch(:current_path) }
set(:current_revision) { capture("cd #{current_path}; git rev-parse --short HEAD").strip }
set(:latest_revision) { capture("cd #{current_path}; git rev-parse --short HEAD").strip }
set(:previous_revision) { capture("cd #{current_path}; git rev-parse --short HEAD#{1}").strip }
default_environment["RAILS_ENV"] = 'production'
#default_environment["PATH"] = "/bin/bash"
#default_environment["GEM_HOME"] = "/usr/local/rvm/gems/ruby-1.9.3-p125"
#default_environment["GEM_PATH"] = "/usr/local/rvm/gems/ruby-1.9.3-p125"
#default_environment["RUBY_VERSION"] = "ruby 1.9.3p125"
#default_run_options[:shell] = 'bash'
namespace :deploy do
desc "Deploy your application"
task :default do
update
restart
end
desc "Setup your git-based deployment app"
task :setup, :except => {:no_release => true} do
dirs = [deploy_to, shared_path]
dirs += shared_children.map { |d| File.join(shared_path, d) }
run "#{try_sudo} mkdir -p #{dirs.join(' ')} && #{try_sudo} chmod g+w #{dirs.join(' ')}"
run "git clone #{repository} #{current_path}"
end
task :cold do
update
migrate
end
task :update do
transaction do
update_code
end
end
desc "Update the deployed code."
task :update_code, :except => {:no_release => true} do
run "cd #{current_path}; git fetch origin; git reset --hard #{branch}"
finalize_update
end
desc "Update the database (overwritten to avoid symlink)"
task :migrations do
transaction do
update_code
end
migrate
restart
end
task :finalize_update, :except => {:no_release => true} do
run "chmod -R g+w #{latest_release}" if fetch(:group_writable, true)
# mkdir -p is making sure that the directories are there for some SCM's that don't
# save empty folders
run <<-CMD
rm -rf #{latest_release}/log #{latest_release}/public/system #{latest_release}/tmp/pids &&
mkdir -p #{latest_release}/public &&
mkdir -p #{latest_release}/tmp &&
ln -s #{shared_path}/log #{latest_release}/log &&
ln -s #{shared_path}/system #{latest_release}/public/system &&
ln -s #{shared_path}/pids #{latest_release}/tmp/pids
CMD
if fetch(:normalize_asset_timestamps, true)
stamp = Time.now.utc.strftime("%Y%m%d%H%M.%S")
asset_paths = fetch(:public_children, %w(images stylesheets javascripts)).map { |p| "#{latest_release}/public/#{p}" }.join(" ")
run "find #{asset_paths} -exec touch -t #{stamp} {} ';'; true", :env => {"TZ" => "UTC"}
end
end
desc "Zero-downtime restart of Unicorn"
task :restart, :except => {:no_release => true} do
#run "kill -s USR2 'cat /srv/paintings/shared/pids/unicorn.pid'"
run "kill -s USR2 'cat /srv/paintings/shared/tmp/unicorn.pid'"
end
desc "Start unicorn"
task :start, :except => {:no_release => true} do
run "cd #{current_path} ; bundle exec unicorn_rails -c config/unicorn.rb -D"
end
desc "Stop unicorn"
task :stop, :except => {:no_release => true} do
#run "kill -s QUIT 'cat /srv/paintings/shared/pids/unicorn.pid'"
run "kill -s QUIT 'cat /tmp/unicorn.pid'"
end
namespace :rollback do
desc "Moves the repo back to the previous version of HEAD"
task :repo, :except => {:no_release => true} do
set :branch, "HEAD#{1}"
deploy.default
end
desc "Rewrite reflog so HEAD#{1} will continue to point to at the next previous release."
task :cleanup, :except => {:no_release => true} do
run "cd #{current_path}; git reflog delete --rewrite HEAD#{1}; git reflog delete --rewrite HEAD#{1}"
end
desc "Rolls back to the previously deployed version."
task :default do
rollback.repo
rollback.cleanup
end
end
end
def run_rake(cmd)
run "cd #{current_path}; #{rake} #{cmd}"
end
I'm using a very similar deploy.rb, but I have one more symbolic link in my finalize_update method:
ln -sf #{shared_path}/database.yml #{latest_release}/config/database.yml