diff --git a/libraries/chef_artifact_nexus.rb b/libraries/chef_artifact_nexus.rb index 182477a..26dec9a 100644 --- a/libraries/chef_artifact_nexus.rb +++ b/libraries/chef_artifact_nexus.rb @@ -55,6 +55,16 @@ def retrieve_from_nexus(source, destination_dir) def get_artifact_sha(coordinates) REXML::Document.new(remote.get_artifact_info(coordinates)).elements["//sha1"].text end + + # Makes a call to Nexus and parses the returned XML to return + # the Nexus Server's stored filename for the given artifact. + # + # @param coordinates [String] a colon-separated Maven identifier that represents the artifact + # + # @return [String] the filename entry for the artifact + def get_artifact_filename(coordinates) + ::File.basename(REXML::Document.new(remote.get_artifact_info(coordinates)).elements["//repositoryPath"].text) + end end end end diff --git a/providers/file.rb b/providers/file.rb index baeecb2..c6b3265 100644 --- a/providers/file.rb +++ b/providers/file.rb @@ -58,9 +58,18 @@ def load_current_resource run_proc :after_download end elsif Chef::Artifact.from_nexus?(file_location) - unless ::File.exists?(new_resource.path) && checksum_valid? + unless ::File.exists?(new_resource.path) && checksum_valid? && (!Chef::Artifact.snapshot?(file_location) || !Chef::Artifact.latest?(file_location)) begin - nexus_connection.retrieve_from_nexus(file_location, ::File.dirname(new_resource.path)) + if ::File.exists?(new_resource.path) + if Digest::SHA1.file(new_resource.path).hexdigest != nexus_connection.get_artifact_sha(file_location) + nexus_connection.retrieve_from_nexus(file_location, ::File.dirname(new_resource.path)) + end + else + nexus_connection.retrieve_from_nexus(file_location, ::File.dirname(new_resource.path)) + end + if nexus_connection.get_artifact_filename(file_location) != ::File.basename(new_resource.path) + ::File.rename(::File.join(::File.dirname(new_resource.path), nexus_connection.get_artifact_filename(file_location)), new_resource.path) + end run_proc :after_download rescue NexusCli::PermissionsException => e msg = "The artifact server returned 401 (Unauthorized) when attempting to retrieve this artifact. Confirm that your credentials are correct." @@ -92,6 +101,11 @@ def checksum_valid? require 'digest' if cached_checksum_exists? + if Chef::Artifact.from_nexus?(file_location) + if Chef::Artifact.snapshot?(file_location) || Chef::Artifact.latest?(file_location) + return Digest::SHA1.file(new_resource.path).hexdigest == nexus_connection.get_artifact_sha(file_location) + end + end return Digest::SHA256.file(new_resource.path).hexdigest == read_checksum end