invoking knife in a ruby class

后端 未结 4 1064
迷失自我
迷失自我 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.

    0 讨论(0)
  • 2021-02-11 02:05

    You could refer to following solution: http://lists.opscode.com/sympa/arc/chef/2011-08/msg00014.html

        require 'rubygems'
        require "chef"
        require "chef/knife/core/bootstrap_context"
        require 'chef/knife'
        require 'chef/knife/ssh'
        require 'net/ssh'
        require 'net/ssh/multi'
        require 'chef/knife/bootstrap'
    
        Chef::Config.from_file(File.expand_path('~/.chef/knife.rb'))
        kb = Chef::Knife::Bootstrap.new
        kb.name_args = "some.host"
        kb.config[:ssh_user] = "ubuntu"
        kb.config[:run_list] = "role[test]"
        kb.config[:use_sudo] = true
        kb.run
    
    0 讨论(0)
  • 2021-02-11 02:06

    Knife does parsing itself.

    require 'chef/knife'
    Chef::Knife.run(%w(bootstrap -N chef-n1 --sudo -x dan chef-n1.dan.lan))
    
    0 讨论(0)
  • 2021-02-11 02:16

    It looks like Knife is expecting a Hash where you have 'disable-editing'. Try this:

    knife.run(['client', 'create', 'new-client'], {:"disable-editing" => true})
    

    When something like this happens, try looking at the Array/Hash api docs to look for the method the error is spitting out. That will give you an idea of what should be going into that parameter (if you don't have documentation for the library and the source is hard to read).

    0 讨论(0)
提交回复
热议问题