0

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

Kiloreux
  • 2,220
  • 1
  • 17
  • 24
Imran Teli
  • 53
  • 1
  • 7
  • 1
    You may try the [application_ruby cookbook](https://github.com/poise/application_ruby) which already takes care of all this path mess needed with rvm – Tensibai Sep 15 '15 at 12:47
  • Have tried poise-ruby cookbook, but it installs gem in vendor/bundle/ruby/2.1.0 I need to have it in vendor/bundle/ruby/1.9.1. Tried mentioning ruby version but it throws error. Cannot find a resource matching ruby_runtime[1.9.1] (did you define it first?) bundle_install '/home/xyz/' do ruby '1.9.1' deployment true end Please excuse me of any dumb mistakes, I am new to chef. – Imran Teli Sep 15 '15 at 17:16
  • Tried application_ruby cookbook as well It installs gems in vendor/bundle/ruby/2.1.0 and throws an error at the end. Error -- Error executing action deploy on resource 'application[/home/xyz/]' -- You must supply a name when declaring a directory resource application '/home/xyz/' do bundle_install '/home/xyz/' do deployment true end end – Imran Teli Sep 18 '15 at 11:40

1 Answers1

1

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!

Community
  • 1
  • 1
Malcolm Jones
  • 1,472
  • 1
  • 12
  • 24