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

After download callback for artifact_package #107

Merged
merged 10 commits into from
Oct 31, 2013
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@ migrate | A proc containing resources to be executed during t
after_migrate | A proc containing resources to be executed after the migration Proc | Proc |
restart | A proc containing resources to be executed at the end of a successful deploy | Proc |
after_deploy | A proc containing resources to be executed after the deploy process ends | Proc |
after_download | A proc containing resources to be executed only if the artifact has been downloaded | Proc |
remove_top_level_directory | Deletes a top level directory from the extracted zip file | Boolean | false
skip_manifest_check | Skips the manifest check for idempotency when the version attribute is not changing | Boolean | false
remove_on_force | Removes the current version directory contents when force is set | Boolean | false
Expand Down Expand Up @@ -132,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 |
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

### Downloading files using artifact_file
Expand Down Expand Up @@ -168,6 +170,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 |
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
20 changes: 17 additions & 3 deletions Vagrantfile
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
VERSION = ENV["artifact_test_version"] || "1.2.3"
LOCATION = ENV["artifact_test_location"] || "/vagrant/fixtures/artifact_test-#{VERSION}.tgz"
VERSION = ENV["artifact_test_version"] || "1.2.3"
LOCATION = ENV["artifact_test_location"] || "/vagrant/fixtures/artifact_test-#{VERSION}.tgz"
OTHER_NEXUS_URL = ENV["artifact_test_other_nexus_url"] || "https://maven-us.nuxeo.org/nexus"
OTHER_NEXUS_REPO = ENV["artifact_test_other_nexus_repo"] || "central"
OTHER_NEXUS_LOCATION = ENV["artifact_test_other_nexus_location"] || "org.mortbay.jetty.dist:jetty-rpm:rpm:8.1.13.v20130916"
OTHER_NEXUS_APP_NAME = ENV["artifact_test_other_nexus_app_name"] || "jetty-hightide-server"
OTHER_NEXUS_RPM_NAME = ENV["artifact_test_other_nexus_rpm_name"] || "jetty-rpm-8.1.13.v20130916.rpm"

Vagrant.configure("2") do |config|

Expand All @@ -15,6 +20,13 @@ Vagrant.configure("2") do |config|
config.vm.provision :chef_solo do |chef|
chef.json = {
:artifact_test => {
:other_nexus => {
:url => OTHER_NEXUS_URL,
:repository => OTHER_NEXUS_REPO,
:app_name => OTHER_NEXUS_APP_NAME,
:location => OTHER_NEXUS_LOCATION,
:rpm_name => OTHER_NEXUS_RPM_NAME
},
:version => VERSION,
:location => LOCATION
}
Expand All @@ -23,7 +35,9 @@ Vagrant.configure("2") do |config|
chef.data_bags_path = "./fixtures/databags/"

chef.run_list = [
"recipe[artifact_test::default]"
"recipe[artifact_test::default]",
"recipe[artifact_test::nexus_package]",
"recipe[artifact_test::nexus_anon]"
]
end
end
20 changes: 13 additions & 7 deletions fixtures/artifact_test/recipes/nexus_anon.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
# Recipe:: nexus_anon
#
# Author:: Kyle Allan (<[email protected]>)
#
#
# Copyright 2013, Riot Games
#
# Licensed under the Apache License, Version 2.0 (the "License");
Expand All @@ -26,14 +26,20 @@
node[:artifact_test][:other_nexus][:url], node[:artifact_test][:other_nexus][:repository]
)

artifact_deploy "artifact_test" do
version node[:artifact_test][:version]
artifact_location node[:artifact_test][:location]
artifact_checksum node[:artifact_test][:checksum]
location_parts = node[:artifact_test][:other_nexus][:location].split(":")
version = location_parts[-1]
type = location_parts[-2]
# notice: replacing the extension and adding classifier
location = node[:artifact_test][:other_nexus][:location].gsub(":#{type}:#{version}", ":jar:sources")
deploy_to = "/srv/" + node[:artifact_test][:other_nexus][:app_name]

artifact_deploy deploy_to do
after_download Proc.new { Chef::Log.info "*** artifact_deploy after_download was called ***" }
version version
artifact_location location
nexus_configuration nexus_configuration
deploy_to node[:artifact_test][:deploy_to]
deploy_to deploy_to
owner "artifacts"
group "artifact"

action :deploy
end
59 changes: 59 additions & 0 deletions fixtures/artifact_test/recipes/nexus_package.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
#
# Cookbook Name:: artifact_test
# Recipe:: nexus_package
#
# Author:: Aaron Feng (<[email protected]>)
#
# Copyright 2013, Riot Games
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#

group "artifact"
user "artifacts"

nexus_configuration = Chef::Artifact::NexusConfiguration.new(
node[:artifact_test][:other_nexus][:url], node[:artifact_test][:other_nexus][:repository]
)

after_download_prc = Proc.new {
Chef::Log.info "*** after download proc was executed! ***"
}

# make sure it works without after_download proc
artifact_package "without after download proc" do
location node[:artifact_test][:other_nexus][:location]
nexus_configuration nexus_configuration
owner "artifacts"
group "artifact"
action :install
end

rpm_package node[:artifact_test][:other_nexus][:app_name] do
action :remove
end

file "/var/chef/cache/artifact_packages/#{node[:artifact_test][:other_nexus][:rpm_name]}" do
action :delete
end

# make sure it works with after_download proc
artifact_package "with after download proc" do
nexus_configuration nexus_configuration
after_download after_download_prc
location node[:artifact_test][:other_nexus][:location]
owner "artifacts"
group "artifact"
action :install
end

8 changes: 4 additions & 4 deletions libraries/chef_artifact_nexus.rb
Original file line number Diff line number Diff line change
Expand Up @@ -27,16 +27,16 @@ def remote
#
# @return [String] the version number that latest resolves to or the passed in value
def get_actual_version(coordinates)
version = coordinates.split(':')[3]
if Chef::Artifact.latest?(version)
artifact = NexusCli::Artifact.new(coordinates)
if Chef::Artifact.latest?(artifact.version)
REXML::Document.new(remote.get_artifact_info(coordinates)).elements["//version"].text
else
version
artifact.version
end
end

# Downloads a file to disk from the configured Nexus server.
#
#
# @param source [String] a colon-separated Maven identified string that represents the artifact
# @param destination_dir [String] a path to download the artifact to
#
Expand Down
26 changes: 26 additions & 0 deletions libraries/chef_helpers.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
class Chef
module Artifact
module Helpers
# A wrapper that adds debug logging for running a recipe_eval on the
# numerous Proc attributes defined for this resource.
#
# @param from [String] location where the proc is executed from
# @param resource [Chef::Resource::ArtifactDeploy] resource being executed on
Copy link
Contributor

Choose a reason for hiding this comment

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

Maybe just Chef::Resource here

# @param name [Symbol] the name of the proc to execute
#
# @return [void]
def execute_run_proc(from, resource, name)
Copy link
Contributor

Choose a reason for hiding this comment

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

Is it possible to remove the from parameter and use #{__FILE__} to get us that extra bit of logging?

It might make it cleaner to have this method be called run_proc and then remove the private methods on the other classes.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I don't think you can if you want to preserve the same style of logging before.

proc = resource.send(name)
proc_name = name.to_s
Chef::Log.debug "#{from}[run_proc::#{proc_name}] Determining whether to execute #{proc_name} proc."
if proc
Chef::Log.debug "#{from}[run_proc::#{proc_name}] Beginning execution of #{proc_name} proc."
recipe_eval(&proc)
Chef::Log.debug "#{from}[run_proc::#{proc_name}] Ending execution of #{proc_name} proc."
else
Chef::Log.debug "#{from}[run_proc::#{proc_name}] Skipping execution of #{proc_name} proc because it was not defined."
end
end
end
end
end
Loading