Created
August 31, 2011 23:16
-
-
Save adamhjk/1185022 to your computer and use it in GitHub Desktop.
Stick this in a library file...
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
file "/tmp/foo" do | |
content "/tmp/bar" | |
provider Chef::Provider::File::Copy | |
end |
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?
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
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
@@ -12,7 +12,7 @@
def set_content
unless compare_content
backup @new_resource.path if ::File.exists?(@new_resource.path)