summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorCaleb Tennis <caleb.tennis@gmail.com>2009-04-17 23:13:39 +0000
committerCaleb Tennis <caleb.tennis@gmail.com>2009-04-17 23:13:48 +0000
commit549ae9da829fe8645b896b28eb8d3564cfc60880 (patch)
tree14f0c5ca55391e78ec479c9580bd1bb7f2080397
parent5081a154d96647e4513ac23a84ca45de699a2ccd (diff)
downloadchef-549ae9da829fe8645b896b28eb8d3564cfc60880.tar.gz
Don't download a file if a checksum is specified and we match
-rw-r--r--chef/lib/chef/provider/remote_file.rb65
-rw-r--r--chef/lib/chef/resource/remote_file.rb9
-rw-r--r--chef/spec/unit/provider/remote_file_spec.rb27
-rw-r--r--chef/spec/unit/resource/remote_file_spec.rb13
4 files changed, 78 insertions, 36 deletions
diff --git a/chef/lib/chef/provider/remote_file.rb b/chef/lib/chef/provider/remote_file.rb
index df2afd4976..57dad2f058 100644
--- a/chef/lib/chef/provider/remote_file.rb
+++ b/chef/lib/chef/provider/remote_file.rb
@@ -46,45 +46,48 @@ class Chef
# The current files checksum
current_checksum = self.checksum(path) if ::File.exists?(path)
- begin
- # The remote filehandle
- raw_file = get_from_uri(source) ||
- get_from_server(source, current_checksum) ||
- get_from_local_cookbook(source)
- rescue Net::HTTPRetriableError => e
- if e.response.kind_of?(Net::HTTPNotModified)
- Chef::Log.debug("File #{path} is unchanged")
- return false
- else
- raise e
+ unless (@new_resource.checksum && @new_resource.checksum == current_checksum)
+ begin
+ # The remote filehandle
+ raw_file = get_from_uri(source) ||
+ get_from_server(source, current_checksum) ||
+ get_from_local_cookbook(source)
+ rescue Net::HTTPRetriableError => e
+ if e.response.kind_of?(Net::HTTPNotModified)
+ Chef::Log.debug("File #{path} is unchanged")
+ return false
+ else
+ raise e
+ end
end
- end
- # If the file exists
- if ::File.exists?(@new_resource.path)
- # And it matches the checksum of the raw file
- @new_resource.checksum(self.checksum(raw_file.path))
- if @new_resource.checksum != @current_resource.checksum
- # Updating target file, let's perform a backup!
- Chef::Log.debug("#{@new_resource} changed from #{@current_resource.checksum} to #{@new_resource.checksum}")
- Chef::Log.info("Updating #{@new_resource} at #{@new_resource.path}")
- backup(@new_resource.path)
+ # If the file exists
+ if ::File.exists?(@new_resource.path)
+ # And it matches the checksum of the raw file
+ @new_resource.checksum(self.checksum(raw_file.path))
+ if @new_resource.checksum != @current_resource.checksum
+ # Updating target file, let's perform a backup!
+ Chef::Log.debug("#{@new_resource} changed from #{@current_resource.checksum} to #{@new_resource.checksum}")
+ Chef::Log.info("Updating #{@new_resource} at #{@new_resource.path}")
+ backup(@new_resource.path)
+ end
+ else
+ # We're creating a new file
+ Chef::Log.info("Creating #{@new_resource} at #{@new_resource.path}")
end
- else
- # We're creating a new file
- Chef::Log.info("Creating #{@new_resource} at #{@new_resource.path}")
- end
- FileUtils.cp(raw_file.path, @new_resource.path)
- @new_resource.updated = true
-
+ FileUtils.cp(raw_file.path, @new_resource.path)
+ @new_resource.updated = true
+
+ # We're done with the file, so make sure to close it if it was open.
+ raw_file.close
+ true
+ end
+
set_owner if @new_resource.owner
set_group if @new_resource.group
set_mode if @new_resource.mode
- # We're done with the file, so make sure to close it if it was open.
- raw_file.close
- true
end
def get_from_uri(source)
diff --git a/chef/lib/chef/resource/remote_file.rb b/chef/lib/chef/resource/remote_file.rb
index a1f276aff3..136a704c63 100644
--- a/chef/lib/chef/resource/remote_file.rb
+++ b/chef/lib/chef/resource/remote_file.rb
@@ -46,6 +46,15 @@ class Chef
)
end
+ def checksum(args=nil)
+ set_or_return(
+ :checksum,
+ args,
+ :kind_of => String
+ )
+ end
+
+
end
end
end
diff --git a/chef/spec/unit/provider/remote_file_spec.rb b/chef/spec/unit/provider/remote_file_spec.rb
index 430a2e5916..b05b6a989d 100644
--- a/chef/spec/unit/provider/remote_file_spec.rb
+++ b/chef/spec/unit/provider/remote_file_spec.rb
@@ -65,10 +65,29 @@ describe Chef::Provider::RemoteFile, "do_remote_file" do
end
describe "when given a URI source" do
- it "should download the file from the remote URL" do
- @resource.source("http://opscode.com/seattle.txt")
- @rest.should_receive(:get_rest).with("http://opscode.com/seattle.txt", true).and_return(@tempfile)
- do_remote_file
+ describe "and given a checksum" do
+ it "should not download the file if the checksum matches" do
+ @resource.checksum("0fd012fdc96e96f8f7cf2046522a54aed0ce470224513e45da6bc1a17a4924aa")
+ @resource.source("http://opscode.com/seattle.txt")
+ @rest.should_not_receive(:get_rest).with("http://opscode.com/seattle.txt", true).and_return(@tempfile)
+ do_remote_file
+ end
+
+ it "should download the file if the checksum matches" do
+ @resource.checksum("this hash doesn't match")
+ @resource.source("http://opscode.com/seattle.txt")
+ @rest.should_receive(:get_rest).with("http://opscode.com/seattle.txt", true).and_return(@tempfile)
+ do_remote_file
+ end
+ end
+
+ describe "and not given a checksum" do
+ it "should download the file from the remote URL" do
+ @resource.checksum(nil)
+ @resource.source("http://opscode.com/seattle.txt")
+ @rest.should_receive(:get_rest).with("http://opscode.com/seattle.txt", true).and_return(@tempfile)
+ do_remote_file
+ end
end
end
diff --git a/chef/spec/unit/resource/remote_file_spec.rb b/chef/spec/unit/resource/remote_file_spec.rb
index 24ed30f4e0..cc31b543ef 100644
--- a/chef/spec/unit/resource/remote_file_spec.rb
+++ b/chef/spec/unit/resource/remote_file_spec.rb
@@ -49,5 +49,16 @@ describe Chef::Resource::RemoteFile do
@resource.cookbook.should == nil
end
end
+
+ describe "checksum" do
+ it "should accept a string for the checksum object" do
+ @resource.checksum "asdf"
+ @resource.checksum.should eql("asdf")
+ end
+
+ it "should default to nil" do
+ @resource.checksum.should == nil
+ end
+ end
-end \ No newline at end of file
+end