Class: Chef::Knife::IndexRebuild

Inherits:
Chef::Knife show all
Defined in:
lib/chef/knife/index_rebuild.rb

Constant Summary

Constants inherited from Chef::Knife

OFFICIAL_PLUGINS

Instance Attribute Summary

Attributes inherited from Chef::Knife

#name_args, #ui

Instance Method Summary collapse

Methods inherited from Chef::Knife

#api_key, #apply_computed_config, category, chef_config_dir, #cli_keys, common_name, #config_file_settings, config_loader, #configure_chef, #create_object, #delete_object, dependency_loaders, deps, #format_rest_error, guess_category, #humanize_exception, #humanize_http_exception, inherited, #initialize, list_commands, load_commands, load_config, load_deps, #maybe_setup_fips, #merge_configs, msg, #noauth_rest, #parse_options, reset_config_loader!, reset_subcommands!, #rest, run, #run_with_pretty_exceptions, #server_url, #show_usage, snake_case_name, subcommand_category, subcommand_class_from, subcommand_files, subcommand_loader, subcommands, subcommands_by_category, #test_mandatory_field, ui, unnamed?, use_separate_defaults?, #username

Methods included from Mixin::ConvertToClassName

#constantize, #convert_to_class_name, #convert_to_snake_case, #filename_to_qualified_string, #normalize_snake_case_name, #snake_case_basename

Methods included from Mixin::PathSanity

#enforce_path_sanity, #sanitized_path

Constructor Details

This class inherits a constructor from Chef::Knife

Instance Method Details

#ctl_command(api_info) ⇒ Object

Given an API info hash (see +#parse_api_info(response)+), return the name of the "server-ctl" command for the kind of server we're interacting with (based on the +flavor+ field)


119
120
121
122
123
124
125
126
127
128
129
# File 'lib/chef/knife/index_rebuild.rb', line 119

def ctl_command(api_info)
  case api_info["flavor"]
  when "osc"
    "chef-server-ctl"
  when "opc"
    "private-chef-ctl"
  else
    # Generic fallback
    "chef-server-ctl"
  end
end

#deprecated_server_messageObject


70
71
72
# File 'lib/chef/knife/index_rebuild.rb', line 70

def deprecated_server_message
  ui.warn("'knife index rebuild' has been removed for Chef 11+ servers. It will continue to work for prior versions, however.")
end

#grab_api_infoObject


45
46
47
48
49
50
51
52
53
54
55
56
# File 'lib/chef/knife/index_rebuild.rb', line 45

def grab_api_info
  # Since we don't yet have any endpoints that implement an
  # OPTIONS handler, we need to get our version header
  # information in a more roundabout way.  We'll try to query
  # for a node we know won't exist; the 404 response that comes
  # back will give us what we want
  dummy_node = "knife_index_rebuild_test_#{rand(1000000)}"
  rest.get("/nodes/#{dummy_node}")
rescue Net::HTTPServerException => exception
  r = exception.response
  parse_api_info(r)
end

#nagObject


74
75
76
77
# File 'lib/chef/knife/index_rebuild.rb', line 74

def nag
  ui.info("This operation is destructive. Rebuilding the index may take some time.")
  ui.confirm("Continue")
end

#parse_api_info(response) ⇒ Object

Chef 11 (and above) servers return various pieces of information about the server in an +x-ops-api-info+ header. This is a +;+ delimited string of key / value pairs, separated by +=+.

Given a Net::HTTPResponse object, this method extracts this information (if present), and returns it as a hash. If no such header is found, an empty hash is returned.


87
88
89
90
91
92
93
94
95
96
97
98
99
# File 'lib/chef/knife/index_rebuild.rb', line 87

def parse_api_info(response)
  value = response["x-ops-api-info"]
  if value
    kv = value.split(";")
    kv.inject({}) do |acc, pair|
      k, v = pair.split("=")
      acc[k] = v
      acc
    end
  else
    {}
  end
end

#runObject


32
33
34
35
36
37
38
39
40
41
42
43
# File 'lib/chef/knife/index_rebuild.rb', line 32

def run
  api_info = grab_api_info

  if unsupported_version?(api_info)
    unsupported_server_message(api_info)
    exit 1
  else
    deprecated_server_message
    nag
    output rest.post("/search/reindex", {})
  end
end

#server_type(api_info) ⇒ Object

Given an API info hash (see +#parse_api_info(response)+), return a string describing the kind of server we're interacting with (based on the +flavor+ field)


104
105
106
107
108
109
110
111
112
113
114
# File 'lib/chef/knife/index_rebuild.rb', line 104

def server_type(api_info)
  case api_info["flavor"]
  when "osc"
    "Open Source Chef Server"
  when "opc"
    "Private Chef Server"
  else
    # Generic fallback
    "Chef Server"
  end
end

#unsupported_server_message(api_info) ⇒ Object


65
66
67
68
# File 'lib/chef/knife/index_rebuild.rb', line 65

def unsupported_server_message(api_info)
  ui.error("Rebuilding the index is not available via knife for #{server_type(api_info)}s version 11.0.0 and above.")
  ui.info("Instead, run the '#{ctl_command(api_info)} reindex' command on the server itself.")
end

#unsupported_version?(api_info) ⇒ Boolean

Only Chef 11+ servers will have version information in their headers, and only those servers will lack an API endpoint for index rebuilding.


61
62
63
# File 'lib/chef/knife/index_rebuild.rb', line 61

def unsupported_version?(api_info)
  !!api_info["version"]
end