Accessing Knife's return values within a script (without shelling out)


#1

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 :frowning:

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


#2

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 :frowning:

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


#3

On Fri, Apr 15, 2011 at 12:58 AM, Daniel DeLeo dan@kallistec.com 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 :frowning:

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.

Thanks Dan, I’ll give that a try.


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


#4

On Fri, Apr 15, 2011 at 12:58 AM, Daniel DeLeo dan@kallistec.com 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 :frowning:

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