Skip to content

Instantly share code, notes, and snippets.

@sbates
Last active December 10, 2015 08:19
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save sbates/4407228 to your computer and use it in GitHub Desktop.
Save sbates/4407228 to your computer and use it in GitHub Desktop.
using the artifactory plugin for generating sha256 checksums, I then wrote some provider code to figure it out on the fly. This keeps chef from having to download all the artifacts first and calculate their checksums; this really speeds up chef runs on slow network days and cuts down on the number of things downloading from our Artifactory server.
# Once we've made our custom calls to artifactory, we can use the built-in chef resource for remote_file to actually fetch the artifact
def download(remote_file_url)
# construct the filename and destination for remote file
remote_file_name = "#{new_resource.name}-#{new_resource.version}.#{new_resource.filetype}"
remote_file_destination = new_resource.artifact_target || ::File.join(Chef::Config[:file_cache_path], remote_file_name)
# construct the remote_file source
sha256_cksum = get_checksum(remote_file_url, "sha256")
Chef::Log.debug("checking sha256 checksum for #{new_resource.name}:#{sha256_cksum.inspect}")
# Use the remote_file resource to go fetch the artifact now that we know the url AND the checksum
f = Chef::Resource::RemoteFile.new(remote_file_destination, run_context)
f.checksum sha256_cksum
f.source remote_file_url
f.run_action :create
f.retries 3
# If the file is new or updated, set this resource to updated so that notifications will happen
new_resource.updated_by_last_action(true) if f.updated_by_last_action?
end
# to get the checksum, input the whole artifact url and the type of checksum you want, as a string (sha-256, sha-1, md5)
def get_checksum(art_url,cksum)
cksum_url = [art_url,cksum].join(".")
call_artifactory(cksum_url, @auth)
end
def call_artifactory(url, auth)
RestClient.proxy = Chef::Config["https_proxy"] unless Chef::Config["https_proxy"].nil?
headers = ({"AUTHORIZATION" => "Basic #{Base64.encode64(auth)}"})
RestClient.get url, headers
end
remote_file "#{Chef::Config['file_cache_path']/solr.war" do
source "some_remote_artifactory_url"
checksum "c002b7......"
end
# OR THIS or you might use the http_request resource. It's a little funky though
url = "https//user:password@myhost.com/artifactory/path/to/file"
cksum_rest = Chef::REST.new("#{url}.sha256", nil, nil)
cksum = cksum_rest.get_rest(cksum_url)
remote_file url do
source url
checksum cksum
end
@sbates
Copy link
Author

sbates commented Dec 29, 2012

Test, the cksum bit looks like this:
This block tested looks like this:
chef:recipe > x = cksum.get_rest(cksum_url)
[2012-12-29T09:08:53-06:00] WARN: Expected JSON response, but got content-type 'application/x-gzip'
=> "21d157bd25e3f1818364a6c8a29b5e09fcd8aad6020b283d1912524cc7f0d9da"
chef:recipe > x
=> "21d157bd25e3f1818364a6c8a29b5e09fcd8aad6020b283d1912524cc7f0d9da"

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment