LWRP for N00bs


#1

I’m trying to figure out why Chef is not correctly loading my lwrps.

So I’m trying to build a custom lwrp in my recipe.

I have a cookbook named web_server. I have a resource called webserver. (real
original, I know.).

I’m getting a NameError when I try to use web_serverwebserver in a recipe. I
suspect I’m doing something wrong in my load_current_resource, but I don’t know
what.

my load_current_resource

def load_current_resource
@current_resource = web_serverwebserver.new(@new_resource.AppName)
@current_resource
end
I have tried

web_server::webserver.
Chef::Resource::webserver
Chef::Resource::web_server::webserver
and pretty much everything else I can think of. What am I doing wrong here?

Is there a good way to get Chef to report why it can’t seem to find my
resource?
-l debug doesn’t seem to provide much in the way of detail as to why it can’t
find my lwrp.


#2

web_server_webserver “” { … }

–aj

On Wed, Aug 27, 2014 at 2:45 PM, Brian bbegy@spotlite.com wrote:

I’m trying to figure out why Chef is not correctly loading my lwrps.

So I’m trying to build a custom lwrp in my recipe.

I have a cookbook named web_server. I have a resource called webserver. (real
original, I know.).

I’m getting a NameError when I try to use web_serverwebserver in a recipe. I
suspect I’m doing something wrong in my load_current_resource, but I don’t know
what.

my load_current_resource

def load_current_resource
@current_resource = web_serverwebserver.new(@new_resource.AppName)
@current_resource
end
I have tried

web_server::webserver.
Chef::Resource::webserver
Chef::Resource::web_server::webserver
and pretty much everything else I can think of. What am I doing wrong here?

Is there a good way to get Chef to report why it can’t seem to find my
resource?
-l debug doesn’t seem to provide much in the way of detail as to why it can’t
find my lwrp.


#3

If you just use the libraries directory, it won’t try to infer the
name from the cookbook.

Example:
https://github.com/opscode-cookbooks/httpd/blob/master/libraries/resource_httpd_service.rb#L7-L8

On Tue, Aug 26, 2014 at 10:45 PM, Brian bbegy@spotlite.com wrote:

I’m trying to figure out why Chef is not correctly loading my lwrps.

So I’m trying to build a custom lwrp in my recipe.

I have a cookbook named web_server. I have a resource called webserver. (real
original, I know.).

I’m getting a NameError when I try to use web_serverwebserver in a recipe. I
suspect I’m doing something wrong in my load_current_resource, but I don’t know
what.

my load_current_resource

def load_current_resource
@current_resource = web_serverwebserver.new(@new_resource.AppName)
@current_resource
end
I have tried

web_server::webserver.
Chef::Resource::webserver
Chef::Resource::web_server::webserver
and pretty much everything else I can think of. What am I doing wrong here?

Is there a good way to get Chef to report why it can’t seem to find my
resource?
-l debug doesn’t seem to provide much in the way of detail as to why it can’t
find my lwrp.


#4

The conversion logic is here:

TL;DR: “web_serverwebserver” is gonna get converted to
"WebServerwebserver", so the class you’re looking for is
Chef::Resource::WebServerwebserver

  • Julian

On Tue, Aug 26, 2014 at 10:45 PM, Brian bbegy@spotlite.com wrote:

I’m trying to figure out why Chef is not correctly loading my lwrps.

So I’m trying to build a custom lwrp in my recipe.

I have a cookbook named web_server. I have a resource called webserver. (real
original, I know.).

I’m getting a NameError when I try to use web_serverwebserver in a recipe. I
suspect I’m doing something wrong in my load_current_resource, but I don’t know
what.

my load_current_resource

def load_current_resource
@current_resource = web_serverwebserver.new(@new_resource.AppName)
@current_resource
end
I have tried

web_server::webserver.
Chef::Resource::webserver
Chef::Resource::web_server::webserver
and pretty much everything else I can think of. What am I doing wrong here?

Is there a good way to get Chef to report why it can’t seem to find my
resource?
-l debug doesn’t seem to provide much in the way of detail as to why it can’t
find my lwrp.


[ 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 ]


#5

While we’re on the topic, what are some pros/cons to writing resources in
the “libraries” directory vs “true” LWRP style in the “resources” and
"providers" directories? I see that a lot of Sean’s contributions go the
"libraries" route, but there’s a pretty diverse mix community-wide (with
most seeming to be “true” LWRP style).

-Ameir

On Wed, Aug 27, 2014 at 1:17 AM, Julian C. Dunn jdunn@aquezada.com wrote:

The conversion logic is here:

https://github.com/opscode/chef/blob/master/lib/chef/mixin/convert_to_class_name.rb#L25-L43

TL;DR: “web_serverwebserver” is gonna get converted to
"WebServerwebserver", so the class you’re looking for is
Chef::Resource::WebServerwebserver

  • Julian

On Tue, Aug 26, 2014 at 10:45 PM, Brian bbegy@spotlite.com wrote:

I’m trying to figure out why Chef is not correctly loading my lwrps.

So I’m trying to build a custom lwrp in my recipe.

I have a cookbook named web_server. I have a resource called webserver.
(real
original, I know.).

I’m getting a NameError when I try to use web_serverwebserver in a
recipe. I
suspect I’m doing something wrong in my load_current_resource, but I
don’t know
what.

my load_current_resource

def load_current_resource
@current_resource = web_serverwebserver.new(@new_resource.AppName)
@current_resource
end
I have tried

web_server::webserver.
Chef::Resource::webserver
Chef::Resource::web_server::webserver
and pretty much everything else I can think of. What am I doing wrong
here?

Is there a good way to get Chef to report why it can’t seem to find my
resource?
-l debug doesn’t seem to provide much in the way of detail as to why it
can’t
find my lwrp.


[ 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 ]


#6

When writing Chef::Resource and Chef::Provider instead of using the LWRP
DSL, you can more easily subclass other resources. For example, the
homebrew cookbook does this to make homebrew a provider for the
Chef::Resource::Package resource. Similarly, the runit cookbook does this
to make runit_service a provider for Chef::Resource::Service.

The main advantage that the LWRP style (‘resources’ and ‘providers’
directories) brings is the DSL. This can be used in resources written in
’libraries’ by subclassing Chef::Resource::LWRPBase and
Chef::Provider::LWRPBase. For an example, see our chef-splunk cookbook.
It’s not a lot of extra code, and you get the DSL, which is nice.

On Wed, Aug 27, 2014 at 5:25 AM, Ameir Abdeldayem ameirh@gmail.com wrote:

While we’re on the topic, what are some pros/cons to writing resources in
the “libraries” directory vs “true” LWRP style in the “resources” and
"providers" directories? I see that a lot of Sean’s contributions go the
"libraries" route, but there’s a pretty diverse mix community-wide (with
most seeming to be “true” LWRP style).

-Ameir

On Wed, Aug 27, 2014 at 1:17 AM, Julian C. Dunn jdunn@aquezada.com
wrote:

The conversion logic is here:

https://github.com/opscode/chef/blob/master/lib/chef/mixin/convert_to_class_name.rb#L25-L43

TL;DR: “web_serverwebserver” is gonna get converted to
"WebServerwebserver", so the class you’re looking for is
Chef::Resource::WebServerwebserver

  • Julian

On Tue, Aug 26, 2014 at 10:45 PM, Brian bbegy@spotlite.com wrote:

I’m trying to figure out why Chef is not correctly loading my lwrps.

So I’m trying to build a custom lwrp in my recipe.

I have a cookbook named web_server. I have a resource called webserver.
(real
original, I know.).

I’m getting a NameError when I try to use web_serverwebserver in a
recipe. I
suspect I’m doing something wrong in my load_current_resource, but I
don’t know
what.

my load_current_resource

def load_current_resource
@current_resource = web_serverwebserver.new(@new_resource.AppName)
@current_resource
end
I have tried

web_server::webserver.
Chef::Resource::webserver
Chef::Resource::web_server::webserver
and pretty much everything else I can think of. What am I doing wrong
here?

Is there a good way to get Chef to report why it can’t seem to find my
resource?
-l debug doesn’t seem to provide much in the way of detail as to why it
can’t
find my lwrp.


[ 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 ]


Joshua Timberman, Chef.


#7

Thanks. I’m still getting an error.

(My resource is actually called SpotliteWebServer).
my resource file is /resources/SpotliteWebServer.rb
my resource provider is /provider/SpotliteWebServer.rb

When I call it like this

web_server_SpotliteWebServer “choice” do
AppName “choice”
----SNIP----
end

uninitialized constant Chef::Resource::Webserverspotlitewebserver

The top of the resource is:

def load_current_resource
@current_resource = Chef::Resource::Webserverspotlitewebserver.new(@new_resource.name)
begin
@current_resource.name(new_resource.name)
@current_resource
rescue
Chef::Log.debug(“Cannot find #{new_resource} in the swarm”)
end
end

I’m basically cargo-culting in some of these

Thanks!

Brian

-----Original Message-----
From: Sean OMeara [mailto:someara@opscode.com]
Sent: Tuesday, August 26, 2014 10:04 PM
To: chef@lists.opscode.com
Subject: [chef] Re: LWRP for N00bs

If you just use the libraries directory, it won’t try to infer the name from the cookbook.

Example:
https://github.com/opscode-cookbooks/httpd/blob/master/libraries/resource_httpd_service.rb#L7-L8

On Tue, Aug 26, 2014 at 10:45 PM, Brian bbegy@spotlite.com wrote:

I’m trying to figure out why Chef is not correctly loading my lwrps.

So I’m trying to build a custom lwrp in my recipe.

I have a cookbook named web_server. I have a resource called
webserver. (real original, I know.).

I’m getting a NameError when I try to use web_serverwebserver in a recipe. I
suspect I’m doing something wrong in my load_current_resource, but I
don’t know what.

my load_current_resource

def load_current_resource
@current_resource = web_serverwebserver.new(@new_resource.AppName)
@current_resource
end
I have tried

web_server::webserver.
Chef::Resource::webserver
Chef::Resource::web_server::webserver
and pretty much everything else I can think of. What am I doing wrong here?

Is there a good way to get Chef to report why it can’t seem to find my
resource?
-l debug doesn’t seem to provide much in the way of detail as to why
it can’t find my lwrp.


#8

The answer I got from an expert is “don’t name your LWRP anything but lower case letters or you will have a Bad Time.”

Replacing my intercapping with all lowercase fixed the problem.

Thanks!

Brian

-----Original Message-----
From: Brian Begy [mailto:bbegy@spotlite.com]
Sent: Wednesday, August 27, 2014 9:29 AM
To: chef@lists.opscode.com
Subject: [chef] RE: Re: LWRP for N00bs

Thanks. I’m still getting an error.

(My resource is actually called SpotliteWebServer).
my resource file is /resources/SpotliteWebServer.rb my resource provider is /provider/SpotliteWebServer.rb

When I call it like this

web_server_SpotliteWebServer “choice” do
AppName “choice”
----SNIP----
end

uninitialized constant Chef::Resource::Webserverspotlitewebserver

The top of the resource is:

def load_current_resource
@current_resource = Chef::Resource::Webserverspotlitewebserver.new(@new_resource.name)
begin
@current_resource.name(new_resource.name)
@current_resource
rescue
Chef::Log.debug(“Cannot find #{new_resource} in the swarm”)
end
end

I’m basically cargo-culting in some of these

Thanks!

Brian

-----Original Message-----
From: Sean OMeara [mailto:someara@opscode.com]
Sent: Tuesday, August 26, 2014 10:04 PM
To: chef@lists.opscode.com
Subject: [chef] Re: LWRP for N00bs

If you just use the libraries directory, it won’t try to infer the name from the cookbook.

Example:
https://github.com/opscode-cookbooks/httpd/blob/master/libraries/resource_httpd_service.rb#L7-L8

On Tue, Aug 26, 2014 at 10:45 PM, Brian bbegy@spotlite.com wrote:

I’m trying to figure out why Chef is not correctly loading my lwrps.

So I’m trying to build a custom lwrp in my recipe.

I have a cookbook named web_server. I have a resource called
webserver. (real original, I know.).

I’m getting a NameError when I try to use web_serverwebserver in a recipe. I
suspect I’m doing something wrong in my load_current_resource, but I
don’t know what.

my load_current_resource

def load_current_resource
@current_resource = web_serverwebserver.new(@new_resource.AppName)
@current_resource
end
I have tried

web_server::webserver.
Chef::Resource::webserver
Chef::Resource::web_server::webserver
and pretty much everything else I can think of. What am I doing wrong here?

Is there a good way to get Chef to report why it can’t seem to find my
resource?
-l debug doesn’t seem to provide much in the way of detail as to why
it can’t find my lwrp.


#9

Hello!

One thing I’ve noticed is that folks trying to implement something like a ‘service’ resource always have better luck with an HWRP where they can actually subclass a real service resource class. It seems like LWRPs that try to imitate a service-like resource almost always fall short due to limitations in notifications.

Is it a best practice to prefer HWRPs that extend existing service resources and/or providers over LWRPs that try to expose service-like functionality?

Cheers,

  • Martin

From: Ameir Abdeldayem [ameirh@gmail.com]
Sent: Wednesday, August 27, 2014 6:25 AM
To: chef@lists.opscode.com
Subject: [chef] Re: Re: LWRP for N00bs

While we’re on the topic, what are some pros/cons to writing resources in the “libraries” directory vs “true” LWRP style in the “resources” and “providers” directories? I see that a lot of Sean’s contributions go the “libraries” route, but there’s a pretty diverse mix community-wide (with most seeming to be “true” LWRP style).

-Ameir

On Wed, Aug 27, 2014 at 1:17 AM, Julian C. Dunn <jdunn@aquezada.commailto:jdunn@aquezada.com> wrote:
The conversion logic is here:
https://github.com/opscode/chef/blob/master/lib/chef/mixin/convert_to_class_name.rb#L25-L43

TL;DR: “web_serverwebserver” is gonna get converted to
"WebServerwebserver", so the class you’re looking for is
Chef::Resource::WebServerwebserver

  • Julian

On Tue, Aug 26, 2014 at 10:45 PM, Brian <bbegy@spotlite.commailto:bbegy@spotlite.com> wrote:

I’m trying to figure out why Chef is not correctly loading my lwrps.

So I’m trying to build a custom lwrp in my recipe.

I have a cookbook named web_server. I have a resource called webserver. (real
original, I know.).

I’m getting a NameError when I try to use web_serverwebserver in a recipe. I
suspect I’m doing something wrong in my load_current_resource, but I don’t know
what.

my load_current_resource

def load_current_resource
@current_resource = web_serverwebserver.new(@new_resource.AppName)
@current_resource
end
I have tried

web_server::webserver.
Chef::Resource::webserver
Chef::Resource::web_server::webserver
and pretty much everything else I can think of. What am I doing wrong here?

Is there a good way to get Chef to report why it can’t seem to find my
resource?
-l debug doesn’t seem to provide much in the way of detail as to why it can’t
find my lwrp.


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