Library gem dependencies

Let’s say a cookbook library requires a gem called treetop (it could be any gem
actually), and so it has this line:

require ‘treetop’

I want to get that installed as a pre-requisite, and so I add this code into
the default recipe:

r = chef_gem “treetop” do
action :nothing
end
r.run_action(:install)

But apparently it won’t get called soon enough. The library has errors about
treetop missing.

As another attempt, I create a different cookbook called pre-requisites. I add
the code “r = chef_gem “treetop” do” into the pre-requisites cookbook, and
have that as a cookbook dependency. Nope. This also doesn’t get called soon
enough. treetop is missing. Installing the treetop gem by hand will fix
it.

The issue seems to be with a library and not with a recipe file, that is the
twist.

I don’t know if I do it correctly, but here’s my answer.

Just do it that way :
chef_gem 'treetop’
require ‘treetop’

It will be installed at the right time before being loaded by the recipe.

Regards,

– Philippe Bérard

-----Message d’origine-----
De : Sam Darwin [mailto:samuel.d.darwin@gmail.com]
Envoyé : mardi 5 mars 2013 10:29
À : chef@lists.opscode.com
Objet : [chef] library gem dependencies

Let’s say a cookbook library requires a gem called treetop (it could be any
gem actually), and so it has this line:

require ‘treetop’

I want to get that installed as a pre-requisite, and so I add this code into
the default recipe:

r = chef_gem “treetop” do
action :nothing
end
r.run_action(:install)

But apparently it won’t get called soon enough. The library has errors
about
treetop missing.

As another attempt, I create a different cookbook called pre-requisites. I
add the code “r = chef_gem “treetop” do” into the pre-requisites cookbook,
and
have that as a cookbook dependency. Nope. This also doesn’t get called
soon
enough. treetop is missing. Installing the treetop gem by hand will
fix
it.

The issue seems to be with a library and not with a recipe file, that is the
twist.

Yup, mine too is working the chef_gem way:

%w(libxml2-dev libxslt1-dev libcurl4-gnutls-dev).each do |pkg|
r = package pkg do
action :nothing
end
r.run_action(:install)
end

chef_gem "aws-sdk"
require 'aws'


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

On Tuesday, March 5, 2013 at 3:17 PM, Philippe Bérard wrote:

I don't know if I do it correctly, but here's my answer.

Just do it that way :
chef_gem 'treetop'
require 'treetop'

It will be installed at the right time before being loaded by the recipe.

Regards,

-- Philippe Bérard

-----Message d'origine-----
De : Sam Darwin [mailto:samuel.d.darwin@gmail.com]
Envoyé : mardi 5 mars 2013 10:29
À : chef@lists.opscode.com (mailto:chef@lists.opscode.com)
Objet : [chef] library gem dependencies

Let's say a cookbook library requires a gem called treetop (it could be any
gem actually), and so it has this line:

require 'treetop'

I want to get that installed as a pre-requisite, and so I add this code into
the default recipe:

r = chef_gem "treetop" do
action :nothing
end
r.run_action(:install)

But apparently it won't get called soon enough. The library has errors
about
treetop missing.

As another attempt, I create a different cookbook called pre-requisites. I
add the code "r = chef_gem "treetop" do" into the pre-requisites cookbook,
and
have that as a cookbook dependency. Nope. This also doesn't get called
soon
enough. treetop is missing. Installing the treetop gem by hand will
fix
it.

The issue seems to be with a library and not with a recipe file, that is the
twist.

In a libraries file, not a recipe file.

On Tue, Mar 5, 2013 at 11:32 AM, Philippe Bérard
philippe.berard@jalios.com wrote:

I don't know if I do it correctly, but here's my answer.

Just do it that way :
chef_gem 'treetop'
require 'treetop'

It will be installed at the right time before being loaded by the recipe.

Regards,

-- Philippe Bérard

-----Message d'origine-----
De : Sam Darwin [mailto:samuel.d.darwin@gmail.com]
Envoyé : mardi 5 mars 2013 10:29
À : chef@lists.opscode.com
Objet : [chef] library gem dependencies

Let's say a cookbook library requires a gem called treetop (it could be any
gem actually), and so it has this line:

require 'treetop'

I want to get that installed as a pre-requisite, and so I add this code into
the default recipe:

r = chef_gem "treetop" do
action :nothing
end
r.run_action(:install)

But apparently it won't get called soon enough. The library has errors
about
treetop missing.

As another attempt, I create a different cookbook called pre-requisites. I
add the code "r = chef_gem "treetop" do" into the pre-requisites cookbook,
and
have that as a cookbook dependency. Nope. This also doesn't get called
soon
enough. treetop is missing. Installing the treetop gem by hand will
fix
it.

The issue seems to be with a library and not with a recipe file, that is the
twist.

I'm afraid to say that it works in a recipe.

Regards,

-- Philippe Bérard

-----Message d'origine-----
De : Sam Darwin [mailto:samuel.d.darwin@gmail.com]
Envoyé : mardi 5 mars 2013 10:38
À : chef@lists.opscode.com; Philippe Bérard
Objet : [chef] Re: library gem dependencies

In a libraries file, not a recipe file.

On Tue, Mar 5, 2013 at 11:32 AM, Philippe Bérard
philippe.berard@jalios.com wrote:

I don't know if I do it correctly, but here's my answer.

Just do it that way :
chef_gem 'treetop'
require 'treetop'

It will be installed at the right time before being loaded by the recipe.

Regards,

-- Philippe Bérard

-----Message d'origine-----
De : Sam Darwin [mailto:samuel.d.darwin@gmail.com]
Envoyé : mardi 5 mars 2013 10:29
À : chef@lists.opscode.com
Objet : [chef] library gem dependencies

Let's say a cookbook library requires a gem called treetop (it could
be any gem actually), and so it has this line:

require 'treetop'

I want to get that installed as a pre-requisite, and so I add this
code into the default recipe:

r = chef_gem "treetop" do
action :nothing
end
r.run_action(:install)

But apparently it won't get called soon enough. The library has errors
about
treetop missing.

As another attempt, I create a different cookbook called
pre-requisites. I add the code "r = chef_gem "treetop" do" into the
pre-requisites cookbook, and
have that as a cookbook dependency. Nope. This also doesn't get called
soon
enough. treetop is missing. Installing the treetop gem by hand will
fix
it.

The issue seems to be with a library and not with a recipe file, that
is the twist.

Libraries are loaded before recipes are compiled, so you can't install
gems for direct use by libraries. What you can do, though, is install
the gems using chef_gem the way you normally would, and then have a
load_dependencies method in your library that requires in all the gems
you needed (warning, untested code follows):

module Cookbook
class MyLibrary
class << self
def load_dependencies
require 'my_gem'
end

    def foo_uses_my_gem bar
        MyLibrary.load_dependencies
        MyGem::do_the_thing_with_the_bar bar
    end
end
end

end

And in your recipe you can do this:

chef_gem 'my_gem'
MyLibrary.foo_users_my_gem node[:cookbook][:bar]

Greg
On Tue 05 Mar 2013 04:10:00 AM CST, Philippe Bérard wrote:

I'm afraid to say that it works in a recipe.

Regards,

-- Philippe Bérard

-----Message d'origine-----
De : Sam Darwin [mailto:samuel.d.darwin@gmail.com]
Envoyé : mardi 5 mars 2013 10:38
À : chef@lists.opscode.com; Philippe Bérard
Objet : [chef] Re: library gem dependencies

In a libraries file, not a recipe file.

On Tue, Mar 5, 2013 at 11:32 AM, Philippe Bérard
philippe.berard@jalios.com wrote:

I don't know if I do it correctly, but here's my answer.

Just do it that way :
chef_gem 'treetop'
require 'treetop'

It will be installed at the right time before being loaded by the recipe.

Regards,

-- Philippe Bérard

-----Message d'origine-----
De : Sam Darwin [mailto:samuel.d.darwin@gmail.com]
Envoyé : mardi 5 mars 2013 10:29
À : chef@lists.opscode.com
Objet : [chef] library gem dependencies

Let's say a cookbook library requires a gem called treetop (it could
be any gem actually), and so it has this line:

require 'treetop'

I want to get that installed as a pre-requisite, and so I add this
code into the default recipe:

r = chef_gem "treetop" do
action :nothing
end
r.run_action(:install)

But apparently it won't get called soon enough. The library has errors
about
treetop missing.

As another attempt, I create a different cookbook called
pre-requisites. I add the code "r = chef_gem "treetop" do" into the
pre-requisites cookbook, and
have that as a cookbook dependency. Nope. This also doesn't get called
soon
enough. treetop is missing. Installing the treetop gem by hand will
fix
it.

The issue seems to be with a library and not with a recipe file, that
is the twist.

1 Like

Very clever Greg! That looks like it would work.