Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Chef Ohai: how to use newly installed Ruby with gem_package

Using chef I have a simple recipe that installs a gem, example:

gem_package "passenger" do
  version node['passenger']['version']
end

I also want to install ruby with another cookbook, it might be Ruby 1.9.3 for some servers and Ruby Enterprise 1.8.7 for others. So I thought I could use gem_binary and ohai to do this, like this:

gem_package "passenger" do
  version node['passenger']['version']
  gem_binary "#{languages['ruby']['bin_dir']/gem}"
end

But then the problems start, because languages['ruby'] is not changed when a new ruby is installed. Ruby Enterprise installs into /opt/ruby-enterprise and adds itself to PATH via /etc/profile.d/ree.sh but that is not being picked up by ohai during the same run, but gets picked up in the next run.

In the first run, ohai says that languages['ruby'] is installed in /opt/vagrant_ruby/bin/ruby when used with vagrant and chef_solo provision. And the passenger gem is installed into the wrong ruby.

How can I make ohai recognize the newly installed ruby?

like image 706
Evgeny Avatar asked Jul 03 '12 15:07

Evgeny


2 Answers

Usually I am working with RVM which has the same problem. There I usually hardcode the path to the gem binary and leave the last bit as an attribute.

E.G.

something like

  5   node["rvm"]["rubies"].each do |ruby|
  6     gem_package "[#{ruby}]-passenger" do
  7       package_name "passenger"
  8       version node[:passenger][:version]
  9       gem_binary "/usr/local/rvm/bin/gem-#{ruby}"
 10       options "--no-ri --no-rdoc"
 11     end
 12   end

Alternatively we have used bash blocks and sourced the appropriate file. Note when using bash blocks only the last thing in the block will be used to determine success, it is often wise to chain them with &&

like image 149
EnabrenTane Avatar answered Oct 05 '22 07:10

EnabrenTane


I think there's a way to reload ohai attributes during running chef recipe:

You need to use ohai resource:

ohai "reload" do
  action :reload
end

See more here: http://wiki.opscode.com/display/chef/Resources#Resources-Ohai

like image 33
Kostiantyn Avatar answered Oct 05 '22 08:10

Kostiantyn