Question

I'm configuring a vagrant box using chef and then deploying my app using capistrano.

my chef recipe to install bundler is the following:

include_recipe "rbenv"

include_recipe "rbenv::ruby_build"

rbenv_ruby node[:rbenv][:ruby] do 
  global true
end

rbenv_gem "bundler" do
  ruby_version node[:rbenv][:ruby]
end

using the Riot rbenv cookbook

When I try using the bundle with capistrano I figured that the executed bundle is one that was preinstalled on the box, rather than the one installed by chef.

Here's the problem:

$ which bundle
/opt/rbenv/shims/bundle
$ sudo which bundle
/usr/bin/bundle
$ sudo su -
root# which bundle
/opt/rbenv/shims/bundle

Basically it appears that Capistrano runs the commands using the second form, were it uses the /usr/bin/bundle.

I've tried running:

$ sudo bundle
/usr/lib/ruby/vendor_ruby/bundler/rubygems_ext.rb:8:in `require': no such file to load -- rubygems (LoadError)
    from /usr/lib/ruby/vendor_ruby/bundler/rubygems_ext.rb:8
    from /usr/lib/ruby/vendor_ruby/bundler.rb:11:in `require'
    from /usr/lib/ruby/vendor_ruby/bundler.rb:11
    from /usr/bin/bundle:4:in `require'
    from /usr/bin/bundle:4

and I obtain exactly the same exception I see in Capistrano.

If I manually uninstall bundler then here's what I obtain:

$ bundle
Bundler::GemfileNotFound
$ sudo bundle
sudo: bundle: command not found
$ sudo su - 
root# bundle
Bundler::GemfileNotFound

UPDATE

I've figured that the whole ruby version is not available for sudoers:

$ ruby -v
ruby 2.0.0p353 (2013-11-22 revision 43784) [x86_64-linux]
$ sudo ruby -v
ruby 1.8.7 (2011-06-30 patchlevel 352) [x86_64-linux]

even more troubles.

Can you help me ?

thanks.

Was it helpful?

Solution 2

Apparently tweaking with the default_env is a possibility, but not the preferred way.

as:

set :default_environment, {
    'PATH' => "/opt/rbenv/shims:/opt/rbenv/bin:$PATH"
}

is not maintained by capistrano and probably even dangerous.

The preferred way is to look into the program one wants to run for a way to fix this.

In particular my problem: the RVM needs to be initialized upon shell startup, and any RVM installation tool will include your .bashrc with the call to the proper init script at the end of the file. Also at the beginning of .bashrc there's a command that exits the script when it's called on a non interactive shell.

Accidentally Capistrano runs the scripts on a non interactive shell, so when it calls RVM commands they are not initialized, in the non interactive shell.

The solution in my case was to edit the .bashrc file in chef so to call the RVM init script before exiting.

OTHER TIPS

The problem is not that the rbenv-managed Ruby is not available to sudoers. The problem is that the system Ruby is used by default, unless overridden by something else -- usually .bashrc or a /etc/profile.d/* or such.

Since capistrano doesn't source any of these files, it uses the system Ruby.

You should tell cap to use the rbenv bin paths before the system ones by adding:

set :default_environment, {
  'PATH' => "/opt/rbenv/shims:/opt/rbenv/bin:$PATH"
}

to your deploy.rb file.

Update

In Capistrano v3, the :default_environment has been renamed to :default_env.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top