invoking knife in a ruby class

后端 未结 4 1071
迷失自我
迷失自我 2021-02-11 01:38

I\'d like to create a nice wrapper class around knife to allow a program to run knife commands in a readable manner. I\'m currently trying to use the knife.rb file in the chef

4条回答
  •  忘掉有多难
    2021-02-11 02:03

    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.

提交回复
热议问题