How to install gems with system wide rbenv by using chef - ruby

I installed rbenv system wide (in /usr/local/rbenv) in my VPS.
And I want to install gems by using chef.
I tried like this, but the gem is installed in system default ruby.
gem_package "daemons" do
action :install
end
How can I install gems in system wide rbenv?
By following coderanger's advice, I installed rbenv cookbook by Berkshelf.
After that I edited recipe file like this:
include_recipe "rbenv::default"
include_recipe "rbenv::ruby_build"
rbenv_ruby "2.0.0-p451"
rbenv_gem "daemons" do
ruby_version "2.0.0-p451"
end
Then I got a error ERROR: No resource, method, or local variable named `rbenv_root_path' for `Chef::Recipe "default"'. How can I solve this problem?

You need to use the rbenv_gem resource.

Related

Ruby Gem conflicts between local gem and system installed gem

I have a Gemfile that looks something like this:
source 'http://rubygems.org'
gemspec
gem "parent_gem", "0.1.3", :path => "#{File.expand_path(__FILE__)}/../vendor/gems/parent_gem-0.1.3"
parent_gem depends on child_gem-1.4.1, which we have packaged in the /vender/gems folder in the package. If a user installs child_gem-1.6.0 on their host on Ubuntu, they will get a Gem::ConflictError because it will try to use 1.6 and log child_gem-1.6.0 conflicts with child_gem (~> 1.4.1). However, if a user runs it on Amazon Linux, it works fine and uses the packaged child gem.
If I run gem list child_gem, I get the following output on both systems:
*** LOCAL GEMS ***
child_gem (1.6.1)
gem list parent_gem returns nothing because it's not installed separated and only exists in the package's vendor directory.
In the files that initiates the script, there is this line:
Gem.use_paths(nil, Gem.path << "/opt/my-app/vendor")
And the /opt/my-app/vendor directory includes child_gem-1.4.1.
Ultimately, I know that setting up the package properly with Bundler should resolve the issue, but I'm trying to understand why this is an issue.
How does Ruby decide where to look for a gem on a system? And where do I look to understand why it works on Amazon Linux but not Ubuntu?

Chef with omnibus using 3rd party gems

Could someone please help with a question regarding use of a 3rd party gem with an omnibus installation running Chef recipes?
I would like to use a 'p4ruby' gem that has been packaged up with omnibus and should be available for use in a recipe using
require 'P4'
I get the error
cannot load such file -- P4
In the installation directory I can see
./embedded/service/gem/ruby/2.1.0/gems/p4ruby-2015.1.0/ext/P4/p4rubyconf.h
./embedded/service/gem/ruby/2.1.0/gems/p4ruby-2015.1.0/ext/P4/p4rubydebug.h
./embedded/service/gem/ruby/2.1.0/cache/p4ruby-2015.1.0.gem
./embedded/service/gem/ruby/2.1.0/specifications/p4ruby-2015.1.0.gemspec
./embedded/service/gem/ruby/2.1.0/build_info/p4ruby-2015.1.0.info
./embedded/lib/ruby/gems/2.1.0/gems/p4ruby-2015.1.0/ext/P4/p4rubyconf.h
./embedded/lib/ruby/gems/2.1.0/gems/p4ruby-2015.1.0/ext/P4/p4rubydebug.h
./embedded/lib/ruby/gems/2.1.0/cache/p4ruby-2015.1.0.gem
I have read through the Chef docs and see that I could use chef_gem or gem_package for example
gem_package 'p4ruby' do
end
However this always tries to compile the gem and this leads to errors in our target installation environment as we cannot expect dev tools such as make and g++ to be available and we do not want to force installation of them.
How can I get Chef to 'see' this gem or to specifiy a 3rd party gem without compilation running? Is this possible?
For further info if I do use gem_package on a machine that does have make and g++ I end up with the following in the install directory (which looks slightly different than before). Is this a path issue?? I've searched all over but cannot find a solution
./embedded/service/gem/ruby/2.1.0/gems/p4ruby-2015.1.0/ext/P4/p4rubyconf.h
./embedded/service/gem/ruby/2.1.0/gems/p4ruby-2015.1.0/ext/P4/p4rubydebug.h
./embedded/service/gem/ruby/2.1.0/cache/p4ruby-2015.1.0.gem
./embedded/service/gem/ruby/2.1.0/specifications/p4ruby-2015.1.0.gemspec
./embedded/service/gem/ruby/2.1.0/build_info/p4ruby-2015.1.0.info
./embedded/lib/ruby/gems/2.1.0/gems/p4ruby-2015.1.0/ext/P4/p4rubyconf.h
./embedded/lib/ruby/gems/2.1.0/gems/p4ruby-2015.1.0/ext/P4/p4rubydebug.h
./embedded/lib/ruby/gems/2.1.0/cache/p4ruby-2015.1.0.gem
./embedded/lib/ruby/gems/2.1.0/specifications/p4ruby-2015.1.0.gemspec
Many thanks for any help
How can I get Chef to 'see' this gem or to specifiy a 3rd party gem
without compilation running? Is this possible?
No, if a gem has some C extension, it must be compiled and will need build tools, that's why there's the build-essential cookbook.
The differences between chef_gem and gem_package are this:
gem_package will install to system ruby (or default to chef embedded ruby is none present) where chef_gem will always install in chef embedded ruby.
gem_package run in converge phase where chef_gem run at compile time
The main reason is that chef_gem is aimed at installing gem to be used in recipes where gem_package is aimed at managing system gems for use out of chef.
Think I found a way of doing this...
$:.unshift *Dir[File.path('<my_path_to_gems>/**/lib')]
I can then
require 'P4'
in my recipes

Different search paths when installing Ruby via RVM

I am using Puppet together with Librarian Puppet. Puppet is installed via the system's package manager, Librarian Puppet via Ruby gem. It only works when I install Ruby (1.9.3) via Debian's package manager. When I use RVM with the identical or another version (>= 1.9.3), I get an error from Librarian, saying that it cannot find puppet. Line 4 require 'puppet' in local.rbcauses the problem in Librarian. The puppet gem-based installation is not recommended and I don't want to do it that way. After some hours debugging, I found out that Ruby's search path differs when you use the package manager or RVM. Using RVM with Ruby 2.1.2, I get:
/usr/local/rvm/rubies/ruby-2.1.2/lib/ruby/site_ruby/2.1.0
/usr/local/rvm/rubies/ruby-2.1.2/lib/ruby/site_ruby/2.1.0/x86_64-linux
/usr/local/rvm/rubies/ruby-2.1.2/lib/ruby/site_ruby
/usr/local/rvm/rubies/ruby-2.1.2/lib/ruby/vendor_ruby/2.1.0
/usr/local/rvm/rubies/ruby-2.1.2/lib/ruby/vendor_ruby/2.1.0/x86_64-linux
/usr/local/rvm/rubies/ruby-2.1.2/lib/ruby/vendor_ruby
/usr/local/rvm/rubies/ruby-2.1.2/lib/ruby/2.1.0
/usr/local/rvm/rubies/ruby-2.1.2/lib/ruby/2.1.0/x86_64-linux
The puppet.rbis located at /usr/lib/ruby/vendor_ruby. This path is part of the search path if I install Ruby via Debian's package manager!
/usr/local/lib/site_ruby
...
/usr/lib/ruby/vendor_ruby
Is there any solution without installing puppet via gem?
librarian-puppet needs the puppet gem to parse the dependencies' Modulefile when using git and path modules. Not for Forge modules though
https://github.com/rodjek/librarian-puppet/blob/master/lib/librarian/puppet/source/local.rb#L8
https://github.com/rodjek/librarian-puppet/pull/62

Local gem repository? Install a ruby gem without 'gem'?

Is it possibile to have something like a 'local' gem repository for Ruby?
I'm working on a custom Linux distribution without admin rights. Ruby is installed on the machine (v.1.8.7) but apparently no 'gem' or 'bundle' or whatever are installed. I need to use some Ruby gems like Nokogiri.
Is it possible to use Ruby gems without installing them through gem install?
Yes. Any gem can be used standalone. You just have to either download the source from github, or download the gem and extract its contents manually.
After you've done that you have to add the lib folder of the gem into the load path ($:) of Ruby. For example:
$:.unshift(File.expand_path("nokogiri-1.6.1/lib"))
require 'nokogiri'
Assuming you are running Ruby in the current directory and the Nokogiri source is in the folder nokogiri-1.6.1.
But remember that first you have to do the same with all Nokogiri prerequisites. I.e. all the libraries Nokogiri depends on.
Another option, which is what I would do, is to install RubyGems in your home directory. That can get a little bit tricky though, but it's possible by downloading the RubyGems package and running something like:
ruby setup.rb --prefix=/home/my_user_name/rubygems
Then you need to set up the ENV variables GEM_HOME and GEM_PATH to point to a location in your home directory where you want all your gems to be installed. See "What's the difference between GEM_HOME and GEM_PATH?" for a description of what they do.
You will also need to add something like /home/my_user_name/rubygems/bin to your PATH so that the gem command can be found by the shell.

chef gem_package not using the right binary

I'm trying to install the gems "sensu_plugin" and "mixlib-shellout", on a windows server 2008r2, from a recipe. In this last one, I have the following code :
%w{ sensu-plugin mixlib-shellout }.each do |gem_plugin|
gem_package gem_plugin do
gem_binary("C:/opt/sensu/embedded/bin/gem")
options("--no-rdoc --no-ri")
action :install
end
end
Yet, when running the recipe on my machine, I receive the error "ERROR: While executing gem ... (Errno::ENOENT) No such file or directory - U:/". What I found out is that whenever I try installing a gem package using the gem binary located in the chef-client directory (C:\opscode\chef\embedded\bin\gem), it raised this last error.
The thing is that the gem I wish to use to installed the gem packages is actually located in C:/opt/sensu/embedded/bin/gem, which I have declared in the gem_binary option.
In the end it looks like chef is ignoring the gem_binary option and trying to install it from its own gem binary.
I can't see what's wrong with this config. Is the gem_binary really indicating what binary to use?

Resources