diff options
author | Thom May <thom@may.lt> | 2018-01-22 10:32:34 +0000 |
---|---|---|
committer | GitHub <noreply@github.com> | 2018-01-22 10:32:34 +0000 |
commit | b21adc71a35c48988f427d404d33ae812866d415 (patch) | |
tree | 12a6824fe2c36fa38093d74b72c45f8ab9103108 | |
parent | f4a7600dfb9020ab818b609cdd4b278802bd7009 (diff) | |
parent | 9690f25ee2458b904d3606a98feaf6629ada69a3 (diff) | |
download | chef-b21adc71a35c48988f427d404d33ae812866d415.tar.gz |
Merge pull request #6728 from chef/reindex
Remove knife index rebuild command that requires Chef < 11
-rw-r--r-- | lib/chef/knife/index_rebuild.rb | 133 | ||||
-rw-r--r-- | spec/unit/knife/index_rebuild_spec.rb | 125 |
2 files changed, 0 insertions, 258 deletions
diff --git a/lib/chef/knife/index_rebuild.rb b/lib/chef/knife/index_rebuild.rb deleted file mode 100644 index 206b7b0fbf..0000000000 --- a/lib/chef/knife/index_rebuild.rb +++ /dev/null @@ -1,133 +0,0 @@ -# -# Author:: Daniel DeLeo (<dan@kallistec.com>) -# Copyright:: Copyright 2009-2016, Daniel DeLeo -# License:: Apache License, Version 2.0 -# -# Licensed under the Apache License, Version 2.0 (the "License"); -# you may not use this file except in compliance with the License. -# You may obtain a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -# See the License for the specific language governing permissions and -# limitations under the License. -# - -require "chef/knife" - -class Chef - class Knife - class IndexRebuild < Knife - - banner "knife index rebuild (options)" - option :yes, - :short => "-y", - :long => "--yes", - :boolean => true, - :description => "don't bother to ask if I'm sure" - - 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 - - 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 - - # Only Chef 11+ servers will have version information in their - # headers, and only those servers will lack an API endpoint for - # index rebuilding. - def unsupported_version?(api_info) - !!api_info["version"] - end - - 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 - - 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 - - def nag - ui.info("This operation is destructive. Rebuilding the index may take some time.") - ui.confirm("Continue") - end - - # 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. - 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 - - # 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) - 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 - - # 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) - 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 - - end - end -end diff --git a/spec/unit/knife/index_rebuild_spec.rb b/spec/unit/knife/index_rebuild_spec.rb deleted file mode 100644 index 97a3a69155..0000000000 --- a/spec/unit/knife/index_rebuild_spec.rb +++ /dev/null @@ -1,125 +0,0 @@ -# -# Author:: Daniel DeLeo (<dan@kallistec.com>) -# Copyright:: Copyright 2009-2016, Daniel DeLeo -# License:: Apache License, Version 2.0 -# -# Licensed under the Apache License, Version 2.0 (the "License"); -# you may not use this file except in compliance with the License. -# You may obtain a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -# See the License for the specific language governing permissions and -# limitations under the License. -# - -require "spec_helper" - -describe Chef::Knife::IndexRebuild do - - let(:knife) { Chef::Knife::IndexRebuild.new } - let(:rest_client) { double(Chef::ServerAPI) } - - let(:stub_rest!) do - expect(knife).to receive(:rest).and_return(rest_client) - end - - before :each do - # This keeps the test output clean - allow(knife.ui).to receive(:stdout).and_return(StringIO.new) - end - - context "#grab_api_info" do - let(:http_not_found_response) do - e = Net::HTTPNotFound.new("1.1", 404, "blah") - allow(e).to receive(:[]).with("x-ops-api-info").and_return(api_header_value) - e - end - - let(:http_server_exception) do - Net::HTTPServerException.new("404: Not Found", http_not_found_response) - end - - before(:each) do - stub_rest! - allow(rest_client).to receive(:get).and_raise(http_server_exception) - end - - context "against a Chef 11 server" do - let(:api_header_value) { "flavor=osc;version=11.0.0;erchef=1.2.3" } - it "retrieves API information" do - expect(knife.grab_api_info).to eq({ "flavor" => "osc", "version" => "11.0.0", "erchef" => "1.2.3" }) - end - end # Chef 11 - - context "against a Chef 10 server" do - let(:api_header_value) { nil } - it "finds no API information" do - expect(knife.grab_api_info).to eq({}) - end - end # Chef 10 - end # grab_api_info - - context "#unsupported_version?" do - context "with Chef 11 API metadata" do - it "is unsupported" do - expect(knife.unsupported_version?({ "version" => "11.0.0", "flavor" => "osc", "erchef" => "1.2.3" })).to be_truthy - end - - it "only truly relies on the version being non-nil" do - expect(knife.unsupported_version?({ "version" => "1", "flavor" => "osc", "erchef" => "1.2.3" })).to be_truthy - end - end - - context "with Chef 10 API metadata" do - it "is supported" do - # Chef 10 will have no metadata - expect(knife.unsupported_version?({})).to be_falsey - end - end - end # unsupported_version? - - context "Simulating a 'knife index rebuild' run" do - - before :each do - expect(knife).to receive(:grab_api_info).and_return(api_info) - server_specific_stubs! - end - - context "against a Chef 11 server" do - let(:api_info) do - { "flavor" => "osc", - "version" => "11.0.0", - "erchef" => "1.2.3", - } - end - let(:server_specific_stubs!) do - expect(knife).to receive(:unsupported_server_message).with(api_info) - expect(knife).to receive(:exit).with(1) - end - - it "should not be allowed" do - knife.run - end - end - - context "against a Chef 10 server" do - let(:api_info) { {} } - let(:server_specific_stubs!) do - stub_rest! - expect(rest_client).to receive(:post).with("/search/reindex", {}).and_return("representative output") - expect(knife).not_to receive(:unsupported_server_message) - expect(knife).to receive(:deprecated_server_message) - expect(knife).to receive(:nag) - expect(knife).to receive(:output).with("representative output") - end - it "should be allowed" do - knife.run - end - end - end - -end |