[chef] Re: Accessing Knife's return values within a script (without shelling out)


Chronological Thread 
  • From: Daniel DeLeo < >
  • To:
  • Subject: [chef] Re: Accessing Knife's return values within a script (without shelling out)
  • Date: Thu, 14 Apr 2011 07:58:20 -0700

On Thursday, April 14, 2011 at 5:22 AM, Hedge Hog wrote:

Hi,
Having got to run Knife commands from a script, without shelling out,
all that remains is to be able to have whichever Knife method return
the data from the show and list sub-commands.
This too does not seem to be trivial, does anyone have any pointers,
tips or tricks?
Redirecting Knife's puts output is not trivial, Knife uses STDOUT
throughout (tut, tut) rather than the recommended $stdout.
The knife run method has output as its last method rather than
returning the data :(

Knife is an application and not a library. The difference is that knife is designed to print output to a terminal and will call Kernel#exit when it encounters an error. These are not behaviors you want in a library but they make knife much simpler to implement. If you want to use Chef from within a ruby program, you'll have a much easier time if you study the knife commands to see how to use Chef's library classes and then use those directly. That will give you complete flexibility to do whatever your application requires with the output, in case of error, etc.

That said, using knife as a library might be okay for a quick and dirty hack. In Chef 0.10, almost all output from knife goes through a Knife::Core::UI object, which you can access via Knife#ui. UI objects allow you to assign objects to be used for stdin, stdout, and stderr.

-- 
Dan DeLeo
 
Appreciate any help.

--
πόλλ' οἶδ ἀλώπηξ, ἀλλ' ἐχῖνος ἓν μέγα
[The fox knows many things, but the hedgehog knows one big thing.]
  Archilochus, Greek poet (c. 680 BC – c. 645 BC)
http://wiki.hedgehogshiatus.com




Archive powered by MHonArc 2.6.16.

§