From f1400d71b8253f8112e5cf09645f3956c0a5e46b Mon Sep 17 00:00:00 2001 From: Lamont Granquist Date: Wed, 22 Aug 2012 16:44:25 -0700 Subject: fix diff output, add tweakables, refactor diff code Conflicts: chef/lib/chef/resource_reporter.rb --- chef/lib/chef/config.rb | 3 ++ chef/lib/chef/provider/file.rb | 68 +++++++++++++++++++------------------- chef/lib/chef/resource_reporter.rb | 7 ++-- 3 files changed, 39 insertions(+), 39 deletions(-) diff --git a/chef/lib/chef/config.rb b/chef/lib/chef/config.rb index 65e985848b..64eab45082 100644 --- a/chef/lib/chef/config.rb +++ b/chef/lib/chef/config.rb @@ -177,6 +177,9 @@ class Chef verbose_logging true node_name nil node_path "/var/chef/node" + diff_disable false + diff_filesize_threshold 10000000 + diff_output_threshold 1000000 pid_file nil diff --git a/chef/lib/chef/provider/file.rb b/chef/lib/chef/provider/file.rb index a5ca2c0028..689138f8af 100644 --- a/chef/lib/chef/provider/file.rb +++ b/chef/lib/chef/provider/file.rb @@ -58,52 +58,52 @@ class Chef def is_binary?(path) ::File.open(path) do |file| - file.read(10000000) !~ /^[[:print:]]*$/ + file.read(Chef::Config[:diff_filesize_threshold]) !~ /^[[:print:]]*$/ end end def diff_current source_path - begin - # Solaris diff doesn't support -N (treat missing files as empty) - # For compatibility we'll create a temp file if the file does not exist - # and substitute it - unless ::File.exists?(source_path) - altfile = Tempfile.new('chef-tempfile') - source_path = altfile.path - end + return [ "(diff output suppressed by config)" ] if Chef::Config[:diff_disabled] + return [ "(creating a new file, diff output suppressed)" ] unless ::File.exists?(@current_resource.path) + return [ "(no source file, diff output suppressed)" ] unless ::File.exists?(source_path) - if ::File.size(@current_resource.path) > 10000000 || ::File.size(source_path) > 10000000 - return [ "(file sizes exceed 10000000 bytes, diff output suppressed)" ] - end + diff_filesize_threshold = Chef::Config[:diff_filesize_threshold] + diff_output_threshold = Chef::Config[:diff_output_threshold] - # MacOSX diff will *sometimes* happily spit out nasty binary diffs - if is_binary?(@current_resource.path) || is_binary?(source_path) - return [ "(binary files, diff output suppressed)" ] - end + if ::File.size(@current_resource.path) > diff_filesize_threshold || ::File.size(source_path) > diff_filesize_threshold + return [ "(file sizes exceed #{diff_filesize_threshold} bytes, diff output suppressed)" ] + end + # MacOSX(BSD?) diff will *sometimes* happily spit out nasty binary diffs + if is_binary?(@current_resource.path) || is_binary?(source_path) + return [ "(binary files, diff output suppressed)" ] + end + + begin # -u: Unified diff format result = shell_out("diff -u #{@current_resource.path} #{source_path}" ) - # diff will set a non-zero return code even when there's - # valid stdout results, if it encounters something unexpected - # So as long as we have output, we'll show it. - if not result.stdout.empty? - if result.stdout.length > 1000000 - [ "(long diff of over 1000000 characters, diff output suppressed)" ] - else - val = result.stdout.split("\n") - val.delete("\\ No newline at end of file") - @new_resource.diff = val - val - end - elsif not result.stderr.empty? - [ "Could not determine diff. Error: #{result.stderr}" ] - else - [ "(no diff)" ] - end rescue Exception => e # Should *not* receive this, but in some circumstances it seems that # an exception can be thrown even using shell_out instead of shell_out! - [ "Could not determine diff. Error: #{e.message}" ] + return [ "Could not determine diff. Error: #{e.message}" ] + end + + # diff will set a non-zero return code even when there's + # valid stdout results, if it encounters something unexpected + # So as long as we have output, we'll show it. + if not result.stdout.empty? + if result.stdout.length > diff_output_threshold + [ "(long diff of over #{diff_output_threshold} characters, diff output suppressed)" ] + else + val = result.stdout.split("\n") + val.delete("\\ No newline at end of file") + @new_resource.diff = val + val + end + elsif not result.stderr.empty? + [ "Could not determine diff. Error: #{result.stderr}" ] + else + [ "(no diff)" ] end end diff --git a/chef/lib/chef/resource_reporter.rb b/chef/lib/chef/resource_reporter.rb index 7ee2e497cb..6e5f4c2a6f 100644 --- a/chef/lib/chef/resource_reporter.rb +++ b/chef/lib/chef/resource_reporter.rb @@ -56,11 +56,8 @@ class Chef as_hash["after"] = new_resource.state as_hash["before"] = current_resource ? current_resource.state : {} as_hash["duration"] = (elapsed_time * 1000).to_i.to_s - # TODO: include diffs, etc. here: - if new_resource.respond_to?("diff") - as_hash["delta"] = new_resource.diff - else - as_hash["delta"] = nil + as_hash["delta"] = new_resource.diff if new_resource.respond_to?("diff") + as_hash["delta"] = "" if as_hash["delta"].nil? # TODO: rename as "action" as_hash["result"] = action.to_s if success? -- cgit v1.2.1