ruby_block documentation wrong; how to set up a RunContext?


#1

I have a library that needs to set up a raw ruby resource, similar to how
it’s documented here: http://docs.opscode.com/chef/resources.html#ruby-block.
Unfortunately, part of that is setting up the RunContext, which I cannot
find properly documented anywhere.

The code snippet run_context = Chef::RunContext.new(node, {}) fails with
an ArgumentError wrong number of arguments (2 for 3), which after looking
at the raw RunContext code, is correct - it wants a third, undocumented
argument called “events”.

How do I instantiate such an object to pass to the RunContext so I can get
my resource to work correctly? Can someone point me at some documentation
or an example for this?

Thank you!
Brian


#2

a run context object expects a node object at leas (rc=
Chef::RunContext.new(node, nil, nil)) , check the setup_run_context method
inside lib/chef/client.rb

On Wed, Sep 25, 2013 at 11:23 AM, Brian Hatfield bmhatfield@gmail.comwrote:

I have a library that needs to set up a raw ruby resource, similar to how
it’s documented here:
http://docs.opscode.com/chef/resources.html#ruby-block. Unfortunately,
part of that is setting up the RunContext, which I cannot find properly
documented anywhere.

The code snippet run_context = Chef::RunContext.new(node, {}) fails with
an ArgumentError wrong number of arguments (2 for 3), which after looking
at the raw RunContext code, is correct - it wants a third, undocumented
argument called “events”.

How do I instantiate such an object to pass to the RunContext so I can get
my resource to work correctly? Can someone point me at some documentation
or an example for this?

Thank you!
Brian


#3

Some more example:

  1. set up run context of a node externally (gran ohai 's json to build the
    node, pass it to the run context), i use this to build chef resource list
    of a container inside its host, outside itself
    https://github.com/ranjib/jugaad/blob/master/lib/jugaad/lxc/extensions/chef.rb#L117
  2. Also you can directly subclass Chef::Client to reuse all the batteries,
    https://github.com/ranjib/chef-stage/blob/master/libraries/staged_chef_client.rb
  3. I have not checked, but i think use_inline_resources implementation also
    has some zen, @kallistec will be able to shed some light on that

On Wed, Sep 25, 2013 at 11:41 AM, Ranjib Dey dey.ranjib@gmail.com wrote:

a run context object expects a node object at leas (rc=
Chef::RunContext.new(node, nil, nil)) , check the setup_run_context method
inside lib/chef/client.rb

On Wed, Sep 25, 2013 at 11:23 AM, Brian Hatfield bmhatfield@gmail.comwrote:

I have a library that needs to set up a raw ruby resource, similar to how
it’s documented here:
http://docs.opscode.com/chef/resources.html#ruby-block. Unfortunately,
part of that is setting up the RunContext, which I cannot find properly
documented anywhere.

The code snippet run_context = Chef::RunContext.new(node, {}) fails
with an ArgumentError wrong number of arguments (2 for 3), which after
looking at the raw RunContext code, is correct - it wants a third,
undocumented argument called “events”.

How do I instantiate such an object to pass to the RunContext so I can
get my resource to work correctly? Can someone point me at some
documentation or an example for this?

Thank you!
Brian


#4

Thank you for you response!

Is it reasonable to just nil the events dispatcher out? I’m struggling to
understand what it does in a concrete manner - I’d be delighted to RTFM if
there’s some overview documentation about how I interact with it somewhere
that I cannot find.

If I wanted to just use an existing events dispatcher during a run, how
would I access it? Is that even a reasonable question, or should I be
making a new one if I am making a new run context?

Thanks again,
Brian

On Wed, Sep 25, 2013 at 2:41 PM, Ranjib Dey dey.ranjib@gmail.com wrote:

a run context object expects a node object at leas (rc=
Chef::RunContext.new(node, nil, nil)) , check the setup_run_context method
inside lib/chef/client.rb

On Wed, Sep 25, 2013 at 11:23 AM, Brian Hatfield bmhatfield@gmail.comwrote:

I have a library that needs to set up a raw ruby resource, similar to how
it’s documented here:
http://docs.opscode.com/chef/resources.html#ruby-block. Unfortunately,
part of that is setting up the RunContext, which I cannot find properly
documented anywhere.

The code snippet run_context = Chef::RunContext.new(node, {}) fails
with an ArgumentError wrong number of arguments (2 for 3), which after
looking at the raw RunContext code, is correct - it wants a third,
undocumented argument called “events”.

How do I instantiate such an object to pass to the RunContext so I can
get my resource to work correctly? Can someone point me at some
documentation or an example for this?

Thank you!
Brian


#5

event dispatchers provides a generic event handler framework, formatters
for example uses this. Depending upon what you are trying to do (or what
api you are exposing via your modules) you can either reuse the existing
handler or just pass nil. If you are exposing a single resource with all
idempotency implementation inside your provider, you might create a run
context from scratch , with no event dispatcher and use appropriate chef
helpers (like converge_by, updated_by_last_action etc) to expose state
change, and chef can take care of the rest. Other wise you can take the
use_inline_resource like route where your sub-context will be constructed
from current run context, this is a proxy abstractions, as in your
abstraction will be broken down into individual resources (that your
abstractions define) transparently, and the full blown
run_context.resource_collection will show all those resources.

i cant find the link, but there was a superb post by yfeldbum on similar
lines?

On Wed, Sep 25, 2013 at 11:48 AM, Brian Hatfield bmhatfield@gmail.comwrote:

Thank you for you response!

Is it reasonable to just nil the events dispatcher out? I’m struggling to
understand what it does in a concrete manner - I’d be delighted to RTFM if
there’s some overview documentation about how I interact with it somewhere
that I cannot find.

If I wanted to just use an existing events dispatcher during a run, how
would I access it? Is that even a reasonable question, or should I be
making a new one if I am making a new run context?

Thanks again,
Brian

On Wed, Sep 25, 2013 at 2:41 PM, Ranjib Dey dey.ranjib@gmail.com wrote:

a run context object expects a node object at leas (rc=
Chef::RunContext.new(node, nil, nil)) , check the setup_run_context method
inside lib/chef/client.rb

On Wed, Sep 25, 2013 at 11:23 AM, Brian Hatfield bmhatfield@gmail.comwrote:

I have a library that needs to set up a raw ruby resource, similar to
how it’s documented here:
http://docs.opscode.com/chef/resources.html#ruby-block. Unfortunately,
part of that is setting up the RunContext, which I cannot find properly
documented anywhere.

The code snippet run_context = Chef::RunContext.new(node, {}) fails
with an ArgumentError wrong number of arguments (2 for 3), which after
looking at the raw RunContext code, is correct - it wants a third,
undocumented argument called “events”.

How do I instantiate such an object to pass to the RunContext so I can
get my resource to work correctly? Can someone point me at some
documentation or an example for this?

Thank you!
Brian


#6

On Wednesday, September 25, 2013 at 11:48 AM, Brian Hatfield wrote:

Thank you for you response!

Is it reasonable to just nil the events dispatcher out? I’m struggling to understand what it does in a concrete manner - I’d be delighted to RTFM if there’s some overview documentation about how I interact with it somewhere that I cannot find.
The event dispatcher provides callbacks for all manner of events that happen in a Chef run, and the formatter output is built on top of this API. If you pass the existing event dispatcher in to a new run context, you’ll get formatted output about what happens in the nested Chef run.

Event dispatch code inherits from this base class: https://github.com/opscode/chef/blob/master/lib/chef/event_dispatch/base.rb which is well commented.

The main dispatcher you pass around is one of these: https://github.com/opscode/chef/blob/master/lib/chef/event_dispatch/dispatcher.rb which implements a heavily simplified publish subscribe pattern to publish data generated by Chef to various event handling implementations (like formatters).

If I wanted to just use an existing events dispatcher during a run, how would I access it? Is that even a reasonable question, or should I be making a new one if I am making a new run context?
RunContext#events (from the outer run context) will give you the existing one.

Thanks again,
Brian


Daniel DeLeo