Skip to content

Instantly share code, notes, and snippets.

@adamhjk
Created August 31, 2011 23:16
Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 4 You must be signed in to fork a gist
  • Save adamhjk/1185022 to your computer and use it in GitHub Desktop.
Save adamhjk/1185022 to your computer and use it in GitHub Desktop.
Stick this in a library file...
class Chef
class Provider
class File
class Copy << Chef::Provider::File
def compare_content
checksum(@current_resource.path) == checksum(@new_resource.content)
end
def set_content
unless compare_content
backup @new_resource.path if ::File.exists?(@new_resource.path)
::File.cp_r(@new_resource.content, @new_resource.path)
Chef::Log.info("#{@new_resource.content} copied to #{@new_resource.path}")
@new_resource.updated_by_last_action(true)
end
end
end
end
end
end
file "/tmp/foo" do
content "/tmp/bar"
provider Chef::Provider::File::Copy
end
@akiernan
Copy link

akiernan commented Sep 9, 2011

Couple of typos:

--- file_provider_copy.rb.orig 2011-09-09 10:50:50.000000000 +0100
+++ file_provider_copy.rb 2011-09-09 10:34:03.000000000 +0100
@@ -3,7 +3,7 @@
class Chef
class Provider
class File

  •  class Copy << Chef::Provider::File
    
  •  class Copy < Chef::Provider::File
    
     def compare_content
       checksum(@current_resource.path) == checksum(@new_resource.content)
    

    @@ -12,7 +12,7 @@
    def set_content
    unless compare_content
    backup @new_resource.path if ::File.exists?(@new_resource.path)

  •        ::File.cp_r(@new_resource.content, @new_resource.path)
    
  •        ::FileUtils.cp_r(@new_resource.content, @new_resource.path)
         Chef::Log.info("#{@new_resource.content} copied to #{@new_resource.path}")
         @new_resource.updated_by_last_action(true)
       end
    

@amilu
Copy link

amilu commented Apr 25, 2013

This works only if the file exists already, otherwise the chef File provider uses the content directly ..
What is the best fix for this ?

Snipet from the original code :

unless ::File.exists?(@new_resource.path)
      Chef::Log.info("Creating #{@new_resource} at #{@new_resource.path}")
        ::File.open(@new_resource.path, "w+") {|f| f.write @new_resource.content }
        @new_resource.updated_by_last_action(true)
      else
        set_content unless @new_resource.content.nil?

@mkantor
Copy link

mkantor commented Nov 14, 2013

A safer solution would be to add a new attribute instead of re-using content for this.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment