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
Related
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'?
The issue seems to be that during a capistrano deploy the created cron job has RAILS_ENV=staging as expected since the deployment environment is staging. However, in a capistrano rollback the created cron job has RAILS_ENV=new_staging where new_staging is the capistrano stage being rolled back.
my schedule file
set :job_template, nil
job_type :rake, "cd :path && :environment_variable=:environment bundle exec rake :task :output"
every 15.minute, roles: [:db] do
rake "jobs:publish", output: lambda { "2>&1 >> /path/to/capistrano_directory/shared/log/jobs.log" }
end
My deploy/new_staging.rb file
set :branch, "develop"
set :deploy_env, 'staging'
set :rails_env, 'staging'
server "user#server_ip", :web, :app, :db, :metrics
role :db, "user#server_ip", :primary => true # This is where Rails migrations will run
ssh_options[:forward_agent] = false # use local SSH keys remotely
ssh_options[:paranoid] = false
set :use_sudo, false
set :deploy_to, "/path/to/capistrano_directory"
set :unicorn_conf, "#{deploy_to}/current/config/environments/#{deploy_env}/unicorn.rb"
set :unicorn_pid, "#{deploy_to}/shared/pids/unicorn.pid"
before 'deploy:finalize_update', 'set_current_release'
before "deploy:finalize_update", "deploy:update_crontab"
after 'deploy:update_code', 'deploy:symlink_db'
namespace :deploy do
task :symlink_db, :roles => :app do
run "ln -nfs #{deploy_to}/shared/config/database.yml #{release_path}/config/database.yml"
end
task :start, :roles => :app, :except => { :no_release => true } do
run "cd #{deploy_to}/current && bundle exec unicorn -c #{unicorn_conf} -E #{deploy_env} -D"
end
task :stop do
run "#{try_sudo} kill -QUIT `cat #{unicorn_pid}`"
end
task :restart, :roles => :app, :except => { :no_release => true } do
run "#{try_sudo} kill -s USR2 `cat #{unicorn_pid}`"
end
end
task :set_current_release, :roles => :app, :except => { :no_release => true } do
set :current_release, latest_release
end
And my deploy.rb
require 'bundler/capistrano'
require 'capistrano/ext/multistage'
set :whenever_command, "bundle exec whenever"
set :whenever_environment, defer { stage }
set :whenever_roles, [:db, :metrics]
require "whenever/capistrano"
set :stages, %w(production staging new_staging)
set :application, "###"
set :repository, "###"
set :deploy_via, :remote_cache
set :scm, :git
default_run_options[:shell] = '/bin/bash --login'
ssh_options[:forward_agent] = false
ssh_options[:paranoid] = false
namespace :deploy do
desc "Update the crontab file"
task :update_crontab do
run "cd #{release_path} && RAILS_ENV=#{fetch(:rails_env)} bundle exec whenever --update-crontab #{application} --set environment=#{fetch(:rails_env)}"
end
end
Whenever Version (0.9.7)
Capistrano Version (2.12.0)
What is causing the rollback to use the capistrano stage instead of the rails_env when running Whenever gem? And how can I get it to properly use the rails_env?
I'm a little confused by your staging setup (what is the difference between staging and new_staging?). That said, you should be able to do this by changing your whenever_environment to use rails_env instead of the stage value set by capistrano:
so
set :whenever_environment, defer { stage }
Would become:
set :whenever_environment, defer { rails_env }
whenever README
I'm using the gem Capistrano ~2.15.5 and deploying my application to my remote server. Everything works fine until the end of the cap prod deploy command. I'm receiving this error:
** [52.74.34.196 :: out] /bin/bash: ebs/apps/CloudTrendz/releases/20150318203140/REVISION: No such file or directory
** [52.74.34.196 :: out]
command finished in 4923ms
*** [deploy:update_code] rolling back
* executing "rm -rf ebs/apps/CloudTrendz/releases/20150318203140; true"
servers: ["52.74.34.196"]
[52.74.34.196] executing command
command finished in 411ms
failed: "/bin/bash --login -c 'git clone -b master git#github.com:prashant4224/CloudTrendz.git ebs/apps/CloudTrendz/releases/20150318203140 && cd ebs/apps/CloudTrendz/releases/20150318203140 && git checkout -b deploy 16cc51ebe1acd97cb489f10313d85f7ae9a69f6b && rm -Rf ebs/apps/CloudTrendz/releases/20150318203140/.git && (echo 16cc51ebe1acd97cb489f10313d85f7ae9a69f6b > ebs/apps/CloudTrendz/releases/20150318203140/REVISION)'" on 52.74.34.196
Here deploy.rb
require "bundler/capistrano"
set :application, "CloudTrendz"
set :repository, "git#github.com:prashant4224/CloudTrendz.git"
set :deploy_to, "ebs/apps/#{application}"
set :applicationdir, "ebs/apps/#{application}"
set :scm, :git # You can set :scm explicitly or Capistrano will make an intelligent guess based on known version control directory names
set :branch, "master"
set :deploy_to, applicationdir
set :deploy_via, :export
set :scm_verbose, true
set :user, "ubuntu"
set :use_sudo, false
set :rails_env, "production"
set :keep_releases, 2
set :precompile_only_if_changed, true
default_run_options[:pty] = true
default_run_options[:shell] = '/bin/bash --login'
ssh_options[:keys] = %w(~/.ssh/prashantec2.pem)
after "deploy:update_code", "deploy:copy_configs"
task :prod do
set :domain, "52.74.34.196"
set :repository, "git#github.com:prashant4224/CloudTrendz.git"
set :local_repository, "git#github.com:prashant4224/CloudTrendz.git"
set :branch, "master"
set :scm_verbose, true
server "52.74.34.196", :app, :web, :db, :primary => true
set :deploy_env, "prod"
"deploy"
end
namespace :deploy do
task :copy_configs, :roles => :app do
run "cp #{release_path}/../../shared/database.yml #{release_path}/config/database.yml"
end
task :migrate, :roles => :app do
run "cd #{release_path} && bundle exec rake db:migrate"
end
task :restart, :roles => :app, :except => { :no_release => true } do
run "#{try_sudo} touch #{File.join(current_path, 'tmp', 'restart.txt')}"
if deploy_env == 'prod'
tag_name = Time.now.strftime("deploy_%Y_%m_%d_%H_%M")
system "git tag -a -m 'Deployment on prod' #{tag_name}"
system "git push origin #{tag_name}"
if $? != 0
raise "Pushing tag to origin failed"
end
end
end
namespace :assets do
task :precompile, :roles => :web, :except => { :no_release => true } do
logger.info "Skipping asset pre-compilation because there were no asset changes"
end
end
end
after "deploy:update", "deploy:migrate", "deploy:cleanup"
What is wrong with this code?
Thank you in advance
It looks to me like it is failing when cloning the git repository from Github. Make sure that you've added the remote server's SSH key to the Github repository.
You can verify if this is the problem by SSH'ing into the remote server and attempting to run:
git clone git#github.com:prashant4224/CloudTrendz.git
If this fails, copy the contents of ~/.ssh/id_rsa.pub and then go to https://github.com/prashant4224/CloudTrendz/settings/keys, click "Add deploy key" and paste it in.
If ~/.ssh/id_rsa.pub does not exist on the remote server, run ssh-keygen to create it.
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 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