RiotGamesCookbooks / rbenv-cookbook

Installs and configures rbenv
http://community.opscode.com/cookbooks/rbenv
Apache License 2.0
137 stars 109 forks source link

Running bundle install with rbenv_execute fails to find gem path #89

Open sentience opened 10 years ago

sentience commented 10 years ago

I’m running into a problem with what I figure must be a pretty common use case.

First, I am using rbenv to install a global Ruby version:

include_recipe "rbenv::default"
include_recipe "rbenv::ruby_build"

rbenv_ruby node[app_name]['ruby_version'] do
  global true
end

Then, I am installing the bundler gem into that Ruby:

rbenv_gem "bundler" do
  ruby_version node[app_name]['ruby_version']
end

Where I get into trouble is when I then use rbenv_execute to run bundle install in my application’s root directory, using that Ruby.

rbenv_execute "bundle install" do
  user node[app_name]['deploy_user']
  cwd node[app_name]['app_dir']
  command "bundle install --system"
  ruby_version node[app_name]['ruby_version']
end

(Note the use of the --system flag, which I added in an attempt to force bundle install to use the default Ruby gem path.)

This bundle install --system command fails when it attempts to create /root/.bundler, which it shouldn’t be creating because I’ve told it to run as my deploy_user (vagrant in this case, which is in the rbenv group and therefore has write access to the gem path).

STDOUT: Fetching source index from https://rubygems.org/
Fetching source index from https://rails-assets.org/
Fetching https://github.com/nicolai86/rails-asset-localization.git
Unfortunately, a fatal error has occurred. Please see the Bundler
troubleshooting documentation at http://bit.ly/bundler-issues. Thanks!
STDERR: /opt/rbenv/versions/2.1.0/lib/ruby/2.1.0/fileutils.rb:250:in `mkdir': Permission denied @ dir_s_mkdir - /root/.bundler (Errno::EACCES)
    from /opt/rbenv/versions/2.1.0/lib/ruby/2.1.0/fileutils.rb:250:in `fu_mkdir'
    from /opt/rbenv/versions/2.1.0/lib/ruby/2.1.0/fileutils.rb:224:in `block (2 levels) in mkdir_p'
    from /opt/rbenv/versions/2.1.0/lib/ruby/2.1.0/fileutils.rb:222:in `reverse_each'
    from /opt/rbenv/versions/2.1.0/lib/ruby/2.1.0/fileutils.rb:222:in `block in mkdir_p'
    from /opt/rbenv/versions/2.1.0/lib/ruby/2.1.0/fileutils.rb:208:in `each'
    from /opt/rbenv/versions/2.1.0/lib/ruby/2.1.0/fileutils.rb:208:in `mkdir_p'
    from /opt/rbenv/versions/2.1.0/lib/ruby/gems/2.1.0/gems/bundler-1.5.3/lib/bundler/source/git/git_proxy.rb:70:in `checkout'
    from /opt/rbenv/versions/2.1.0/lib/ruby/gems/2.1.0/gems/bundler-1.5.3/lib/bundler/source/git.rb:144:in `specs'
    from /opt/rbenv/versions/2.1.0/lib/ruby/gems/2.1.0/gems/bundler-1.5.3/lib/bundler/lazy_specification.rb:53:in `__materialize__'
    from /opt/rbenv/versions/2.1.0/lib/ruby/gems/2.1.0/gems/bundler-1.5.3/lib/bundler/spec_set.rb:88:in `block in materialize'
    from /opt/rbenv/versions/2.1.0/lib/ruby/gems/2.1.0/gems/bundler-1.5.3/lib/bundler/spec_set.rb:85:in `map!'
    from /opt/rbenv/versions/2.1.0/lib/ruby/gems/2.1.0/gems/bundler-1.5.3/lib/bundler/spec_set.rb:85:in `materialize'
    from /opt/rbenv/versions/2.1.0/lib/ruby/gems/2.1.0/gems/bundler-1.5.3/lib/bundler/definition.rb:133:in `specs'
    from /opt/rbenv/versions/2.1.0/lib/ruby/gems/2.1.0/gems/bundler-1.5.3/lib/bundler/definition.rb:122:in `resolve_remotely!'
    from /opt/rbenv/versions/2.1.0/lib/ruby/gems/2.1.0/gems/bundler-1.5.3/lib/bundler/installer.rb:82:in `run'
    from /opt/rbenv/versions/2.1.0/lib/ruby/gems/2.1.0/gems/bundler-1.5.3/lib/bundler/installer.rb:15:in `install'
    from /opt/rbenv/versions/2.1.0/lib/ruby/gems/2.1.0/gems/bundler-1.5.3/lib/bundler/cli.rb:255:in `install'
    from /opt/rbenv/versions/2.1.0/lib/ruby/gems/2.1.0/gems/bundler-1.5.3/lib/bundler/vendor/thor/command.rb:27:in `run'
    from /opt/rbenv/versions/2.1.0/lib/ruby/gems/2.1.0/gems/bundler-1.5.3/lib/bundler/vendor/thor/invocation.rb:121:in `invoke_command'
    from /opt/rbenv/versions/2.1.0/lib/ruby/gems/2.1.0/gems/bundler-1.5.3/lib/bundler/vendor/thor.rb:363:in `dispatch'
    from /opt/rbenv/versions/2.1.0/lib/ruby/gems/2.1.0/gems/bundler-1.5.3/lib/bundler/vendor/thor/base.rb:440:in `start'
    from /opt/rbenv/versions/2.1.0/lib/ruby/gems/2.1.0/gems/bundler-1.5.3/lib/bundler/cli.rb:10:in `start'
    from /opt/rbenv/versions/2.1.0/lib/ruby/gems/2.1.0/gems/bundler-1.5.3/bin/bundle:20:in `block in <top (required)>'
    from /opt/rbenv/versions/2.1.0/lib/ruby/gems/2.1.0/gems/bundler-1.5.3/lib/bundler/friendly_errors.rb:5:in `with_friendly_errors'
    from /opt/rbenv/versions/2.1.0/lib/ruby/gems/2.1.0/gems/bundler-1.5.3/bin/bundle:20:in `<top (required)>'
    from /opt/rbenv/versions/2.1.0/bin/bundle:23:in `load'
    from /opt/rbenv/versions/2.1.0/bin/bundle:23:in `<main>'

If I shell into the machine as vagrant and run bundle install --system by hand, the gems are installed to the correct path.

Any idea why Bundler would be trying to write to that path rather than the Ruby gem path? Is there some more correct way to be running bundle install with my rbenv-installed Ruby?

avit commented 10 years ago

+1 seeing this as well. Here's a test running "printenv" that shows the problem with the execute environment:

https://gist.github.com/avit/c33992107aaaf8e87a3c

avit commented 10 years ago

Had to work around with:

execute "sudo -i -u #{user} bundle install --gemfile=#{File.join deploy_path, 'Gemfile'}"

Without the login shell (which sources rbenv init) and the user's environment there doesn't seem to be much sense to rbenv_execute.

lucke84 commented 10 years ago

+1

docwhat commented 10 years ago

I tried fixing it by adding export HOME=#{node[app]['home']} to the script, but then I get errors like:

   Gem::Exception: Cannot load gem at [/opt/chef/embedded/lib/ruby/gems/1.9.1/cache/minitest-5.3.3.gem] in /opt/myapp

That implies that it isn't running rbenv's bundle command, but something out of the chef embedded collection instead. Does chef set GEM_HOME and GEM_PATH now and rbenv isn't clearing them out?

s2t2 commented 9 years ago

another fix that worked for me was to assign ownership of rbenv to the user whose bash profile contains the rbenv init:

chown -R ec2-user ${RBENV_DIR}
noklin commented 8 years ago

For me works with added variable environment $HOME. Example:

bash "setup plugin" do cwd node[:redmine][:dir] user node[:redmine][:user] environment 'RAILS_ENV' => 'production' , 'HOME' => node[:redmine][:home]
code <<-EOH bundle install --without development test postgresql sqlite rake redmine:plugins:migrate EOH end