Cookbook name as an attribute

I am trying to pass the cookbook name to the logger via a library and have not been able to figure out how to pull that attribute, does anyone know how a cookbook calls its self in the form of an attribute ?

On Dec 2, 2013, at 9:11 AM, Tim Cook tim.cook@hightail.com wrote:

I am trying to pass the cookbook name to the logger via a library and have not been able to figure out how to pull that attribute, does anyone know how a cookbook calls its self in the form of an attribute ?

Within the context of a recipe or provider you can use #cookbook_name (be warned it is sometimes a Symbol instead of a String). Within a library method you can't get to it though, so you would need to pass it in as a parameter.

--Noah

Within a cookbook the variable "cookbook_name" should contain its own name.

Adam Mielke
System Engineer
Research Computing and Engineering / College of Liberal Arts
University of Minnesota

On Mon, Dec 2, 2013 at 11:11 AM, Tim Cook tim.cook@hightail.com wrote:

I am trying to pass the cookbook name to the logger via a library and
have not been able to figure out how to pull that attribute, does anyone
know how a cookbook calls its self in the form of an attribute ?

On Monday, December 2, 2013 at 9:28 AM, Adam Mielke wrote:

Within a cookbook the variable "cookbook_name" should contain its own name.

This won’t be true in library files, they are evaluated with Ruby’s load function so they don’t have the same contextual stuff that recipes do.

The best you can do here is look at __FILE__ and infer the cookbook name from the file path.

Adam Mielke
System Engineer
Research Computing and Engineering / College of Liberal Arts
University of Minnesota

On Mon, Dec 2, 2013 at 11:11 AM, Tim Cook <tim.cook@hightail.com (mailto:tim.cook@hightail.com)> wrote:

I am trying to pass the cookbook name to the logger via a library and have not been able to figure out how to pull that attribute, does anyone know how a cookbook calls its self in the form of an attribute ?

--
Daniel DeLeo

Cool thanks, worked perfect . I was calling it wrong .

From: Adam Mielke <adam@umn.edumailto:adam@umn.edu>
Reply-To: "chef@lists.opscode.commailto:chef@lists.opscode.com" <chef@lists.opscode.commailto:chef@lists.opscode.com>
Date: Monday, December 2, 2013 9:28 AM
To: "chef@lists.opscode.commailto:chef@lists.opscode.com" <chef@lists.opscode.commailto:chef@lists.opscode.com>
Subject: [chef] Re: cookbook name as an attribute

Within a cookbook the variable “cookbook_name” should contain its own name.

Adam Mielke
System Engineer
Research Computing and Engineering / College of Liberal Arts
University of Minnesota

On Mon, Dec 2, 2013 at 11:11 AM, Tim Cook <tim.cook@hightail.commailto:tim.cook@hightail.com> wrote:
I am trying to pass the cookbook name to the logger via a library and have not been able to figure out how to pull that attribute, does anyone know how a cookbook calls its self in the form of an attribute ?

Why am I unable to pass the cookbook as an instance variable to a library class ?

From: Daniel DeLeo <dan@kallistec.commailto:dan@kallistec.com>
Reply-To: "chef@lists.opscode.commailto:chef@lists.opscode.com" <chef@lists.opscode.commailto:chef@lists.opscode.com>
Date: Monday, December 2, 2013 9:30 AM
To: "chef@lists.opscode.commailto:chef@lists.opscode.com" <chef@lists.opscode.commailto:chef@lists.opscode.com>
Subject: [chef] Re: Re: cookbook name as an attribute

On Monday, December 2, 2013 at 9:28 AM, Adam Mielke wrote:
Within a cookbook the variable “cookbook_name” should contain its own name.

This won’t be true in library files, they are evaluated with Ruby’s load function so they don’t have the same contextual stuff that recipes do.

The best you can do here is look at __FILE__ and infer the cookbook name from the file path.

Adam Mielke
System Engineer
Research Computing and Engineering / College of Liberal Arts
University of Minnesota

On Mon, Dec 2, 2013 at 11:11 AM, Tim Cook <tim.cook@hightail.commailto:tim.cook@hightail.com> wrote:
I am trying to pass the cookbook name to the logger via a library and have not been able to figure out how to pull that attribute, does anyone know how a cookbook calls its self in the form of an attribute ?


Daniel DeLeo

On Monday, December 2, 2013 at 9:44 AM, Tim Cook wrote:

Why am I unable to pass the cookbook as an instance variable to a library class ?

That you can do. In more exact terms:

recipes, attribute files, resource definitions, and LWRP files are all evaluated within a special context. For recipes, this is instance_eval of an already initialized Chef::Recipe object, which already has the node object, run_context and other stuff populated as instance variables with accessor methods you can use.

library files, on the other hand, are evaluated just by calling ruby’s Kernel.load function so they don’t have any of this special context available, so if you need access to the node, run_context or whatever, you have to pass it in from wherever the code gets called.

--
Daniel DeLeo