summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--lib/chef/provider/file.rb13
-rw-r--r--lib/chef/provider/remote_file.rb22
-rw-r--r--lib/chef/provider/remote_file/http.rb9
3 files changed, 29 insertions, 15 deletions
diff --git a/lib/chef/provider/file.rb b/lib/chef/provider/file.rb
index 79c9ea8a9b..2cfc36c822 100644
--- a/lib/chef/provider/file.rb
+++ b/lib/chef/provider/file.rb
@@ -61,7 +61,10 @@ class Chef
@current_resource ||= Chef::Resource::File.new(@new_resource.name)
@new_resource.path.gsub!(/\\/, "/") # for Windows
@current_resource.path(@new_resource.path)
- if ::File.exists?(@current_resource.path)
+ if ::File.exists?(@current_resource.path) && !::File.directory?(@current_resource.path)
+ if @action != :create_if_missing
+ @current_resource.checksum(checksum(@current_resource.path))
+ end
load_resource_attributes_from_file(@current_resource)
end
@current_resource
@@ -188,6 +191,7 @@ class Chef
backup unless file_created?
deployment_strategy.deploy(tempfile.path, @new_resource.path)
Chef::Log.info("#{@new_resource} updated file contents #{@new_resource.path}")
+ @new_resource.checksum(@new_resource.path) # for reporting
end
def do_contents_changes
@@ -232,13 +236,6 @@ class Chef
end
def load_resource_attributes_from_file(resource)
- if resource.respond_to?(:checksum)
- if ::File.exists?(resource.path) && !::File.directory?(resource.path)
- if @action != :create_if_missing # XXX: don't we break current_resource semantics by skipping this?
- resource.checksum(checksum(resource.path))
- end
- end
- end
if Chef::Platform.windows?
# TODO: To work around CHEF-3554, add support for Windows
diff --git a/lib/chef/provider/remote_file.rb b/lib/chef/provider/remote_file.rb
index c3d98a27e7..fedd7ec5fd 100644
--- a/lib/chef/provider/remote_file.rb
+++ b/lib/chef/provider/remote_file.rb
@@ -31,11 +31,18 @@ class Chef
def load_current_resource
@current_resource = Chef::Resource::RemoteFile.new(@new_resource.name)
super
- fileinfo = load_fileinfo
- if fileinfo && fileinfo["checksum"] == @current_resource.checksum
- @current_resource.etag fileinfo["etag"]
- @current_resource.last_modified fileinfo["last_modified"]
- @current_resource.source fileinfo["src"]
+ unless fileinfo.nil? || fileinfo["checksum"].nil? || fileinfo["checksum"] == ""
+ Chef::Log.debug("found cached file information at #{Chef::Config[:file_cache_path]}/remote_file/#{new_resource.name}")
+ if fileinfo["checksum"] == @current_resource.checksum
+ @current_resource.etag fileinfo["etag"]
+ @current_resource.last_modified fileinfo["last_modified"]
+ @current_resource.source fileinfo["src"]
+ Chef::Log.debug("loaded etag %s, last_modified %s, source %s from file metadata cache" % [fileinfo["etag"], fileinfo["last_modified"], fileinfo["src"]])
+ else
+ Chef::Log.debug("checksum of current resource does not match cached checksum, not loading metadata...")
+ end
+ else
+ Chef::Log.debug("no cached file information found")
end
end
@@ -50,8 +57,8 @@ class Chef
save_fileinfo(@content.raw_file_source)
end
- def load_fileinfo
- begin
+ def fileinfo
+ @fileinfo ||= begin
Chef::JSONCompat.from_json(Chef::FileCache.load("remote_file/#{new_resource.name}"))
rescue Chef::Exceptions::FileNotFound
nil
@@ -66,6 +73,7 @@ class Chef
cache["checksum"] = @new_resource.checksum
cache_path = new_resource.name.sub(/^([A-Za-z]:)/, "") # strip drive letter on Windows
Chef::FileCache.store("remote_file/#{cache_path}", cache.to_json)
+ Chef::Log.debug("stored etag '%s', last_modified '%s', checksum '%s' for source '%s' into %s" % [cache["etag"], cache["last_modified"], cache["checksum"], source, "#{Chef::Config[:file_cache_path]}/remote_file/#{cache_path}"] )
end
end
end
diff --git a/lib/chef/provider/remote_file/http.rb b/lib/chef/provider/remote_file/http.rb
index af3c620f94..dc80da776d 100644
--- a/lib/chef/provider/remote_file/http.rb
+++ b/lib/chef/provider/remote_file/http.rb
@@ -35,9 +35,11 @@ class Chef
if current_resource.source && Chef::Provider::RemoteFile::Util.uri_matches_string?(uri, current_resource.source[0])
if current_resource.use_etag && current_resource.etag
@headers['if-none-match'] = "\"#{current_resource.etag}\""
+ Chef::Log.debug("set if-none-match header to #{current_resource.etag}")
end
if current_resource.use_last_modified && current_resource.last_modified
@headers['if-modified-since'] = current_resource.last_modified.strftime("%a, %d %b %Y %H:%M:%S %Z")
+ Chef::Log.debug("set if-modified-since header to #{@headers['if-modified-since']}")
end
end
@uri = uri
@@ -49,18 +51,24 @@ class Chef
tempfile = rest.streaming_request(@uri, @headers)
if rest.last_response['last_modified']
mtime = Time.parse(rest.last_response['last_modified'])
+ Chef::Log.debug("found last_modified header on response set to #{mtime}")
elsif rest.last_response['date']
mtime = Time.parse(rest.last_response['date'])
+ Chef::Log.debug("found date header on response set to #{mtime}")
else
mtime = Time.now
+ Chef::Log.debug("returning current time #{mtime} as last_modified time")
end
if rest.last_response['etag']
etag = rest.last_response['etag']
+ Chef::Log.debug("found etag header on response set to #{etag}")
else
etag = nil
+ Chef::Log.debug("did not find an etag header on response")
end
rescue Net::HTTPRetriableError => e
if e.response.is_a? Net::HTTPNotModified
+ Chef::Log.debug("got 304 HTTPNotModified response")
tempfile = nil
else
raise e
@@ -82,6 +90,7 @@ class Chef
# case you'd end up with a tar archive (no gzip) named, e.g., foo.tgz,
# which is not what you wanted.
if @uri.to_s =~ /gz$/
+ Chef::Log.debug("turning gzip compression off due to filename ending in gz")
opts[:disable_gzip] = true
end
opts