Opsworks : cron : Ruby version is incorrect - ruby

My objective is to setup a cron entry to run a rake task on my Opsworks rails application layer.
I have followed the AWS doco on this subject found here
My Chef Job:
cron_env = {"PATH" => "/usr/local/bin:$PATH"}
cron "warm up the cache" do
environment cron_env
hour "5"
minute "10"
command "cd /srv/www/myapp/current && RAILS_ENV=production bundle exec rake cache:warm_api"
end
The crontab on the instance:
user#myinstance:~$ sudo crontab -l -u root
# Chef Name: warm up the cache
PATH=/usr/local/bin:$PATH
10 5 * * * cd /srv/www/myapp/current && RAILS_ENV=production bundle exec rake cache:warm_api
My ruby -v in /usr/local/bin:
user#myinstance:~$ /usr/local/bin/ruby -v
ruby 2.1.2p95 (2014-05-08 revision 45877) [x86_64-linux]
THE PROBLEM:
During execution, my cron fails to use the correct ruby.
/usr/local/lib/ruby/gems/2.1.0/gems/bundler-1.7.3/lib/bundler/definition.rb:385:in `validate_ruby!': Your Ruby version is 1.9.3, but your Gemfile specified 2.1.2 (Bundler::RubyVersionMismatch)
from /usr/local/lib/ruby/gems/2.1.0/gems/bundler-1.7.3/lib/bundler.rb:117:in `setup'
from /usr/local/lib/ruby/gems/2.1.0/gems/bundler-1.7.3/lib/bundler/setup.rb:17:in `<top (required)>'
from /usr/lib/ruby/1.9.1/rubygems/custom_require.rb:36:in `require'
from /usr/lib/ruby/1.9.1/rubygems/custom_require.rb:36:in `require

This turned out to be temporary glitch in Opsworks.

Related

Running bundle exec rake inside cron cannot find specific version of rake

I am setting up cron to run some rake tasks for my ruby on rails server. They are generated by whenever and look like this:
* * * * * /bin/bash -l -c 'cd /var/www && RAILS_ENV=production /usr/local/bin/bundle exec /usr/local/bin/rake my:task --silent >> /var/www/log/cron.log 2>&1'
bundle and rake executables seem to be found, but inside cron.log I find this error:
bundler: failed to load command: /usr/local/bin/rake (/usr/local/bin/rake)
Bundler::GemNotFound: Could not find rake-12.3.2 in any of the sources
/usr/local/lib/ruby/gems/2.5.0/gems/bundler-1.16.6/lib/bundler/spec_set.rb:91:in `block in materialize'
...
...
As I can see it tried to load rake-12.3.2, which is a correct version, from /usr/local/lib/ruby/gems/2.5.0/gems, which is not a correct path, because there is no rake-12.3.2. Correct version of rake is under another path /usr/local/bundle/gems, but I have no luck setting this path for cron to use.
I have tried adding GEM_PATH="/usr/local/bundle/gems" to cron file, but it doesn't work.
I am not using rvm or rbenv, ruby is installed directly.

crontab dont execute require

I'm using a osx and I created a ruby script in the path: /Users/diogo/workspace/outros/crawler_trf with name get_news.rb
So I tried to execute it via crontab with the following line: */1 * * * * 'ruby /Users/diogo/workspace/outros/crawler_trf/get_news.rb' > /tmp/crawler_trf.out and I've got the error: /bin/sh: ruby /Users/diogo/workspace/outros/crawler_trf/get_news.rb: No such file or directory
After some searches and a question here I solved this problem removing the quotes and now my crontab looks like that: */1 * * * * cd /Users/diogo/workspace/outros/crawler_trf/ && ruby get_news.rb > /tmp/crawler_trf.out
But now I'm having a new error that is:
/System/Library/Frameworks/Ruby.framework/Versions/2.0/usr/lib/ruby/2.0.0/rubygems/core_ext/kernel_require.rb:55:in `require': cannot load such file -- mail (LoadError)
from /System/Library/Frameworks/Ruby.framework/Versions/2.0/usr/lib/ruby/2.0.0/rubygems/core_ext/kernel_require.rb:55:in `require'
from get_news.rb:3:in `<main>'
The beginning of my file is:
require 'nokogiri'
require 'open-uri'
require 'mail'
My ruby version is 2.3.0 and I have it specified in .ruby-version and using gemset specified in `.ruby-gemset
I installed the gems via bundle
I really had searching for my answers for long but I didn't found nothing.
You should always fully specify the paths of all files when executing them out of cron, since the environment provided by cron will be different from your login environment. That might mean replacing the invocation of ruby with the full path to your ruby binary (maybe /usr/local/bin/ruby).
Try running ruby -v out of cron to see what version you are picking up. On my Mac OS system I have two:
Edwards-MacBook-Air:~ emv$ /usr/bin/ruby -v
ruby 2.0.0p648 (2015-12-16 revision 53162) [universal.x86_64-darwin15]
Edwards-MacBook-Air:~ emv$ /usr/local/bin/ruby -v
ruby 2.3.1p112 (2016-04-26 revision 54768) [x86_64-darwin15]
one from the operating system, and one from Homebrew.

travis failing to run rake with LoadError: cannot load such file -- rspec/core/rake_task

Let me preface this with this all runs locally and succeeds.
Source code repo
Travis log for job #4.1
Error:
$ rake
rake aborted!
LoadError: cannot load such file -- rspec/core/rake_task
/home/travis/build/alienfast/gulp-pipeline-rails/Rakefile:4:in `<top (required)>'
/home/travis/.rvm/gems/ruby-2.2.2/bin/ruby_executable_hooks:15:in `eval'
/home/travis/.rvm/gems/ruby-2.2.2/bin/ruby_executable_hooks:15:in `<main>'
(See full trace by running task with --trace)
.travis.yml
language: ruby
rvm:
- 2.2.2
- ruby-head
matrix:
allow_failures:
- rvm: ruby-head
cache: bundler
before_script:
# install Gulp 4 CLI tools globally from 4.0 GitHub branch
- npm install gulpjs/gulp-cli#4.0 -g
script: rake
Gemfile
gemspec
# travis-ci quirk?
group :development, :test do
gem 'rake'
gem 'rspec'
gem 'rspec-rails'
gem 'capybara'
gem 'capybara-webkit'
end
Fix attempts:
Locally, I have emptied my rvm gemset, deleted locks, and replicated the travis build - still works
I tried moving the necessary test gems from the gemspec to the Gemfile
So I'm pretty confused, I have other projects that don't have this problem and I don't see anything obvious. Is there any reason to get LoadError: cannot load such file -- rspec/core/rake_task?
Have you tried using bundle exec rake -t As noted here LoadError: cannot load such file -- rspec/core/rake_task

How Can I get Rake to use RVM's Ruby?

I'm trying to install an application on Ubuntu that uses ruby. I installed RVM and Ruby 2.0 and ran rake install, but Rake tried using an older system version of Ruby. So I uninstalled the system version and tried running Rake again. This time it said "No such file". How do I get Rake to use RVM's ruby? Below is the output from different commands.
$ rake install
sudo bundle --without production staging test levelbuilder --quiet
sudo: unable to execute /usr/local/bin/bundle: No such file or directory
rake aborted!
...
/home/ubuntu/../rake_utils.rb:33:in `system'
..
/home/ubuntu/../rake_utils.rb:43:in `bundle_install'
/home/ubuntu/../Rakefile:198:in `block (3 levels) in <top (required)>'
..
/home/ubuntu/.rvm/gems/ruby-2.0.0-p643/bin/ruby_executable_hooks:15:in `eval'
...
The path and version look right:
$ echo $PATH
/home/ubuntu/.rvm/gems/ruby-2.0.0-p643/bin:/home/ubuntu/.rvm/gems/ruby-2.0.0-p643#global/bin:/home/ubuntu/.rvm/rubies/ruby-2.0.0-p643/bin:/home/ubuntu/.rvm/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games
$ ruby -v
ruby 2.0.0p643 (2015-02-25 revision 49749) [x86_64-linux]
$ which ruby
/home/ubuntu/.rvm/rubies/ruby-2.0.0-p643/bin/ruby
Here's my .bash_profile:
[[ -s "$HOME/.profile" ]] && source "$HOME/.profile" # Load the default .profile
[[ -s "$HOME/.rvm/scripts/rvm" ]] && source "$HOME/.rvm/scripts/rvm" # Load RVM into a shell session *as a function*
and here's the bottom of .bashrc:
export PATH="$PATH:$HOME/.rvm/bin" # Add RVM to PATH for scripting.
Execute source ~/.bash_profile

Can't load capistrano-recipes

I've added capistrano-recipes to my Gemfile
group :development do
...
gem 'capistrano-recipes',require: false
end
Added to my deploy script:
require 'capistrano-recipes'
The result of bundle list:
bundle list | grep capistrano
* capistrano (2.15.5)
* capistrano-ext (1.2.1)
* capistrano-platform-resources (0.1.3)
* capistrano-rbenv (1.0.5)
* capistrano-recipes (0.8.0)
But when I'm running command like cap -T I got error:
/usr/local/rvm/gems/ruby-2.0.0-p0-turbo/gems/capistrano-2.15.5/lib/capistrano/configuration/loading.rb:152:in `require': cannot load such file -- capistrano-recipes (LoadError)
from /usr/local/rvm/gems/ruby-2.0.0-p0-turbo/gems/capistrano-2.15.5/lib/capistrano/configuration/loading.rb:152:in `require'
from ./config/deploy.rb:12:in `load'
What might be a problem here? Thanks
From the README, this is what you should put in your deploy script:
require 'capistrano_recipes'
Note that it uses an underscore, and not a hyphen.
Either install the gem globally
gem install capistrano-recipes
cap -T
or just:
bundle exec cap -T

Resources