How to do conditional checks for gem installation?


#1

Following installs the ruby gems on each chef-client run.

gems = %w(aws-sdk gist)
gems.each do |name|
r = gem_package name do
version node[“dev_tools”]["#{name}_version"]
action :nothing
end

r.run_action(:install)
end

How to apply the not_if conditional checking for the r.run_action(:install) action?


@millisami
~ Sachin Sagar Rai
Ruby on Rails Developer
http://tfm.com.np
http://nepalonrails.tumblr.com
Sent with Sparrow (http://www.sparrowmailapp.com/?sig)


#2

On Thu, Mar 8, 2012 at 4:46 AM, Sachin Sagar Rai millisami@gmail.com wrote:

Following installs the ruby gems on each chef-client run.

gems = %w(aws-sdk gist)
gems.each do |name|
r = gem_package name do
version node[“dev_tools”]["#{name}_version"]
action :nothing
end

r.run_action(:install)
end

How to apply the not_if conditional checking for the
r.run_action(:install) action?

As I mentioned in the other email thread, this pattern is a bit of a
kludge to install the gem immediately so they can be used in a Chef
recipe, and the pattern is replaced with chef_gem in the next version
of Chef. If you don’t need to use the gem in the Chef recipe, use the
standard pattern:

gems = %w(aws-sdk gist)
gems.each do |name|
gem_package name do
version node[“dev_tools”]["#{name}_version"]
not_if "true"
end
end

If you do need them immediately you could use the existing kludge
pattern but wrap the call to run_action in a conditional:

if !system(‘false’)
r.run_action(:install)
end

Bryan


#3

@Bryan

Yup, I want to use the gem immediately as well.
So, did u mean to wrap as the following:

gems = %w(aws-sdk gist)
gems.each do |name|
  gem_package name do
    version node["dev_tools"]["#{name}_version"]
    not_if "true"
  end
  if !system('false')
    r.run_action(:install)
  end
end

@millisami
~ Sachin Sagar Rai
Ruby on Rails Developer
http://tfm.com.np
http://nepalonrails.tumblr.com
Sent with Sparrow (http://www.sparrowmailapp.com/?sig)

On Thursday, March 8, 2012 at 8:30 PM, Bryan McLellan wrote:

On Thu, Mar 8, 2012 at 4:46 AM, Sachin Sagar Rai <millisami@gmail.com (mailto:millisami@gmail.com)> wrote:

Following installs the ruby gems on each chef-client run.

gems = %w(aws-sdk gist)
gems.each do |name|
r = gem_package name do
version node[“dev_tools”]["#{name}_version"]
action :nothing
end

r.run_action(:install)
end

How to apply the not_if conditional checking for the
r.run_action(:install) action?

As I mentioned in the other email thread, this pattern is a bit of a
kludge to install the gem immediately so they can be used in a Chef
recipe, and the pattern is replaced with chef_gem in the next version
of Chef. If you don’t need to use the gem in the Chef recipe, use the
standard pattern:

gems = %w(aws-sdk gist)
gems.each do |name|
gem_package name do
version node[“dev_tools”]["#{name}_version"]
not_if "true"
end
end

If you do need them immediately you could use the existing kludge
pattern but wrap the call to run_action in a conditional:

if !system(‘false’)
r.run_action(:install)
end

Bryan


#4

On Thu, Mar 8, 2012 at 10:03 AM, Sachin Sagar Rai millisami@gmail.com wrote:

Yup, I want to use the gem immediately as well.

The r.run_action(:install) call is a hack that utilizes the fact that
recipes are in Ruby to call the install method on a resource that you
have just created manually without waiting for the phase of the Chef
run that would do so as part of process. This isn’t part of the Chef
DSL and is regular Ruby. gem_package is part of the Chef DSL, but we
can place that object in ‘r’ and then access its methods through ‘r’.
Thus I was suggesting using more ruby to control if that call is made
or not.

gems = %w(aws-sdk gist)
gems.each do |name|
r = gem_package name do
version node[“dev_tools”]["#{name}_version"]
end

if !system(‘false’)
r.run_action(:install)
end
end

Bryan