How to use Chef::Knife.run(...)?


#1

Hi,
Rather than shell out of a script to execute Knife commands, it should
be possible to call
Chef::Knife.run(cmd_arry, opts)
from within your script.

Maybe I’m tired, or too dense to see the wood for the trees.
But I’m having a devil of a time trying to work out how to pass in the options.

If I pass in “{:no_editor=>true, :file=>”/some/sekret.pem",
:admin=>true, :config_file=>"/etc/chef/knife.rb"}" I get

TypeError: can’t dup TrueClass
/home/hedge/.rvm/gems/ruby-1.9.2-p180@cucken/gems/mixlib-cli-1.2.0/lib/mixlib/cli.rb:95:in
dup' /home/hedge/.rvm/gems/ruby-1.9.2-p180@cucken/gems/mixlib-cli-1.2.0/lib/mixlib/cli.rb:95:inblock in initialize’
/home/hedge/.rvm/gems/ruby-1.9.2-p180@cucken/gems/mixlib-cli-1.2.0/lib/mixlib/cli.rb:94:in
each' /home/hedge/.rvm/gems/ruby-1.9.2-p180@cucken/gems/mixlib-cli-1.2.0/lib/mixlib/cli.rb:94:ininject’
/home/hedge/.rvm/gems/ruby-1.9.2-p180@cucken/gems/mixlib-cli-1.2.0/lib/mixlib/cli.rb:94:in
initialize' /home/hedge/.rvm/gems/ruby-1.9.2-p180@cucken/gems/chef-0.9.12/lib/chef/knife.rb:191:ininitialize’
/home/hedge/.rvm/gems/ruby-1.9.2-p180@cucken/gems/chef-0.9.12/lib/chef/knife.rb:125:in
new' /home/hedge/.rvm/gems/ruby-1.9.2-p180@cucken/gems/chef-0.9.12/lib/chef/knife.rb:125:inrun’

and if I convert all the booleans to strings, I get:

TypeError: can’t convert Symbol into Integer

Appreciate if any one could show how this usage should be written.

Best wishes


πόλλ’ οἶδ ἀλώπηξ, ἀλλ’ ἐχῖνος ἓν μέγα
[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 Fri, Apr 8, 2011 at 3:18 PM, Hedge Hog hedgehogshiatus@gmail.com wrote:

Hi,
Rather than shell out of a script to execute Knife commands, it should
be possible to call
Chef::Knife.run(cmd_arry, opts)
from within your script.

Maybe I’m tired, or too dense to see the wood for the trees.
But I’m having a devil of a time trying to work out how to pass in the options.

If I pass in “{:no_editor=>true, :file=>”/some/sekret.pem",
:admin=>true, :config_file=>"/etc/chef/knife.rb"}" I get

TypeError: can’t dup TrueClass
/home/hedge/.rvm/gems/ruby-1.9.2-p180@cucken/gems/mixlib-cli-1.2.0/lib/mixlib/cli.rb:95:in
dup' /home/hedge/.rvm/gems/ruby-1.9.2-p180@cucken/gems/mixlib-cli-1.2.0/lib/mixlib/cli.rb:95:inblock in initialize’
/home/hedge/.rvm/gems/ruby-1.9.2-p180@cucken/gems/mixlib-cli-1.2.0/lib/mixlib/cli.rb:94:in
each' /home/hedge/.rvm/gems/ruby-1.9.2-p180@cucken/gems/mixlib-cli-1.2.0/lib/mixlib/cli.rb:94:ininject’
/home/hedge/.rvm/gems/ruby-1.9.2-p180@cucken/gems/mixlib-cli-1.2.0/lib/mixlib/cli.rb:94:in
initialize' /home/hedge/.rvm/gems/ruby-1.9.2-p180@cucken/gems/chef-0.9.12/lib/chef/knife.rb:191:ininitialize’
/home/hedge/.rvm/gems/ruby-1.9.2-p180@cucken/gems/chef-0.9.12/lib/chef/knife.rb:125:in
new' /home/hedge/.rvm/gems/ruby-1.9.2-p180@cucken/gems/chef-0.9.12/lib/chef/knife.rb:125:inrun’

and if I convert all the booleans to strings, I get:

TypeError: can’t convert Symbol into Integer

Appreciate if any one could show how this usage should be written.

In case anyone else wishes to use Knife’s cli without shelling out,
this should get you heading in the right direction:

HTH

Best wishes


πόλλ’ οἶδ ἀλώπηξ, ἀλλ’ ἐχῖνος ἓν μέγα
[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


#3

On 11 April 2011 03:13, Hedge Hog hedgehogshiatus@gmail.com wrote:

In case anyone else wishes to use Knife’s cli without shelling out,
this should get you heading in the right direction:

https://gist.github.com/912977

Ew! Really?


Jonathan Matthews
London, UK
http://www.jpluscplusm.com/contact.html


#4

On Mon, Apr 11, 2011 at 5:30 AM, Jonathan Matthews
contact@jpluscplusm.com wrote:

On 11 April 2011 03:13, Hedge Hog hedgehogshiatus@gmail.com wrote:

In case anyone else wishes to use Knife’s cli without shelling out,
this should get you heading in the right direction:

https://gist.github.com/912977

Ew! Really?

If you know ahead of time which knife subcommand you’re running, can
call #run on the subclass directly instead of on Chef::Knife, and just
pass in the array of arguments without the explicit CliTemplate.


Mark J. Reed markjreed@gmail.com


#5

On Mon, Apr 11, 2011 at 8:43 PM, Mark J. Reed markjreed@gmail.com wrote:

On Mon, Apr 11, 2011 at 5:30 AM, Jonathan Matthews
contact@jpluscplusm.com wrote:

On 11 April 2011 03:13, Hedge Hog hedgehogshiatus@gmail.com wrote:

In case anyone else wishes to use Knife’s cli without shelling out,
this should get you heading in the right direction:

https://gist.github.com/912977

Ew! Really?

If you know ahead of time which knife subcommand you’re running, can
call #run on the subclass directly instead of on Chef::Knife, and just
pass in the array of arguments without the explicit CliTemplate.

Actually it is a little easier than all the above, you can call:

::Chef::Application::Knife.new.run

However. Care has to be taken to reset ARGV after you’ve clobbered it
with your desired knife array.
While setting this up is not trivial, if someone comes this way, there
are (in a few days) examples in the cuken API.

HTH


Mark J. Reed markjreed@gmail.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