This is my first time to deploy a rails app in DigitalOcean, but I'm having a problem in deploying the application. When I tried to run the cap production deploy or bundle exec cap deploy:setup I got an error of :
/usr/local/rvm/gems/ruby-2.2.0/gems/capistrano-bundler-1.1.4/lib/capistrano/tasks/bundler.cap:1:in `<top (required)>': undefined method `namespace' for main:Object (NoMethodError)
this is my deploy.rb
set :application, "myapp.com"
set :repository, "git#heroku.com:myapp.git"
set :scm, :git
# Or: `accurev`, `bzr`, `cvs`, `darcs`, `git`, `mercurial`, `perforce`, `subversion` or `none`
set :user, 'user'
set :use_sudo, false
set :deploy_to, "/home/user/video-benta"
set :deploy_via, :remote_cache
role :web, "myapp.com" # Your HTTP server, Apache/etc
role :app, "myapp.com" # This may be the same as your `Web` server
role :db, "myapp", :primary => true # This is where Rails migrations will run
#role :db, "your slave db-server here"
# 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:
after "deploy", "deploy:bundle_gems"
after "deploy:bundle_gems", "deploy:restart"
namespace :deploy do
task :bundle_gems do
run "cd #{deploy_to}/current && bundle install vendor/gems"
end
task :start do ; end
task :stop do ; end
task :restart, :roles => :app, :except => { :no_release => true } do
run "touch #{File.join(current_path,'tmp','restart.txt')}"
end
end
Capfile ( Update )
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
require 'capistrano/bundler'
require 'capistrano/rails'
# If you are using rvm add these lines:
require 'capistrano/rvm'
set :rvm_type, :user
set :rvm_ruby_version, '2.2.0p0'
You are trying to use different Capistrano extensions incompatible with your Capistrano version. Your Capistrano version is 2.x (detected by format of your Capfile), but capistrano/bundler for example is a extension for Capistrano 3.x.
Just use Capistrano 3 (if possible) and compatible extensions.
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.
I'm trying to deploy my rails app called Blog to my website through Capistrano. I have created a Rails App in cPanel at /rails_app/Blog/ and have deleted the skeleton code that was generated.
When i type this command in the terminal
cap deploy:setup
I get this error message
USER#USER-PC ~/documents/Aptana Studio 3 Workspace/Blog
$ cap deploy:setup
* executing `deploy:setup'
* executing "sudo -p 'sudo password: ' mkdir -p /rails_app/Blog/ /rails_app/Bl
og/releases /rails_app/Blog/shared /rails_app/Blog/shared/system /rails_app/Blog
/shared/log /rails_app/Blog/shared/pids"
servers: ["mydomain.co.nz"]
connection failed for: mydomain.co.nz (Errno::ETIMEDOUT: A connection attempt fail
ed because the connected party did not properly respond after a period of time,
or established connection failed because connected host has failed to respond. -
connect(2))
Here is my deploy.rb file config settings
set :application, "mydomain.co.nz"
set :repository, "set your repository location here"
set :scm, :subversion
# Or: `accurev`, `bzr`, `cvs`, `darcs`, `git`, `mercurial`, `perforce`, `subversion` or `none`
# These are not not part of the defaults
set :port, 12002
set :deploy_to, "/rails_app/Blog/"
#
role :web, application # Your HTTP server, Apache/etc
role :app, application # This may be the same as your `Web` server
role :db, application, :primary => true # This is where Rails migrations will run
role :db, application
try this, this is example of deploy.rb file that work for me
require "bundler/capistrano"
set :application, "testcapistrano"
set :rvm_ruby_string, "ruby-1.9.3-p194#testcapistrano"
require "rvm/capistrano"
set :rvm_type, :user
set :rvm_install_ruby, :install
set :repository, "xyz"
set :scm, :git
# Or: `accurev`, `bzr`, `cvs`, `darcs`, `git`, `mercurial`, `perforce`, `subversion` or `none`
role :web, "testcapistrano.dipak.cs" # Your HTTP server, Apache/etc
role :app, "testcapistrano.dipak.cs" # This may be the same as your `Web` server
role :db, "testcapistrano.dipak.cs", :primary => true # This is where Rails migrations will run
default_run_options[:pty] = true
ssh_options[:forward_agent] = true
set :deploy_to, "/dipak/capistron/"
set :deploy_via, :remote_cache
set :user, "your username"
set :password, "password"
set :use_sudo, false
set :keep_releases, 5
before 'deploy:setup', 'rvm:install_ruby'
# tasks
namespace :deploy do
task :start, :roles => :app do
run "touch #{current_path}/tmp/restart.txt"
end
task :stop, :roles => :app do
# Do nothing.
end
desc "Restart Application"
task :restart, :roles => :app do
run "touch #{current_path}/tmp/restart.txt"
end
end
When I deploy with capistrano I get an error (and rollback) when capistrano attempts to run assets:precompile.
I'm using rails 3.2.1, bundler 1.0.22, capistrano 2.11.2
If I run rake assets:precompile from /webapps/myapp/current it runs successfully.
error:
failed: "sh -c 'cd /webapps/myapp/releases/20120304160347 && bundle exec rake RAILS_ENV=production RAILS_GROUPS=assets assets:precompile'"
deploy.rb
require "bundler/capistrano"
load 'deploy/assets'
set :application, "myapp"
set :domain, '24.17.71.95'
set :repository, "."
set :deploy_via, :copy
set :local_repository, '/home/me/myapp/.git'
set :deploy_to, '/webapps/myapp/'
set :scm, :none #:git
set :user, 'me'
set :password, 'me$pw'
# Or: `accurev`, `bzr`, `cvs`, `darcs`, `git`, `mercurial`, `perforce`, `subversion` or `none`
default_run_options[:pty] = true
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 # This is where Rails migrations will run
set :branch, 'master'
# if you're still using the script/reaper helper you will need
# these http://github.com/rails/irs_process_scripts
before "deploy:assets:precompile", "bundle:install"
# If you are using Passenger mod_rails uncomment this:
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
As far as I know, the asset precompiling task should be one of the last (or even the last one) tasks that are beeing executed.
So please try to move
load 'deploy/assets'
out of deploy.rb into Capfile (root folder of your Rails app) and paste it as the last line of the file.
See my answer to this Stackoverflow question.
If that fails, try the advice in http://www.simonecarletti.com/blog/2012/02/heroku-and-rails-3-2-assetprecompile-error/
When trying to deploy with Capistrano I am getting a ton of these errors:
[err :: localhost] tar: 20120220182722/app/assets/images/assets/Thumbs.db: Cannot open: No such file or directory
Every asset I have can't be found and capistrano errors out (doing a deploy:cold)
I've uncommented this line in the capfile:
# Uncomment if you are using Rails' asset pipeline
load 'deploy/assets'
but it still says the same thing.
The above line is actually in my Capfile here is the rest of my deploy.rb (note that I am using vagrant and trying to deploy to a VM created with vagrant)
require 'bundler/capistrano'
set :application, "testdeploy"
set :scm, :git
set :repository, "."
# Or: `accurev`, `bzr`, `cvs`, `darcs`, `git`, `mercurial`, `perforce`, `subversion` or `none`
server "localhost", :app, :db, :primary => true
ssh_options[:port] = 2222
ssh_options[:keys] = "~/.rvm/gems/ruby-1.9.3-p125#testdeploy/gems/vagrant-0.9.4/keys/vagrant"
set :user, "vagrant"
set :group, "vagrant"
set :deploy_to, "/var/testdeploy"
set :use_sudo, true
set :deploy_via, :copy
set :copy_strategy, :export
# If you are using Passenger mod_rails uncomment this:
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