Skip to content

Commit 549ae9d

Browse files
committed
Don't download a file if a checksum is specified and we match
1 parent 5081a15 commit 549ae9d

4 files changed

Lines changed: 78 additions & 36 deletions

File tree

chef/lib/chef/provider/remote_file.rb

Lines changed: 34 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -46,45 +46,48 @@ def do_remote_file(source, path)
4646
# The current files checksum
4747
current_checksum = self.checksum(path) if ::File.exists?(path)
4848

49-
begin
50-
# The remote filehandle
51-
raw_file = get_from_uri(source) ||
52-
get_from_server(source, current_checksum) ||
53-
get_from_local_cookbook(source)
54-
rescue Net::HTTPRetriableError => e
55-
if e.response.kind_of?(Net::HTTPNotModified)
56-
Chef::Log.debug("File #{path} is unchanged")
57-
return false
58-
else
59-
raise e
49+
unless (@new_resource.checksum && @new_resource.checksum == current_checksum)
50+
begin
51+
# The remote filehandle
52+
raw_file = get_from_uri(source) ||
53+
get_from_server(source, current_checksum) ||
54+
get_from_local_cookbook(source)
55+
rescue Net::HTTPRetriableError => e
56+
if e.response.kind_of?(Net::HTTPNotModified)
57+
Chef::Log.debug("File #{path} is unchanged")
58+
return false
59+
else
60+
raise e
61+
end
6062
end
61-
end
6263

63-
# If the file exists
64-
if ::File.exists?(@new_resource.path)
65-
# And it matches the checksum of the raw file
66-
@new_resource.checksum(self.checksum(raw_file.path))
67-
if @new_resource.checksum != @current_resource.checksum
68-
# Updating target file, let's perform a backup!
69-
Chef::Log.debug("#{@new_resource} changed from #{@current_resource.checksum} to #{@new_resource.checksum}")
70-
Chef::Log.info("Updating #{@new_resource} at #{@new_resource.path}")
71-
backup(@new_resource.path)
64+
# If the file exists
65+
if ::File.exists?(@new_resource.path)
66+
# And it matches the checksum of the raw file
67+
@new_resource.checksum(self.checksum(raw_file.path))
68+
if @new_resource.checksum != @current_resource.checksum
69+
# Updating target file, let's perform a backup!
70+
Chef::Log.debug("#{@new_resource} changed from #{@current_resource.checksum} to #{@new_resource.checksum}")
71+
Chef::Log.info("Updating #{@new_resource} at #{@new_resource.path}")
72+
backup(@new_resource.path)
73+
end
74+
else
75+
# We're creating a new file
76+
Chef::Log.info("Creating #{@new_resource} at #{@new_resource.path}")
7277
end
73-
else
74-
# We're creating a new file
75-
Chef::Log.info("Creating #{@new_resource} at #{@new_resource.path}")
76-
end
7778

78-
FileUtils.cp(raw_file.path, @new_resource.path)
79-
@new_resource.updated = true
80-
79+
FileUtils.cp(raw_file.path, @new_resource.path)
80+
@new_resource.updated = true
81+
82+
# We're done with the file, so make sure to close it if it was open.
83+
raw_file.close
84+
true
85+
end
86+
8187
set_owner if @new_resource.owner
8288
set_group if @new_resource.group
8389
set_mode if @new_resource.mode
8490

85-
# We're done with the file, so make sure to close it if it was open.
86-
raw_file.close
87-
true
8891
end
8992

9093
def get_from_uri(source)

chef/lib/chef/resource/remote_file.rb

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,15 @@ def cookbook(args=nil)
4646
)
4747
end
4848

49+
def checksum(args=nil)
50+
set_or_return(
51+
:checksum,
52+
args,
53+
:kind_of => String
54+
)
55+
end
56+
57+
4958
end
5059
end
5160
end

chef/spec/unit/provider/remote_file_spec.rb

Lines changed: 23 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -65,10 +65,29 @@ def do_remote_file
6565
end
6666

6767
describe "when given a URI source" do
68-
it "should download the file from the remote URL" do
69-
@resource.source("http://opscode.com/seattle.txt")
70-
@rest.should_receive(:get_rest).with("http://opscode.com/seattle.txt", true).and_return(@tempfile)
71-
do_remote_file
68+
describe "and given a checksum" do
69+
it "should not download the file if the checksum matches" do
70+
@resource.checksum("0fd012fdc96e96f8f7cf2046522a54aed0ce470224513e45da6bc1a17a4924aa")
71+
@resource.source("http://opscode.com/seattle.txt")
72+
@rest.should_not_receive(:get_rest).with("http://opscode.com/seattle.txt", true).and_return(@tempfile)
73+
do_remote_file
74+
end
75+
76+
it "should download the file if the checksum matches" do
77+
@resource.checksum("this hash doesn't match")
78+
@resource.source("http://opscode.com/seattle.txt")
79+
@rest.should_receive(:get_rest).with("http://opscode.com/seattle.txt", true).and_return(@tempfile)
80+
do_remote_file
81+
end
82+
end
83+
84+
describe "and not given a checksum" do
85+
it "should download the file from the remote URL" do
86+
@resource.checksum(nil)
87+
@resource.source("http://opscode.com/seattle.txt")
88+
@rest.should_receive(:get_rest).with("http://opscode.com/seattle.txt", true).and_return(@tempfile)
89+
do_remote_file
90+
end
7291
end
7392
end
7493

chef/spec/unit/resource/remote_file_spec.rb

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,5 +49,16 @@
4949
@resource.cookbook.should == nil
5050
end
5151
end
52+
53+
describe "checksum" do
54+
it "should accept a string for the checksum object" do
55+
@resource.checksum "asdf"
56+
@resource.checksum.should eql("asdf")
57+
end
58+
59+
it "should default to nil" do
60+
@resource.checksum.should == nil
61+
end
62+
end
5263

53-
end
64+
end

0 commit comments

Comments
 (0)