Capistrano custom task fails because "Rails requires RubyGems >= 1.3.2" - ruby-on-rails

My custom capistrano task "app:sample" fails with the following error message:
mnylen ilmo-on-rails $ cap app:sample
* executing `app:sample'
* executing "export RAILS_ENV=production; cd /home/mnylen/ilmo-on-rails/current; ruby script/coursegen 10"
servers: ["rails.cs.helsinki.fi"]
* establishing connection to gateway `melkinpaasi.cs.helsinki.fi'
* Creating gateway using melkinpaasi.cs.helsinki.fi
* establishing connection to `rails.cs.helsinki.fi' via gateway
Password:
[rails.cs.helsinki.fi] executing command
*** [err :: rails.cs.helsinki.fi] Rails requires RubyGems >= 1.3.2. Please install RubyGems and try again: http://rubygems.rubyforge.org
command finished
failed: "sh -c 'export RAILS_ENV=production; cd /home/mnylen/ilmo-on-rails/current; ruby script/coursegen 10'" on rails.cs.helsinki.fi
Am I missing something or doing something wrong? The task is:
namespace :app do
desc "Run sample data on production2
task :sample do
run "export RAILS_ENV=production; cd #{current_path}; ruby script/coursegen 10"
end
end
If I run the same command from the actual server, it works fine.

Cap is running the remote command as an unexpected user - and that user does not have the correct path to ruby and gem. Check your settings in your recipe for :user and :use_sudo. Carefully read the cap output to see what user is being connected. I see you are using a :gateway; there can be two users in this case. One to connect to the gateway, and another to actually run commands on the target server.

Okay, solved.
The problem was that there was two Ruby installations on the production server.
The .profile file under my home directory on the production server set the PATH environment variable to point to the correct Ruby version.
run command, it seems, doesn't source the .profile file and thus, running ruby script/coursegen 10 in the task used the wrong Ruby version, which was the reason for the weird error message about RubyGems version. This also explains why it worked when manually running the command from production servers shell.
My solution was to use full path to the Ruby executable in my run task, like this:
run "export RAILS_ENV=production; cd #{current_path}; /opt/ruby-enterprise-1.8.7-2009.10/bin/ruby script/coursegen 10"
Of course, this isn't pretty, but it works. If anyone has any prettier solutions, I'd be more than glad to use those instead. :)

Seems like you should update your RubyGems on the remote server.

Related

Can't execute Rails rake task with cron

I'm trying to run rake task with cron on Ubuntu with rvm
My crontab -l
* * * * * cd /media/sf_visa-tracker/ && /home/ruslan/.rvm/gems/ruby-2.1.0/bin/rake parse RAILS_ENV=production >> /var/log/visa-parse.log 2>&1
which rake says
/home/ruslan/.rvm/gems/ruby-2.1.0/bin/rake
And I keep getting this error in my log (/var/log/visa-parse.log)
/usr/bin/env: ruby_executable_hooks: No such file or directory
Also, if I run cd /media/sf_visa-tracker/ && /home/ruslan/.rvm/gems/ruby-2.1.0/bin/rake parse RAILS_ENV=production from my terminal, rake task works.
Pls help ;)
The simplest way to use rvm with cron is to use rvm's wrappers. Your shell sets up a whole bunch of rvm-related environment when it starts, and that's missing from your cron job. The wrappers are versions of the Ruby-related commands that take care of this for you.
In this case, if you have rvm installed to /usr/local, your cron job should look something like this:
* * * * * cd /media/sf_visa-tracker/ && /usr/local/rvm/wrappers/ruby-2.1.0/bin/rake parse RAILS_ENV=production >> /var/log/visa-parse.log 2>&1
You could also bundle up your rvm setup into a shell script that loads rvm before invoking rake; there's more details of both approaches in rvm's documentation on working with cron.
I struggled to get something that worked and seemed clean. Here's what I ended up doing in case it helps.
crontab
0 5 * * * /home/deploy/apps/myproject/shared/cron/daily_updates.sh
I put the script in myproject/shared since I'm using Capistrano and that seemed like a good place for things that would persist between releases.
shared/cron/daily_updates.sh
#!/usr/bin/env bash
# You will need to update this as you bump ruby versions
source /home/deploy/.rvm/environments/ruby-2.3.1#myproject
# Since I had a separate place for Rails specific environment variables (e.g. secrets, app keys)
# you should set the permissions on that file to be owner read/write only
source /home/deploy/.rails_env
cd /home/deploy/apps/myproject/current
rake RAILS_ENV=production daily_update:send_updates 2>&1

Ruby on Rails fails on bundle install when using capistrano

I have not much knowledge of Ruby. I have to host a Ruby website. When I make deploy setup and check, is everything ok. But when I cap deploy I get the following error.
*** [deploy:update_code] rolling back
* executing "rm -rf /var/www/lpavisit/releases/20131009211837; true"
servers: ["x.xx.xx.xxx"]
[5.56.61.183] executing command
command finished in 209ms
failed: "sh -c 'cd /var/www/lpavisit/releases/20131009211837 && bundle install --without development test'" on x.xx.xx.xxx
Thanks!!
Setting up a Capistrano-based deploy system is a bit complex. You might want to give Heroku a try until you're more familiar with Ruby. But to continue with Capistrano, first ask it if it has everything it needs to deploy:
$ cap deploy:check
You want to see it say, You appear to have all necessary dependencies installed. Yours probably won't. In that case, you should look for a good capistrano book or online tutorial.

(CRON) error (grandchild #26321 failed with exit status 127)

I am using whenever gem my schedule.rb content is
every 2.minutes do runner
"Refinery::Page.update_publishing_date_to_list_in_menu" end
I get this by crontab -l
# Begin Whenever generated tasks for: /home/harssh/Documents/Aptana Studio 3 Workspacwinbox/wineboxnew/config/schedule.rb
0,2,4,6,8,10,12,14,16,18,20,22,24,26,28,30,32,34,36,38,40,42,44,46,48,50,52,54,56,58
* * * * /bin/bash -l -c 'cd /home/harssh/Documents/Aptana\ Studio\ 3\ Workspacwinbox/wineboxnew && RAILS_ENV=production bundle exec rake page:publish --silent'
# End Whenever generated tasks for: /home/harssh/Documents/Aptana Studio 3 Workspacwinbox/wineboxnew/config/schedule.rb
This runs well on my ubuntu 10.10
But I get (CRON) error (grandchild #26321 failed with exit status 127) on my debian server
where I am using apache and passenger with rvm using Ruby 1.9.3
If I use script in terminal in debian it also runs properly but automated cron task gives error
I am also faced same problem, Finally I resolved this issue.
Problem 1: Ruby path in script/runner file
ruby path in script/runner file:
#!/usr/local/bin/ruby
You can replace (/usr/local/bin/ruby) with your machine ruby path. You can check ruby path with the following command in your machine:
which ruby
Problem 2: I think your runner don't have execution permission.
You can set execution permission to runner file the following way:
Ruby project path/script> chmod +x ruuner

Cannot run rake with cron tasks

I am unable to run rake tasks with cron. The error it throws up is:-
/Library/Ruby/Gems/1.8/gems/bundler-1.2.0/lib/bundler/runtime.rb:199: warning: Insecure world writable dir /Library/Ruby/Gems/1.8 in PATH, mode 040777
/Library/Ruby/Gems/1.8/gems/bundler-1.2.0/lib/bundler/spec_set.rb:90:in `materialize': Could not find unf_ext-0.0.5 in any of the sources (Bundler::GemNotFound)
However, when I run the same rake task directly from the terminal, it works fine. In case of the cron job, as well as while running the rake task directly from the terminal, i cd into the (exact) same directory. Starting the rails server works as well.
What is the problem and how should I resolve this?
It could be an issue with your daemon user and your user when you're running it directly. Or it could be a problem with Ruby and/or your cron file commands.
Firstly, use this gem for making cron jobs with rake tasks:
https://github.com/javan/whenever
Then install RVM since you're using the standard ruby library provided by OSX and that can lead to some problems when you're developing various projects. This should fix the permissions error that you have displayed.

Capistrano deploy fails with rake run (rails3 + rvm)

EDIT:
The issue here was that I used RVM on my local machine and rbenv on the server. This is highly not recommended, if you're managing several ruby installation (or upgrading your ruby version but do not want to change the system's) use either RVM or rbenv on all environments!
/EDIT
So I'll start with a bit history:
I'm in the process of upgrading our rails2 website to rails3. Most of the things are working properly by now, except Capistrano's deploy script.
The current setup I have is:
RVM (1.14.1) installed locally
ruby-1.9.3-p194 (set to the project's folder)
Capistrano v2.12.0 (upgraded from 2.6.0, but it doesn't work on 2.6.0 as well) installed as an RVM gem
rvm-capistrano (1.2.2), added to the Gemfile (after doing some reading online and on SO)
rbenv (with ruby 1.9.3p194 used globally) installed on the server
(by now if you see anything that doesn't make sense, please let me know)
Nothing changed in the deploy script, yet when I try to deploy to my testing server (let's call it beta) I get this error:
* executing `deploy:restart'
* executing "cd /home/foo/bar/current && rake RAILS_ENV=beta queue:restart_workers"
servers: ["208.0..."]
[208.0...] executing command
*** [err :: 208.0...] rake aborted!
*** [err :: 208.0...] no such file to load -- bundler/setup
*** [err :: 208.0...] /home/foo/bar/releases/20120630161947/Rakefile:5
*** [err :: 208.0...] (See full trace by running task with --trace)
** [out :: 208.0...] (in /home/foo/bar/releases/20120630161947)
command finished in 1183ms
failed: "sh -c 'cd /home/foo/bar/current && rake RAILS_ENV=beta queue:restart_workers'" on 208.0...
The relevant part on deploy.rb looks like this:
task :restart do
run "cd /home/foo/bar/current && rake RAILS_ENV=#{CAP_ENV} queue:restart_workers"
run "cd /home/foo/bar/current && rake RAILS_ENV=#{CAP_ENV} db:migrate"
run "touch #{deploy_to}/current/tmp/restart.txt"
end
Needless to say, rake RAILS_ENV=beta queue:restart_workers' works perfectly when run manually on the server. Also, the application gets deployed (the code was copied from git, it's just the last part of the deploy fails).
Lastly, the error didn't change since before I installed rvm-capistrano and added to the Gemfile, so I'm not even sure it's related to rvm, I'm just guessing from looking online.
Thanks
In order to work correctly, rbenv must override all the ruby and gem-related executables with the shims it provides.
Usually this is done with a startup script (this is why it works when you login to your server) but Capistrano logs in without shell and thus does not run those scripts.
You must add the following to your deploy.rb :
set :default_environment, {
'PATH' => "$HOME/.rbenv/shims:$HOME/.rbenv/bin:$PATH"
}
and do not use anything rvm related on your server, rbenv and rvm really do not like each other
PS : a little more explanation on this topic : http://henriksjokvist.net/archive/2012/2/deploying-with-rbenv-and-capistrano/

Resources