简体   繁体   中英

RVM problems `bundle install` in a Vagrant/Chef-solo vm

I'm having some trouble doing a bundle install of my rails app dependencies as part of the vagrant up configuring my VM.

The problem I'm having is getting the bundle install to run as the correct user (vagrant) with correct rvm.

So my rvm is set up via Berkshelf with the following configuration:

  rvm: {
    rubies: ['2.0.0'],
    default_ruby: '2.0.0',
    global_gems: [{ name: 'bundler' },
                    { name: 'rake' }],
    vagrant: { system_chef_solo: '/usr/bin/chef-solo' }
  }

and I'm using the following RVM recipes:

chef.run_list = [
  'recipe[apt]',
  'recipe[rvm]',
  'recipe[rvm::vagrant]',
  'recipe[rvm::system]',
  'recipe[rvm::gem_package]',
  'recipe[mongodb::10gen_repo]',
  'recipe[mongodb]',
  'recipe[nodejs]',
  'recipe[phantomjs]',
  'recipe[lasso-plan]'
]

(note lasso-plan is my own local one)

now if I try:

execute 'bundle install' do
  cwd node['lasso_plan']['path']
  user 'vagrant'
end

or

rvm_shell "bundle the gems" do 
  cwd node['lasso_plan']['path']
  code 'bundle install'
  user 'vagrant'
end

I get

STDERR: /usr/local/rvm/rubies/ruby-2.0.0-p247/lib/ruby/site_ruby/2.0.0/rubygems/dependency.rb:296:in `to_specs': Could not find 'bundler' (>= 0) among 74 total gem(s) (Gem::LoadError)`

whereas without the user declaration it seems to install the gems fine but they are not accessible as the vagrant user.

I'm assuming this is because of rvm and it treating users differently, if anyone could shed any light on how to move forward with this that would be great.

Thanks

Update

So after some help (thank you) I've made some progress with this and I've found a similar issue reported before: http://lists.opscode.com/sympa/arc/chef/2012-05/msg00153.html

At the moment I've got a bit of work around going by running:

rvm_shell 'bundle the gems' do 
  ruby_string ruby_version
  cwd node['lasso_plan']['path']
  code 'bundle install --path .bundle'
  user 'vagrant'
end 

Which allows the bundles to be installed in the directory of my app, it has the downside that the bins aren't in a path but this can be resolved with bundle exec

If I run:

rvm_shell 'bundle the gems' do 
  ruby_string ruby_version
  cwd node['lasso_plan']['path']
  code 'bundle install'
  user 'vagrant'
end

I get the following error:

STDERR: /usr/local/rvm/rubies/ruby-2.0.0-p247/lib/ruby/2.0.0/fileutils.rb:245:in `mkdir': Permission denied - /root/.bundler (Errno::EACCES)
  from /usr/local/rvm/rubies/ruby-2.0.0-p247/lib/ruby/2.0.0/fileutils.rb:245:in `fu_mkdir'
  from /usr/local/rvm/rubies/ruby-2.0.0-p247/lib/ruby/2.0.0/fileutils.rb:219:in `block (2 levels) in mkdir_p'
  from /usr/local/rvm/rubies/ruby-2.0.0-p247/lib/ruby/2.0.0/fileutils.rb:217:in `reverse_each'
  from /usr/local/rvm/rubies/ruby-2.0.0-p247/lib/ruby/2.0.0/fileutils.rb:217:in `block in mkdir_p'
  from /usr/local/rvm/rubies/ruby-2.0.0-p247/lib/ruby/2.0.0/fileutils.rb:203:in `each'
  from /usr/local/rvm/rubies/ruby-2.0.0-p247/lib/ruby/2.0.0/fileutils.rb:203:in `mkdir_p'
  from /usr/local/rvm/gems/ruby-2.0.0-p247@global/gems/bundler-1.3.5/lib/bundler/source/git/git_proxy.rb:46:in `checkout'
  from /usr/local/rvm/gems/ruby-2.0.0-p247@global/gems/bundler-1.3.5/lib/bundler/source/git.rb:144:in `specs'
  from /usr/local/rvm/gems/ruby-2.0.0-p247@global/gems/bundler-1.3.5/lib/bundler/lazy_specification.rb:52:in `__materialize__'
  from /usr/local/rvm/gems/ruby-2.0.0-p247@global/gems/bundler-1.3.5/lib/bundler/spec_set.rb:88:in `block in materialize'
  from /usr/local/rvm/gems/ruby-2.0.0-p247@global/gems/bundler-1.3.5/lib/bundler/spec_set.rb:85:in `map!'
  from /usr/local/rvm/gems/ruby-2.0.0-p247@global/gems/bundler-1.3.5/lib/bundler/spec_set.rb:85:in `materialize'
  from /usr/local/rvm/gems/ruby-2.0.0-p247@global/gems/bundler-1.3.5/lib/bundler/definition.rb:114:in `specs'
  from /usr/local/rvm/gems/ruby-2.0.0-p247@global/gems/bundler-1.3.5/lib/bundler/definition.rb:109:in `resolve_remotely!'
  from /usr/local/rvm/gems/ruby-2.0.0-p247@global/gems/bundler-1.3.5/lib/bundler/installer.rb:83:in `run'
  from /usr/local/rvm/gems/ruby-2.0.0-p247@global/gems/bundler-1.3.5/lib/bundler/installer.rb:14:in `install'
  from /usr/local/rvm/gems/ruby-2.0.0-p247@global/gems/bundler-1.3.5/lib/bundler/cli.rb:247:in `install'
  from /usr/local/rvm/gems/ruby-2.0.0-p247@global/gems/bundler-1.3.5/lib/bundler/vendor/thor/task.rb:27:in `run'
  from /usr/local/rvm/gems/ruby-2.0.0-p247@global/gems/bundler-1.3.5/lib/bundler/vendor/thor/invocation.rb:120:in `invoke_task'
  from /usr/local/rvm/gems/ruby-2.0.0-p247@global/gems/bundler-1.3.5/lib/bundler/vendor/thor.rb:344:in `dispatch'
  from /usr/local/rvm/gems/ruby-2.0.0-p247@global/gems/bundler-1.3.5/lib/bundler/vendor/thor/base.rb:434:in `start'
  from /usr/local/rvm/gems/ruby-2.0.0-p247@global/gems/bundler-1.3.5/bin/bundle:20:in `block in <top (required)>'
  from /usr/local/rvm/gems/ruby-2.0.0-p247@global/gems/bundler-1.3.5/lib/bundler/friendly_errors.rb:3:in `with_friendly_errors'
  from /usr/local/rvm/gems/ruby-2.0.0-p247@global/gems/bundler-1.3.5/bin/bundle:20:in `<top (required)>'
  from /usr/local/rvm/gems/ruby-2.0.0-p247@global/bin/bundle:23:in `load'
  from /usr/local/rvm/gems/ruby-2.0.0-p247@global/bin/bundle:23:in `<main>'
  from /usr/local/rvm/gems/ruby-2.0.0-p247@global/bin/ruby_noexec_wrapper:14:in `eval'
  from /usr/local/rvm/gems/ruby-2.0.0-p247@global/bin/ruby_noexec_wrapper:14:in `<main>'
---- End output of "bash"  "/tmp/chef-script20130711-23336-uqohfl" ----

Which I think might be to do with the sudo problems outlined here: https://github.com/bundler/bundler/blob/master/man/bundle-install.ronn as I'm assuming chef uses sudo to run as the vagrant user and it may be thinking the home directory is /root/

and then if I don't specify the user and run it as root:

rvm_shell 'bundle the gems' do 
  ruby_string ruby_version
  cwd node['lasso_plan']['path']
  code 'bundle install'
end

It installs most of them but not ones where I'm referencing git repositories and errors about the missing ones not existing like so:

Gems included by the bundle:
git://github.com/bkeepers/dotenv.git (at master) is not checked out. Please run
`bundle install`

I'm not sure if there would be other implications to having the bundle installed by root and not the vagrant user if I wanted to run bundle install again

So ideally I want to install to the gem path rather than into .bundle in the app (or if I can't at least understand why) as the user. Thanks for the help

It's been a while since I last tried to use this cookbook, but here's what I remember...

The global_gems array puts those gems in a gemset called global . In your case, they would be placed in ruby 2.0.0@global .

If you set that as your default_ruby you might fix your problem with the least effort (disclaimer -- I haven't tested it, but it should work).

Another option would be to leverage the gems attribute, setting it to something like:

node['rvm']['gems'] = {
  '2.0.0' => [
    { name: 'bundler' },
    { name: 'rake' }
  ]
}

This latter option should install the gems without a gemset, thus letting them be found in your current default_ruby .

Update:

Your problem must be that the bundle install commands are running outside of RVM, thus using Chef's embedded Ruby. Try "sourcing" the rvm profile script prior to running bundle:

execute 'bundle install' do
  cwd node['lasso_plan']['path']
  command <-EOC
    . /etc/profile.d/rvm.sh;
    bundle install
  EOC
  user 'vagrant'
end

You might need to change /etc/profile.d/rvm.sh to wherever RVM has its profile script installed.

I had the exact same problem. When installing normally using the system install into /usr/local/rvm it would skip custom GitHub gems (for some reason??). But when trying to install as the vagrant user, I would get the Permission denied - /root/.bundler error. Here's what I came up with:

rvm_shell "bundle" do
  ruby_string node[:rvm][:default_ruby]
  user        "vagrant"
  group       "vagrant"
  cwd         "/home/vagrant/my_app"
  code        <<-EOF
    bundle install --path .bundle
  EOF
end

Using the --path directive installs gems into the current working directory. It seems to work. Gems will have to be installed independently for each app using this method, and everything must be run with bundle exec all the time from that directory, but it seems to work.

I used OpsWorks and had the same issue. I got it solved successfully following suggestion from cbl, but I used rvmsudo bundle install instead of only bundle install :

execute 'bundle install' do
  cwd node['lasso_plan']['path']
  command <<-EOF
    source /etc/profile.d/rvm.sh
    rvmsudo bundle install
  EOF
  user 'vagrant'
end

The reason of using rvmsudo is that, I think, some gems try to create directories at places that do not allow normal users.

execute "bundle install" do                                                                                                                                                                      
  cwd "/vagrant"                                                                                                                                                                                 
  command "/usr/local/rvm/bin/rvm ruby-2.1.2 do /usr/bin/env bundle install"                                                                                                                     
  action :nothing                                                                                                                                                                                
end

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM