Using resources in a library?

Hi everyone,

I’m trying to add a gem dependency to a library. I found a couple
conversations talking about using resources in libraries in my travels:

http://stackoverflow.com/questions/21908433/using-class-methods-from-libraries-in-a-recipe

In both, the most reasonable advice is to create an instance of the
resource’s class and call #run_action(:whatever) to get it to execute
immediately. From my testing, this doesn’t work, with the problem
specifically being this code snippet and error:

My first thought was to pass it run_context… But there’s no run_context
at this point for me to pass to the resource’s constructor.

To demonstrate in further detail, I put together a quick cookbook with a
Test Kitchen suite: https://github.com/notnmeyer/gem-dep

Any thoughts? What’s the proper way to immediately execute a resource
during compile time?

Thanks!
Nate

So it looks like if I move the relevant bits
https://github.com/notnmeyer/gem-dep/blob/master/libraries/test.rb#L2-L4
inside of the class's initialize method, it works. Feels like the wrong to
do it this though. Still curious about any opinions on the "right" way to
do this.

On Tue, Oct 6, 2015 at 5:05 PM Nate Meyer nmeyer@gmail.com wrote:

Hi everyone,

I'm trying to add a gem dependency to a library. I found a couple
conversations talking about using resources in libraries in my travels:

chef infra - Using class methods from libraries in a recipe - Stack Overflow

In both, the most reasonable advice is to create an instance of the
resource's class and call #run_action(:whatever) to get it to execute
immediately. From my testing, this doesn't work, with the problem
specifically being this code snippet and error:
blah.md · GitHub

My first thought was to pass it run_context... But there's no run_context
at this point for me to pass to the resource's constructor.

To demonstrate in further detail, I put together a quick cookbook with a
Test Kitchen suite: GitHub - notnmeyer/gem-dep

Any thoughts? What's the proper way to immediately execute a resource
during compile time?

Thanks!
Nate

Pass an empty run_context into the ChefGem.new call, created like this?

run_context = Chef::RunContext.new(Chef::Node.new, {}, Chef::EventDispatch::Dispatcher.new)

From: Nate Meyer
Reply-To: "chef@lists.opscode.commailto:chef@lists.opscode.com"
Date: Tuesday, October 6, 2015 at 5:05 PM
To: "chef@lists.opscode.commailto:chef@lists.opscode.com"
Subject: [chef] Using resources in a library?

Hi everyone,

I’m trying to add a gem dependency to a library. I found a couple conversations talking about using resources in libraries in my travels:

In both, the most reasonable advice is to create an instance of the resource’s class and call #run_action(:whatever) to get it to execute immediately. From my testing, this doesn’t work, with the problem specifically being this code snippet and error: https://gist.github.com/notnmeyer/04bc1c0be0e41504cd62

My first thought was to pass it run_context… But there’s no run_context at this point for me to pass to the resource’s constructor.

To demonstrate in further detail, I put together a quick cookbook with a Test Kitchen suite: https://github.com/notnmeyer/gem-dep

Any thoughts? What’s the proper way to immediately execute a resource during compile time?

Thanks!
Nate

Well that certainly was easy :slight_smile: Thanks Joe!

On Tue, Oct 6, 2015 at 5:21 PM Jos Backus josb@mobileiron.com wrote:

Pass an empty run_context into the ChefGem.new call, created like this?

run_context = Chef::RunContext.new(Chef::Node.new, {},
Chef::EventDispatch::Dispatcher.new)

From: Nate Meyer
Reply-To: "chef@lists.opscode.com"
Date: Tuesday, October 6, 2015 at 5:05 PM
To: "chef@lists.opscode.com"
Subject: [chef] Using resources in a library?

Hi everyone,

I'm trying to add a gem dependency to a library. I found a couple
conversations talking about using resources in libraries in my travels:

chef infra - Using class methods from libraries in a recipe - Stack Overflow

In both, the most reasonable advice is to create an instance of the
resource's class and call #run_action(:whatever) to get it to execute
immediately. From my testing, this doesn't work, with the problem
specifically being this code snippet and error:
blah.md · GitHub

My first thought was to pass it run_context... But there's no run_context
at this point for me to pass to the resource's constructor.

To demonstrate in further detail, I put together a quick cookbook with a
Test Kitchen suite: GitHub - notnmeyer/gem-dep

Any thoughts? What's the proper way to immediately execute a resource
during compile time?

Thanks!
Nate

On Oct 6, 2015, at 5:05 PM, Nate Meyer nmeyer@gmail.com wrote:

Hi everyone,

I'm trying to add a gem dependency to a library. I found a couple conversations talking about using resources in libraries in my travels:

chef infra - Using class methods from libraries in a recipe - Stack Overflow
chef - [chef] Re: Re: Re: Re: Re: How to add ruby lib as depend at LWRP

In both, the most reasonable advice is to create an instance of the resource's class and call #run_action(:whatever) to get it to execute immediately. From my testing, this doesn't work, with the problem specifically being this code snippet and error: blah.md · GitHub

My first thought was to pass it run_context... But there's no run_context at this point for me to pass to the resource's constructor.

To demonstrate in further detail, I put together a quick cookbook with a Test Kitchen suite: GitHub - notnmeyer/gem-dep

Any thoughts? What's the proper way to immediately execute a resource during compile time?

A second option (only works if there are no C extensions in the mix, but if you are trying to early install the gem that should be the case anyway) is to use Halite to convert it to a cookbook. Basically just clone the gem's code, add Halite as a dependency, and either add the Halite rake tasks for make a script to run `Halite.convert('thegemname', 'path/to/write/cookbook') and it will make a Chef cookbook containing the gem's code in such a way that other cookbooks can depend on it and require as normal.

--Noah