diff options
Diffstat (limited to 'lib')
-rw-r--r-- | lib/chef/application.rb | 15 | ||||
-rw-r--r-- | lib/chef/application/client.rb | 12 | ||||
-rw-r--r-- | lib/chef/application/solo.rb | 12 | ||||
-rw-r--r-- | lib/chef/application/windows_service.rb | 11 | ||||
-rw-r--r-- | lib/chef/config.rb | 3 | ||||
-rw-r--r-- | lib/chef/knife/cookbook_site_install.rb | 11 | ||||
-rw-r--r-- | lib/chef/provider/cookbook_file.rb | 3 | ||||
-rw-r--r-- | lib/chef/provider/file.rb | 25 | ||||
-rw-r--r-- | lib/chef/provider/remote_directory.rb | 39 | ||||
-rw-r--r-- | lib/chef/resource_reporter.rb | 39 |
10 files changed, 102 insertions, 68 deletions
diff --git a/lib/chef/application.rb b/lib/chef/application.rb index 328a81a2eb..f864996ea5 100644 --- a/lib/chef/application.rb +++ b/lib/chef/application.rb @@ -34,6 +34,8 @@ class Chef::Application def initialize super + @chef_client = nil + @chef_client_json = nil trap("TERM") do Chef::Application.fatal!("SIGTERM received, stopping", 1) end @@ -127,6 +129,18 @@ class Chef::Application raise Chef::Exceptions::Application, "#{self.to_s}: you must override run_application" end + # Initializes Chef::Client instance and runs it + def run_chef_client + @chef_client = Chef::Client.new( + @chef_client_json, + :override_runlist => config[:override_runlist] + ) + @chef_client_json = nil + + @chef_client.run + @chef_client = nil + end + private def apply_config(config_file_path) @@ -134,7 +148,6 @@ class Chef::Application Chef::Config.merge!(config) end - class << self def debug_stacktrace(e) message = "#{e.class}: #{e}\n#{e.backtrace.join("\n")}" diff --git a/lib/chef/application/client.rb b/lib/chef/application/client.rb index 3a68a41c14..ef40a7a358 100644 --- a/lib/chef/application/client.rb +++ b/lib/chef/application/client.rb @@ -186,9 +186,6 @@ class Chef::Application::Client < Chef::Application def initialize super - - @chef_client = nil - @chef_client_json = nil end # Reconfigure the chef client @@ -274,14 +271,7 @@ class Chef::Application::Client < Chef::Application Chef::Log.debug("Splay sleep #{splay} seconds") sleep splay end - @chef_client = Chef::Client.new( - @chef_client_json, - :override_runlist => config[:override_runlist] - ) - @chef_client_json = nil - - @chef_client.run - @chef_client = nil + run_chef_client if Chef::Config[:interval] Chef::Log.debug("Sleeping for #{Chef::Config[:interval]} seconds") unless SELF_PIPE.empty? diff --git a/lib/chef/application/solo.rb b/lib/chef/application/solo.rb index 7ec6c36e76..4aca7238f6 100644 --- a/lib/chef/application/solo.rb +++ b/lib/chef/application/solo.rb @@ -149,8 +149,6 @@ class Chef::Application::Solo < Chef::Application def initialize super - @chef_solo = nil - @chef_solo_json = nil end def reconfigure @@ -182,7 +180,7 @@ class Chef::Application::Solo < Chef::Application end begin - @chef_solo_json = Chef::JSONCompat.from_json(json_io.read) + @chef_client_json = Chef::JSONCompat.from_json(json_io.read) json_io.close unless json_io.closed? rescue JSON::ParserError => error Chef::Application.fatal!("Could not parse the provided JSON file (#{Chef::Config[:json_attribs]})!: " + error.message, 2) @@ -223,12 +221,7 @@ class Chef::Application::Solo < Chef::Application sleep splay end - @chef_solo = Chef::Client.new( - @chef_solo_json, - :override_runlist => config[:override_runlist] - ) - @chef_solo.run - @chef_solo = nil + run_chef_client if Chef::Config[:interval] Chef::Log.debug("Sleeping for #{Chef::Config[:interval]} seconds") sleep Chef::Config[:interval] @@ -251,4 +244,5 @@ class Chef::Application::Solo < Chef::Application end end end + end diff --git a/lib/chef/application/windows_service.rb b/lib/chef/application/windows_service.rb index 961ec8f92f..0d4a022fdf 100644 --- a/lib/chef/application/windows_service.rb +++ b/lib/chef/application/windows_service.rb @@ -88,14 +88,7 @@ class Chef # If we've stopped, then bail out now, instead of going on to run Chef next if state != RUNNING - @chef_client = Chef::Client.new( - @chef_client_json, - :override_runlist => config[:override_runlist] - ) - @chef_client_json = nil - - @chef_client.run - @chef_client = nil + run_chef_client Chef::Log.debug("Sleeping for #{Chef::Config[:interval]} seconds") client_sleep Chef::Config[:interval] @@ -225,7 +218,7 @@ class Chef sleep chunk_length end end - + end end end diff --git a/lib/chef/config.rb b/lib/chef/config.rb index 61c8806a66..dceac4fa34 100644 --- a/lib/chef/config.rb +++ b/lib/chef/config.rb @@ -287,6 +287,9 @@ class Chef cache_type "BasicFile" cache_options({ :path => platform_specific_path("/var/chef/cache/checksums"), :skip_expires => true }) + # Set to false to silence Chef 11 deprecation warnings: + chef11_deprecation_warnings true + # Arbitrary knife configuration data knife Hash.new diff --git a/lib/chef/knife/cookbook_site_install.rb b/lib/chef/knife/cookbook_site_install.rb index b2e0d84751..584735d8ff 100644 --- a/lib/chef/knife/cookbook_site_install.rb +++ b/lib/chef/knife/cookbook_site_install.rb @@ -107,6 +107,7 @@ class Chef end end + unless config[:no_deps] md = Chef::Cookbook::Metadata.new md.from_file(File.join(@install_path, @cookbook_name, "metadata.rb")) @@ -143,13 +144,21 @@ class Chef def extract_cookbook(upstream_file, version) ui.info("Uncompressing #{@cookbook_name} version #{version}.") - shell_out!("tar zxvf #{Shellwords.escape upstream_file}", :cwd => @install_path) + shell_out!("tar zxvf #{convert_path upstream_file}", :cwd => @install_path) end def clear_existing_files(cookbook_path) ui.info("Removing pre-existing version.") FileUtils.rmtree(cookbook_path) if File.directory?(cookbook_path) end + + def convert_path(upstream_file) + if ENV['MSYSTEM'] == 'MINGW32' + return upstream_file.sub(/^([[:alpha:]]):/, '/\1') + else + return Shellwords.escape upstream_file + end + end end end end diff --git a/lib/chef/provider/cookbook_file.rb b/lib/chef/provider/cookbook_file.rb index 431f3f2367..144afbddeb 100644 --- a/lib/chef/provider/cookbook_file.rb +++ b/lib/chef/provider/cookbook_file.rb @@ -44,9 +44,6 @@ class Chef Chef::Log.debug("#{@new_resource} staging #{file_cache_location} to #{tempfile.path}") tempfile.close FileUtils.cp(file_cache_location, tempfile.path) - # Since the @new_resource.path file will not be updated - # at the time of converge, we must use the tempfile - update_new_file_state(tempfile.path) end Chef::Log.info("#{@new_resource} created file #{@new_resource.path}") end diff --git a/lib/chef/provider/file.rb b/lib/chef/provider/file.rb index 659afc6517..77f5217027 100644 --- a/lib/chef/provider/file.rb +++ b/lib/chef/provider/file.rb @@ -132,7 +132,9 @@ class Chef @current_resource.path(@new_resource.path) if !::File.directory?(@new_resource.path) if ::File.exist?(@new_resource.path) - @current_resource.checksum(checksum(@new_resource.path)) + if @action != :create_if_missing + @current_resource.checksum(checksum(@new_resource.path)) + end end end load_current_resource_attrs @@ -142,6 +144,13 @@ class Chef end def load_current_resource_attrs + if Chef::Platform.windows? + # TODO: To work around CHEF-3554, add support for Windows + # equivalent, or implicit resource reporting won't work for + # Windows. + return + end + if ::File.exist?(@new_resource.path) stat = ::File.stat(@new_resource.path) @current_resource.owner(stat.uid) @@ -215,13 +224,21 @@ class Chef # override the default with the tempfile, since the # file at @new_resource.path will not be updated on converge def update_new_file_state(path=@new_resource.path) + if !::File.directory?(path) + @new_resource.checksum(checksum(path)) + end + + if Chef::Platform.windows? + # TODO: To work around CHEF-3554, add support for Windows + # equivalent, or implicit resource reporting won't work for + # Windows. + return + end + stat = ::File.stat(path) @new_resource.owner(stat.uid) @new_resource.mode(stat.mode & 07777) @new_resource.group(stat.gid) - if !::File.directory?(path) - @new_resource.checksum(checksum(path)) - end end def action_create diff --git a/lib/chef/provider/remote_directory.rb b/lib/chef/provider/remote_directory.rb index 9ccd7ea056..dee383f763 100644 --- a/lib/chef/provider/remote_directory.rb +++ b/lib/chef/provider/remote_directory.rb @@ -34,23 +34,27 @@ class Chef def action_create super - - files_to_purge = Set.new( - Dir.glob(::File.join(@new_resource.path, '**', '*'), ::File::FNM_DOTMATCH).select do |name| - name !~ /(?:^|#{Regexp.escape(::File::SEPARATOR)})\.\.?$/ - end - ) - files_to_transfer.each do |cookbook_file_relative_path| - create_cookbook_file(cookbook_file_relative_path) - # the file is removed from the purge list - files_to_purge.delete(::File.join(@new_resource.path, cookbook_file_relative_path)) - # parent directories are also removed from the purge list - directories=::File.dirname(::File.join(@new_resource.path, cookbook_file_relative_path)).split(::File::SEPARATOR) - for i in 0..directories.length-1 - files_to_purge.delete(::File.join(directories[0..i])) + files_to_purge = Set.new(Dir.glob(::File.join(@new_resource.path, '**', '*'), + ::File::FNM_DOTMATCH).select do |name| + name !~ /(?:^|#{Regexp.escape(::File::SEPARATOR)})\.\.?$/ + end) + + converge_by("Create managed files in directory") do + files_to_transfer.each do |cookbook_file_relative_path| + create_cookbook_file(cookbook_file_relative_path) + # the file is removed from the purge list + files_to_purge.delete(::File.join(@new_resource.path, cookbook_file_relative_path)) + # parent directories are also removed from the purge list + directories=::File.dirname(::File.join(@new_resource.path, cookbook_file_relative_path)).split(::File::SEPARATOR) + for i in 0..directories.length-1 + files_to_purge.delete(::File.join(directories[0..i])) + end end end - purge_unmanaged_files(files_to_purge) + + converge_by("Purge unmanaged files from directory") do + purge_unmanaged_files(files_to_purge) + end end def action_create_if_missing @@ -170,5 +174,10 @@ class Chef end end + + def whyrun_supported? + true + end + end end diff --git a/lib/chef/resource_reporter.rb b/lib/chef/resource_reporter.rb index 3d10c1e961..f9ad5e1cb9 100644 --- a/lib/chef/resource_reporter.rb +++ b/lib/chef/resource_reporter.rb @@ -116,15 +116,15 @@ class Chef Chef::Log.info("Chef server generated run history id: #{@run_id}") @summary_only = server_response["summary_only"] rescue Timeout::Error, Errno::EINVAL, Errno::ECONNRESET, EOFError, Net::HTTPBadResponse, Net::HTTPHeaderSyntaxError, Net::ProtocolError => e - if !e.response || e.response.code.to_s != 404 + if !e.response || e.response.code.to_s != "404" if Chef::Config[:enable_reporting_url_fatals] - Chef::Log.error("Received exception attempting to generate run history id (URL Path: #{resource_history_url}), and enable_reporting_url_fatals is set, aborting run.") + Chef::Log.error("Received exception #{"(" + e.response.code + ") " if e.response.code}attempting to generate run history id (URL Path: #{resource_history_url}), and enable_reporting_url_fatals is set, aborting run.") raise else - Chef::Log.info("Received exception attempting to generate run history id (URL Path: #{resource_history_url}), disabling reporting for this run.") + Chef::Log.info("Received exception #{"(" + e.response.code + ") " if e.response.code}attempting to generate run history id (URL Path: #{resource_history_url}), disabling reporting for this run.") end else - Chef::Log.debug("Received 404 attempting to generate run history id (URL Path: #{resource_history_url}), assuming feature is not supported.") + Chef::Log.debug("Received 404 attempting to generate run history id (URL Path: #{resource_history_url}), assuming feature is not supported on server.") end @reporting_enabled = false end @@ -187,17 +187,26 @@ class Chef Chef::Log.info("Sending resource update report (run-id: #{@run_id})") Chef::Log.debug run_data.inspect compressed_data = encode_gzip(run_data.to_json) - #if summary only is enabled send the uncompressed run_data excluding the run_data["resources"] and some additional metrics. - if @summary_only - run_data = report_summary(run_data, compressed_data) - Chef::Log.info("run_data_summary: #{run_data}") - @rest_client.post_rest(resource_history_url, run_data) - else - Chef::Log.debug("Sending Compressed Run Data...") - # Since we're posting compressed data we can not directly call - # post_rest which expects JSON - reporting_url = @rest_client.create_url(resource_history_url) - @rest_client.raw_http_request(:POST, reporting_url, {'Content-Encoding' => 'gzip'}, compressed_data) + begin + #if summary only is enabled send the uncompressed run_data excluding the run_data["resources"] and some additional metrics. + if @summary_only + run_data = report_summary(run_data, compressed_data) + Chef::Log.info("run_data_summary: #{run_data}") + @rest_client.post_rest(resource_history_url, run_data) + else + Chef::Log.debug("Sending compressed run data...") + # Since we're posting compressed data we can not directly call + # post_rest which expects JSON + reporting_url = @rest_client.create_url(resource_history_url) + @rest_client.raw_http_request(:POST, reporting_url, {'Content-Encoding' => 'gzip'}, compressed_data) + end + rescue Net::HTTPServerException => e + if e.response.code.to_s == "400" + Chef::FileCache.store("failed-reporting-data.json", Chef::JSONCompat.to_json_pretty(run_data), 0640) + Chef::Log.error("Failed to post reporting data to server (HTTP 400), saving to #{Chef::FileCache.load("failed-reporting-data.json", false)}") + else + Chef::Log.error("Failed to post reporting data to server (HTTP #{e.response.code.to_s})") + end end else Chef::Log.debug("Server doesn't support resource history, skipping resource report.") |