Making a library global?

Hello,

The wiki says:
“Any libraries you include in a Cookbook
http://wiki.opscode.com/display/chef/Cookbooks will automatically be
required, and available within all your Recipes
http://wiki.opscode.com/display/chef/Recipes, Attribute Files
http://wiki.opscode.com/display/chef/Attributes and Definitions
http://wiki.opscode.com/display/chef/Definitions.”

Is there a way to make a library global so that it’s available to all
cookbooks?

-Paul Choi

Hello!

On Jul 16, 2010, at 4:38 PM, Paul Choi wrote:

The wiki says:
"Any libraries you include in a Cookbook will automatically be required, and available within all your Recipes, Attribute Files and Definitions."

Is there a way to make a library global so that it's available to all cookbooks?

Two ways.

  1. Include the cookbook in the run list of the node, or a role on the node. It doesn't have to do anything - the default.rb can be blank. E.g.,

{ "run_list": [ "recipe[your_library_cookbook]" ] }

That will cause it to be downloaded, and its libraries loaded on the node.

  1. Put the cookbook in the metadata of any cookbooks you want to use the library in. E.g.,

depends "your_library_cookbook"

In metadata.rb of another cookbook where you want to use the library.

--
Opscode, Inc
Joshua Timberman, Senior Solutions Engineer
C: 720.334.RUBY E: joshua@opscode.com

Thanks, Josh!

So because everything is converged when a client runs on a node, a
library is automatically available to any cookbook in the whole node.
Great, good to know!

I think I'll just create a "libraries" cookbook and modify the metadata
to include as needed. It's kinda like having my own lib directory, in a way.

-Paul

On 7/16/10 3:42 PM, Joshua Timberman wrote:

Hello!

On Jul 16, 2010, at 4:38 PM, Paul Choi wrote:

The wiki says:
"Any libraries you include in a Cookbook will automatically be required, and available within all your Recipes, Attribute Files and Definitions."

Is there a way to make a library global so that it's available to all cookbooks?

Two ways.

  1. Include the cookbook in the run list of the node, or a role on the node. It doesn't have to do anything - the default.rb can be blank. E.g.,

{ "run_list": [ "recipe[your_library_cookbook]" ] }

That will cause it to be downloaded, and its libraries loaded on the node.

  1. Put the cookbook in the metadata of any cookbooks you want to use the library in. E.g.,

depends "your_library_cookbook"

In metadata.rb of another cookbook where you want to use the library.

Joshua,

On 17 July 2010 00:42, Joshua Timberman joshua@opscode.com wrote:

Two ways.

  1. Include the cookbook in the run list of the node, or a role on the node. It doesn't have to do anything - the default.rb can be blank. E.g.,

{ "run_list": [ "recipe[your_library_cookbook]" ] }

That will cause it to be downloaded, and its libraries loaded on the node.

  1. Put the cookbook in the metadata of any cookbooks you want to use the library in. E.g.,

depends "your_library_cookbook"

In metadata.rb of another cookbook where you want to use the library.

I have put cookbook "dss" in a role definition and put the role in the
node run list. "dss" depends on "pylabs" (via metadata) and in
"pylabs" I have a library defining an additional type of script
resource:

-- START --
require 'chef/resource/script'

class Chef
class Resource
class QShell < Chef::Resource::Script

  def initialize(name, run_context=nil)
    super
    @resource_name = :pylabs_qshell
    @interpreter = "/opt/qbase3/qshell -f"
  end

end

end
end
-- END --

All required files are in my client cache, but still I get a
NoMethodError on "pylabs_qshell", which is the @resource_name.

2010-07-19_09:41:42.73142 [Mon, 19 Jul 2010 11:41:42 +0200] ERROR: NoMethodError
2010-07-19_09:41:42.73143 [Mon, 19 Jul 2010 11:41:42 +0200] FATAL:
undefined method pylabs_qshell' for #<Chef::Recipe:0x7fc7e53f9aa8> 2010-07-19_09:41:42.73143 /usr/lib/ruby/gems/1.8/gems/chef-0.9.6/bin/../lib/chef/mixin/recipe_definition_dsl_core.rb:55:in method_missing'
2010-07-19_09:41:42.73143
/srv/chef/cache/cookbooks/dss/recipes/pylabs.rb:23:in from_file' 2010-07-19_09:41:42.73144 /usr/lib/ruby/gems/1.8/gems/chef-0.9.6/bin/../lib/chef/cookbook_version.rb:316:in load_recipe'

What am I missing here?

Ringo

-- START --
require 'chef/resource/script'

class Chef
class Resource
class QShell < Chef::Resource::Script

 def initialize(name, run_context=nil)
   super
   @resource_name = :pylabs_qshell
   @interpreter = "/opt/qbase3/qshell -f"
 end

end
end
end
-- END --

All required files are in my client cache, but still I get a
NoMethodError on "pylabs_qshell", which is the @resource_name.

2010-07-19_09:41:42.73142 [Mon, 19 Jul 2010 11:41:42 +0200] ERROR: NoMethodError
2010-07-19_09:41:42.73143 [Mon, 19 Jul 2010 11:41:42 +0200] FATAL:
undefined method pylabs_qshell' for #<Chef::Recipe:0x7fc7e53f9aa8> 2010-07-19_09:41:42.73143 /usr/lib/ruby/gems/1.8/gems/chef-0.9.6/bin/../lib/chef/mixin/recipe_definition_dsl_core.rb:55:in method_missing'
2010-07-19_09:41:42.73143
/srv/chef/cache/cookbooks/dss/recipes/pylabs.rb:23:in from_file' 2010-07-19_09:41:42.73144 /usr/lib/ruby/gems/1.8/gems/chef-0.9.6/bin/../lib/chef/cookbook_version.rb:316:in load_recipe'

What am I missing here?

Ringo

Resources are looked up by finding a constant defined on
Chef::Resource matching the CamelCased name of the method you called,
so calling pylabs_qshell causes chef to look for
Chef::Resource::PylabsQshell. I'm not sure what the resource_name is
used for, but it doesn't manipulate the way the method names map to
resource classes.

Dan DeLeo

Dan,

On 19 July 2010 17:52, Daniel DeLeo dan@kallistec.com wrote:

Resources are looked up by finding a constant defined on
Chef::Resource matching the CamelCased name of the method you called,
so calling pylabs_qshell causes chef to look for
Chef::Resource::PylabsQshell. I'm not sure what the resource_name is
used for, but it doesn't manipulate the way the method names map to
resource classes.

I now passed the Resource problem, but the following error is that no
provider could be found that matches it:

2010-07-20_06:27:14.86605 [Tue, 20 Jul 2010 08:27:14 +0200] ERROR:
Running exception handlers
2010-07-20_06:27:14.86606 [Tue, 20 Jul 2010 08:27:14 +0200] ERROR:
Exception handlers complete
2010-07-20_06:27:14.86606 [Tue, 20 Jul 2010 08:27:14 +0200] ERROR:
Re-raising exception: ArgumentError - Cannot find a provider for
pylabs_qshell[install_devtools] on ubuntu version 10.04
2010-07-20_06:27:14.86608
/usr/lib/ruby/gems/1.8/gems/chef-0.9.6/bin/../lib/chef/platform.rb:328:in
find_provider' 2010-07-20_06:27:14.86609 /usr/lib/ruby/gems/1.8/gems/chef-0.9.6/bin/../lib/chef/platform.rb:256:in find_provider_for_node'
2010-07-20_06:27:14.86610
/usr/lib/ruby/gems/1.8/gems/chef-0.9.6/bin/../lib/chef/runner.rb:39:in
`build_provider'

I didn't create a provider, as my resource inherits from the Script
resource. As far as I can see, my script resource is similar to the
Python and Bash script resource that have a specific provider neither.
Why is this happening? I am really struggling to make progress here!

Ringo

On 20 July 2010 08:37, Ringo De Smet ringo.desmet@gmail.com wrote:

I didn't create a provider, as my resource inherits from the Script
resource. As far as I can see, my script resource is similar to the
Python and Bash script resource that have a specific provider neither.
Why is this happening? I am really struggling to make progress here!

After re-reading the wiki, I found out that the mapping is handled in
Chef::Platform. In my library file, I added the following line:

Chef::Platform::platforms[:default][:pylabs_qshell] = Chef::Provider::Script

Don't know if this is the proper way to do it, but it seems to work.
My script seems to launch (except for errors in the script itself now
:slight_smile: ).

Ringo

On Tue, Jul 20, 2010 at 1:44 AM, Ringo De Smet ringo.desmet@gmail.com wrote:

After re-reading the wiki, I found out that the mapping is handled in
Chef::Platform. In my library file, I added the following line:

Chef::Platform::platforms[:default][:pylabs_qshell] = Chef::Provider::Script

Don't know if this is the proper way to do it, but it seems to work.
My script seems to launch (except for errors in the script itself now
:slight_smile: ).

Ringo

You can also set the @platform variable on your resource, and it will be used.

http://github.com/opscode/chef/blob/master/chef/lib/chef/resource/deploy_revision.rb

HTH,
Dan DeLeo

resource_name is used for lookup and stringification of resources:

@resource_name = :pylabs_qshell

Means you can do:

resources(:pylabs_qshell => "foo")

Adam

On Mon, Jul 19, 2010 at 8:52 AM, Daniel DeLeo dan@kallistec.com wrote:

-- START --
require 'chef/resource/script'

class Chef
class Resource
class QShell < Chef::Resource::Script

 def initialize(name, run_context=nil)
   super
   @resource_name = :pylabs_qshell
   @interpreter = "/opt/qbase3/qshell -f"
 end

end
end
end
-- END --

All required files are in my client cache, but still I get a
NoMethodError on "pylabs_qshell", which is the @resource_name.

2010-07-19_09:41:42.73142 [Mon, 19 Jul 2010 11:41:42 +0200] ERROR: NoMethodError
2010-07-19_09:41:42.73143 [Mon, 19 Jul 2010 11:41:42 +0200] FATAL:
undefined method pylabs_qshell' for #<Chef::Recipe:0x7fc7e53f9aa8> 2010-07-19_09:41:42.73143 /usr/lib/ruby/gems/1.8/gems/chef-0.9.6/bin/../lib/chef/mixin/recipe_definition_dsl_core.rb:55:in method_missing'
2010-07-19_09:41:42.73143
/srv/chef/cache/cookbooks/dss/recipes/pylabs.rb:23:in from_file' 2010-07-19_09:41:42.73144 /usr/lib/ruby/gems/1.8/gems/chef-0.9.6/bin/../lib/chef/cookbook_version.rb:316:in load_recipe'

What am I missing here?

Ringo

Resources are looked up by finding a constant defined on
Chef::Resource matching the CamelCased name of the method you called,
so calling pylabs_qshell causes chef to look for
Chef::Resource::PylabsQshell. I'm not sure what the resource_name is
used for, but it doesn't manipulate the way the method names map to
resource classes.

Dan DeLeo

--
Opscode, Inc.
Adam Jacob, CTO
T: (206) 508-7449 E: adam@opscode.com

Correction!

On Tue, Jul 20, 2010 at 8:28 AM, Daniel DeLeo dan@kallistec.com wrote:

You can also set the @platform variable on your resource, and it will be used.
Actually, it's the @provider variable, and you set it to the name of
the provider class.

http://github.com/opscode/chef/blob/master/chef/lib/chef/resource/deploy_revision.rb

HTH,
Dan DeLeo