Skip to content

Fix downloading SNAPSHOT and LATEST artifacts #131

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 3 additions & 12 deletions libraries/chef_artifact_nexus.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why does the behavior of get_actual_version have to change here? This was put in so that SNAPSHOT versions resolve to their unique versions. I put this in so that I could report the actual artifact deployed for traceability purposes. Changing this behavior will definitely break my use case.

REXML::Document.new(remote.get_artifact_info(coordinates)).elements["//version"].text
else
artifact.version
Expand All @@ -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
Expand All @@ -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
35 changes: 11 additions & 24 deletions providers/file.rb
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
# limitations under the License.
#
require 'chef/mixin/create_path'
require 'fileutils'

attr_reader :file_location
attr_reader :nexus_configuration
Expand All @@ -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
Expand All @@ -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."
Expand Down Expand Up @@ -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
Expand All @@ -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
Expand Down Expand Up @@ -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
Expand Down