Invoking Knife in a Ruby Class

invoking knife in a ruby class

So I was able to solve this problem. It does indeed want a hash, but it wants it to be a subset of the Mixlib::CLI class. So, this is the code needed to create a client via knife programmatically:

    class MyCLI
include Mixlib::CLI
end

#Add the option for disable editing. If you look in knife help, it's --disable-editing
MyCLI.option(:disable_editing, :long => "--disable-editing", :boolean => true)

#instantiate knife object and add the disable-editing flag to it
knife = Chef::Knife.new
knife.options=MyCLI.options

#set up client creation arguments and run
args = ['client', 'create', 'new_client', '--disable-editing' ]
new_client = Chef::Knife.run(args, MyCLI.options)

It's not the most elegant solution, but it does use knife via the command line and saves someone from have to use a system call to use it.

Call knife commands from a Ruby script without shelling out

You can do this by using the Kernel module's system method[1], which will execute your command in a subshell and pipe its output to your current shell. I use this in many of my custom Knife plugins when I want to be able to see the realtime output of programmatically built up arguments Knife commands like knife ssh.

Here's an example where I construct and execute what could be a very complex command:


query = "chef_environment:#{environment.name}"
options = [ "-x #{::Chef::Config[:node_name]}" ].join(' ')
command = %Q{knife ssh "#{query}" #{options} "#{chef_command}"}
system(command)

[1] http://www.ruby-doc.org/core-1.9.3/Kernel.html#method-i-system

chef/knife (ruby) error: superclass mismatch for class Edit (TypeError)

I think this Edit class is creating the issue, and uninstalling knife-essentials gem will solve your problem.

Try it once:

gem uninstall knife-essentials

What is the correct way to use Chef from Ruby (Rails)?

We got stuck on something similar and ended up using the ridley gem:

As per this SO question.

knife configure -i fails

Upgrade to 11.12.4+

The fix is in the Chef code in 11.12.4.rc1, the same 1 line fix as mentioned in user34's answer. Upgrading to 11.12.4+, when released, should solve the problem.

Rich

How do I log every method that's called in a Ruby program?

This is definitely possible -- in fact, there's even a method for it! Just add this somewhere in your code before the point that you want to start logging things:

set_trace_func proc { |event, file, line, id, binding, classname|
printf "%8s %s:%-2d %10s %8s\n", event, file, line, id, classname
}

The secret sauce you want comes from Kernel#set_trace_func, as noted above:

  • set_trace_func(proc) => proc
  • set_trace_func(nil) => nil

Establishes proc as the handler for tracing, or disables tracing if the parameter is nil. proc takes up to six parameters: an event name, a filename, a line number, an object id, a binding, and the name of a class. proc is invoked whenever an event occurs. Events are: c-call (call a C-language routine), c-return (return from a C-language routine), call (call a Ruby method), class (start a class or module definition), end (finish a class or module definition), line (execute code on a new line), raise (raise an exception), and return (return from a Ruby method). Tracing is disabled within the context of proc.

Here's a handy example:

class Test
def test
a = 1
b = 2
end
end

set_trace_func proc { |event, file, line, id, binding, classname|
printf "%8s %s:%-2d %10s %8s\n", event, file, line, id, classname
}

t = Test.new
t.test

(Note: don't try this in irb unless you want a huge scrolling screen of text.) The resulting output is:

    line test.rb:11               false
c-call test.rb:11 new Class
c-call test.rb:11 initialize Object
c-return test.rb:11 initialize Object
c-return test.rb:11 new Class
line test.rb:12 false
call test.rb:2 test Test
line test.rb:3 test Test
line test.rb:4 test Test
return test.rb:4 test Test

You can play around with the formatting string above to get just the results you want to log (for example, it sounds like you're only interested in call events).



Related Topics



Leave a reply



Submit