diff --git a/libraries/chef_artifact_nexus.rb b/libraries/chef_artifact_nexus.rb index 26dec9a..12cb21c 100644 --- a/libraries/chef_artifact_nexus.rb +++ b/libraries/chef_artifact_nexus.rb @@ -28,7 +28,7 @@ def remote # @return [String] the version number that latest resolves to or the passed in value def get_actual_version(coordinates) artifact = NexusCli::Artifact.new(coordinates) - if Chef::Artifact.latest?(artifact.version) || Chef::Artifact.snapshot?(artifact.version) + if Chef::Artifact.latest?(artifact.version) REXML::Document.new(remote.get_artifact_info(coordinates)).elements["//version"].text else artifact.version @@ -43,7 +43,8 @@ def get_actual_version(coordinates) # @return [Hash] writes a file to disk and returns a Hash with # information about that file. See NexusCli::ArtifactActions#pull_artifact. def retrieve_from_nexus(source, destination_dir) - remote.pull_artifact(source, destination_dir) + data = remote.pull_artifact(source, destination_dir) + return data end # Makes a call to Nexus and parses the returned XML to return @@ -55,16 +56,6 @@ 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 c6b3265..60a8590 100644 --- a/providers/file.rb +++ b/providers/file.rb @@ -19,6 +19,7 @@ # limitations under the License. # require 'chef/mixin/create_path' +require 'fileutils' attr_reader :file_location attr_reader :nexus_configuration @@ -43,7 +44,6 @@ def load_current_resource end end @file_location = new_resource.location - @file_path = new_resource.path @current_resource = Chef::Resource::ArtifactFile.new(@new_resource.name) @current_resource @@ -53,23 +53,15 @@ def load_current_resource retries = new_resource.download_retries begin if Chef::Artifact.from_s3?(file_location) - unless ::File.exists?(new_resource.path) && checksum_valid? - Chef::Artifact.retrieve_from_s3(node, file_location, new_resource.path) + unless ::File.exists?(new_resource.name) && checksum_valid? + Chef::Artifact.retrieve_from_s3(node, file_location, new_resource.name) run_proc :after_download end elsif Chef::Artifact.from_nexus?(file_location) - unless ::File.exists?(new_resource.path) && checksum_valid? && (!Chef::Artifact.snapshot?(file_location) || !Chef::Artifact.latest?(file_location)) + unless ::File.exists?(new_resource.name) && checksum_valid? begin - 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 + data = nexus_connection.retrieve_from_nexus(file_location, ::File.dirname(new_resource.name)) + FileUtils.mv(data[:file_path], new_resource.name) 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." @@ -101,19 +93,14 @@ 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 + return Digest::SHA256.file(new_resource.name).hexdigest == read_checksum end if Chef::Artifact.from_nexus?(file_location) - Digest::SHA1.file(new_resource.path).hexdigest == nexus_connection.get_artifact_sha(file_location) + Digest::SHA1.file(new_resource.name).hexdigest == nexus_connection.get_artifact_sha(file_location) else if new_resource.checksum - Digest::SHA256.file(new_resource.path).hexdigest == new_resource.checksum + Digest::SHA256.file(new_resource.name).hexdigest == new_resource.checksum else Chef::Log.debug "[artifact_file] No checksum provided for artifact_file, assuming checksum is valid." true @@ -127,7 +114,7 @@ def checksum_valid? # # @return [Chef::Resource::RemoteFile] def remote_file_resource - @remote_file_resource ||= remote_file new_resource.path do + @remote_file_resource ||= remote_file new_resource.name do source file_location checksum new_resource.checksum owner new_resource.owner @@ -183,7 +170,7 @@ def cached_checksum_exists? # # @return [NilClass] def write_checksum - ::File.open(cached_checksum, "w") { |file| file.puts Digest::SHA256.file(new_resource.path).hexdigest } + ::File.open(cached_checksum, "w") { |file| file.puts Digest::SHA256.file(new_resource.name).hexdigest } end # Reads the cached_checksum