From 26b8ae178f2ab8cb8e748f242d14c5cec9481978 Mon Sep 17 00:00:00 2001 From: Barthelemy Vessemont Date: Tue, 25 Mar 2014 17:44:28 +0100 Subject: [PATCH 1/4] handling snapshot & latest Nexus Tag + renaming target file --- libraries/chef_artifact_nexus.rb | 28 ++++++++++++++++++++++++++++ providers/file.rb | 18 ++++++++++++++++-- 2 files changed, 44 insertions(+), 2 deletions(-) diff --git a/libraries/chef_artifact_nexus.rb b/libraries/chef_artifact_nexus.rb index 182477a..8c83e27 100644 --- a/libraries/chef_artifact_nexus.rb +++ b/libraries/chef_artifact_nexus.rb @@ -55,6 +55,34 @@ 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 + + # Returns true when the coordinates specifies a snapshot version. + # + # @param location [String] the artifact_location + # + # @return [Boolean] true when the coordinates's version is a snapshot. + def snapshot?(coordinates) + coordinates.split(':')[-1].include?('-SNAPSHOT') + end + + # Returns true when the coordinates specifies latest version. + # + # @param location [String] the artifact_location + # + # @return [Boolean] true when the coordinates's version is latest. + def latest?(coordinates) + coordinates.split(':')[-1].casecmp('LATEST') == 0 + end end end end diff --git a/providers/file.rb b/providers/file.rb index baeecb2..0f85954 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? && (!nexus_connection.snapshot?(file_location) || !nexus_connection.latest?(file_location)) begin - nexus_connection.retrieve_from_nexus(file_location, ::File.dirname(new_resource.path)) + if ::File.exists?(new_resource.path) + if Digest::SHA256.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) != 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 nexus_connection.snapshot?(file_location) || nexus_connection.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 From 1e72bf974d726550601f4819533f196218ed2d6e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Barth=C3=A9lemy=20Vessemont?= Date: Tue, 25 Mar 2014 18:34:12 +0100 Subject: [PATCH 2/4] fix renaming condition (using basename) --- providers/file.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/providers/file.rb b/providers/file.rb index 0f85954..eea9185 100644 --- a/providers/file.rb +++ b/providers/file.rb @@ -67,7 +67,7 @@ def load_current_resource else nexus_connection.retrieve_from_nexus(file_location, ::File.dirname(new_resource.path)) end - if nexus_connection.get_artifact_filename(file_location) != new_resource.path + 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 From 662557894ecbdcf23fe049e69c9c8573b8694d9c Mon Sep 17 00:00:00 2001 From: Barthelemy Vessemont Date: Wed, 26 Mar 2014 10:59:02 +0100 Subject: [PATCH 3/4] Now using Artifact snap & latest methods + fix SHA1 test --- libraries/chef_artifact_nexus.rb | 18 ------------------ providers/file.rb | 6 +++--- 2 files changed, 3 insertions(+), 21 deletions(-) diff --git a/libraries/chef_artifact_nexus.rb b/libraries/chef_artifact_nexus.rb index 8c83e27..26dec9a 100644 --- a/libraries/chef_artifact_nexus.rb +++ b/libraries/chef_artifact_nexus.rb @@ -65,24 +65,6 @@ def get_artifact_sha(coordinates) def get_artifact_filename(coordinates) ::File.basename(REXML::Document.new(remote.get_artifact_info(coordinates)).elements["//repositoryPath"].text) end - - # Returns true when the coordinates specifies a snapshot version. - # - # @param location [String] the artifact_location - # - # @return [Boolean] true when the coordinates's version is a snapshot. - def snapshot?(coordinates) - coordinates.split(':')[-1].include?('-SNAPSHOT') - end - - # Returns true when the coordinates specifies latest version. - # - # @param location [String] the artifact_location - # - # @return [Boolean] true when the coordinates's version is latest. - def latest?(coordinates) - coordinates.split(':')[-1].casecmp('LATEST') == 0 - end end end end diff --git a/providers/file.rb b/providers/file.rb index 0f85954..2a43c71 100644 --- a/providers/file.rb +++ b/providers/file.rb @@ -58,10 +58,10 @@ def load_current_resource run_proc :after_download end elsif Chef::Artifact.from_nexus?(file_location) - unless ::File.exists?(new_resource.path) && checksum_valid? && (!nexus_connection.snapshot?(file_location) || !nexus_connection.latest?(file_location)) + unless ::File.exists?(new_resource.path) && checksum_valid? && (!Chef::Artifact.snapshot?(file_location) || !Chef::Artifact.latest?(file_location)) begin if ::File.exists?(new_resource.path) - if Digest::SHA256.file(new_resource.path).hexdigest != nexus_connection.get_artifact_sha(file_location) + 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 @@ -102,7 +102,7 @@ def checksum_valid? if cached_checksum_exists? if Chef::Artifact.from_nexus?(file_location) - if nexus_connection.snapshot?(file_location) || nexus_connection.latest?(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 From ea1659437295ce71dccd0c1476b16e8781dbf737 Mon Sep 17 00:00:00 2001 From: Barthelemy Vessemont Date: Wed, 26 Mar 2014 11:11:39 +0100 Subject: [PATCH 4/4] Now using Artifact snap & latest methods + fix SHA1 test --- providers/file.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/providers/file.rb b/providers/file.rb index cad1f8c..c6b3265 100644 --- a/providers/file.rb +++ b/providers/file.rb @@ -67,7 +67,7 @@ def load_current_resource 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) + 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