Calling providers without arguments


#1

Hi guys,

I’m new to Chef, get a bestpractice question.

When using custom provider, which looks like this:

action :upt do
command="uptime"
end

…if I call it with

testcookbook_testprovider do
action :upt
end

I get an error, “You must supply a name when declaring a
testcookbook_testprovider resource”.

So for now I call my resource with

testcookbook_testprovider “foo” do
action :upt
end

…which is kinda ugly. How do I overcome the need in passing “foo”, when I
don’t really need any agruments for running my provider?

Thanks,
Greg


#2

My brief idea (TL;DR): You have to give a name to your ressource (even
with “foo” but I would say “uptime” in this case), the system is
designed this way for some reasons.

Long version on why it is designed this way (my personnal point of
view):

How chef run (two pass):

On first pass (compile) each resource is added to a collection (and a
class instance of the provider is created)

On second pass (converge) each ressource is evaluated (the code of the
provider is run) to see if there’s something to do or not.

The name is a key in the resource collection (the provider class
instance in fact).

In case you define a resource with the same name twice you’ll get a
warning about CHEF-3694 as chef will clone previous resource and update
with your current definition.

Exemple:

sercice “tomcat” do

start_command “/bin/true”

provider

action :stop

end

[… Whatever could be done meanwhile …]

service “tomcat” do

action :start

end

The second service will try to start tomcat but it won’t as the
start_command has been cloned from the first instance.

Dealing with anonymous resources would ends up with some crazy cases.

How to know you should clone the previous resource or not, the provider
could be different (testcookbook_testprovider1 do and
testcookbook_testprovider2) and you may not wish to get the attributes
from the first one.

If you don’t override all the atributes from the first resource you have
to ensure that how the previous resource was declared won’t make the
second fail.

In a logging point of view, dealing with how to tell this provider has
been called and has ended in success or failure would be horrible.

At the end of this, having to name the resource sounds light, easy and
less failproof than trying to deal with thoose edge cases.

Le 2015-02-18 10:37, gregory grey a écrit :

Hi guys,
I’m new to Chef, get a bestpractice question.

When using custom provider, which looks like this:

action :upt do
command="uptime"
end

…if I call it with

testcookbook_testprovider do
action :upt
end

I get an error, “You must supply a name when declaring a testcookbook_testprovider resource”.

So for now I call my resource with

testcookbook_testprovider “foo” do
action :upt
end

…which is kinda ugly. How do I overcome the need in passing “foo”, when I don’t really need any agruments for running my provider?

Thanks,
Greg