Shef for recipe writing - maybe


#1

Hi,

I’m looking for some general help here as a chef n00b. I’m getting hung up
while writing recipes with logic because I don’t know how to test my cases.
For instance, in bash if I were going to write some lines that test for a
variable or parses command output I’ll drop to the shell, run the command or
set the variable, test, and verify I’m getting what I expect, then drop back
into vi and write to that output. I’m getting lost while writing recipes
because I don’t know how to do this (and maybe I shouldn’t)

For example, I have a recipe that I want to test if an attribute is set, if
set do something, else, do something else. I know I can do:

if node.tomcat.attribute?(“folder”)
do some stuff
else
do some other stuff
end

But what I don’t know is how to test that attribute first, before committing
that to a recipe. Generally, for ruby scripting, I do that in IRB. Was
kind of hoping shef would provide that for me but I can’t get it to work
like I think it should (and/or don’t understand how it should so I can
change the way I do things). Another case might be verifying that an
attribute has been overridden as expected.

I’ve tried dumping attributes from a node with ‘knife node show
-F json > somefile.json’ and then launching shef and importing those ‘shef
-j somefile.json’ but I still don’t seem to be able to access my attributes.

I’ve tried ‘become_node’ but limited success with that.

I’m not really seeking tech support here. More I’d like to hear from the
people who are knee deep in chef every day. Is shef the IRB of Chef as I
assumed it was? Do you know of any “best practices” guides or how-tos for
writing recipes and the best tools/environment settings for doing so? Am I
completely off in left field (aka, I’m looking at this all wrong)?

Thanks,

Mike G.


#2

Hello!

On Tue, Oct 4, 2011 at 7:03 PM, Michael Glenney mike.glenney@gmail.com wrote:

But what I don’t know is how to test that attribute first, before committing
that to a recipe. Generally, for ruby scripting, I do that in IRB. Was
kind of hoping shef would provide that for me but I can’t get it to work
like I think it should (and/or don’t understand how it should so I can
change the way I do things). Another case might be verifying that an
attribute has been overridden as expected.

I’ve tried dumping attributes from a node with ‘knife node show
-F json > somefile.json’ and then launching shef and importing those ‘shef
-j somefile.json’ but I still don’t seem to be able to access my attributes.

In Shef, you can enter the attributes context:

chef > attributes
chef:attributes >

Which lets you apply attributes to the node object as though you were
modifying a cookbook’s attributes file.

If you want to operate on the node object as though in the context of a recipe:

chef > recipe

Then use the set, default or override methods on the node object to
set the attributes you want for testing purposes.

chef > recipe
chef:recipe > node.set[‘tomcat’][‘folder’] = “/usr/local/tomcat”

To ‘erase’ the attribute value (e.g., test for non-extistence, simply
set it to nil.

I’m not really seeking tech support here. More I’d like to hear from the
people who are knee deep in chef every day. Is shef the IRB of Chef as I
assumed it was? Do you know of any “best practices” guides or how-tos for
writing recipes and the best tools/environment settings for doing so? Am I
completely off in left field (aka, I’m looking at this all wrong)?

Yes. Shef is an IRB console with a bunch of Chef classes already
loaded. It can function in different contexts (attributes, recipe,
main) and modes (solo, client, standalone). I probably use Shef daily
in testing recipe development.

The wiki has:

http://wiki.opscode.com/display/chef/Shef

We’re working on adding a unit on Shef to the Chef Advanced training class.


#3

On Tue, Oct 4, 2011 at 7:13 PM, Joshua Timberman joshua@opscode.com wrote:

Hello!

On Tue, Oct 4, 2011 at 7:03 PM, Michael Glenney mike.glenney@gmail.com
wrote:

But what I don’t know is how to test that attribute first, before
committing
that to a recipe. Generally, for ruby scripting, I do that in IRB. Was
kind of hoping shef would provide that for me but I can’t get it to work
like I think it should (and/or don’t understand how it should so I can
change the way I do things). Another case might be verifying that an
attribute has been overridden as expected.

I’ve tried dumping attributes from a node with ‘knife node show

-F json > somefile.json’ and then launching shef and importing those
’shef
-j somefile.json’ but I still don’t seem to be able to access my
attributes.

In Shef, you can enter the attributes context:

chef > attributes
chef:attributes >

I was kind of hoping for a way to “assume” the attributes already on the
node but I think what you described here may actually work out better
because it gives me better control and allows me to test what I want to
test. Thanks for your reply. I’ll be trying this out today.

In my trial and error I thought what I would want is “become_node”. But I
haven’t been able to get that to work yet. I would start a separate thread
to troubleshoot that but I don’t know what I would use it for. have you
found “become_node” a useful feature in shef? If so what type of tasks is
it useful for?

And let me be the first to vote for a rename of shef. It works well in
writing but around the office we have to spell it out “Hey Bob, you should
try to troubleshoot that recipe in s-h-e-f”, “Does anyone have experience
with this s-h-e-f tool?”, etc. How about “chirb”? Or, keeping with the
theme, “crockpot”? “wok”? :wink:

Which lets you apply attributes to the node object as though you were
modifying a cookbook’s attributes file.

If you want to operate on the node object as though in the context of a
recipe:

chef > recipe

Then use the set, default or override methods on the node object to
set the attributes you want for testing purposes.

chef > recipe
chef:recipe > node.set[‘tomcat’][‘folder’] = “/usr/local/tomcat”

To ‘erase’ the attribute value (e.g., test for non-extistence, simply
set it to nil.

I’m not really seeking tech support here. More I’d like to hear from the
people who are knee deep in chef every day. Is shef the IRB of Chef as I
assumed it was? Do you know of any “best practices” guides or how-tos
for
writing recipes and the best tools/environment settings for doing so? Am
I
completely off in left field (aka, I’m looking at this all wrong)?

Yes. Shef is an IRB console with a bunch of Chef classes already
loaded. It can function in different contexts (attributes, recipe,
main) and modes (solo, client, standalone). I probably use Shef daily
in testing recipe development.

The wiki has:

http://wiki.opscode.com/display/chef/Shef

We’re working on adding a unit on Shef to the Chef Advanced training class.

MG


#4

On Oct 5, 2011, at 12:58 PM, Michael Glenney wrote:

And let me be the first to vote for a rename of shef. It works well in writing but around the office we have to spell it out “Hey Bob, you should try to troubleshoot that recipe in s-h-e-f”, “Does anyone have experience with this s-h-e-f tool?”, etc. How about “chirb”? Or, keeping with the theme, “crockpot”? “wok”? :wink:

I always thought that the pronunciation should be “shee-eff”.

But with Ruby being “rb”, and Interactive Ruby being “irb”, if we were to follow the same model with chef, then we would have “ichef-client” or somesuch. Another alternative might be “chefsh”. I tried translating “test kitchen” into a variety of other languages but didn’t hit on any good solutions. Certain languages like Japanese and Korean weren’t in an ASCII-readable format, so I have no idea what they actually might have said.


Brad Knowles bknowles@ihiji.com
SAGE Level IV, Chef Level 0.0.1