Last active
February 5, 2018 19:18
-
-
Save phwelo/73100b2c5d9758b84e9b0f56488046b1 to your computer and use it in GitHub Desktop.
[Artifactory for Chef] Takes identifying information and identifies release artifacts #Ruby #Chef #Artifactory
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/ruby | |
require 'rest-client' | |
require 'json' | |
require 'uri' | |
artifactory_base_uri = 'http://artifactory.llamadev.local:8081/artifactory/' | |
artifactory_search_path = 'api/search/prop' | |
artifactory_search_uri = artifactory_base_uri + artifactory_search_path | |
# returns an array of our package values for the specified environment | |
def package_list(uri,environment) | |
# Verified that RestClient provides good errors on connection issues | |
package_request = RestClient.get uri, :params => {:'deploy.environments' => environment} | |
package_json = JSON.parse(package_request)['results'] | |
package_list = Array.new | |
# Loop through each item in the object and output an array | |
package_json.each do |package| | |
package_list.push(package['uri']) | |
end | |
# return list of packages for specified environment | |
return package_list | |
end | |
# returns a string containing id for package whose url was specified | |
def get_item_id(uri) | |
prop_request = RestClient.get "#{uri}?properties=nuget.id" | |
prop_json = JSON.parse(prop_request)['properties']['nuget.id'] | |
# returns only the ID of the item targeted | |
return prop_json | |
end | |
# returns hash containing sha1 and md5 checksums. Not sure which if any I'll need at this point | |
def get_item_checksums(uri) | |
prop_request = RestClient.get uri | |
prop_json = JSON.parse(prop_request)['checksums'] | |
return prop_json | |
end | |
# since the API returns API links for the items we need to dive in and get the download uri | |
def get_download_uri(uri) | |
dl_request = RestClient.get uri | |
dl_json = JSON.parse(dl_request)['downloadUri'] | |
# returns a string holding the download uri of package | |
return dl_json | |
end | |
def buildhash(search_uri,environment,service) | |
packages = package_list(search_uri,environment) | |
packages.each do |package| | |
service_id = get_item_id(package) | |
if service_id.to_s.casecmp service.to_s | |
svc_hash = Hash.new | |
svc_hash["id"] = service_id | |
svc_hash["checksums"] = get_item_checksums(package) | |
svc_hash["download_uri"] = get_download_uri(package) | |
return svc_hash | |
end | |
end | |
end | |
end_hash = buildhash(artifactory_search_uri, 'LLCP-QA', 'CIDevAM' ) | |
puts end_hash% |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
required gems: rest-client