I have a Rails 3.2.14 legacy app in production and it's working fine. I'm migrating this app to a new server and will migrate the database from the production at time of switch over. So far I have the app spun up and it's working (for the most part). But when I use Capistrano to deploy to that server, the whenever gem doesn't seem to run (though it does on the original production server and updates crontab).
I'm using whenever gem 0.7.3, capistrano 2.12.0, Ruby 1.9.3p194 to keep the environment identical.
Here's what my deploy.rb in Capistrano looks like (and works on the old server to execute whenever:
require "bundler/capistrano"
set :whenever_command, "bundle exec whenever"
require "whenever/capistrano"
server "72.14.181.80", :web, :app, :db, primary: true
set :application, "appname"
set :user, "deploy"
set :deploy_to, "/home/#{user}/#{application}"
set :use_sudo, false
set :rails_env, "production"
set :scm, "git"
set :repository, "git#github.com:username/#{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
after "deploy:update", "gps_listener:restart"
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
desc "reload the database with seed data"
task :seed do
run "cd #{current_path}; bundle exec rake db:seed RAILS_ENV=#{rails_env}"
end
end
namespace :gps_listener do
task :start do
run "#{sudo} start app-gps"
end
task :stop do
run "#{sudo} stop app-gps"
end
task :restart do
run "#{sudo} stop app-gps && sleep 1 && #{sudo} start app-gps"
end
end
task :after_update_code do
run "ln -nfs #{deploy_to}/shared/config/database.yml #{release_path}/config/database.yml"
run "cd #{current_path}; RAILS_ENV=#{rails_env} bundle exec rake gps:listen"
end
What's interesting is, when I deploy to our old production server whenever will execute and update the crontab with the rake tasks. But if I switch the IP in deploy.rb and deploy to our new server it will not execute and the Capistrano output doesn't show anything fishy that I can see.
Is there somewhere I can debug this? I want to keep the Capistrano/Rails configuration the same for both servers. Just not sure what the problem is. Maybe I'm overlooking something.
It should also be noted, that when I'm on the new server in the app/current directory I can run the following and it will update my crontab properly:
RAILS_ENV=production bundle exec whenever -w
Related
I've been experiencing an issue where capistrano will deploy an old revision from our git repo, unless we specify the exact revision we want to deploy to.
#this will deploy a revision from a couple weeks ago
cap staging deploy:migrations
#this will deploy correctly a new revision
cap staging deploy:migrations -S revision=74d27c00363cdcd456942d6951230564893ccb28
Does anyone have an idea why this could be happening?
here is the cap deploy file:
set :rvm_ruby_string, 'ruby-1.9.3-p194#ac_helenefrance_01' # Or:
#set :rvm_ruby_string, ENV['GEM_HOME'].gsub(/.*\//,"") # Read from local system
require "rvm/capistrano" # Load RVM's capistrano plugin.
require "bundler/capistrano"
# set :verbose ,1
require 'capistrano/ext/multistage'
set :stages, %w(staging production)
set :default_stage, "staging"
set :user, "webm"
# set :deploy_via, :remote_cache
set :use_sudo, false
set :scm, "git"
set :repository, "git#aliencom.beanstalkapp.com:/ac_helenefrance_01.git"
# :branch is being set in stage files
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 "#{sudo} service unicorn_#{server_configuration} #{command}"
end
end
desc "build missing paperclip styles"
task :build_missing_paperclip_styles, :roles=> :app do
run "cd #{release_path}; RAILS_ENV=production bundle exec rake paperclip:refresh:missing_styles"
end
after "deploy:update", "deploy:build_missing_paperclip_styles"
task :setup_config, roles: :app do
puts "#making symlink to nginx sites-enabled"
run "#{sudo} ln -fs #{current_path}/config/server/#{server_configuration}/nginx.conf /etc/nginx/sites-enabled/#{server_configuration}"
puts "#making symlink to unicorn service script"
run "#{sudo} ln -fs #{current_path}/config/server/#{server_configuration}/unicorn_init.sh /etc/init.d/unicorn_#{server_configuration}"
puts "#making a the new config directory"
run "mkdir -p #{shared_path}/config"
run "sunique 1"
put File.read("config/database.yml"), "#{shared_path}/config/database.yml"
run "sunique 0"
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"
puts "#for reference:"
puts "#rvm wrapper 1.9.3#ac_helenefrance_01 ruby-1.9.3-p194##{server_configuration} unicorn cap"
puts "#now be sure to run: sudo update-rc.d unicorn_#{server_configuration} defaults"
end
after "deploy:finalize_update", "deploy:symlink_config"
end
and the environment/stage.rb for the default multistage env.
server "xxx.xxx.xxx.xxx", :web, :app, :db, primary: true
set :branch, "sitemap"
set :isRemote, true
set :server_configuration, "st_ac_helenefrance_01"
set :application, "#{server_configuration}"
set :deploy_to, "/home/#{user}/#{server_configuration}"
It was setup to an old branch >:(
My colleague accidentally committed a test he was doing.
How do i setup queue_classic background jobs on EC2 using the rubber gem?
I tried using foreman export but i'm not sure where to run it (app or web role?).
My failed attempt is using http://blog.sosedoff.com/2011/07/24/foreman-capistrano-for-rails-3-applications/.
Should i be creating a new instance to run these jobs? (or a new role?)
Thanks for the help!
Figured it out.
First create a Procfile with the queue_classic rake task (see http://blog.daviddollar.org/2011/05/06/introducing-foreman.html)
Then i added foreman to my host (make sure you have a procfile for that environment, ex: Procfile.production)
# Foreman tasks
namespace :foreman do
desc 'Export the Procfile to Ubuntu upstart scripts'
task :export, :roles => :queue do
run "cd #{release_path} && bundle exec foreman export upstart /etc/init -f ./Procfile.#{Rubber.env} -a #{application} -u #{user} -l #{release_path}/log/foreman"
end
desc "Start the application services"
task :start, :roles => :queue do
rsudo "start #{application}"
end
desc "Stop the application services"
task :stop, :roles => :queue do
rsudo "stop #{application}"
end
desc "Restart the application services"
task :restart, :roles => :queue do
rsudo "stop #{application}; start #{application}"
#run "sudo start #{application} || sudo restart #{application}"
end
end
after "deploy:update", "foreman:export" # Export foreman scripts
before "deploy:restart", "foreman:restart" # Restart application scripts
after "deploy:stop", "foreman:stop" # Restart application scripts
I have a project rails 3.1 in production environment.
This is my deploy.rb now:
$:.unshift(File.expand_path('./lib', ENV['rvm_path'])) # Add RVM's lib directory to the load pathe
require "rvm/capistrano" # Load RVM's capistrano plugin.
require "bundler/capistrano"
set :rvm_ruby_string, 'ruby-1.9.2-p318#global'
set :rvm_type, :user
set :application, "domain.com"
set :user, 'user'
#set :repository, "#{user}#ip.ip.ip.ip:~/app"
set :repository, "ssh://git#bitbucket.org/user/app.git"
set :keep_releases, 3
set :scm, :git
set :use_sudo, false
set :deploy_to, "~/#{application}"
#set :deploy_via, :copy
set :unicorn_conf, "#{deploy_to}/current/config/unicorn.rb"
set :unicorn_pid, "#{deploy_to}/shared/pids/unicorn.pid"
set :deploy_via, :remote_cache
ssh_options[:forward_agent] = true
default_run_options[:pty] = true
role :web, "ip.ip.ip.ip" # Your HTTP server, Apache/etc
role :app, "ip.ip.ip.ip" # This may be the same as your `Web` server
role :db, "ip.ip.ip.ip", :primary => true # This is where Rails migrations will run
namespace :deploy do
task :restart do
run "if [ -f #{unicorn_pid} ] && [ -e /proc/$(cat #{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 "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
load 'deploy/assets'
after "deploy:restart", "deploy:cleanup"
I want make these tasks in capistrano. Now I perform these tasks manually:
1º I kill sunspot solr pid with:
a) Find the pid with ps aux | grep 'solr'
b) Kill pid with kill pid_number
2º Remove index solr in production environment if exist with:
a) rm -r solr/data/production/index
3º turn on sunspot solr with:
a) RAILS_ENV=production rake sunspot:solr:start
4º Reindex models with:
a) RAILS_ENV=production rake sunspot:mongo:reindex
My question is:
How can I add these tasks to my deploy.rb?
Thank you!
This might be a good starting point:
namespace :solr do
task :reindex do
run "cd #{current_path} && #{rake} RAILS_ENV=#{rails_env} sunspot:solr:reindex"
end
end
You can just call the rake sunspot:solr:stop instead of kill? I'm not sure you need to remove the index if you are going to do a reindex...
task :stop_solr do
begin
run("cd #{deploy_to}/current && /usr/bin/env rake sunspot:solr:stop RAILS_ENV=#{rails_env}")
rescue Exception => error
puts "***Unable to stop Solr with error: #{error}"
puts "***Solr may have not been started. Continuing anyway.***"
end
end
#restart and reindex any newly added full search fields:
task :restart_solr do
begin
run("cd #{release_path} && /usr/bin/env rake sunspot:solr:start RAILS_ENV=#{rails_env}")
rescue Exception => error
puts "***Unable to start Solr with error: #{error}."
puts "***Continuing anyway.***"
end
end
task :reindex_solr do
begin
run("cd #{release_path} && /usr/bin/env rake sunspot:reindex RAILS_ENV=#{rails_env}")
rescue Exception => error
puts "***Unable to reindex Solr with error: #{error}"
puts "***Continuing anyway.***"
end
end
Also, as I mentioned in my comment to Kris's answer, you'll have issues if you clean up old Capistrano directories, unless you kill the SOLR process and force it to point to new index files. One way to avoid this scenario is to set up SOLR in a shared directory and reset the symlink during deployment.
I want to deploy a website the first time using Rails 3.2.1 with capistrano 2.8.0.
When it comes to "rake db:migrate" the process is aborted:
* executing `deploy:migrate'
* executing "cd /home/user/public_html/website.com/releases/20120323114534 && bundle exec rake RAILS_ENV=production db:migrate"
servers: ["23.11.63.203"]
[23.11.63.203] executing command
** [out :: 23.11.63.203] rake aborted!
** [out :: 23.11.63.203] No such file or directory - /home/user/public_html/website.com/releases/20120323114534/log/production.log
I'm deploying via github, here is relevant part of my deploy.rb:
require 'bundler/capistrano'
require 'whenever/capistrano'
set :rails_env, 'production'
set :application, "website.com"
set :domain, "website.com"
set :user, "user"
set :sudo_use, false
set :repository, "git#github.com:User/website.com.git"
set :local_repository, '~/rails_projects/website.com/.git'
set :deploy_to, "/home/user/public_html/#{application}"
set :scm, :git
set :branch, "master"
default_run_options[:pty] = true
ssh_options[:forward_agent] = true
server "23.11.63.203", :app, :web, :db, :primary => true
after "deploy", "deploy:bundle_gems"
after "deploy:bundle_gems", "deploy:restart"
after "deploy:update_code", "deploy:migrate"
namespace :deploy do
task :bundle_gems do
run "cd #{deploy_to}/current && bundle install vendor/gems"
end
...(there are some more tasks, but I think they cannot be relevant)
end
I tried the following in my deploy.rb, but it was not working, so maybe it's the wrong way to resolve this:
after "deploy", "deploy:create_production_log"
namespace :deploy do
desc "Creates the production log if it does not exist"
task :create_production_log do
unless File.exist?(File.join(shared_path, 'log', 'production.log'))
puts "\n\n=== Creating Production Log! ===\n\n"
run "touch #{File.join(shared_path, 'log', 'production.log')}"
end
end
end
How can I do my first deploy in this case?
Did you perform cap deploy:setup and does cap deploy:check tells you everything's ok ?
Otherwise, I'd say it's because the log dir doesn't exist, probably because you don't have it under version control
I'm trying to deploy a simple Ruby on Rails app to dreamhost but I'm getting an error related to the bundle command. Here is the error:
servers: ["bullseye.dreamhost.com"]
[bullseye.dreamhost.com] executing command
command finished
* executing "find /home/USER-NAME/MY-DOMAIN/releases/20110123014150/public/images /home/USER-NAME/MY-DOMAIN/releases/20110123014150/public/stylesheets /home/USER-NAME/MY-DOMAIN/releases/20110123014150/public/javascripts -exec touch -t 201101230141.52 {} ';'; true"
servers: ["bullseye.dreamhost.com"]
[bullseye.dreamhost.com] executing command
command finished
triggering after callbacks for `deploy:update_code'
* executing `bundle:install'
* executing "ls -xt /home/USER-NAME/MY-DOMAIN/releases"
servers: ["bullseye.dreamhost.com"]
[bullseye.dreamhost.com] executing command
command finished
* executing "bundle install --gemfile /home/USER-NAME/MY-DOMAIN/releases/20110123014150/Gemfile --path /home/USER-NAME/MY-DOMAIN/shared/bundle --deployment --quiet --without development test"
servers: ["bullseye.dreamhost.com"]
[bullseye.dreamhost.com] executing command
*** [err :: bullseye.dreamhost.com] sh: bundle: command not found
command finished
*** [deploy:update_code] rolling back
* executing "rm -rf /home/USER-NAME/MY-DOMAIN/releases/20110123014150; true"
servers: ["bullseye.dreamhost.com"]
[bullseye.dreamhost.com] executing command
command finished
failed: "sh -c 'bundle install --gemfile /home/USER-NAME/MY-DOMAIN/releases/20110123014150/Gemfile --path /home/USER-NAME/MY-DOMAIN/shared/bundle --deployment --quiet --without development test'" on bullseye.dreamhost.com
Here is my deploy.rb file.
require 'bundler/capistrano'
set :user, "MY-USERNAME"
set :password, "MY-PASSWORD"
set :domain, 'bullseye.dreamhost.com' # Dreamhost servername where your account is located
set :project, 'blog' # Your application as its called in the repository
set :application, 'MY-DOMAIN' # Your app's location (domain or sub-domain name as setup in panel)
set :applicationdir, "/home/#{user}/#{application}" # The standard Dreamhost setup
# version control config
set :scm_username, 'MY-SVN-USERNAME'
set :scm_password, 'MY-SVN-PWD'
set :repository, "http://MY-SVN-URL/01/blog/"
# roles (servers)
role :web, domain
role :app, domain
role :db, domain, :primary => true
# deploy config
set :deploy_to, applicationdir
set :deploy_via, :export
# additional settings
default_run_options[:pty] = false # Forgo errors when deploying from windows
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
end
I found a similar question at Bundle install failing when deploying a Rails 3 app to Dreamhost with Capistrano but the solution didn't work. Anybody have any ideas as to what might be happening in my case?
I got this figured out eventually. What I did was
ssh into the dreamhost server and execute which bundle command
from ssh session execute echo $PATH command
edit config/deploy.rb and combine both strings with a : between and place inside default_environment PATH value, see below
set :default_environment, {
'PATH' => "'/usr/lib/ruby/gems/1.8/bin//bundle:/home/sayed3/.gems/bin:/usr/lib/ruby/gems/1.8/bin/:/usr/local/bin:/usr/bin:/bin:/usr/bin/X11:/usr/games'"
}
Once I did this it was able to execute the bundle command successfully, but then I started running into some other issues. I eventually decided to use a VPS that I have hosted elsewhere, but I'm sure if I spent a bit more time I could have figured it out.
Besides adding the path to bundle, as specified above, I also had to add the following line to my config/deploy.rb in order to force capistrano to use bash, instead of the default shell, which, on dreamhost, is dash:
set :shell, '/bin/bash'