- From: Hedge Hog <
>
- To:
- Cc: Daniel DeLeo <
>
- Subject: [chef] Re: Re: Accessing Knife's return values within a script (without shelling out)
- Date: Fri, 15 Apr 2011 15:14:52 +1000
- Domainkey-signature: a=rsa-sha1; c=nofws; d=gmail.com; s=gamma; h=mime-version:in-reply-to:references:date:message-id:subject:from:to :cc:content-type:content-transfer-encoding; b=PBkZFDfour74KOEOPvOj5wwoiQMdd2Zqtpq2ggOtOimimhrpa8NzqoW79HpADnnMYb JHJl+2PkjvDh8Tc/uTG6tvNiaHG68vLqsZ2ZDtinsvMg1XJpUEyubk0wzEDgGKnKJswR tFzygRSKZfrYKuCQ6RVTGAOL3CKOzF1kOUHi8=
On Fri, Apr 15, 2011 at 12:58 AM, Daniel DeLeo
<
>
wrote:
>
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.
In 0.9.12 redefining Chef.Knife.ui to take different argument than
STDOUT, STDERR, STDIN gave mixed results.
If anyone else tries this the following should work:
class ::Chef
class Knife
private
def output(data)
data
end
end
end
This relies on output(data) being the last method called by the each
of the sub command classes' run method - so this may change in later
version of Knife - I haven't checked so YMMV
HTH
>
--
>
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
>
>
--
πόλλ' οἶδ ἀλώπηξ, ἀλλ' ἐχῖνος ἓν μέγα
[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.