Skip to content
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

Allow artifact_file to handle snapshot and latest version correctly #125

Closed
wants to merge 31 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
31 commits
Select commit Hold shift + click to select a range
6edd097
wip
Mar 15, 2014
e8c33d6
snapshot and latest checksum check fixed
Mar 15, 2014
4272ff6
ruby syntax
Mar 15, 2014
139b743
ruby syntax
Mar 15, 2014
b7ec58e
loggin message in case of snapshot version
Mar 15, 2014
848d811
loggin message in case of snapshot version
Mar 15, 2014
796ea84
remove already patched file resource
Mar 15, 2014
6416ed1
latest version issue fixed
Mar 15, 2014
9c409bd
using negative index to get artifact version from coordinates
Mar 16, 2014
cc7ae5f
Merge pull request #124 from BarthV/123-fix
KAllan357 Mar 17, 2014
cae8147
wip
Mar 15, 2014
40731b3
snapshot and latest checksum check fixed
Mar 15, 2014
0b9fb2e
ruby syntax
Mar 15, 2014
bab8aba
ruby syntax
Mar 15, 2014
4868658
loggin message in case of snapshot version
Mar 15, 2014
884631b
loggin message in case of snapshot version
Mar 15, 2014
315da93
remove already patched file resource
Mar 15, 2014
aa4aa2a
latest version issue fixed
Mar 15, 2014
9291014
using negative index to get artifact version from coordinates
Mar 16, 2014
29f75e5
Merge branch 'file-handle-snapshot' of https://github.com/BarthV/arti…
Mar 20, 2014
51a6bbd
merge fix #124 for local tests
Mar 20, 2014
3a39ced
LWRP file : new resource attr + handling symlink
Mar 21, 2014
8fad345
comments & readme update
Mar 21, 2014
a656c42
Merge pull request #1 from BarthV/file-symlink
BarthV Mar 21, 2014
924c91a
moved symlink attr presence test
Mar 21, 2014
d9eece7
def fix
Mar 21, 2014
14ae606
doc fix
Mar 21, 2014
1277b40
Merge pull request #2 from BarthV/file-symlink
BarthV Mar 21, 2014
5bc48b4
dest_file from nexus reworked
Mar 21, 2014
7e5aeab
log info updated - FINAL version
Mar 21, 2014
c5e5457
fix issue - file is OK but snapshot is deleted
Mar 21, 2014
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
14 changes: 14 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,7 @@ location | The location to the artifact file. Either a nexus ident
checksum | The SHA256 checksum for verifying URL downloads. Not used when location is Nexus | String |
owner | Owner of the downloaded file | String |
group | Group of the downloaded file | String |
symlink | If specified (absolute path), creates a symlink pointing to the downloaded artifact | String |
after_download | A proc containing resources to be executed only if the artifact has been downloaded | Proc |
download_retries | The number of times to attempt to download the file if it fails its integrity check | Integer | 1

Expand Down Expand Up @@ -446,6 +447,19 @@ Your data_bag can contain both ```nexus``` and ```aws``` configuration.
resource. For example, when force is true, you can also change the value of owner and group to remap the deployed artifact to
a new permissions scheme.

##### Using artifact_file to download lastest artifact from Nexus

artifact_file "/tmp/my-artifact.jar" do
location "com.test:my-artifact:tgz:LATEST"
owner "me"
group "mes"
symlink "/tmp/my-artifact-LATEST.jar"
action :create
end

In this case, LWRP can't guess artifact filename during runtime, so we are not able to know the final artifactname.
Adding a symlink to the file allow to always have a static file pointing to latest downloaded version.

##### Using artifact_file to download a file from an S3 bucket

artifact_file "/tmp/my-artifact.tgz" do
Expand Down
10 changes: 10 additions & 0 deletions libraries/chef_artifact_nexus.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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 (with ext) for the given artifact.
#
# @param coordinates [String] a colon-separated Maven identifier that represents the artifact
#
# @return [String] the filename 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
23 changes: 23 additions & 0 deletions providers/file.rb
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,16 @@ def load_current_resource

@nexus_configuration = new_resource.nexus_configuration
@nexus_connection = Chef::Artifact::Nexus.new(node, nexus_configuration)

if Chef::Artifact.snapshot?(new_resource.location.split(':')[-1]) || Chef::Artifact.latest?(new_resource.location.split(':')[-1])
Copy link
Contributor

Choose a reason for hiding this comment

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

I think we should come up with a way to refactor this so that we don't have to do this splitting logic. I think adding a new snapshot? and latest? to Chef::Artifact::Nexus would do the trick - see https://github.com/RiotGames/artifact-cookbook/blob/master/libraries/chef_artifact_nexus.rb#L30-L31 for reference.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

ok, I'll work on this and think about adding new snapshot/latest method directly at Nexus object level.

Chef::Log.info "#{new_resource.name} is a rolling/snapshot version - filename is retrieved from metadata, existing name is ignored"
dest_filepath = ::File.join(
::File.dirname(new_resource.path),
@nexus_connection.get_artifact_filename(new_resource.location)
)
new_resource.path(dest_filepath)
end

elsif Chef::Artifact.from_s3?(@new_resource.location)
chef_gem "aws-sdk" do
version "1.29.0"
Expand Down Expand Up @@ -71,6 +81,7 @@ def load_current_resource
remote_file_resource.run_action(:create)
end
raise Chef::Artifact::ArtifactChecksumError unless checksum_valid?
create_symlink if new_resource.symlink
write_checksum if Chef::Artifact.from_nexus?(file_location) || Chef::Artifact.from_s3?(file_location)
rescue Chef::Artifact::ArtifactChecksumError => e
if retries > 0
Expand Down Expand Up @@ -178,3 +189,15 @@ def write_checksum
def read_checksum
::File.read(cached_checksum).strip
end

# Manage a symlink pointing to artifact downloaded file
# Symlink attr should specify absolute symlink path
#
# @return [NilClass] the created path
def create_symlink
link new_resource.symlink do
to new_resource.path
owner new_resource.owner
group new_resource.group
end
end
1 change: 1 addition & 0 deletions resources/file.rb
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
attribute :checksum, :kind_of => String
attribute :owner, :kind_of => String, :required => true, :regex => Chef::Config[:user_valid_regex]
attribute :group, :kind_of => String, :required => true, :regex => Chef::Config[:user_valid_regex]
attribute :symlink, :kind_of => String
attribute :download_retries, :kind_of => Integer, :default => 1
attribute :after_download, :kind_of => Proc
attribute :nexus_configuration, :kind_of => Chef::Artifact::NexusConfiguration, :default => Chef::Artifact::NexusConfiguration.from_data_bag