Imran Teli
Imran Teli

Reputation: 53

Bundle unable to locate/execute gems for ruby app if installed through Chef

I am trying to deploy a ruby app through chef. I have installed ruby and rubygems through rvm.

When I do a bundle install --deployment manually on the shell I get all the bundles downloaded to vendor/bundle location and everything works fine.

But when I do the same with chef using bash resource I get error messages mentioned below. I feel like some issue with the PATH or environmental variables. Through chef also it downloads all the gems but when I open the shell and check I get errors. Please note - chef recipes executes without any errors.

*bundle exec rake db:create RAILS_ENV=production
(in /home/xyz)
rake aborted!
undefined method `[]' for nil:NilClass
/home/xyz/Rakefile:4:in `require'
(See full trace by running task with --trace)*

# ruby -v
ruby 1.9.2p290 (2011-07-09 revision 32553) [x86_64-linux]

# gem -v
1.8.1

bundle -v
Bundler version 1.0.15

# gem env
RubyGems Environment:
  - RUBYGEMS VERSION: 1.8.1
  - RUBY VERSION: 1.9.2 (2011-07-09 patchlevel 290) [x86_64-linux]
  - INSTALLATION DIRECTORY: /usr/local/rvm/gems/ruby-1.9.2-p290
  - RUBY EXECUTABLE: /usr/local/rvm/rubies/ruby-1.9.2-p290/bin/ruby
  - EXECUTABLE DIRECTORY: /usr/local/rvm/gems/ruby-1.9.2-p290/bin
  - RUBYGEMS PLATFORMS:
    - ruby
    - x86_64-linux
  - GEM PATHS:
     - /usr/local/rvm/gems/ruby-1.9.2-p290
     - /usr/local/rvm/gems/ruby-1.9.2-p290@global
  - GEM CONFIGURATION:
     - :update_sources => true
     - :verbose => true
     - :benchmark => false
     - :backtrace => false
     - :bulk_threshold => 1000
  - REMOTE SOURCES:
     - http://rubygems.org/

    bash "bundle install" do
     user "root"
     cwd "/home/xyz/"
     code <<-EOH
     source /etc/profile.d/rvm.sh 
     rvm use 1.9.2-p290 --default 
     /usr/local/rvm/gems/ruby-1.9.2-p290/bin/bundle install --path  vendor/bundle
     /usr/local/rvm/gems/ruby-1.9.2-p290/bin/bundle exec rake db:create RAILS_ENV=production
     EOH
    end

This is the first time I am deploying any ruby app, I have googled a lot and tried many solutions but I get more and more errors but no solutions. Any kind of help would be greatly appreciated.

Thanks, Imran

Upvotes: 0

Views: 466

Answers (1)

Malcolm Jones
Malcolm Jones

Reputation: 1482

So I believe the reason why you are having some issues with ruby installed by RVM has to do with the nature of linux + shells. RVM masks a lot complexity underneath when it presents you with a list of ruby builds that you can install on your system. Simple answer is RVM ( similar to rbenv ) works by hooking into your environment's PATH ... This is a very detailed explanation provided by Sam Stephenson here: How do RVM and rbenv actually work? but I digress. My guess is that your RVM installation is made for an individual user, eg user who has home directory /home/xyz/. At this point, I think your problem is the following:

You installed RVM via Single-User installation( Details here: https://rvm.io/rvm/install ) . When you installed RVM, they probably made you do this step here: echo "source $HOME/.rvm/scripts/rvm" >> ~/.bash_profile. This step adds a command into your .bash_profile so that on each new session/terminal window, your user ( in this case, user xyz ) reads in information on how to run/use RVM. The bash resource in chef isn't actually using the same shell that you use to connect and run commands. My thought is that, if you run something like:

bash 'install_ruby_stuff' do cwd ::File.dirname(/home/xyz) code <<-EOH source /home/xyz/.rvm/scripts/rvm bundle exec rake db:create RAILS_ENV=production EOH end

I have a feeling it might work a bit better. If that doesn't work, run which bundle and figure out exactly where rvm installs the bin stub for the bundle command, then replace bundle above, with /path/to/bundle and that might help as well. Each bash resource call in chef is essentially treated like a separate shell, so any PATH or ENV variables that you need, need to be defined within the same context that the bash resource is called.

Hope this helps you a bit!

Upvotes: 1

Related Questions