# LWRP prevents code-reuse

**URL:** <https://discourse.chef.io/t/lwrp-prevents-code-reuse/1455>\
**Category:** Chef Infra (archive)\
**Created:** [June 29, 2010, 12:23pm UTC](https://discourse.chef.io/t/lwrp-prevents-code-reuse/1455 "2010-06-29T12:23:21Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![ringods](https://sea2.discourse-cdn.com/flex016/user_avatar/discourse.chef.io/ringods/32/154_2.png) [@ringods](https://discourse.chef.io/u/ringods)\
**Post date:** [June 29, 2010, 12:23pm UTC](https://discourse.chef.io/t/lwrp-prevents-code-reuse/1455/1 "2010-06-29T12:23:21Z")

</div>

Hello,

Within our company, we use PyLabs (1). This environment has it’s own  
packaging system (qpackages) and it’s own shell (qshell) based on  
ipython. I started a cookbook to install the base environment, and  
that was quickly done. However, I have more problems with the custom  
resources and providers I am trying to implement:

1. a custom resource named “pylabs\_qshell”, based upon the "script"  
resource, similar to “bash” and “python”.
2. a custom resource named “pylabs\_package”, based on the standard  
"Package" resource
3. a custom provider named “pylabs\_package”, extending the standard  
"Package" provider

However, after several try-outs, I ended up looking in the core Chef  
code to see how custom resources/providers worked. If someone packages  
resources/providers in the cookbook, you seem to be obliged to use the  
Light-Weight Resource Provider syntax. The generated classes in LWRP  
are just Resources, not the Script or Package I want it to be. Similar  
problem for the custom Provider.

Eventually, I replaced the LWRP syntax with full-fledged resource and  
provider classes, but the build\_from\_file didn’t seem to like it. What  
is my best option to go forward?

Greetings,

Ringo

(1) [http://www.pylabs.org](http://www.pylabs.org)

---

<div class="post-metadata">

**Author:** ![AJ\_Christensen](https://sea2.discourse-cdn.com/flex016/user_avatar/discourse.chef.io/aj_christensen/32/525_2.png) [@AJ\_Christensen](https://discourse.chef.io/u/AJ_Christensen)\
**Post date:** [June 29, 2010, 10:56pm UTC](https://discourse.chef.io/t/lwrp-prevents-code-reuse/1455/2 "2010-06-29T22:56:11Z")

</div>

Chuck it in a library

On 30 June 2010 00:23, Ringo De Smet [ringo.desmet@gmail.com](mailto:ringo.desmet@gmail.com) wrote:

> Hello,
> 
> Within our company, we use PyLabs (1). This environment has it's own  
> packaging system (qpackages) and it's own shell (qshell) based on  
> ipython. I started a cookbook to install the base environment, and  
> that was quickly done. However, I have more problems with the custom  
> resources and providers I am trying to implement:
> 
> 1. a custom resource named "pylabs\_qshell", based upon the "script"  
> resource, similar to "bash" and "python".
> 2. a custom resource named "pylabs\_package", based on the standard  
> "Package" resource
> 3. a custom provider named "pylabs\_package", extending the standard  
> "Package" provider
> 
> However, after several try-outs, I ended up looking in the core Chef  
> code to see how custom resources/providers worked. If someone packages  
> resources/providers in the cookbook, you seem to be obliged to use the  
> Light-Weight Resource Provider syntax. The generated classes in LWRP  
> are just Resources, not the Script or Package I want it to be. Similar  
> problem for the custom Provider.
> 
> Eventually, I replaced the LWRP syntax with full-fledged resource and  
> provider classes, but the build\_from\_file didn't seem to like it. What  
> is my best option to go forward?
> 
> Greetings,
> 
> Ringo
> 
> (1) [http://www.pylabs.org](http://www.pylabs.org)

---

<div class="post-metadata">

**Author:** ![ringods](https://sea2.discourse-cdn.com/flex016/user_avatar/discourse.chef.io/ringods/32/154_2.png) [@ringods](https://discourse.chef.io/u/ringods)\
**Post date:** [June 30, 2010, 1:46pm UTC](https://discourse.chef.io/t/lwrp-prevents-code-reuse/1455/3 "2010-06-30T13:46:53Z")

</div>

AJ,

On 30 June 2010 00:56, AJ Christensen [aj@junglist.gen.nz](mailto:aj@junglist.gen.nz) wrote:

> Chuck it in a library

Tnx for the tip.

One subsequent question though: the documentation states that  
libraries are automatically "require"-d in your recipes, but what if  
you want to "require" one library into another? I tried the following  
variants:

require "qshell\_resource"  
require "libraries/qshell\_resource"  
require "/libraries/qshell\_resource"

to refer to "/libraries/qshell\_resource.rb".

None of them worked.

Ringo

---

<div class="post-metadata">

**Author:** ![kallistec](https://sea2.discourse-cdn.com/flex016/user_avatar/discourse.chef.io/kallistec/32/23_2.png) [@kallistec](https://discourse.chef.io/u/kallistec)\
**Post date:** [July 1, 2010, 3:50pm UTC](https://discourse.chef.io/t/lwrp-prevents-code-reuse/1455/4 "2010-07-01T15:50:44Z")

</div>

On Wed, Jun 30, 2010 at 6:46 AM, Ringo De Smet [ringo.desmet@gmail.com](mailto:ringo.desmet@gmail.com) wrote:

> AJ,
> 
> On 30 June 2010 00:56, AJ Christensen [aj@junglist.gen.nz](mailto:aj@junglist.gen.nz) wrote:
> 
> > Chuck it in a library
> 
> Tnx for the tip.
> 
> One subsequent question though: the documentation states that  
> libraries are automatically "require"-d in your recipes, but what if  
> you want to "require" one library into another? I tried the following  
> variants:
> 
> require "qshell\_resource"  
> require "libraries/qshell\_resource"  
> require "/libraries/qshell\_resource"
> 
> to refer to "/libraries/qshell\_resource.rb".

Chef will automatically load them, but not in any guaranteed order. If  
they are all in the same cookbook, I'd recommend using something like:

require File.dirname( **FILE** ) + '/qshell\_resource'

> None of them worked.
> 
> Ringo

---

<div class="post-metadata">

**Author:** ![ringods](https://sea2.discourse-cdn.com/flex016/user_avatar/discourse.chef.io/ringods/32/154_2.png) [@ringods](https://discourse.chef.io/u/ringods)\
**Post date:** [July 5, 2010, 3:12pm UTC](https://discourse.chef.io/t/lwrp-prevents-code-reuse/1455/5 "2010-07-05T15:12:49Z")

</div>

Daniel,

On 1 July 2010 17:50, Daniel DeLeo [dan@kallistec.com](mailto:dan@kallistec.com) wrote:

> Chef will automatically load them, but not in any guaranteed order. If  
> they are all in the same cookbook, I'd recommend using something like:
> 
> require File.dirname( **FILE** ) + '/qshell\_resource'

The ruby require worked now. However, on the Chef level, it is a pity  
I can't use my custom script resource in the body of my package  
provider:

def install\_package(name, version)  
pylabs\_qshell "install\_package" do  
user "root"  
code \<\<-EOH  
pkg = q.qp.find('#{new\_resource.package\_name}','#{new\_resource.domain}','#{new\_resource.version}')  
pkg.install()  
EOH  
end  
end

This would be a very elegant implementation. Any more suggestions? 🙂

Ringo

---

<div class="post-metadata">

**Author:** ![kallistec](https://sea2.discourse-cdn.com/flex016/user_avatar/discourse.chef.io/kallistec/32/23_2.png) [@kallistec](https://discourse.chef.io/u/kallistec)\
**Post date:** [July 5, 2010, 11:28pm UTC](https://discourse.chef.io/t/lwrp-prevents-code-reuse/1455/6 "2010-07-05T23:28:18Z")

</div>

On Mon, Jul 5, 2010 at 8:12 AM, Ringo De Smet [ringo.desmet@gmail.com](mailto:ringo.desmet@gmail.com) wrote:

> Daniel,
> 
> On 1 July 2010 17:50, Daniel DeLeo [dan@kallistec.com](mailto:dan@kallistec.com) wrote:
> 
> > Chef will automatically load them, but not in any guaranteed order. If  
> > they are all in the same cookbook, I'd recommend using something like:
> > 
> > require File.dirname( **FILE** ) + '/qshell\_resource'
> 
> The ruby require worked now. However, on the Chef level, it is a pity  
> I can't use my custom script resource in the body of my package  
> provider:
> 
> def install\_package(name, version)  
> pylabs\_qshell "install\_package" do  
> user "root"  
> code \<\<-EOH  
> pkg = q.qp.find('#{new\_resource.package\_name}','#{new\_resource.domain}','#{new\_resource.version}')  
> pkg.install()  
> EOH  
> end  
> end
> 
> This would be a very elegant implementation. Any more suggestions? 🙂

Sure, here's a few:

- Create a resource via something like Chef::ResourcePylabsQshell.new,  
set the values you want and then use run\_action to run it, similar to  
this: [http://github.com/opscode/chef/blob/master/chef/lib/chef/provider/remote\_directory.rb#L99-108](http://github.com/opscode/chef/blob/master/chef/lib/chef/provider/remote_directory.rb#L99-108)
- Use Chef::Provider#recipe\_eval  
[http://github.com/opscode/chef/blob/master/chef/lib/chef/provider.rb#L61-77](http://github.com/opscode/chef/blob/master/chef/lib/chef/provider.rb#L61-77)
- Make your qshell functionality a ruby Module that you could mix in
- Make your qshell functionality into an "implementation class" and do  
the same thing as the previous suggestion, except with composition  
instead of inheritance.

> Ringo
