Ruby on Rails: Capistrano 3 deployment error - ruby-on-rails

I'm trying to deploy my rails 4 application in development mode. When I try cap deploy:setup, it says staging is not set. Next I tried cap development deploy according to its suggestion and got the following error. Please help me with the deployment.
command: cap development deploy
cap aborted!
wrong number of arguments (5 for 1..2)
/home/divya/.rvm/gems/ruby-2.1.0/gems/capistrano-3.1.0/lib/capistrano/dsl/env.rb:38:in `server'
config/deploy/development.rb:5:in `<top (required)>'
/home/divya/.rvm/gems/ruby-2.1.0/gems/capistrano-3.1.0/lib/capistrano/setup.rb:15:in `load'
/home/divya/.rvm/gems/ruby-2.1.0/gems/capistrano-3.1.0/lib/capistrano/setup.rb:15:in `block (2 levels) in <top (required)>'
/home/divya/.rvm/gems/ruby-2.1.0/gems/capistrano-3.1.0/lib/capistrano/application.rb:15:in `run'
/home/divya/.rvm/gems/ruby-2.1.0/gems/capistrano-3.1.0/bin/cap:3:in `<top (required)>'
/home/divya/.rvm/gems/ruby-2.1.0/bin/cap:23:in `load'
/home/divya/.rvm/gems/ruby-2.1.0/bin/cap:23:in `<main>'
/home/divya/.rvm/gems/ruby-2.1.0/bin/ruby_executable_hooks:15:in `eval'
/home/divya/.rvm/gems/ruby-2.1.0/bin/ruby_executable_hooks:15:in `<main>'
Tasks: TOP => development
(See full trace by running task with --trace)
Using the following files:
deploy.rb
set :application, 'timeTracker'
set :scm, :git
set :repository, "git#github.com:p/project.git"
set :user, "r"
set :scm_passphrase, "r"
set :branch, "master"
set :deploy_via, :remote_cache
set :stages, ["staging", "development", "production"]
set :default_stage, "development"
#set :rvm_ruby_string, :local
#before 'deploy:setup', 'rvm:install_rvm'
#set :default_shell, "/bin/bash -l"
#set :rvm_type, :user
#set :rvm_install_with_sudo, true
#default_run_options[:pty] = true
#ssh_options[:forward_agent] = true
#ssh_options[:auth_methods] = "publickey"
# ssh_options[:keys] = "il.pem"
# set :ssh_options, {:auth_methods => "publickey"}
# set :ssh_options, {:keys => ["/home/c/mbk.pem"]}
# default_environment['PATH'] = '/usr/local/rvm/gems/ruby-1.9.3-p392:$PATH'
# default_environment['GEM_PATH']= '/usr/local/rvm/gems/ruby-1.9.3-p392'
# set :location, "http://ec2-23-23-59-41.compute-1.amazonaws.com"
# set :use_sudo, false
# ssh_options[:keys] = ["/home/c/ec2/mbk.pem"]
config/development.rb
require "rvm/capistrano"
require 'capistrano/bundler'
server "IP", :app, :web, :db, :primary => true
set :deploy_to, "/var/www/t/"
set :branch, 'master'
set :scm_verbose, true
set :use_sudo, false
set :rails_env, "development" #added for delayed job
set :rvm_type, :system
after 'deploy:update_code' do
# run "cd #{release_path}; RAILS_ENV=production rake assets:precompile"
run "cd #{release_path};"
run "mkdir -p #{release_path}/tmp/cache;"
run "chmod -R 777 #{release_path}/tmp/cache;"
run "mkdir -p #{release_path}/public/uploads;"
run "chmod -R 777 #{release_path}/public/uploads"
run "rm -rf #{release_path}/public/system"
# run "unlink #{release_path}/public/db_admin"
# run "unlink #{release_path}/public/blog"
run "ln -s #{shared_path}/system/ #{release_path}/public/"
run "ln -s '/var/www/blog' #{release_path}/public/"
run "mv #{release_path}/config/database.example.yml #{release_path}/config/database.yml"
run "cd #{release_path} && bundle install"
run "cd #{release_path} && rake db:create"
run "cd #{release_path} && rake db:migrate"
# run "cd #{release_path} && RAILS_ENV=production rake assets:precompile"
# run "chown -R www-data:www-data #{release_path}/*"
# run "chmod -R 777 #{release_path}/log"
end
namespace :deploy do
task :restart, :roles => :app, :except => { :no_release => true } do
run "#{try_sudo} touch #{File.join(current_path,'tmp','restart.txt')}"
end
end

wrong number of arguments (5 for 1..2)
/home/divya/.rvm/gems/ruby-2.1.0/gems/capistrano-3.1.0/lib/capistrano/dsl/env.rb:38:in `server'
It's telling you you're passing too many arguments to server. Try replacing the first line with the second:
server "151.236.218.85", :app, :web, :db, :primary => true
server "151.236.218.85", roles: [:app, :web, :db], :primary => true
It's also possible to use roles: %w(app web db) if you find that cleaner.

Related

Capistrano Rollback breaking Whenever Cronjob

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

Can't stop process, no PID found in tmp/pids/thin.8080.pid (Thin::PidFileNotFound)

I am installing app from my private git repository. I installed all dependencies and I am using Capistrano.I am able to run app successfully on my local machine.I am using rails -v 3.2.14 and ruby -v ruby 1.9.3p362 and linux server
when I am running cap production deploy I am getting error
......
....
sftp upload complete
* executing "cd /var/www/vhosts/..../apps/login/current && bundle exec thin -C /var/www/vhosts/....../apps/login/current/thin.yml -O restart"
servers: ["......net"]
[root#emerge126.mysitehosting.net] executing command
** [out :: root#........net] Stopping server on 127.0.0.1:8080 ...
** [out :: root#........net] /var/www/vhosts/......./apps/.../shared/bundle/ruby/1.9.1/gems/thin-1.5.0/lib/thin/daemonizing.rb:131:in 'send_signal': Can't stop process, no PID found in tmp/pids/thin.8080.pid (Thin::PidFileNotFound)
My deploy.rb file require
'bundler/capistrano'
require 'capistrano/ext/multistage'
require 'capistrano-thin'
set :stages, ["staging", "production"]
set :default_stage, "production"
default_run_options[:pty] = true
ssh_options[:forward_agent] = true
set :ssh_options, {
config: false
}
set :application, "myapp"
set :repository, "https://........git"
set :scm, :git
set :deploy_via, :remote_cache
set :branch, 'master'
set :deploy_to, "/var/www/vhosts/......../apps/#{application}"
set :keep_releases, 5
set :use_sudo, false
set :thin_servers, 1
set :thin_port, 8081
namespace :deploy do
namespace :db do
desc "Seed the database"
task :seed, :roles => :db do
# on_rollback { deploy.db.restore }
run_remote_rake('db:seed')
end
end
task :symlink_configs do
run "ln -nfs #{shared_path}/config/database.yml #{release_path}/config/database.yml"
run "ln -nfs #{shared_path}/config/auth.yml #{current_path}/config/auth.yml"
end
end
before "deploy:restart", "deploy:symlink_configs"
after "deploy:update", "deploy:cleanup"
def run_remote(cmd)
run "cd #{current_path} && #{cmd}"
end
def run_remote_rake(task)
rails_env = fetch(:rails_env, 'integration')
run_remote("rake #{task} RAILS_ENV=#{rails_env}")
end
My production.rb file
set :deploy_to, "/var/www/vhosts/....../apps/# {application}"
server 'root#.......net', :web, :app, :db, :primary => true
set :thin_port, 8081
set :rails_env, 'production'
I need some help. Its a multi rails app server. Please ask any more info if you need. I have also replaced site and github url with .... because of site security.
There is no thin process running on your server, that's why you're getting the Can't stop process, no PID found error. I suggest starting thin manually so your deploy script can then successfully restart it.

Can't deploy with capistrano

When I try to deploy with capistrano, i'hve got an error :
cap production deploy:setup
I've this error message :
/home/paul/.rbenv/versions/2.0.0-p247/lib/ruby/2.0.0/rubygems/core_ext/kernel_require.rb:45:in `require': /home/paul/.rbenv/versions/2.0.0-p247/lib/ruby/gems/2.0.0/gems/capistrano-2.0.0/lib/capistrano/gateway.rb:55: formal argument cannot be an instance variable (SyntaxError)
SSH.connect(server, #options) do |#session|
^
from /home/paul/.rbenv/versions/2.0.0-p247/lib/ruby/2.0.0/rubygems/core_ext/kernel_require.rb:45:in `require'
from /home/paul/.rbenv/versions/2.0.0-p247/lib/ruby/gems/2.0.0/gems/capistrano-2.0.0/lib/capistrano/configuration/connections.rb:1:in `<top (required)>'
from /home/paul/.rbenv/versions/2.0.0-p247/lib/ruby/2.0.0/rubygems/core_ext/kernel_require.rb:45:in `require'
from /home/paul/.rbenv/versions/2.0.0-p247/lib/ruby/2.0.0/rubygems/core_ext/kernel_require.rb:45:in `require'
from /home/paul/.rbenv/versions/2.0.0-p247/lib/ruby/gems/2.0.0/gems/capistrano-2.0.0/lib/capistrano/configuration.rb:4:in `<top (required)>'
from /home/paul/.rbenv/versions/2.0.0-p247/lib/ruby/2.0.0/rubygems/core_ext/kernel_require.rb:45:in `require'
from /home/paul/.rbenv/versions/2.0.0-p247/lib/ruby/2.0.0/rubygems/core_ext/kernel_require.rb:45:in `require'
from /home/paul/.rbenv/versions/2.0.0-p247/lib/ruby/gems/2.0.0/gems/capistrano-2.0.0/lib/capistrano.rb:1:in `<top (required)>'
from /home/paul/.rbenv/versions/2.0.0-p247/lib/ruby/2.0.0/rubygems/core_ext/kernel_require.rb:45:in `require'
from /home/paul/.rbenv/versions/2.0.0-p247/lib/ruby/2.0.0/rubygems/core_ext/kernel_require.rb:45:in `require'
from /home/paul/.rbenv/versions/2.0.0-p247/lib/ruby/gems/2.0.0/gems/capistrano-2.0.0/lib/capistrano/cli.rb:1:in `<top (required)>'
from /home/paul/.rbenv/versions/2.0.0-p247/lib/ruby/2.0.0/rubygems/core_ext/kernel_require.rb:45:in `require'
from /home/paul/.rbenv/versions/2.0.0-p247/lib/ruby/2.0.0/rubygems/core_ext/kernel_require.rb:45:in `require'
from /home/paul/.rbenv/versions/2.0.0-p247/lib/ruby/gems/2.0.0/gems/capistrano-2.0.0/bin/cap:3:in `<top (required)>'
from /home/paul/.rbenv/versions/2.0.0-p247/bin/cap:23:in `load'
from /home/paul/.rbenv/versions/2.0.0-p247/bin/cap:23:in `<main>'
Here is my Capfile :
load 'deploy' if respond_to?(:namespace) # cap2 differentiator
load 'config/deploy'
And my deploy.rb :
require "bundler/capistrano"
# allowing shell interactions
default_run_options[:pty] = true
# multistaging
set :stages, %w(staging production)
set :default_stage, "staging"
require 'capistrano/ext/multistage'
set :application, "myapp"
set :user, "myapp"
set :repository, "git#gitlab.conicrea.com:conicrea/myapp.git"
set :ssh_options, { :forward_agent => true }
set :deploy_to, "/var/www/#{application}"
set :scm, :git
set :deploy_via, :remote_cache
# number of releases we want to keep
set :keep_releases, 3
set :use_sudo, false
# default rails_env, should be overrided in config/deploy/#{environnement}.rb
set :rails_env, "staging"
# unicorn informations
set :unicorn_config, "#{current_path}/config/unicorn.rb"
set :unicorn_pid, "#{shared_path}/pids/unicorn.pid"
set :unicorn_bin, "#{current_path}/bin/unicorn"
# useful for rbenv
set :default_environment, {
'PATH' => "/home/synbioz/.rbenv/shims:/home/synbioz/.rbenv/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin"
}
namespace :maintenance do
task :start do
run "ln -nfs #{shared_path}/system/_maintenance.html #{shared_path}/system/maintenance.html"
end
task :stop do
run "rm -f #{shared_path}/system/maintenance.html"
end
end
namespace :deploy do
task :start, :roles => :app, :except => { :no_release => true } do
run <<-CMD
cd #{current_path} && #{unicorn_bin} -c #{unicorn_config} -E #{rails_env} -D
CMD
end
task :force_stop, :roles => :app, :except => { :no_release => true } do
run <<-CMD
if [ -e #{unicorn_pid} ]; then
kill -9 $(cat #{unicorn_pid});
fi
CMD
end
task :stop, :roles => :app, :except => { :no_release => true } do
run <<-CMD
if [ -e #{unicorn_pid} ]; then
kill $(cat #{unicorn_pid});
fi
CMD
end
task :graceful_stop, :roles => :app, :except => { :no_release => true } do
run <<-CMD
if [ -e #{unicorn_pid} ]; then
kill -s QUIT $(cat #{unicorn_pid});
fi
CMD
end
task :reload, :roles => :app, :except => { :no_release => true } do
run <<-CMD
if [ -e #{unicorn_pid} ]; then
kill -s USR2 $(cat #{unicorn_pid});
fi
CMD
end
task :restart, :roles => :app, :except => { :no_release => true } do
run <<-CMD
if [ -e #{unicorn_pid} ]; then
kill -9 $(cat #{unicorn_pid});
sleep 5;
cd #{current_path} && #{unicorn_bin} -c #{unicorn_config} -E #{rails_env} -D;
fi
CMD
end
end
desc "Create shared folders."
after 'deploy:setup', :roles => :app do
# for unicorn
run "mkdir -p #{shared_path}/sockets"
run "mkdir -p #{shared_path}/config"
# only for sqlite
run "mkdir -p #{shared_path}/db"
end
desc "Link db.yml."
after 'deploy:update_code', :roles => :app do
run "ln -nfs #{shared_path}/config/database.yml #{release_path}/config/database.yml"
end
desc "Seed datas when cold deploying."
after 'deploy:migrate', :roles => :app, :only => { :no_release => true } do
run "cd #{release_path} && bin/rake db:seed RAILS_ENV=#{rails_env}"
end
desc "Link maintenance, rbenv and precompile assets."
after 'deploy:symlink', :roles => [:web, :app] do
run "ln -nfs #{shared_path}/config/.rbenv-version #{release_path}/config/.rbenv-version"
run "cp #{release_path}/public/_maintenance.html #{shared_path}/system/"
run "cd #{release_path}; RAILS_ENV=#{rails_env} bin/rake assets:precompile"
end
desc "remove unused releases."
after "deploy", "deploy:cleanup"
I don't know what to do !
Can somebody help me ?
Thanks a lot !
That is a very old version of Capistrano (2.0.0 is from July 2007, and doesn't support changes in Ruby 1.9/2.0 -- in this case, Ruby 1.9 no longer allowed using instance variables as arguments to blocks).
You'll need to upgrade to the newest 2.x release (2.15.5).
You could also upgrade to Capistrano v3, but keep in mind it was a complete rewrite and your existing configuration will not be compatible with v3 without some work.

Assets won't precompile when deploying with capistrano to production on amazon EC2

I worked on being able to deploy to production using capistrano. I face several issues and while fixing most of them we still have a last one.
Our precompile assets options are not properly compiling them on production and because of that, we are unable to use the last developed features as they rely heavily on JS.
Without trying to influence on how anyone would analyze this problem, this is some of what I did trying to make it work:
Precompiled assets locally, pushed to github repo, cap deployed from local machines to ec2. cap deploy is local, the code being pushed to ec2 is the one on github.
Tried using capistrano tasks as suggested. Using load 'deploy'assets' in the Capfile and letting the cap deploy:setup task do its thing.
Used the option cap deploy:assets:clean and then cap deploy:assets:precompile
Tried removing assets from public and then use a pipeline_precompile task in deploy.rb
Expired assets, forcing rails to precompile everything changing assets.versions in application.rb
Tried different combinations on config.assets in environments/production.rb
Finally, tried deleting public/assets in production and precompiling up there using RAILS_ENV=production bundle exec rake assets:precompile
The app is just not using the new JS files. If you check the code either on the repo or in the server itself, I introduced a simple comment to the name.js.coffee ("# Shows and hides menus depending on the data on DB" on line xxx) and this is not in the compiled assets.js in production. This is a quick test to be sure the recent assets are being used.
The whole problem here is the js and css files, not so much rails. Which is why it is so difficult to test or find.. Thus one of the reasons for the popularity of js frameworks lately. In case of problems, you don't have to kill yourself looking for where the problem is.. If the prob is in ruby or rails, usually doesn't take that long to find out. Once you get to js, css and cross browser compatibility, well, this IS the problem at hand.
Here's my deploy.rb file. Running rails 3.2.12 ruby-1.9.3-p327:
# $:.unshift(File.expand_path('./lib', ENV['rvm_path']))
# Load rvm's capistrono plugins
require 'rvm/capistrano'
require 'bundler/capistrano'
set :rvm_type, :user
set :user, 'username'
set :domain, 'ip_address'
set :application, "app_pro"
set :keep_releases, 2 # It keeps on two old releases.
# git repo details
set :scm, :git # You can set :scm explicitly or Capistrano will make an intelligent guess based on known version control directory names
set :repository, "git#github.com:user/app.git"
set :scm_username, 'user'
set :git_enable_submodules, 1
set :git_shallow_clone, 1
set :branch, 'master'
# Or: `accurev`, `bzr`, `cvs`, `darcs`, `git`, `mercurial`, `perforce`, `subversion` or `none`
role :web, domain # Your HTTP server, Apache/etc
role :app, domain # This may be the same as your `Web` server
role :db, domain, :primary => true# 'ec2-23-23-156-118.compute-1.amazonaws.com' This is where Rails migrations will run
# role :db, "your slave db-server here"
# deply options
default_run_options[:pty] = true
set :ssh_options, {:forward_agent => true}
set :ssh_options, {:auth_methods => "publickey"}
set :ssh_options, {:keys => ["~/Downloads/key.pem"]}
set :deploy_to, "/home/user/appdir"
set :deploy_via, :remote_cache
set :use_sudo, false
# if you want to clean up old releases on each deploy uncomment this:
after "deploy:restart", "deploy:cleanup"
# if you're still using the script/reaper helper you will need
# these http://github.com/rails/irs_process_scripts
# If you are using Passenger mod_rails uncomment this:
namespace :deploy do
task :start do
# run COMMAND="/etc/init.d/nginx restart" invoke SUDO=1
run "sudo /etc/init.d/nginx restart"
# exit
end
after "deploy:start", "deploy:cleanup"
task :stop do ; end
task :restart, :roles => :app, :except => { :no_release => true } do
run "touch #{File.join(current_path,'tmp','restart.txt')}"
end
task :setup_config, roles: :app do
run "mkdir -p #{shared_path}/config"
put File.read("config/database.example.yml"), "#{shared_path}/config/database.yml"
puts 'now edit the config file database in #{shared_path}'
end
after 'deploy:setup', 'deploy:setup_config'
desc "Symlink shared resources on each release - not used"
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 "It helps to seed database with values"
task :seed do
run "cd #{current_path}; bundle exec rake db:seed RAILS_ENV=#{rails_env}"
end
task :create_schema do
run "cd #{current_path}; bundle exec rake db:create RAILS_ENV=#{rails_env} --trace"
end
end
On-working new/alternative (deploy_new2.rb) file:
# On-working new/alternative deploy.rb file:
require 'rvm/capistrano'
require 'bundler/capistrano'
set :rvm_type, :user
set :application, "ip_address"
set :domain, 'ip_address'
# Roles
role :web, domain
role :app, domain
role :db, domain, :primary => true
#deployment details
set :deploy_via, :remote_cache
set :user, "username"
set :copy_compression, :bz2
set :git_shallow_clone, 1
set :scm_verbose, true
set :use_sudo, false
set :deploy_to, "/home/user/dir"
default_run_options[:pty] = true
set :ssh_options, {:forward_agent => true}
set :ssh_options, {:auth_methods => "publickey"}
set :ssh_options, {:keys => ["~/Downloads/key.pem"]}
#repo details
set :scm, :git
set :repository, "git#github.com:user/app.git"
set :scm_username, 'user'
set :keep_releases, 2
set :branch, "master"
namespace :deploy do
# task :start, :roles => :app, :except => { :no_release => true } do
# # not need to restart nginx every time
# # run "service nginx start"
# run "cd #{release_path} && touch tmp/restart.txt"
# end
# after "deploy:start", "deploy:cleanup"
# after 'deploy:cleanup', 'deploy:symlink_config'
# You do not need reload nginx every time, eventhought if you use passenger or unicorn
# task :stop, :roles => :app, :except => { :no_release => true } do
# run "service nginx stop"
# end
# task :graceful_stop, :roles => :app, :except => { :no_release => true } do
# run "service nginx stop"
# end
# task :reload, :roles => :app, :except => { :no_release => true } do
# run "cd #{release_path} && touch tmp/restart.txt"
# run "service nginx restart"
# end
task :restart, :roles => :app, :except => { :no_release => true } do
run "cd #{release_path} && touch tmp/restart.txt"
end
# If you enable assets/deploy in Capfile, you do not need this
# task :pipeline_precompile do
# # run "cd #{release_path}; RAILS_ENV=#{rails_env} bundle exec rake assets:precompile"
# # precompile assets before deploy and upload them to server
# # run_locally("RAILS_ENV=#{rails_env} rake assets:clean && RAILS_ENV=#{rails_env} rake assets:precompile")
# # top.upload "public/assets", "#{release_path}/public/assets", :via =>:scp, :recursive => true
# end
end
# you do not need to this, because you already add require 'bundler/capistrano'
# before "deploy:assets:precompile", "bundle:install"
And ./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
Thank you in advance for any help! Let me know if you need more info.
You don't need your own :precompile_assets task. You are using Capistrano's by including load 'deploy/assets' in your Capfile.
Removing the :precompile_assets task from your deploy.rb may resolve the issue. If you look at the source code for Capistrano, you'll see it implements :precompile_assets quite differently: https://github.com/capistrano/capistrano/blob/legacy-v2/lib/capistrano/recipes/deploy/assets.rb
You can try this code
# On-working new/alternative deploy.rb file:
require 'rvm/capistrano'
require 'bundler/capistrano'
set :rvm_type, :user
set :application, "ip_address"
set :domain, 'ip_address'
# Roles
role :web, domain
role :app, domain
role :db, domain, :primary => true
#deployment details
set :deploy_via, :remote_cache
set :user, "username"
set :copy_compression, :bz2
set :git_shallow_clone, 1
set :scm_verbose, true
set :use_sudo, false
set :deploy_to, "/home/user/dir"
default_run_options[:pty] = true
set :ssh_options, {:forward_agent => true}
set :ssh_options, {:auth_methods => "publickey"}
set :ssh_options, {:keys => ["~/Downloads/key.pem"]}
#repo details
set :scm, :git
set :repository, "git#github.com:user/app.git"
set :scm_username, 'user'
set :keep_releases, 2
set :branch, "master"
namespace :deploy do
# task :start, :roles => :app, :except => { :no_release => true } do
# # not need to restart nginx every time
# # run "service nginx start"
# run "cd #{release_path} && touch tmp/restart.txt"
# end
# after "deploy:start", "deploy:cleanup"
# after 'deploy:cleanup', 'deploy:symlink_config'
# You do not need reload nginx every time, eventhought if you use passenger or unicorn
# task :stop, :roles => :app, :except => { :no_release => true } do
# run "service nginx stop"
# end
# task :graceful_stop, :roles => :app, :except => { :no_release => true } do
# run "service nginx stop"
# end
# task :reload, :roles => :app, :except => { :no_release => true } do
# run "cd #{release_path} && touch tmp/restart.txt"
# run "service nginx restart"
# end
task :restart, :roles => :app, :except => { :no_release => true } do
run "cd #{release_path} && touch tmp/restart.txt"
end
# If you enable assets/deploy in Capfile, you do not need this
# task :pipeline_precompile do
# # run "cd #{release_path}; RAILS_ENV=#{rails_env} bundle exec rake assets:precompile"
# # precompile assets before deploy and upload them to server
# # run_locally("RAILS_ENV=#{rails_env} rake assets:clean && RAILS_ENV=#{rails_env} rake assets:precompile")
# # top.upload "public/assets", "#{release_path}/public/assets", :via =>:scp, :recursive => true
# end
end
# you do not need to this, because you already add require 'bundler/capistrano'
# before "deploy:assets:precompile", "bundle:install"
Solution: Here's the working deploy file-
require 'rvm/capistrano'
require 'bundler/capistrano'
set :rvm_type, :user
set :application, "ip_address"
set :domain, 'ip_address'
# Roles
role :web, domain
role :app, domain
role :db, domain, :primary => true
#deployment details
set :deploy_via, :remote_cache
set :user, "user"
set :copy_compression, :bz2
set :git_shallow_clone, 1
set :scm_verbose, true
set :use_sudo, false
set :deploy_to, "/home/user/app_dir"
default_run_options[:pty] = true
set :ssh_options, {:forward_agent => true}
set :ssh_options, {:auth_methods => "publickey"}
set :ssh_options, {:keys => ["~/sites/app/config/key.pem"]}
#repo details
set :scm, :git
set :repository, "git#github.com:github_id/app.git"
set :scm_username, 'github_id'
set :keep_releases, 2
set :branch, "master"
after 'deploy:update_code', 'deploy:symlink_db'
namespace :deploy do
# task :start, :roles => :app, :except => { :no_release => true } do
# # not need to restart nginx every time
# # run "service nginx start"
# run "cd #{release_path} && touch tmp/restart.txt"
# end
# after "deploy:start", "deploy:cleanup"
# after 'deploy:cleanup', 'deploy:symlink_config'
# You do not need reload nginx every time, eventhought if you use passenger or unicorn
# task :stop, :roles => :app, :except => { :no_release => true } do
# run "service nginx stop"
# end
# task :graceful_stop, :roles => :app, :except => { :no_release => true } do
# run "service nginx stop"
# end
# task :reload, :roles => :app, :except => { :no_release => true } do
# run "cd #{release_path} && touch tmp/restart.txt"
# run "service nginx restart"
# end
task :restart, :roles => :app, :except => { :no_release => true } do
run "cd #{release_path} && touch tmp/restart.txt"
end
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
# If you enable assets/deploy in Capfile, you do not need this
# task :pipeline_precompile do
# # run "cd #{release_path}; RAILS_ENV=#{rails_env} bundle exec rake assets:precompile"
# # precompile assets before deploy and upload them to server
# # run_locally("RAILS_ENV=#{rails_env} rake assets:clean && RAILS_ENV=#{rails_env} rake assets:precompile")
# # top.upload "public/assets", "#{release_path}/public/assets", :via =>:scp, :recursive => true
# end
end
# you do not need to this, because you already add require 'bundler/capistrano'
# before "deploy:assets:precompile", "bundle:install"
I found that the capistrano asset compile rules got run before the bundler rules, so nothing worked, I wrote my own:
after "bundle:install" do
run "cd #{release_path}; RAILS_ENV=production bundle exec rake assets:precompile"
end
I then found that I really wasn't interested in installing a js runtime on my production machines. I went back to just putting my assets into a deploy branch of my git tree.
First time I did this, I forgot to include the manifest.yml file.
http://jimneath.org/2012/05/05/precompile-assets-using-a-git-hook.html
If you have uglifier in Gemfile, which runs during asset compilation, it may have been configured to remove just the sort of comment you are using as a test for whether the changed Javascript assets are being deployed.
Uglifier's Readme says this is possible, and distinguishes several kinds of comments. So it's possible you may have been misled by seeing another comment still remaining after asset compilation.
Another clue to judging this possibility is that (as you say) this problem arose after a long (tiring) process of fixing other problems. Often, this can block people from a perception like this; a long effort can 'lock them in' to an assumption.
So, try adding a variable declaration instead of a comment, in your Javascript.
I had an issue similar to this but the solution was to remove the assets group in the Gemfile, as the default task cap uses to precompile depends on the assets not being in a separate group seemingly.
I encountered this problem. Turns out I had to install node and yarn on my servers for rake assets:precompile to work
I also had this issue recently, asset precompilation with Capistrano deployment was hanging in AWS. I solved the issue by switching from a t3.micro instance to a t3.small instance temporarily while I deployed. It looks like the micro instance did not have enough resources for the precompilation step.

Rake fails with Capistrano deployment

I am deploying to Ubuntu with Capistrano.
I have God + Resque set up for background process.
App runs fine and Resque workers are going to their queues, but are stuck there. When I looked into logs, I found out that it's Rake failing. Here is the trace:
FIRST THERE IS ABOUT 1000 OF THE BELOW LINE
/home/ubuntu/myapp/releases/20120724024732/config/application.rb:68:in `invoke'
/home/ubuntu/myapp/shared/bundle/ruby/1.9.1/gems/rake-0.9.2.2/lib/rake/application.rb:116:in `invoke_task'
/home/ubuntu/myapp/shared/bundle/ruby/1.9.1/gems/rake-0.9.2.2/lib/rake/application.rb:94:in `block (2 levels) in top_level'
/home/ubuntu/myapp/shared/bundle/ruby/1.9.1/gems/rake-0.9.2.2/lib/rake/application.rb:94:in `each'
/home/ubuntu/myapp/shared/bundle/ruby/1.9.1/gems/rake-0.9.2.2/lib/rake/application.rb:94:in `block in top_level'
/home/ubuntu/myapp/shared/bundle/ruby/1.9.1/gems/rake-0.9.2.2/lib/rake/application.rb:133:in `standard_exception_handling'
/home/ubuntu/myapp/shared/bundle/ruby/1.9.1/gems/rake-0.9.2.2/lib/rake/application.rb:88:in `top_level'
/home/ubuntu/myapp/shared/bundle/ruby/1.9.1/gems/rake-0.9.2.2/lib/rake/application.rb:66:in `block in run'
/home/ubuntu/myapp/shared/bundle/ruby/1.9.1/gems/rake-0.9.2.2/lib/rake/application.rb:133:in `standard_exception_handling'
/home/ubuntu/myapp/shared/bundle/ruby/1.9.1/gems/rake-0.9.2.2/lib/rake/application.rb:63:in `run'
/home/ubuntu/myapp/shared/bundle/ruby/1.9.1/gems/rake-0.9.2.2/bin/rake:33:in `<top (required)>'
/home/ubuntu/.rvm/gems/ruby-1.9.3-p125#global/bin/rake:19:in `load'
/home/ubuntu/.rvm/gems/ruby-1.9.3-p125#global/bin/rake:19:in `<main>'
/home/ubuntu/.rvm/gems/ruby-1.9.3-p125/bin/ruby_noexec_wrapper:14:in `eval'
/home/ubuntu/.rvm/gems/ruby-1.9.3-p125/bin/ruby_noexec_wrapper:14:in `<main>'
rake aborted!
Too many open files - rake_tasks_log.log
/home/ubuntu/.rvm/rubies/ruby-1.9.3-p125/lib/ruby/1.9.1/open-uri.rb:35:in `initialize'
/home/ubuntu/.rvm/rubies/ruby-1.9.3-p125/lib/ruby/1.9.1/open-uri.rb:35:in `open'
/home/ubuntu/.rvm/rubies/ruby-1.9.3-p125/lib/ruby/1.9.1/open-uri.rb:35:in `open'
/home/ubuntu/.rvm/rubies/ruby-1.9.3-p125/lib/ruby/1.9.1/logger.rb:592:in `open_logfile'
/home/ubuntu/.rvm/rubies/ruby-1.9.3-p125/lib/ruby/1.9.1/logger.rb:549:in `initialize'
/home/ubuntu/.rvm/rubies/ruby-1.9.3-p125/lib/ruby/1.9.1/logger.rb:314:in `new'
/home/ubuntu/.rvm/rubies/ruby-1.9.3-p125/lib/ruby/1.9.1/logger.rb:314:in `initialize'
/home/ubuntu/myapp/shared/bundle/ruby/1.9.1/gems/activesupport-3.2.6/lib/active_support/core_ext/logger.rb:72:in `initialize'
Here is the function from application.rb line 68 is marked with comment:
module Rake
class Task
alias_method :origin_invoke, :invoke if method_defined?(:invoke)
def invoke(*args)
logger = Logger.new('rake_tasks_log.log')
logger.info "#{Time.now} -- #{name} -- #{args.inspect}"
puts args
origin_invoke(args) ### LINE 68
end
end
end
**EDIT:: Here is a clue, but don't know what to do with it:: Apparently the *args that are being passed is an empty array...
Here is my deploy.rb
require "bundler/capistrano"
$:.unshift(File.expand_path('./lib', ENV['rvm_path']))
require 'rvm/capistrano'
set :rvm_ruby_string, ENV['GEM_HOME'].gsub(/.*\//,"")
set :rvm_type, :user
# -*- encoding : utf-8 -*-
set :assets_dependencies, %w(app/assets lib/assets vendor/assets Gemfile.lock config/routes.rb)
namespace :deploy do
namespace :assets do
desc <<-DESC
Run the asset precompilation rake task. You can specify the full path \
to the rake executable by setting the rake variable. You can also \
specify additional environment variables to pass to rake via the \
asset_env variable. The defaults are:
set :rake, "rake"
set :rails_env, "production"
set :asset_env, "RAILS_GROUPS=assets"
set :assets_dependencies, fetch(:assets_dependencies) + %w(config/locales/js)
DESC
task :precompile, :roles => :web, :except => { :no_release => true } do
from = source.next_revision(current_revision)
if capture("cd #{latest_release} && #{source.local.log(from)} #{assets_dependencies.join ' '} | 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
end
set :application, "myapp"
set :repository, "git#github.com:..."
set :scm, :git
set :git_shallow_clone, 1
set :branch, "master"
set :location, "ec2-....amazonaws.com"
role :app, location
role :web, location
role :db, location, :primary => true
set :deploy_via, :remote_cache
set :user, "ubuntu"
set :use_sudo, false
set :deploy_to, "/home/ubuntu/myapp"
ssh_options[:forward_agent] = true
default_run_options[:pty] = true
ssh_options[:keys] = ["#{ENV['HOME']}/...pem"]
set :keep_releases, 1
# setup some Capistrano roles
role :app, location
role :web, location
role :db, location, :primary => true
set :default_environment, {
'PATH' => "/home/ubuntu/.rvm/gems/ruby-1.9.3-p125/bin:/home/ubuntu/.rvm/gems/ruby-1.9.3-p125#global/bin:/home/ubuntu/.rvm/rubies/ruby-1.9.3-p194/bin:/home/ubuntu/.rvm/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/lib/pkgconfig:/usr/lib/jvm/java-1.6.0-openjdk-amd64:/usr/lib/jvm/java-6-openjdk-amd64:$PATH",
'JAVA_HOME' => "/usr/lib/jvm/java-6-openjdk-amd64",
'PKG_CONFIG_PATH' => '$PKG_CONFIG_PATH:/usr/local/lib/pkgconfig'
}
desc "show path"
task :show_path do
run "echo $PATH"
end
###PASSENGER:
namespace :passenger do
desc "Restart Application"
task :restart do
run 'sudo /etc/init.d/nginx stop'
run 'sudo /etc/init.d/nginx start'
end
end
after :deploy, "passenger:restart"
after :deploy, "deploy:migrate"
Last note - everything works fine if I don't use Capistrano (e.g. just copy Rails app folder to server).. so I guess I am missing something on Capistrano side..
So it turns out that my redis folder had permissions problem and could not write to the database. So I checked where the database file is being saved (or rather not saved in my case) and did sudo chown redis:redis now all is good.

Resources