Using cookbook_name method in cookbooks


#1

Hi all,

I’ve noticed in a lot of cookbook code when needing the cookbook name, folks have been using the hard coded string name.

Eg. In the apache cookbook a defined attribute looks something like this where ‘apache’ is listed 100s of times:

#default apache run directory
default[‘apache’][‘run_dir’] = ‘/var/run/httpd’

Instead of this:

#default apache run directory
default[cookbook_name][‘run_dir’] = '/var/run/httpd’

Obviously, changing a cookbook name is not an everyday thing. However we have a strict naming convention at our shop for cookbooks to clarify the purpose of cookbook (eg. whether the cookbook is a library, wrapper, policy, operating system, etc.). And have had to change the name frequently initially to get right.

Any thoughts on doing this? Are there drawbacks with this?

Thanks,

Cliff


#2

Speculation on my part, but in the context of wrapping a cookbook, wouldn’t the cookbook_name variable become the name of the wrapper cookbook, not the cookbook that the attribute(s) belong to?


Jeff Byrnes
@thejeffbyrnes
Lead DevOps Engineer
EverTrue
704.516.4628

On March 18, 2015 at 8:23:21 AM, Cliff Pracht (cpracht@ebsco.com) wrote:

Hi all,

I’ve noticed in a lot of cookbook code when needing the cookbook name, folks have been using the hard coded string name.

Eg. In the apache cookbook a defined attribute looks something like this where ‘apache’ is listed 100s of times:

#default apache run directory
default[‘apache’][‘run_dir’] = ‘/var/run/httpd’

Instead of this:

#default apache run directory
default[cookbook_name][‘run_dir’] = '/var/run/httpd’

Obviously, changing a cookbook name is not an everyday thing. However we have a strict naming convention at our shop for cookbooks to clarify the purpose of cookbook (eg. whether the cookbook is a library, wrapper, policy, operating system, etc.). And have had to change the name frequently initially to get right.

Any thoughts on doing this? Are there drawbacks with this?

Thanks,

Cliff


#3

Yes true, good point - the attributes (that you want to change) in wrappers would be a different cookbook name. It wouldn’t make sense to use the method in that case.

From: Jeff Byrnes
Reply-To: "chef@lists.opscode.commailto:chef@lists.opscode.com"
Date: Wednesday, March 18, 2015 at 8:26 AM
To: "chef@lists.opscode.commailto:chef@lists.opscode.com"
Subject: [chef] Re: Using cookbook_name method in cookbooks

Speculation on my part, but in the context of wrapping a cookbook, wouldn’t the cookbook_name variable become the name of the wrapper cookbook, not the cookbook that the attribute(s) belong to?


Jeff Byrnes
@thejeffbyrneshttp://twitter.com/thejeffbyrnes
Lead DevOps Engineer
EverTruehttp://www.evertrue.com/
704.516.4628

On March 18, 2015 at 8:23:21 AM, Cliff Pracht (cpracht@ebsco.commailto:cpracht@ebsco.com) wrote:

Hi all,

I’ve noticed in a lot of cookbook code when needing the cookbook name, folks have been using the hard coded string name.

Eg. In the apache cookbook a defined attribute looks something like this where ‘apache’ is listed 100s of times:

#default apache run directory
default[‘apache’][‘run_dir’] = ‘/var/run/httpd’

Instead of this:

#default apache run directory
default[cookbook_name][‘run_dir’] = '/var/run/httpd’

Obviously, changing a cookbook name is not an everyday thing. However we have a strict naming convention at our shop for cookbooks to clarify the purpose of cookbook (eg. whether the cookbook is a library, wrapper, policy, operating system, etc.). And have had to change the name frequently initially to get right.

Any thoughts on doing this? Are there drawbacks with this?

Thanks,

Cliff


#4

There’s a couple other things here.

It was intended, in the apache2 cookbook, that those attributes would be
applicable at that namespace if they were used in, e.g., an apache1dot3
cookbook. Or in an apache3 cookbook, should the world come to that.

In some situations we’ve had attributes that are simply named
inconsistently with the cookbooks, usually because of attribute access
styles. For example, the chef-client cookbook has node['chef_client']
attributes, because some folks like to use node[:chef_client] or
node.chef_client to access the methods, and - (hyphen/dash) is not legal
in Ruby symbols or methods. You’d have to use node[:'chef-client'], but
then you use the supposed benefit of using a symbol (not having to type
quotes).

In other cases, such as with the sudo cookbook where the
node['authorization'] attribute is used, there was some forward thinking
that didn’t pan out. You’d have to search the internet machine, but there
have been discussions in the past where the various tool authors/vendors
have attempted to get some kind of cohesive default attribute space that
explain some of the reasoning behind this.

Hope this helps.
Joshua

On Wed, Mar 18, 2015 at 6:34 AM, Cliff Pracht cpracht@ebsco.com wrote:

Yes true, good point - the attributes (that you want to change) in
wrappers would be a different cookbook name. It wouldn’t make sense to use
the method in that case.

From: Jeff Byrnes
Reply-To: "chef@lists.opscode.com"
Date: Wednesday, March 18, 2015 at 8:26 AM
To: "chef@lists.opscode.com"
Subject: [chef] Re: Using cookbook_name method in cookbooks

Speculation on my part, but in the context of wrapping a cookbook,
wouldn’t the cookbook_name variable become the name of the wrapper
cookbook, not the cookbook that the attribute(s) belong to?


Jeff Byrnes
@thejeffbyrnes http://twitter.com/thejeffbyrnes
Lead DevOps Engineer
EverTrue http://www.evertrue.com/
704.516.4628

On March 18, 2015 at 8:23:21 AM, Cliff Pracht (cpracht@ebsco.com) wrote:

Hi all,

I’ve noticed in a lot of cookbook code when needing the cookbook name,
folks have been using the hard coded string name.

Eg. In the apache cookbook a defined attribute looks something like this
where ‘apache’ is listed 100s of times:

#default apache run directory
default[‘apache’][‘run_dir’] = ‘/var/run/httpd’

Instead of this:

#default apache run directory
default[cookbook_name][‘run_dir’] = '/var/run/httpd’

Obviously, changing a cookbook name is not an everyday thing. However
we have a strict naming convention at our shop for cookbooks to clarify the
purpose of cookbook (eg. whether the cookbook is a library, wrapper,
policy, operating system, etc.). And have had to change the name
frequently initially to get right.

Any thoughts on doing this? Are there drawbacks with this?

Thanks,

Cliff