Class: Psearch
- Inherits:
-
Chef::Knife
- Object
- Chef::Knife
- Psearch
- Includes:
- Chef::Knife::Core::MultiAttributeReturnOption
- Defined in:
- lib/chef/knife/psearch.rb
Constant Summary collapse
- DEFAULT_NODE_HASH =
{ "name" => ["name"], "chef_environment" => ["chef_environment"], "fqdn" => ["fqdn"], "ipaddress" => ["ipaddress"], "run_list" => ["run_list"], "roles" => ["roles"], "recipes" => ["recipes"], "platform" => ["platform"], "tags" => ["tags"] }
- ID_ONLY_HASH =
“id” will be used by the generic presenter automatically when config is true
{ "id" => ["name"] }
Instance Method Summary collapse
- #build_key_hash ⇒ Object
- #build_key_hash_from_attrs(attrs) ⇒ Object
- #create_node(node_data) ⇒ Object
- #print_results(items) ⇒ Object
- #run ⇒ Object
Instance Method Details
#build_key_hash ⇒ Object
109 110 111 112 113 114 115 116 117 118 119 120 |
# File 'lib/chef/knife/psearch.rb', line 109 def build_key_hash key_hash = {} specs = @keys.map { |i| i.split(",") }.flatten specs.each do |spc| name, value = spc.split("=") key_hash[name] = value.split(".") end # This seems like a sane default. The results without the name # are usually not what we want. key_hash["name"] = [ "name" ] unless key_hash.has_key?("name") key_hash end |
#build_key_hash_from_attrs(attrs) ⇒ Object
100 101 102 103 104 105 106 107 |
# File 'lib/chef/knife/psearch.rb', line 100 def build_key_hash_from_attrs(attrs) key_hash = {} attrs.each do |a| key_hash[a] = a.split(".") end key_hash["id"] = [ "name" ] unless key_hash.has_key?("name") key_hash end |
#create_node(node_data) ⇒ Object
130 131 132 133 134 135 |
# File 'lib/chef/knife/psearch.rb', line 130 def create_node(node_data) node_data['attributes'] = node_data.reject do |key, value| ["name", "chef_environment", "run_list"].include?(key) end Chef::Node.json_create(node_data) end |
#print_results(items) ⇒ Object
122 123 124 125 126 127 128 |
# File 'lib/chef/knife/psearch.rb', line 122 def print_results(items) items.each do |res| res = create_node(res) if @inflate_nodes ui.output ui.format_for_display(res) puts "\n" if ! config[:id_only] end end |
#run ⇒ Object
64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 |
# File 'lib/chef/knife/psearch.rb', line 64 def run @index, @search, *@keys = @name_args @inflate_nodes = false args_hash = {} args_hash[:keys] = if config[:id_only] ID_ONLY_HASH elsif ! @keys.empty? build_key_hash elsif ! config[:attribute].nil? && ! config[:attribute].empty? # config[:attribute] comes from the -a option, # which is provided by Knife::Core::MultiAttributeReturnOption build_key_hash_from_attrs(config[:attribute]) elsif @index == "node" DEFAULT_NODE_HASH else ui.warn("Falling back to full search. Use the -a or -i option to enable parital search") nil end # Create output similar to knife-search # in the default case by creating Chef::Node objects # and using the node presenter if args_hash[:keys] == DEFAULT_NODE_HASH && @index == 'node' Chef::Log.debug("Using NodePresenter for output") ui.use_presenter ::Chef::Knife::Core::NodePresenter @inflate_nodes = true end args_hash[:sort] = config[:sort] args_hash[:start] = config[:start] args_hash[:rows] = config[:rows] results = Chef::PartialSearch.new.search(@index, @search, args_hash) print_results(results.first) end |