Accessing Node Object in Libraries


#1

Should I always be able to access the node object in libraries? Julian Dunn wrote a great post on writing libraries herehttp://www.getchef.com/blog/2014/03/12/writing-libraries-in-chef-cookbooks/, but there’s no reference to a node object.

Unfortunately, I don’t have a specific example, but I know I’ve come across cases where I have to pass the node object from a recipe into a library method because the library didn’t know about the node object itself. I believe the differences come into play when you create a proper library using ruby modules (as in Julian’s post) vs. just adding helper methods with no namespacing.

Curtis Stewart
Consultant
m 217.390.5067
Skype cstewart8710
cstewart@momentumsi.commailto:cstewart@momentumsi.com
www.momentumsi.comhttp://www.momentumsi.com/

[cid:71466434-A311-4F82-BC03-F7D6BA0FA526]http://www.momentumsi.com/http://www.momentumsi.com/

Cloud Migration - Architecture - DevOps - Big Data - App Dev


#2

“Should you”: that’s an architectural question for our developers,
presumably. You’re right – I usually pass either the node object itself or
the attributes thereof to a helper, if I need them:

  • Julian

On Fri, May 16, 2014 at 9:10 AM, Stewart, Curtis cstewart@momentumsi.comwrote:

Should I always be able to access the node object in libraries? Julian
Dunn wrote a great post on writing libraries herehttp://www.getchef.com/blog/2014/03/12/writing-libraries-in-chef-cookbooks/,
but there’s no reference to a node object.

Unfortunately, I don’t have a specific example, but I know I’ve come
across cases where I have to pass the node object from a recipe into a
library method because the library didn’t know about the node object
itself. I believe the differences come into play when you create a proper
library using ruby modules (as in Julian’s post) vs. just adding helper
methods with no namespacing.

 *Curtis Stewart*

Consultant
m 217.390.5067
Skype cstewart8710
cstewart@momentumsi.com
www.momentumsi.com


[ Julian C. Dunn jdunn@aquezada.com * Sorry, I’m ]
[ WWW: http://www.aquezada.com/staff/julian * only Web 1.0 ]
[ gopher://sdf.org/1/users/keymaker/ * compliant! ]
[ PGP: 91B3 7A9D 683C 7C16 715F 442C 6065 D533 FDC2 05B9 ]


#3

On Friday, May 16, 2014 at 7:34 AM, Julian C. Dunn wrote:

“Should you”: that’s an architectural question for our developers, presumably. You’re right – I usually pass either the node object itself or the attributes thereof to a helper, if I need them:

https://github.com/juliandunn/oracle-instantclient/blob/master/libraries/utils.rb#L26

  • Julian
    The difference with library files is that they’re 100% plain ruby, evaluated in the context of the top level ruby environment (the TOPLEVEL_BINDING object) so there is no architecturally sound way for Chef to hook into them and add additional context (e.g., access to the node object). By comparison, other file types, such as recipes, attributes, and LWRPs, are evaluated in the context of an object that chef initializes for you, so chef can internally set variables (node, run_context, etc.) before handing control over to your code.


Daniel DeLeo


#4

On Fri, May 16, 2014 at 10:31 AM, Daniel DeLeo dan@kallistec.com
wrote:

On Friday, May 16, 2014 at 7:34 AM, Julian C. Dunn wrote:

“Should you”: that’s an architectural question for our developers,
presumably. You’re right – I usually pass either the node object
itself or the attributes thereof to a helper, if I need them:

[…]

The difference with library files is that they’re 100% plain ruby,
evaluated in the context of the top level ruby environment (the
TOPLEVEL_BINDING object) so there is no architecturally sound way for
Chef to hook into them and add additional context (e.g., access to
the node object). By comparison, other file types, such as recipes,
attributes, and LWRPs, are evaluated in the context of an object that
chef initializes for you, so chef can internally set variables (node,
run_context, etc.) before handing control over to your code.

This behavior allows for some very interesting things we can do to Chef
from cookbooks.

https://github.com/lambda-linux-cookbooks/chef-solo-node-save/blob/master/libraries/node_ext.rb

Please be careful if you are trying this at home! :slight_smile:

Best,
Rajiv


#5

On Fri, May 16, 2014 at 2:26 PM, Rajiv Ranganath
rajiv.ranganath@atihita.com wrote:

This behavior allows for some very interesting things we can do to Chef
from cookbooks.

https://github.com/lambda-linux-cookbooks/chef-solo-node-save/blob/master/libraries/node_ext.rb

Please be careful if you are trying this at home! :slight_smile:

Yup – you can monkeypatch anything in Chef itself from a library.

Good: You can experiment with patches to core without having to modify
the client. (q.v. chef-metal, chef-elevate, others)
Bad: You can blow your foot clean off super easily. :slight_smile:

  • Julian