From 4827c9d31e68eafdff8d865d40479a18e6382793 Mon Sep 17 00:00:00 2001 From: danielsdeleo Date: Fri, 27 Mar 2015 13:12:28 -0700 Subject: Add minimal ohai mode option flag --- lib/chef/application/apply.rb | 5 +++++ lib/chef/application/client.rb | 5 +++++ lib/chef/application/solo.rb | 5 +++++ lib/chef/client.rb | 3 ++- lib/chef/config.rb | 5 +++++ spec/integration/client/client_spec.rb | 2 +- spec/integration/client/ipv6_spec.rb | 2 +- spec/integration/recipes/lwrp_inline_resources_spec.rb | 2 +- spec/integration/solo/solo_spec.rb | 12 +++++++----- spec/unit/client_spec.rb | 13 +++++++++++++ 10 files changed, 45 insertions(+), 9 deletions(-) diff --git a/lib/chef/application/apply.rb b/lib/chef/application/apply.rb index 42805001d8..e9768b218c 100644 --- a/lib/chef/application/apply.rb +++ b/lib/chef/application/apply.rb @@ -85,6 +85,11 @@ class Chef::Application::Apply < Chef::Application :default => !Chef::Platform.windows?, :description => "Use colored output, defaults to enabled" + option :minimal_ohai, + :long => "--minimal-ohai", + :description => "Only run the bare minimum ohai plugins chef needs to function", + :boolean => true + attr_reader :json_attribs def initialize diff --git a/lib/chef/application/client.rb b/lib/chef/application/client.rb index 03fd07e9f0..9984ad5b9d 100644 --- a/lib/chef/application/client.rb +++ b/lib/chef/application/client.rb @@ -253,6 +253,11 @@ class Chef::Application::Client < Chef::Application :description => "Enable audit-mode with `enabled`. Disable audit-mode with `disabled`. Skip converge and only perform audits with `audit-only`", :proc => lambda { |mo| mo.gsub("-", "_").to_sym } + option :minimal_ohai, + :long => "--minimal-ohai", + :description => "Only run the bare minimum ohai plugins chef needs to function", + :boolean => true + IMMEDIATE_RUN_SIGNAL = "1".freeze attr_reader :chef_client_json diff --git a/lib/chef/application/solo.rb b/lib/chef/application/solo.rb index 97a1952d0f..dd09d65b42 100644 --- a/lib/chef/application/solo.rb +++ b/lib/chef/application/solo.rb @@ -180,6 +180,11 @@ class Chef::Application::Solo < Chef::Application :description => "Set maximum duration to wait for another client run to finish, default is indefinitely.", :proc => lambda { |s| s.to_i } + option :minimal_ohai, + :long => "--minimal-ohai", + :description => "Only run the bare minimum ohai plugins chef needs to function", + :boolean => true + attr_reader :chef_client_json def initialize diff --git a/lib/chef/client.rb b/lib/chef/client.rb index f5cde4bfb3..a4f15c271f 100644 --- a/lib/chef/client.rb +++ b/lib/chef/client.rb @@ -272,7 +272,8 @@ class Chef end def run_ohai - ohai.all_plugins + filter = Chef::Config[:minimal_ohai] ? %w[fqdn machinename hostname platform platform_version os os_version] : nil + ohai.all_plugins(filter) @events.ohai_completed(node) end diff --git a/lib/chef/config.rb b/lib/chef/config.rb index a9fa9f1552..b897f9fdbd 100644 --- a/lib/chef/config.rb +++ b/lib/chef/config.rb @@ -333,6 +333,11 @@ class Chef # This can be removed when audit-mode is enabled by default. default :audit_mode, :disabled + # Chef only needs ohai to run the hostname plugin for the most basic + # functionality. If the rest of the ohai plugins are not needed (like in + # most of our testing scenarios) + default :minimal_ohai, false + # Policyfile is an experimental feature where a node gets its run list and # cookbook version set from a single document on the server instead of # expanding the run list and having the server compute the cookbook version diff --git a/spec/integration/client/client_spec.rb b/spec/integration/client/client_spec.rb index 8afb52e29a..aa9aefe1a7 100644 --- a/spec/integration/client/client_spec.rb +++ b/spec/integration/client/client_spec.rb @@ -45,7 +45,7 @@ describe "chef-client" do # machine that has omnibus chef installed. In that case we need to ensure # we're running `chef-client` from the source tree and not the external one. # cf. CHEF-4914 - let(:chef_client) { "ruby '#{chef_dir}/chef-client'" } + let(:chef_client) { "ruby '#{chef_dir}/chef-client' --minimal-ohai" } when_the_repository "has a cookbook with a no-op recipe" do before { file 'cookbooks/x/recipes/default.rb', '' } diff --git a/spec/integration/client/ipv6_spec.rb b/spec/integration/client/ipv6_spec.rb index 76dd1938f7..8be873edf4 100644 --- a/spec/integration/client/ipv6_spec.rb +++ b/spec/integration/client/ipv6_spec.rb @@ -76,7 +76,7 @@ END_CLIENT_RB let(:chef_dir) { File.join(File.dirname(__FILE__), "..", "..", "..", "bin") } - let(:chef_client_cmd) { %Q[ruby '#{chef_dir}/chef-client' -c "#{path_to('config/client.rb')}" -lwarn] } + let(:chef_client_cmd) { %Q[ruby '#{chef_dir}/chef-client' --minimal-ohai -c "#{path_to('config/client.rb')}" -lwarn] } after do FileUtils.rm_rf(cache_path) diff --git a/spec/integration/recipes/lwrp_inline_resources_spec.rb b/spec/integration/recipes/lwrp_inline_resources_spec.rb index a3baba8b0f..b4c4e6ca11 100644 --- a/spec/integration/recipes/lwrp_inline_resources_spec.rb +++ b/spec/integration/recipes/lwrp_inline_resources_spec.rb @@ -16,7 +16,7 @@ describe "LWRPs with inline resources" do # machine that has omnibus chef installed. In that case we need to ensure # we're running `chef-client` from the source tree and not the external one. # cf. CHEF-4914 - let(:chef_client) { "ruby '#{chef_dir}/chef-client'" } + let(:chef_client) { "ruby '#{chef_dir}/chef-client' --minimal-ohai" } when_the_repository "has a cookbook with a nested LWRP" do before do diff --git a/spec/integration/solo/solo_spec.rb b/spec/integration/solo/solo_spec.rb index 41f5f5506f..f45933c799 100644 --- a/spec/integration/solo/solo_spec.rb +++ b/spec/integration/solo/solo_spec.rb @@ -15,6 +15,8 @@ describe "chef-solo" do let(:cookbook_ancient_100_metadata_rb) { cb_metadata("ancient", "1.0.0") } + let(:chef_solo) { "ruby bin/chef-solo --minimal-ohai" } + when_the_repository "has a cookbook with a basic recipe" do before do file 'cookbooks/x/metadata.rb', cookbook_x_100_metadata_rb @@ -26,7 +28,7 @@ describe "chef-solo" do cookbook_path "#{path_to('cookbooks')}" file_cache_path "#{path_to('config/cache')}" EOM - result = shell_out("ruby bin/chef-solo -c \"#{path_to('config/solo.rb')}\" -o 'x::default' -l debug", :cwd => chef_dir) + result = shell_out("#{chef_solo} -c \"#{path_to('config/solo.rb')}\" -o 'x::default' -l debug", :cwd => chef_dir) result.error! expect(result.stdout).to include("ITWORKS") end @@ -41,7 +43,7 @@ EOM {"run_list":["x::default"]} E - result = shell_out("ruby bin/chef-solo -c \"#{path_to('config/solo.rb')}\" -j '#{path_to('config/node.json')}' -l debug", :cwd => chef_dir) + result = shell_out("#{chef_solo} -c \"#{path_to('config/solo.rb')}\" -j '#{path_to('config/node.json')}' -l debug", :cwd => chef_dir) result.error! expect(result.stdout).to include("ITWORKS") end @@ -62,7 +64,7 @@ E cookbook_path "#{path_to('cookbooks')}" file_cache_path "#{path_to('config/cache')}" EOM - result = shell_out("ruby bin/chef-solo -c \"#{path_to('config/solo.rb')}\" -o 'x::default' -l debug", :cwd => chef_dir) + result = shell_out("#{chef_solo} -c \"#{path_to('config/solo.rb')}\" -o 'x::default' -l debug", :cwd => chef_dir) expect(result.exitstatus).to eq(0) # For CHEF-5120 this becomes 1 expect(result.stdout).to include("WARN: MissingCookbookDependency") end @@ -95,14 +97,14 @@ EOM chef_dir = File.join(File.dirname(__FILE__), "..", "..", "..") # Instantiate the first chef-solo run - s1 = Process.spawn("ruby bin/chef-solo -c \"#{path_to('config/solo.rb')}\" -o 'x::default' \ + s1 = Process.spawn("#{chef_solo} -c \"#{path_to('config/solo.rb')}\" -o 'x::default' \ -l debug -L #{path_to('logs/runs.log')}", :chdir => chef_dir) # Give it some time to progress sleep 1 # Instantiate the second chef-solo run - s2 = Process.spawn("ruby bin/chef-solo -c \"#{path_to('config/solo.rb')}\" -o 'x::default' \ + s2 = Process.spawn("#{chef_solo} -c \"#{path_to('config/solo.rb')}\" -o 'x::default' \ -l debug -L #{path_to('logs/runs.log')}", :chdir => chef_dir) Process.waitpid(s1) diff --git a/spec/unit/client_spec.rb b/spec/unit/client_spec.rb index cf995838fa..fa8317744c 100644 --- a/spec/unit/client_spec.rb +++ b/spec/unit/client_spec.rb @@ -78,6 +78,19 @@ describe Chef::Client do allow(Ohai::System).to receive(:new).and_return(ohai_system) end + context "when minimal ohai is configured" do + before do + Chef::Config[:minimal_ohai] = true + end + + it "runs ohai with only the minimum required plugins" do + expected_filter = %w[fqdn machinename hostname platform platform_version os os_version] + expect(ohai_system).to receive(:all_plugins).with(expected_filter) + client.run_ohai + end + + end + describe "authentication protocol selection" do after do Chef::Config[:authentication_protocol_version] = "1.0" -- cgit v1.2.1